Back to index

libindicator  12.10.0
indicator-loader.c
Go to the documentation of this file.
00001 /*
00002 A small test loader for loading indicators in test suites
00003 and during development of them.
00004 
00005 Copyright 2009 Canonical Ltd.
00006 
00007 Authors:
00008     Ted Gould <ted@canonical.com>
00009 
00010 This library is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 version 3.0 as published by the Free Software Foundation.
00013 
00014 This library is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License version 3.0 for more details.
00018 
00019 You should have received a copy of the GNU General Public
00020 License along with this library. If not, see
00021 <http://www.gnu.org/licenses/>.
00022 */
00023 
00024 
00025 #include <gtk/gtk.h>
00026 #include <libindicator/indicator-object.h>
00027 
00028 #define ENTRY_DATA_NAME "indicator-custom-entry-data"
00029 
00030 static void
00031 activate_entry (GtkWidget * widget, gpointer user_data)
00032 {
00033        g_return_if_fail(INDICATOR_IS_OBJECT(user_data));
00034        gpointer entry = g_object_get_data(G_OBJECT(widget), ENTRY_DATA_NAME);
00035        if (entry == NULL) {
00036               g_debug("Activation on: (null)");
00037        }
00038 
00039        indicator_object_entry_activate(INDICATOR_OBJECT(user_data), (IndicatorObjectEntry *)entry, gtk_get_current_event_time());
00040        return;
00041 }
00042 
00043 static void
00044 entry_added (IndicatorObject * io, IndicatorObjectEntry * entry, gpointer user_data)
00045 {
00046        g_debug("Signal: Entry Added");
00047 
00048        if (entry->parent_object == NULL) {
00049               g_warning("Entry '%p' does not have a parent object", entry);
00050        }
00051 
00052        GtkWidget * menuitem = gtk_menu_item_new();
00053 #if GTK_CHECK_VERSION(3,0,0)
00054        GtkWidget * hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 3);
00055 #else
00056        GtkWidget * hbox = gtk_hbox_new(FALSE, 3);
00057 #endif
00058 
00059        if (entry->image != NULL) {
00060               gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(entry->image), FALSE, FALSE, 0);
00061        }
00062        if (entry->label != NULL) {
00063               gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(entry->label), FALSE, FALSE, 0);
00064        }
00065        gtk_container_add(GTK_CONTAINER(menuitem), hbox);
00066        gtk_widget_show(hbox);
00067 
00068        if (entry->menu != NULL) {
00069               gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), GTK_WIDGET(entry->menu));
00070        }
00071 
00072        g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(activate_entry), io);
00073 
00074        gtk_menu_shell_append(GTK_MENU_SHELL(user_data), menuitem);
00075        gtk_widget_show(menuitem);
00076 
00077        g_object_set_data(G_OBJECT(menuitem), ENTRY_DATA_NAME, entry);
00078 
00079        return;
00080 }
00081 
00082 static void
00083 entry_removed_cb (GtkWidget * widget, gpointer userdata)
00084 {
00085        gpointer data = g_object_get_data(G_OBJECT(widget), ENTRY_DATA_NAME);
00086 
00087        if (data != userdata) {
00088               return;
00089        }
00090 
00091        gtk_widget_destroy(widget);
00092        return;
00093 }
00094 
00095 static void 
00096 entry_removed (IndicatorObject * io, IndicatorObjectEntry * entry, gpointer user_data)
00097 {
00098        g_debug("Signal: Entry Removed");
00099 
00100        gtk_container_foreach(GTK_CONTAINER(user_data), entry_removed_cb, entry);
00101 
00102        return;
00103 }
00104 
00105 static void
00106 menu_show (IndicatorObject * io, IndicatorObjectEntry * entry, guint timestamp, gpointer user_data)
00107 {
00108        if (entry != NULL) {
00109               g_debug("Show Menu: %s", entry->label != NULL ? gtk_label_get_text(entry->label) : "No Label");
00110        } else {
00111               g_debug("Show Menu: (null)");
00112        }
00113        return;
00114 }
00115 
00116 static gboolean
00117 load_module (const gchar * name, GtkWidget * menu)
00118 {
00119        g_debug("Looking at Module: %s", name);
00120        g_return_val_if_fail(name != NULL, FALSE);
00121 
00122        if (!g_str_has_suffix(name, G_MODULE_SUFFIX)) {
00123               return FALSE;
00124        }
00125 
00126        g_debug("Loading Module: %s", name);
00127 
00128        /* Build the object for the module */
00129        IndicatorObject * io = indicator_object_new_from_file(name);
00130 
00131        /* Connect to it's signals */
00132        g_signal_connect(G_OBJECT(io), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED,   G_CALLBACK(entry_added),    menu);
00133        g_signal_connect(G_OBJECT(io), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED, G_CALLBACK(entry_removed),  menu);
00134        g_signal_connect(G_OBJECT(io), INDICATOR_OBJECT_SIGNAL_MENU_SHOW,     G_CALLBACK(menu_show),      NULL);
00135 
00136        /* Work on the entries */
00137        GList * entries = indicator_object_get_entries(io);
00138        GList * entry = NULL;
00139 
00140        for (entry = entries; entry != NULL; entry = g_list_next(entry)) {
00141               IndicatorObjectEntry * entrydata = (IndicatorObjectEntry *)entry->data;
00142               entry_added(io, entrydata, menu);
00143        }
00144 
00145        g_list_free(entries);
00146 
00147        return TRUE;
00148 }
00149 
00150 static void
00151 destroy (gpointer data)
00152 {
00153        gtk_main_quit();
00154        return;
00155 }
00156 
00157 int
00158 main (int argc, char ** argv)
00159 {
00160        /* Make sure we don't proxy to ourselves */
00161        g_unsetenv("UBUNTU_MENUPROXY");
00162 
00163        gtk_init(&argc, &argv);
00164 
00165        if (argc != 2) {
00166               g_error("Need filename");
00167               return 1;
00168        }
00169 
00170        GtkWidget * menubar = gtk_menu_bar_new();
00171        if (!load_module(argv[1], menubar)) {
00172               g_error("Unable to load module");
00173               return 1;
00174        }
00175 
00176        GtkWidget * window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00177        g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
00178 
00179        gtk_container_add(GTK_CONTAINER(window), menubar);
00180 
00181        gtk_widget_show(menubar);
00182        gtk_widget_show(window);
00183 
00184        gtk_main();
00185 
00186        return 0;
00187 }