status icon support
[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 #include "ui_status.h"
27 #include "ui_appindicator.h"
28
29 static void save_window_pos(struct ui_psensor *ui)
30 {
31         gboolean visible;
32         GtkWindow *win;
33         struct config *cfg;
34
35         visible = gtk_widget_get_visible(ui->main_window);
36         log_printf(LOG_DEBUG, "Window visible: %d", visible);
37
38         if (visible == TRUE) {
39                 cfg = ui->config;
40
41                 win = GTK_WINDOW(ui->main_window);
42
43                 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
44                 log_printf(LOG_DEBUG,
45                            "Window position: %d %d",
46                            cfg->window_x,
47                            cfg->window_y);
48
49                 gtk_window_get_size(win,
50                                     &cfg->window_w,
51                                     &cfg->window_h);
52                 log_printf(LOG_DEBUG,
53                            "Window size: %d %d",
54                            cfg->window_w,
55                            cfg->window_h);
56
57                 cfg->window_divider_pos
58                         = gtk_paned_get_position(GTK_PANED(ui->sensor_box));
59
60                 config_save(cfg);
61         }
62 }
63
64 static gboolean
65 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
66 {
67         struct ui_psensor *ui = data;
68
69         save_window_pos(ui);
70
71         log_printf(LOG_DEBUG,
72                    "is_status_supported: %d\n", is_status_supported());
73
74         if (is_appindicator_supported() || is_status_supported())
75                 gtk_widget_hide(ui->main_window);
76         else
77                 ui_psensor_quit(ui);
78
79         return TRUE;
80 }
81
82 void ui_show_about_dialog()
83 {
84         gtk_show_about_dialog(NULL,
85                               "comments",
86                               _("Psensor is a GTK+ application for monitoring "
87                                 "hardware sensors"),
88                               "copyright",
89                               _("Copyright(c) 2010-2011\njeanfi@gmail.com"),
90                               "logo-icon-name", "psensor",
91                               "program-name", "Psensor",
92                               "title", _("About Psensor"),
93                               "version", VERSION,
94                               "website", PACKAGE_URL,
95                               "website-label", _("Psensor Homepage"),
96                               NULL);
97 }
98
99 static void cb_about(GtkMenuItem *mi, gpointer data)
100 {
101         ui_show_about_dialog();
102 }
103
104 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
105 {
106         ui_psensor_quit((struct ui_psensor *)data);
107 }
108
109 static void cb_preferences(GtkMenuItem *mi, gpointer data)
110 {
111         ui_pref_dialog_run((struct ui_psensor *)data);
112 }
113
114 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
115 {
116         struct ui_psensor *ui = data;
117
118         if (ui->sensors && *ui->sensors)
119                 ui_sensorpref_dialog_run(*ui->sensors, ui);
120 }
121
122 void ui_psensor_quit(struct ui_psensor *ui)
123 {
124         save_window_pos(ui);
125
126         log_printf(LOG_DEBUG, "Destroy main window");
127         gtk_widget_destroy(ui->main_window);
128         gtk_main_quit();
129 }
130
131 static const char *menu_desc =
132 "<ui>"
133 "  <menubar name='MainMenu'>"
134 "    <menu name='Psensor' action='PsensorMenuAction'>"
135 "      <menuitem name='Preferences' action='PreferencesAction' />"
136 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
137 "      <separator />"
138 "      <menuitem name='Quit' action='QuitAction' />"
139 "    </menu>"
140 "    <menu name='Help' action='HelpMenuAction'>"
141 "      <menuitem name='About' action='AboutAction' />"
142 "    </menu>"
143 "  </menubar>"
144 "</ui>";
145
146 static GtkActionEntry entries[] = {
147   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
148
149   { "PreferencesAction", GTK_STOCK_PREFERENCES,   /* name, stock id */
150     N_("_Preferences"), NULL,                     /* label, accelerator */
151     N_("Preferences"),                            /* tooltip */
152     G_CALLBACK(cb_preferences) },
153
154   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
155     N_("_Sensor Preferences"), NULL,
156     N_("Sensor Preferences"),
157     G_CALLBACK(cb_sensor_preferences) },
158
159   { "QuitAction",
160     GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"), G_CALLBACK(cb_menu_quit) },
161
162   { "HelpMenuAction", NULL, "_Help" },
163
164   { "AboutAction", GTK_STOCK_PREFERENCES,
165     N_("_About"), NULL,
166     N_("About"),
167     G_CALLBACK(cb_about) }
168 };
169 static guint n_entries = G_N_ELEMENTS(entries);
170
171 static GtkWidget *get_menu(struct ui_psensor *ui)
172 {
173         GtkActionGroup      *action_group;
174         GtkUIManager        *menu_manager;
175         GError              *error;
176
177         action_group = gtk_action_group_new("PsensorActions");
178         gtk_action_group_set_translation_domain(action_group, PACKAGE);
179         menu_manager = gtk_ui_manager_new();
180
181         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
182         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
183
184         error = NULL;
185         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
186
187         if (error)
188                 g_error(_("building menus failed: %s"), error->message);
189
190         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
191 }
192
193 static unsigned int enable_alpha_channel(GtkWidget *w)
194 {
195         GdkScreen *screen = gtk_widget_get_screen(w);
196
197 #if (GTK_CHECK_VERSION(3, 0, 0))
198         GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
199
200         if (visual) {
201                 gtk_widget_set_visual(w, visual);
202                 return 1;
203         }
204 #else
205         GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
206
207         if (colormap) {
208                 gtk_widget_set_colormap(w, colormap);
209                 return 1;
210         }
211 #endif
212         return 0;
213 }
214
215 void ui_window_create(struct ui_psensor *ui)
216 {
217         GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
218         GdkScreen *screen;
219         GdkPixbuf *icon;
220         GtkIconTheme *icon_theme;
221         GtkWidget *menubar;
222         struct config *cfg;
223
224         cfg = ui->config;
225         if (cfg->window_restore_enabled)
226                 gtk_window_move(GTK_WINDOW(window),
227                                 cfg->window_x,
228                                 cfg->window_y);
229
230         gtk_window_set_default_size(GTK_WINDOW(window),
231                                     cfg->window_w,
232                                     cfg->window_h);
233
234
235         gtk_window_set_title(GTK_WINDOW(window),
236                              _("Psensor - Temperature Monitor"));
237         gtk_window_set_role(GTK_WINDOW(window), "psensor");
238
239         screen = gtk_widget_get_screen(window);
240
241         if (ui->config->alpha_channel_enabled
242             && gdk_screen_is_composited(screen)) {
243                 if (!enable_alpha_channel(window))
244                         ui->config->alpha_channel_enabled = 0;
245         } else {
246                 ui->config->alpha_channel_enabled = 0;
247         }
248
249         icon_theme = gtk_icon_theme_get_default();
250         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
251         if (icon)
252                 gtk_window_set_icon(GTK_WINDOW(window), icon);
253         else
254                 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
255
256         g_signal_connect(window,
257                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
258
259         gtk_window_set_decorated(GTK_WINDOW(window),
260                                  ui->config->window_decoration_enabled);
261
262         gtk_window_set_keep_below(GTK_WINDOW(window),
263                                   ui->config->window_keep_below_enabled);
264
265         /* main box */
266         menubar = get_menu(ui);
267
268         ui->main_box = gtk_vbox_new(FALSE, 1);
269
270         gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
271                            FALSE, TRUE, 0);
272
273         gtk_container_add(GTK_CONTAINER(window), ui->main_box);
274
275         ui->main_window = window;
276         ui->menu_bar = menubar;
277
278         log_printf(LOG_DEBUG,
279                    "is_status_supported: %d\n", is_status_supported());
280
281         if (ui->config->hide_on_startup
282             && (is_appindicator_supported() || is_status_supported()))
283                 gtk_widget_show_all(ui->main_box);
284         else
285                 ui_window_show(ui);
286 }
287
288 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
289 {
290         if (show)
291                 gtk_widget_show(ui->menu_bar);
292         else
293                 gtk_widget_hide(ui->menu_bar);
294 }
295
296 void ui_window_update(struct ui_psensor *ui)
297 {
298         struct config *cfg;
299         int init = 1;
300
301         cfg = ui->config;
302
303         if (ui->sensor_box) {
304                 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
305
306                 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
307                                      ui->ui_sensorlist->widget);
308
309                 gtk_container_remove(GTK_CONTAINER(ui->main_box),
310                                      ui->sensor_box);
311
312                 ui->w_graph = ui_graph_create(ui);
313
314                 init = 0;
315         }
316
317         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
318             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
319                 ui->sensor_box = gtk_hpaned_new();
320         else
321                 ui->sensor_box = gtk_vpaned_new();
322
323         gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
324
325         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
326             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
327                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
328                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
329                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
330                                 ui->ui_sensorlist->widget, FALSE, TRUE);
331         } else {
332                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
333                                 ui->ui_sensorlist->widget, FALSE, TRUE);
334                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
335                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
336         }
337
338         if (cfg->window_restore_enabled)
339                 gtk_paned_set_position(GTK_PANED(ui->sensor_box),
340                                        ui->config->window_divider_pos);
341
342
343         if (!init)
344                 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
345
346         gtk_widget_show_all(ui->sensor_box);
347
348         if (cfg->menu_bar_disabled)
349                 menu_bar_show(0, ui);
350         else
351                 menu_bar_show(1, ui);
352 }
353
354 void ui_window_show(struct ui_psensor *ui)
355 {
356         gtk_widget_show_all(ui->main_window);
357
358 }