Welcome to little lamb

Code » gtkscrollbar » commit 7b51353

Add scrollbar example

author Olivier Brunel
2023-06-17 17:33:47 UTC
committer Olivier Brunel
2023-06-28 16:14:18 UTC
parent 074381c126fb1e17dc7d995bbe439f379e9f90cf

Add scrollbar example

Just trying to get a scrollbar that can only work in increments multiple
of 1, i.e. no 0.5 or anything. But also have the stepper still work!

meta/deps/gtk3/get_cflags +2 -0
meta/deps/gtk3/get_libs +2 -0
meta/deps/gtk3/get_version +2 -0
meta/deps/gtk3/library +1 -0
meta/deps/gtk3/version +1 -0
meta/deps/limb/configure +0 -1
meta/deps/limb/git +0 -1
meta/deps/limb/incdir +0 -1
meta/deps/limb/version +0 -1
meta/desc +7 -1
meta/doc +0 -1
meta/site +0 -1
src/doc/gtkscrollbar.1.md +0 -44
meta/deps/limb/files => src/gtkscrollbar/+gtk3 +0 -0
src/gtkscrollbar/+limb +0 -0
src/gtkscrollbar/gtkscrollbar.c +85 -78

diff --git a/meta/deps/gtk3/get_cflags b/meta/deps/gtk3/get_cflags
new file mode 100755
index 0000000..a7a8b73
--- /dev/null
+++ b/meta/deps/gtk3/get_cflags
@@ -0,0 +1,2 @@
+#!/bin/sh
+pkg-config --cflags gtk+-3.0
diff --git a/meta/deps/gtk3/get_libs b/meta/deps/gtk3/get_libs
new file mode 100755
index 0000000..548aefd
--- /dev/null
+++ b/meta/deps/gtk3/get_libs
@@ -0,0 +1,2 @@
+#!/bin/sh
+pkg-config --libs gtk+-3.0
diff --git a/meta/deps/gtk3/get_version b/meta/deps/gtk3/get_version
new file mode 100755
index 0000000..b76a077
--- /dev/null
+++ b/meta/deps/gtk3/get_version
@@ -0,0 +1,2 @@
+#!/bin/sh
+pkg-config --modversion gtk+-3.0
diff --git a/meta/deps/gtk3/library b/meta/deps/gtk3/library
new file mode 100644
index 0000000..b119202
--- /dev/null
+++ b/meta/deps/gtk3/library
@@ -0,0 +1 @@
+gtk-3
diff --git a/meta/deps/gtk3/version b/meta/deps/gtk3/version
new file mode 100644
index 0000000..91437dd
--- /dev/null
+++ b/meta/deps/gtk3/version
@@ -0,0 +1 @@
+3.18.9
diff --git a/meta/deps/limb/configure b/meta/deps/limb/configure
deleted file mode 100644
index 1661455..0000000
--- a/meta/deps/limb/configure
+++ /dev/null
@@ -1 +0,0 @@
---no-doc --no-shared
diff --git a/meta/deps/limb/git b/meta/deps/limb/git
deleted file mode 100644
index 109fd23..0000000
--- a/meta/deps/limb/git
+++ /dev/null
@@ -1 +0,0 @@
-git://lila.oss/limb.git
diff --git a/meta/deps/limb/incdir b/meta/deps/limb/incdir
deleted file mode 100644
index 30b990f..0000000
--- a/meta/deps/limb/incdir
+++ /dev/null
@@ -1 +0,0 @@
-src/liblimb/include
diff --git a/meta/deps/limb/version b/meta/deps/limb/version
deleted file mode 100644
index 8acdd82..0000000
--- a/meta/deps/limb/version
+++ /dev/null
@@ -1 +0,0 @@
-0.0.1
diff --git a/meta/desc b/meta/desc
index 5c370b9..ff110ab 100644
--- a/meta/desc
+++ b/meta/desc
@@ -1 +1,7 @@
-FIXME: desc here
+Example of trying to use GtkScrollbar
+
+Just trying to use a GtkScrollbar with specific steps (full integers value only)
+and yet have steppers working as expected.
+
+More info can be found here :
+https://lila.oss/blog/2023-06-19-any-gtk-wizard-out-there-who-knows-how-to-control-a-scrollbar
diff --git a/meta/doc b/meta/doc
deleted file mode 100644
index 8e177c5..0000000
--- a/meta/doc
+++ /dev/null
@@ -1 +0,0 @@
-https://lila.oss/doc/gtkscrollbar
diff --git a/meta/site b/meta/site
deleted file mode 100644
index 5c8e5ea..0000000
--- a/meta/site
+++ /dev/null
@@ -1 +0,0 @@
-https://lila.oss/gtkscrollbar
diff --git a/src/doc/gtkscrollbar.1.md b/src/doc/gtkscrollbar.1.md
deleted file mode 100644
index 40bc023..0000000
--- a/src/doc/gtkscrollbar.1.md
+++ /dev/null
@@ -1,44 +0,0 @@
-% gtkscrollbar
-% gtkscrollbar(1)
-
-# NAME
-
-gtkscrollbar - FIXME: desc here
-
-# SYNOPSIS
-
-*gtkscrollbar* [*-q*] [*-v*] [*--debug* [[@[\`level\`]:]+\`FD\`|\`FILE\`]] [*-O* [@[\`level\`]:]+\`FD\`|\`FILE\`]
-
-# OPTIONS
-
-: *--debug* [[@[\`level\`]:]+\`FD\`|\`FILE\`]
-<inc autoopt_debug.md>
-<inc autoopt_logdbg_level.md>
-
-<inc opt_help.md>
-
-: *-O*, *--log-file* [@[\`level\`]:]+\`FD\`|\`FILE\`
-<inc autoopt_log.md>
-<inc autoopt_logdbg_level.md>
-
-: *-q*, *--quiet*
-<inc autoopt_quiet.md>
-
-: *-v*, *--verbose*
-<inc autoopt_verbose.md>
-
-<inc opt_version.md>
-
-# DESCRIPTION
-
-*gtkscrollbar* is...
-
-<inc bugs.md>
-
-# REPOSITORY
-
-You can find the latest about *gtkscrollbar* from its official site at https://lila.oss/gtkscrollbar
-Latest source code can be browsed online at https://lila.oss/code/gtkscrollbar; or you can clone its
-[git](1) repo from git://lila.oss/gtkscrollbar.git
-
-<inc author.md>
diff --git a/meta/deps/limb/files b/src/gtkscrollbar/+gtk3
similarity index 100%
rename from meta/deps/limb/files
rename to src/gtkscrollbar/+gtk3
diff --git a/src/gtkscrollbar/+limb b/src/gtkscrollbar/+limb
deleted file mode 100644
index e69de29..0000000
diff --git a/src/gtkscrollbar/gtkscrollbar.c b/src/gtkscrollbar/gtkscrollbar.c
index 5f0fa4c..ba39db5 100644
--- a/src/gtkscrollbar/gtkscrollbar.c
+++ b/src/gtkscrollbar/gtkscrollbar.c
@@ -1,95 +1,102 @@
 /* This file is part of gtkscrollbar           https://lila.oss/gtkscrollbar
  * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
 /* SPDX-License-Identifier: GPL-2.0-only */
-#include <fcntl.h> /* AT_FDCWD */
-#include <limb/autoopt.h>
-#include <limb/exitcode.h>
-#include <limb/loadopt.h>
-#include <limb/output.h>
-#include <limb/stralloc.h>
-#include "config.h"
-
-const char *PROG = "gtkscrollbar";
+#include <math.h>
+#include <gtk/gtk.h>
 
 struct ctx {
-    stralloc sa;
-    size_t fileoff;
+    GtkLabel *lbl;
+    gdouble last[2];
 };
 
-enum {
-    OPTID_VERSION = OPTID_FIRST,
-    OPTID_DEBUG,
-    /* arguments */
-    ARGID_FILE,
-};
+static gboolean
+sb_changed(GtkRange *range, GtkScrollType scroll, gdouble val, struct ctx *ctx)
+{
+    gdouble oval, fval;
+
+    oval = gtk_range_get_value(range);
+    fval = floor(val + 0.5);
+
+    if ((ctx->last[0] && ctx->last[1] && ctx->last[0] == ctx->last[1] && val == ctx->last[0])
+            && oval == fval) {
+        if (val > fval)
+            fval = oval + 1;
+        else
+            fval = oval - 1;
+        ctx->last[1] = fval;
+    } else {
+        ctx->last[0] = ctx->last[1];
+        ctx->last[1] = val;
+    }
+
+    gtk_range_set_value(range, fval);
+    return TRUE;
+}
 
 static void
-parse_cmdline(int argc, const char *argv[], int dirfd, const char *confdir, struct ctx *ctx)
+sb_scrolled(GtkAdjustment *adj, struct ctx *ctx)
 {
-    const char usage[] = "[-h] [OPTION..] FILE";
-    const struct option options[] = {
-        OPTION_ARG_OPT(  0 , "debug",                   OPT_SKIP,   OPTID_DEBUG),
-        OPTION_ARG_NONE('h', "help",                    OPT_SKIP,   OPTID_SHORTOPT),
-        OPTION_ARG_REQ( 'O', "log-file",                0,          OPTID_SHORTOPT),
-        OPTION_ARG_NONE('q', "quiet",                   0,          OPTID_SHORTOPT),
-        OPTION_ARG_NONE('v', "verbose",                 0,          OPTID_SHORTOPT),
-        OPTION_ARG_NONE( 0 , "version",                 OPT_SKIP,   OPTID_VERSION),
-        LOADOPT_ARGUMENTS,
-        ARGUMENT_REQ( "file",                           OPT_PATH,   ARGID_FILE),
-        LOADOPT_DONE
-    };
-    struct loadopt lo = LOADOPT_ZERO;
-
-    int c;
-    while ((c = loadopt(&ctx->sa, argc, argv, options, dirfd, confdir, 0, &lo))) switch (c) {
-        case OPTID_DEBUG:
-            if (!autoopt_debug(&options[LO_IDX(&lo)], LO_ARG(&lo)))
-                dieusage(EX_USAGE, usage);
-            break;
-        case 'h':
-            diehelp(0, usage,
-"     --debug [[@[level]:]+FD|FILE]     Enable debug output (to FD|FILE)\n"
-" -O, --output [@[level]:]+FD|FILE      Set output log to FD|FILE\n"
-"\n"
-" -q, --quiet                           Enable quiet mode\n"
-" -v, --verbose                         Enable verbose mode\n"
-"\n"
-" -h, --help                            Show this help screen and exit\n"
-"     --version                         Show version information and exit\n"
-);
-        case 'O':
-            if (!autoopt_log(&options[LO_IDX(&lo)], LO_ARG(&lo)))
-                dieusage(EX_USAGE, usage);
-            break;
-        case 'q':
-            autoopt_quiet(&options[LO_IDX(&lo)], LO_ARG(&lo));
-            break;
-        case 'v':
-            autoopt_verbose(&options[LO_IDX(&lo)], LO_ARG(&lo));
-            break;
-        case OPTID_VERSION:
-            dieversion(GTKSCROLLBAR_VERSION, "2023", GTKSCROLLBAR_CURYEAR, GTKSCROLLBAR_AUTHOR, GTKSCROLLBAR_URL, NULL);
-
-        case ARGID_FILE:
-            ctx->fileoff = LO_OFF(&lo);
-            break;
-
-        case -1:
-            dieusage(EX_USAGE, usage);
-        default:
-            die(EX_SOFTWARE, "unexpected return value ", PMINT(c), " from loadopt");
-    };
+    gdouble v = gtk_adjustment_get_value(adj);
+    gchar *s = g_strdup_printf("v=%f", v);
+    gtk_label_set_text(ctx->lbl, s);
+    g_free(s);
+}
+
+static void
+setup_scrollbar(GtkRange *range, struct ctx *ctx)
+{
+    GtkAdjustment *adj;
+
+    g_signal_connect(range, "change-value", G_CALLBACK(sb_changed), ctx);
+    adj = gtk_range_get_adjustment(range);
+
+    gtk_adjustment_configure(adj,
+                             0,         /* value */
+                             0, 10,     /* min, max */
+                             1, 2,      /* step, page */
+                             3          /* page size */
+                            );
+
+#if 0
+    gtk_range_set_round_digits(range, 0);
+#endif
+
+    g_signal_connect(adj, "value-changed", G_CALLBACK(sb_scrolled), ctx);
+}
+
+static void
+on_activate(GtkApplication *app, struct ctx *ctx)
+{
+    GtkContainer *cont;
+    GtkWidget *w_window, *w_sb;
+
+    w_window = gtk_application_window_new(app);
+    gtk_window_set_default_size(GTK_WINDOW(w_window), 400, -1);
+
+    cont = GTK_CONTAINER(gtk_box_new(GTK_ORIENTATION_VERTICAL, 4));
+    gtk_container_add(GTK_CONTAINER(w_window), GTK_WIDGET(cont));
+
+    ctx->lbl = GTK_LABEL(gtk_label_new("0"));
+    gtk_container_add(cont, GTK_WIDGET(ctx->lbl));
+
+    w_sb = gtk_scrollbar_new(GTK_ORIENTATION_HORIZONTAL, NULL);
+    gtk_container_add(cont, w_sb);
+
+    setup_scrollbar(GTK_RANGE(w_sb), ctx);
+
+    gtk_widget_show_all(w_window);
 }
 
 int
-main(int argc, const char *argv[])
+main(int argc, char *argv[])
 {
-    struct ctx ctx = { STRALLOC_ZERO, 0 };
-    const char confdir[] = "/etc/gtkscrollbar/conf";
-    parse_cmdline(argc, argv, AT_FDCWD, confdir, &ctx);
+    struct ctx ctx = { 0 };
+    GtkApplication *app;
 
-    out("file is ", ESC, ctx.sa.s + ctx.fileoff, ESC);
+    app = gtk_application_new("org.gtk.example", G_APPLICATION_FLAGS_NONE);
+    g_signal_connect(app, "activate", G_CALLBACK(on_activate), &ctx);
+    int r = g_application_run(G_APPLICATION(app), argc, argv);
 
-    stralloc_free(&ctx.sa);
-    return 0;
+    g_object_unref(app);
+    return r;
 }