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 -
77 gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_TEMP, str,
81 str = psensor_value_to_string(s->type, s->min);
82 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
83 COL_TEMP_MIN, str, -1);
86 str = psensor_value_to_string(s->type, s->max);
87 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
88 COL_TEMP_MAX, str, -1);
91 valid = gtk_tree_model_iter_next(model, &iter);
97 * Returns the sensor corresponding to the x/y position
102 static struct psensor *
103 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
107 gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
110 gint *i = gtk_tree_path_get_indices(path);
112 return *(sensors + *i);
118 * Returns the index of the column corresponding
119 * to the x position in the table.
123 static int get_col_index_at_pos(GtkTreeView *view, int x)
125 GList *columns = gtk_tree_view_get_columns(view);
130 for (node = columns; node; node = node->next) {
131 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
134 x < (colx + gtk_tree_view_column_get_width(checkcol)))
137 colx += gtk_tree_view_column_get_width(checkcol);
145 void ui_sensorlist_update_sensors_preferences(struct ui_psensor *ui)
149 = gtk_tree_view_get_model(ui->ui_sensorlist->treeview);
150 gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
151 struct psensor **sensor = ui->ui_sensorlist->sensors;
153 while (valid && *sensor) {
157 color.red = (*sensor)->color->red;
158 color.green = (*sensor)->color->green;
159 color.blue = (*sensor)->color->blue;
161 scolor = gdk_color_to_string(&color);
163 gtk_list_store_set(GTK_LIST_STORE(model),
164 &iter, COL_NAME, (*sensor)->name, -1);
166 gtk_list_store_set(GTK_LIST_STORE(model),
167 &iter, COL_COLOR_STR, scolor, -1);
169 gtk_list_store_set(GTK_LIST_STORE(model),
170 &iter, COL_ENABLED, (*sensor)->enabled, -1);
174 valid = gtk_tree_model_iter_next(model, &iter);
179 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
181 struct cb_data *cb_data = data;
183 ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
186 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
187 struct psensor *sensor)
191 GtkWidget *separator;
192 struct cb_data *data;
194 menu = gtk_menu_new();
196 item = gtk_menu_item_new_with_label(sensor->name);
197 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
199 separator = gtk_separator_menu_item_new();
200 gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
202 item = gtk_menu_item_new_with_label(_("Preferences"));
203 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
205 data = malloc(sizeof(struct cb_data));
207 data->sensor = sensor;
209 g_signal_connect(item,
211 G_CALLBACK(on_preferences_activated), data);
213 gtk_widget_show_all(menu);
218 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
220 struct ui_psensor *ui = (struct ui_psensor *)data;
221 GtkTreeView *view = ui->ui_sensorlist->treeview;
223 struct psensor *sensor = get_sensor_at_pos(view,
229 int coli = col_index_to_col(get_col_index_at_pos(view,
232 if (coli == COL_COLOR) {
233 if (ui_change_color(_("Select foreground color"),
235 ui_sensorlist_update_sensors_preferences(ui);
236 config_set_sensor_color(sensor->id,
239 } else if (coli >= 0 && coli != COL_ENABLED) {
240 GtkWidget *menu = create_sensor_popup(ui,
243 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
244 event->button, event->time);
253 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
255 struct ui_sensorlist *list = (struct ui_sensorlist *)data;
257 = gtk_tree_view_get_model(list->treeview);
259 GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
263 gtk_tree_model_get_iter(model, &iter, path);
264 gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
268 i = gtk_tree_path_get_indices(path);
271 struct psensor **sensor = list->sensors;
274 (*sensor)->enabled = fixed;
275 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
278 gtk_list_store_set(GTK_LIST_STORE(model),
279 &iter, COL_ENABLED, fixed, -1);
281 gtk_tree_path_free(path);
284 static void create_widget(struct ui_psensor *ui)
287 GtkCellRenderer *renderer;
288 struct psensor **s_cur = ui->sensors;
289 struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
291 store = gtk_list_store_new(COLS_COUNT,
298 G_TYPE_BOOLEAN, G_TYPE_STRING);
300 ui_sl->treeview = GTK_TREE_VIEW
301 (gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
303 gtk_tree_selection_set_mode
304 (gtk_tree_view_get_selection(ui_sl->treeview),
307 renderer = gtk_cell_renderer_text_new();
308 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
312 "text", COL_NAME, NULL);
314 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
318 "text", COL_TEMP, NULL);
320 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
324 "text", COL_TEMP_MIN, NULL);
326 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
330 "text", COL_TEMP_MAX, NULL);
332 renderer = gtk_cell_renderer_text_new();
333 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
338 "background", COL_COLOR_STR,
341 g_signal_connect(ui_sl->treeview,
342 "button-press-event", (GCallback) on_clicked, ui);
344 renderer = gtk_cell_renderer_toggle_new();
345 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
349 "active", COL_ENABLED,
351 g_signal_connect(G_OBJECT(renderer),
352 "toggled", (GCallback) on_toggled, ui_sl);
354 renderer = gtk_cell_renderer_text_new();
355 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
359 "text", COL_EMPTY, NULL);
365 struct psensor *s = *s_cur;
367 color.red = s->color->red;
368 color.green = s->color->green;
369 color.blue = s->color->blue;
371 scolor = gdk_color_to_string(&color);
373 gtk_list_store_append(store, &iter);
374 gtk_list_store_set(store, &iter,
377 COL_TEMP_MIN, _("N/A"),
378 COL_TEMP_MAX, _("N/A"),
379 COL_COLOR_STR, scolor,
380 COL_ENABLED, s->enabled, -1);
387 ui_sl->widget = gtk_scrolled_window_new(NULL, NULL);
388 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui_sl->widget),
389 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
390 gtk_container_add(GTK_CONTAINER(ui_sl->widget),
391 GTK_WIDGET(ui_sl->treeview));
394 void ui_sensorlist_create(struct ui_psensor *ui)
396 ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
397 ui->ui_sensorlist->sensors = ui->sensors;