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"
43 struct ui_psensor *ui;
44 struct psensor *sensor;
47 static int col_index_to_col(int idx)
57 void ui_sensorlist_update(struct ui_psensor *ui, bool complete)
59 char *scolor, *value, *min, *max;
68 model = gtk_tree_view_get_model(ui->sensors_tree);
69 store = GTK_LIST_STORE(model);
71 use_celcius = ui->config->temperature_unit == CELCIUS;
73 valid = gtk_tree_model_get_iter_first(model, &iter);
75 gtk_tree_model_get(model, &iter, COL_SENSOR, &s, -1);
77 value = psensor_value_to_str(s->type,
78 psensor_get_current_value(s),
80 min = psensor_value_to_str(s->type, s->min, use_celcius);
81 max = psensor_value_to_str(s->type, s->max, use_celcius);
83 gtk_list_store_set(store, &iter,
93 color.red = s->color->red;
94 color.green = s->color->green;
95 color.blue = s->color->blue;
97 scolor = gdk_color_to_string(&color);
99 gtk_list_store_set(store, &iter,
101 COL_COLOR_STR, scolor,
102 COL_ENABLED, s->enabled,
107 valid = gtk_tree_model_iter_next(model, &iter);
112 * Returns the sensor corresponding to the x/y position
117 static struct psensor *
118 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
122 gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
125 gint *i = gtk_tree_path_get_indices(path);
127 return *(sensors + *i);
133 * Returns the index of the column corresponding
134 * to the x position in the table.
138 static int get_col_index_at_pos(GtkTreeView *view, int x)
140 GList *columns = gtk_tree_view_get_columns(view);
145 for (node = columns; node; node = node->next) {
146 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
149 x < (colx + gtk_tree_view_column_get_width(checkcol)))
152 colx += gtk_tree_view_column_get_width(checkcol);
160 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
162 struct cb_data *cb_data = data;
164 ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
167 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
168 struct psensor *sensor)
172 GtkWidget *separator;
173 struct cb_data *data;
175 menu = gtk_menu_new();
177 item = gtk_menu_item_new_with_label(sensor->name);
178 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
180 separator = gtk_separator_menu_item_new();
181 gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
183 item = gtk_menu_item_new_with_label(_("Preferences"));
184 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
186 data = malloc(sizeof(struct cb_data));
188 data->sensor = sensor;
190 g_signal_connect(item,
192 G_CALLBACK(on_preferences_activated), data);
194 gtk_widget_show_all(menu);
199 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
202 struct ui_psensor *ui = (struct ui_psensor *)data;
203 GtkTreeView *view = ui->ui_sensorlist->treeview;
205 if (event->button != 3)
208 struct psensor *sensor = get_sensor_at_pos(view,
214 int coli = col_index_to_col(get_col_index_at_pos(view,
217 if (coli == COL_COLOR) {
218 if (ui_change_color(_("Select foreground color"),
220 ui_sensorlist_update(ui, 1);
221 config_set_sensor_color(sensor->id,
224 } else if (coli >= 0 && coli != COL_ENABLED) {
225 menu = create_sensor_popup(ui, sensor);
227 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
228 event->button, event->time);
236 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
238 struct ui_sensorlist *list = (struct ui_sensorlist *)data;
240 = gtk_tree_view_get_model(list->treeview);
242 GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
246 gtk_tree_model_get_iter(model, &iter, path);
247 gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
251 i = gtk_tree_path_get_indices(path);
254 struct psensor **sensor = list->sensors;
257 (*sensor)->enabled = fixed;
258 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
261 gtk_list_store_set(GTK_LIST_STORE(model),
262 &iter, COL_ENABLED, fixed, -1);
264 gtk_tree_path_free(path);
267 static void create_widget(struct ui_psensor *ui)
270 GtkCellRenderer *renderer;
271 struct psensor **s_cur = ui->sensors;
272 struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
274 store = ui->sensors_store;
276 ui_sl->treeview = ui->sensors_tree;
278 renderer = gtk_cell_renderer_text_new();
279 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
283 "text", COL_NAME, NULL);
285 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
289 "text", COL_TEMP, NULL);
291 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
295 "text", COL_TEMP_MIN, NULL);
297 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
301 "text", COL_TEMP_MAX, NULL);
303 renderer = gtk_cell_renderer_text_new();
304 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
309 "background", COL_COLOR_STR,
312 g_signal_connect(ui_sl->treeview,
313 "button-press-event", (GCallback) on_clicked, ui);
315 renderer = gtk_cell_renderer_toggle_new();
316 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
320 "active", COL_ENABLED,
322 g_signal_connect(G_OBJECT(renderer),
323 "toggled", (GCallback) on_toggled, ui_sl);
325 renderer = gtk_cell_renderer_text_new();
326 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
330 "text", COL_EMPTY, NULL);
336 struct psensor *s = *s_cur;
338 color.red = s->color->red;
339 color.green = s->color->green;
340 color.blue = s->color->blue;
342 scolor = gdk_color_to_string(&color);
344 gtk_list_store_append(store, &iter);
345 gtk_list_store_set(store, &iter,
348 COL_TEMP_MIN, _("N/A"),
349 COL_TEMP_MAX, _("N/A"),
350 COL_COLOR_STR, scolor,
351 COL_ENABLED, s->enabled,
360 void ui_sensorlist_create(struct ui_psensor *ui)
362 log_debug("ui_sensorlist_create()");
363 ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
364 ui->ui_sensorlist->sensors = ui->sensors;