2 Copyright (C) 2010-2011 jeanfi@gmail.com
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.
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.
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
26 #include "ui_sensorlist.h"
27 #include "ui_sensorpref.h"
45 struct ui_psensor *ui;
46 struct psensor *sensor;
49 static int col_index_to_col(int idx)
59 void ui_sensorlist_update(struct ui_psensor *ui)
62 struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
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;
68 while (valid && *sensor) {
69 struct psensor *s = *sensor;
73 str = psensor_value_to_string(s->type,
74 s->measures[s->values_max_length -
76 gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_TEMP, str,
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);
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);
90 valid = gtk_tree_model_iter_next(model, &iter);
96 * Returns the sensor corresponding to the x/y position
101 static struct psensor *
102 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
106 gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
109 gint *i = gtk_tree_path_get_indices(path);
111 return *(sensors + *i);
117 * Returns the index of the column corresponding
118 * to the x position in the table.
122 static int get_col_index_at_pos(GtkTreeView *view, int x)
124 GList *columns = gtk_tree_view_get_columns(view);
129 for (node = columns; node; node = node->next) {
130 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
133 x < (colx + gtk_tree_view_column_get_width(checkcol)))
136 colx += gtk_tree_view_column_get_width(checkcol);
144 void ui_sensorlist_update_sensors_preferences(struct ui_psensor *ui)
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;
152 while (valid && *sensor) {
156 color.red = (*sensor)->color->red;
157 color.green = (*sensor)->color->green;
158 color.blue = (*sensor)->color->blue;
160 scolor = gdk_color_to_string(&color);
162 gtk_list_store_set(GTK_LIST_STORE(model),
163 &iter, COL_NAME, (*sensor)->name, -1);
165 gtk_list_store_set(GTK_LIST_STORE(model),
166 &iter, COL_COLOR_STR, scolor, -1);
168 gtk_list_store_set(GTK_LIST_STORE(model),
169 &iter, COL_ENABLED, (*sensor)->enabled, -1);
173 valid = gtk_tree_model_iter_next(model, &iter);
178 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
180 struct cb_data *cb_data = data;
182 ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
185 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
186 struct psensor *sensor)
190 GtkWidget *separator;
191 struct cb_data *data;
193 menu = gtk_menu_new();
195 item = gtk_menu_item_new_with_label(sensor->name);
196 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
198 separator = gtk_separator_menu_item_new();
199 gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
201 item = gtk_menu_item_new_with_label(_("Preferences"));
202 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
204 data = malloc(sizeof(struct cb_data));
206 data->sensor = sensor;
208 g_signal_connect(item,
210 G_CALLBACK(on_preferences_activated), data);
212 gtk_widget_show_all(menu);
217 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
219 struct ui_psensor *ui = (struct ui_psensor *)data;
220 GtkTreeView *view = ui->ui_sensorlist->treeview;
222 struct psensor *sensor = get_sensor_at_pos(view,
228 int coli = col_index_to_col(get_col_index_at_pos(view,
231 if (coli == COL_COLOR) {
232 if (ui_change_color(_("Select foreground color"),
234 ui_sensorlist_update_sensors_preferences(ui);
235 config_set_sensor_color(sensor->id,
238 } else if (coli >= 0 && coli != COL_ENABLED) {
239 GtkWidget *menu = create_sensor_popup(ui,
242 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
243 event->button, event->time);
252 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
254 struct ui_sensorlist *list = (struct ui_sensorlist *)data;
256 = gtk_tree_view_get_model(list->treeview);
258 GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
262 gtk_tree_model_get_iter(model, &iter, path);
263 gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
267 i = gtk_tree_path_get_indices(path);
270 struct psensor **sensor = list->sensors;
273 (*sensor)->enabled = fixed;
274 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
277 gtk_list_store_set(GTK_LIST_STORE(model),
278 &iter, COL_ENABLED, fixed, -1);
280 gtk_tree_path_free(path);
283 static void create_widget(struct ui_psensor *ui)
286 GtkCellRenderer *renderer;
287 struct psensor **s_cur = ui->sensors;
288 struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
290 store = gtk_list_store_new(COLS_COUNT,
297 G_TYPE_BOOLEAN, G_TYPE_STRING);
299 ui_sl->treeview = GTK_TREE_VIEW
300 (gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
302 gtk_tree_selection_set_mode
303 (gtk_tree_view_get_selection(ui_sl->treeview),
306 renderer = gtk_cell_renderer_text_new();
307 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
311 "text", COL_NAME, NULL);
313 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
317 "text", COL_TEMP, NULL);
319 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
323 "text", COL_TEMP_MIN, NULL);
325 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
329 "text", COL_TEMP_MAX, NULL);
331 renderer = gtk_cell_renderer_text_new();
332 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
337 "background", COL_COLOR_STR,
340 g_signal_connect(ui_sl->treeview,
341 "button-press-event", (GCallback) on_clicked, ui);
343 renderer = gtk_cell_renderer_toggle_new();
344 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
348 "active", COL_ENABLED,
350 g_signal_connect(G_OBJECT(renderer),
351 "toggled", (GCallback) on_toggled, ui_sl);
353 renderer = gtk_cell_renderer_text_new();
354 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
358 "text", COL_EMPTY, NULL);
364 struct psensor *s = *s_cur;
366 color.red = s->color->red;
367 color.green = s->color->green;
368 color.blue = s->color->blue;
370 scolor = gdk_color_to_string(&color);
372 gtk_list_store_append(store, &iter);
373 gtk_list_store_set(store, &iter,
376 COL_TEMP_MIN, _("N/A"),
377 COL_TEMP_MAX, _("N/A"),
378 COL_COLOR_STR, scolor,
379 COL_ENABLED, s->enabled, -1);
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));
393 void ui_sensorlist_create(struct ui_psensor *ui)
395 ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
396 ui->ui_sensorlist->sensors = ui->sensors;