added close handler to UiContext (GTK, Motif)

Sun, 24 Jan 2016 18:47:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 24 Jan 2016 18:47:39 +0100
changeset 111
40dbf1a7526a
parent 110
36f1581b43e2
child 112
fdd33964b35f

added close handler to UiContext (GTK, Motif)

application/main.c file | annotate | diff | comparison | revisions
ui/common/context.c file | annotate | diff | comparison | revisions
ui/common/context.h file | annotate | diff | comparison | revisions
ui/gtk/container.c file | annotate | diff | comparison | revisions
ui/gtk/window.c file | annotate | diff | comparison | revisions
ui/motif/container.c file | annotate | diff | comparison | revisions
ui/motif/container.h file | annotate | diff | comparison | revisions
ui/motif/window.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Jan 24 12:39:05 2016 +0100
+++ b/application/main.c	Sun Jan 24 18:47:39 2016 +0100
@@ -82,6 +82,10 @@
     printf("click[%d](%d,%d)\n", me->type, me->x, me->y);
 }
 
+void window_close(UiEvent *event, void *data) {
+    printf("window close\n");
+}
+
 int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
     
@@ -101,6 +105,7 @@
     ui_toolbar_add_default("button2");
     
     UiObject *obj = ui_simplewindow("Test", NULL);
+    ui_context_closefunc(obj->ctx, window_close, NULL);
     //UIWIDGET w = ui_drawingarea(obj, draw, NULL);
     //ui_mouse_handler(obj, w, click, NULL);
     
@@ -116,14 +121,11 @@
     ui_button(obj, "ABC", action_button2, NULL);
     ui_newline(obj);
     
-    ui_radiobutton(obj, "Radio1", &radio);
-    ui_radiobutton(obj, "Radio2", &radio);
-    ui_radiobutton(obj, "Radio3", &radio);
+    //ui_radiobutton(obj, "Radio1", &radio);
+    //ui_radiobutton(obj, "Radio2", &radio);
+    //ui_radiobutton(obj, "Radio3", &radio);
     
     
-    ui_layout_vexpand(obj, TRUE);
-    ui_space(obj);
-    
     ui_end(obj);
 //*/
     
--- a/ui/common/context.c	Sun Jan 24 12:39:05 2016 +0100
+++ b/ui/common/context.c	Sun Jan 24 18:47:39 2016 +0100
@@ -38,19 +38,14 @@
 
 UiContext* uic_context(UiObject *toplevel, UcxMempool *mp) {
     UiContext *ctx = ucx_mempool_malloc(mp, sizeof(UiContext));
-    ctx->parent = NULL;
+    memset(ctx, 0, sizeof(UiContext));
     ctx->mempool = mp;
-    ctx->document = NULL;
     ctx->obj = toplevel;
     ctx->vars = ucx_map_new_a(mp->allocator, 16);
-    ctx->groups = NULL;
-    ctx->group_widgets = NULL;
     
     ctx->set_document = uic_context_set_document;
     ctx->detach_document = uic_context_detach_document;
     
-    ctx->title = NULL;
-    
 #ifdef UI_GTK
     if(toplevel->widget) {
         ctx->accel_group = gtk_accel_group_new();
@@ -334,6 +329,11 @@
 
 // public API
 
+void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata) {
+    ctx->close_callback = fnc;
+    ctx->close_data = udata;
+}
+
 int ui_getint(UiObject *obj, char *name) {
     UiVar *var = uic_get_var(obj->ctx, name);
     if(var) {
--- a/ui/common/context.h	Sun Jan 24 12:39:05 2016 +0100
+++ b/ui/common/context.h	Sun Jan 24 18:47:39 2016 +0100
@@ -55,11 +55,14 @@
     void (*set_document)(UiContext *ctx, void *document);
     void (*detach_document)(UiContext *ctx, void *document);
     
-    char         *title;
+    char          *title;
     
 #ifdef UI_GTK
     GtkAccelGroup *accel_group;
 #endif
+    
+    ui_callback   close_callback;
+    void          *close_data;
 };
 
 struct UiVar {
--- a/ui/gtk/container.c	Sun Jan 24 12:39:05 2016 +0100
+++ b/ui/gtk/container.c	Sun Jan 24 18:47:39 2016 +0100
@@ -467,12 +467,12 @@
 
 void ui_layout_hexpand(UiObject *obj, UiBool expand) {
     UiContainer *ct = uic_get_current_container(obj);
-    ct->layout.hexpand = ui_bool2lb(expand);
+    ct->layout.hexpand = expand;
 }
 
 void ui_layout_vexpand(UiObject *obj, UiBool expand) {
     UiContainer *ct = uic_get_current_container(obj);
-    ct->layout.vexpand = ui_bool2lb(expand);
+    ct->layout.vexpand = expand;
 }
 
 void ui_layout_gridwidth(UiObject *obj, int width) {
--- a/ui/gtk/window.c	Sun Jan 24 12:39:05 2016 +0100
+++ b/ui/gtk/window.c	Sun Jan 24 18:47:39 2016 +0100
@@ -40,17 +40,23 @@
 
 static int nwindows = 0;
 
-static ui_callback wclose_fnc = NULL;
-static void *wclose_udata = NULL;
-
 static int window_default_width = 650;
 static int window_default_height = 550;
 
 void ui_exit_event(GtkWidget *widget, gpointer data) {
-    if(wclose_fnc) {
-        // TODO: use UiEvent
-        wclose_fnc(data, wclose_udata);
+    UiObject *obj = data;
+    UiEvent ev;
+    ev.window = obj->window;
+    ev.document = obj->ctx->document;
+    ev.obj = obj;
+    ev.eventdata = NULL;
+    ev.intval = 0;
+    
+    if(obj->ctx->close_callback) {
+        obj->ctx->close_callback(&ev, obj->ctx->close_data);
     }
+    // TODO: free UiObject
+    
     nwindows--;
     if(nwindows == 0) {
         gtk_main_quit();
@@ -85,7 +91,7 @@
             obj->widget,
             "destroy",
             G_CALLBACK(ui_exit_event),
-            window_data);
+            obj);
     
     GtkWidget *vbox = ui_gtk_vbox_new(0);
     gtk_container_add(GTK_CONTAINER(obj->widget), vbox);
--- a/ui/motif/container.c	Sun Jan 24 12:39:05 2016 +0100
+++ b/ui/motif/container.c	Sun Jan 24 18:47:39 2016 +0100
@@ -735,6 +735,11 @@
     ct->layout.vexpand = expand;
 }
 
+void ui_layout_gridwidth(UiObject *obj, int width) {
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->layout.gridwidth = width;
+}
+
 void ui_newline(UiObject *obj) {
     UiContainer *ct = uic_get_current_container(obj);
     ct->layout.newline = TRUE;
--- a/ui/motif/container.h	Sun Jan 24 12:39:05 2016 +0100
+++ b/ui/motif/container.h	Sun Jan 24 18:47:39 2016 +0100
@@ -67,8 +67,9 @@
     UiLayoutBool fill;
     UiBool       newline;
     char         *label;
-    UiLayoutBool hexpand;
-    UiLayoutBool vexpand;
+    UiBool       hexpand;
+    UiBool       vexpand;
+    int          gridwidth;
 };
 
 struct UiContainer {
--- a/ui/motif/window.c	Sun Jan 24 12:39:05 2016 +0100
+++ b/ui/motif/window.c	Sun Jan 24 18:47:39 2016 +0100
@@ -42,6 +42,19 @@
 static int window_default_height = 500;
 
 static void window_close_handler(Widget window, void *udata, void *cdata) {
+    UiObject *obj = udata;
+    UiEvent ev;
+    ev.window = obj->window;
+    ev.document = obj->ctx->document;
+    ev.obj = obj;
+    ev.eventdata = NULL;
+    ev.intval = 0;
+    
+    if(obj->ctx->close_callback) {
+        obj->ctx->close_callback(&ev, obj->ctx->close_data);
+    }
+    // TODO: free UiObject
+    
     nwindows--;
     if(nwindows == 0) {
         ui_exit_mainloop();
--- a/ui/ui/toolkit.h	Sun Jan 24 12:39:05 2016 +0100
+++ b/ui/ui/toolkit.h	Sun Jan 24 18:47:39 2016 +0100
@@ -236,6 +236,8 @@
 void ui_exitfunc(ui_callback f, void *userdata);
 void ui_openfilefunc(ui_callback f, void *userdata);
 
+void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata);
+
 void ui_main();
 void ui_show(UiObject *obj);
 void ui_close(UiObject *obj);

mercurial