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
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_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);
44 log_debug("Window position: %d %d",
48 gtk_window_get_size(win,
51 log_debug("Window size: %d %d", cfg->window_w, cfg->window_h);
53 cfg->window_divider_pos
54 = gtk_paned_get_position(GTK_PANED(ui->sensor_box));
61 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
63 struct ui_psensor *ui = data;
67 log_debug("is_status_supported: %d\n", is_status_supported());
69 if (is_appindicator_supported() || is_status_supported())
70 gtk_widget_hide(ui->main_window);
77 void ui_show_about_dialog()
82 _("Psensor is a GTK+ application for monitoring hardware "
85 _("Copyright(c) 2010-2012\njeanfi@gmail.com"),
86 "logo-icon-name", "psensor",
87 "program-name", "Psensor",
88 "title", _("About Psensor"),
90 "website", PACKAGE_URL,
91 "website-label", _("Psensor Homepage"),
95 static void cb_about(GtkMenuItem *mi, gpointer data)
97 ui_show_about_dialog();
100 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
102 ui_psensor_quit((struct ui_psensor *)data);
105 static void cb_preferences(GtkMenuItem *mi, gpointer data)
107 ui_pref_dialog_run((struct ui_psensor *)data);
110 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
112 struct ui_psensor *ui = data;
114 if (ui->sensors && *ui->sensors)
115 ui_sensorpref_dialog_run(*ui->sensors, ui);
118 void ui_psensor_quit(struct ui_psensor *ui)
122 log_debug("Destroy main window");
123 gtk_widget_destroy(ui->main_window);
127 static const char *menu_desc =
129 " <menubar name='MainMenu'>"
130 " <menu name='Psensor' action='PsensorMenuAction'>"
131 " <menuitem name='Preferences' action='PreferencesAction' />"
132 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
134 " <menuitem name='Quit' action='QuitAction' />"
136 " <menu name='Help' action='HelpMenuAction'>"
137 " <menuitem name='About' action='AboutAction' />"
142 static GtkActionEntry entries[] = {
143 { "PsensorMenuAction", NULL, "_Psensor" },
145 { "PreferencesAction", GTK_STOCK_PREFERENCES,
146 N_("_Preferences"), NULL,
148 G_CALLBACK(cb_preferences) },
150 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
151 N_("S_ensor Preferences"), NULL,
152 N_("Sensor Preferences"),
153 G_CALLBACK(cb_sensor_preferences) },
156 GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"),
157 G_CALLBACK(cb_menu_quit) },
159 { "HelpMenuAction", NULL, N_("_Help") },
161 { "AboutAction", GTK_STOCK_PREFERENCES,
164 G_CALLBACK(cb_about) }
166 static guint n_entries = G_N_ELEMENTS(entries);
168 static GtkWidget *get_menu(struct ui_psensor *ui)
170 GtkActionGroup *action_group;
171 GtkUIManager *menu_manager;
174 action_group = gtk_action_group_new("PsensorActions");
175 gtk_action_group_set_translation_domain(action_group, PACKAGE);
176 menu_manager = gtk_ui_manager_new();
178 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
179 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
182 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
185 g_error(_("building menus failed: %s"), error->message);
187 return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
190 void ui_enable_alpha_channel(struct ui_psensor *ui)
198 screen = gtk_widget_get_screen(ui->main_window);
200 log_debug("Config alpha channel enabled: %d",
201 cfg->alpha_channel_enabled);
202 if (cfg->alpha_channel_enabled && gdk_screen_is_composited(screen)) {
203 log_debug("Screen is composited");
204 visual = gdk_screen_get_rgba_visual(screen);
206 gtk_widget_set_visual(ui->main_window, visual);
208 cfg->alpha_channel_enabled = 0;
209 log_err("Enable alpha channel has failed");
212 cfg->alpha_channel_enabled = 0;
217 static void on_slog_enabled_cb(GConfClient *client,
222 struct psensor **sensors;
224 sensors = (struct psensor **)user_data;
226 log_debug("cbk_slog_enabled");
228 if (is_slog_enabled())
229 slog_init(NULL, sensors);
231 slog_close(NULL, sensors);
234 void ui_window_create(struct ui_psensor *ui)
236 GtkWidget *window, *menubar;
238 GtkIconTheme *icon_theme;
241 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
244 if (cfg->window_restore_enabled)
245 gtk_window_move(GTK_WINDOW(window),
249 config_slog_enabled_notify_add(on_slog_enabled_cb, ui->sensors);
251 gtk_window_set_default_size(GTK_WINDOW(window),
255 gtk_window_set_title(GTK_WINDOW(window),
256 _("Psensor - Temperature Monitor"));
257 gtk_window_set_role(GTK_WINDOW(window), "psensor");
259 icon_theme = gtk_icon_theme_get_default();
260 icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
262 gtk_window_set_icon(GTK_WINDOW(window), icon);
264 log_err(_("Failed to load Psensor icon."));
266 g_signal_connect(window,
267 "delete_event", G_CALLBACK(on_delete_event_cb), ui);
269 gtk_window_set_decorated(GTK_WINDOW(window),
270 cfg->window_decoration_enabled);
272 gtk_window_set_keep_below(GTK_WINDOW(window),
273 cfg->window_keep_below_enabled);
276 menubar = get_menu(ui);
278 ui->main_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 1);
279 gtk_box_set_homogeneous(GTK_BOX(ui->main_box), FALSE);
280 gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
283 gtk_container_add(GTK_CONTAINER(window), ui->main_box);
285 ui->main_window = window;
286 ui->menu_bar = menubar;
288 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_paned_new(GTK_ORIENTATION_HORIZONTAL);
324 ui->sensor_box = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
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);
346 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
348 gtk_widget_show_all(ui->sensor_box);
350 if (cfg->menu_bar_disabled)
351 menu_bar_show(0, ui);
353 menu_bar_show(1, ui);
356 void ui_window_show(struct ui_psensor *ui)
358 log_debug("ui_window_show()");
359 gtk_window_present(GTK_WINDOW(ui->main_window));