style
[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 void ui_cb_about(GtkMenuItem *mi, gpointer data)
96 {
97         ui_show_about_dialog();
98 }
99
100 void ui_cb_menu_quit(GtkMenuItem *mi, gpointer data)
101 {
102         ui_psensor_quit((struct ui_psensor *)data);
103 }
104
105 void ui_cb_preferences(GtkMenuItem *mi, gpointer data)
106 {
107         ui_pref_dialog_run((struct ui_psensor *)data);
108 }
109
110 void ui_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 void ui_enable_alpha_channel(struct ui_psensor *ui)
128 {
129         GdkScreen *screen;
130         GdkVisual *visual;
131         struct config *cfg;
132
133         cfg = ui->config;
134
135         screen = gtk_widget_get_screen(ui->main_window);
136
137         log_debug("Config alpha channel enabled: %d",
138                   cfg->alpha_channel_enabled);
139         if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
140                 log_debug("Screen is composited");
141                 visual = gdk_screen_get_rgba_visual(screen);
142                 if (visual) {
143                         gtk_widget_set_visual(ui->main_window, visual);
144                 } else {
145                         cfg->alpha_channel_enabled = 0;
146                         log_err("Enable alpha channel has failed");
147                 }
148         } else {
149                 cfg->alpha_channel_enabled = 0;
150         }
151 }
152
153 static void
154 slog_enabled_cbk(GConfClient *client, guint id, GConfEntry *e, gpointer data)
155 {
156         struct ui_psensor *ui;
157         struct psensor **sensors;
158         pthread_mutex_t *mutex;
159
160         ui = (struct ui_psensor *)data;
161         sensors = ui->sensors;
162         mutex = &ui->sensors_mutex;
163
164         log_debug("slog_enabled_cbk");
165
166         if (is_slog_enabled())
167                 slog_activate(NULL, sensors, mutex, config_get_slog_interval());
168         else
169                 slog_close();
170 }
171
172 void ui_window_create(struct ui_psensor *ui)
173 {
174         GtkWidget *window;
175         GdkPixbuf *icon;
176         GtkIconTheme *icon_theme;
177         struct config *cfg;
178         guint ok;
179         GtkBuilder *builder;
180         GError *error;
181
182         builder = gtk_builder_new();
183
184         error = NULL;
185         ok = gtk_builder_add_from_file
186                 (builder,
187                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "psensor.glade",
188                  &error);
189
190         if (!ok) {
191                 log_printf(LOG_ERR, error->message);
192                 g_error_free(error);
193                 return ;
194         }
195
196         window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
197         gtk_builder_connect_signals(builder, ui);
198         cfg = ui->config;
199         if (cfg->window_restore_enabled)
200                 gtk_window_move(GTK_WINDOW(window),
201                                 cfg->window_x,
202                                 cfg->window_y);
203
204         config_slog_enabled_notify_add(slog_enabled_cbk, ui);
205
206         gtk_window_set_default_size(GTK_WINDOW(window),
207                                     cfg->window_w,
208                                     cfg->window_h);
209
210         icon_theme = gtk_icon_theme_get_default();
211         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
212         if (icon)
213                 gtk_window_set_icon(GTK_WINDOW(window), icon);
214         else
215                 log_err(_("Failed to load Psensor icon."));
216
217         g_signal_connect(window,
218                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
219
220         gtk_window_set_decorated(GTK_WINDOW(window),
221                                  cfg->window_decoration_enabled);
222
223         gtk_window_set_keep_below(GTK_WINDOW(window),
224                                   cfg->window_keep_below_enabled);
225
226         ui->menu_bar = GTK_WIDGET(gtk_builder_get_object(builder, "menu_bar"));
227         ui->main_box = GTK_WIDGET(gtk_builder_get_object(builder, "main_box"));
228         ui->popup_menu = GTK_WIDGET(gtk_builder_get_object(builder,
229                                                            "popup_menu"));
230         g_object_ref(G_OBJECT(ui->popup_menu));
231         ui->main_window = window;
232         ui->w_graph = GTK_WIDGET(gtk_builder_get_object(builder,
233                                                         "graph"));
234         ui_graph_create(ui);
235
236         ui->sensor_box = GTK_PANED(gtk_builder_get_object(builder,
237                                                           "sensor_box"));
238         ui->sensors_store = GTK_LIST_STORE(gtk_builder_get_object
239                                            (builder, "sensors_store"));
240         ui->sensors_tree = GTK_TREE_VIEW(gtk_builder_get_object
241                                          (builder, "sensors_tree"));
242         ui->sensors_scrolled_tree
243                 = GTK_SCROLLED_WINDOW(gtk_builder_get_object
244                                       (builder, "sensors_scrolled_tree"));
245
246         ui_sensorlist_create(ui);
247
248         log_debug("ui_window_create(): show_all");
249         gtk_widget_show_all(ui->main_box);
250
251         g_object_unref(G_OBJECT(builder));
252
253         log_debug("ui_window_create() ends");
254 }
255
256 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
257 {
258         if (show)
259                 gtk_widget_show(ui->menu_bar);
260         else
261                 gtk_widget_hide(ui->menu_bar);
262 }
263
264 void ui_window_update(struct ui_psensor *ui)
265 {
266         struct config *cfg;
267
268         log_debug("ui_window_update()");
269
270         cfg = ui->config;
271
272         g_object_ref(GTK_WIDGET(ui->sensors_scrolled_tree));
273         g_object_ref(GTK_WIDGET(ui->w_graph));
274
275         gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
276                              GTK_WIDGET(ui->sensors_scrolled_tree));
277
278         gtk_container_remove(GTK_CONTAINER(ui->sensor_box), ui->w_graph);
279
280         gtk_container_remove(GTK_CONTAINER(ui->main_box),
281                              GTK_WIDGET(ui->sensor_box));
282
283         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
284             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
285                 ui->sensor_box
286                         = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_HORIZONTAL));
287         else
288                 ui->sensor_box
289                         = GTK_PANED(gtk_paned_new(GTK_ORIENTATION_VERTICAL));
290
291         gtk_box_pack_end(GTK_BOX(ui->main_box),
292                          GTK_WIDGET(ui->sensor_box), TRUE, TRUE, 2);
293
294         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
295             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
296                 gtk_paned_pack1(ui->sensor_box,
297                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
298                 gtk_paned_pack2(ui->sensor_box,
299                                 GTK_WIDGET(ui->sensors_scrolled_tree),
300                                 FALSE, TRUE);
301         } else {
302                 gtk_paned_pack1(ui->sensor_box,
303                                 GTK_WIDGET(ui->sensors_scrolled_tree),
304                                 FALSE, TRUE);
305                 gtk_paned_pack2(ui->sensor_box,
306                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
307         }
308
309         if (cfg->window_restore_enabled)
310                 gtk_paned_set_position(ui->sensor_box, cfg->window_divider_pos);
311
312         g_object_unref(GTK_WIDGET(ui->sensors_scrolled_tree));
313         g_object_unref(GTK_WIDGET(ui->w_graph));
314
315         gtk_widget_show_all(GTK_WIDGET(ui->sensor_box));
316
317         if (cfg->menu_bar_disabled)
318                 menu_bar_show(0, ui);
319         else
320                 menu_bar_show(1, ui);
321 }
322
323 void ui_window_show(struct ui_psensor *ui)
324 {
325         log_debug("ui_window_show()");
326         ui_window_update(ui);
327         gtk_window_present(GTK_WINDOW(ui->main_window));
328 }