From 26e42bc1ba7564a1c6c9cc8df4319a5027fd4372 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Thu, 23 Apr 2015 18:48:01 -0400 Subject: [PATCH] Hooked menus up to windows on the GTK+ backend. --- new/unix/menu.c | 48 +++++++++++++++++++++++++++++++++++++++++- new/unix/uipriv_unix.h | 3 +++ new/unix/window.c | 5 +++-- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/new/unix/menu.c b/new/unix/menu.c index 4efae3b1..46f551df 100644 --- a/new/unix/menu.c +++ b/new/unix/menu.c @@ -274,4 +274,50 @@ void menuDestroy(void) } g_array_free(menus, TRUE); } -*/ \ No newline at end of file +*/ + +static void appendMenuItem(GtkMenuShell *submenu, struct menuItem *item, uiWindow *w) +{ + GtkWidget *menuitem; + + menuitem = g_object_new(G_OBJECT_TYPE(item->baseItem), NULL); + if (item->name != NULL) + gtk_menu_item_set_label(GTK_MENU_ITEM(menuitem), item->name); + if (item->type != typeSeparator) { + g_signal_connect(menuitem, "activate", G_CALLBACK(onClicked), item); + if (item->type == typeCheckbox) + g_object_bind_property(item->baseItem, "active", + menuitem, "active", + G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); + } + gtk_menu_shell_append(submenu, menuitem); + g_hash_table_insert(item->uiWindows, menuitem, w); +} + +// TODO should this return a zero-height widget (or NULL) if there are no menus defined? +GtkWidget *makeMenubar(uiWindow *w) +{ + GtkWidget *menubar; + guint i, j; + struct menu *m; + GtkWidget *menuitem; + GtkWidget *submenu; + + menusFinalized = TRUE; + + menubar = gtk_menu_bar_new(); + + for (i = 0; i < menus->len; i++) { + m = &g_array_index(menus, struct menu, i); + menuitem = gtk_menu_item_new_with_label(m->name); + submenu = gtk_menu_new(); + gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu); + for (j = 0; j < m->items->len; j++) + appendMenuItem(GTK_MENU_SHELL(submenu), &g_array_index(m->items, struct menuItem, j), w); + gtk_menu_shell_append(GTK_MENU_SHELL(menubar), menuitem); + } + + gtk_widget_set_hexpand(menubar, TRUE); + gtk_widget_set_halign(menubar, GTK_ALIGN_FILL); + return menubar; +} diff --git a/new/unix/uipriv_unix.h b/new/unix/uipriv_unix.h index db38d834..b9177de8 100644 --- a/new/unix/uipriv_unix.h +++ b/new/unix/uipriv_unix.h @@ -13,3 +13,6 @@ // text.c extern char *strdupText(const char *); + +// menu.c +extern GtkWidget *makeMenubar(uiWindow *); diff --git a/new/unix/window.c b/new/unix/window.c index 93c249ad..97a2477f 100644 --- a/new/unix/window.c +++ b/new/unix/window.c @@ -131,7 +131,7 @@ static void windowSetMargined(uiWindow *ww, int margined) uiOSContainerUpdate(w->content); } -uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubars) +uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar) { struct window *w; @@ -148,7 +148,8 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubars) // set the vbox as the GtkWindow child gtk_container_add(w->container, w->vboxwidget); - // TODO menus + if (hasMenubar) + gtk_container_add(w->vboxcontainer, makeMenubar(uiWindow(w))); // and add the OS container w->content = uiNewOSContainer((uintptr_t) (w->vboxcontainer));