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