# HG changeset patch # User Olaf Wintermann # Date 1453657659 -3600 # Node ID 40dbf1a7526a68acfcb75a0d8a027460c7072557 # Parent 36f1581b43e2b5641665a853a952468e39989e48 added close handler to UiContext (GTK, Motif) diff -r 36f1581b43e2 -r 40dbf1a7526a application/main.c --- 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); //*/ diff -r 36f1581b43e2 -r 40dbf1a7526a ui/common/context.c --- 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) { diff -r 36f1581b43e2 -r 40dbf1a7526a ui/common/context.h --- 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 { diff -r 36f1581b43e2 -r 40dbf1a7526a ui/gtk/container.c --- 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) { diff -r 36f1581b43e2 -r 40dbf1a7526a ui/gtk/window.c --- 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); diff -r 36f1581b43e2 -r 40dbf1a7526a ui/motif/container.c --- 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; diff -r 36f1581b43e2 -r 40dbf1a7526a ui/motif/container.h --- 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 { diff -r 36f1581b43e2 -r 40dbf1a7526a ui/motif/window.c --- 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(); diff -r 36f1581b43e2 -r 40dbf1a7526a ui/ui/toolkit.h --- 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);