removed useless const
[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 = ui->ui_sensorlist->treeview;
204
205         if (event->button != 3)
206                 return FALSE;
207
208         struct psensor *sensor = get_sensor_at_pos(view,
209                                                    event->x,
210                                                    event->y,
211                                                    ui->sensors);
212
213         if (sensor) {
214                 int coli = col_index_to_col(get_col_index_at_pos(view,
215                                                                  event->x));
216
217                 if (coli == COL_COLOR) {
218                         if (ui_change_color(_("Select foreground color"),
219                                             sensor->color)) {
220                                 ui_sensorlist_update(ui, 1);
221                                 config_set_sensor_color(sensor->id,
222                                                         sensor->color);
223                         }
224                 } else if (coli >= 0 && coli != COL_ENABLED) {
225                         menu = create_sensor_popup(ui, sensor);
226
227                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
228                                        event->button, event->time);
229                 }
230
231         }
232         return TRUE;
233 }
234
235 static void
236 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
237 {
238         struct ui_sensorlist *list = (struct ui_sensorlist *)data;
239         GtkTreeModel *model
240             = gtk_tree_view_get_model(list->treeview);
241         GtkTreeIter iter;
242         GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
243         gboolean fixed;
244         gint *i;
245
246         gtk_tree_model_get_iter(model, &iter, path);
247         gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
248
249         fixed ^= 1;
250
251         i = gtk_tree_path_get_indices(path);
252         if (i) {
253                 int n = *i;
254                 struct psensor **sensor = list->sensors;
255                 while (n--)
256                         sensor++;
257                 (*sensor)->enabled = fixed;
258                 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
259         }
260
261         gtk_list_store_set(GTK_LIST_STORE(model),
262                            &iter, COL_ENABLED, fixed, -1);
263
264         gtk_tree_path_free(path);
265 }
266
267 static void create_widget(struct ui_psensor *ui)
268 {
269         GtkListStore *store;
270         GtkCellRenderer *renderer;
271         struct psensor **s_cur = ui->sensors;
272         struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
273
274         store = ui->sensors_store;
275
276         ui_sl->treeview = ui->sensors_tree;
277
278         renderer = gtk_cell_renderer_text_new();
279         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
280                                                     -1,
281                                                     _("Sensor"),
282                                                     renderer,
283                                                     "text", COL_NAME, NULL);
284
285         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
286                                                     -1,
287                                                     _("Value"),
288                                                     renderer,
289                                                     "text", COL_TEMP, NULL);
290
291         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
292                                                     -1,
293                                                     _("Min"),
294                                                     renderer,
295                                                     "text", COL_TEMP_MIN, NULL);
296
297         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
298                                                     -1,
299                                                     _("Max"),
300                                                     renderer,
301                                                     "text", COL_TEMP_MAX, NULL);
302
303         renderer = gtk_cell_renderer_text_new();
304         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
305                                                     -1,
306                                                     _("Color"),
307                                                     renderer,
308                                                     "text", COL_COLOR,
309                                                     "background", COL_COLOR_STR,
310                                                     NULL);
311
312         g_signal_connect(ui_sl->treeview,
313                          "button-press-event", (GCallback) on_clicked, ui);
314
315         renderer = gtk_cell_renderer_toggle_new();
316         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
317                                                     -1,
318                                                     _("Graph"),
319                                                     renderer,
320                                                     "active", COL_ENABLED,
321                                                     NULL);
322         g_signal_connect(G_OBJECT(renderer),
323                          "toggled", (GCallback) on_toggled, ui_sl);
324
325         renderer = gtk_cell_renderer_text_new();
326         gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
327                                                     -1,
328                                                     "",
329                                                     renderer,
330                                                     "text", COL_EMPTY, NULL);
331
332         while (*s_cur) {
333                 GtkTreeIter iter;
334                 GdkColor color;
335                 gchar *scolor;
336                 struct psensor *s = *s_cur;
337
338                 color.red = s->color->red;
339                 color.green = s->color->green;
340                 color.blue = s->color->blue;
341
342                 scolor = gdk_color_to_string(&color);
343
344                 gtk_list_store_append(store, &iter);
345                 gtk_list_store_set(store, &iter,
346                                    COL_NAME, s->name,
347                                    COL_TEMP, _("N/A"),
348                                    COL_TEMP_MIN, _("N/A"),
349                                    COL_TEMP_MAX, _("N/A"),
350                                    COL_COLOR_STR, scolor,
351                                    COL_ENABLED, s->enabled,
352                                    COL_SENSOR, s, -1);
353
354                 free(scolor);
355
356                 s_cur++;
357         }
358 }
359
360 void ui_sensorlist_create(struct ui_psensor *ui)
361 {
362         log_debug("ui_sensorlist_create()");
363         ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
364         ui->ui_sensorlist->sensors = ui->sensors;
365
366         create_widget(ui);
367 }