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) {
40 win = GTK_WINDOW(ui->main_window);
42 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
44 "Window position: %d %d",
48 gtk_window_get_size(win,
56 cfg->window_divider_pos
57 = gtk_paned_get_position(GTK_PANED(ui->sensor_box));
64 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
66 struct ui_psensor *ui = data;
70 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
71 if (is_appindicator_supported()) {
72 log_printf(LOG_DEBUG, "hiding, WM is supporting appindicator");
73 gtk_widget_hide(ui->main_window);
75 log_printf(LOG_DEBUG, "quitting, WM not supporting appindicator");
85 void ui_show_about_dialog()
87 gtk_show_about_dialog(NULL,
89 _("Psensor is a GTK+ application for monitoring "
92 _("Copyright(c) 2010-2011\njeanfi@gmail.com"),
93 "logo-icon-name", "psensor",
94 "program-name", "Psensor",
95 "title", _("About Psensor"),
97 "website", PACKAGE_URL,
98 "website-label", _("Psensor Homepage"),
102 static void cb_about(GtkMenuItem *mi, gpointer data)
104 ui_show_about_dialog();
107 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
109 ui_psensor_quit((struct ui_psensor *)data);
112 static void cb_preferences(GtkMenuItem *mi, gpointer data)
114 ui_pref_dialog_run((struct ui_psensor *)data);
117 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
119 struct ui_psensor *ui = data;
121 if (ui->sensors && *ui->sensors)
122 ui_sensorpref_dialog_run(*ui->sensors, ui);
125 void ui_psensor_quit(struct ui_psensor *ui)
129 log_printf(LOG_DEBUG, "Destroy main window");
130 gtk_widget_destroy(ui->main_window);
134 static const char *menu_desc =
136 " <menubar name='MainMenu'>"
137 " <menu name='Psensor' action='PsensorMenuAction'>"
138 " <menuitem name='Preferences' action='PreferencesAction' />"
139 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
141 " <menuitem name='Quit' action='QuitAction' />"
143 " <menu name='Help' action='HelpMenuAction'>"
144 " <menuitem name='About' action='AboutAction' />"
149 static GtkActionEntry entries[] = {
150 { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
152 { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */
153 N_("_Preferences"), NULL, /* label, accelerator */
154 N_("Preferences"), /* tooltip */
155 G_CALLBACK(cb_preferences) },
157 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
158 N_("_Sensor Preferences"), NULL,
159 N_("Sensor Preferences"),
160 G_CALLBACK(cb_sensor_preferences) },
163 GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"), G_CALLBACK(cb_menu_quit) },
165 { "HelpMenuAction", NULL, "_Help" },
167 { "AboutAction", GTK_STOCK_PREFERENCES,
170 G_CALLBACK(cb_about) }
172 static guint n_entries = G_N_ELEMENTS(entries);
174 static GtkWidget *get_menu(struct ui_psensor *ui)
176 GtkActionGroup *action_group;
177 GtkUIManager *menu_manager;
180 action_group = gtk_action_group_new("PsensorActions");
181 gtk_action_group_set_translation_domain(action_group, PACKAGE);
182 menu_manager = gtk_ui_manager_new();
184 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
185 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
188 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
191 g_error(_("building menus failed: %s"), error->message);
193 return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
196 static unsigned int enable_alpha_channel(GtkWidget *w)
198 GdkScreen *screen = gtk_widget_get_screen(w);
200 #if (GTK_CHECK_VERSION(3, 0, 0))
201 GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
204 gtk_widget_set_visual(w, visual);
208 GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
211 gtk_widget_set_colormap(w, colormap);
218 void ui_window_create(struct ui_psensor *ui)
220 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
223 GtkIconTheme *icon_theme;
228 if (cfg->window_restore_enabled)
229 gtk_window_move(GTK_WINDOW(window),
233 gtk_window_set_default_size(GTK_WINDOW(window),
238 gtk_window_set_title(GTK_WINDOW(window),
239 _("Psensor - Temperature Monitor"));
240 gtk_window_set_role(GTK_WINDOW(window), "psensor");
242 screen = gtk_widget_get_screen(window);
244 if (ui->config->alpha_channel_enabled
245 && gdk_screen_is_composited(screen)) {
246 if (!enable_alpha_channel(window))
247 ui->config->alpha_channel_enabled = 0;
249 ui->config->alpha_channel_enabled = 0;
252 icon_theme = gtk_icon_theme_get_default();
253 icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
255 gtk_window_set_icon(GTK_WINDOW(window), icon);
257 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
259 g_signal_connect(window,
260 "delete_event", G_CALLBACK(on_delete_event_cb), ui);
262 gtk_window_set_decorated(GTK_WINDOW(window),
263 ui->config->window_decoration_enabled);
265 gtk_window_set_keep_below(GTK_WINDOW(window),
266 ui->config->window_keep_below_enabled);
269 menubar = get_menu(ui);
271 ui->main_box = gtk_vbox_new(FALSE, 1);
273 gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
276 gtk_container_add(GTK_CONTAINER(window), ui->main_box);
278 ui->main_window = window;
279 ui->menu_bar = menubar;
281 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
282 if (ui->config->hide_on_startup)
283 gtk_widget_show_all(ui->main_box);
291 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
294 gtk_widget_show(ui->menu_bar);
296 gtk_widget_hide(ui->menu_bar);
299 void ui_window_update(struct ui_psensor *ui)
306 if (ui->sensor_box) {
307 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
309 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
310 ui->ui_sensorlist->widget);
312 gtk_container_remove(GTK_CONTAINER(ui->main_box),
315 ui->w_graph = ui_graph_create(ui);
320 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
321 || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
322 ui->sensor_box = gtk_hpaned_new();
324 ui->sensor_box = gtk_vpaned_new();
326 gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
328 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
329 || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
330 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
331 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
332 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
333 ui->ui_sensorlist->widget, FALSE, TRUE);
335 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
336 ui->ui_sensorlist->widget, FALSE, TRUE);
337 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
338 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
341 if (cfg->window_restore_enabled)
342 gtk_paned_set_position(GTK_PANED(ui->sensor_box),
343 ui->config->window_divider_pos);
347 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
349 gtk_widget_show_all(ui->sensor_box);
351 if (cfg->menu_bar_disabled)
352 menu_bar_show(0, ui);
354 menu_bar_show(1, ui);
357 void ui_window_show(struct ui_psensor *ui)
359 gtk_widget_show_all(ui->main_window);