port button, text to gtk4 newapi

Mon, 16 Sep 2024 22:09:32 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 16 Sep 2024 22:09:32 +0200
branch
newapi
changeset 298
c5e207d01ff2
parent 297
a73c60adf188
child 299
48763a9d19a7

port button, text to gtk4

ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/gtk/display.c file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/gtk/button.c	Sun Sep 15 20:59:48 2024 +0200
+++ b/ui/gtk/button.c	Mon Sep 16 22:09:32 2024 +0200
@@ -180,15 +180,22 @@
 #endif
 }
 
-
-
-
-
+#if GTK_MAJOR_VERSION >= 4
+#define RADIO_GROUP GtkCheckButton
+#define RADIOBUTTON_NEW(group, label) gtk_check_button_new_with_label(label)
+#define RADIOBUTTON_SET_GROUP(button, group) 
+#define RADIOBUTTON_GET_GROUP(button) GTK_CHECK_BUTTON(button)
+#else
+#define RADIO_GROUP GSList
+#define RADIOBUTTON_NEW(group, label) gtk_radio_button_new_with_label(group, label)
+#define RADIOBUTTON_SET_GROUP(button, group) /* noop */
+#define RADIOBUTTON_GET_GROUP(button) gtk_radio_button_get_group(GTK_RADIO_BUTTON(button))
+#endif
 
 UIWIDGET ui_radiobutton_create(UiObject *obj, UiToggleArgs args) {
     UiObject* current = uic_current_obj(obj);
     
-    GSList *rg = NULL;
+    RADIO_GROUP *rg = NULL;
     UiInteger *rgroup;
     
     UiVar* var = NULL;
@@ -207,8 +214,8 @@
         }
     }
     
-    GtkWidget *rbutton = gtk_radio_button_new_with_label(rg, args.label ? args.label : "");
-    rg = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton));
+    GtkWidget *rbutton = RADIOBUTTON_NEW(rg, args.label);
+    rg = RADIOBUTTON_GET_GROUP(rbutton);
     
     if(rgroup) {
         rgroup->obj = rg;
@@ -244,7 +251,7 @@
     return rbutton;
 }
 
-void ui_radio_obs(GtkToggleToolButton *widget, UiVarEventData *event) {
+void ui_radio_obs(GtkToggleButton *widget, UiVarEventData *event) {
     UiInteger *i = event->var->value;
     
     UiEvent e;
--- a/ui/gtk/container.c	Sun Sep 15 20:59:48 2024 +0200
+++ b/ui/gtk/container.c	Mon Sep 16 22:09:32 2024 +0200
@@ -343,11 +343,6 @@
 }
 
 
-#if GTK_MAJOR_VERSION >= 4
-#define SCROLLEDWINDOW_NEW() gtk_scrolled_window_new()
-#else
-#define SCROLLEDWINDOW_NEW() gtk_scrolled_window_new(NULL, NULL)
-#endif
 UIWIDGET ui_scrolledwindow_create(UiObject* obj, UiFrameArgs args) {
     UiObject* current = uic_current_obj(obj);
     UI_APPLY_LAYOUT1(current, args);
--- a/ui/gtk/display.c	Sun Sep 15 20:59:48 2024 +0200
+++ b/ui/gtk/display.c	Mon Sep 16 22:09:32 2024 +0200
@@ -36,7 +36,7 @@
 #include "../ui/display.h"
 
 static void set_alignment(GtkWidget *widget, float xalign, float yalign) {
-#if GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16
+#if GTK_MAJOR_VERSION >= 4 || (GTK_MAJOR_VERSION >= 3 && GTK_MINOR_VERSION >= 16)
     gtk_label_set_xalign(GTK_LABEL(widget), xalign);
     gtk_label_set_yalign(GTK_LABEL(widget), yalign);
 #else
@@ -106,7 +106,7 @@
 }
 
 UIWIDGET ui_separator_deprecated(UiObject *obj) {
-#if UI_GTK3
+#if GTK_MAJOR_VERSION >= 3
     GtkWidget *widget = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
 #else
     GtkWidget *widget = gtk_hseparator_new();
--- a/ui/gtk/text.c	Sun Sep 15 20:59:48 2024 +0200
+++ b/ui/gtk/text.c	Mon Sep 16 22:09:32 2024 +0200
@@ -82,17 +82,17 @@
                 G_CALLBACK(ui_textarea_destroy),
                 uitext);
     
-    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), text_area);
+    SCROLLEDWINDOW_SET_CHILD(scroll_area, text_area);
     
     // font and padding
     PangoFontDescription *font;
     font = pango_font_description_from_string("Monospace");
-    gtk_widget_modify_font(text_area, font);
+    //gtk_widget_modify_font(text_area, font); // TODO
     pango_font_description_free(font);
     
     gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text_area), 2);
@@ -180,7 +180,7 @@
 }
 
 UIWIDGET ui_textarea_gettextwidget(UIWIDGET textarea) {
-    return gtk_bin_get_child(GTK_BIN(textarea));
+    return SCROLLEDWINDOW_GET_CHILD(textarea);
 }
 
 char* ui_textarea_get(UiText *text) {
@@ -557,7 +557,10 @@
                 uitext);
     
     if(args.width > 0) {
+        // TODO: gtk4
+#if GTK_MAJOR_VERSION <= 3
         gtk_entry_set_width_chars(GTK_ENTRY(textfield), args.width);
+#endif
     }
     if(frameless) {
         // TODO: gtk2legacy workaroud
@@ -573,7 +576,7 @@
     if(var) {
         UiString *value = var->value;
         if(value->value.ptr) {
-            gtk_entry_set_text(GTK_ENTRY(textfield), value->value.ptr);
+            ENTRY_SET_TEXT(textfield, value->value.ptr);
             value->value.free(value->value.ptr);
             value->value.ptr = NULL;
             value->value.free = NULL;
@@ -631,13 +634,13 @@
     if(str->value.ptr) {
         str->value.free(str->value.ptr);
     }
-    str->value.ptr = g_strdup(gtk_entry_get_text(str->obj));
+    str->value.ptr = g_strdup(ENTRY_GET_TEXT(str->obj));
     str->value.free = (ui_freefunc)g_free;
     return str->value.ptr;
 }
 
 void ui_textfield_set(UiString *str, const char *value) {
-    gtk_entry_set_text(str->obj, value);
+    ENTRY_SET_TEXT(str->obj, value);
     if(str->value.ptr) {
         str->value.free(str->value.ptr);
         str->value.ptr = NULL;
@@ -688,6 +691,16 @@
     return elms;
 }
 
+
+#if GTK_MAJOR_VERSION >= 4
+
+UIWIDGET ui_path_textfield_create(UiObject* obj, UiPathTextFieldArgs args) {
+    // TODO
+    return NULL;    
+}
+
+#else
+
 static gboolean path_textfield_btn_pressed(GtkWidget *widget, GdkEventButton *event, UiPathTextField *pathtf) {
     gtk_box_pack_start(GTK_BOX(pathtf->hbox), pathtf->entry, TRUE, TRUE, 0);
     gtk_container_remove(GTK_CONTAINER(pathtf->hbox), pathtf->buttonbox);
@@ -932,3 +945,5 @@
         str->value.free = NULL;
     }
 }
+
+#endif
--- a/ui/gtk/toolkit.h	Sun Sep 15 20:59:48 2024 +0200
+++ b/ui/gtk/toolkit.h	Mon Sep 16 22:09:32 2024 +0200
@@ -45,13 +45,21 @@
 #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_SET_TEXT(entry, text) gtk_editable_set_text(GTK_EDITABLE(entry), text)
 #define ENTRY_GET_TEXT(entry) gtk_editable_get_text(GTK_EDITABLE(entry))
+#define SCROLLEDWINDOW_NEW() gtk_scrolled_window_new()
+#define SCROLLEDWINDOW_SET_CHILD(sw, child) gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(sw), child)
+#define SCROLLEDWINDOW_GET_CHILD(sw) gtk_scrolled_window_get_child(GTK_SCROLLED_WINDOW(sw))
 #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_SET_TEXT(entry, text) gtk_entry_set_text(GTK_EDITABLE(entry), text)
 #define ENTRY_GET_TEXT(entry) gtk_entry_get_text(GTK_EDITABLE(entry))
+#define SCROLLEDWINDOW_NEW() gtk_scrolled_window_new(NULL, NULL)
+#define SCROLLEDWINDOW_SET_CHILD(sw, child) gtk_container_add(GTK_CONTAINER(sw), child)
+#define SCROLLEDWINDOW_GET_CHILD(sw) gtk_bin_get_child(GTK_BIN(sw))
 #endif
 
 typedef struct UiEventData {

mercurial