2 * Copyright (C) 2010-2011 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)
60 struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
62 = gtk_tree_view_get_model(ui_sl->treeview);
63 gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
64 struct psensor **sensor = ui->sensors;
66 while (valid && *sensor) {
67 struct psensor *s = *sensor;
71 str = psensor_value_to_string(s->type,
72 s->measures[s->values_max_length -
75 gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_TEMP, str,
79 str = psensor_value_to_string(s->type, s->min);
80 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
81 COL_TEMP_MIN, str, -1);
84 str = psensor_value_to_string(s->type, s->max);
85 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
86 COL_TEMP_MAX, str, -1);
89 valid = gtk_tree_model_iter_next(model, &iter);
95 * Returns the sensor corresponding to the x/y position
100 static struct psensor *
101 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
105 gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
108 gint *i = gtk_tree_path_get_indices(path);
110 return *(sensors + *i);
116 * Returns the index of the column corresponding
117 * to the x position in the table.
121 static int get_col_index_at_pos(GtkTreeView *view, int x)
123 GList *columns = gtk_tree_view_get_columns(view);
128 for (node = columns; node; node = node->next) {
129 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
132 x < (colx + gtk_tree_view_column_get_width(checkcol)))
135 colx += gtk_tree_view_column_get_width(checkcol);
143 void ui_sensorlist_update_sensors_preferences(struct ui_psensor *ui)
147 = gtk_tree_view_get_model(ui->ui_sensorlist->treeview);
148 gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
149 struct psensor **sensor = ui->ui_sensorlist->sensors;
151 while (valid && *sensor) {
155 color.red = (*sensor)->color->red;
156 color.green = (*sensor)->color->green;
157 color.blue = (*sensor)->color->blue;
159 scolor = gdk_color_to_string(&color);
161 gtk_list_store_set(GTK_LIST_STORE(model),
162 &iter, COL_NAME, (*sensor)->name, -1);
164 gtk_list_store_set(GTK_LIST_STORE(model),
165 &iter, COL_COLOR_STR, scolor, -1);
167 gtk_list_store_set(GTK_LIST_STORE(model),
168 &iter, COL_ENABLED, (*sensor)->enabled, -1);
172 valid = gtk_tree_model_iter_next(model, &iter);
177 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
179 struct cb_data *cb_data = data;
181 ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
184 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
185 struct psensor *sensor)
189 GtkWidget *separator;
190 struct cb_data *data;
192 menu = gtk_menu_new();
194 item = gtk_menu_item_new_with_label(sensor->name);
195 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
197 separator = gtk_separator_menu_item_new();
198 gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
200 item = gtk_menu_item_new_with_label(_("Preferences"));
201 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
203 data = malloc(sizeof(struct cb_data));
205 data->sensor = sensor;
207 g_signal_connect(item,
209 G_CALLBACK(on_preferences_activated), data);
211 gtk_widget_show_all(menu);
216 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
218 struct ui_psensor *ui = (struct ui_psensor *)data;
219 GtkTreeView *view = ui->ui_sensorlist->treeview;
221 struct psensor *sensor = get_sensor_at_pos(view,
227 int coli = col_index_to_col(get_col_index_at_pos(view,
230 if (coli == COL_COLOR) {
231 if (ui_change_color(_("Select foreground color"),
233 ui_sensorlist_update_sensors_preferences(ui);
234 config_set_sensor_color(sensor->id,
237 } else if (coli >= 0 && coli != COL_ENABLED) {
238 GtkWidget *menu = create_sensor_popup(ui,
241 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
242 event->button, event->time);
251 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
253 struct ui_sensorlist *list = (struct ui_sensorlist *)data;
255 = gtk_tree_view_get_model(list->treeview);
257 GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
261 gtk_tree_model_get_iter(model, &iter, path);
262 gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
266 i = gtk_tree_path_get_indices(path);
269 struct psensor **sensor = list->sensors;
272 (*sensor)->enabled = fixed;
273 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
276 gtk_list_store_set(GTK_LIST_STORE(model),
277 &iter, COL_ENABLED, fixed, -1);
279 gtk_tree_path_free(path);
282 static void create_widget(struct ui_psensor *ui)
285 GtkCellRenderer *renderer;
286 struct psensor **s_cur = ui->sensors;
287 struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
289 store = gtk_list_store_new(COLS_COUNT,
296 G_TYPE_BOOLEAN, G_TYPE_STRING);
298 ui_sl->treeview = GTK_TREE_VIEW
299 (gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
301 gtk_tree_selection_set_mode
302 (gtk_tree_view_get_selection(ui_sl->treeview),
305 renderer = gtk_cell_renderer_text_new();
306 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
310 "text", COL_NAME, NULL);
312 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
316 "text", COL_TEMP, NULL);
318 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
322 "text", COL_TEMP_MIN, NULL);
324 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
328 "text", COL_TEMP_MAX, NULL);
330 renderer = gtk_cell_renderer_text_new();
331 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
336 "background", COL_COLOR_STR,
339 g_signal_connect(ui_sl->treeview,
340 "button-press-event", (GCallback) on_clicked, ui);
342 renderer = gtk_cell_renderer_toggle_new();
343 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
347 "active", COL_ENABLED,
349 g_signal_connect(G_OBJECT(renderer),
350 "toggled", (GCallback) on_toggled, ui_sl);
352 renderer = gtk_cell_renderer_text_new();
353 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
357 "text", COL_EMPTY, NULL);
363 struct psensor *s = *s_cur;
365 color.red = s->color->red;
366 color.green = s->color->green;
367 color.blue = s->color->blue;
369 scolor = gdk_color_to_string(&color);
371 gtk_list_store_append(store, &iter);
372 gtk_list_store_set(store, &iter,
375 COL_TEMP_MIN, _("N/A"),
376 COL_TEMP_MAX, _("N/A"),
377 COL_COLOR_STR, scolor,
378 COL_ENABLED, s->enabled, -1);
385 ui_sl->widget = gtk_scrolled_window_new(NULL, NULL);
386 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui_sl->widget),
387 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
388 gtk_container_add(GTK_CONTAINER(ui_sl->widget),
389 GTK_WIDGET(ui_sl->treeview));
392 void ui_sensorlist_create(struct ui_psensor *ui)
394 ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
395 ui->ui_sensorlist->sensors = ui->sensors;