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