improved box container and added radio button (GTK)

Sat, 23 Jan 2016 23:44:41 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 23 Jan 2016 23:44:41 +0100
changeset 107
9aff1dc3990d
parent 106
a4f4123ca12a
child 108
fcf6d5fac8f5

improved box container and added radio button (GTK)

application/main.c file | annotate | diff | comparison | revisions
ui/gtk/button.c file | annotate | diff | comparison | revisions
ui/gtk/button.h file | annotate | diff | comparison | revisions
ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/gtk/container.h file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
ui/ui/button.h file | annotate | diff | comparison | revisions
ui/ui/text.h file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
ui/ui/tree.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sat Jan 23 20:47:07 2016 +0100
+++ b/application/main.c	Sat Jan 23 23:44:41 2016 +0100
@@ -33,6 +33,8 @@
 #include <ucx/buffer.h>
 #include <ucx/utils.h>
 
+UiRadioButtonGroup rgroup;
+
 void action_menu(UiEvent *event, void *data) {
     printf("action_menu test: {%s}\n", data);
     printf("text: {%s}\n", ui_gettext(event->obj, "text"));
@@ -45,10 +47,14 @@
 }
 
 void action_button(UiEvent *event, void *data) {
-    printf("button clicked\n");
+    printf("radio: %d\n", ui_radiobuttongroup_selection(&rgroup));
     fflush(stdout);
 }
 
+void action_button2(UiEvent *event, void *data) {
+    ui_radiobuttongroup_select(&rgroup, 3);
+}
+
 void draw(UiEvent *event, UiGraphics *g, void *data) {
     /*
     int width = g->width;
@@ -95,27 +101,47 @@
     ui_toolbar_add_default("button1");
     ui_toolbar_add_default("button2");
     
-    UiObject *obj = ui_window("Test", NULL);
+    UiObject *obj = ui_simplewindow("Test", NULL);
     //UIWIDGET w = ui_drawingarea(obj, draw, NULL);
     //ui_mouse_handler(obj, w, click, NULL);
     
 ///*
-    ui_grid_sp(obj, 8, 4, 4);
+    ui_vbox_sp(obj, 8, 8);
     
-    ui_button(obj, "OK", NULL, NULL);
-    ui_layout_hexpand(obj, TRUE);
-    ui_button(obj, "Google", NULL, NULL);
+    ui_layout_fill(obj, FALSE);
+    ui_grid_sp(obj, 0, 4, 4);
+    ui_label(obj, "Name");
     ui_textfield(obj, NULL);
     ui_newline(obj);
-    
-    ui_vbox(obj);
-    ui_button(obj, "txt", NULL, NULL);
+    ui_label(obj, "Email");
     ui_textfield(obj, NULL);
     ui_end(obj);
-    ui_layout_hexpand(obj, TRUE);
-    ui_layout_vexpand(obj, TRUE);
-    ui_textarea(obj, NULL);
-    ui_button(obj, "BTN1", NULL, NULL);
+    
+    ui_separator(obj);
+    
+    ui_layout_fill(obj, FALSE);
+    ui_grid_sp(obj, 0, 4, 4);
+    ui_checkbox(obj, "Shit", NULL);
+    ui_newline(obj);
+    ui_checkbox(obj, "Nein", NULL);
+    ui_checkbox(obj, "Nein", NULL);
+    ui_checkbox(obj, "Nein", NULL);
+    ui_end(obj);
+    
+    ui_separator(obj);
+    
+    ui_layout_fill(obj, FALSE);
+    ui_grid_sp(obj, 0, 4, 4);
+    rgroup = ui_radiobuttongroup();
+    ui_radiobutton(obj, "better", &rgroup);
+    ui_newline(obj);
+    ui_radiobutton(obj, "awesome", &rgroup);
+    ui_newline(obj);
+    ui_radiobutton(obj, "smarter", &rgroup);
+    ui_end(obj);
+    
+    ui_button(obj, "OK", action_button, NULL);
+    ui_button(obj, "Activate", action_button2, NULL);
     
     ui_end(obj);
 //*/
--- a/ui/gtk/button.c	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/gtk/button.c	Sat Jan 23 23:44:41 2016 +0100
@@ -150,3 +150,53 @@
     return button;
 }
 
+
+UiRadioButtonGroup ui_radiobuttongroup() {
+    UiRadioButtonGroup rgroup;
+    rgroup.group = NULL;
+    rgroup.ref = 0;
+    return rgroup;
+}
+
+void ui_radiobuttongroup_select(UiRadioButtonGroup *rgroup, int index) {
+    GSList *ls = rgroup->group;
+    int s = g_slist_length(rgroup->group) - 1 - index;
+    int i = 0;
+    while(ls) {
+        if(i == s) {
+            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ls->data), TRUE);
+            break;
+        }
+        ls = ls->next;
+        i++;
+    }
+}
+
+int ui_radiobuttongroup_selection(UiRadioButtonGroup *rgroup) {
+    GSList *ls = rgroup->group;
+    int i = 0;
+    while(ls) {
+        if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ls->data))) {
+            return g_slist_length(rgroup->group) - i - 1;
+        }
+        ls = ls->next;
+        i++;
+    }
+    return -1;
+}
+
+int io_radiobuttongroup_count(UiRadioButtonGroup *rgroup) {
+    return g_slist_length(rgroup->group);
+}
+
+UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiRadioButtonGroup *rgroup) {
+    GtkWidget *rbutton = gtk_radio_button_new_with_label(rgroup->group, label);
+    rgroup->group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(rbutton));
+    rgroup->ref++;
+    
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(ct, rbutton, FALSE);
+    
+    return rbutton;
+}
+
--- a/ui/gtk/button.h	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/gtk/button.h	Sat Jan 23 23:44:41 2016 +0100
@@ -30,6 +30,7 @@
 #define	BUTTON_H
 
 #include "../ui/toolkit.h"
+#include "../ui/button.h"
 #include "toolkit.h"
 
 #ifdef	__cplusplus
--- a/ui/gtk/container.c	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/gtk/container.c	Sat Jan 23 23:44:41 2016 +0100
@@ -33,19 +33,19 @@
 #include "../common/context.h"
 #include "../common/object.h"
 
-GtkWidget* ui_gtk_vbox_new() {
+GtkWidget* ui_gtk_vbox_new(int spacing) {
 #ifdef UI_GTK3
-    return gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+    return gtk_box_new(GTK_ORIENTATION_VERTICAL, spacing);
 #else
-    return gtk_vbox_new(FALSE, 0);
+    return gtk_vbox_new(FALSE, spacing);
 #endif
 }
 
-GtkWidget* ui_gtk_hbox_new() {
+GtkWidget* ui_gtk_hbox_new(int spacing) {
 #ifdef UI_GTK3
-    return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+    return gtk_box_new(GTK_ORIENTATION_HORIZONTAL, spacing);
 #else
-    return gtk_hbox_new(FALSE, 0);
+    return gtk_hbox_new(FALSE, spacing);
 #endif
 }
 
@@ -203,29 +203,60 @@
 
 
 UIWIDGET ui_vbox(UiObject *obj) {
+    return ui_vbox_sp(obj, 0, 0);
+}
+
+UIWIDGET ui_hbox(UiObject *obj) {
+    return ui_hbox_sp(obj, 0, 0);
+}
+
+static GtkWidget* box_set_margin(GtkWidget *box, int margin) {
+    GtkWidget *ret = box;
+#ifdef UI_GTK3
+#if GTK_MAJOR_VERSION == 3 && GTK_MINOR_VERSION >= 12
+    gtk_widget_set_margin_start(box, margin);
+    gtk_widget_set_margin_end(box, margin);
+#else
+    gtk_widget_set_margin_left(box, margin);
+    gtk_widget_set_margin_right(box, margin);
+#endif
+    gtk_widget_set_margin_top(box, margin);
+    gtk_widget_set_margin_bottom(box, margin);
+#elif defined(UI_GTK2)
+    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), box);
+    ret = a;
+#endif
+    return ret;
+}
+
+UIWIDGET ui_vbox_sp(UiObject *obj, int margin, int spacing) {
     UiContainer *ct = uic_get_current_container(obj);
     
-    GtkWidget *vbox = ui_gtk_vbox_new();
-    ct->add(ct, vbox, TRUE);
+    GtkWidget *vbox = ui_gtk_vbox_new(spacing);
+    GtkWidget *widget = margin > 0 ? box_set_margin(vbox, margin) : vbox;
+    ct->add(ct, widget, TRUE);
     
     UiObject *newobj = uic_object_new(obj, vbox);
     newobj->container = ui_box_container(obj, vbox);
     uic_obj_add(obj, newobj);
     
-    return vbox;
+    return widget;
 }
 
-UIWIDGET ui_hbox(UiObject *obj) {
+UIWIDGET ui_hbox_sp(UiObject *obj, int margin, int spacing) {
     UiContainer *ct = uic_get_current_container(obj);
     
-    GtkWidget *hbox = ui_gtk_hbox_new();
-    ct->add(ct, hbox, TRUE);
+    GtkWidget *hbox = ui_gtk_hbox_new(spacing);
+    GtkWidget *widget = margin > 0 ? box_set_margin(hbox, margin) : hbox;
+    ct->add(ct, widget, TRUE);
     
     UiObject *newobj = uic_object_new(obj, hbox);
     newobj->container = ui_box_container(obj, hbox);
     uic_obj_add(obj, newobj);
     
-    return hbox;
+    return widget;
 }
 
 UIWIDGET ui_grid(UiObject *obj) {
@@ -310,7 +341,7 @@
 #endif
     gtk_paned_set_position(GTK_PANED(paned), 200);
     
-    GtkWidget *sidebar = ui_gtk_vbox_new();
+    GtkWidget *sidebar = ui_gtk_vbox_new(0);
     gtk_paned_pack1(GTK_PANED(paned), sidebar, TRUE, FALSE);
     
     UiObject *left = uic_object_new(obj, sidebar);
--- a/ui/gtk/container.h	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/gtk/container.h	Sat Jan 23 23:44:41 2016 +0100
@@ -89,8 +89,8 @@
     UiContainer container;
 } UiTabViewContainer;
 
-GtkWidget* ui_gtk_vbox_new();
-GtkWidget* ui_gtk_hbox_new();
+GtkWidget* ui_gtk_vbox_new(int spacing);
+GtkWidget* ui_gtk_hbox_new(int spacing);
 
 UiContainer* ui_frame_container(UiObject *obj, GtkWidget *frame);
 void ui_frame_container_add(UiContainer *ct, GtkWidget *widget, UiBool fill);
--- a/ui/gtk/window.c	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/gtk/window.c	Sat Jan 23 23:44:41 2016 +0100
@@ -87,7 +87,7 @@
             G_CALLBACK(ui_exit_event),
             window_data);
     
-    GtkWidget *vbox = ui_gtk_vbox_new();
+    GtkWidget *vbox = ui_gtk_vbox_new(0);
     gtk_container_add(GTK_CONTAINER(obj->widget), vbox);
     
     if(!simple) {
@@ -110,7 +110,7 @@
     gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, TRUE, 0);
     
     // content vbox
-    GtkWidget *content_box = ui_gtk_vbox_new();
+    GtkWidget *content_box = ui_gtk_vbox_new(0);
     gtk_container_add(GTK_CONTAINER(frame), content_box);
     obj->container = ui_box_container(obj, content_box);
     
--- a/ui/ui/button.h	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/ui/button.h	Sat Jan 23 23:44:41 2016 +0100
@@ -35,11 +35,23 @@
 extern "C" {
 #endif
 
+typedef struct {
+    void *group;
+    int ref;
+} UiRadioButtonGroup;
+    
 UIWIDGET ui_button(UiObject *obj, char *label, ui_callback f, void *data);
 
 UIWIDGET ui_checkbox(UiObject *obj, char *label, UiInteger *value);
 UIWIDGET ui_checkbox_cb(UiObject *obj, char *label, ui_callback f, void *data);
 
+UiRadioButtonGroup ui_radiobuttongroup();
+void ui_radiobuttongroup_select(UiRadioButtonGroup *rgroup, int index);
+int ui_radiobuttongroup_selection(UiRadioButtonGroup *rgroup);
+int io_radiobuttongroup_count(UiRadioButtonGroup *rgroup);
+
+UIWIDGET ui_radiobutton(UiObject *obj, char *label, UiRadioButtonGroup *rgroup);
+
 
 #ifdef	__cplusplus
 }
--- a/ui/ui/text.h	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/ui/text.h	Sat Jan 23 23:44:41 2016 +0100
@@ -37,8 +37,6 @@
 
 UIWIDGET ui_textarea(UiObject *obj, UiText *value);
 UIWIDGET ui_textarea_nv(UiObject *obj, char *varname);
-UIWIDGET ui_textfield_w(UiObject *obj, int width, UiString *value);
-UIWIDGET ui_textfield_wnv(UiObject *obj, int width, char *varname);
 
 void ui_text_undo(UiText *value);
 void ui_text_redo(UiText *value);
@@ -46,6 +44,9 @@
 UIWIDGET ui_textfield(UiObject *obj, UiString *value);
 UIWIDGET ui_textfield_nv(UiObject *obj, char *varname);
 
+UIWIDGET ui_textfield_w(UiObject *obj, int width, UiString *value);
+UIWIDGET ui_textfield_wnv(UiObject *obj, int width, char *varname);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/ui/ui/toolkit.h	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/ui/toolkit.h	Sat Jan 23 23:44:41 2016 +0100
@@ -248,6 +248,8 @@
 
 UIWIDGET ui_vbox(UiObject *obj);
 UIWIDGET ui_hbox(UiObject *obj);
+UIWIDGET ui_vbox_sp(UiObject *obj, int margin, int spacing);
+UIWIDGET ui_hbox_sp(UiObject *obj, int margin, int spacing);
 
 UIWIDGET ui_grid(UiObject *obj);
 UIWIDGET ui_grid_sp(UiObject *obj, int margin, int columnspacing, int rowspacing);
--- a/ui/ui/tree.h	Sat Jan 23 20:47:07 2016 +0100
+++ b/ui/ui/tree.h	Sat Jan 23 23:44:41 2016 +0100
@@ -63,8 +63,8 @@
     char **titles;
     
     /*
-     * function for translatring model data to view data
-     * first argument is a pointer from the model (UiList, UiTree)
+     * function for translating model data to view data
+     * first argument is the pointer returned by UiList->get or UiTree->get
      * second argument is the column index
      * TODO: return
      */

mercurial