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_status.h"
27 #include "ui_appindicator.h"
29 static void save_window_pos(struct ui_psensor *ui)
35 visible = gtk_widget_get_visible(ui->main_window);
36 log_printf(LOG_DEBUG, "Window visible: %d", visible);
38 if (visible == TRUE) {
41 win = GTK_WINDOW(ui->main_window);
43 gtk_window_get_position(win, &cfg->window_x, &cfg->window_y);
45 "Window position: %d %d",
49 gtk_window_get_size(win,
57 cfg->window_divider_pos
58 = gtk_paned_get_position(GTK_PANED(ui->sensor_box));
65 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
67 struct ui_psensor *ui = data;
72 "is_status_supported: %d\n", is_status_supported());
74 if (is_appindicator_supported() || is_status_supported())
75 gtk_widget_hide(ui->main_window);
82 void ui_show_about_dialog()
84 gtk_show_about_dialog(NULL,
86 _("Psensor is a GTK+ application for monitoring "
89 _("Copyright(c) 2010-2011\njeanfi@gmail.com"),
90 "logo-icon-name", "psensor",
91 "program-name", "Psensor",
92 "title", _("About Psensor"),
94 "website", PACKAGE_URL,
95 "website-label", _("Psensor Homepage"),
99 static void cb_about(GtkMenuItem *mi, gpointer data)
101 ui_show_about_dialog();
104 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
106 ui_psensor_quit((struct ui_psensor *)data);
109 static void cb_preferences(GtkMenuItem *mi, gpointer data)
111 ui_pref_dialog_run((struct ui_psensor *)data);
114 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
116 struct ui_psensor *ui = data;
118 if (ui->sensors && *ui->sensors)
119 ui_sensorpref_dialog_run(*ui->sensors, ui);
122 void ui_psensor_quit(struct ui_psensor *ui)
126 log_printf(LOG_DEBUG, "Destroy main window");
127 gtk_widget_destroy(ui->main_window);
131 static const char *menu_desc =
133 " <menubar name='MainMenu'>"
134 " <menu name='Psensor' action='PsensorMenuAction'>"
135 " <menuitem name='Preferences' action='PreferencesAction' />"
136 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
138 " <menuitem name='Quit' action='QuitAction' />"
140 " <menu name='Help' action='HelpMenuAction'>"
141 " <menuitem name='About' action='AboutAction' />"
146 static GtkActionEntry entries[] = {
147 { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
149 { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */
150 N_("_Preferences"), NULL, /* label, accelerator */
151 N_("Preferences"), /* tooltip */
152 G_CALLBACK(cb_preferences) },
154 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
155 N_("_Sensor Preferences"), NULL,
156 N_("Sensor Preferences"),
157 G_CALLBACK(cb_sensor_preferences) },
160 GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"), G_CALLBACK(cb_menu_quit) },
162 { "HelpMenuAction", NULL, "_Help" },
164 { "AboutAction", GTK_STOCK_PREFERENCES,
167 G_CALLBACK(cb_about) }
169 static guint n_entries = G_N_ELEMENTS(entries);
171 static GtkWidget *get_menu(struct ui_psensor *ui)
173 GtkActionGroup *action_group;
174 GtkUIManager *menu_manager;
177 action_group = gtk_action_group_new("PsensorActions");
178 gtk_action_group_set_translation_domain(action_group, PACKAGE);
179 menu_manager = gtk_ui_manager_new();
181 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
182 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
185 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
188 g_error(_("building menus failed: %s"), error->message);
190 return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
193 static unsigned int enable_alpha_channel(GtkWidget *w)
195 GdkScreen *screen = gtk_widget_get_screen(w);
197 #if (GTK_CHECK_VERSION(3, 0, 0))
198 GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
201 gtk_widget_set_visual(w, visual);
205 GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
208 gtk_widget_set_colormap(w, colormap);
215 void ui_window_create(struct ui_psensor *ui)
217 GtkWidget *window, *menubar;
220 GtkIconTheme *icon_theme;
223 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
226 if (cfg->window_restore_enabled)
227 gtk_window_move(GTK_WINDOW(window),
231 gtk_window_set_default_size(GTK_WINDOW(window),
235 gtk_window_set_title(GTK_WINDOW(window),
236 _("Psensor - Temperature Monitor"));
237 gtk_window_set_role(GTK_WINDOW(window), "psensor");
239 screen = gtk_widget_get_screen(window);
241 if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
242 if (!enable_alpha_channel(window))
243 cfg->alpha_channel_enabled = 0;
245 cfg->alpha_channel_enabled = 0;
248 icon_theme = gtk_icon_theme_get_default();
249 icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
251 gtk_window_set_icon(GTK_WINDOW(window), icon);
253 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
255 g_signal_connect(window,
256 "delete_event", G_CALLBACK(on_delete_event_cb), ui);
258 gtk_window_set_decorated(GTK_WINDOW(window),
259 cfg->window_decoration_enabled);
261 gtk_window_set_keep_below(GTK_WINDOW(window),
262 cfg->window_keep_below_enabled);
265 menubar = get_menu(ui);
267 ui->main_box = gtk_vbox_new(FALSE, 1);
269 gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
272 gtk_container_add(GTK_CONTAINER(window), ui->main_box);
274 ui->main_window = window;
275 ui->menu_bar = menubar;
277 log_printf(LOG_DEBUG,
278 "is_status_supported: %d\n", is_status_supported());
280 if (cfg->hide_on_startup
281 && (is_appindicator_supported() || is_status_supported()))
282 gtk_widget_show_all(ui->main_box);
287 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
290 gtk_widget_show(ui->menu_bar);
292 gtk_widget_hide(ui->menu_bar);
295 void ui_window_update(struct ui_psensor *ui)
302 if (ui->sensor_box) {
303 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
305 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
306 ui->ui_sensorlist->widget);
308 gtk_container_remove(GTK_CONTAINER(ui->main_box),
311 ui->w_graph = ui_graph_create(ui);
316 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
317 || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
318 ui->sensor_box = gtk_hpaned_new();
320 ui->sensor_box = gtk_vpaned_new();
322 gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
324 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
325 || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
326 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
327 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
328 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
329 ui->ui_sensorlist->widget, FALSE, TRUE);
331 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
332 ui->ui_sensorlist->widget, FALSE, TRUE);
333 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
334 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
337 if (cfg->window_restore_enabled)
338 gtk_paned_set_position(GTK_PANED(ui->sensor_box),
339 ui->config->window_divider_pos);
342 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
344 gtk_widget_show_all(ui->sensor_box);
346 if (cfg->menu_bar_disabled)
347 menu_bar_show(0, ui);
349 menu_bar_show(1, ui);
352 void ui_window_show(struct ui_psensor *ui)
354 gtk_widget_show_all(ui->main_window);