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