using GtkApplication now

Sun, 22 Jan 2017 11:16:21 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 22 Jan 2017 11:16:21 +0100
changeset 133
6dd780cbc8c6
parent 132
a24465cc7262
child 134
69e8e0936858

using GtkApplication now

application/main.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/gtk/window.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/application/main.c	Sun Jan 15 15:08:07 2017 +0100
+++ b/application/main.c	Sun Jan 22 11:16:21 2017 +0100
@@ -33,104 +33,42 @@
 #include <ucx/buffer.h>
 #include <ucx/utils.h>
 
-UiInteger radio;
-UiRange range;
-UIWIDGET drawingarea;
-
 void action_menu(UiEvent *event, void *data) {
-    printf("action_menu test: {%s}\n", data);
-    //printf("text: {%s}\n", ui_gettext(event->obj, "text"));
-    //fflush(stdout);
-    //
-    //char *file = ui_openfiledialog(event->obj);
-    ////printf("file: %s\n", file);
-    //fflush(stdout);
-    //free(file);
+    printf("action_menu\n");
 }
 
 void action_button(UiEvent *event, void *data) {
-    //printf("radio: %d\n", ui_getval(radio));
-}
-
-void action_button2(UiEvent *event, void *data) {
-    ui_setval(radio, 1);
-}
-
-UiTextLayout *text;
-
-void action_scroll(UiEvent *event, void *data) {
-    //ui_drawingarea_redraw(drawingarea);
-    printf("scroll\n");
+    printf("action_button\n");
 }
 
 
-void draw(UiEvent *event, UiGraphics *g, void *data) {
-    int width = g->width;
-    int height = g->height;
-    //printf("rec[%d,%d]\n", width, height);
-    
-    ui_graphics_color(g, 64, 64, 64);
-    ui_draw_rect(g, 0, 0, width, height, TRUE);
-    
-    
+void application_startup(UiEvent *event, void *data) {
+    UiObject *obj = ui_window("Test", NULL);
+    ui_textarea(obj, NULL);
+    ui_show(obj);
 }
-/*
-
-void* model_getval(void *obj, int col) {
-    if(col == 0) {
-        return "folder";
-    } else if(col == 2) {
-        return "test@localhost";
-    }
-    return obj;
-}
-
-UIMENU ctxmenu;
-void click(UiEvent *event, void *data) {
-    UiMouseEvent *me = event->eventdata;
-    printf("click[%d](%d,%d)\n", me->type, me->x, me->y);
-    
-    if(me->button == 1) {
-        ui_contextmenu_popup(ctxmenu);
-    }
-    
-}
-
-void window_close(UiEvent *event, void *data) {
-    printf("window close\n");
-}
-*/
 
 int main(int argc, char** argv) { 
     ui_init("app1", argc, argv);
+    ui_onstartup(application_startup, NULL);
     
-    UiList *list = ui_list_new();
-    ui_list_append(list, "Hello");
-    ui_list_append(list, "World");
-    ui_list_append(list, "Test");
-    
+    // menu
     ui_menu("File");
-    ui_menuitem("Hello", action_menu, "hello");
+    ui_menuitem("Hello", action_menu, NULL);
     ui_submenu("Submenu1");
     ui_submenu("Submenu2");
-    ui_menuitem("item2", NULL, NULL);
+    ui_menuitem("item2", action_menu, NULL);
+    ui_submenu_end();
+    ui_menuitem("item3", action_menu, NULL);
     ui_submenu_end();
-    ui_menuitem("item3", NULL, NULL);
-    ui_submenu_end();
-    ui_menuitem("item4", NULL, NULL);
+    ui_menuitem("item4", action_menu, NULL);
     
+    // toolbar
     ui_toolitem("button1", "Test", action_button, NULL);
     ui_toolitem("button2", "OK", action_button, NULL);
-    //ui_toolbar_combobox_str("combo", list, NULL, NULL);
     ui_toolbar_add_default("button1");
     ui_toolbar_add_default("button2");
-    //ui_toolbar_add_default("combo");
     
-    UiObject *obj = ui_window("Test", NULL);
-    //ui_context_closefunc(obj->ctx, window_close, NULL);
-    ui_drawingarea(obj, draw, NULL);
-    
-    ui_show(obj);
     ui_main();
     
     return (EXIT_SUCCESS);
--- a/ui/gtk/toolkit.c	Sun Jan 15 15:08:07 2017 +0100
+++ b/ui/gtk/toolkit.c	Sun Jan 22 11:16:21 2017 +0100
@@ -38,10 +38,21 @@
 #include "../common/document.h"
 #include "../common/properties.h"
 
+#include <ucx/utils.h>
+
 #include <pthread.h>
 
+static GtkApplication *app;
+
 static char *application_name;
 
+static ui_callback   startup_func;
+static void          *startup_data;
+static ui_callback   open_func;
+void                 *open_data;
+static ui_callback   exit_func;
+void                 *exit_data;
+
 static ui_callback   appclose_fnc;
 static void          *appclose_udata;
 
@@ -66,23 +77,62 @@
     return application_name;
 }
 
-void ui_exitfunc(ui_callback f, void *userdata) {
-    appclose_fnc = f;
-    appclose_udata = userdata;
+void ui_onstartup(ui_callback f, void *userdata) {
+    startup_func = f;
+    startup_data = userdata;
+}
+
+void ui_onopen(ui_callback f, void *userdata) {
+    open_func = f;
+    open_data = userdata;
 }
 
-void ui_openfilefunc(ui_callback f, void *userdata) {
-    // OS X only
+void ui_onexit(ui_callback f, void *userdata) {
+    exit_func = f;
+    exit_data = userdata;
+}
+
+
+static void app_startup(GtkApplication* app, gpointer userdata) {
+    startup_func(NULL, startup_data);
+}
+
+static void app_activate(GtkApplication* app, gpointer userdata) {
+    printf("activate\n");
 }
 
 void ui_main() {
+#ifndef GTK2_LEGACY
+    sstr_t appid = ucx_sprintf(
+            "ui.%s",
+            application_name ? application_name : "application1");
+    
+    app = gtk_application_new(
+            appid.ptr,
+            G_APPLICATION_FLAGS_NONE);
+    g_signal_connect (app, "startup", G_CALLBACK (app_startup), NULL);
+    g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL);
+    g_application_run(G_APPLICATION (app), 0, NULL);
+    g_object_unref (app);
+    
+    free(appid.ptr);
+#else
     gtk_main();
+#endif
     if(appclose_fnc) {
         appclose_fnc(NULL, appclose_udata);
     }
     uic_store_app_properties();
 }
 
+void ui_app_quit() {
+    g_application_quit(G_APPLICATION(app));
+}
+
+GtkApplication* ui_get_application() {
+    return app;
+}
+
 void ui_show(UiObject *obj) {
     uic_check_group_widgets(obj->ctx);
     gtk_widget_show_all(obj->widget);
--- a/ui/gtk/toolkit.h	Sun Jan 15 15:08:07 2017 +0100
+++ b/ui/gtk/toolkit.h	Sun Jan 22 11:16:21 2017 +0100
@@ -55,6 +55,9 @@
 typedef enum UiOrientation UiOrientation;
 enum UiOrientation { UI_HORIZONTAL = 0, UI_VERTICAL };
 
+void ui_app_quit();
+GtkApplication* ui_get_application();
+
 void ui_destroy_userdata(GtkWidget *object, void *userdata);
 
 void ui_set_active_window(UiObject *obj);
--- a/ui/gtk/window.c	Sun Jan 15 15:08:07 2017 +0100
+++ b/ui/gtk/window.c	Sun Jan 22 11:16:21 2017 +0100
@@ -58,15 +58,24 @@
     // TODO: free UiObject
     
     nwindows--;
+#ifdef GTK2_LEGACY
     if(nwindows == 0) {
         gtk_main_quit();
     }
+#endif
 }
 
 static UiObject* create_window(char *title, void *window_data, UiBool simple) {
     UcxMempool *mp = ucx_mempool_new(256);
-    UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject));  
+    UiObject *obj = ucx_mempool_calloc(mp, 1, sizeof(UiObject)); 
+    
+#ifndef GTK2_LEGACY
+    obj->widget = gtk_application_window_new(ui_get_application());
+#else
     obj->widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+#endif
+    
+    
     obj->ctx = uic_context(obj, mp);
     obj->window = window_data;
     
--- a/ui/ui/toolkit.h	Sun Jan 15 15:08:07 2017 +0100
+++ b/ui/ui/toolkit.h	Sun Jan 22 11:16:21 2017 +0100
@@ -254,11 +254,15 @@
 void ui_init(char *appname, int argc, char **argv);
 char* ui_appname();
 
-void ui_exitfunc(ui_callback f, void *userdata);
-void ui_openfilefunc(ui_callback f, void *userdata);
+void ui_exitfunc(ui_callback f, void *userdata); // deprecated
+void ui_openfilefunc(ui_callback f, void *userdata); // deprecated
 
 void ui_context_closefunc(UiContext *ctx, ui_callback fnc, void *udata);
 
+void ui_onstartup(ui_callback f, void *userdata);
+void ui_onopen(ui_callback f, void *userdata);
+void ui_onexit(ui_callback f, void *userdata);
+
 void ui_main();
 void ui_show(UiObject *obj);
 void ui_close(UiObject *obj);

mercurial