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