add imgviewer button press/release callbacks (GTK)

3 days ago

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 02 Apr 2025 20:31:04 +0200 (3 days ago)
changeset 547
631162b266d1
parent 546
5b00f1e55f4e
child 548
198756ab2845

add imgviewer button press/release callbacks (GTK)

ui/gtk/image.c file | annotate | diff | comparison | revisions
ui/gtk/image.h file | annotate | diff | comparison | revisions
ui/ui/image.h file | annotate | diff | comparison | revisions
--- a/ui/gtk/image.c	Wed Apr 02 19:48:45 2025 +0200
+++ b/ui/gtk/image.c	Wed Apr 02 20:31:04 2025 +0200
@@ -88,6 +88,11 @@
     
     UiImageViewer *imgviewer = malloc(sizeof(UiImageViewer));
     memset(imgviewer, 0, sizeof(UiImageViewer));
+    imgviewer->obj = obj;
+    imgviewer->onbuttonpress = args.onbuttonpress;
+    imgviewer->onbuttonpressdata = args.onbuttonpressdata;
+    imgviewer->onbuttonrelease = args.onbuttonrelease;
+    imgviewer->onbuttonreleasedata = args.onbuttonreleasedata;
     if(args.image_padding > 0) {
         imgviewer->padding_left = args.image_padding;
         imgviewer->padding_right = args.image_padding;
@@ -144,6 +149,11 @@
     g_signal_connect(drag, "drag-update", G_CALLBACK(ui_imageviewer_drag_update_cb), imgviewer);
     gtk_widget_add_controller(GTK_WIDGET(drawingarea), GTK_EVENT_CONTROLLER(drag));
     
+    GtkGesture *click = gtk_gesture_click_new();
+    g_signal_connect(click, "pressed", G_CALLBACK(ui_imageviewer_pressed_cb), imgviewer);
+    g_signal_connect(click, "released", G_CALLBACK(ui_imageviewer_released_cb), imgviewer);
+    gtk_widget_add_controller(GTK_WIDGET(drawingarea), GTK_EVENT_CONTROLLER(click));
+    
 #elif GTK_MAJOR_VERSION == 3
     g_signal_connect(
             drawingarea,
@@ -351,7 +361,7 @@
 }
 
 void ui_imageviewer_drag_begin_cb(
-        GtkGestureDrag* self,
+        GtkGestureDrag *self,
         gdouble start_x,
         gdouble start_y,
         gpointer userdata)
@@ -371,7 +381,7 @@
 }
 
 void ui_imageviewer_drag_update_cb(
-        GtkGestureDrag* self,
+        GtkGestureDrag *self,
         gdouble x,
         gdouble y,
         gpointer userdata)
@@ -384,6 +394,48 @@
     }
 }
 
+static void imgviewer_button_event(
+        GtkGestureClick *gesture,
+        UiImageViewer *imgviewer,
+        ui_callback callback,
+        void *userdata)
+{
+    UiEvent event;
+    event.obj = imgviewer->obj;
+    event.window = event.obj->window;
+    event.document = event.obj->ctx->document;
+    event.eventdata = NULL;
+    event.intval = gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(gesture));
+    event.set = 0;
+    callback(&event, userdata);
+}
+
+void ui_imageviewer_pressed_cb(
+        GtkGestureClick *self,
+        gint n_press,
+        gdouble x,
+        gdouble y,
+        gpointer userdata)
+{
+    UiImageViewer *imgviewer = userdata;
+    if(imgviewer->onbuttonpress) {
+        imgviewer_button_event(self, imgviewer, imgviewer->onbuttonpress, imgviewer->onbuttonpressdata);
+    }
+}
+
+void ui_imageviewer_released_cb(
+        GtkGestureClick *self,
+        gint n_press,
+        gdouble x,
+        gdouble y,
+        gpointer userdata)
+{
+    UiImageViewer *imgviewer = userdata;
+    if(imgviewer->onbuttonrelease) {
+        imgviewer_button_event(self, imgviewer, imgviewer->onbuttonrelease, imgviewer->onbuttonreleasedata);
+    }
+}
+
 #else
 
 gboolean ui_imageviewer_scroll_event(
@@ -391,7 +443,7 @@
         GdkEventScroll event,
         gpointer userdata)
 {
-    printf("scroll event\n");
+    // TODO
     return FALSE;
 }
 
@@ -400,7 +452,8 @@
         GdkEventButton event,
         gpointer userdata)
 {
-    printf("button pressed\n");
+    // TODO
+    return FALSE;
 }
 
 gboolean ui_imageviewer_button_release_event(
@@ -408,7 +461,8 @@
         GdkEventButton event,
         gpointer userdata)
 {
-    printf("button released\n");
+    // TODO
+    return FALSE;
 }
 
 #endif
--- a/ui/gtk/image.h	Wed Apr 02 19:48:45 2025 +0200
+++ b/ui/gtk/image.h	Wed Apr 02 20:31:04 2025 +0200
@@ -37,6 +37,7 @@
 #endif
 
 typedef struct UiImageViewer {
+    UiObject *obj;
     GtkWidget *widget;
     UiVar *var;
     int padding_left;
@@ -56,6 +57,11 @@
     UiBool isautoscaled;
     double user_scale;
     double scale;
+    
+    ui_callback onbuttonpress;
+    void *onbuttonpressdata;
+    ui_callback onbuttonrelease;
+    void *onbuttonreleasedata;
 } UiImageViewer;
 
 void ui_cairo_draw_image(UiImageViewer *imgviewer, cairo_t *cr, int width, int height);
@@ -90,6 +96,20 @@
         gdouble y,
         gpointer userdata);
 
+void ui_imageviewer_pressed_cb(
+        GtkGestureClick *self,
+        gint n_press,
+        gdouble x,
+        gdouble y,
+        gpointer userdata);
+
+void ui_imageviewer_released_cb(
+        GtkGestureClick *self,
+        gint n_press,
+        gdouble x,
+        gdouble y,
+        gpointer userdata);
+
 #else
 
 gboolean ui_imageviewer_scroll_event(
--- a/ui/ui/image.h	Wed Apr 02 19:48:45 2025 +0200
+++ b/ui/ui/image.h	Wed Apr 02 20:31:04 2025 +0200
@@ -61,6 +61,11 @@
     UiGeneric *value;
     const char *varname;
     UiMenuBuilder *contextmenu;
+    
+    ui_callback onbuttonpress;
+    void *onbuttonpressdata;
+    ui_callback onbuttonrelease;
+    void *onbuttonreleasedata;
 } UiImageViewerArgs;
     
 #define ui_imageviewer(obj, ...) ui_imageviewer_create(obj, (UiImageViewerArgs){ __VA_ARGS__ } )

mercurial