add more gtk4 porting (incomplete) newapi

Sun, 15 Sep 2024 20:19:52 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 15 Sep 2024 20:19:52 +0200
branch
newapi
changeset 296
a362c76dbf99
parent 295
35d6e9b5d4b3
child 297
a73c60adf188

add more gtk4 porting (incomplete)

ui/gtk/image.c file | annotate | diff | comparison | revisions
ui/gtk/image.h file | annotate | diff | comparison | revisions
ui/gtk/menu.h file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.h file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
--- a/ui/gtk/image.c	Mon Jun 17 21:22:38 2024 +0200
+++ b/ui/gtk/image.c	Sun Sep 15 20:19:52 2024 +0200
@@ -62,7 +62,9 @@
 // **** deprecated2****
 
 static UiIcon* get_icon(const char *name, int size, int scale) {
-#ifdef UI_SUPPORTS_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);
+#elif defined(UI_SUPPORTS_SCALE)
     GtkIconInfo *info = gtk_icon_theme_lookup_icon_for_scale(icon_theme, name, size, scale, 0);
 #else
     GtkIconInfo *info = gtk_icon_theme_lookup_icon(icon_theme, name, size, 0);
--- a/ui/gtk/image.h	Mon Jun 17 21:22:38 2024 +0200
+++ b/ui/gtk/image.h	Sun Sep 15 20:19:52 2024 +0200
@@ -41,7 +41,11 @@
 
     
 struct UiIcon {
+#if GTK_MAJOR_VERSION >= 4
+    GtkIconPaintable *info;
+#else
     GtkIconInfo *info;
+#endif
     GdkPixbuf *pixbuf;
 };
 
--- a/ui/gtk/menu.h	Mon Jun 17 21:22:38 2024 +0200
+++ b/ui/gtk/menu.h	Sun Sep 15 20:19:52 2024 +0200
@@ -34,6 +34,8 @@
 #include <cx/list.h>
 #include "toolkit.h"
 
+#if UI_GTK2 || UI_GTK3
+
 #ifdef	__cplusplus
 extern "C" {
 #endif
@@ -77,5 +79,7 @@
 }
 #endif
 
+#endif /* UI_GTK2 || UI_GTK3 */
+
 #endif	/* MENU_H */
 
--- a/ui/gtk/toolkit.c	Mon Jun 17 21:22:38 2024 +0200
+++ b/ui/gtk/toolkit.c	Sun Sep 15 20:19:52 2024 +0200
@@ -67,22 +67,24 @@
 static int scale_factor = 1;
 
 UIEXPORT void ui_init(const char *appname, int argc, char **argv) {
+    application_name = appname;
     uic_init_global_context();
     
+#if GTK_MAJOR_VERSION >= 4
+    gtk_init();
+#else
     gtk_init(&argc, &argv);
-    application_name = appname;
+#endif
     
     ui_css_init();
-    
     uic_docmgr_init();
-    
     uic_toolbar_init();
-    
     ui_image_init();
-    
     uic_load_app_properties();
     
-#ifdef UI_SUPPORTS_SCALE
+#if GTK_MAJOR_VERSION >= 4
+    scale_factor = 1; // TODO
+#elif defined(UI_SUPPORTS_SCALE)
     scale_factor = gdk_monitor_get_scale_factor(
             gdk_display_get_primary_monitor(gdk_display_get_default()));
 #endif
@@ -159,11 +161,16 @@
 
 void ui_show(UiObject *obj) {
     uic_check_group_widgets(obj->ctx);
+#if GTK_MAJOR_VERSION >= 4
+    gtk_window_present(GTK_WINDOW(obj->widget));
+#elif GTK_MAJOR_VERSION <= 3
     gtk_widget_show_all(obj->widget);
+#endif
 }
 
 void ui_close(UiObject *obj) {
-    gtk_widget_destroy(obj->widget);
+    // TODO
+    //gtk_widget_destroy(obj->widget);
 }
 
 
@@ -226,24 +233,38 @@
 }
 
 void ui_set_show_all(UIWIDGET widget, int value) {
+    // TODO: gtk4
+#if GTK_MAJOR_VERSION <= 3
     gtk_widget_set_no_show_all(widget, !value);
+#endif
 }
 
 void ui_set_visible(UIWIDGET widget, int visible) {
+    // TODO: gtk4
+#if GTK_MAJOR_VERSION <= 3
     if(visible) {
         gtk_widget_set_no_show_all(widget, FALSE);
         gtk_widget_show_all(widget);
     } else {
         gtk_widget_hide(widget);
     }
+#endif
 }
 
 void ui_clipboard_set(char *str) {
+#if GTK_MAJOR_VERSION >= 4
+    // TODO: gtk4: needs widget
+#else
     GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
     gtk_clipboard_set_text(cb, str, strlen(str));
+#endif
 }
 
 char* ui_clipboard_get() {
+#if GTK_MAJOR_VERSION >= 4
+    // TODO: gtk4: needs widget
+    return NULL;
+#else
     GtkClipboard *cb = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
     char *str = gtk_clipboard_wait_for_text(cb);
     if(str) {
@@ -253,6 +274,7 @@
     } else {
         return NULL;
     }
+#endif
 }
 
 int ui_get_scalefactor() {
--- a/ui/gtk/toolkit.h	Mon Jun 17 21:22:38 2024 +0200
+++ b/ui/gtk/toolkit.h	Sun Sep 15 20:19:52 2024 +0200
@@ -38,6 +38,21 @@
 #endif
     
 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
+    
+    
+#if GTK_MAJOR_VERSION >= 4
+#define WINDOW_SHOW(window) gtk_window_present(GTK_WINDOW(window))
+#define WINDOW_DESTROY(window) gtk_window_destroy(GTK_WINDOW(window))
+#define WINDOW_SET_CONTENT(window, child) gtk_window_set_child(GTK_WINDOW(window), child)
+#define BOX_ADD(box, child) gtk_box_append(GTK_BOX(box), child)
+#define ENTRY_GET_TEXT(entry) gtk_editable_get_text(GTK_EDITABLE(entry))
+#else
+#define WINDOW_SHOW(window) gtk_widget_show_all(window)
+#define WINDOW_DESTROY(window) gtk_window_destroy(GTK_WINDOW(window))
+#define WINDOW_SET_CONTENT(window, child) gtk_container_add(GTK_CONTAINER(window), child)
+#define BOX_ADD(box, child) gtk_box_pack_end(GTK_BOX(box), child, TRUE, TRUE, 0)
+#define ENTRY_GET_TEXT(entry) gtk_entry_get_text(GTK_EDITABLE(entry))
+#endif
 
 typedef struct UiEventData {
     UiObject    *obj;
--- a/ui/gtk/window.c	Mon Jun 17 21:22:38 2024 +0200
+++ b/ui/gtk/window.c	Sun Sep 15 20:19:52 2024 +0200
@@ -108,6 +108,9 @@
             obj);
     
     GtkWidget *vbox = ui_gtk_vbox_new(0);
+#if GTK_MAJOR_VERSION >= 4
+    WINDOW_SET_CONTENT(obj->widget, vbox);
+#else
     gtk_container_add(GTK_CONTAINER(obj->widget), vbox);
     
     if(!simple) {
@@ -130,9 +133,12 @@
         //GtkWidget *hb = ui_create_headerbar(obj);
         //gtk_window_set_titlebar(GTK_WINDOW(obj->widget), hb);
     }
+#endif
     
     // window content
     // the content has a (TODO: not yet) configurable frame
+    // TODO: really? why
+    /*
     GtkWidget *frame = gtk_frame_new(NULL);
     gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_NONE);
     gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
@@ -141,6 +147,10 @@
     GtkWidget *content_box = ui_gtk_vbox_new(0);
     gtk_container_add(GTK_CONTAINER(frame), content_box);
     obj->container = ui_box_container(obj, content_box);
+    */
+    GtkWidget *content_box = ui_gtk_vbox_new(0);
+    BOX_ADD(GTK_BOX(vbox), content_box);
+    obj->container = ui_box_container(obj, content_box);
     
     nwindows++;
     return obj;
@@ -173,7 +183,7 @@
     
     if(data->customdata) {
         GtkWidget *entry = data->customdata;
-        evt.eventdata = (void*)gtk_entry_get_text(GTK_ENTRY(entry));
+        evt.eventdata = (void*)ENTRY_GET_TEXT(GTK_ENTRY(entry));
         
     }
     
@@ -186,7 +196,7 @@
         data->callback(&evt, data->userdata);
     }
     
-    gtk_widget_destroy(GTK_WIDGET(self));
+    WINDOW_DESTROY(GTK_WIDGET(self));
 }
 
 void ui_dialog_create(UiObject *parent, UiDialogArgs args) {
@@ -208,13 +218,13 @@
     GtkWidget *content_area = gtk_dialog_get_content_area(dialog);
     if(args.content) {
         GtkWidget *label = gtk_label_new(args.content);
-        gtk_container_add(GTK_CONTAINER(content_area), label);
+        BOX_ADD(content_area, label);
     }
     
     GtkWidget *textfield = NULL;
     if(args.input) {
         textfield = gtk_entry_new();
-        gtk_container_add(GTK_CONTAINER(content_area), textfield);
+        BOX_ADD(content_area, textfield);
     }
     
     UiEventData *event = malloc(sizeof(UiEventData));
@@ -229,7 +239,7 @@
                            G_CALLBACK(ui_dialog_response),
                            event);
     
-    gtk_widget_show_all(GTK_WIDGET(dialog_w));
+    WINDOW_SHOW(GTK_WIDGET(dialog_w));
 }
 
 static void ui_gtkfilechooser(UiObject *obj, GtkFileChooserAction action, unsigned int mode, ui_callback file_selected_callback, void *cbdata) {

mercurial