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"
28 on_delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data)
31 #if defined(HAVE_APPINDICATOR) || defined(HAVE_APPINDICATOR_029)
32 gtk_widget_hide(((struct ui_psensor *)data)->main_window);
40 static void cb_menu_quit(GtkMenuItem *mi, gpointer data)
45 static void cb_preferences(GtkMenuItem *mi, gpointer data)
47 ui_pref_dialog_run((struct ui_psensor *)data);
50 static void cb_sensor_preferences(GtkMenuItem *mi, gpointer data)
52 struct ui_psensor *ui = data;
54 if (ui->sensors && *ui->sensors)
55 ui_sensorpref_dialog_run(*ui->sensors, ui);
58 void ui_psensor_quit()
63 static const char *menu_desc =
65 " <menubar name='MainMenu'>"
66 " <menu name='Psensor' action='PsensorMenuAction'>"
67 " <menuitem name='Preferences' action='PreferencesAction' />"
68 " <menuitem name='SensorPreferences' action='SensorPreferencesAction' />"
70 " <menuitem name='Quit' action='QuitAction' />"
75 static GtkActionEntry entries[] = {
76 { "PsensorMenuAction", NULL, "_Psensor" }, /* name, stock id, label */
78 { "PreferencesAction", GTK_STOCK_PREFERENCES, /* name, stock id */
79 N_("_Preferences"), NULL, /* label, accelerator */
80 N_("Preferences"), /* tooltip */
81 G_CALLBACK(cb_preferences) },
83 { "SensorPreferencesAction", GTK_STOCK_PREFERENCES,
84 N_("_Sensor Preferences"), NULL,
85 N_("Sensor Preferences"),
86 G_CALLBACK(cb_sensor_preferences) },
89 GTK_STOCK_QUIT, N_("_Quit"), NULL, N_("Quit"), G_CALLBACK(cb_menu_quit) }
91 static guint n_entries = G_N_ELEMENTS(entries);
93 static GtkWidget *get_menu(struct ui_psensor *ui)
95 GtkActionGroup *action_group;
96 GtkUIManager *menu_manager;
99 action_group = gtk_action_group_new("PsensorActions");
100 gtk_action_group_set_translation_domain(action_group, PACKAGE);
101 menu_manager = gtk_ui_manager_new();
103 gtk_action_group_add_actions(action_group, entries, n_entries, ui);
104 gtk_ui_manager_insert_action_group(menu_manager, action_group, 0);
107 gtk_ui_manager_add_ui_from_string(menu_manager, menu_desc, -1, &error);
110 g_error(_("building menus failed: %s"), error->message);
112 return gtk_ui_manager_get_widget(menu_manager, "/MainMenu");
115 static unsigned int enable_alpha_channel(GtkWidget *w)
117 GdkScreen *screen = gtk_widget_get_screen(w);
119 #if (GTK_CHECK_VERSION(3, 0, 0))
120 GdkVisual *visual = gdk_screen_get_rgba_visual(screen);
123 gtk_widget_set_visual(w, visual);
127 GdkColormap *colormap = gdk_screen_get_rgba_colormap(screen);
130 gtk_widget_set_colormap(w, colormap);
137 void ui_window_create(struct ui_psensor *ui)
139 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
142 GtkIconTheme *icon_theme;
145 gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
147 gtk_window_set_title(GTK_WINDOW(window),
148 _("Psensor - Temperature Monitor"));
149 gtk_window_set_role(GTK_WINDOW(window), "psensor");
151 screen = gtk_widget_get_screen(window);
153 if (ui->config->alpha_channel_enabled
154 && gdk_screen_is_composited(screen)) {
155 if (!enable_alpha_channel(window))
156 ui->config->alpha_channel_enabled = 0;
158 ui->config->alpha_channel_enabled = 0;
161 icon_theme = gtk_icon_theme_get_default();
162 icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
164 gtk_window_set_icon(GTK_WINDOW(window), icon);
166 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
168 g_signal_connect(window,
169 "delete_event", G_CALLBACK(on_delete_event_cb), ui);
171 gtk_window_set_decorated(GTK_WINDOW(window),
172 ui->config->window_decoration_enabled);
174 gtk_window_set_keep_below(GTK_WINDOW(window),
175 ui->config->window_keep_below_enabled);
178 menubar = get_menu(ui);
180 ui->main_box = gtk_vbox_new(FALSE, 1);
182 gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
185 gtk_container_add(GTK_CONTAINER(window), ui->main_box);
187 ui->main_window = window;
188 ui->menu_bar = menubar;
190 if (ui->config->hide_on_startup)
191 gtk_widget_show_all(ui->main_box);
193 gtk_widget_show_all(ui->main_window);
196 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
199 gtk_widget_show(ui->menu_bar);
201 gtk_widget_hide(ui->menu_bar);
204 void ui_window_update(struct ui_psensor *ui)
211 if (ui->sensor_box) {
212 g_object_ref(GTK_WIDGET(ui->ui_sensorlist->widget));
214 gtk_container_remove(GTK_CONTAINER(ui->sensor_box),
215 ui->ui_sensorlist->widget);
217 gtk_container_remove(GTK_CONTAINER(ui->main_box),
220 ui->w_graph = ui_graph_create(ui);
225 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
226 || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
227 ui->sensor_box = gtk_hpaned_new();
229 ui->sensor_box = gtk_vpaned_new();
231 gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
233 if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
234 || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
235 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
236 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
237 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
238 ui->ui_sensorlist->widget, FALSE, TRUE);
240 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
241 ui->ui_sensorlist->widget, FALSE, TRUE);
242 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
243 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
247 g_object_unref(GTK_WIDGET(ui->ui_sensorlist->widget));
249 gtk_widget_show_all(ui->sensor_box);
251 if (cfg->menu_bar_disabled)
252 menu_bar_show(0, ui);
254 menu_bar_show(1, ui);