added menu callback
[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
46 void ui_psensor_quit()
47 {
48         gtk_main_quit();
49 }
50
51 static GtkItemFactoryEntry menu_items[] = {
52         {"/Psensor", NULL, NULL, 0, "<Branch>"},
53         {"/Psensor/Preferences",
54          NULL, cb_menu_preferences, 0, "<Item>"},
55         {"/Psensor/sep1",
56          NULL, NULL, 0, "<Separator>"},
57         {"/Psensor/Quit",
58          "", cb_menu_quit, 0, "<StockItem>", GTK_STOCK_QUIT},
59 };
60
61 static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
62
63 static GtkWidget *get_menu(struct ui_psensor *ui)
64 {
65         GtkItemFactory *item_factory;
66
67         item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", NULL);
68
69         gtk_item_factory_create_items(item_factory,
70                                       nmenu_items, menu_items, ui);
71         return gtk_item_factory_get_widget(item_factory, "<main>");
72 }
73
74 void ui_window_create(struct ui_psensor *ui)
75 {
76         GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
77         GdkScreen *screen;
78         GdkColormap *colormap;
79         GdkPixbuf *icon;
80         GtkIconTheme *icon_theme;
81         GtkWidget *menubar;
82
83         gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
84
85         gtk_window_set_title(GTK_WINDOW(window),
86                              _("Psensor - Temperature Monitor"));
87         gtk_window_set_role(GTK_WINDOW(window), "psensor");
88
89         screen = gtk_widget_get_screen(window);
90
91         if (ui->config->alpha_channel_enabled
92             && gdk_screen_is_composited(screen)) {
93
94                 colormap = gdk_screen_get_rgba_colormap(screen);
95                 if (colormap)
96                         gtk_widget_set_colormap(window, colormap);
97                 else
98                         ui->config->alpha_channel_enabled = 0;
99         } else {
100                 ui->config->alpha_channel_enabled = 0;
101         }
102
103         icon_theme = gtk_icon_theme_get_default();
104         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
105         if (icon)
106                 gtk_window_set_icon(GTK_WINDOW(window), icon);
107         else
108                 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
109
110         g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), ui);
111
112         gtk_window_set_decorated(GTK_WINDOW(window),
113                                  ui->config->window_decoration_enabled);
114
115         gtk_window_set_keep_below(GTK_WINDOW(window),
116                                   ui->config->window_keep_below_enabled);
117
118         /* main box */
119         menubar = get_menu(ui);
120
121         ui->main_box = gtk_vbox_new(FALSE, 1);
122
123         gtk_box_pack_start(GTK_BOX(ui->main_box), menubar,
124                            FALSE, TRUE, 0);
125
126         gtk_container_add(GTK_CONTAINER(window), ui->main_box);
127
128         ui->main_window = window;
129 }
130
131 void ui_sensor_box_create(struct ui_psensor *ui)
132 {
133         struct config *cfg;
134         GtkWidget *w_sensorlist;
135
136         cfg = ui->config;
137
138         if (ui->sensor_box) {
139                 ui_sensorlist_create_widget(ui->ui_sensorlist);
140
141                 gtk_container_remove(GTK_CONTAINER(ui->main_box),
142                                      ui->sensor_box);
143
144                 ui->w_graph = ui_graph_create(ui);
145                 ui->w_sensorlist = ui->ui_sensorlist->widget;
146         }
147
148         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
149             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
150                 ui->sensor_box = gtk_hpaned_new();
151         else
152                 ui->sensor_box = gtk_vpaned_new();
153
154         w_sensorlist = gtk_scrolled_window_new(NULL, NULL);
155         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w_sensorlist),
156                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
157         gtk_container_add(GTK_CONTAINER(w_sensorlist),
158                           ui->ui_sensorlist->widget);
159
160         gtk_box_pack_end(GTK_BOX(ui->main_box), ui->sensor_box, TRUE, TRUE, 2);
161
162         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
163             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
164                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
165                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
166                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
167                                 w_sensorlist, FALSE, TRUE);
168         } else {
169                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
170                                 w_sensorlist, FALSE, TRUE);
171                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
172                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
173         }
174
175         gtk_widget_show_all(ui->sensor_box);
176 }