From a9912c3c35dd231d8232e88564a664a8d91e4bf0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jir=CC=8Ci=CC=81=20Techet?= Date: Tue, 24 Feb 2015 19:11:51 +0100 Subject: [PATCH] Don't use single menu for "New with template" shared by toolbar and menubar The current implementation uses single menu for the toolbar and menubar and reparents it when file menu is shown/hidden. Connectiong "show"/"hide" signals doesn't work for menu items on OS X (and I suppose Ubuntu either) so the template submenu is never shown in the File menu. The easiest fix seems to be having two identical menus the same way we have them for recent files. --- src/templates.c | 81 +++++++++++++++++++-------------------------------------- 1 file changed, 27 insertions(+), 54 deletions(-) diff --git a/src/templates.c b/src/templates.c index 4d385333c..77c0c718c 100644 --- a/src/templates.c +++ b/src/templates.c @@ -50,7 +50,8 @@ GeanyTemplatePrefs template_prefs; -static GtkWidget *new_with_template_menu = NULL; /* submenu used for both file menu and toolbar */ +static GtkWidget *new_with_template_menu = NULL; +static GtkWidget *new_with_template_toolbar_menu = NULL; /* TODO: implement custom insertion templates instead? */ static gchar *templates[GEANY_MAX_TEMPLATES]; @@ -257,7 +258,7 @@ static void add_file_item(const gchar *fname, GtkWidget *menu) } -static gboolean add_custom_template_items(void) +static void populate_file_template_menu(GtkWidget *menu) { GSList *list = utils_get_config_files(GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S "files"); GSList *node; @@ -266,40 +267,25 @@ static gboolean add_custom_template_items(void) { gchar *fname = node->data; - add_file_item(fname, new_with_template_menu); + add_file_item(fname, menu); g_free(fname); } g_slist_free(list); - return list != NULL; } static void create_file_template_menu(void) { - new_with_template_menu = gtk_menu_new(); - add_custom_template_items(); - - /* unless the file menu is showing, menu should be in the toolbar widget */ - geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION( - toolbar_get_action_by_name("New")), new_with_template_menu); -} - + GtkWidget *item; -static void on_file_menu_show(GtkWidget *item) -{ - geany_menu_button_action_set_menu( - GEANY_MENU_BUTTON_ACTION(toolbar_get_action_by_name("New")), NULL); + new_with_template_menu = gtk_menu_new(); item = ui_lookup_widget(main_widgets.window, "menu_new_with_template1"); gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), new_with_template_menu); -} - -static void on_file_menu_hide(GtkWidget *item) -{ - item = ui_lookup_widget(main_widgets.window, "menu_new_with_template1"); - gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), NULL); - geany_menu_button_action_set_menu( - GEANY_MENU_BUTTON_ACTION(toolbar_get_action_by_name("New")), new_with_template_menu); + new_with_template_toolbar_menu = gtk_menu_new(); + g_object_ref(new_with_template_toolbar_menu); + geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(toolbar_get_action_by_name("New")), + new_with_template_toolbar_menu); } @@ -328,23 +314,15 @@ void templates_init(void) init_general_templates(); - create_file_template_menu(); - /* we hold our own ref for the menu as it has no parent whilst being moved */ - g_object_ref(new_with_template_menu); - - /* only connect signals to persistent objects once */ if (!init_done) { - GtkWidget *item; - /* reparent the template menu as needed */ - item = ui_lookup_widget(main_widgets.window, "file1"); - item = gtk_menu_item_get_submenu(GTK_MENU_ITEM(item)); - g_signal_connect(item, "show", G_CALLBACK(on_file_menu_show), NULL); - g_signal_connect(item, "hide", G_CALLBACK(on_file_menu_hide), NULL); - + create_file_template_menu(); g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL); + init_done = TRUE; } - init_done = TRUE; + + populate_file_template_menu(new_with_template_menu); + populate_file_template_menu(new_with_template_toolbar_menu); } @@ -527,30 +505,25 @@ gchar *templates_get_template_changelog(GeanyDocument *doc) } -void templates_free_templates(void) +static void free_template_menu_items(GtkWidget *menu) { - gint i; GList *children, *item; - /* disconnect the menu from the action widget, so destroying the items below doesn't - * trigger rebuilding of the menu on each item destroy */ - geany_menu_button_action_set_menu( - GEANY_MENU_BUTTON_ACTION(toolbar_get_action_by_name("New")), NULL); - - for (i = 0; i < GEANY_MAX_TEMPLATES; i++) - { - g_free(templates[i]); - } - /* destroy "New with template" sub menu items (in case we want to reload the templates) */ - children = gtk_container_get_children(GTK_CONTAINER(new_with_template_menu)); + children = gtk_container_get_children(GTK_CONTAINER(menu)); foreach_list(item, children) - { gtk_widget_destroy(GTK_WIDGET(item->data)); - } g_list_free(children); +} - g_object_unref(new_with_template_menu); - new_with_template_menu = NULL; + +void templates_free_templates(void) +{ + gint i; + + for (i = 0; i < GEANY_MAX_TEMPLATES; i++) + g_free(templates[i]); + free_template_menu_items(new_with_template_menu); + free_template_menu_items(new_with_template_toolbar_menu); } -- 2.11.4.GIT