updated to new version of checkpatch.pl
[psensor.git] / src / ui.c
1 /*
2  * Copyright (C) 2010-2012 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
79                 (NULL,
80                  "comments",
81                  _("Psensor is a GTK+ application for monitoring hardware "
82                    "sensors"),
83                  "copyright",
84                  _("Copyright(c) 2010-2012\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" },
143
144         { "PreferencesAction", GTK_STOCK_PREFERENCES,
145           N_("_Preferences"), NULL,
146           N_("Preferences"),
147           G_CALLBACK(cb_preferences) },
148
149         { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
150           N_("S_ensor Preferences"), NULL,
151           N_("Sensor Preferences"),
152           G_CALLBACK(cb_sensor_preferences) },
153
154         { "QuitAction",
155           GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"),
156           G_CALLBACK(cb_menu_quit) },
157
158         { "HelpMenuAction", NULL, N_("_Help") },
159
160         { "AboutAction", GTK_STOCK_PREFERENCES,
161           N_("_About"), NULL,
162           N_("About"),
163           G_CALLBACK(cb_about) }
164 };
165 static guint n_entries = G_N_ELEMENTS(entries);
166
167 static GtkWidget *get_menu(struct ui_psensor *ui)
168 {
169         GtkActionGroup *action_group;
170         GtkUIManager *menu_manager;
171         GError *error;
172
173         action_group = gtk_action_group_new("PsensorActions");
174         gtk_action_group_set_translation_domain(action_group, PACKAGE);
175         menu_manager = gtk_ui_manager_new();
176
177         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
178         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
179
180         error = NULL;
181         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
182
183         if (error)
184                 g_error(_("building menus failed: %s"), error->message);
185
186         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
187 }
188
189 void ui_enable_alpha_channel(struct ui_psensor *ui)
190 {
191         GdkScreen *screen;
192         GdkVisual *visual;
193         struct config *cfg;
194
195         cfg = ui->config;
196
197         screen = gtk_widget_get_screen(ui->main_window);
198
199         log_debug("Config alpha channel enabled: %d",
200                   cfg->alpha_channel_enabled);
201         if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
202                 log_debug("Screen is composited");
203                 visual = gdk_screen_get_rgba_visual(screen);
204                 if (visual) {
205                         gtk_widget_set_visual(ui->main_window, visual);
206                 } else {
207                         cfg->alpha_channel_enabled = 0;
208                         log_err("Enable alpha channel has failed");
209                 }
210         } else {
211                 cfg->alpha_channel_enabled = 0;
212         }
213
214 }
215
216 void ui_window_create(struct ui_psensor *ui)
217 {
218         GtkWidget *window, *menubar;
219         GdkPixbuf *icon;
220         GtkIconTheme *icon_theme;
221         struct config *cfg;
222
223         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
224
225         cfg = ui->config;
226         if (cfg->window_restore_enabled)
227                 gtk_window_move(GTK_WINDOW(window),
228                                 cfg->window_x,
229                                 cfg->window_y);
230
231         gtk_window_set_default_size(GTK_WINDOW(window),
232                                     cfg->window_w,
233                                     cfg->window_h);
234
235         gtk_window_set_title(GTK_WINDOW(window),
236                              _("Psensor - Temperature Monitor"));
237         gtk_window_set_role(GTK_WINDOW(window), "psensor");
238
239         icon_theme = gtk_icon_theme_get_default();
240         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
241         if (icon)
242                 gtk_window_set_icon(GTK_WINDOW(window), icon);
243         else
244                 log_err(_("Failed to load Psensor icon."));
245
246         g_signal_connect(window,
247                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
248
249         gtk_window_set_decorated(GTK_WINDOW(window),
250                                  cfg->window_decoration_enabled);
251
252         gtk_window_set_keep_below(GTK_WINDOW(window),
253                                   cfg->window_keep_below_enabled);
254
255         /* main box */
256         menubar = get_menu(ui);
257
258         ui->main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
259         gtk_box_set_homogeneous(GTK_BOX(ui->main_box), FALSE);
260         gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
261                            FALSE, TRUE, 0);
262
263         gtk_container_add(GTK_CONTAINER(window), ui->main_box);
264
265         ui->main_window = window;
266         ui->menu_bar = menubar;
267
268         gtk_widget_show_all(ui->main_box);
269 }
270
271 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
272 {
273         if (show)
274                 gtk_widget_show(ui->menu_bar);
275         else
276                 gtk_widget_hide(ui->menu_bar);
277 }
278
279 void ui_window_update(struct ui_psensor *ui)
280 {
281         struct config *cfg;
282         int init = 1;
283
284         cfg = ui->config;
285
286         if (ui->sensor_box) {
287                 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
288
289                 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
290                                      ui->ui_sensorlist->widget);
291
292                 gtk_container_remove(GTK_CONTAINER(ui->main_box),
293                                      ui->sensor_box);
294
295                 ui->w_graph = ui_graph_create(ui);
296
297                 init = 0;
298         }
299
300         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
301             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
302                 ui->sensor_box = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
303         else
304                 ui->sensor_box = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
305
306         gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
307
308         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
309             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
310                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
311                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
312                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
313                                 ui->ui_sensorlist->widget, FALSE, TRUE);
314         } else {
315                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
316                                 ui->ui_sensorlist->widget, FALSE, TRUE);
317                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
318                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
319         }
320
321         if (cfg->window_restore_enabled)
322                 gtk_paned_set_position(GTK_PANED(ui->sensor_box),
323                                        ui->config->window_divider_pos);
324
325         if (!init)
326                 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
327
328         gtk_widget_show_all(ui->sensor_box);
329
330         if (cfg->menu_bar_disabled)
331                 menu_bar_show(0, ui);
332         else
333                 menu_bar_show(1, ui);
334 }
335
336 void ui_window_show(struct ui_psensor *ui)
337 {
338         log_debug("ui_window_show()");
339         gtk_window_present(GTK_WINDOW(ui->main_window));
340 }