renamed ui_main_box_create to ui_sensor_box_create
[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_sensorlist.h"
24
25 void on_destroy(GtkWidget *widget, gpointer data)
26 {
27         ui_psensor_exit();
28 }
29
30 void ui_psensor_exit()
31 {
32         gtk_main_quit();
33 }
34
35 GtkWidget *ui_window_create(struct ui_psensor * ui)
36 {
37         GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
38         GdkScreen *screen;
39         GdkColormap *colormap;
40         GdkPixbuf *icon;
41         GtkIconTheme *icon_theme;
42
43         gtk_window_set_default_size(GTK_WINDOW(window), 800, 200);
44
45         gtk_window_set_title(GTK_WINDOW(window),
46                              _("Psensor - Temperature Monitor"));
47         gtk_window_set_role(GTK_WINDOW(window), "psensor");
48
49         screen = gtk_widget_get_screen(window);
50
51         if (ui->config->alpha_channel_enabled
52             && gdk_screen_is_composited(screen)) {
53
54                 colormap = gdk_screen_get_rgba_colormap(screen);
55                 if (colormap)
56                         gtk_widget_set_colormap(window, colormap);
57                 else
58                         ui->config->alpha_channel_enabled = 0;
59         } else {
60                 ui->config->alpha_channel_enabled = 0;
61         }
62
63         icon_theme = gtk_icon_theme_get_default();
64         icon = gtk_icon_theme_load_icon(icon_theme, "psensor", 48, 0, NULL);
65         if (icon)
66                 gtk_window_set_icon(GTK_WINDOW(window), icon);
67         else
68                 fprintf(stderr, _("ERROR: Failed to load psensor icon.\n"));
69
70         g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), ui);
71
72         gtk_window_set_decorated(GTK_WINDOW(window),
73                                  ui->config->window_decoration_enabled);
74
75         gtk_window_set_keep_below(GTK_WINDOW(window),
76                                   ui->config->window_keep_below_enabled);
77
78         return window;
79 }
80
81 void ui_sensor_box_create(struct ui_psensor *ui)
82 {
83         struct config *cfg;
84         GtkWidget *w_sensorlist;
85
86         cfg = ui->config;
87
88         if (ui->sensor_box) {
89                 ui_sensorlist_create_widget(ui->ui_sensorlist);
90
91                 gtk_container_remove(GTK_CONTAINER(ui->main_window),
92                                      ui->sensor_box);
93
94                 ui->w_graph = ui_graph_create(ui);
95                 ui->w_sensorlist = ui->ui_sensorlist->widget;
96         }
97
98         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
99             || cfg->sensorlist_position == SENSORLIST_POSITION_LEFT)
100                 ui->sensor_box = gtk_hpaned_new();
101         else
102                 ui->sensor_box = gtk_vpaned_new();
103
104         w_sensorlist = gtk_scrolled_window_new(NULL, NULL);
105         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w_sensorlist),
106                                        GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
107         gtk_container_add(GTK_CONTAINER(w_sensorlist),
108                           ui->ui_sensorlist->widget);
109
110         gtk_container_add(GTK_CONTAINER(ui->main_window), ui->sensor_box);
111
112         if (cfg->sensorlist_position == SENSORLIST_POSITION_RIGHT
113             || cfg->sensorlist_position == SENSORLIST_POSITION_BOTTOM) {
114                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
115                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
116                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
117                                 w_sensorlist, FALSE, TRUE);
118         } else {
119                 gtk_paned_pack1(GTK_PANED(ui->sensor_box),
120                                 w_sensorlist, FALSE, TRUE);
121                 gtk_paned_pack2(GTK_PANED(ui->sensor_box),
122                                 GTK_WIDGET(ui->w_graph), TRUE, TRUE);
123         }
124
125         gtk_widget_show_all(ui->sensor_box);
126 }