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