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