added support of enabling/disabling menu
[psensor.git] / src / ui.c
1 /*
2     Copyright (C) 2010-2011 jeanfi@gmail.com
3
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.
8
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.
13
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
17     02110-1301 USA
18 */
19
20 #include "cfg.h"
21 #include "ui.h"
22 #include "ui_graph.h"
23 #include "ui_pref.h"
24 #include "ui_sensorlist.h"
25
26 static void on_destroy(GtkWidget *widget, gpointer data)
27 {
28         ui_psensor_quit();
29 }
30
31 static void cb_menu_quit(gpointer data,
32                          guint cb_action,
33                          GtkWidget *item)
34 {
35         ui_psensor_quit();
36 }
37
38 static void cb_menu_preferences(gpointer data,
39                                 guint cb_action,
40                                 GtkWidget *item)
41 {
42         ui_pref_dialog_run((struct ui_psensor *)data);
43 }
44
45 void ui_psensor_quit()
46 {
47         gtk_main_quit();
48 }
49
50 static GtkItemFactoryEntry menu_items[] = {
51         {"/Psensor", NULL, NULL, 0, "<Branch>"},
52         {"/Psensor/Preferences",
53          NULL, cb_menu_preferences, 0, "<Item>"},
54         {"/Psensor/sep1",
55          NULL, NULL, 0, "<Separator>"},
56         {"/Psensor/Quit",
57          "", cb_menu_quit, 0, "<StockItem>", GTK_STOCK_QUIT},
58 };
59
60 static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
61
62 static GtkWidget *get_menu(struct ui_psensor *ui)
63 {
64         GtkItemFactory *item_factory;
65
66         item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", NULL);
67
68         gtk_item_factory_create_items(item_factory,
69                                       nmenu_items, menu_items, ui);
70         return gtk_item_factory_get_widget(item_factory, "<main>");
71 }
72
73 void ui_window_create(struct ui_psensor *ui)
74 {
75         GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
76         GdkScreen *screen;
77         GdkColormap *colormap;
78         GdkPixbuf *icon;
79         GtkIconTheme *icon_theme;
80         GtkWidget *menubar;
81
82         gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
83
84         gtk_window_set_title(GTK_WINDOW(window),
85                              _("Psensor - Temperature Monitor"));
86         gtk_window_set_role(GTK_WINDOW(window), "psensor");
87
88         screen = gtk_widget_get_screen(window);
89
90         if (ui->config->alpha_channel_enabled
91             && gdk_screen_is_composited(screen)) {
92
93                 colormap = gdk_screen_get_rgba_colormap(screen);
94                 if (colormap)
95                         gtk_widget_set_colormap(window, colormap);
96                 else
97                         ui->config->alpha_channel_enabled = 0;
98         } else {
99                 ui->config->alpha_channel_enabled = 0;
100         }
101
102         icon_theme = gtk_icon_theme_get_default();
103         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
104         if (icon)
105                 gtk_window_set_icon(GTK_WINDOW(window), icon);
106         else
107                 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
108
109         g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), ui);
110
111         gtk_window_set_decorated(GTK_WINDOW(window),
112                                  ui->config->window_decoration_enabled);
113
114         gtk_window_set_keep_below(GTK_WINDOW(window),
115                                   ui->config->window_keep_below_enabled);
116
117         /* main box */
118         menubar = get_menu(ui);
119
120         ui->main_box = gtk_vbox_new(FALSE, 1);
121
122         gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
123                            FALSE, TRUE, 0);
124
125         gtk_container_add(GTK_CONTAINER(window), ui->main_box);
126
127         ui->main_window = window;
128         ui->menu_bar = menubar;
129
130         gtk_widget_show_all(ui->main_window);
131 }
132
133 static void menu_bar_show(unsigned int show, struct ui_psensor *ui)
134 {
135         if (show)
136                 gtk_widget_show(ui->menu_bar);
137         else
138                 gtk_widget_hide(ui->menu_bar);
139 }
140
141 void ui_window_update(struct ui_psensor *ui)
142 {
143         struct config *cfg;
144         GtkWidget *w_sensorlist;
145
146         cfg = ui->config;
147
148         if (ui->sensor_box) {
149                 ui_sensorlist_create_widget(ui->ui_sensorlist);
150
151                 gtk_container_remove(GTK_CONTAINER(ui->main_box),
152                                      ui->sensor_box);
153
154                 ui->w_graph = ui_graph_create(ui);
155                 ui->w_sensorlist = ui->ui_sensorlist->widget;
156         }
157
158         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
159             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
160                 ui->sensor_box = gtk_hpaned_new();
161         else
162                 ui->sensor_box = gtk_vpaned_new();
163
164         w_sensorlist = gtk_scrolled_window_new(NULL, NULL);
165         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w_sensorlist),
166                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
167         gtk_container_add(GTK_CONTAINER(w_sensorlist),
168                           ui->ui_sensorlist->widget);
169
170         gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
171
172         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
173             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
174                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
175                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
176                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
177                                 w_sensorlist, FALSE, TRUE);
178         } else {
179                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
180                                 w_sensorlist, FALSE, TRUE);
181                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
182                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
183         }
184
185         gtk_widget_show_all(ui->sensor_box);
186
187         if (cfg->menu_bar_disabled)
188                 menu_bar_show(0, ui);
189         else
190                 menu_bar_show(1, ui);
191 }