support of gtk3
[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 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
41 {
42         ui_psensor_quit();
43 }
44
45 static void cb_preferences(GtkMenuItem *mi, gpointer data)
46 {
47         ui_pref_dialog_run((struct ui_psensor *)data);
48 }
49
50 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
51 {
52         struct ui_psensor *ui = data;
53
54         if (ui->sensors && *ui->sensors)
55                 ui_sensorpref_dialog_run(*ui->sensors, ui);
56 }
57
58 void ui_psensor_quit()
59 {
60         gtk_main_quit();
61 }
62
63 static const char *menu_desc =
64 "<ui>"
65 "  <menubar name='MainMenu'>"
66 "    <menu name='Psensor' action='PsensorMenuAction'>"
67 "      <menuitem name='Preferences' action='PreferencesAction' />"
68 "      <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
69 "      <separator />"
70 "      <menuitem name='Quit' action='QuitAction' />"
71 "    </menu>"
72 "  </menubar>"
73 "</ui>";
74
75 static GtkActionEntry entries[] = {
76   { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
77
78   { "PreferencesAction", GTK_STOCK_PREFERENCES,     /* name, stock id */
79     "_Preferences", NULL,                           /* label, accelerator */
80     "Preferences",                                  /* tooltip */
81     G_CALLBACK(cb_preferences) },
82
83   { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
84     "_Sensor Preferences",
85     NULL,
86     "SensorPreferences",
87     G_CALLBACK(cb_sensor_preferences) },
88
89   { "QuitAction",
90     GTK_STOCK_QUIT, "_Quit", NULL, "Quit", G_CALLBACK(cb_menu_quit) }
91 };
92 static guint n_entries = G_N_ELEMENTS(entries);
93
94 static GtkWidget *get_menu(struct ui_psensor *ui)
95 {
96         GtkActionGroup      *action_group;
97         GtkUIManager        *menu_manager;
98         GError              *error;
99
100         action_group = gtk_action_group_new("PsensorActions");
101         gtk_action_group_set_translation_domain(action_group, PACKAGE);
102         menu_manager = gtk_ui_manager_new();
103
104         gtk_action_group_add_actions(action_group, entries, n_entries, ui);
105         gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
106
107         error = NULL;
108         gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
109
110         if (error)
111                 g_error(_("building menus failed: %s"), error->message);
112
113         return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
114 }
115
116 static unsigned int enable_alpha_channel(GtkWidget *w)
117 {
118         GdkScreen *screen = gtk_widget_get_screen(w);
119
120 #if (GTK_CHECK_VERSION(3, 0, 0))
121         GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
122
123         if (visual) {
124                 gtk_widget_set_visual(w, visual);
125                 return 1;
126         }
127 #else
128         GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
129
130         if (colormap) {
131                 gtk_widget_set_colormap(w, colormap);
132                 return 1;
133         }
134 #endif
135         return 0;
136 }
137
138 void ui_window_create(struct ui_psensor *ui)
139 {
140         GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
141         GdkScreen *screen;
142         GdkPixbuf *icon;
143         GtkIconTheme *icon_theme;
144         GtkWidget *menubar;
145
146         gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
147
148         gtk_window_set_title(GTK_WINDOW(window),
149                              _("Psensor - Temperature Monitor"));
150         gtk_window_set_role(GTK_WINDOW(window), "psensor");
151
152         screen = gtk_widget_get_screen(window);
153
154         if (ui->config->alpha_channel_enabled
155             && gdk_screen_is_composited(screen)) {
156                 if (!enable_alpha_channel(window))
157                         ui->config->alpha_channel_enabled = 0;
158         } else {
159                 ui->config->alpha_channel_enabled = 0;
160         }
161
162         icon_theme = gtk_icon_theme_get_default();
163         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
164         if (icon)
165                 gtk_window_set_icon(GTK_WINDOW(window), icon);
166         else
167                 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
168
169         g_signal_connect(window,
170                          "delete_event", G_CALLBACK(on_delete_event_cb), ui);
171
172         gtk_window_set_decorated(GTK_WINDOW(window),
173                                  ui->config->window_decoration_enabled);
174
175         gtk_window_set_keep_below(GTK_WINDOW(window),
176                                   ui->config->window_keep_below_enabled);
177
178         /* main box */
179         menubar = get_menu(ui);
180
181         ui->main_box = gtk_vbox_new(FALSE, 1);
182
183         gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
184                            FALSE, TRUE, 0);
185
186         gtk_container_add(GTK_CONTAINER(window), ui->main_box);
187
188         ui->main_window = window;
189         ui->menu_bar = menubar;
190
191         gtk_widget_show_all(ui->main_window);
192 }
193
194 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
195 {
196         if (show)
197                 gtk_widget_show(ui->menu_bar);
198         else
199                 gtk_widget_hide(ui->menu_bar);
200 }
201
202 void ui_window_update(struct ui_psensor *ui)
203 {
204         struct config *cfg;
205         int init = 1;
206
207         cfg = ui->config;
208
209         if (ui->sensor_box) {
210                 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
211
212                 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
213                                      ui->ui_sensorlist->widget);
214
215                 gtk_container_remove(GTK_CONTAINER(ui->main_box),
216                                      ui->sensor_box);
217
218                 ui->w_graph = ui_graph_create(ui);
219
220                 init = 0;
221         }
222
223         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
224             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
225                 ui->sensor_box = gtk_hpaned_new();
226         else
227                 ui->sensor_box = gtk_vpaned_new();
228
229         gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
230
231         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
232             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
233                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
234                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
235                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
236                                 ui->ui_sensorlist->widget, FALSE, TRUE);
237         } else {
238                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
239                                 ui->ui_sensorlist->widget, FALSE, TRUE);
240                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
241                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
242         }
243
244         if (!init)
245                 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
246
247         gtk_widget_show_all(ui->sensor_box);
248
249         if (cfg->menu_bar_disabled)
250                 menu_bar_show(0, ui);
251         else
252                 menu_bar_show(1, ui);
253 }