2 * Copyright (C) 2010-2012 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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
23 #include "ui_sensorpref.h"
24 #include "ui_sensorlist.h"
25 #include "ui_status.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_debug("Window visible: %d", visible);
37 if (visible == TRUE) {
40 win = GTK_WINDOW(ui->main_window);
42 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
43 log_debug("Window position: %d %d",
47 gtk_window_get_size(win,
50 log_debug("Window size: %d %d", cfg->window_w, cfg->window_h);
52 cfg->window_divider_pos
53 = gtk_paned_get_position(GTK_PANED(ui->sensor_box));
60 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
62 struct ui_psensor *ui = data;
66 log_debug("is_status_supported: %d\n", is_status_supported());
68 if (is_appindicator_supported() || is_status_supported())
69 gtk_widget_hide(ui->main_window);
76 void ui_show_about_dialog()
78 gtk_show_about_dialog(NULL,
80 _("Psensor is a GTK+ application for monitoring "
83 _("Copyright(c) 2010-2012\njeanfi@gmail.com"),
84 "logo-icon-name", "psensor",
85 "program-name", "Psensor",
86 "title", _("About Psensor"),
88 "website", PACKAGE_URL,
89 "website-label", _("Psensor Homepage"),
93 static void cb_about(GtkMenuItem *mi, gpointer data)
95 ui_show_about_dialog();
98 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
100 ui_psensor_quit((struct ui_psensor *)data);
103 static void cb_preferences(GtkMenuItem *mi, gpointer data)
105 ui_pref_dialog_run((struct ui_psensor *)data);
108 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
110 struct ui_psensor *ui = data;
112 if (ui->sensors && *ui->sensors)
113 ui_sensorpref_dialog_run(*ui->sensors, ui);
116 void ui_psensor_quit(struct ui_psensor *ui)
120 log_debug("Destroy main window");
121 gtk_widget_destroy(ui->main_window);
125 static const char *menu_desc =
127 " <menubar name='MainMenu'>"
128 " <menu name='Psensor' action='PsensorMenuAction'>"
129 " <menuitem name='Preferences' action='PreferencesAction' />"
130 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
132 " <menuitem name='Quit' action='QuitAction' />"
134 " <menu name='Help' action='HelpMenuAction'>"
135 " <menuitem name='About' action='AboutAction' />"
140 static GtkActionEntry entries[] = {
141 { "PsensorMenuAction", NULL, "_Psensor" },
143 { "PreferencesAction", GTK_STOCK_PREFERENCES,
144 N_("_Preferences"), NULL,
146 G_CALLBACK(cb_preferences) },
148 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
149 N_("_Sensor Preferences"), NULL,
150 N_("Sensor Preferences"),
151 G_CALLBACK(cb_sensor_preferences) },
154 GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"),
155 G_CALLBACK(cb_menu_quit) },
157 { "HelpMenuAction", NULL, "_Help" },
159 { "AboutAction", GTK_STOCK_PREFERENCES,
162 G_CALLBACK(cb_about) }
164 static guint n_entries = G_N_ELEMENTS(entries);
166 static GtkWidget *get_menu(struct ui_psensor *ui)
168 GtkActionGroup *action_group;
169 GtkUIManager *menu_manager;
172 action_group = gtk_action_group_new("PsensorActions");
173 gtk_action_group_set_translation_domain(action_group, PACKAGE);
174 menu_manager = gtk_ui_manager_new();
176 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
177 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
180 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
183 g_error(_("building menus failed: %s"), error->message);
185 return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
188 void ui_enable_alpha_channel(struct ui_psensor *ui)
196 screen = gtk_widget_get_screen(ui->main_window);
198 log_debug("Config alpha channel enabled: %d",
199 cfg->alpha_channel_enabled);
200 if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
201 log_debug("Screen is composited");
202 visual = gdk_screen_get_rgba_visual(screen);
204 gtk_widget_set_visual(ui->main_window, visual);
206 cfg->alpha_channel_enabled = 0;
207 log_err("Enable alpha channel has failed");
210 cfg->alpha_channel_enabled = 0;
215 void ui_window_create(struct ui_psensor *ui)
217 GtkWidget *window, *menubar;
219 GtkIconTheme *icon_theme;
222 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
225 if (cfg->window_restore_enabled)
226 gtk_window_move(GTK_WINDOW(window),
230 gtk_window_set_default_size(GTK_WINDOW(window),
234 gtk_window_set_title(GTK_WINDOW(window),
235 _("Psensor - Temperature Monitor"));
236 gtk_window_set_role(GTK_WINDOW(window), "psensor");
238 icon_theme = gtk_icon_theme_get_default();
239 icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
241 gtk_window_set_icon(GTK_WINDOW(window), icon);
243 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
245 g_signal_connect(window,
246 "delete_event", G_CALLBACK(on_delete_event_cb), ui);
248 gtk_window_set_decorated(GTK_WINDOW(window),
249 cfg->window_decoration_enabled);
251 gtk_window_set_keep_below(GTK_WINDOW(window),
252 cfg->window_keep_below_enabled);
255 menubar = get_menu(ui);
257 ui->main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
258 gtk_box_set_homogeneous(GTK_BOX(ui->main_box), FALSE);
259 gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
262 gtk_container_add(GTK_CONTAINER(window), ui->main_box);
264 ui->main_window = window;
265 ui->menu_bar = menubar;
267 gtk_widget_show_all(ui->main_box);
270 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
273 gtk_widget_show(ui->menu_bar);
275 gtk_widget_hide(ui->menu_bar);
278 void ui_window_update(struct ui_psensor *ui)
285 if (ui->sensor_box) {
286 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
288 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
289 ui->ui_sensorlist->widget);
291 gtk_container_remove(GTK_CONTAINER(ui->main_box),
294 ui->w_graph = ui_graph_create(ui);
299 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
300 || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
301 ui->sensor_box = gtk_paned_new(GTK_ORIENTATION_HORIZONTAL);
303 ui->sensor_box = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
305 gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
307 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
308 || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
309 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
310 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
311 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
312 ui->ui_sensorlist->widget, FALSE, TRUE);
314 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
315 ui->ui_sensorlist->widget, FALSE, TRUE);
316 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
317 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
320 if (cfg->window_restore_enabled)
321 gtk_paned_set_position(GTK_PANED(ui->sensor_box),
322 ui->config->window_divider_pos);
325 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
327 gtk_widget_show_all(ui->sensor_box);
329 if (cfg->menu_bar_disabled)
330 menu_bar_show(0, ui);
332 menu_bar_show(1, ui);
335 void ui_window_show(struct ui_psensor *ui)
337 log_debug("ui_window_show()");
338 gtk_window_present(GTK_WINDOW(ui->main_window));