use fct get_current_value
[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 #include "compat.h"
29
30 enum {
31         COL_NAME = 0,
32         COL_TEMP,
33         COL_TEMP_MIN,
34         COL_TEMP_MAX,
35         COL_COLOR,
36         COL_COLOR_STR,
37         COL_ENABLED,
38         COL_EMPTY,
39         COL_SENSOR,
40         COLS_COUNT
41 };
42
43 struct cb_data {
44         struct ui_psensor *ui;
45         struct psensor *sensor;
46 };
47
48 static int col_index_to_col(int idx)
49 {
50         if (idx == 5)
51                 return COL_ENABLED;
52         else if (idx > 5)
53                 return -1;
54
55         return idx;
56 }
57
58 void ui_sensorlist_update(struct ui_psensor *ui, bool complete)
59 {
60         char *scolor, *value, *min, *max;
61         struct psensor *s;
62         GtkTreeIter iter;
63         GtkTreeModel *model;
64         gboolean valid;
65         int use_celcius;
66         GdkColor color;
67         GtkListStore *store;
68
69         model = gtk_tree_view_get_model(ui->sensors_tree);
70         store = GTK_LIST_STORE(model);
71
72         use_celcius = ui->config->temperature_unit == CELCIUS;
73
74         valid = gtk_tree_model_get_iter_first(model, &iter);
75         while (valid) {
76                 gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
77
78                 value = psensor_value_to_str(s->type,
79                                              psensor_get_current_value(s),
80                                              use_celcius);
81                 min = psensor_value_to_str(s->type, s->min, use_celcius);
82                 max = psensor_value_to_str(s->type, s->max, use_celcius);
83
84                 gtk_list_store_set(store, &iter,
85                                    COL_TEMP, value,
86                                    COL_TEMP_MIN, min,
87                                    COL_TEMP_MAX, max,
88                                    -1);
89                 free(value);
90                 free(min);
91                 free(max);
92
93                 if (complete) {
94                         color.red = s->color->red;
95                         color.green = s->color->green;
96                         color.blue = s->color->blue;
97
98                         scolor = gdk_color_to_string(&color);
99
100                         gtk_list_store_set(store, &iter,
101                                            COL_NAME, s->name,
102                                            COL_COLOR_STR, scolor,
103                                            COL_ENABLED, s->enabled,
104                                            -1);
105                         free(scolor);
106                 }
107
108                 valid = gtk_tree_model_iter_next(model, &iter);
109         }
110 }
111
112 /*
113  * Returns the sensor corresponding to the x/y position
114  * in the table.
115  *
116  * <null> if none.
117  */
118 static struct psensor *
119 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
120 {
121         GtkTreePath *path;
122
123         gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
124
125         if (path) {
126                 gint *i = gtk_tree_path_get_indices(path);
127                 if (i)
128                         return *(sensors + *i);
129         }
130         return NULL;
131 }
132
133 /*
134  * Returns the index of the column corresponding
135  * to the x position in the table.
136  *
137  * -1 if none
138  */
139 static int get_col_index_at_pos(GtkTreeView *view, int x)
140 {
141         GList *columns = gtk_tree_view_get_columns(view);
142         GList *node;
143         int colx = 0;
144         int coli = 0;
145
146         for (node = columns; node; node = node->next) {
147                 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
148
149                 if (x >= colx &&
150                     x < (colx + gtk_tree_view_column_get_width(checkcol)))
151                         return coli;
152                 else
153                         colx += gtk_tree_view_column_get_width(checkcol);
154
155                 coli++;
156         }
157
158         return -1;
159 }
160
161 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
162 {
163         struct cb_data *cb_data = data;
164
165         ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
166 }
167
168 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
169                                       struct psensor *sensor)
170 {
171         GtkWidget *menu;
172         GtkWidget *item;
173         GtkWidget *separator;
174         struct cb_data *data;
175
176         menu = gtk_menu_new();
177
178         item = gtk_menu_item_new_with_label(sensor->name);
179         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
180
181         separator = gtk_separator_menu_item_new();
182         gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
183
184         item = gtk_menu_item_new_with_label(_("Preferences"));
185         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
186
187         data = malloc(sizeof(struct cb_data));
188         data->ui = ui;
189         data->sensor = sensor;
190
191         g_signal_connect(item,
192                          "activate",
193                          G_CALLBACK(on_preferences_activated), data);
194
195         gtk_widget_show_all(menu);
196
197         return menu;
198 }
199
200 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
201 {
202         GtkWidget *menu;
203         struct ui_psensor *ui = (struct ui_psensor *)data;
204         GtkTreeView *view = ui->ui_sensorlist->treeview;
205
206         if (event->button != 3)
207                 return FALSE;
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_sensorlist *list = (struct ui_sensorlist *)data;
240         GtkTreeModel *model
241             = gtk_tree_view_get_model(list->treeview);
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 = list->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 = ui->sensors;
273         struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
274
275         store = ui->sensors_store;
276
277         ui_sl->treeview = ui->sensors_tree;
278
279         renderer = gtk_cell_renderer_text_new();
280         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
281                                                     -1,
282                                                     _("Sensor"),
283                                                     renderer,
284                                                     "text", COL_NAME, NULL);
285
286         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
287                                                     -1,
288                                                     _("Value"),
289                                                     renderer,
290                                                     "text", COL_TEMP, NULL);
291
292         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
293                                                     -1,
294                                                     _("Min"),
295                                                     renderer,
296                                                     "text", COL_TEMP_MIN, NULL);
297
298         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
299                                                     -1,
300                                                     _("Max"),
301                                                     renderer,
302                                                     "text", COL_TEMP_MAX, NULL);
303
304         renderer = gtk_cell_renderer_text_new();
305         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
306                                                     -1,
307                                                     _("Color"),
308                                                     renderer,
309                                                     "text", COL_COLOR,
310                                                     "background", COL_COLOR_STR,
311                                                     NULL);
312
313         g_signal_connect(ui_sl->treeview,
314                          "button-press-event", (GCallback) on_clicked, ui);
315
316         renderer = gtk_cell_renderer_toggle_new();
317         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
318                                                     -1,
319                                                     _("Graph"),
320                                                     renderer,
321                                                     "active", COL_ENABLED,
322                                                     NULL);
323         g_signal_connect(G_OBJECT(renderer),
324                          "toggled", (GCallback) on_toggled, ui_sl);
325
326         renderer = gtk_cell_renderer_text_new();
327         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
328                                                     -1,
329                                                     "",
330                                                     renderer,
331                                                     "text", COL_EMPTY, NULL);
332
333         while (*s_cur) {
334                 GtkTreeIter iter;
335                 GdkColor color;
336                 gchar *scolor;
337                 struct psensor *s = *s_cur;
338
339                 color.red = s->color->red;
340                 color.green = s->color->green;
341                 color.blue = s->color->blue;
342
343                 scolor = gdk_color_to_string(&color);
344
345                 gtk_list_store_append(store, &iter);
346                 gtk_list_store_set(store, &iter,
347                                    COL_NAME, s->name,
348                                    COL_TEMP, _("N/A"),
349                                    COL_TEMP_MIN, _("N/A"),
350                                    COL_TEMP_MAX, _("N/A"),
351                                    COL_COLOR_STR, scolor,
352                                    COL_ENABLED, s->enabled,
353                                    COL_SENSOR, s, -1);
354
355                 free(scolor);
356
357                 s_cur++;
358         }
359 }
360
361 void ui_sensorlist_create(struct ui_psensor *ui)
362 {
363         log_debug("ui_sensorlist_create()");
364         ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
365         ui->ui_sensorlist->sensors = ui->sensors;
366
367         create_widget(ui);
368 }