From c2d6d46b9ca89cb1c0729ee599c3566d3d1e7cf7 Mon Sep 17 00:00:00 2001 From: Javier Date: Tue, 28 Jan 2014 16:50:47 +0100 Subject: initial import --- module/Makefile.am | 9 + module/appmenu.c | 222 +++++++++++++++ module/appmenu.h | 21 ++ module/data.c | 156 +++++++++++ module/data.h | 48 ++++ module/main.c | 724 ++++++++++++++++++++++++++++++++++++++++++++++++ module/menuitem-proxy.c | 421 ++++++++++++++++++++++++++++ module/menuitem-proxy.h | 8 + 8 files changed, 1609 insertions(+) create mode 100644 module/Makefile.am create mode 100644 module/appmenu.c create mode 100644 module/appmenu.h create mode 100644 module/data.c create mode 100644 module/data.h create mode 100644 module/main.c create mode 100644 module/menuitem-proxy.c create mode 100644 module/menuitem-proxy.h (limited to 'module') diff --git a/module/Makefile.am b/module/Makefile.am new file mode 100644 index 0000000..ac75b74 --- /dev/null +++ b/module/Makefile.am @@ -0,0 +1,9 @@ + +gtk_moduledir = $(GTK_MODULE_DIR) +gtk_module_LTLIBRARIES = libtopmenu-gtk-module.la + +libtopmenu_gtk_module_la_SOURCES = main.c data.c data.h \ + menuitem-proxy.c menuitem-proxy.h appmenu.c appmenu.h +libtopmenu_gtk_module_la_CPPFLAGS = $(GTK_CFLAGS) -DG_LOG_DOMAIN=\"topmenu-module\" +libtopmenu_gtk_module_la_LIBADD = $(GTK_LIBS) ../libtopmenu-client/libtopmenu-client.la +libtopmenu_gtk_module_la_LDFLAGS = -avoid-version -module -shared diff --git a/module/appmenu.c b/module/appmenu.c new file mode 100644 index 0000000..a73846a --- /dev/null +++ b/module/appmenu.c @@ -0,0 +1,222 @@ +#include + +#include "appmenu.h" +#include "data.h" +#include "menuitem-proxy.h" + +typedef enum _AppMenuRole { + APP_MENU_ROLE_NONE = 0, + APP_MENU_ROLE_ABOUT, + APP_MENU_ROLE_PREFERENCES, + APP_MENU_ROLE_QUIT, + APP_MENU_ROLE_MAX +} AppMenuRole; + +static AppMenuRole detect_role_by_stock(const gchar *stock_id) +{ + if (g_strcmp0(stock_id, GTK_STOCK_PREFERENCES) == 0) { + return APP_MENU_ROLE_PREFERENCES; + } else if (g_strcmp0(stock_id, GTK_STOCK_QUIT) == 0) { + return APP_MENU_ROLE_QUIT; + } else if (g_strcmp0(stock_id, GTK_STOCK_ABOUT) == 0) { + return APP_MENU_ROLE_ABOUT; + } else { + return APP_MENU_ROLE_NONE; + } +} + +static AppMenuRole detect_item_role(GtkMenuItem *item) +{ + if (GTK_IS_IMAGE_MENU_ITEM(item)) { + GtkImageMenuItem *iitem = GTK_IMAGE_MENU_ITEM(item); + if (gtk_image_menu_item_get_use_stock(iitem)) { + return detect_role_by_stock(gtk_menu_item_get_label(item)); + } else { + GtkWidget *iwidget = gtk_image_menu_item_get_image(iitem); + if (GTK_IS_IMAGE(iwidget)) { + GtkImage *image = GTK_IMAGE(iwidget); + if (gtk_image_get_storage_type(image) == GTK_IMAGE_STOCK) { + gchar *stock_id; + GtkIconSize icon_size; + gtk_image_get_stock(image, &stock_id, &icon_size); + return detect_role_by_stock(stock_id); + } + } + } + } + + return APP_MENU_ROLE_NONE; +} + +static void handle_default_quit(GtkMenuItem *item, gpointer user_data) +{ + gtk_main_quit(); +} + +static GtkMenuItem *create_separator() +{ + GtkWidget *sep = gtk_separator_menu_item_new(); + gtk_widget_show(sep); + return GTK_MENU_ITEM(sep); +} + +static GtkMenuItem *create_default_exit() +{ + GtkMenuItem *item = GTK_MENU_ITEM(gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL)); + g_signal_connect(item, "activate", G_CALLBACK(handle_default_quit), NULL); + gtk_widget_show_all(GTK_WIDGET(item)); + return item; +} + +static gint get_role_position(AppMenu *self, AppMenuRole role) +{ + gint position = 0; + switch (role) { + case APP_MENU_ROLE_MAX: + if (self->quit_item) position++; + if (self->sep1_item) position++; + case APP_MENU_ROLE_QUIT: + if (self->prefs_item) position++; + case APP_MENU_ROLE_PREFERENCES: + if (self->about_item) position++; + case APP_MENU_ROLE_ABOUT: + break; + default: + g_warn_if_reached(); + break; + } + return position; +} + +static void set_item_for_role(AppMenu *self, GtkMenuItem *item, AppMenuRole role) +{ + g_return_if_fail(role != APP_MENU_ROLE_NONE); + + switch (role) { + case APP_MENU_ROLE_ABOUT: + if (self->about_item) { + gtk_widget_destroy(GTK_WIDGET(self->about_item)); + self->about_item = NULL; + } + if (item) { + if (self->quit_item && !self->sep1_item) { + self->sep1_item = create_separator(); + gtk_menu_shell_insert(GTK_MENU_SHELL(self->menu), + GTK_WIDGET(self->sep1_item), + get_role_position(self, APP_MENU_ROLE_QUIT)); + } + self->about_item = item; + gtk_menu_shell_insert(GTK_MENU_SHELL(self->menu), + GTK_WIDGET(self->about_item), + get_role_position(self, APP_MENU_ROLE_ABOUT)); + } + break; + case APP_MENU_ROLE_PREFERENCES: + if (self->prefs_item) { + gtk_widget_destroy(GTK_WIDGET(self->prefs_item)); + self->prefs_item = NULL; + } + if (item) { + if (self->quit_item && !self->sep1_item) { + self->sep1_item = create_separator(); + gtk_menu_shell_insert(GTK_MENU_SHELL(self->menu), + GTK_WIDGET(self->sep1_item), + get_role_position(self, APP_MENU_ROLE_QUIT)); + } + self->prefs_item = item; + gtk_menu_shell_insert(GTK_MENU_SHELL(self->menu), + GTK_WIDGET(self->prefs_item), + get_role_position(self, APP_MENU_ROLE_PREFERENCES)); + } + break; + case APP_MENU_ROLE_QUIT: + if (self->quit_item) { + gtk_widget_destroy(GTK_WIDGET(self->quit_item)); + self->quit_item = NULL; + } + if (item) { + if ((self->about_item || self->prefs_item) && !self->sep1_item) { + self->sep1_item = create_separator(); + gtk_menu_shell_insert(GTK_MENU_SHELL(self->menu), + GTK_WIDGET(self->sep1_item), + get_role_position(self, APP_MENU_ROLE_QUIT)); + } + self->quit_item = item; + gtk_menu_shell_insert(GTK_MENU_SHELL(self->menu), + GTK_WIDGET(self->quit_item), + get_role_position(self, APP_MENU_ROLE_QUIT) + 1); + } + break; + default: + g_warn_if_reached(); + } +} + +GtkWidget * topmenu_appmenu_build(AppMenu *self) +{ + self->menu = GTK_MENU(gtk_menu_new()); + + return GTK_WIDGET(self->menu); +} + +typedef struct _MenuScanData +{ + AppMenu *appmenu; + gint depth; +} MenuScanData; + +static void appmenu_scan_cb(GtkWidget *widget, gpointer user_data) +{ + MenuScanData *data = user_data; + g_return_if_fail(GTK_IS_MENU_ITEM(widget)); + + GtkMenuItem *item = GTK_MENU_ITEM(widget); + GtkWidget *submenu = gtk_menu_item_get_submenu(item); + + if (!submenu) { + MenuItemData *item_data = topmenu_get_menu_item_data(item); + if (item_data && item_data->proxy) { + submenu = gtk_menu_item_get_submenu(item_data->proxy); + } + } + + if (submenu) { + data->depth--; + if (data->depth >= 0) { + g_warn_if_fail(GTK_IS_CONTAINER(submenu)); + gtk_container_foreach(GTK_CONTAINER(submenu), appmenu_scan_cb, data); + } + data->depth++; + } else { + AppMenuRole role = detect_item_role(item); + if (role != APP_MENU_ROLE_NONE) { + GtkMenuItem *proxy = topmenu_create_proxy_menu_item(item); + set_item_for_role(data->appmenu, proxy, role); + } + } +} + +void topmenu_appmenu_scan_for_items(AppMenu *self, GtkMenuShell *menu_shell) +{ + g_return_if_fail(GTK_IS_MENU(self->menu)); + + MenuScanData data; + data.appmenu = self; + data.depth = 1; + gtk_container_foreach(GTK_CONTAINER(menu_shell), appmenu_scan_cb, &data); + + // Create default items + if (!self->quit_item) { + GtkMenuItem *item = create_default_exit(); + set_item_for_role(self, item, APP_MENU_ROLE_QUIT); + } +} + +void topmenu_appmenu_destroy(AppMenu *self) +{ + if (self->menu) { + gtk_widget_destroy(GTK_WIDGET(self->menu)); + self->menu = NULL; + } + memset(self, 0, sizeof(AppMenu)); +} diff --git a/module/appmenu.h b/module/appmenu.h new file mode 100644 index 0000000..e02f68c --- /dev/null +++ b/module/appmenu.h @@ -0,0 +1,21 @@ +#ifndef _APPMENU_H_ +#define _APPMENU_H_ + +#include + +typedef struct _AppMenu +{ + GtkMenu *menu; + GtkMenuItem *about_item; + GtkMenuItem *prefs_item; + GtkMenuItem *sep1_item; + GtkMenuItem *quit_item; +} AppMenu; + +GtkWidget * topmenu_appmenu_build(AppMenu *appmenu); + +void topmenu_appmenu_scan_for_items(AppMenu *self, GtkMenuShell *menu_shell); + +void topmenu_appmenu_destroy(AppMenu *self); + +#endif diff --git a/module/data.c b/module/data.c new file mode 100644 index 0000000..9225d01 --- /dev/null +++ b/module/data.c @@ -0,0 +1,156 @@ +#include "../libtopmenu-client/topmenu-monitor.h" + +#include "data.h" +#include "appmenu.h" + +G_DEFINE_QUARK(topmenu-window-data, window_data) +G_DEFINE_QUARK(topmenu-menu-shell-data, menu_shell_data) +G_DEFINE_QUARK(topmenu-menu-item-data, menu_item_data) + +gboolean +topmenu_is_blacklisted (void) +{ + return FALSE; +} + +gboolean +topmenu_is_window_blacklisted (GtkWindow *window) +{ + if (gtk_window_get_window_type (window) != GTK_WINDOW_TOPLEVEL) + return TRUE; + + if (GTK_IS_PLUG (window)) + return TRUE; + + return FALSE; +} + +static WindowData * +window_data_new (void) +{ + return g_slice_new0 (WindowData); +} + +static void +window_data_free (gpointer data) +{ + WindowData *window_data = data; + + if (window_data != NULL) + { + if (window_data->menus != NULL) + g_slist_free_full (window_data->menus, g_object_unref); + + if (window_data->monitor_connection_id) + g_signal_handler_disconnect(topmenu_monitor_get_instance(), + window_data->monitor_connection_id); + + if (window_data->appmenu.menu) + topmenu_appmenu_destroy(&window_data->appmenu); + + if (window_data->appmenubar) + gtk_widget_destroy(GTK_WIDGET(window_data->appmenubar)); + + g_slice_free (WindowData, window_data); + } +} + +static MenuShellData * +menu_shell_data_new (void) +{ + return g_slice_new0 (MenuShellData); +} + +static void +menu_shell_data_free (gpointer data) +{ + if (data != NULL) + g_slice_free (MenuShellData, data); +} + +static MenuItemData * +menu_item_data_new (void) +{ + return g_slice_new0 (MenuItemData); +} + +static void +menu_item_data_free (gpointer data) +{ + if (data != NULL) { + MenuItemData *item_data = data; + + if (item_data->proxy) + gtk_widget_destroy (GTK_WIDGET (item_data->proxy)); + + g_slice_free (MenuItemData, data); + } +} + +MenuItemData * +topmenu_get_menu_item_data (GtkMenuItem *menu_item) +{ + MenuItemData *menu_item_data; + + g_return_val_if_fail (GTK_IS_MENU_ITEM (menu_item), NULL); + + menu_item_data = g_object_get_qdata (G_OBJECT (menu_item), menu_item_data_quark ()); + + if (menu_item_data == NULL) + { + menu_item_data = menu_item_data_new (); + + g_object_set_qdata_full (G_OBJECT (menu_item), menu_item_data_quark (), menu_item_data, menu_item_data_free); + } + + return menu_item_data; +} + +MenuShellData * +topmenu_get_menu_shell_data (GtkMenuShell *menu_shell) +{ + MenuShellData *menu_shell_data; + + g_return_val_if_fail (GTK_IS_MENU_SHELL (menu_shell), NULL); + + menu_shell_data = g_object_get_qdata (G_OBJECT (menu_shell), menu_shell_data_quark ()); + + if (menu_shell_data == NULL) + { + menu_shell_data = menu_shell_data_new (); + + g_object_set_qdata_full (G_OBJECT (menu_shell), menu_shell_data_quark (), menu_shell_data, menu_shell_data_free); + } + + return menu_shell_data; +} + +WindowData * +topmenu_get_window_data (GtkWindow *window) +{ + WindowData *window_data; + + g_return_val_if_fail (GTK_IS_WINDOW (window), NULL); + + window_data = g_object_get_qdata (G_OBJECT (window), window_data_quark ()); + + if (window_data == NULL) + { + if (topmenu_is_window_blacklisted (window)) + return NULL; + + window_data = window_data_new (); + + // Nothing to initialize right now. + + g_object_set_qdata_full (G_OBJECT (window), window_data_quark (), window_data, window_data_free); + } + + return window_data; +} + +void +topmenu_remove_window_data (GtkWindow *window) +{ + g_object_set_qdata (G_OBJECT (window), window_data_quark (), NULL); +} diff --git a/module/data.h b/module/data.h new file mode 100644 index 0000000..9bb643f --- /dev/null +++ b/module/data.h @@ -0,0 +1,48 @@ +#ifndef _DATA_H_ +#define _DATA_H_ + +#include + +#include "../libtopmenu-client/topmenu-appmenubar.h" +#include "appmenu.h" + +typedef struct _WindowData WindowData; +typedef struct _MenuShellData MenuShellData; +typedef struct _MenuItemData MenuItemData; + +struct _WindowData +{ + GSList *menus; + TopMenuAppMenuBar *appmenubar; + AppMenu appmenu; + gulong monitor_connection_id; +}; + +struct _MenuShellData +{ + GtkWindow *window; +}; + +struct _MenuItemData +{ + GtkMenuItem *proxy; +}; +gboolean +topmenu_is_blacklisted (void); + +gboolean +topmenu_is_window_blacklisted (GtkWindow *window); + +WindowData * +topmenu_get_window_data (GtkWindow *window); + +void +topmenu_remove_window_data (GtkWindow *window); + +MenuShellData * +topmenu_get_menu_shell_data (GtkMenuShell *menu_shell); + +MenuItemData * +topmenu_get_menu_item_data (GtkMenuItem *menu_item); + +#endif diff --git a/module/main.c b/module/main.c new file mode 100644 index 0000000..42ea332 --- /dev/null +++ b/module/main.c @@ -0,0 +1,724 @@ +/* + * Copyright 2012 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * Authors: Ryan Lortie + * William Hua + */ + +#include +#include + +#include "../global.h" +#include "../libtopmenu-client/topmenu-client.h" +#include "../libtopmenu-client/topmenu-monitor.h" + +#include "menuitem-proxy.h" +#include "appmenu.h" +#include "data.h" + +static gboolean already_initialized = FALSE; + +static void (* pre_hijacked_window_realize) (GtkWidget *widget); + +static void (* pre_hijacked_window_unrealize) (GtkWidget *widget); + +#if GTK_MAJOR_VERSION == 3 +static void (* pre_hijacked_application_window_realize) (GtkWidget *widget); +#endif + +static void (* pre_hijacked_menu_bar_realize) (GtkWidget *widget); + +static void (* pre_hijacked_menu_bar_unrealize) (GtkWidget *widget); + +static void (* pre_hijacked_widget_size_allocate) (GtkWidget *widget, + GtkAllocation *allocation); + +static void (* pre_hijacked_menu_bar_size_allocate) (GtkWidget *widget, + GtkAllocation *allocation); + +#if GTK_MAJOR_VERSION == 2 +static void (* pre_hijacked_menu_bar_size_request) (GtkWidget *widget, + GtkRequisition *requisition); +#elif GTK_MAJOR_VERSION == 3 +static void (* pre_hijacked_menu_bar_get_preferred_width) (GtkWidget *widget, + gint *minimum_width, + gint *natural_width); + +static void (* pre_hijacked_menu_bar_get_preferred_height) (GtkWidget *widget, + gint *minimum_height, + gint *natural_height); + +static void (* pre_hijacked_menu_bar_get_preferred_width_for_height) (GtkWidget *widget, + gint height, + gint *minimum_width, + gint *natural_width); + +static void (* pre_hijacked_menu_bar_get_preferred_height_for_width) (GtkWidget *widget, + gint width, + gint *minimum_height, + gint *natural_height); +#endif + +static void +handle_should_hide_menubar_updated (GObject *object, + GParamSpec *pspec, + gpointer user_data); + +static void +count_container_items_helper (GtkWidget *widget, gpointer data) +{ + gint *count = data; + (*count)++; +} + +static gint +count_container_items (GtkContainer *container) +{ + gint count = 0; + gtk_container_foreach (container, count_container_items_helper, &count); + return count; +} + + + + +static gboolean +topmenu_should_hide_menubar_on_window (GtkWindow *window) +{ + TopMenuMonitor *monitor = topmenu_monitor_get_instance (); + + if (topmenu_is_window_blacklisted (window)) + return FALSE; + + return monitor->available; +} + +static gboolean +topmenu_should_hide_menubar (GtkWidget *widget) +{ + GtkWindow *window = GTK_WINDOW (gtk_widget_get_toplevel (widget)); + + g_return_val_if_fail (GTK_IS_MENU_SHELL (widget), FALSE); + + return topmenu_should_hide_menubar_on_window (window); +} + +static void +topmenu_prepare_window (GtkWindow *window) +{ + WindowData *window_data; + + g_return_if_fail (GTK_IS_WINDOW (window)); + g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (window))); + + window_data = topmenu_get_window_data (window); + + if (window_data == NULL) + return; // Window is ignored + + if (window_data->appmenubar != 0) + return; // Already prepared + + window_data->appmenubar = TOPMENU_APP_MENU_BAR (topmenu_app_menu_bar_new ()); + gtk_widget_show(GTK_WIDGET(window_data->appmenubar)); + + topmenu_app_menu_bar_set_app_menu (window_data->appmenubar, + topmenu_appmenu_build(&window_data->appmenu)); +} + +static void +topmenu_connect_window (GtkWindow *window) +{ + WindowData *window_data; + + g_return_if_fail (GTK_IS_WINDOW (window)); + g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (window))); + + window_data = topmenu_get_window_data (window); + + if (window_data == NULL) + return; // Window is ignored + + if (window_data->monitor_connection_id != 0) + return; // Already connected + + g_return_if_fail (window_data->menus != NULL); // Must contain one menu at least. + g_return_if_fail (window_data->appmenubar != NULL); // Must be prepared + + TopMenuMonitor *monitor = topmenu_monitor_get_instance(); + window_data->monitor_connection_id = g_signal_connect(monitor, "notify::available", + G_CALLBACK (handle_should_hide_menubar_updated), window); + + topmenu_client_connect_window_widget (gtk_widget_get_window (GTK_WIDGET (window)), + GTK_WIDGET (window_data->appmenubar)); +} + +static void +topmenu_disconnect_window (GtkWindow *window) +{ + WindowData *window_data; + + g_return_if_fail (GTK_IS_WINDOW (window)); + + window_data = topmenu_get_window_data (window); + + if (window_data == NULL) + return; // Already disconnected or ignored + + if (window_data->monitor_connection_id == 0) + return; // Already disconnected + + TopMenuMonitor *monitor = topmenu_monitor_get_instance(); + g_signal_handler_disconnect(monitor, window_data->monitor_connection_id); + window_data->monitor_connection_id = 0; + + if (window_data->appmenu.menu) + topmenu_appmenu_destroy(&window_data->appmenu); + + if (window_data->appmenubar) + { + gtk_widget_destroy (GTK_WIDGET (window_data->appmenubar)); + window_data->appmenubar = NULL; + } + + if (gtk_widget_get_realized (GTK_WIDGET(window))) + { + topmenu_client_disconnect_window (gtk_widget_get_window (GTK_WIDGET (window))); + } +} + +static gint +compute_shell_position_in_appmenu (WindowData *window_data, GtkMenuShell *menu_shell) +{ + GSList *iter; + gint position = 1; // Skip app_menu_item + + for (iter = window_data->menus; iter; iter = g_slist_next (iter)) + { + if (iter->data == menu_shell) { + return position; + } + + position += count_container_items (GTK_CONTAINER (iter->data)); + } + + return -1; +} + +static void +add_menu_item_to_appmenu (WindowData *window_data, GtkMenuItem *item, gint position) +{ + MenuItemData *item_data = topmenu_get_menu_item_data (item); + item_data->proxy = topmenu_create_proxy_menu_item (item); + + gtk_menu_shell_insert (GTK_MENU_SHELL (window_data->appmenubar), + GTK_WIDGET (item_data->proxy), position); +} + +static void +remove_menu_item_from_appmenu (WindowData *window_data, GtkMenuItem *item) +{ + MenuItemData *item_data = topmenu_get_menu_item_data (item); + + if (item_data->proxy) { + gtk_widget_destroy (GTK_WIDGET (item_data->proxy)); + item_data->proxy = NULL; + } +} + +static void +handle_shell_insert (GtkMenuShell *menu_shell, GtkWidget *child, gint position, WindowData *window_data) +{ + GtkMenuItem *item = GTK_MENU_ITEM (child); + gint offset = compute_shell_position_in_appmenu (window_data, menu_shell); + g_return_if_fail (offset >= 0); + + add_menu_item_to_appmenu (window_data, item, offset + position); +} + +static void +handle_shell_remove (GtkMenuShell *menu_shell, GtkWidget *widget, WindowData *window_data) +{ + GtkMenuItem *item = GTK_MENU_ITEM (widget); + remove_menu_item_from_appmenu (window_data, item); +} + +typedef struct _AddShellCbData +{ + WindowData *window_data; + gint position; +} AddShellCbData; + +static void +add_shell_cb (GtkWidget *widget, gpointer user_data) +{ + AddShellCbData *data = user_data; + GtkMenuItem *item = GTK_MENU_ITEM (widget); + add_menu_item_to_appmenu (data->window_data, item, data->position); + + data->position++; +} + +static void +add_shell_to_appmenu (WindowData *window_data, GtkMenuShell *menu_shell) +{ + AddShellCbData data; + data.window_data = window_data; + data.position = compute_shell_position_in_appmenu (window_data, menu_shell); + + g_warn_if_fail (data.position >= 0); + + gtk_container_foreach (GTK_CONTAINER (menu_shell), + add_shell_cb, &data); + + topmenu_appmenu_scan_for_items (&window_data->appmenu, menu_shell); + + g_signal_connect (menu_shell, "insert", + G_CALLBACK (handle_shell_insert), window_data); + g_signal_connect (menu_shell, "remove", + G_CALLBACK (handle_shell_remove), window_data); +} + +static void +remove_shell_cb (GtkWidget *widget, gpointer user_data) +{ + WindowData *window_data = user_data; + GtkMenuItem *item = GTK_MENU_ITEM (widget); + remove_menu_item_from_appmenu (window_data, item); +} + +static void +remove_shell_from_appmenu (WindowData *window_data, GtkMenuShell *menu_shell) +{ + gtk_container_foreach (GTK_CONTAINER (menu_shell), + remove_shell_cb, window_data); + + g_signal_handlers_disconnect_by_data (menu_shell, window_data); +} + +static void +topmenu_disconnect_menu_shell (GtkWindow *window, + GtkMenuShell *menu_shell) +{ + WindowData *window_data; + MenuShellData *menu_shell_data; + + g_return_if_fail (GTK_IS_WINDOW (window)); + g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell)); + + menu_shell_data = topmenu_get_menu_shell_data (menu_shell); + + g_warn_if_fail (window == menu_shell_data->window); + + window_data = topmenu_get_window_data (menu_shell_data->window); + + if (window_data != NULL) + { + GSList *iter; + + for (iter = window_data->menus; iter != NULL; iter = g_slist_next (iter)) + if (GTK_MENU_SHELL(iter->data) == menu_shell) + break; + + if (iter != NULL) + { + GtkMenuShell *menu_shell = GTK_MENU_SHELL(iter->data); + + remove_shell_from_appmenu (window_data, menu_shell); + + window_data->menus = g_slist_delete_link (window_data->menus, iter); + } + } + + menu_shell_data->window = NULL; +} + +static void +topmenu_connect_menu_shell (GtkWindow *window, + GtkMenuShell *menu_shell) +{ + MenuShellData *menu_shell_data; + + g_return_if_fail (GTK_IS_WINDOW (window)); + g_return_if_fail (GTK_IS_MENU_SHELL (menu_shell)); + + menu_shell_data = topmenu_get_menu_shell_data (menu_shell); + + if (window != menu_shell_data->window) + { + WindowData *window_data; + + if (menu_shell_data->window != NULL) + topmenu_disconnect_menu_shell (menu_shell_data->window, menu_shell); + + window_data = topmenu_get_window_data (window); + + if (window_data != NULL) + { + GSList *iter; + + for (iter = window_data->menus; iter != NULL; iter = g_slist_next (iter)) + if (GTK_MENU_SHELL(iter->data) == menu_shell) + break; + + if (iter == NULL) + { + topmenu_prepare_window (window); + + window_data->menus = g_slist_append (window_data->menus, menu_shell); + + add_shell_to_appmenu (window_data, menu_shell); + + topmenu_connect_window (window); // Does nothing if already connected + } + } + + menu_shell_data->window = window; + } +} + +static void +handle_should_hide_menubar_updated (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + g_return_if_fail (GTK_IS_WINDOW (user_data)); + + GtkWindow *window = GTK_WINDOW (user_data); + WindowData *window_data = topmenu_get_window_data (window); + GSList *iter; + + for (iter = window_data->menus; iter != NULL; iter = g_slist_next (iter)) + { + gtk_widget_queue_resize (GTK_WIDGET (iter->data)); + } +} + +static void +hijacked_window_realize (GtkWidget *widget) +{ + g_return_if_fail (GTK_IS_WINDOW (widget)); + + if (pre_hijacked_window_realize != NULL) + (* pre_hijacked_window_realize) (widget); + +#if GTK_MAJOR_VERSION == 3 + if (!GTK_IS_APPLICATION_WINDOW (widget)) +#endif + topmenu_get_window_data (GTK_WINDOW (widget)); +} + +static void +hijacked_window_unrealize (GtkWidget *widget) +{ + g_return_if_fail (GTK_IS_WINDOW (widget)); + + if (pre_hijacked_window_unrealize != NULL) + (* pre_hijacked_window_unrealize) (widget); + + topmenu_disconnect_window (GTK_WINDOW (widget)); + topmenu_remove_window_data (GTK_WINDOW (widget)); +} + +#if GTK_MAJOR_VERSION == 3 +static void +hijacked_application_window_realize (GtkWidget *widget) +{ + g_return_if_fail (GTK_IS_APPLICATION_WINDOW (widget)); + + if (pre_hijacked_application_window_realize != NULL) + (* pre_hijacked_application_window_realize) (widget); + + gtk_window_get_window_data (GTK_WINDOW (widget)); +} +#endif + +static void +hijacked_menu_bar_realize (GtkWidget *widget) +{ + GtkWidget *window; + + g_return_if_fail (GTK_IS_MENU_BAR (widget)); + + if (pre_hijacked_menu_bar_realize != NULL) + (* pre_hijacked_menu_bar_realize) (widget); + + window = gtk_widget_get_toplevel (widget); + + if (GTK_IS_WINDOW (window)) + topmenu_connect_menu_shell (GTK_WINDOW (window), GTK_MENU_SHELL (widget)); +} + +static void +hijacked_menu_bar_unrealize (GtkWidget *widget) +{ + MenuShellData *menu_shell_data; + + g_return_if_fail (GTK_IS_MENU_BAR (widget)); + + if (pre_hijacked_menu_bar_unrealize != NULL) + (* pre_hijacked_menu_bar_unrealize) (widget); + + menu_shell_data = topmenu_get_menu_shell_data (GTK_MENU_SHELL (widget)); + + if (menu_shell_data->window != NULL) + topmenu_disconnect_menu_shell (menu_shell_data->window, GTK_MENU_SHELL (widget)); +} + +static void +hijacked_menu_bar_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + GtkAllocation zero = { 0, 0, 0, 0 }; + GdkWindow *window; + + g_return_if_fail (GTK_IS_MENU_BAR (widget)); + + if (topmenu_should_hide_menubar (widget)) + { + /* + * We manually assign an empty allocation to the menu bar to + * prevent the container from attempting to draw it at all. + */ + if (pre_hijacked_widget_size_allocate != NULL) + (* pre_hijacked_widget_size_allocate) (widget, &zero); + + /* + * Then we move the GdkWindow belonging to the menu bar outside of + * the clipping rectangle of the parent window so that we can't + * see it. + */ + window = gtk_widget_get_window (widget); + + if (window != NULL) + gdk_window_move_resize (window, -1, -1, 1, 1); + } + else if (pre_hijacked_menu_bar_size_allocate != NULL) + (* pre_hijacked_menu_bar_size_allocate) (widget, allocation); +} + +#if GTK_MAJOR_VERSION == 2 +static void +hijacked_menu_bar_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + g_return_if_fail (GTK_IS_MENU_BAR (widget)); + + if (pre_hijacked_menu_bar_size_request != NULL) + (* pre_hijacked_menu_bar_size_request) (widget, requisition); + + if (topmenu_should_hide_menubar(widget)) + { + requisition->width = 0; + requisition->height = 0; + } +} +#elif GTK_MAJOR_VERSION == 3 +static void +hijacked_menu_bar_get_preferred_width (GtkWidget *widget, + gint *minimum_width, + gint *natural_width) +{ + g_return_if_fail (GTK_IS_MENU_BAR (widget)); + + if (pre_hijacked_menu_bar_get_preferred_width != NULL) + (* pre_hijacked_menu_bar_get_preferred_width) (widget, minimum_width, natural_width); + + if (gtk_widget_shell_shows_menubar (widget)) + { + *minimum_width = 0; + *natural_width = 0; + } +} + +static void +hijacked_menu_bar_get_preferred_height (GtkWidget *widget, + gint *minimum_height, + gint *natural_height) +{ + g_return_if_fail (GTK_IS_MENU_BAR (widget)); + + if (pre_hijacked_menu_bar_get_preferred_height != NULL) + (* pre_hijacked_menu_bar_get_preferred_height) (widget, minimum_height, natural_height); + + if (gtk_widget_shell_shows_menubar (widget)) + { + *minimum_height = 0; + *natural_height = 0; + } +} + +static void +hijacked_menu_bar_get_preferred_width_for_height (GtkWidget *widget, + gint height, + gint *minimum_width, + gint *natural_width) +{ + g_return_if_fail (GTK_IS_MENU_BAR (widget)); + + if (pre_hijacked_menu_bar_get_preferred_width_for_height != NULL) + (* pre_hijacked_menu_bar_get_preferred_width_for_height) (widget, height, minimum_width, natural_width); + + if (gtk_widget_shell_shows_menubar (widget)) + { + *minimum_width = 0; + *natural_width = 0; + } +} + +static void +hijacked_menu_bar_get_preferred_height_for_width (GtkWidget *widget, + gint width, + gint *minimum_height, + gint *natural_height) +{ + g_return_if_fail (GTK_IS_MENU_BAR (widget)); + + if (pre_hijacked_menu_bar_get_preferred_height_for_width != NULL) + (* pre_hijacked_menu_bar_get_preferred_height_for_width) (widget, width, minimum_height, natural_height); + + if (gtk_widget_shell_shows_menubar (widget)) + { + *minimum_height = 0; + *natural_height = 0; + } +} +#endif + +static void +hijack_window_class_vtable (GType type) +{ + GtkWidgetClass *widget_class = g_type_class_ref (type); + GType *children; + guint n; + guint i; + + if (widget_class->realize == pre_hijacked_window_realize) + widget_class->realize = hijacked_window_realize; + +#if GTK_MAJOR_VERSION == 3 + if (widget_class->realize == pre_hijacked_application_window_realize) + widget_class->realize = hijacked_application_window_realize; +#endif + + if (widget_class->unrealize == pre_hijacked_window_unrealize) + widget_class->unrealize = hijacked_window_unrealize; + + children = g_type_children (type, &n); + + for (i = 0; i < n; i++) + hijack_window_class_vtable (children[i]); + + g_free (children); +} + +static void +hijack_menu_bar_class_vtable (GType type) +{ + GtkWidgetClass *widget_class = g_type_class_ref (type); + GType *children; + guint n; + guint i; + + /* This fixes lp:1113008. */ + widget_class->hierarchy_changed = NULL; + + if (widget_class->realize == pre_hijacked_menu_bar_realize) + widget_class->realize = hijacked_menu_bar_realize; + + if (widget_class->unrealize == pre_hijacked_menu_bar_unrealize) + widget_class->unrealize = hijacked_menu_bar_unrealize; + + if (widget_class->size_allocate == pre_hijacked_menu_bar_size_allocate) + widget_class->size_allocate = hijacked_menu_bar_size_allocate; + +#if GTK_MAJOR_VERSION == 2 + if (widget_class->size_request == pre_hijacked_menu_bar_size_request) + widget_class->size_request = hijacked_menu_bar_size_request; +#elif GTK_MAJOR_VERSION == 3 + if (widget_class->get_preferred_width == pre_hijacked_menu_bar_get_preferred_width) + widget_class->get_preferred_width = hijacked_menu_bar_get_preferred_width; + + if (widget_class->get_preferred_height == pre_hijacked_menu_bar_get_preferred_height) + widget_class->get_preferred_height = hijacked_menu_bar_get_preferred_height; + + if (widget_class->get_preferred_width_for_height == pre_hijacked_menu_bar_get_preferred_width_for_height) + widget_class->get_preferred_width_for_height = hijacked_menu_bar_get_preferred_width_for_height; + + if (widget_class->get_preferred_height_for_width == pre_hijacked_menu_bar_get_preferred_height_for_width) + widget_class->get_preferred_height_for_width = hijacked_menu_bar_get_preferred_height_for_width; +#endif + + children = g_type_children (type, &n); + + for (i = 0; i < n; i++) + hijack_menu_bar_class_vtable (children[i]); + + g_free (children); +} + +G_MODULE_EXPORT +void gtk_module_init(void) +{ + if (!topmenu_is_blacklisted()) + { + GtkWidgetClass *widget_class; + + /* gtk_module_init may be called more than once in a resident module */ + g_return_if_fail(!already_initialized); + already_initialized = TRUE; + + /* store the base GtkWidget size_allocate vfunc */ + widget_class = g_type_class_ref (GTK_TYPE_WIDGET); + pre_hijacked_widget_size_allocate = widget_class->size_allocate; + +#if GTK_MAJOR_VERSION == 3 + /* store the base GtkApplicationWindow realize vfunc */ + widget_class = g_type_class_ref (GTK_TYPE_APPLICATION_WINDOW); + pre_hijacked_application_window_realize = widget_class->realize; +#endif + + /* intercept window realize vcalls on GtkWindow */ + widget_class = g_type_class_ref (GTK_TYPE_WINDOW); + pre_hijacked_window_realize = widget_class->realize; + pre_hijacked_window_unrealize = widget_class->unrealize; + hijack_window_class_vtable (GTK_TYPE_WINDOW); + + /* intercept size request and allocate vcalls on GtkMenuBar (for hiding) */ + widget_class = g_type_class_ref (GTK_TYPE_MENU_BAR); + pre_hijacked_menu_bar_realize = widget_class->realize; + pre_hijacked_menu_bar_unrealize = widget_class->unrealize; + pre_hijacked_menu_bar_size_allocate = widget_class->size_allocate; +#if GTK_MAJOR_VERSION == 2 + pre_hijacked_menu_bar_size_request = widget_class->size_request; +#elif GTK_MAJOR_VERSION == 3 + pre_hijacked_menu_bar_get_preferred_width = widget_class->get_preferred_width; + pre_hijacked_menu_bar_get_preferred_height = widget_class->get_preferred_height; + pre_hijacked_menu_bar_get_preferred_width_for_height = widget_class->get_preferred_width_for_height; + pre_hijacked_menu_bar_get_preferred_height_for_width = widget_class->get_preferred_height_for_width; +#endif + hijack_menu_bar_class_vtable (GTK_TYPE_MENU_BAR); + } +} + +G_MODULE_EXPORT +const gchar * g_module_check_init(GModule *module) +{ + /* It is hard to unhijack Gtk's vtables, specially if some other module + * has decided to also hijack them. + * Thus, we just prevent unloading of this module. */ + g_module_make_resident(module); + return NULL; +} diff --git a/module/menuitem-proxy.c b/module/menuitem-proxy.c new file mode 100644 index 0000000..6a3e555 --- /dev/null +++ b/module/menuitem-proxy.c @@ -0,0 +1,421 @@ +/* Contains code from GTK - The GIMP Toolkit + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "../libtopmenu-client/topmenu-monitor.h" + +#include "menuitem-proxy.h" + +static gboolean static_data_ok = FALSE; +static const gchar *pname_visible; +static const gchar *pname_sensitive; +static const gchar *pname_label; +static const gchar *pname_submenu; + +static void init_static_data() +{ + if (!static_data_ok) { + pname_visible = g_intern_string("visible"); + pname_sensitive = g_intern_string("sensitive"); + pname_label = g_intern_string("label"); + pname_submenu = g_intern_string("submenu"); + + static_data_ok = TRUE; + } +} + +static void +free_timeval (GTimeVal *val) +{ + g_slice_free (GTimeVal, val); +} + +static void +get_offsets (GtkMenu *menu, + gint *horizontal_offset, + gint *vertical_offset) +{ + gint vertical_padding; + gint horizontal_padding; + + gtk_widget_style_get (GTK_WIDGET (menu), + "horizontal-offset", horizontal_offset, + "vertical-offset", vertical_offset, + "horizontal-padding", &horizontal_padding, + "vertical-padding", &vertical_padding, + NULL); + + *vertical_offset -= GTK_WIDGET (menu)->style->ythickness; + *vertical_offset -= vertical_padding; + *horizontal_offset += horizontal_padding; +} + +static void +menu_item_position_menu (GtkMenu *menu, + gint *x, + gint *y, + gboolean *push_in, + gpointer user_data) +{ + GtkMenuItem *menu_item; + GtkWidget *widget; + GtkMenuItem *parent_menu_item; + GdkScreen *screen; + gint twidth, theight; + gint tx, ty; + GtkTextDirection direction; + GdkRectangle monitor; + gint monitor_num; + gint horizontal_offset; + gint vertical_offset; + gint parent_xthickness; + gint available_left, available_right; + + g_return_if_fail (menu != NULL); + g_return_if_fail (x != NULL); + g_return_if_fail (y != NULL); + + menu_item = GTK_MENU_ITEM (user_data); + widget = GTK_WIDGET (user_data); + + if (push_in) + *push_in = FALSE; + + direction = gtk_widget_get_direction (widget); + + twidth = GTK_WIDGET (menu)->requisition.width; + theight = GTK_WIDGET (menu)->requisition.height; + + screen = gtk_widget_get_screen (GTK_WIDGET (menu)); + monitor_num = gdk_screen_get_monitor_at_window (screen, menu_item->event_window); + if (monitor_num < 0) + monitor_num = 0; + gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor); + + if (!gdk_window_get_origin (widget->window, &tx, &ty)) + { + g_warning ("Menu not on screen"); + return; + } + + tx += widget->allocation.x; + ty += widget->allocation.y; + + get_offsets (menu, &horizontal_offset, &vertical_offset); + + available_left = tx - monitor.x; + available_right = monitor.x + monitor.width - (tx + widget->allocation.width); + + if (GTK_IS_MENU_BAR (widget->parent)) + { + menu_item->from_menubar = TRUE; + } + else if (GTK_IS_MENU (widget->parent)) + { + if (GTK_MENU (widget->parent)->parent_menu_item) + menu_item->from_menubar = GTK_MENU_ITEM (GTK_MENU (widget->parent)->parent_menu_item)->from_menubar; + else + menu_item->from_menubar = FALSE; + } + else + { + menu_item->from_menubar = FALSE; + } + + switch (menu_item->submenu_placement) + { + case GTK_TOP_BOTTOM: + if (direction == GTK_TEXT_DIR_LTR) + menu_item->submenu_direction = GTK_DIRECTION_RIGHT; + else + { + menu_item->submenu_direction = GTK_DIRECTION_LEFT; + tx += widget->allocation.width - twidth; + } + if ((ty + widget->allocation.height + theight) <= monitor.y + monitor.height) + ty += widget->allocation.height; + else if ((ty - theight) >= monitor.y) + ty -= theight; + else if (monitor.y + monitor.height - (ty + widget->allocation.height) > ty) + ty += widget->allocation.height; + else + ty -= theight; + break; + + case GTK_LEFT_RIGHT: + if (GTK_IS_MENU (widget->parent)) + parent_menu_item = GTK_MENU_ITEM (GTK_MENU (widget->parent)->parent_menu_item); + else + parent_menu_item = NULL; + + parent_xthickness = widget->parent->style->xthickness; + + if (parent_menu_item && !GTK_MENU (widget->parent)->torn_off) + { + menu_item->submenu_direction = parent_menu_item->submenu_direction; + } + else + { + if (direction == GTK_TEXT_DIR_LTR) + menu_item->submenu_direction = GTK_DIRECTION_RIGHT; + else + menu_item->submenu_direction = GTK_DIRECTION_LEFT; + } + + switch (menu_item->submenu_direction) + { + case GTK_DIRECTION_LEFT: + if (tx - twidth - parent_xthickness - horizontal_offset >= monitor.x || + available_left >= available_right) + tx -= twidth + parent_xthickness + horizontal_offset; + else + { + menu_item->submenu_direction = GTK_DIRECTION_RIGHT; + tx += widget->allocation.width + parent_xthickness + horizontal_offset; + } + break; + + case GTK_DIRECTION_RIGHT: + if (tx + widget->allocation.width + parent_xthickness + horizontal_offset + twidth <= monitor.x + monitor.width || + available_right >= available_left) + tx += widget->allocation.width + parent_xthickness + horizontal_offset; + else + { + menu_item->submenu_direction = GTK_DIRECTION_LEFT; + tx -= twidth + parent_xthickness + horizontal_offset; + } + break; + } + + ty += vertical_offset; + + /* If the height of the menu doesn't fit we move it upward. */ + ty = CLAMP (ty, monitor.y, MAX (monitor.y, monitor.y + monitor.height - theight)); + break; + } + + /* If we have negative, tx, here it is because we can't get + * the menu all the way on screen. Favor the left portion. + */ + *x = CLAMP (tx, monitor.x, MAX (monitor.x, monitor.x + monitor.width - twidth)); + *y = ty; + + gtk_menu_set_monitor (menu, monitor_num); + + if (!gtk_widget_get_visible (menu->toplevel)) + { + gtk_window_set_type_hint (GTK_WINDOW (menu->toplevel), menu_item->from_menubar? + GDK_WINDOW_TYPE_HINT_DROPDOWN_MENU : GDK_WINDOW_TYPE_HINT_POPUP_MENU); + } +} + +static void handle_menuitem_notify(GtkMenuItem *item, GParamSpec *pspec, GtkMenuItem *proxy) +{ + // Note that it is OK to compare strings by pointer as they are all interned + if (pspec->name == pname_submenu) { + // Nothing to do! + } else if (pspec->name == pname_label) { + const gchar *label = gtk_menu_item_get_label(item); + gtk_menu_item_set_label(proxy, label); + } else if (pspec->name == pname_sensitive) { + gtk_widget_set_sensitive(GTK_WIDGET(proxy), + gtk_widget_get_sensitive(GTK_WIDGET(item))); + } else if (pspec->name == pname_visible) { + if (gtk_widget_get_visible(GTK_WIDGET(item))) { + gtk_widget_show(GTK_WIDGET(proxy)); + } else { + gtk_widget_hide(GTK_WIDGET(proxy)); + } + } +} + +static gboolean handle_menuitem_mnemonic_activate(GtkMenuItem *item, gboolean cycling, GtkMenuItem *proxy) +{ + TopMenuMonitor *monitor = topmenu_monitor_get_instance(); + GtkWidget *parent = gtk_widget_get_parent(GTK_WIDGET(proxy)); + + if (parent && monitor->available) { + GtkMenuShell *parent_shell = GTK_MENU_SHELL(parent); + if (GTK_IS_MENU_BAR(parent_shell) || parent_shell->active) { + gtk_widget_mnemonic_activate(GTK_WIDGET(proxy), cycling); + return TRUE; + } + } + + return FALSE; +} + +static gboolean handle_menu_leave_notify(GtkMenu *menu, GdkEvent *event, GtkMenuItem *item) +{ + return TRUE; +} + +static void handle_parent_move_current(GtkMenuShell *shell, GtkMenuDirectionType dir, GtkMenuItem *item) +{ + if (dir == GTK_MENU_DIR_CHILD) { + GtkWidget *submenu = gtk_menu_item_get_submenu(item); + if (submenu) { + gtk_menu_shell_select_first(GTK_MENU_SHELL(submenu), TRUE); + } + } +} + +static void handle_proxy_select(GtkMenuItem *proxy, GtkMenuItem *item) +{ + GtkWidget *submenu = gtk_menu_item_get_submenu(item); + + if (submenu) { + if (!gtk_widget_is_sensitive(GTK_WIDGET(submenu))) + return; + + GtkMenuShell *parent_shell = GTK_MENU_SHELL(GTK_WIDGET(proxy)->parent); + GTimeVal *popup_time = g_slice_new0(GTimeVal); + + g_get_current_time(popup_time); + g_object_set_data_full(G_OBJECT(submenu), + "gtk-menu-exact-popup-time", popup_time, + (GDestroyNotify) free_timeval); + + g_signal_connect_object(submenu, "leave-notify-event", + G_CALLBACK(handle_menu_leave_notify), item, 0); + + g_signal_connect_object(gtk_widget_get_parent(GTK_WIDGET(proxy)), "move-current", + G_CALLBACK(handle_parent_move_current), item, 0); + + gtk_menu_popup(GTK_MENU(submenu), + GTK_WIDGET(parent_shell), + GTK_WIDGET(proxy), + menu_item_position_menu, + proxy, + parent_shell->button, + 0); + } +} + +static void handle_proxy_deselect(GtkMenuItem *proxy, GtkMenuItem *item) +{ + GtkWidget *submenu = gtk_menu_item_get_submenu(item); + + if (submenu) { + g_signal_handlers_disconnect_by_func(submenu, handle_menu_leave_notify, item); + g_signal_handlers_disconnect_by_func(gtk_widget_get_parent(GTK_WIDGET(proxy)), + handle_parent_move_current, item); + gtk_menu_popdown(GTK_MENU(submenu)); + } +} + +static void handle_proxy_activate(GtkMenuItem *proxy, GtkMenuItem *item) +{ + GtkWidget *submenu = gtk_menu_item_get_submenu(item); + + if (submenu) { + // Do nothing! + } else { + gtk_menu_item_activate(item); + } +} + +static void handle_proxy_activate_item(GtkMenuItem *proxy, GtkMenuItem *item) +{ + GtkWidget *submenu = gtk_menu_item_get_submenu(item); + + if (submenu) { + GtkMenuShell *parent = GTK_MENU_SHELL(gtk_widget_get_parent(GTK_WIDGET(proxy))); + if (parent) { + if (!parent->active) { + //gtk_grab_add(GTK_WIDGET(parent)); + //parent->have_grab = TRUE; + parent->active = TRUE; + } + gtk_menu_shell_select_item(parent, GTK_WIDGET(proxy)); + gtk_menu_shell_select_first(GTK_MENU_SHELL(submenu), TRUE); + } + } +} + +static GtkWidget *construct_image_widget_proxy(GtkImage *widget) +{ + GtkImageType itype = gtk_image_get_storage_type(widget); + gchar *icon_name; + GtkIconSize icon_size; + switch (itype) { + case GTK_IMAGE_STOCK: + gtk_image_get_stock(widget, &icon_name, &icon_size); + return gtk_image_new_from_stock(icon_name, icon_size); + case GTK_IMAGE_EMPTY: + return gtk_image_new(); + default: + return gtk_image_new(); + } +} + +GtkMenuItem *topmenu_create_proxy_menu_item(GtkMenuItem *item) +{ + init_static_data(); + + GtkMenuItem *proxy = NULL; + + const gchar *label = gtk_menu_item_get_label(item); + + if (GTK_IS_IMAGE_MENU_ITEM(item)) { + GtkImageMenuItem *iitem = GTK_IMAGE_MENU_ITEM(item); + if (gtk_image_menu_item_get_use_stock(iitem)) { + proxy = GTK_MENU_ITEM(gtk_image_menu_item_new_from_stock(label, NULL)); + } else { + GtkWidget *iwidget = gtk_image_menu_item_get_image(iitem); + proxy = GTK_MENU_ITEM(gtk_image_menu_item_new_with_mnemonic(label)); + if (iwidget) { + // Let's suppport some common widget types + if (GTK_IS_IMAGE(iwidget)) { + gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(proxy), + construct_image_widget_proxy(GTK_IMAGE(iwidget))); + } + } + } + gtk_image_menu_item_set_always_show_image(GTK_IMAGE_MENU_ITEM(proxy), + gtk_image_menu_item_get_always_show_image(iitem)); + } + else if (GTK_IS_SEPARATOR_MENU_ITEM(item)) { + proxy = GTK_MENU_ITEM(gtk_separator_menu_item_new()); + } else { + proxy = GTK_MENU_ITEM(gtk_menu_item_new_with_mnemonic(label)); + } + + gtk_widget_set_sensitive(GTK_WIDGET(proxy), + gtk_widget_get_sensitive(GTK_WIDGET(item))); + + if (gtk_widget_get_visible(GTK_WIDGET(item))) { + gtk_widget_show(GTK_WIDGET(proxy)); + } + + g_signal_connect_object(item, "notify", + G_CALLBACK(handle_menuitem_notify), proxy, 0); + g_signal_connect_object(item, "mnemonic-activate", + G_CALLBACK(handle_menuitem_mnemonic_activate), proxy, 0); + + g_signal_connect_object(proxy, "select", + G_CALLBACK(handle_proxy_select), item, 0); + g_signal_connect_object(proxy, "deselect", + G_CALLBACK(handle_proxy_deselect), item, 0); + g_signal_connect_object(proxy, "activate", + G_CALLBACK(handle_proxy_activate), item, 0); + g_signal_connect_object(proxy, "activate-item", + G_CALLBACK(handle_proxy_activate_item), item, 0); + + return proxy; +} diff --git a/module/menuitem-proxy.h b/module/menuitem-proxy.h new file mode 100644 index 0000000..7a13a80 --- /dev/null +++ b/module/menuitem-proxy.h @@ -0,0 +1,8 @@ +#ifndef _MENUITEM_PROXY_H_ +#define _MENUITEM_PROXY_H_ + +#include + +GtkMenuItem * topmenu_create_proxy_menu_item (GtkMenuItem *item); + +#endif -- cgit v1.2.3