2 * Copyright (C) 2010-2013 jeanfi@gmail.com
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.
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.
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
24 #include "ui_sensorlist.h"
25 #include "ui_sensorpref.h"
42 struct ui_psensor *ui;
43 struct psensor *sensor;
46 static int col_index_to_col(int idx)
56 void ui_sensorlist_update(struct ui_psensor *ui, bool complete)
58 char *scolor, *value, *min, *max;
67 model = gtk_tree_view_get_model(ui->sensors_tree);
68 store = GTK_LIST_STORE(model);
70 use_celcius = ui->config->temperature_unit == CELCIUS;
72 valid = gtk_tree_model_get_iter_first(model, &iter);
74 gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
76 value = psensor_value_to_str(s->type,
77 psensor_get_current_value(s),
79 min = psensor_value_to_str(s->type, s->min, use_celcius);
80 max = psensor_value_to_str(s->type, s->max, use_celcius);
82 gtk_list_store_set(store, &iter,
92 color.red = s->color->red;
93 color.green = s->color->green;
94 color.blue = s->color->blue;
96 scolor = gdk_color_to_string(&color);
98 gtk_list_store_set(store, &iter,
100 COL_COLOR_STR, scolor,
101 COL_ENABLED, s->enabled,
106 valid = gtk_tree_model_iter_next(model, &iter);
111 * Returns the sensor corresponding to the x/y position
116 static struct psensor *
117 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct ui_psensor *ui)
124 gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
125 model = gtk_tree_view_get_model(ui->sensors_tree);
128 if (gtk_tree_model_get_iter(model, &iter, path)) {
129 gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
137 * Returns the index of the column corresponding
138 * to the x position in the table.
142 static int get_col_index_at_pos(GtkTreeView *view, int x)
146 GtkTreeViewColumn *checkcol;
148 cols = gtk_tree_view_get_columns(view);
151 for (node = cols; node; node = node->next) {
152 checkcol = (GtkTreeViewColumn *)node->data;
155 && x < (colx + gtk_tree_view_column_get_width(checkcol)))
158 colx += gtk_tree_view_column_get_width(checkcol);
166 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
168 struct cb_data *cb_data = data;
170 ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
173 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
174 struct psensor *sensor)
176 GtkWidget *menu, *item, *separator;
177 struct cb_data *data;
179 menu = gtk_menu_new();
181 item = gtk_menu_item_new_with_label(sensor->name);
182 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
184 separator = gtk_separator_menu_item_new();
185 gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
187 item = gtk_menu_item_new_with_label(_("Preferences"));
188 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
190 data = malloc(sizeof(struct cb_data));
192 data->sensor = sensor;
194 g_signal_connect(item,
196 G_CALLBACK(on_preferences_activated), data);
198 gtk_widget_show_all(menu);
203 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
206 struct ui_psensor *ui;
211 if (event->button != 3)
214 ui = (struct ui_psensor *)data;
215 view = ui->sensors_tree;
217 s = get_sensor_at_pos(view, event->x, event->y, ui);
220 coli = col_index_to_col(get_col_index_at_pos(view, event->x));
222 if (coli == COL_COLOR) {
223 if (ui_change_color(_("Select foreground color"),
225 ui_sensorlist_update(ui, 1);
226 config_set_sensor_color(s->id, s->color);
228 } else if (coli >= 0 && coli != COL_ENABLED) {
229 menu = create_sensor_popup(ui, s);
231 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
232 event->button, event->time);
240 toggled_cbk(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
242 struct ui_psensor *ui;
248 ui = (struct ui_psensor *)data;
249 model = gtk_tree_view_get_model(ui->sensors_tree);
250 path = gtk_tree_path_new_from_string(path_str);
252 gtk_tree_model_get_iter(model, &iter, path);
254 gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
257 config_set_sensor_enabled(s->id, s->enabled);
259 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
260 COL_ENABLED, s->enabled, -1);
262 gtk_tree_path_free(path);
265 static int cmp_sensors(const void *p1, const void *p2)
267 const struct psensor *s1, *s2;
273 pos1 = config_get_sensor_position(s1->id);
274 pos2 = config_get_sensor_position(s2->id);
279 void ui_sensorlist_create(struct ui_psensor *ui)
282 GtkCellRenderer *renderer;
283 struct psensor **s_cur;
285 struct psensor **ordered_sensors;
287 log_debug("ui_sensorlist_create()");
289 renderer = gtk_cell_renderer_text_new();
290 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
294 "text", COL_NAME, NULL);
296 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
300 "text", COL_TEMP, NULL);
302 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
306 "text", COL_TEMP_MIN, NULL);
308 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
312 "text", COL_TEMP_MAX, NULL);
314 renderer = gtk_cell_renderer_text_new();
315 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
320 "background", COL_COLOR_STR,
323 g_signal_connect(ui->sensors_tree,
324 "button-press-event", (GCallback) on_clicked, ui);
326 renderer = gtk_cell_renderer_toggle_new();
327 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
331 "active", COL_ENABLED,
333 g_signal_connect(G_OBJECT(renderer),
334 "toggled", (GCallback) toggled_cbk, ui);
336 renderer = gtk_cell_renderer_text_new();
337 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
341 "text", COL_EMPTY, NULL);
343 ordered_sensors = psensor_list_copy(ui->sensors);
344 qsort(ordered_sensors,
345 psensor_list_size(ordered_sensors),
346 sizeof(struct psensor *),
349 store = ui->sensors_store;
350 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
351 gtk_list_store_append(store, &iter);
352 gtk_list_store_set(store, &iter, COL_SENSOR, *s_cur, -1);
354 free(ordered_sensors);
356 ui_sensorlist_update(ui, 1);