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