simplfy macro usage in container.c (GTK) newapi

Sun, 22 Sep 2024 18:16:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 22 Sep 2024 18:16:14 +0200
branch
newapi
changeset 305
98470af75dcf
parent 304
d554b2a60105
child 306
d02e1149cdb9

simplfy macro usage in container.c (GTK)

ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/gtk/container.c	Sun Sep 22 18:01:17 2024 +0200
+++ b/ui/gtk/container.c	Sun Sep 22 18:16:14 2024 +0200
@@ -124,10 +124,8 @@
             sizeof(UiGridContainer));
     ct->container.widget = grid;
     ct->container.add = ui_grid_container_add;
-#ifdef UI_GTK2
-    ct->width = 0;
-    ct->height = 1;
-#endif
+    UI_GTK_V2(ct->width = 0);
+    UI_GTK_V2(ct->height = 1);
     return (UiContainer*)ct;
 }
 
@@ -214,13 +212,7 @@
 
 void ui_scrolledwindow_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
     // TODO: check if the widget implements GtkScrollable
-#ifdef UI_GTK4
-    gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(ct->widget), widget);
-#elif defined(UI_GTK3)
-    gtk_container_add(GTK_CONTAINER(ct->widget), widget);
-#else
-    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(ct->widget), widget);
-#endif
+    SCROLLEDWINDOW_SET_CHILD(ct->widget, widget);
     ui_reset_layout(ct->layout);
     ct->current = widget;
 }
@@ -292,43 +284,26 @@
     return ui_box_create(obj, args, UI_CONTAINER_HBOX);
 }
 
-
+static GtkWidget* create_grid(int colspacing, int rowspacing) {
+#if GTK_MAJOR_VERSION >= 3
+    GtkWidget *grid = gtk_grid_new();
+    gtk_grid_set_column_spacing(GTK_GRID(grid), colspacing);
+    gtk_grid_set_row_spacing(GTK_GRID(grid), rowspacing);
+#else
+    GtkWidget *grid = gtk_table_new(1, 1, FALSE);
+    gtk_table_set_col_spacings(GTK_TABLE(grid), colspacing);
+    gtk_table_set_row_spacings(GTK_TABLE(grid), rowspacing);
+#endif
+    return grid;
+}
 
 UIWIDGET ui_grid_create(UiObject *obj, UiContainerArgs args) {
     UiObject* current = uic_current_obj(obj);
     UI_APPLY_LAYOUT1(current, args);
     GtkWidget *widget;
     
-#if GTK_MAJOR_VERSION >= 3
-    GtkWidget *grid = gtk_grid_new();
-    gtk_grid_set_column_spacing(GTK_GRID(grid), args.columnspacing);
-    gtk_grid_set_row_spacing(GTK_GRID(grid), args.rowspacing);
-#if GTK_MAJOR_VERSION * 1000 + GTK_MINOR_VERSION >= 3012
-    gtk_widget_set_margin_start(grid, args.margin);
-    gtk_widget_set_margin_end(grid, args.margin);
-#else
-    gtk_widget_set_margin_left(grid, args.margin);
-    gtk_widget_set_margin_right(grid, args.margin);
-#endif
-    gtk_widget_set_margin_top(grid, args.margin);
-    gtk_widget_set_margin_bottom(grid, args.margin);
-    
-    widget = grid;
-#elif defined(UI_GTK2)
-    GtkWidget *grid = gtk_table_new(1, 1, FALSE);
-    
-    gtk_table_set_col_spacings(GTK_TABLE(grid), columnspacing);
-    gtk_table_set_row_spacings(GTK_TABLE(grid), rowspacing);
-    
-    if(margin > 0) {
-        GtkWidget *a = gtk_alignment_new(0.5, 0.5, 1, 1);
-        gtk_alignment_set_padding(GTK_ALIGNMENT(a), margin, margin, margin, margin);
-        gtk_container_add(GTK_CONTAINER(a), grid);
-        widget = a;
-    } else {
-        widget = grid;
-    }
-#endif
+    GtkWidget *grid = create_grid(args.columnspacing, args.rowspacing);
+    widget = box_set_margin(grid, args.margin);
     current->container->add(current->container, widget, TRUE);
     
     UiObject *newobj = uic_object_new(obj, grid);
--- a/ui/gtk/toolkit.h	Sun Sep 22 18:01:17 2024 +0200
+++ b/ui/gtk/toolkit.h	Sun Sep 22 18:16:14 2024 +0200
@@ -75,6 +75,26 @@
 #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
+    
+#ifdef UI_GTK2
+#undef SCROLLEDWINDOW_SET_CHILD
+#define SCROLLEDWINDOW_SET_CHILD(sw, child) gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), child)
+#endif
+    
+#if GTK_MAJOR_VERSION >= 4
+#define UI_GTK_SINCE_V4(st) st
+#define UI_GTK_SINCE_V3(st) 
+#define UI_GTK_V2(st)
+#elif GTK_MAJOR_VERSION >= 3
+#define UI_GTK_SINCE_V4(st) st
+#define UI_GTK_SINCE_V3(st) st 
+#define UI_GTK_V2(st)
+#else
+#define UI_GTK_SINCE_V4(st) 
+#define UI_GTK_SINCE_V3(st) 
+#define UI_GTK_V2(st) st
+#endif
+
 
 typedef struct UiEventData {
     UiObject    *obj;

mercurial