Import dhcpcd-gtk
[dhcpcd-ui] / menu.c
1 /*
2  * dhcpcd-gtk
3  * Copyright 2009 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <gtk/gtk.h>
28
29 #ifdef HAVE_GNOME
30 # include <gnome.h>
31 #endif
32
33 #include "config.h"
34 #include "menu.h"
35
36 static const char *copyright = "Copyright (c) 2009 Roy Marples";
37
38 static const char *authors[] = {
39         "Roy Marples <roy@marples.name>",
40         NULL
41 };
42 static const char *license =
43         "Licensed under the 2 clause BSD license.\n"
44         "\n"
45         "Redistribution and use in source and binary forms, with or without\n"
46         "modification, are permitted provided that the following conditions\n"
47         "are met:\n"
48         "1. Redistributions of source code must retain the above copyright\n"
49         "   notice, this list of conditions and the following disclaimer.\n"
50         "2. Redistributions in binary form must reproduce the above copyright\n"
51         "   notice, this list of conditions and the following disclaimer in the\n"
52         "   documentation and/or other materials provided with the distribution.\n"
53         "\n"
54         "THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND\n"
55         "ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n"
56         "IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\n"
57         "ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE\n"
58         "FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\n"
59         "DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\n"
60         "OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"
61         "HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT\n"
62         "LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY\n"
63         "OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF\n"
64         "SUCH DAMAGE.";
65
66 /* Should be in a header */
67 void notify_close(void);
68
69 static void
70 on_quit(_unused GtkMenuItem *item, _unused gpointer data)
71 {
72         gtk_main_quit();
73 }
74
75 static void
76 on_help(_unused GtkMenuItem *item, _unused gpointer data)
77 {
78 }
79
80 #ifdef HAVE_GNOME
81 static void
82 url_show(GtkAboutDialog *dialog, const char *url)
83 {
84         GdkScreen *screen;
85
86         screen = gtk_widget_get_screen(GTK_WIDGET(dialog));
87         gnome_url_show_on_screen(url, screen, NULL);
88 }
89
90 static void
91 email_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer p)
92 {
93         char *address;
94
95         address = g_strdup_printf("mailto:%s", url);
96         url_show(dialog, address);
97         g_free(address);
98 }
99
100 static void
101 url_hook(GtkAboutDialog *dialog, const char *url, _unused gpointer p)
102 {
103         url_show(dialog, url);
104 }
105 #endif
106
107 static void
108 on_about(_unused GtkMenuItem *item, _unused gpointer data)
109 {
110         gtk_window_set_default_icon_name(GTK_STOCK_NETWORK);
111 #ifdef HAVE_GNOME
112         gtk_about_dialog_set_email_hook(email_hook, NULL, NULL);
113         gtk_about_dialog_set_url_hook(url_hook, NULL, NULL);
114 #elif HAVE_XFCE
115         gtk_about_dialog_set_email_hook(exo_url_about_dialog_hook, NULL, NULL);
116         gtk_about_dialog_set_url_hook(exo_url_about_dialog_hook, NULL, NULL);
117 #endif
118         gtk_show_about_dialog(NULL,
119                               "version", VERSION,
120                               "copyright", copyright,
121                               "license", license,
122                               "website-label", "dhcpcd GTK Website",
123                               "website", "http://roy.marples.name/projects/dhcpcd",
124                               "authors", authors,
125                               "wrap-license", TRUE,
126                               "logo-icon-name", GTK_STOCK_NETWORK,
127                               NULL);
128 }
129
130 static void
131 on_activate(_unused GtkStatusIcon *icon, _unused guint button, _unused guint32 atime, _unused gpointer data)
132 {
133         notify_close();
134 }
135
136 static void
137 on_popup(GtkStatusIcon *icon, guint button, guint32 atime, gpointer data)
138 {
139         GtkMenu *menu;
140         GtkWidget *item, *image;
141
142         notify_close();
143
144         menu = (GtkMenu *)gtk_menu_new();
145
146         item = gtk_image_menu_item_new_with_mnemonic("_Quit");
147         image = gtk_image_new_from_icon_name(GTK_STOCK_QUIT,
148                                              GTK_ICON_SIZE_MENU);
149         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
150         g_signal_connect(G_OBJECT(item), "activate",
151                          G_CALLBACK(on_quit), icon);
152         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
153
154         item = gtk_separator_menu_item_new();
155         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
156
157         item = gtk_image_menu_item_new_with_mnemonic("_Help");
158         image = gtk_image_new_from_icon_name(GTK_STOCK_HELP,
159                                              GTK_ICON_SIZE_MENU);
160         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
161         g_signal_connect(G_OBJECT(item), "activate",
162                          G_CALLBACK(on_help), icon);
163         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
164
165         item = gtk_image_menu_item_new_with_mnemonic("_About");
166         image = gtk_image_new_from_icon_name(GTK_STOCK_ABOUT,
167                                              GTK_ICON_SIZE_MENU);
168         gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
169         g_signal_connect(G_OBJECT(item), "activate",
170                          G_CALLBACK(on_about), icon);
171         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
172
173         gtk_widget_show_all(GTK_WIDGET(menu));
174         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
175                         gtk_status_icon_position_menu, data, button, atime);
176         if (button == 0)
177                 gtk_menu_shell_select_first(GTK_MENU_SHELL(menu), FALSE);
178 }
179
180 void
181 menu_init(GtkStatusIcon *icon)
182 {
183         g_signal_connect_object(G_OBJECT(icon), "activate",
184                                 G_CALLBACK(on_activate), icon, 0);
185         g_signal_connect_object(G_OBJECT(icon), "popup_menu",
186                                 G_CALLBACK(on_popup), icon, 0);
187 }