code cleanup
[psensor.git] / src / ui_sensorlist.c
1 /*
2  * Copyright (C) 2010-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * 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 #include <stdlib.h>
20 #include <string.h>
21
22 #include "ui.h"
23 #include "ui_pref.h"
24 #include "ui_sensorlist.h"
25 #include "ui_sensorpref.h"
26 #include "cfg.h"
27 #include "ui_color.h"
28
29 enum {
30         COL_NAME = 0,
31         COL_TEMP,
32         COL_TEMP_MIN,
33         COL_TEMP_MAX,
34         COL_COLOR,
35         COL_COLOR_STR,
36         COL_ENABLED,
37         COL_EMPTY,
38         COL_SENSOR
39 };
40
41 struct cb_data {
42         struct ui_psensor *ui;
43         struct psensor *sensor;
44 };
45
46 static int col_index_to_col(int idx)
47 {
48         if (idx == 5)
49                 return COL_ENABLED;
50         else if (idx > 5)
51                 return -1;
52
53         return idx;
54 }
55
56 void ui_sensorlist_update(struct ui_psensor *ui, bool complete)
57 {
58         char *scolor, *value, *min, *max;
59         struct psensor *s;
60         GtkTreeIter iter;
61         GtkTreeModel *model;
62         gboolean valid;
63         int use_celcius;
64         GdkColor color;
65         GtkListStore *store;
66
67         model = gtk_tree_view_get_model(ui->sensors_tree);
68         store = GTK_LIST_STORE(model);
69
70         use_celcius = ui->config->temperature_unit == CELCIUS;
71
72         valid = gtk_tree_model_get_iter_first(model, &iter);
73         while (valid) {
74                 gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
75
76                 value = psensor_value_to_str(s->type,
77                                              psensor_get_current_value(s),
78                                              use_celcius);
79                 min = psensor_value_to_str(s->type, s->min, use_celcius);
80                 max = psensor_value_to_str(s->type, s->max, use_celcius);
81
82                 gtk_list_store_set(store, &iter,
83                                    COL_TEMP, value,
84                                    COL_TEMP_MIN, min,
85                                    COL_TEMP_MAX, max,
86                                    -1);
87                 free(value);
88                 free(min);
89                 free(max);
90
91                 if (complete) {
92                         color.red = s->color->red;
93                         color.green = s->color->green;
94                         color.blue = s->color->blue;
95
96                         scolor = gdk_color_to_string(&color);
97
98                         gtk_list_store_set(store, &iter,
99                                            COL_NAME, s->name,
100                                            COL_COLOR_STR, scolor,
101                                            COL_ENABLED, s->enabled,
102                                            -1);
103                         free(scolor);
104                 }
105
106                 valid = gtk_tree_model_iter_next(model, &iter);
107         }
108 }
109
110 /*
111  * Returns the sensor corresponding to the x/y position
112  * in the table.
113  *
114  * <null> if none.
115  */
116 static struct psensor *
117 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
118 {
119         GtkTreePath *path;
120
121         gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
122
123         if (path) {
124                 gint *i = gtk_tree_path_get_indices(path);
125                 if (i)
126                         return *(sensors + *i);
127         }
128         return NULL;
129 }
130
131 /*
132  * Returns the index of the column corresponding
133  * to the x position in the table.
134  *
135  * -1 if none
136  */
137 static int get_col_index_at_pos(GtkTreeView *view, int x)
138 {
139         GList *columns = gtk_tree_view_get_columns(view);
140         GList *node;
141         int colx = 0;
142         int coli = 0;
143
144         for (node = columns; node; node = node->next) {
145                 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
146
147                 if (x >= colx &&
148                     x < (colx + gtk_tree_view_column_get_width(checkcol)))
149                         return coli;
150                 else
151                         colx += gtk_tree_view_column_get_width(checkcol);
152
153                 coli++;
154         }
155
156         return -1;
157 }
158
159 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
160 {
161         struct cb_data *cb_data = data;
162
163         ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
164 }
165
166 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
167                                       struct psensor *sensor)
168 {
169         GtkWidget *menu;
170         GtkWidget *item;
171         GtkWidget *separator;
172         struct cb_data *data;
173
174         menu = gtk_menu_new();
175
176         item = gtk_menu_item_new_with_label(sensor->name);
177         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
178
179         separator = gtk_separator_menu_item_new();
180         gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
181
182         item = gtk_menu_item_new_with_label(_("Preferences"));
183         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
184
185         data = malloc(sizeof(struct cb_data));
186         data->ui = ui;
187         data->sensor = sensor;
188
189         g_signal_connect(item,
190                          "activate",
191                          G_CALLBACK(on_preferences_activated), data);
192
193         gtk_widget_show_all(menu);
194
195         return menu;
196 }
197
198 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
199 {
200         GtkWidget *menu;
201         struct ui_psensor *ui = (struct ui_psensor *)data;
202         GtkTreeView *view;
203
204         if (event->button != 3)
205                 return FALSE;
206
207         view = ui->sensors_tree;
208
209         struct psensor *sensor = get_sensor_at_pos(view,
210                                                    event->x,
211                                                    event->y,
212                                                    ui->sensors);
213
214         if (sensor) {
215                 int coli = col_index_to_col(get_col_index_at_pos(view,
216                                                                  event->x));
217
218                 if (coli == COL_COLOR) {
219                         if (ui_change_color(_("Select foreground color"),
220                                             sensor->color)) {
221                                 ui_sensorlist_update(ui, 1);
222                                 config_set_sensor_color(sensor->id,
223                                                         sensor->color);
224                         }
225                 } else if (coli >= 0 && coli != COL_ENABLED) {
226                         menu = create_sensor_popup(ui, sensor);
227
228                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
229                                        event->button, event->time);
230                 }
231
232         }
233         return TRUE;
234 }
235
236 static void
237 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
238 {
239         struct ui_psensor *ui = (struct ui_psensor *)data;
240         GtkTreeModel *model
241             = gtk_tree_view_get_model(ui->sensors_tree);
242         GtkTreeIter iter;
243         GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
244         gboolean fixed;
245         gint *i;
246
247         gtk_tree_model_get_iter(model, &iter, path);
248         gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
249
250         fixed ^= 1;
251
252         i = gtk_tree_path_get_indices(path);
253         if (i) {
254                 int n = *i;
255                 struct psensor **sensor = ui->sensors;
256                 while (n--)
257                         sensor++;
258                 (*sensor)->enabled = fixed;
259                 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
260         }
261
262         gtk_list_store_set(GTK_LIST_STORE(model),
263                            &iter, COL_ENABLED, fixed, -1);
264
265         gtk_tree_path_free(path);
266 }
267
268 static void create_widget(struct ui_psensor *ui)
269 {
270         GtkListStore *store;
271         GtkCellRenderer *renderer;
272         struct psensor **s_cur;
273         GtkTreeIter iter;
274
275         renderer = gtk_cell_renderer_text_new();
276         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
277                                                     -1,
278                                                     _("Sensor"),
279                                                     renderer,
280                                                     "text", COL_NAME, NULL);
281
282         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
283                                                     -1,
284                                                     _("Value"),
285                                                     renderer,
286                                                     "text", COL_TEMP, NULL);
287
288         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
289                                                     -1,
290                                                     _("Min"),
291                                                     renderer,
292                                                     "text", COL_TEMP_MIN, NULL);
293
294         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
295                                                     -1,
296                                                     _("Max"),
297                                                     renderer,
298                                                     "text", COL_TEMP_MAX, NULL);
299
300         renderer = gtk_cell_renderer_text_new();
301         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
302                                                     -1,
303                                                     _("Color"),
304                                                     renderer,
305                                                     "text", COL_COLOR,
306                                                     "background", COL_COLOR_STR,
307                                                     NULL);
308
309         g_signal_connect(ui->sensors_tree,
310                          "button-press-event", (GCallback) on_clicked, ui);
311
312         renderer = gtk_cell_renderer_toggle_new();
313         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
314                                                     -1,
315                                                     _("Graph"),
316                                                     renderer,
317                                                     "active", COL_ENABLED,
318                                                     NULL);
319         g_signal_connect(G_OBJECT(renderer),
320                          "toggled", (GCallback) on_toggled, ui);
321
322         renderer = gtk_cell_renderer_text_new();
323         gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
324                                                     -1,
325                                                     "",
326                                                     renderer,
327                                                     "text", COL_EMPTY, NULL);
328
329         store = ui->sensors_store;
330         for (s_cur = ui->sensors; *s_cur; s_cur++) {
331                 gtk_list_store_append(store, &iter);
332                 gtk_list_store_set(store, &iter, COL_SENSOR, *s_cur, -1);
333         }
334
335         ui_sensorlist_update(ui, 1);
336 }
337
338 void ui_sensorlist_create(struct ui_psensor *ui)
339 {
340         log_debug("ui_sensorlist_create()");
341         create_widget(ui);
342 }