ui/gtk/range.c

changeset 0
804d8803eade
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/gtk/range.c	Wed Dec 09 11:32:01 2020 +0100
@@ -0,0 +1,131 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2017 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "range.h"
+#include "container.h"
+#include <ucx/mempool.h>
+#include "../common/context.h"
+#include "../common/object.h"
+
+
+static UIWIDGET ui_scrollbar(UiObject *obj, UiOrientation orientation, UiRange *range, ui_callback f, void *userdata) {
+#ifdef UI_GTK3
+    GtkWidget *scrollbar = gtk_scrollbar_new(orientation == UI_HORIZONTAL ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL, NULL);
+#else
+    GtkWidget *scrollbar;
+    if(orientation == UI_HORIZONTAL) {
+        scrollbar = gtk_hscrollbar_new(NULL);
+    } else {
+        scrollbar = gtk_hscrollbar_new(NULL);
+    }
+#endif
+    
+    if(range) {
+        range->get = ui_scrollbar_get;
+        range->set = ui_scrollbar_set;
+        range->setrange = ui_scrollbar_setrange;
+        range->setextent = ui_scrollbar_setextent;
+        range->obj = scrollbar;
+    }
+    
+    if(f) {
+        UiEventData *event = malloc(sizeof(UiEventData));
+        event->obj = obj;
+        event->userdata = userdata;
+        event->callback = f;
+        event->value = 0;
+        
+        g_signal_connect(
+                G_OBJECT(scrollbar),
+                "value-changed",
+                G_CALLBACK(ui_scrollbar_value_changed),
+                event);
+        g_signal_connect(
+                scrollbar,
+                "destroy",
+                G_CALLBACK(ui_destroy_userdata),
+                event);
+    }
+    
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(ct, scrollbar, FALSE);
+    
+    return scrollbar;
+}
+
+UIWIDGET ui_hscrollbar(UiObject *obj, UiRange *range, ui_callback f, void *userdata) {
+    return ui_scrollbar(obj, UI_HORIZONTAL, range, f, userdata);
+}
+
+UIWIDGET ui_vscrollbar(UiObject *obj, UiRange *range, ui_callback f, void *userdata) {
+    return ui_scrollbar(obj, UI_VERTICAL, range, f, userdata);
+}
+
+gboolean ui_scrollbar_value_changed(GtkRange *range, UiEventData *event) {
+    UiEvent e;
+    e.obj = event->obj;
+    e.window = event->obj->window;
+    e.document = event->obj->ctx->document;
+    e.eventdata = NULL;
+    e.intval = event->value;
+    event->callback(&e, event->userdata); 
+    return TRUE;
+}
+
+double ui_scrollbar_get(UiRange *range) {
+    double value = gtk_range_get_value(GTK_RANGE(range->obj));
+    range->value = value;
+    return value;
+}
+
+void   ui_scrollbar_set(UiRange *range, double value) {
+    gtk_range_set_value(GTK_RANGE(range->obj), value);
+    range->value = value;
+}
+
+void   ui_scrollbar_setrange(UiRange *range, double min, double max) {
+    gtk_range_set_range(GTK_RANGE(range->obj), min, max);
+    range->min = min;
+    range->max = max;
+}
+
+void   ui_scrollbar_setextent(UiRange *range, double extent) {
+    GtkAdjustment *a = gtk_range_get_adjustment(GTK_RANGE(range->obj));
+#ifdef UI_GTK2LEGACY
+    a->page_size = extent;
+#else
+    gtk_adjustment_set_page_size(a, extent);
+#endif
+#if !(GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 18)
+    gtk_adjustment_changed(a);
+#endif
+    range->extent = extent;
+}

mercurial