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