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