diff options
Diffstat (limited to 'src/lib/trayicon.c')
-rw-r--r-- | src/lib/trayicon.c | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/lib/trayicon.c b/src/lib/trayicon.c new file mode 100644 index 00000000..200c9068 --- /dev/null +++ b/src/lib/trayicon.c | |||
@@ -0,0 +1,132 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2010, 2011 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/lib/trayicon.c | ||
23 | * @brief trayicon support | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "gnunet_gtk_config.h" | ||
27 | #include "gnunet_gtk.h" | ||
28 | |||
29 | |||
30 | /** | ||
31 | * Our tray icon. | ||
32 | */ | ||
33 | static GtkStatusIcon *tray_icon; | ||
34 | |||
35 | /** | ||
36 | * The main window. | ||
37 | */ | ||
38 | static GtkWindow *main_window; | ||
39 | |||
40 | |||
41 | /** | ||
42 | * We got a click on our tray icon. Toggle visibility of the main | ||
43 | * window. | ||
44 | */ | ||
45 | static void | ||
46 | tray_icon_on_click(GtkStatusIcon *status_icon, | ||
47 | gpointer user_data) | ||
48 | { | ||
49 | if (gtk_window_is_active (main_window)) | ||
50 | gtk_widget_hide (GTK_WIDGET (main_window)); | ||
51 | else | ||
52 | gtk_window_present (main_window); | ||
53 | } | ||
54 | |||
55 | |||
56 | /** | ||
57 | * We got a right-click on the tray icon. Display the context | ||
58 | * menu (which should have a 'quit' button). | ||
59 | */ | ||
60 | static int | ||
61 | tray_icon_on_menu(GtkWidget *widget, | ||
62 | GdkEvent *event, | ||
63 | gpointer user_data) | ||
64 | { | ||
65 | GtkMenu *tray_menu; | ||
66 | GdkEventButton *event_button; | ||
67 | GtkBuilder *builder; | ||
68 | |||
69 | if (event->type == GDK_BUTTON_PRESS) | ||
70 | { | ||
71 | event_button = (GdkEventButton *) event; | ||
72 | if (event_button->button == 3) | ||
73 | { | ||
74 | builder = GNUNET_GTK_get_new_builder ("gnunet_gtk_status_bar_menu.glade"); | ||
75 | tray_menu = GTK_MENU (gtk_builder_get_object (builder, | ||
76 | "GNUNET_GTK_status_bar_popup_menu")); | ||
77 | gtk_menu_popup (tray_menu, NULL, NULL, NULL, NULL, | ||
78 | event_button->button, event_button->time); | ||
79 | g_object_unref (builder); | ||
80 | } | ||
81 | } | ||
82 | return FALSE; | ||
83 | } | ||
84 | |||
85 | |||
86 | /** | ||
87 | * Create our tray icon. | ||
88 | * | ||
89 | * @param main handle to the main window (show or hide) | ||
90 | * @param icon_name name of the tray icon file | ||
91 | * @param tooltip tooltip for the tray icon | ||
92 | */ | ||
93 | void | ||
94 | GNUNET_GTK_tray_icon_create (GtkWindow *main, | ||
95 | const char *icon_name, | ||
96 | const char *tooltip) | ||
97 | { | ||
98 | if (NULL != tray_icon) | ||
99 | { | ||
100 | GNUNET_break (0); | ||
101 | return; | ||
102 | } | ||
103 | main_window = main; | ||
104 | tray_icon = gtk_status_icon_new(); | ||
105 | g_signal_connect(G_OBJECT(tray_icon), "activate", | ||
106 | G_CALLBACK(tray_icon_on_click), NULL); | ||
107 | g_signal_connect (G_OBJECT(tray_icon), | ||
108 | "button_press_event", | ||
109 | G_CALLBACK(tray_icon_on_menu), | ||
110 | tray_icon); | ||
111 | gtk_status_icon_set_from_icon_name (tray_icon, | ||
112 | icon_name); | ||
113 | gtk_status_icon_set_tooltip(tray_icon, | ||
114 | tooltip); | ||
115 | gtk_status_icon_set_visible(tray_icon, TRUE); | ||
116 | } | ||
117 | |||
118 | |||
119 | /** | ||
120 | * Destroy the tray icon. | ||
121 | */ | ||
122 | void | ||
123 | GNUNET_GTK_tray_icon_destroy () | ||
124 | { | ||
125 | if (tray_icon == NULL) | ||
126 | return; | ||
127 | g_object_unref (G_OBJECT (tray_icon)); | ||
128 | tray_icon = NULL; | ||
129 | main_window = NULL; | ||
130 | } | ||
131 | |||
132 | /* end of trayicon.c */ | ||