make gtk4 port compilable newapi

Tue, 17 Sep 2024 22:00:07 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 17 Sep 2024 22:00:07 +0200
branch
newapi
changeset 299
48763a9d19a7
parent 298
c5e207d01ff2
child 300
2f924f7ca901

make gtk4 port compilable

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/draw_cairo.c file | annotate | diff | comparison | revisions
ui/gtk/graphics.c file | annotate | diff | comparison | revisions
ui/gtk/image.c file | annotate | diff | comparison | revisions
ui/gtk/range.c file | annotate | diff | comparison | revisions
ui/gtk/tree.c file | annotate | diff | comparison | revisions
--- a/application/main.c	Mon Sep 16 22:09:32 2024 +0200
+++ b/application/main.c	Tue Sep 17 22:00:07 2024 +0200
@@ -142,11 +142,13 @@
         ui_combobox(obj, .hexpand = true, .vexpand = false, .colspan = 2, .varname = "list", .getvalue = list_getvalue);
         ui_newline(obj);
         
+        /*
         ui_hbox0(obj) {
             ui_radiobutton(obj, .label = "Radio 1", .varname = "radio");
             ui_radiobutton(obj, .label = "Radio 2", .varname = "radio");
             ui_radiobutton(obj, .label = "Radio 3", .varname = "radio");
         }
+        */
     }
     
     ui_show(obj);
--- a/ui/gtk/draw_cairo.c	Mon Sep 16 22:09:32 2024 +0200
+++ b/ui/gtk/draw_cairo.c	Tue Sep 17 22:00:07 2024 +0200
@@ -33,17 +33,19 @@
 
 #include "draw_cairo.h"
 
-#if UI_GTK3 || UI_GTK4
-gboolean ui_drawingarea_expose(GtkWidget *w, cairo_t *cr, void *data) {
+
+#if GTK_MAJOR_VERSION >= 3
+static void ui_drawingarea_draw(
+        GtkDrawingArea *area,
+        cairo_t *cr,
+        int width,
+        int height,
+        gpointer data)
+{
     UiCairoGraphics g;
-#ifdef UI_GTK4
-    g.g.width = gtk_widget_get_width(w);
-    g.g.height = gtk_widget_get_height(w);
-#else
-    g.g.width = gtk_widget_get_allocated_width(w);
-    g.g.height = gtk_widget_get_allocated_height(w);
-#endif
-    g.widget = w;
+    g.g.width = width;
+    g.g.height = height;
+    g.widget = GTK_WIDGET(area);
     g.cr = cr;
     
     UiDrawEvent *event = data;
@@ -53,7 +55,14 @@
     ev.document = event->obj->ctx->document;
     
     event->callback(&ev, &g.g, event->userdata);
-    
+}
+#endif
+
+#if UI_GTK3
+gboolean ui_drawingarea_expose(GtkWidget *w, cairo_t *cr, void *data) {
+    int width = gtk_widget_get_allocated_width(w);
+    int height = gtk_widget_get_allocated_height(w);
+    ui_drawingarea_draw(w, cr, width, height, data);
     return FALSE;
 }
 #endif
@@ -80,7 +89,9 @@
 // function from graphics.h
 
 void ui_connect_draw_handler(GtkWidget *widget, UiDrawEvent *event) {
-#if UI_GTK3 || UI_GTK4
+#if GTK_MAJOR_VERSION >= 4
+    gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(widget), ui_drawingarea_draw, event, NULL);
+#elif GTK_MAJOR_VERSION == 3
     g_signal_connect(G_OBJECT(widget),
             "draw",
             G_CALLBACK(ui_drawingarea_expose),
--- a/ui/gtk/graphics.c	Mon Sep 16 22:09:32 2024 +0200
+++ b/ui/gtk/graphics.c	Tue Sep 17 22:00:07 2024 +0200
@@ -51,6 +51,7 @@
 }
 
 
+#if GTK_MAJOR_VERSION <= 3
 static gboolean widget_button_pressed(
         GtkWidget *widget,
         GdkEvent *event,
@@ -82,14 +83,18 @@
     }
     return TRUE;
 }
+#endif
 
 void ui_drawingarea_getsize(UIWIDGET drawingarea, int *width, int *height) {
-#ifdef UI_GTK3
-        *width = gtk_widget_get_allocated_width(drawingarea);
-        *height = gtk_widget_get_allocated_height(drawingarea);
+#if GTK_MAJOR_VERSION >= 4
+    *width = gtk_widget_get_width(drawingarea);
+    *height = gtk_widget_get_height(drawingarea);
+#elif GTK_MAJOR_VERSION == 3
+    *width = gtk_widget_get_allocated_width(drawingarea);
+    *height = gtk_widget_get_allocated_height(drawingarea);
 #else
-        *width = drawingarea->allocation.width;
-        *height = drawingarea->allocation.height;
+    *width = drawingarea->allocation.width;
+    *height = drawingarea->allocation.height;
 #endif
 }
 
@@ -98,6 +103,9 @@
 }
 
 void ui_drawingarea_mousehandler(UiObject *obj, UIWIDGET widget, ui_callback f, void *u) {
+#if GTK_MAJOR_VERSION >= 4
+    // TODO
+#else
     gtk_widget_set_events(widget, GDK_BUTTON_PRESS_MASK);
     if(f) {
         UiEventData *event = malloc(sizeof(UiEventData));
@@ -114,6 +122,7 @@
     } else {
          // TODO: warning
     }
+#endif
 }
 
 
--- a/ui/gtk/image.c	Mon Sep 16 22:09:32 2024 +0200
+++ b/ui/gtk/image.c	Tue Sep 17 22:00:07 2024 +0200
@@ -39,10 +39,16 @@
 
 static GtkIconTheme *icon_theme;
 
+#if GTK_MAJOR_VERSION >= 4
+#define ICONTHEME_GET_DEFAULT() gtk_icon_theme_get_for_display(gdk_display_get_default())
+#else
+#define ICONTHEME_GET_DEFAULT() gtk_icon_theme_get_default()
+#endif
+
 void ui_image_init(void) {
     image_map = cxHashMapCreateSimple(CX_STORE_POINTERS);
     
-    icon_theme = gtk_icon_theme_get_default();
+    icon_theme = ICONTHEME_GET_DEFAULT();
 }
 
 // **** deprecated functions ****
@@ -63,7 +69,7 @@
 
 static UiIcon* get_icon(const char *name, int size, int scale) {
 #if GTK_MAJOR_VERSION >= 4
-    GtkIconPaintable *info = gtk_icon_theme_lookup_icon(icon_theme, NULL, size, scale, GTK_TEXT_DIR_LTR, GTK_ICON_LOOKUP_FORCE_REGULAR);
+    GtkIconPaintable *info = gtk_icon_theme_lookup_icon(icon_theme, name, NULL, size, scale, GTK_TEXT_DIR_LTR, GTK_ICON_LOOKUP_FORCE_REGULAR);
 #elif defined(UI_SUPPORTS_SCALE)
     GtkIconInfo *info = gtk_icon_theme_lookup_icon_for_scale(icon_theme, name, size, scale, 0);
 #else
@@ -118,6 +124,11 @@
     return get_icon(name, size, 1);
 }
 
+#if GTK_MAJOR_VERSION >= 4
+GdkPixbuf* ui_icon_pixbuf(UiIcon *icon) {
+    return NULL; // TODO
+}
+#else
 GdkPixbuf* ui_icon_pixbuf(UiIcon *icon) {
     if(!icon->pixbuf) {
         GError *error = NULL;
@@ -125,7 +136,9 @@
     }
     return icon->pixbuf;
 }
+#endif
 
+/*
 UiImage* ui_icon_image(UiIcon *icon) {
     GError *error = NULL;
     GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error);
@@ -136,6 +149,7 @@
     }
     return NULL;
 }
+*/
 
 /*
 UiImage* ui_image(const char *filename) {
--- a/ui/gtk/range.c	Mon Sep 16 22:09:32 2024 +0200
+++ b/ui/gtk/range.c	Tue Sep 17 22:00:07 2024 +0200
@@ -36,7 +36,7 @@
 
 
 static UIWIDGET ui_scrollbar(UiObject *obj, UiOrientation orientation, UiRange *range, ui_callback f, void *userdata) {
-#ifdef UI_GTK3
+#if GTK_MAJOR_VERSION >= 3
     GtkWidget *scrollbar = gtk_scrollbar_new(orientation == UI_HORIZONTAL ? GTK_ORIENTATION_HORIZONTAL : GTK_ORIENTATION_VERTICAL, NULL);
 #else
     GtkWidget *scrollbar;
@@ -124,7 +124,7 @@
 #else
     gtk_adjustment_set_page_size(a, extent);
 #endif
-#if !(GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 18)
+#if GTK_MAJOR_VERSION * 100 + GTK_MIMOR_VERSION < 318
     gtk_adjustment_changed(a);
 #endif
     range->extent = extent;
--- a/ui/gtk/tree.c	Mon Sep 16 22:09:32 2024 +0200
+++ b/ui/gtk/tree.c	Tue Sep 17 22:00:07 2024 +0200
@@ -95,6 +95,9 @@
                     case UI_ICON: {
                         g_value_init(&value, G_TYPE_OBJECT);
                         UiIcon *icon = data;
+#if GTK_MAJOR_VERSION >= 4
+                        g_value_set_object(&value, icon->info); // TODO: does this work?
+#else
                         if(!icon->pixbuf && icon->info) {
                             GError *error = NULL;
                             GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error);
@@ -104,14 +107,20 @@
                         if(icon->pixbuf) {
                             g_value_set_object(&value, icon->pixbuf);
                         }
-
-                        
+#endif
                         break;
                     }
                     case UI_ICON_TEXT:
                     case UI_ICON_TEXT_FREE: {
+                        UiIcon *icon = data;
+                        char *str = model->getvalue(elm, c);
+#if GTK_MAJOR_VERSION >= 4
+                        GValue iconvalue = G_VALUE_INIT;
+                        g_value_init(&iconvalue, G_TYPE_OBJECT);
+                        g_value_set_object(&iconvalue, icon->info);
+                        gtk_list_store_set_value(store, &iter, c, &iconvalue);
+#else
                         GValue pixbufvalue = G_VALUE_INIT;
-                        UiIcon *icon = data;
                         if(!icon->pixbuf && icon->info) {
                             GError *error = NULL;
                             GdkPixbuf *pixbuf = gtk_icon_info_load_icon(icon->info, &error);
@@ -120,9 +129,8 @@
                         g_value_init(&pixbufvalue, G_TYPE_OBJECT);
                         g_value_set_object(&pixbufvalue, icon->pixbuf);
                         gtk_list_store_set_value(store, &iter, c, &pixbufvalue);
+#endif
                         c++;
-                        
-                        char *str = model->getvalue(elm, c);
                         g_value_init(&value, G_TYPE_STRING);
                         g_value_set_string(&value, str);
                         if(model->types[i] == UI_ICON_TEXT_FREE) {
@@ -205,12 +213,12 @@
     }
     
     // add widget to the current container
-    GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL);
+    GtkWidget *scroll_area = SCROLLEDWINDOW_NEW();
     gtk_scrolled_window_set_policy(
             GTK_SCROLLED_WINDOW(scroll_area),
             GTK_POLICY_AUTOMATIC,
             GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS  
-    gtk_container_add(GTK_CONTAINER(scroll_area), view);
+    SCROLLEDWINDOW_SET_CHILD(scroll_area, view);
     
     UI_APPLY_LAYOUT1(current, args);
     current->container->add(current->container, scroll_area, FALSE);
@@ -222,6 +230,7 @@
     return scroll_area;
 }
 
+/*
 static void drag_begin(GtkWidget *widget, GdkDragContext *context, gpointer udata) {
     printf("drag begin\n");
     
@@ -236,13 +245,16 @@
     printf("drag end\n");
     
 }
+*/
 
+/*
 static GtkTargetEntry targetentries[] =
     {
       { "STRING",        0, 0 },
       { "text/plain",    0, 1 },
       { "text/uri-list", 0, 2 },
     };
+*/
 
 UIWIDGET ui_table_create(UiObject *obj, UiListArgs args) {
     UiObject* current = uic_current_obj(obj);
@@ -356,12 +368,12 @@
     gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
     
     // add widget to the current container
-    GtkWidget *scroll_area = gtk_scrolled_window_new(NULL, NULL);
+    GtkWidget *scroll_area = SCROLLEDWINDOW_NEW();
     gtk_scrolled_window_set_policy(
             GTK_SCROLLED_WINDOW(scroll_area),
             GTK_POLICY_AUTOMATIC,
             GTK_POLICY_AUTOMATIC); // GTK_POLICY_ALWAYS  
-    gtk_container_add(GTK_CONTAINER(scroll_area), view);
+    SCROLLEDWINDOW_SET_CHILD(scroll_area, view);
     
     UI_APPLY_LAYOUT1(current, args);
     current->container->add(current->container, scroll_area, FALSE);
@@ -375,11 +387,7 @@
 
 
 GtkWidget* ui_get_tree_widget(UIWIDGET widget) {
-    GList *c = gtk_container_get_children(GTK_CONTAINER(widget));
-    if(c) {
-        return c->data;
-    }
-    return NULL;
+    return SCROLLEDWINDOW_GET_CHILD(widget);
 }
 
 static char** targets2array(char *target0, va_list ap, int *nelm) {
@@ -402,6 +410,7 @@
     return targets;
 }
 
+/*
 static GtkTargetEntry* targetstr2gtktargets(char **str, int nelm) {
     GtkTargetEntry *targets = calloc(nelm, sizeof(GtkTargetEntry));
     for(int i=0;i<nelm;i++) {
@@ -409,6 +418,7 @@
     }
     return targets;
 }
+*/
 
 void ui_table_dragsource(UIWIDGET tablewidget, int actions, char *target0, ...) { 
     va_list ap;

mercurial