2 * Copyright (C) 2010-2012 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)
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;
69 use_celcius = ui->config->temperature_unit == CELCIUS;
71 while (valid && *sensor) {
74 str = psensor_value_to_string(s->type,
75 s->measures[s->values_max_length -
79 gtk_list_store_set(GTK_LIST_STORE(model), &iter, COL_TEMP, str,
83 str = psensor_value_to_string(s->type, s->min, use_celcius);
84 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
85 COL_TEMP_MIN, str, -1);
88 str = psensor_value_to_string(s->type, s->max, use_celcius);
89 gtk_list_store_set(GTK_LIST_STORE(model), &iter,
90 COL_TEMP_MAX, str, -1);
93 valid = gtk_tree_model_iter_next(model, &iter);
99 * Returns the sensor corresponding to the x/y position
104 static struct psensor *
105 get_sensor_at_pos(GtkTreeView *view, int x, int y, struct psensor **sensors)
109 gtk_tree_view_get_path_at_pos(view, x, y, &path, NULL, NULL, NULL);
112 gint *i = gtk_tree_path_get_indices(path);
114 return *(sensors + *i);
120 * Returns the index of the column corresponding
121 * to the x position in the table.
125 static int get_col_index_at_pos(GtkTreeView *view, int x)
127 GList *columns = gtk_tree_view_get_columns(view);
132 for (node = columns; node; node = node->next) {
133 GtkTreeViewColumn *checkcol = (GtkTreeViewColumn *) node->data;
136 x < (colx + gtk_tree_view_column_get_width(checkcol)))
139 colx += gtk_tree_view_column_get_width(checkcol);
147 void ui_sensorlist_update_sensors_preferences(struct ui_psensor *ui)
151 = gtk_tree_view_get_model(ui->ui_sensorlist->treeview);
152 gboolean valid = gtk_tree_model_get_iter_first(model, &iter);
153 struct psensor **sensor = ui->ui_sensorlist->sensors;
155 while (valid && *sensor) {
159 color.red = (*sensor)->color->red;
160 color.green = (*sensor)->color->green;
161 color.blue = (*sensor)->color->blue;
163 scolor = gdk_color_to_string(&color);
165 gtk_list_store_set(GTK_LIST_STORE(model),
166 &iter, COL_NAME, (*sensor)->name, -1);
168 gtk_list_store_set(GTK_LIST_STORE(model),
169 &iter, COL_COLOR_STR, scolor, -1);
171 gtk_list_store_set(GTK_LIST_STORE(model),
172 &iter, COL_ENABLED, (*sensor)->enabled, -1);
176 valid = gtk_tree_model_iter_next(model, &iter);
181 static void on_preferences_activated(GtkWidget *menu_item, gpointer data)
183 struct cb_data *cb_data = data;
185 ui_sensorpref_dialog_run(cb_data->sensor, cb_data->ui);
188 static GtkWidget *create_sensor_popup(struct ui_psensor *ui,
189 struct psensor *sensor)
193 GtkWidget *separator;
194 struct cb_data *data;
196 menu = gtk_menu_new();
198 item = gtk_menu_item_new_with_label(sensor->name);
199 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
201 separator = gtk_separator_menu_item_new();
202 gtk_menu_shell_append(GTK_MENU_SHELL(menu), separator);
204 item = gtk_menu_item_new_with_label(_("Preferences"));
205 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
207 data = malloc(sizeof(struct cb_data));
209 data->sensor = sensor;
211 g_signal_connect(item,
213 G_CALLBACK(on_preferences_activated), data);
215 gtk_widget_show_all(menu);
220 static int on_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data)
222 struct ui_psensor *ui = (struct ui_psensor *)data;
223 GtkTreeView *view = ui->ui_sensorlist->treeview;
225 struct psensor *sensor = get_sensor_at_pos(view,
231 int coli = col_index_to_col(get_col_index_at_pos(view,
234 if (coli == COL_COLOR) {
235 if (ui_change_color(_("Select foreground color"),
237 ui_sensorlist_update_sensors_preferences(ui);
238 config_set_sensor_color(sensor->id,
241 } else if (coli >= 0 && coli != COL_ENABLED) {
242 GtkWidget *menu = create_sensor_popup(ui,
245 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
246 event->button, event->time);
255 on_toggled(GtkCellRendererToggle *cell, gchar *path_str, gpointer data)
257 struct ui_sensorlist *list = (struct ui_sensorlist *)data;
259 = gtk_tree_view_get_model(list->treeview);
261 GtkTreePath *path = gtk_tree_path_new_from_string(path_str);
265 gtk_tree_model_get_iter(model, &iter, path);
266 gtk_tree_model_get(model, &iter, COL_ENABLED, &fixed, -1);
270 i = gtk_tree_path_get_indices(path);
273 struct psensor **sensor = list->sensors;
276 (*sensor)->enabled = fixed;
277 config_set_sensor_enabled((*sensor)->id, (*sensor)->enabled);
280 gtk_list_store_set(GTK_LIST_STORE(model),
281 &iter, COL_ENABLED, fixed, -1);
283 gtk_tree_path_free(path);
286 static void create_widget(struct ui_psensor *ui)
289 GtkCellRenderer *renderer;
290 struct psensor **s_cur = ui->sensors;
291 struct ui_sensorlist *ui_sl = ui->ui_sensorlist;
293 store = gtk_list_store_new(COLS_COUNT,
300 G_TYPE_BOOLEAN, G_TYPE_STRING);
302 ui_sl->treeview = GTK_TREE_VIEW
303 (gtk_tree_view_new_with_model(GTK_TREE_MODEL(store)));
305 gtk_tree_selection_set_mode
306 (gtk_tree_view_get_selection(ui_sl->treeview),
309 renderer = gtk_cell_renderer_text_new();
310 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
314 "text", COL_NAME, NULL);
316 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
320 "text", COL_TEMP, NULL);
322 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
326 "text", COL_TEMP_MIN, NULL);
328 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
332 "text", COL_TEMP_MAX, NULL);
334 renderer = gtk_cell_renderer_text_new();
335 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
340 "background", COL_COLOR_STR,
343 g_signal_connect(ui_sl->treeview,
344 "button-press-event", (GCallback) on_clicked, ui);
346 renderer = gtk_cell_renderer_toggle_new();
347 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
351 "active", COL_ENABLED,
353 g_signal_connect(G_OBJECT(renderer),
354 "toggled", (GCallback) on_toggled, ui_sl);
356 renderer = gtk_cell_renderer_text_new();
357 gtk_tree_view_insert_column_with_attributes(ui_sl->treeview,
361 "text", COL_EMPTY, NULL);
367 struct psensor *s = *s_cur;
369 color.red = s->color->red;
370 color.green = s->color->green;
371 color.blue = s->color->blue;
373 scolor = gdk_color_to_string(&color);
375 gtk_list_store_append(store, &iter);
376 gtk_list_store_set(store, &iter,
379 COL_TEMP_MIN, _("N/A"),
380 COL_TEMP_MAX, _("N/A"),
381 COL_COLOR_STR, scolor,
382 COL_ENABLED, s->enabled, -1);
389 ui_sl->widget = gtk_scrolled_window_new(NULL, NULL);
390 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ui_sl->widget),
391 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
392 gtk_container_add(GTK_CONTAINER(ui_sl->widget),
393 GTK_WIDGET(ui_sl->treeview));
396 void ui_sensorlist_create(struct ui_psensor *ui)
398 ui->ui_sensorlist = malloc(sizeof(struct ui_sensorlist));
399 ui->ui_sensorlist->sensors = ui->sensors;