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