ui/gtk/container.c

changeset 44
473954dc6b74
parent 32
e5f4d8af567e
child 45
ab71409644b0
--- a/ui/gtk/container.c	Mon Jun 17 21:20:58 2024 +0200
+++ b/ui/gtk/container.c	Sun Sep 29 13:32:51 2024 +0200
@@ -52,7 +52,7 @@
 }
 
 GtkWidget* ui_gtk_vbox_new(int spacing) {
-#ifdef UI_GTK3
+#if GTK_MAJOR_VERSION >= 3
     return gtk_box_new(GTK_ORIENTATION_VERTICAL, spacing);
 #else
     return gtk_vbox_new(FALSE, spacing);
@@ -60,7 +60,7 @@
 }
 
 GtkWidget* ui_gtk_hbox_new(int spacing) {
-#ifdef UI_GTK3
+#if GTK_MAJOR_VERSION >= 3
     return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, spacing);
 #else
     return gtk_hbox_new(FALSE, spacing);
@@ -70,13 +70,14 @@
 
 
 /* -------------------- Box Container -------------------- */
-UiContainer* ui_box_container(UiObject *obj, GtkWidget *box) {
+UiContainer* ui_box_container(UiObject *obj, GtkWidget *box, UiSubContainerType type) {
     UiBoxContainer *ct = cxCalloc(
             obj->ctx->allocator,
             1,
             sizeof(UiBoxContainer));
     ct->container.widget = box;
     ct->container.add = ui_box_container_add;
+    ct->type = type;
     return (UiContainer*)ct;
 }
 
@@ -95,7 +96,22 @@
     }
     
     UiBool expand = fill;
+#if GTK_MAJOR_VERSION >= 4
+    gtk_box_append(GTK_BOX(ct->widget), widget);
+    GtkAlign align = expand ? GTK_ALIGN_FILL : GTK_ALIGN_START; 
+    if(bc->type == UI_CONTAINER_VBOX) {
+        gtk_widget_set_valign(widget, align);
+        gtk_widget_set_vexpand(widget, expand);
+        gtk_widget_set_hexpand(widget, TRUE);
+    } else if(bc->type == UI_CONTAINER_HBOX) {
+        gtk_widget_set_halign(widget, align);
+        gtk_widget_set_hexpand(widget, expand);
+        gtk_widget_set_vexpand(widget, TRUE);
+    }
+    
+#else
     gtk_box_pack_start(GTK_BOX(ct->widget), widget, expand, fill, 0);
+#endif
     
     ui_reset_layout(ct->layout);
     ct->current = widget;
@@ -108,14 +124,12 @@
             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;
 }
 
-#ifdef UI_GTK3
+#if GTK_MAJOR_VERSION >= 3
 void ui_grid_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
     UiGridContainer *grid = (UiGridContainer*)ct;
     
@@ -134,12 +148,8 @@
         vexpand = ct->layout.vexpand;
     }
     
-    if(hexpand) {
-        gtk_widget_set_hexpand(widget, TRUE);
-    }
-    if(vexpand) {
-        gtk_widget_set_vexpand(widget, TRUE);
-    }
+    gtk_widget_set_hexpand(widget, hexpand);
+    gtk_widget_set_vexpand(widget, vexpand);
     
     int colspan = ct->layout.colspan > 0 ? ct->layout.colspan : 1;
     int rowspan = ct->layout.rowspan > 0 ? ct->layout.rowspan : 1;
@@ -202,11 +212,7 @@
 
 void ui_scrolledwindow_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill) {
     // TODO: check if the widget implements GtkScrollable
-#ifdef 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;
 }
@@ -235,8 +241,8 @@
 
 static GtkWidget* box_set_margin(GtkWidget *box, int margin) {
     GtkWidget *ret = box;
-#ifdef UI_GTK3
-#if GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION >= 12
+#if GTK_MAJOR_VERSION >= 3
+#if GTK_MAJOR_VERSION * 1000 + GTK_MINOR_VERSION >= 3012
     gtk_widget_set_margin_start(box, margin);
     gtk_widget_set_margin_end(box, margin);
 #else
@@ -264,7 +270,7 @@
     ct->add(ct, widget, TRUE);
     
     UiObject *newobj = uic_object_new(obj, box);
-    newobj->container = ui_box_container(obj, box);
+    newobj->container = ui_box_container(obj, box, type);
     uic_obj_add(obj, newobj);
     
     return widget;
@@ -278,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;
     
-#ifdef UI_GTK3
-    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 == 3 && GTK_MINOR_VERSION >= 12
-    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);
@@ -329,7 +318,7 @@
     UiObject* current = uic_current_obj(obj);
     UI_APPLY_LAYOUT1(current, args);
     
-    GtkWidget *sw = gtk_scrolled_window_new(NULL, NULL);
+    GtkWidget *sw = SCROLLEDWINDOW_NEW();
     UiObject *newobj = uic_object_new(obj, sw);
     newobj->container = ui_scrolledwindow_container(obj, sw);
     uic_obj_add(obj, newobj);
@@ -345,7 +334,7 @@
 /* -------------------- Splitpane -------------------- */
 
 static GtkWidget* create_paned(UiOrientation orientation) {
-#ifdef UI_GTK3
+#if GTK_MAJOR_VERSION >= 3
     switch(orientation) {
         case UI_HORIZONTAL: return gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
         case UI_VERTICAL: return gtk_paned_new(GTK_ORIENTATION_VERTICAL);

mercurial