added about dialog
[psensor.git] / src / ui.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17     02110-1301 USA
18 */
19
20 #include "cfg.h"
21 #include "ui.h"
22 #include "ui_graph.h"
23 #include "ui_pref.h"
24 #include "ui_sensorpref.h"
25 #include "ui_sensorlist.h"
26
27 static gboolean
28 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
29 {
30
31 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
32         gtk_widget_hide(((struct ui_psensor *)data)->main_window);
33 #else
34         ui_psensor_quit();
35 #endif
36
37         return TRUE;
38 }
39
40 void ui_show_about_dialog()
41 {
42         gtk_show_about_dialog(NULL,
43                               "comments",
44                               _("Psensor is a GTK+ application for monitoring "
45                                 "hardware sensors"),
46                               "copyright",
47                               _("Copyright(c) 2010-2011\njeanfi@gmail.com"),
48                               "logo-icon-name", "psensor",
49                               "program-name", "Psensor",
50                               "title", _("About Psensor"),
51                               "version", VERSION,
52                               "website", PACKAGE_URL,
53                               "website-label", _("Psensor Homepage"),
54                               NULL);
55 }
56
57 static void cb_about(GtkMenuItem *mi, gpointer data)
58 {
59         ui_show_about_dialog();
60 }
61
62 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
63 {
64         ui_psensor_quit();
65 }
66
67 static void cb_preferences(GtkMenuItem *mi, gpointer data)
68 {
69         ui_pref_dialog_run((struct ui_psensor *)data);
70 }
71
72 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
73 {
74         struct ui_psensor *ui = data;
75
76         if (ui->sensors && *ui->sensors)
77                 ui_sensorpref_dialog_run(*ui->sensors, ui);
78 }
79
80 void ui_psensor_quit()
81 {
82         gtk_main_quit();
83 }
84
85 static const char *menu_desc =
86 "<ui>"
87 "  <menubar name='MainMenu'>"
88 "    <menu name='Psensor' action='PsensorMenuAction'>"
89 "      <menuitem name='Preferences' action='PreferencesAction' />"
90 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
91 "      <separator />"
92 "      <menuitem name='Quit' action='QuitAction' />"
93 "    </menu>"
94 "    <menu name='Help' action='HelpMenuAction'>"
95 "      <menuitem name='About' action='AboutAction' />"
96 "    </menu>"
97 "  </menubar>"
98 "</ui>";
99
100 static GtkActionEntry entries[] = {
101   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
102
103   { "PreferencesAction", GTK_STOCK_PREFERENCES,   /* name, stock id */
104     N_("_Preferences"), NULL,                     /* label, accelerator */
105     N_("Preferences"),                            /* tooltip */
106     G_CALLBACK(cb_preferences) },
107
108   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
109     N_("_Sensor Preferences"), NULL,
110     N_("Sensor Preferences"),
111     G_CALLBACK(cb_sensor_preferences) },
112
113   { "QuitAction",
114     GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"), G_CALLBACK(cb_menu_quit) },
115
116   { "HelpMenuAction", NULL, "_Help" },
117
118   { "AboutAction", GTK_STOCK_PREFERENCES,
119     N_("_About"), NULL,
120     N_("About"),
121     G_CALLBACK(cb_about) }
122 };
123 static guint n_entries = G_N_ELEMENTS(entries);
124
125 static GtkWidget *get_menu(struct ui_psensor *ui)
126 {
127         GtkActionGroup      *action_group;
128         GtkUIManager        *menu_manager;
129         GError              *error;
130
131         action_group = gtk_action_group_new("PsensorActions");
132         gtk_action_group_set_translation_domain(action_group, PACKAGE);
133         menu_manager = gtk_ui_manager_new();
134
135         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
136         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
137
138         error = NULL;
139         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
140
141         if (error)
142                 g_error(_("building menus failed: %s"), error->message);
143
144         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
145 }
146
147 static unsigned int enable_alpha_channel(GtkWidget *w)
148 {
149         GdkScreen *screen = gtk_widget_get_screen(w);
150
151 #if (GTK_CHECK_VERSION(3, 0, 0))
152         GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
153
154         if (visual) {
155                 gtk_widget_set_visual(w, visual);
156                 return 1;
157         }
158 #else
159         GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
160
161         if (colormap) {
162                 gtk_widget_set_colormap(w, colormap);
163                 return 1;
164         }
165 #endif
166         return 0;
167 }
168
169 void ui_window_create(struct ui_psensor *ui)
170 {
171         GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
172         GdkScreen *screen;
173         GdkPixbuf *icon;
174         GtkIconTheme *icon_theme;
175         GtkWidget *menubar;
176
177         gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
178
179         gtk_window_set_title(GTK_WINDOW(window),
180                              _("Psensor - Temperature Monitor"));
181         gtk_window_set_role(GTK_WINDOW(window), "psensor");
182
183         screen = gtk_widget_get_screen(window);
184
185         if (ui->config->alpha_channel_enabled
186             && gdk_screen_is_composited(screen)) {
187                 if (!enable_alpha_channel(window))
188                         ui->config->alpha_channel_enabled = 0;
189         } else {
190                 ui->config->alpha_channel_enabled = 0;
191         }
192
193         icon_theme = gtk_icon_theme_get_default();
194         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
195         if (icon)
196                 gtk_window_set_icon(GTK_WINDOW(window), icon);
197         else
198                 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
199
200         g_signal_connect(window,
201                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
202
203         gtk_window_set_decorated(GTK_WINDOW(window),
204                                  ui->config->window_decoration_enabled);
205
206         gtk_window_set_keep_below(GTK_WINDOW(window),
207                                   ui->config->window_keep_below_enabled);
208
209         /* main box */
210         menubar = get_menu(ui);
211
212         ui->main_box = gtk_vbox_new(FALSE, 1);
213
214         gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
215                            FALSE, TRUE, 0);
216
217         gtk_container_add(GTK_CONTAINER(window), ui->main_box);
218
219         ui->main_window = window;
220         ui->menu_bar = menubar;
221
222 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
223         if (ui->config->hide_on_startup)
224                 gtk_widget_show_all(ui->main_box);
225         else
226                 gtk_widget_show_all(ui->main_window);
227 #else
228         gtk_widget_show_all(ui->main_window);
229 #endif
230
231 }
232
233 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
234 {
235         if (show)
236                 gtk_widget_show(ui->menu_bar);
237         else
238                 gtk_widget_hide(ui->menu_bar);
239 }
240
241 void ui_window_update(struct ui_psensor *ui)
242 {
243         struct config *cfg;
244         int init = 1;
245
246         cfg = ui->config;
247
248         if (ui->sensor_box) {
249                 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
250
251                 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
252                                      ui->ui_sensorlist->widget);
253
254                 gtk_container_remove(GTK_CONTAINER(ui->main_box),
255                                      ui->sensor_box);
256
257                 ui->w_graph = ui_graph_create(ui);
258
259                 init = 0;
260         }
261
262         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
263             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
264                 ui->sensor_box = gtk_hpaned_new();
265         else
266                 ui->sensor_box = gtk_vpaned_new();
267
268         gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
269
270         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
271             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
272                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
273                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
274                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
275                                 ui->ui_sensorlist->widget, FALSE, TRUE);
276         } else {
277                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
278                                 ui->ui_sensorlist->widget, FALSE, TRUE);
279                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
280                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
281         }
282
283         if (!init)
284                 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
285
286         gtk_widget_show_all(ui->sensor_box);
287
288         if (cfg->menu_bar_disabled)
289                 menu_bar_show(0, ui);
290         else
291                 menu_bar_show(1, ui);
292 }