use glade to describe the main window
[psensor.git] / src / ui.c
1 /*
2  * Copyright (C) 2010-2013 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 "slog.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
80                 (NULL,
81                  "comments",
82                  _("Psensor is a GTK+ application for monitoring hardware "
83                    "sensors"),
84                  "copyright",
85                  _("Copyright(c) 2010-2012\njeanfi@gmail.com"),
86                  "logo-icon-name", "psensor",
87                  "program-name", "Psensor",
88                  "title", _("About Psensor"),
89                  "version", VERSION,
90                  "website", PACKAGE_URL,
91                  "website-label", _("Psensor Homepage"),
92                  NULL);
93 }
94
95 static void cb_about(GtkMenuItem *mi, gpointer data)
96 {
97         ui_show_about_dialog();
98 }
99
100 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
101 {
102         ui_psensor_quit((struct ui_psensor *)data);
103 }
104
105 static void cb_preferences(GtkMenuItem *mi, gpointer data)
106 {
107         ui_pref_dialog_run((struct ui_psensor *)data);
108 }
109
110 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
111 {
112         struct ui_psensor *ui = data;
113
114         if (ui->sensors && *ui->sensors)
115                 ui_sensorpref_dialog_run(*ui->sensors, ui);
116 }
117
118 void ui_psensor_quit(struct ui_psensor *ui)
119 {
120         save_window_pos(ui);
121
122         log_debug("Destroy main window");
123         gtk_widget_destroy(ui->main_window);
124         gtk_main_quit();
125 }
126
127 static const char *menu_desc =
128 "<ui>"
129 "  <menubar name='MainMenu'>"
130 "    <menu name='Psensor' action='PsensorMenuAction'>"
131 "      <menuitem name='Preferences' action='PreferencesAction' />"
132 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
133 "      <separator />"
134 "      <menuitem name='Quit' action='QuitAction' />"
135 "    </menu>"
136 "    <menu name='Help' action='HelpMenuAction'>"
137 "      <menuitem name='About' action='AboutAction' />"
138 "    </menu>"
139 "  </menubar>"
140 "</ui>";
141
142 static GtkActionEntry entries[] = {
143         { "PsensorMenuAction", NULL, "_Psensor" },
144
145         { "PreferencesAction", GTK_STOCK_PREFERENCES,
146           N_("_Preferences"), NULL,
147           N_("Preferences"),
148           G_CALLBACK(cb_preferences) },
149
150         { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
151           N_("S_ensor Preferences"), NULL,
152           N_("Sensor Preferences"),
153           G_CALLBACK(cb_sensor_preferences) },
154
155         { "QuitAction",
156           GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"),
157           G_CALLBACK(cb_menu_quit) },
158
159         { "HelpMenuAction", NULL, N_("_Help") },
160
161         { "AboutAction", GTK_STOCK_PREFERENCES,
162           N_("_About"), NULL,
163           N_("About"),
164           G_CALLBACK(cb_about) }
165 };
166 static guint n_entries = G_N_ELEMENTS(entries);
167
168 static GtkWidget *get_menu(struct ui_psensor *ui)
169 {
170         GtkActionGroup *action_group;
171         GtkUIManager *menu_manager;
172         GError *error;
173
174         action_group = gtk_action_group_new("PsensorActions");
175         gtk_action_group_set_translation_domain(action_group, PACKAGE);
176         menu_manager = gtk_ui_manager_new();
177
178         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
179         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
180
181         error = NULL;
182         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
183
184         if (error)
185                 g_error(_("building menus failed: %s"), error->message);
186
187         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
188 }
189
190 void ui_enable_alpha_channel(struct ui_psensor *ui)
191 {
192         GdkScreen *screen;
193         GdkVisual *visual;
194         struct config *cfg;
195
196         cfg = ui->config;
197
198         screen = gtk_widget_get_screen(ui->main_window);
199
200         log_debug("Config alpha channel enabled: %d",
201                   cfg->alpha_channel_enabled);
202         if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
203                 log_debug("Screen is composited");
204                 visual = gdk_screen_get_rgba_visual(screen);
205                 if (visual) {
206                         gtk_widget_set_visual(ui->main_window, visual);
207                 } else {
208                         cfg->alpha_channel_enabled = 0;
209                         log_err("Enable alpha channel has failed");
210                 }
211         } else {
212                 cfg->alpha_channel_enabled = 0;
213         }
214
215 }
216
217 static void on_slog_enabled_cb(GConfClient *client,
218                                guint cnxn_id,
219                                GConfEntry *entry,
220                                gpointer user_data)
221 {
222         struct ui_psensor *ui;
223         struct psensor **sensors;
224         pthread_mutex_t *mutex;
225
226         ui = (struct ui_psensor *)user_data;
227         sensors = ui->sensors;
228         mutex = &ui->sensors_mutex;
229
230         log_debug("cbk_slog_enabled");
231
232         if (is_slog_enabled())
233                 slog_activate(NULL, sensors, mutex, config_get_slog_interval());
234         else
235                 slog_close();
236 }
237
238 void ui_window_create(struct ui_psensor *ui)
239 {
240         GtkWidget *window, *menubar;
241         GdkPixbuf *icon;
242         GtkIconTheme *icon_theme;
243         struct config *cfg;
244         guint ok;
245         GtkBuilder *builder;
246         GError *error;
247
248         builder = gtk_builder_new();
249
250         error = NULL;
251         ok = gtk_builder_add_from_file
252                 (builder,
253                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
254                  &error);
255
256         if (!ok) {
257                 log_printf(LOG_ERR, error->message);
258                 g_error_free(error);
259                 return ;
260         }
261
262         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
263
264         cfg = ui->config;
265         if (cfg->window_restore_enabled)
266                 gtk_window_move(GTK_WINDOW(window),
267                                 cfg->window_x,
268                                 cfg->window_y);
269
270         config_slog_enabled_notify_add(on_slog_enabled_cb, ui);
271
272         gtk_window_set_default_size(GTK_WINDOW(window),
273                                     cfg->window_w,
274                                     cfg->window_h);
275
276         icon_theme = gtk_icon_theme_get_default();
277         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
278         if (icon)
279                 gtk_window_set_icon(GTK_WINDOW(window), icon);
280         else
281                 log_err(_("Failed to load Psensor icon."));
282
283         g_signal_connect(window,
284                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
285
286         gtk_window_set_decorated(GTK_WINDOW(window),
287                                  cfg->window_decoration_enabled);
288
289         gtk_window_set_keep_below(GTK_WINDOW(window),
290                                   cfg->window_keep_below_enabled);
291
292         /* main box */
293         menubar = get_menu(ui);
294
295         ui->main_box = GTK_WIDGET(gtk_builder_get_object(builder, "main_box"));
296         gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
297                            FALSE, TRUE, 0);
298
299         gtk_container_add(GTK_CONTAINER(window), ui->main_box);
300
301         ui->main_window = window;
302         ui->menu_bar = menubar;
303
304         gtk_widget_show_all(ui->main_box);
305 }
306
307 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
308 {
309         if (show)
310                 gtk_widget_show(ui->menu_bar);
311         else
312                 gtk_widget_hide(ui->menu_bar);
313 }
314
315 void ui_window_update(struct ui_psensor *ui)
316 {
317         struct config *cfg;
318         int init = 1;
319
320         cfg = ui->config;
321
322         if (ui->sensor_box) {
323                 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
324
325                 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
326                                      ui->ui_sensorlist->widget);
327
328                 gtk_container_remove(GTK_CONTAINER(ui->main_box),
329                                      ui->sensor_box);
330
331                 ui->w_graph = ui_graph_create(ui);
332
333                 init = 0;
334         }
335
336         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
337             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
338                 ui->sensor_box = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
339         else
340                 ui->sensor_box = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
341
342         gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
343
344         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
345             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
346                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
347                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
348                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
349                                 ui->ui_sensorlist->widget, FALSE, TRUE);
350         } else {
351                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
352                                 ui->ui_sensorlist->widget, FALSE, TRUE);
353                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
354                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
355         }
356
357         if (cfg->window_restore_enabled)
358                 gtk_paned_set_position(GTK_PANED(ui->sensor_box),
359                                        ui->config->window_divider_pos);
360
361         if (!init)
362                 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
363
364         gtk_widget_show_all(ui->sensor_box);
365
366         if (cfg->menu_bar_disabled)
367                 menu_bar_show(0, ui);
368         else
369                 menu_bar_show(1, ui);
370 }
371
372 void ui_window_show(struct ui_psensor *ui)
373 {
374         log_debug("ui_window_show()");
375         ui_window_update(ui);
376         gtk_window_present(GTK_WINDOW(ui->main_window));
377 }