add option to name widgets and add css classes (GTK) newapi

Sun, 29 Sep 2024 15:01:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 29 Sep 2024 15:01:14 +0200
branch
newapi
changeset 313
b679cc6059ab
parent 312
3f2b3d15668b
child 314
d96ba820083a

add option to name widgets and add css classes (GTK)

ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/gtk/entry.c file | annotate | diff | comparison | revisions
ui/gtk/text.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.c file | annotate | diff | comparison | revisions
ui/gtk/toolkit.h file | annotate | diff | comparison | revisions
ui/ui/button.h file | annotate | diff | comparison | revisions
ui/ui/container.h file | annotate | diff | comparison | revisions
ui/ui/entry.h file | annotate | diff | comparison | revisions
ui/ui/text.h file | annotate | diff | comparison | revisions
--- a/ui/gtk/button.c	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/gtk/button.c	Sun Sep 29 15:01:14 2024 +0200
@@ -90,6 +90,7 @@
 UIWIDGET ui_button_create(UiObject *obj, UiButtonArgs args) {
     UiObject* current = uic_current_obj(obj);
     GtkWidget *button = ui_create_button(obj, args.label, args.icon, args.onclick, args.onclickdata);
+    ui_set_name_and_style(button, args.name, args.style_class);
     UI_APPLY_LAYOUT1(current, args);
     current->container->add(current->container, button, FALSE);
     return button;
@@ -230,6 +231,7 @@
     UiObject* current = uic_current_obj(obj);
     
     ui_setup_togglebutton(current, widget, args.label, args.icon, args.varname, args.value, args.onchange, args.onchangedata);
+    ui_set_name_and_style(widget, args.name, args.style_class);
     
     UI_APPLY_LAYOUT1(current, args);
     current->container->add(current->container, widget, FALSE);
@@ -340,6 +342,7 @@
     }
     
     GtkWidget *rbutton = RADIOBUTTON_NEW(rg, args.label); 
+    ui_set_name_and_style(rbutton, args.name, args.style_class);
     if(rgroup) {
 #if GTK_MAJOR_VERSION >= 4
         if(rg) {
--- a/ui/gtk/container.c	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/gtk/container.c	Sun Sep 29 15:01:14 2024 +0200
@@ -266,6 +266,7 @@
     UI_APPLY_LAYOUT1(current, args);
     
     GtkWidget *box = type == UI_CONTAINER_VBOX ? ui_gtk_vbox_new(args.spacing) : ui_gtk_hbox_new(args.spacing);
+    ui_set_name_and_style(box, args.name, args.style_class);
     GtkWidget *widget = args.margin > 0 ? box_set_margin(box, args.margin) : box;
     ct->add(ct, widget, TRUE);
     
@@ -303,6 +304,7 @@
     GtkWidget *widget;
     
     GtkWidget *grid = create_grid(args.columnspacing, args.rowspacing);
+    ui_set_name_and_style(grid, args.name, args.style_class);
     widget = box_set_margin(grid, args.margin);
     current->container->add(current->container, widget, TRUE);
     
@@ -319,6 +321,7 @@
     UI_APPLY_LAYOUT1(current, args);
     
     GtkWidget *sw = SCROLLEDWINDOW_NEW();
+    ui_set_name_and_style(sw, args.name, args.style_class);
     UiObject *newobj = uic_object_new(obj, sw);
     newobj->container = ui_scrolledwindow_container(obj, sw);
     uic_obj_add(obj, newobj);
--- a/ui/gtk/entry.c	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/gtk/entry.c	Sun Sep 29 15:01:14 2024 +0200
@@ -70,6 +70,7 @@
     }
 #endif
     GtkWidget *spin = gtk_spin_button_new_with_range(min, max, args.step);
+    ui_set_name_and_style(spin, args.name, args.style_class);
     gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), args.digits);
     UiObserver **obs = NULL;
     if(var) {
--- a/ui/gtk/text.c	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/gtk/text.c	Sun Sep 29 15:01:14 2024 +0200
@@ -542,6 +542,7 @@
 
 static UIWIDGET create_textfield(UiObject *obj, UiBool frameless, UiBool password, UiTextFieldArgs args) {
     GtkWidget *textfield = gtk_entry_new();
+    ui_set_name_and_style(textfield, args.name, args.style_class);
     
     UiObject* current = uic_current_obj(obj);
     UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.value, args.varname, UI_VAR_STRING);
--- a/ui/gtk/toolkit.c	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/gtk/toolkit.c	Sun Sep 29 15:01:14 2024 +0200
@@ -383,4 +383,21 @@
 
 
 
-#endif
\ No newline at end of file
+#endif
+
+void ui_set_name_and_style(GtkWidget *widget, const char *name, const char *style_classes) {
+    if(name) {
+        gtk_widget_set_name(widget, name);
+    }
+    if(style_classes) {
+        cxstring *cls = NULL;
+        size_t numClasses = cx_strsplit_a(cxDefaultAllocator, cx_str(style_classes), CX_STR(" "), 128, &cls);
+        for(int i=0;i<numClasses;i++) {
+            cxmutstr m = cx_strdup(cls[i]);
+            gtk_widget_add_css_class(widget, m.ptr);
+            free(m.ptr);
+        }
+        free(cls);
+        
+    }
+}
--- a/ui/gtk/toolkit.h	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/gtk/toolkit.h	Sun Sep 29 15:01:14 2024 +0200
@@ -136,6 +136,8 @@
 
 int ui_get_scalefactor();
 
+void ui_set_name_and_style(GtkWidget *widget, const char *name, const char *style);
+
 void ui_destroy_userdata(GtkWidget *object, void *userdata);
 void ui_destroy_vardata(GtkWidget *object, UiVarEventData *data);
 void ui_destroy_boundvar(UiContext *ctx, UiVar *var);
--- a/ui/ui/button.h	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/ui/button.h	Sun Sep 29 15:01:14 2024 +0200
@@ -41,6 +41,8 @@
     UiBool vexpand;
     int colspan;
     int rowspan;
+    const char *name;
+    const char *style_class;
 
     const char* label;
     const char* stockid;
@@ -56,7 +58,9 @@
     UiBool vexpand;
     int colspan;
     int rowspan;
-
+    const char *name;
+    const char *style_class;
+    
     const char* label;
     const char* stockid;
     const char* icon;
@@ -66,7 +70,7 @@
     ui_callback onchange;
     void* onchangedata;
 } UiToggleArgs;
-   
+ 
 #define ui_button(obj, ...) ui_button_create(obj, (UiButtonArgs){ __VA_ARGS__ } )
 #define ui_togglebutton(obj, ...) ui_togglebutton_create(obj, (UiToggleArgs){ __VA_ARGS__ } )
 #define ui_checkbox(obj, ...) ui_checkbox_create(obj, (UiToggleArgs){ __VA_ARGS__ } )
--- a/ui/ui/container.h	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/ui/container.h	Sun Sep 29 15:01:14 2024 +0200
@@ -56,6 +56,8 @@
     UiBool vexpand;
     int colspan;
     int rowspan;
+    const char *name;
+    const char *style_class;
 
     int margin;
     int spacing;
@@ -69,6 +71,8 @@
     UiBool vexpand;
     int colspan;
     int rowspan;
+    const char *name;
+    const char *style_class;
 
     UiSubContainerType subcontainer;
 
@@ -87,6 +91,8 @@
     UiBool vexpand;
     int colspan;
     int rowspan;
+    const char *name;
+    const char *style_class;
 
     UiTabViewType tabview;
 
--- a/ui/ui/entry.h	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/ui/entry.h	Sun Sep 29 15:01:14 2024 +0200
@@ -42,6 +42,8 @@
     UiBool vexpand;
     int colspan;
     int rowspan;
+    const char *name;
+    const char *style_class;
 
     double step;
     int digits;
--- a/ui/ui/text.h	Sun Sep 29 13:33:34 2024 +0200
+++ b/ui/ui/text.h	Sun Sep 29 15:01:14 2024 +0200
@@ -42,6 +42,8 @@
     int colspan;
     int rowspan;
     int width;
+    const char *name;
+    const char *style_class;
 
     UiString* value;
     const char* varname;
@@ -66,6 +68,8 @@
     UiBool vexpand;
     int colspan;
     int rowspan;
+    const char *name;
+    const char *style_class;
 
     UiString *value;
     const char* varname;

mercurial