set parent of the about dialog
[psensor.git] / src / ui.c
1 /*
2  * Copyright (C) 2010-2016 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 <stdlib.h>
20
21 #include <cfg.h>
22 #include <slog.h>
23 #include <ui.h>
24 #include <ui_appindicator.h>
25 #include <ui_graph.h>
26 #include <ui_pref.h>
27 #include <ui_sensorlist.h>
28 #include <ui_sensorpref.h>
29 #include <ui_status.h>
30
31 static GtkWidget *w_sensors_scrolled_tree;
32 static GtkWidget *w_graph;
33 static GtkContainer *w_sensor_box;
34 static GtkContainer *w_main_box;
35
36 static void update_layout(void)
37 {
38         enum sensorlist_position sensorlist_pos;
39
40         g_object_ref(w_sensors_scrolled_tree);
41         g_object_ref(w_graph);
42
43         gtk_container_remove(w_sensor_box,
44                              w_sensors_scrolled_tree);
45
46         gtk_container_remove(w_sensor_box, w_graph);
47
48         gtk_container_remove(w_main_box, GTK_WIDGET(w_sensor_box));
49
50         sensorlist_pos = config_get_sensorlist_position();
51         if (sensorlist_pos == SENSORLIST_POSITION_RIGHT
52             || sensorlist_pos == SENSORLIST_POSITION_LEFT)
53                 w_sensor_box
54                         = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_HORIZONTAL));
55         else
56                 w_sensor_box
57                         = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_VERTICAL));
58
59         gtk_box_pack_end(GTK_BOX(w_main_box),
60                          GTK_WIDGET(w_sensor_box), TRUE, TRUE, 2);
61
62         if (sensorlist_pos == SENSORLIST_POSITION_RIGHT
63             || sensorlist_pos == SENSORLIST_POSITION_BOTTOM) {
64                 gtk_paned_pack1(w_sensor_box, w_graph, TRUE, TRUE);
65                 gtk_paned_pack2(w_sensor_box,
66                                 w_sensors_scrolled_tree,
67                                 FALSE,
68                                 TRUE);
69         } else {
70                 gtk_paned_pack1(w_sensor_box,
71                                 w_sensors_scrolled_tree,
72                                 FALSE,
73                                 TRUE);
74                 gtk_paned_pack2(w_sensor_box, w_graph, TRUE, TRUE);
75         }
76
77         g_object_unref(w_sensors_scrolled_tree);
78         g_object_unref(w_graph);
79
80         gtk_widget_show_all(GTK_WIDGET(w_sensor_box));
81 }
82
83 static void set_decoration(GtkWindow *win)
84 {
85         gtk_window_set_decorated(win, config_is_window_decoration_enabled());
86 }
87
88 static void set_keep_below(GtkWindow *win)
89 {
90         gtk_window_set_keep_below(win, config_is_window_keep_below_enabled());
91 }
92
93 static void set_menu_bar_enabled(GtkWidget *bar)
94 {
95         if (config_is_menu_bar_enabled())
96                 gtk_widget_show(bar);
97         else
98                 gtk_widget_hide(bar);
99 }
100
101 static void
102 decoration_changed_cbk(GSettings *settings, gchar *key, gpointer data)
103 {
104         set_decoration(GTK_WINDOW(data));
105 }
106
107 static void
108 keep_below_changed_cbk(GSettings *settings, gchar *key, gpointer data)
109 {
110         set_keep_below(GTK_WINDOW(data));
111 }
112
113 static void
114 menu_bar_changed_cbk(GSettings *settings, gchar *key, gpointer data)
115 {
116         set_menu_bar_enabled(GTK_WIDGET(data));
117 }
118
119 static void
120 sensorlist_position_changed_cbk(GSettings *settings, gchar *key, gpointer data)
121 {
122         update_layout();
123 }
124
125 static void connect_cbks(GtkWindow *win, GtkWidget *menu_bar)
126 {
127         log_fct_enter();
128
129         g_signal_connect_after(config_get_GSettings(),
130                                "changed::interface-window-decoration-disabled",
131                                G_CALLBACK(decoration_changed_cbk),
132                                win);
133
134         g_signal_connect_after(config_get_GSettings(),
135                                "changed::interface-window-keep-below-enabled",
136                                G_CALLBACK(keep_below_changed_cbk),
137                                win);
138
139         g_signal_connect_after(config_get_GSettings(),
140                                "changed::interface-menu-bar-disabled",
141                                G_CALLBACK(menu_bar_changed_cbk),
142                                menu_bar);
143
144         g_signal_connect_after(config_get_GSettings(),
145                                "changed::interface-sensorlist-position",
146                                G_CALLBACK(sensorlist_position_changed_cbk),
147                                menu_bar);
148
149
150         log_fct_exit();
151 }
152
153 static void save_window_pos(struct ui_psensor *ui)
154 {
155         gboolean visible;
156         GtkWindow *win;
157         struct config *cfg;
158
159         visible = gtk_widget_get_visible(ui->main_window);
160         log_debug("Window visible: %d", visible);
161
162         if (visible == TRUE) {
163                 cfg = ui->config;
164
165                 win = GTK_WINDOW(ui->main_window);
166
167                 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
168                 log_debug("Window position: %d %d",
169                           cfg->window_x,
170                           cfg->window_y);
171
172                 gtk_window_get_size(win,
173                                     &cfg->window_w,
174                                     &cfg->window_h);
175                 log_debug("Window size: %d %d", cfg->window_w, cfg->window_h);
176
177                 cfg->window_divider_pos
178                         = gtk_paned_get_position(GTK_PANED(w_sensor_box));
179
180                 config_save(cfg);
181         }
182 }
183
184 static gboolean
185 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
186 {
187         struct ui_psensor *ui = data;
188
189         save_window_pos(ui);
190
191         log_debug("is_status_supported: %d\n", is_status_supported());
192
193         if (is_appindicator_supported() || is_status_supported())
194                 gtk_widget_hide(ui->main_window);
195         else
196                 ui_psensor_quit(ui);
197
198         return TRUE;
199 }
200
201 void ui_show_about_dialog(GtkWidget *parent)
202 {
203         static const char *const authors[] = { "jeanfi@gmail.com", NULL };
204
205         log_fct("parent=%p", parent);
206
207         gtk_show_about_dialog
208                 (parent,
209                  "authors", authors,
210                  "comments",
211                  _("Psensor is a GTK+ application for monitoring hardware "
212                    "sensors"),
213                  "copyright",
214                  _("Copyright(c) 2010-2016 jeanfi@gmail.com"),
215 #if GTK_CHECK_VERSION(3, 12, 0)
216                  "license-type", GTK_LICENSE_GPL_2_0,
217 #endif
218                  "logo-icon-name", "psensor",
219                  "program-name", "Psensor",
220                  "title", _("About Psensor"),
221                  "translator-credits", _("translator-credits"),
222                  "version", VERSION,
223                  "website", PACKAGE_URL,
224                  "website-label", _("Psensor Homepage"),
225                  NULL);
226 }
227
228 void ui_cb_about(GtkAction *a, gpointer data)
229 {
230         struct ui_psensor *ui;
231         GtkWidget *parent;
232
233         ui = (struct ui_psensor *)data;
234
235         log_fct("ui=%p", ui);
236
237         if (ui)
238                 parent = ui->main_window;
239         else
240                 parent = NULL;
241
242         ui_show_about_dialog(parent);
243 }
244
245 void ui_cb_menu_quit(GtkMenuItem *mi, gpointer data)
246 {
247         ui_psensor_quit((struct ui_psensor *)data);
248 }
249
250 void ui_cb_preferences(GtkMenuItem *mi, gpointer data)
251 {
252         ui_pref_dialog_run((struct ui_psensor *)data);
253 }
254
255 void ui_cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
256 {
257         struct ui_psensor *ui = data;
258
259         if (ui->sensors && *ui->sensors)
260                 ui_sensorpref_dialog_run(*ui->sensors, ui);
261 }
262
263 void ui_psensor_quit(struct ui_psensor *ui)
264 {
265         save_window_pos(ui);
266
267         log_debug("Destroy main window");
268         gtk_widget_destroy(ui->main_window);
269         gtk_main_quit();
270 }
271
272 void ui_enable_alpha_channel(struct ui_psensor *ui)
273 {
274         GdkScreen *screen;
275         GdkVisual *visual;
276         struct config *cfg;
277
278         cfg = ui->config;
279
280         screen = gtk_widget_get_screen(ui->main_window);
281
282         log_debug("Config alpha channel enabled: %d",
283                   cfg->alpha_channel_enabled);
284         if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
285                 log_debug("Screen is composited");
286                 visual = gdk_screen_get_rgba_visual(screen);
287                 if (visual) {
288                         gtk_widget_set_visual(ui->main_window, visual);
289                 } else {
290                         cfg->alpha_channel_enabled = 0;
291                         log_err("Enable alpha channel has failed");
292                 }
293         } else {
294                 cfg->alpha_channel_enabled = 0;
295         }
296 }
297
298 static void slog_enabled_cbk(void *data)
299 {
300         struct ui_psensor *ui;
301         struct psensor **sensors;
302         pthread_mutex_t *mutex;
303
304         ui = (struct ui_psensor *)data;
305         sensors = ui->sensors;
306         mutex = &ui->sensors_mutex;
307
308         log_debug("slog_enabled_cbk");
309
310         if (is_slog_enabled())
311                 slog_activate(NULL, sensors, mutex, config_get_slog_interval());
312         else
313                 slog_close();
314 }
315
316 void ui_window_create(struct ui_psensor *ui)
317 {
318         GtkWidget *window, *menu_bar;
319         GdkPixbuf *icon;
320         GtkIconTheme *icon_theme;
321         struct config *cfg;
322         guint ok;
323         GtkBuilder *builder;
324         GError *error;
325
326         log_fct("ui=%p", ui);
327
328         builder = gtk_builder_new();
329
330         error = NULL;
331         ok = gtk_builder_add_from_file
332                 (builder,
333                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
334                  &error);
335
336         if (!ok) {
337                 log_printf(LOG_ERR, error->message);
338                 g_error_free(error);
339                 return;
340         }
341
342         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
343         gtk_builder_connect_signals(builder, ui);
344         cfg = ui->config;
345         if (cfg->window_restore_enabled)
346                 gtk_window_move(GTK_WINDOW(window),
347                                 cfg->window_x,
348                                 cfg->window_y);
349
350         config_set_slog_enabled_changed_cbk(slog_enabled_cbk, ui);
351
352         gtk_window_set_default_size(GTK_WINDOW(window),
353                                     cfg->window_w,
354                                     cfg->window_h);
355
356         icon_theme = gtk_icon_theme_get_default();
357         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
358         if (icon)
359                 gtk_window_set_icon(GTK_WINDOW(window), icon);
360         else
361                 log_err(_("Failed to load Psensor icon."));
362
363         g_signal_connect(window,
364                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
365
366         set_decoration(GTK_WINDOW(window));
367         set_keep_below(GTK_WINDOW(window));
368
369         menu_bar = GTK_WIDGET(gtk_builder_get_object(builder, "menu_bar"));
370         w_main_box = GTK_CONTAINER(gtk_builder_get_object(builder, "main_box"));
371         ui->popup_menu = GTK_WIDGET(gtk_builder_get_object(builder,
372                                                            "popup_menu"));
373         g_object_ref(G_OBJECT(ui->popup_menu));
374         ui->main_window = window;
375         w_graph = GTK_WIDGET(gtk_builder_get_object(builder, "graph"));
376         ui_graph_create(ui);
377
378         w_sensor_box = GTK_PANED(gtk_builder_get_object(builder,
379                                                         "sensor_box"));
380         ui->sensors_store = GTK_LIST_STORE(gtk_builder_get_object
381                                            (builder, "sensors_store"));
382         ui->sensors_tree = GTK_TREE_VIEW(gtk_builder_get_object
383                                          (builder, "sensors_tree"));
384         w_sensors_scrolled_tree
385                 = GTK_SCROLLED_WINDOW(gtk_builder_get_object
386                                       (builder, "sensors_scrolled_tree"));
387
388         ui_sensorlist_create(ui);
389
390         connect_cbks(GTK_WINDOW(window), menu_bar);
391
392         log_debug("ui_window_create(): show_all");
393         update_layout();
394         gtk_widget_show_all(GTK_WIDGET(w_main_box));
395         set_menu_bar_enabled(menu_bar);
396
397         g_object_unref(G_OBJECT(builder));
398
399         log_debug("ui_window_create() ends");
400 }
401
402 void ui_window_update(struct ui_psensor *ui)
403 {
404         struct config *cfg;
405         enum sensorlist_position sensorlist_pos;
406
407         log_debug("ui_window_update()");
408
409         cfg = ui->config;
410
411         if (cfg->window_restore_enabled)
412                 gtk_paned_set_position(w_sensor_box, cfg->window_divider_pos);
413
414 }
415
416 void ui_window_show(struct ui_psensor *ui)
417 {
418         log_debug("ui_window_show()");
419         ui_window_update(ui);
420         gtk_window_present(GTK_WINDOW(ui->main_window));
421 }
422
423 static int cmp_sensors(const void *p1, const void *p2)
424 {
425         const struct psensor *s1, *s2;
426         int pos1, pos2;
427
428         s1 = *(void **)p1;
429         s2 = *(void **)p2;
430
431         pos1 = config_get_sensor_position(s1->id);
432         pos2 = config_get_sensor_position(s2->id);
433
434         return pos1 - pos2;
435 }
436
437 struct psensor **ui_get_sensors_ordered_by_position(struct psensor **sensors)
438 {
439         struct psensor **result;
440
441         result = psensor_list_copy(sensors);
442         qsort(result,
443               psensor_list_size(result),
444               sizeof(struct psensor *),
445               cmp_sensors);
446
447         return result;
448 }
449
450 GtkWidget *ui_get_graph(void)
451 {
452         return w_graph;
453 }