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)
144 GList *columns = gtk_tree_view_get_columns(view);
149 for (node = columns; node; node = node->next) {
150 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
153 x < (colx + gtk_tree_view_column_get_width(checkcol)))
156 colx += gtk_tree_view_column_get_width(checkcol);
164 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
166 struct cb_data *cb_data = data;
168 ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
171 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
172 struct psensor *sensor)
174 GtkWidget *menu, *item, *separator;
175 struct cb_data *data;
177 menu = gtk_menu_new();
179 item = gtk_menu_item_new_with_label(sensor->name);
180 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
182 separator = gtk_separator_menu_item_new();
183 gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
185 item = gtk_menu_item_new_with_label(_("Preferences"));
186 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
188 data = malloc(sizeof(struct cb_data));
190 data->sensor = sensor;
192 g_signal_connect(item,
194 G_CALLBACK(on_preferences_activated), data);
196 gtk_widget_show_all(menu);
201 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
204 struct ui_psensor *ui;
209 if (event->button != 3)
212 ui = (struct ui_psensor *)data;
213 view = ui->sensors_tree;
215 s = get_sensor_at_pos(view, event->x, event->y, ui);
218 coli = col_index_to_col(get_col_index_at_pos(view, event->x));
220 if (coli == COL_COLOR) {
221 if (ui_change_color(_("Select foreground color"),
223 ui_sensorlist_update(ui, 1);
224 config_set_sensor_color(s->id, s->color);
226 } else if (coli >= 0 && coli != COL_ENABLED) {
227 menu = create_sensor_popup(ui, s);
229 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
230 event->button, event->time);
238 toggled_cbk(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
240 struct ui_psensor *ui;
246 ui = (struct ui_psensor *)data;
247 model = gtk_tree_view_get_model(ui->sensors_tree);
248 path = gtk_tree_path_new_from_string(path_str);
250 gtk_tree_model_get_iter(model, &iter, path);
252 gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
255 config_set_sensor_enabled(s->id, s->enabled);
257 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
258 COL_ENABLED, s->enabled, -1);
260 gtk_tree_path_free(path);
263 static int cmp_sensors(const void *p1, const void *p2)
265 const struct psensor *s1, *s2;
271 pos1 = config_get_sensor_position(s1->id);
272 pos2 = config_get_sensor_position(s2->id);
277 void ui_sensorlist_create(struct ui_psensor *ui)
280 GtkCellRenderer *renderer;
281 struct psensor **s_cur;
283 struct psensor **ordered_sensors;
285 log_debug("ui_sensorlist_create()");
287 renderer = gtk_cell_renderer_text_new();
288 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
292 "text", COL_NAME, NULL);
294 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
298 "text", COL_TEMP, NULL);
300 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
304 "text", COL_TEMP_MIN, NULL);
306 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
310 "text", COL_TEMP_MAX, NULL);
312 renderer = gtk_cell_renderer_text_new();
313 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
318 "background", COL_COLOR_STR,
321 g_signal_connect(ui->sensors_tree,
322 "button-press-event", (GCallback) on_clicked, ui);
324 renderer = gtk_cell_renderer_toggle_new();
325 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
329 "active", COL_ENABLED,
331 g_signal_connect(G_OBJECT(renderer),
332 "toggled", (GCallback) toggled_cbk, ui);
334 renderer = gtk_cell_renderer_text_new();
335 gtk_tree_view_insert_column_with_attributes(ui->sensors_tree,
339 "text", COL_EMPTY, NULL);
341 ordered_sensors = psensor_list_copy(ui->sensors);
342 qsort(ordered_sensors,
343 psensor_list_size(ordered_sensors),
344 sizeof(struct psensor *),
347 store = ui->sensors_store;
348 for (s_cur = ordered_sensors; *s_cur; s_cur++) {
349 gtk_list_store_append(store, &iter);
350 gtk_list_store_set(store, &iter, COL_SENSOR, *s_cur, -1);
352 free(ordered_sensors);
354 ui_sensorlist_update(ui, 1);