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