2 Copyright (C) 2010-2011 jeanfi@gmail.com
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.
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.
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
24 #include "ui_sensorpref.h"
25 #include "ui_sensorlist.h"
26 #include "ui_appindicator.h"
28 static void save_window_pos(struct ui_psensor *ui)
34 visible = gtk_widget_get_visible(ui->main_window);
35 log_printf(LOG_DEBUG, "Window visible: %d", visible);
37 if (visible == TRUE) {
38 win = GTK_WINDOW(ui->main_window);
40 gtk_window_get_position(win, &x, &y);
41 gtk_window_get_size(win, &w, &h);
43 log_printf(LOG_DEBUG, "Window position: %d %d", x, y);
44 log_printf(LOG_DEBUG, "Window size: %d %d", w, h);
49 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
51 struct ui_psensor *ui = data;
55 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
56 if (is_appindicator_supported()) {
57 log_puts(LOG_DEBUG, "hiding, WM is supporting appindicator");
58 gtk_widget_hide(ui->main_window);
60 log_puts(LOG_DEBUG, "quitting, WM not supporting appindicator");
70 void ui_show_about_dialog()
72 gtk_show_about_dialog(NULL,
74 _("Psensor is a GTK+ application for monitoring "
77 _("Copyright(c) 2010-2011\njeanfi@gmail.com"),
78 "logo-icon-name", "psensor",
79 "program-name", "Psensor",
80 "title", _("About Psensor"),
82 "website", PACKAGE_URL,
83 "website-label", _("Psensor Homepage"),
87 static void cb_about(GtkMenuItem *mi, gpointer data)
89 ui_show_about_dialog();
92 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
94 ui_psensor_quit((struct ui_psensor *)data);
97 static void cb_preferences(GtkMenuItem *mi, gpointer data)
99 ui_pref_dialog_run((struct ui_psensor *)data);
102 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
104 struct ui_psensor *ui = data;
106 if (ui->sensors && *ui->sensors)
107 ui_sensorpref_dialog_run(*ui->sensors, ui);
110 void ui_psensor_quit(struct ui_psensor *ui)
114 log_puts(LOG_DEBUG, "Destroy main window");
115 gtk_widget_destroy(ui->main_window);
119 static const char *menu_desc =
121 " <menubar name='MainMenu'>"
122 " <menu name='Psensor' action='PsensorMenuAction'>"
123 " <menuitem name='Preferences' action='PreferencesAction' />"
124 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
126 " <menuitem name='Quit' action='QuitAction' />"
128 " <menu name='Help' action='HelpMenuAction'>"
129 " <menuitem name='About' action='AboutAction' />"
134 static GtkActionEntry entries[] = {
135 { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
137 { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */
138 N_("_Preferences"), NULL, /* label, accelerator */
139 N_("Preferences"), /* tooltip */
140 G_CALLBACK(cb_preferences) },
142 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
143 N_("_Sensor Preferences"), NULL,
144 N_("Sensor Preferences"),
145 G_CALLBACK(cb_sensor_preferences) },
148 GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"), G_CALLBACK(cb_menu_quit) },
150 { "HelpMenuAction", NULL, "_Help" },
152 { "AboutAction", GTK_STOCK_PREFERENCES,
155 G_CALLBACK(cb_about) }
157 static guint n_entries = G_N_ELEMENTS(entries);
159 static GtkWidget *get_menu(struct ui_psensor *ui)
161 GtkActionGroup *action_group;
162 GtkUIManager *menu_manager;
165 action_group = gtk_action_group_new("PsensorActions");
166 gtk_action_group_set_translation_domain(action_group, PACKAGE);
167 menu_manager = gtk_ui_manager_new();
169 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
170 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
173 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
176 g_error(_("building menus failed: %s"), error->message);
178 return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
181 static unsigned int enable_alpha_channel(GtkWidget *w)
183 GdkScreen *screen = gtk_widget_get_screen(w);
185 #if (GTK_CHECK_VERSION(3, 0, 0))
186 GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
189 gtk_widget_set_visual(w, visual);
193 GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
196 gtk_widget_set_colormap(w, colormap);
203 void ui_window_create(struct ui_psensor *ui)
205 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
208 GtkIconTheme *icon_theme;
211 gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
213 gtk_window_set_title(GTK_WINDOW(window),
214 _("Psensor - Temperature Monitor"));
215 gtk_window_set_role(GTK_WINDOW(window), "psensor");
217 screen = gtk_widget_get_screen(window);
219 if (ui->config->alpha_channel_enabled
220 && gdk_screen_is_composited(screen)) {
221 if (!enable_alpha_channel(window))
222 ui->config->alpha_channel_enabled = 0;
224 ui->config->alpha_channel_enabled = 0;
227 icon_theme = gtk_icon_theme_get_default();
228 icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
230 gtk_window_set_icon(GTK_WINDOW(window), icon);
232 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
234 g_signal_connect(window,
235 "delete_event", G_CALLBACK(on_delete_event_cb), ui);
237 gtk_window_set_decorated(GTK_WINDOW(window),
238 ui->config->window_decoration_enabled);
240 gtk_window_set_keep_below(GTK_WINDOW(window),
241 ui->config->window_keep_below_enabled);
244 menubar = get_menu(ui);
246 ui->main_box = gtk_vbox_new(FALSE, 1);
248 gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
251 gtk_container_add(GTK_CONTAINER(window), ui->main_box);
253 ui->main_window = window;
254 ui->menu_bar = menubar;
256 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
257 if (ui->config->hide_on_startup)
258 gtk_widget_show_all(ui->main_box);
260 gtk_widget_show_all(ui->main_window);
262 gtk_widget_show_all(ui->main_window);
266 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
269 gtk_widget_show(ui->menu_bar);
271 gtk_widget_hide(ui->menu_bar);
274 void ui_window_update(struct ui_psensor *ui)
281 if (ui->sensor_box) {
282 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
284 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
285 ui->ui_sensorlist->widget);
287 gtk_container_remove(GTK_CONTAINER(ui->main_box),
290 ui->w_graph = ui_graph_create(ui);
295 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
296 || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
297 ui->sensor_box = gtk_hpaned_new();
299 ui->sensor_box = gtk_vpaned_new();
301 gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
303 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
304 || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
305 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
306 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
307 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
308 ui->ui_sensorlist->widget, FALSE, TRUE);
310 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
311 ui->ui_sensorlist->widget, FALSE, TRUE);
312 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
313 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
317 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
319 gtk_widget_show_all(ui->sensor_box);
321 if (cfg->menu_bar_disabled)
322 menu_bar_show(0, ui);
324 menu_bar_show(1, ui);