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"
29 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
31 struct ui_psensor *ui = data;
33 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
34 if (is_appindicator_supported()) {
35 log_puts(LOG_DEBUG, "hiding, WM is supporting appindicator");
36 gtk_widget_hide(ui->main_window);
38 log_puts(LOG_DEBUG, "quitting, WM not supporting appindicator");
48 void ui_show_about_dialog()
50 gtk_show_about_dialog(NULL,
52 _("Psensor is a GTK+ application for monitoring "
55 _("Copyright(c) 2010-2011\njeanfi@gmail.com"),
56 "logo-icon-name", "psensor",
57 "program-name", "Psensor",
58 "title", _("About Psensor"),
60 "website", PACKAGE_URL,
61 "website-label", _("Psensor Homepage"),
65 static void cb_about(GtkMenuItem *mi, gpointer data)
67 ui_show_about_dialog();
70 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
72 ui_psensor_quit((struct ui_psensor *)data);
75 static void cb_preferences(GtkMenuItem *mi, gpointer data)
77 ui_pref_dialog_run((struct ui_psensor *)data);
80 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
82 struct ui_psensor *ui = data;
84 if (ui->sensors && *ui->sensors)
85 ui_sensorpref_dialog_run(*ui->sensors, ui);
88 void ui_psensor_quit(struct ui_psensor *ui)
90 gtk_widget_destroy(ui->main_window);
94 static const char *menu_desc =
96 " <menubar name='MainMenu'>"
97 " <menu name='Psensor' action='PsensorMenuAction'>"
98 " <menuitem name='Preferences' action='PreferencesAction' />"
99 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
101 " <menuitem name='Quit' action='QuitAction' />"
103 " <menu name='Help' action='HelpMenuAction'>"
104 " <menuitem name='About' action='AboutAction' />"
109 static GtkActionEntry entries[] = {
110 { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
112 { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */
113 N_("_Preferences"), NULL, /* label, accelerator */
114 N_("Preferences"), /* tooltip */
115 G_CALLBACK(cb_preferences) },
117 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
118 N_("_Sensor Preferences"), NULL,
119 N_("Sensor Preferences"),
120 G_CALLBACK(cb_sensor_preferences) },
123 GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"), G_CALLBACK(cb_menu_quit) },
125 { "HelpMenuAction", NULL, "_Help" },
127 { "AboutAction", GTK_STOCK_PREFERENCES,
130 G_CALLBACK(cb_about) }
132 static guint n_entries = G_N_ELEMENTS(entries);
134 static GtkWidget *get_menu(struct ui_psensor *ui)
136 GtkActionGroup *action_group;
137 GtkUIManager *menu_manager;
140 action_group = gtk_action_group_new("PsensorActions");
141 gtk_action_group_set_translation_domain(action_group, PACKAGE);
142 menu_manager = gtk_ui_manager_new();
144 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
145 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
148 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
151 g_error(_("building menus failed: %s"), error->message);
153 return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
156 static unsigned int enable_alpha_channel(GtkWidget *w)
158 GdkScreen *screen = gtk_widget_get_screen(w);
160 #if (GTK_CHECK_VERSION(3, 0, 0))
161 GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
164 gtk_widget_set_visual(w, visual);
168 GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
171 gtk_widget_set_colormap(w, colormap);
178 void ui_window_create(struct ui_psensor *ui)
180 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
183 GtkIconTheme *icon_theme;
186 gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
188 gtk_window_set_title(GTK_WINDOW(window),
189 _("Psensor - Temperature Monitor"));
190 gtk_window_set_role(GTK_WINDOW(window), "psensor");
192 screen = gtk_widget_get_screen(window);
194 if (ui->config->alpha_channel_enabled
195 && gdk_screen_is_composited(screen)) {
196 if (!enable_alpha_channel(window))
197 ui->config->alpha_channel_enabled = 0;
199 ui->config->alpha_channel_enabled = 0;
202 icon_theme = gtk_icon_theme_get_default();
203 icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
205 gtk_window_set_icon(GTK_WINDOW(window), icon);
207 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
209 g_signal_connect(window,
210 "delete_event", G_CALLBACK(on_delete_event_cb), ui);
212 gtk_window_set_decorated(GTK_WINDOW(window),
213 ui->config->window_decoration_enabled);
215 gtk_window_set_keep_below(GTK_WINDOW(window),
216 ui->config->window_keep_below_enabled);
219 menubar = get_menu(ui);
221 ui->main_box = gtk_vbox_new(FALSE, 1);
223 gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
226 gtk_container_add(GTK_CONTAINER(window), ui->main_box);
228 ui->main_window = window;
229 ui->menu_bar = menubar;
231 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
232 if (ui->config->hide_on_startup)
233 gtk_widget_show_all(ui->main_box);
235 gtk_widget_show_all(ui->main_window);
237 gtk_widget_show_all(ui->main_window);
242 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
245 gtk_widget_show(ui->menu_bar);
247 gtk_widget_hide(ui->menu_bar);
250 void ui_window_update(struct ui_psensor *ui)
257 if (ui->sensor_box) {
258 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
260 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
261 ui->ui_sensorlist->widget);
263 gtk_container_remove(GTK_CONTAINER(ui->main_box),
266 ui->w_graph = ui_graph_create(ui);
271 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
272 || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
273 ui->sensor_box = gtk_hpaned_new();
275 ui->sensor_box = gtk_vpaned_new();
277 gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
279 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
280 || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
281 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
282 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
283 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
284 ui->ui_sensorlist->widget, FALSE, TRUE);
286 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
287 ui->ui_sensorlist->widget, FALSE, TRUE);
288 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
289 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
293 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
295 gtk_widget_show_all(ui->sensor_box);
297 if (cfg->menu_bar_disabled)
298 menu_bar_show(0, ui);
300 menu_bar_show(1, ui);