/* * Copyright 2010 Canonical, Ltd. * * 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. * * Authors: * Cody Russell */ #include "config.h" #include #include #include #include #include #include #include #include #include #include "bridge.h" #define UPANEL_MENU_DBUS_OBJECT "/org/ayatana/upanel/menu" static void app_menu_bridge_insert (GtkMenuProxy *proxy, GtkWidget *child, guint position); struct _AppMenuBridgePrivate { GHashTable *items; /* */ DbusmenuServer *server; DbusmenuMenuitem *root; }; G_DEFINE_DYNAMIC_TYPE(AppMenuBridge, app_menu_bridge, GTK_TYPE_MENU_PROXY) static void app_menu_bridge_init (AppMenuBridge *bridge) { bridge->priv = G_TYPE_INSTANCE_GET_PRIVATE (bridge, APP_MENU_TYPE_BRIDGE, AppMenuBridgePrivate); bridge->priv->items = g_hash_table_new (g_direct_hash, g_direct_equal); bridge->priv->server = dbusmenu_server_new (UPANEL_MENU_DBUS_OBJECT); bridge->priv->root = dbusmenu_menuitem_new (); dbusmenu_server_set_root (bridge->priv->server, bridge->priv->root); } static void app_menu_bridge_finalize (GObject *object) { g_hash_table_destroy (APP_MENU_BRIDGE (object)->priv->items); } static void app_menu_bridge_class_init (AppMenuBridgeClass *class) { GtkMenuProxyClass *proxy_class; GObjectClass *object_class; app_menu_bridge_parent_class = g_type_class_peek_parent (class); proxy_class = GTK_MENU_PROXY_CLASS (class); object_class = G_OBJECT_CLASS (class); proxy_class->insert = app_menu_bridge_insert; object_class->finalize = app_menu_bridge_finalize; g_type_class_add_private (class, sizeof (AppMenuBridgePrivate)); } static void app_menu_bridge_class_finalize (AppMenuBridgeClass *class) { } static GtkWidget * find_parent (GtkWidget *widget) { if (widget->parent) { if (GTK_IS_MENU_ITEM (widget->parent) || GTK_IS_MENU_SHELL (widget->parent)) return widget->parent; else return find_parent (widget->parent); } else { return NULL; } } static GtkWidget * find_menu_label (GtkWidget *widget) { GtkWidget *label = NULL; if (GTK_IS_LABEL (widget)) return widget; if (GTK_IS_CONTAINER (widget)) { GList *children; GList *l; children = gtk_container_get_children (GTK_CONTAINER (widget)); for (l = children; l; l = l->next) { label = find_menu_label (l->data); if (label) break; } g_list_free (children); } return label; } static const gchar * get_menu_label_text (GtkWidget *menuitem) { GtkWidget *label = find_menu_label (menuitem); if (label) return gtk_label_get_text (GTK_LABEL (label)); return NULL; } static void item_activated (DbusmenuMenuitem *item, gpointer user_data) { GtkWidget *child; if (user_data != NULL) { child = (GtkWidget *)user_data; if (GTK_IS_MENU_ITEM (child)) { gtk_menu_item_activate (GTK_MENU_ITEM (child)); } } } static void app_menu_bridge_insert (GtkMenuProxy *proxy, GtkWidget *child, guint position) { AppMenuBridge *bridge; DbusmenuMenuitem *item; bridge = APP_MENU_BRIDGE (proxy); if (GTK_IS_TEAROFF_MENU_ITEM (child) || GTK_IS_MENU (child)) return; if (g_hash_table_lookup (bridge->priv->items, child)) { return; } item = dbusmenu_menuitem_new (); if (GTK_IS_MENU_ITEM (child)) { GtkWidget *parent = find_parent (child); DbusmenuMenuitem *parent_item; GtkWidget *submenu; if (GTK_IS_SEPARATOR_MENU_ITEM (child)) { dbusmenu_menuitem_property_set (item, "type", "separator"); } else { g_print (" ** insert menuitem '%s'\n", get_menu_label_text (child)); dbusmenu_menuitem_property_set (item, "label", get_menu_label_text (child)); g_signal_connect (G_OBJECT (item), "item_activated", G_CALLBACK (item_activated), child); } submenu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (child)); if (submenu != NULL && !g_hash_table_lookup (bridge->priv->items, submenu)) { g_hash_table_insert (bridge->priv->items, submenu, item); } if (parent != NULL) { parent_item = g_hash_table_lookup (bridge->priv->items, parent); if (!parent_item) { if (GTK_IS_MENU_BAR (parent)) { parent_item = bridge->priv->root; } } if (parent_item) { // XXX - this probably doesn't strictly enforce the order.. dbusmenu_menuitem_child_append (parent_item, item); } } } g_hash_table_insert (bridge->priv->items, child, item); } G_MODULE_EXPORT void menu_proxy_module_load (GtkMenuProxyModule *module) { static gboolean registered = FALSE; if (!registered) { guint nameret = 0; GError *error = NULL; DBusGProxy *bus_proxy; DBusGConnection *connection; connection = dbus_g_bus_get (DBUS_BUS_SESSION, NULL); bus_proxy = dbus_g_proxy_new_for_name (connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); if (!org_freedesktop_DBus_request_name (bus_proxy, "ayatana.upanel.menu", 0, &nameret, &error)) { g_error ("Unable to call to request name"); return; } app_menu_bridge_register_type (G_TYPE_MODULE (module)); registered = TRUE; } } G_MODULE_EXPORT void menu_proxy_module_unload (GtkMenuProxyModule *module) { }