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