Back to index

libappindicator  12.10.0
simple-client.c
Go to the documentation of this file.
00001 /*
00002 A small piece of sample code demonstrating a very simple application
00003 with an indicator.
00004 
00005 Copyright 2009 Canonical Ltd.
00006 
00007 Authors:
00008     Ted Gould <ted@canonical.com>
00009 
00010 This program is free software: you can redistribute it and/or modify it 
00011 under the terms of the GNU General Public License version 3, as published 
00012 by the Free Software Foundation.
00013 
00014 This program is distributed in the hope that it will be useful, but 
00015 WITHOUT ANY WARRANTY; without even the implied warranties of 
00016 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
00017 PURPOSE.  See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License along 
00020 with this program.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #include "app-indicator.h"
00024 #include "libdbusmenu-glib/server.h"
00025 #include "libdbusmenu-glib/menuitem.h"
00026 
00027 GMainLoop * mainloop = NULL;
00028 static gboolean active = TRUE;
00029 static gboolean can_haz_label = TRUE;
00030 
00031 static void
00032 label_toggle_cb (GtkWidget * widget, gpointer data)
00033 {
00034     can_haz_label = !can_haz_label;
00035 
00036     if (can_haz_label) {
00037         gtk_menu_item_set_label(GTK_MENU_ITEM(widget), "Hide label");
00038     } else {
00039         gtk_menu_item_set_label(GTK_MENU_ITEM(widget), "Show label");
00040     }
00041 
00042     return;
00043 }
00044 
00045 static void
00046 activate_clicked_cb (GtkWidget *widget, gpointer data)
00047 {
00048     AppIndicator * ci = APP_INDICATOR(data);
00049 
00050     if (active) {
00051         app_indicator_set_status (ci, APP_INDICATOR_STATUS_ATTENTION);
00052         gtk_menu_item_set_label(GTK_MENU_ITEM(widget), "I'm okay now");
00053         active = FALSE;
00054     } else {
00055         app_indicator_set_status (ci, APP_INDICATOR_STATUS_ACTIVE);
00056         gtk_menu_item_set_label(GTK_MENU_ITEM(widget), "Get Attention");
00057         active = TRUE;
00058     }
00059 
00060 }
00061 
00062 static void
00063 local_icon_toggle_cb (GtkWidget *widget, gpointer data)
00064 {
00065     AppIndicator * ci = APP_INDICATOR(data);
00066 
00067     if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
00068         app_indicator_set_icon_full(ci, LOCAL_ICON, "Local Icon");
00069     } else {
00070         app_indicator_set_icon_full(ci, "indicator-messages", "System Icon");
00071     }
00072 
00073     return;
00074 }
00075 
00076 static void
00077 item_clicked_cb (GtkWidget *widget, gpointer data)
00078 {
00079   const gchar *text = (const gchar *)data;
00080 
00081   g_print ("%s clicked!\n", text);
00082 }
00083 
00084 static void
00085 toggle_sensitivity_cb (GtkWidget *widget, gpointer data)
00086 {
00087   GtkWidget *target = (GtkWidget *)data;
00088 
00089   gtk_widget_set_sensitive (target, !gtk_widget_is_sensitive (target));
00090 }
00091 
00092 static void
00093 image_clicked_cb (GtkWidget *widget, gpointer data)
00094 {
00095   gtk_image_set_from_stock (GTK_IMAGE (gtk_image_menu_item_get_image (
00096                             GTK_IMAGE_MENU_ITEM (widget))),
00097                             GTK_STOCK_OPEN, GTK_ICON_SIZE_MENU);
00098 }
00099 
00100 static void
00101 scroll_event_cb (AppIndicator * ci, gint delta, guint direction, gpointer data)
00102 {
00103     g_print("Got scroll event! delta: %d, direction: %d\n", delta, direction);
00104 }
00105 
00106 static void
00107 append_submenu (GtkWidget *item)
00108 {
00109   GtkWidget *menu;
00110   GtkWidget *mi;
00111   GtkWidget *prev_mi;
00112 
00113   menu = gtk_menu_new ();
00114 
00115   mi = gtk_menu_item_new_with_label ("Sub 1");
00116   gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
00117   g_signal_connect (mi, "activate",
00118                     G_CALLBACK (item_clicked_cb), "Sub 1");
00119 
00120   prev_mi = mi;
00121   mi = gtk_menu_item_new_with_label ("Sub 2");
00122   gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
00123   g_signal_connect (mi, "activate",
00124                     G_CALLBACK (toggle_sensitivity_cb), prev_mi);
00125 
00126   mi = gtk_menu_item_new_with_label ("Sub 3");
00127   gtk_menu_shell_append (GTK_MENU_SHELL (menu), mi);
00128   g_signal_connect (mi, "activate",
00129                     G_CALLBACK (item_clicked_cb), "Sub 3");
00130 
00131   gtk_widget_show_all (menu);
00132 
00133   gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), menu);
00134 }
00135 
00136 guint percentage = 0;
00137 
00138 static gboolean
00139 percent_change (gpointer user_data)
00140 {
00141     percentage = (percentage + 1) % 100;
00142     if (can_haz_label) {
00143         gchar * percentstr = g_strdup_printf("%d%%", percentage + 1);
00144         app_indicator_set_label (APP_INDICATOR(user_data), percentstr, "100%");
00145         g_free(percentstr);
00146     } else {
00147         app_indicator_set_label (APP_INDICATOR(user_data), NULL, NULL);
00148     }
00149     return TRUE;
00150 }
00151 
00152 int
00153 main (int argc, char ** argv)
00154 {
00155         GtkWidget *menu = NULL;
00156         AppIndicator *ci = NULL;
00157 
00158         gtk_init (&argc, &argv);
00159 
00160         ci = app_indicator_new ("example-simple-client",
00161                                 "indicator-messages",
00162                                 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00163 
00164     g_assert (IS_APP_INDICATOR (ci));
00165         g_assert (G_IS_OBJECT (ci));
00166 
00167     app_indicator_set_status (ci, APP_INDICATOR_STATUS_ACTIVE);
00168     app_indicator_set_attention_icon_full(ci, "indicator-messages-new", "System Messages Icon Highlighted");
00169     app_indicator_set_label (ci, "1%", "100%");
00170        app_indicator_set_title (ci, "Test Inidcator");
00171 
00172     g_signal_connect (ci, "scroll-event",
00173                       G_CALLBACK (scroll_event_cb), NULL);
00174 
00175     g_timeout_add_seconds(1, percent_change, ci);
00176 
00177         menu = gtk_menu_new ();
00178         GtkWidget *item = gtk_check_menu_item_new_with_label ("1");
00179         g_signal_connect (item, "activate",
00180                           G_CALLBACK (item_clicked_cb), "1");
00181         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
00182         gtk_widget_show (item);
00183 
00184         item = gtk_radio_menu_item_new_with_label (NULL, "2");
00185         g_signal_connect (item, "activate",
00186                           G_CALLBACK (item_clicked_cb), "2");
00187         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
00188         gtk_widget_show (item);
00189 
00190         item = gtk_menu_item_new_with_label ("3");
00191         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
00192         append_submenu (item);
00193         gtk_widget_show (item);
00194 
00195         GtkWidget *toggle_item = gtk_menu_item_new_with_label ("Toggle 3");
00196         g_signal_connect (toggle_item, "activate",
00197                           G_CALLBACK (toggle_sensitivity_cb), item);
00198         gtk_menu_shell_append (GTK_MENU_SHELL (menu), toggle_item);
00199         gtk_widget_show(toggle_item);
00200 
00201         item = gtk_image_menu_item_new_from_stock (GTK_STOCK_NEW, NULL);
00202         g_signal_connect (item, "activate",
00203                           G_CALLBACK (image_clicked_cb), NULL);
00204         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
00205         gtk_widget_show(item);
00206 
00207         item = gtk_menu_item_new_with_label ("Get Attention");
00208         g_signal_connect (item, "activate",
00209                           G_CALLBACK (activate_clicked_cb), ci);
00210         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
00211         gtk_widget_show(item);
00212         app_indicator_set_secondary_activate_target(ci, item);
00213 
00214         item = gtk_menu_item_new_with_label ("Show label");
00215         label_toggle_cb(item, ci);
00216         g_signal_connect (item, "activate",
00217                           G_CALLBACK (label_toggle_cb), ci);
00218         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
00219         gtk_widget_show(item);
00220 
00221         item = gtk_check_menu_item_new_with_label ("Set Local Icon");
00222         g_signal_connect (item, "activate",
00223                           G_CALLBACK (local_icon_toggle_cb), ci);
00224         gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
00225         gtk_widget_show(item);
00226 
00227         app_indicator_set_menu (ci, GTK_MENU (menu));
00228 
00229     mainloop = g_main_loop_new(NULL, FALSE);
00230     g_main_loop_run(mainloop);
00231 
00232     return 0;
00233 }