| 1 |
/***************************************************************************** |
| 2 |
* |
| 3 |
* cdebconf - An implementation of the Debian Configuration Management |
| 4 |
* System |
| 5 |
* |
| 6 |
* cdebconf is (c) 2000-2007 Randolph Chung and others under the following |
| 7 |
* license. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted provided that the following conditions |
| 11 |
* are met: |
| 12 |
* |
| 13 |
* 1. Redistributions of source code must retain the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer. |
| 15 |
* |
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 17 |
* notice, this list of conditions and the following disclaimer in the |
| 18 |
* documentation and/or other materials provided with the distribution. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND |
| 21 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 24 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 25 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 26 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 28 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 29 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 |
* SUCH DAMAGE. |
| 31 |
* |
| 32 |
*****************************************************************************/ |
| 33 |
|
| 34 |
/** @file select_handlers.c |
| 35 |
* select and multiselect question handlers of the GTK+ frontend |
| 36 |
* |
| 37 |
* @see handlers.c |
| 38 |
*/ |
| 39 |
|
| 40 |
#include "select_handlers.h" |
| 41 |
|
| 42 |
#include <string.h> |
| 43 |
#include <gtk/gtk.h> |
| 44 |
|
| 45 |
#include "frontend.h" |
| 46 |
#include "question.h" |
| 47 |
|
| 48 |
#include "cdebconf_gtk.h" |
| 49 |
#include "fe_data.h" |
| 50 |
#include "descriptions.h" |
| 51 |
#include "choice_model.h" |
| 52 |
#include "ui.h" |
| 53 |
#include "align_text_renderer.h" |
| 54 |
|
| 55 |
/** Setter function for the select handler in multiple questions form. |
| 56 |
* |
| 57 |
* @param question question being set |
| 58 |
* @param combo_box handler's combo box widget |
| 59 |
*/ |
| 60 |
static void set_value_from_combo(struct question * question, |
| 61 |
GtkComboBox * combo_box) |
| 62 |
{ |
| 63 |
GtkTreeModel * model; |
| 64 |
GtkTreeIter iter; |
| 65 |
char * value; |
| 66 |
|
| 67 |
if (gtk_combo_box_get_active_iter(combo_box, &iter)) { |
| 68 |
model = gtk_combo_box_get_model(GTK_COMBO_BOX(combo_box)); |
| 69 |
gtk_tree_model_get(model, &iter, |
| 70 |
/* column: */ CHOICE_MODEL_VALUE, &value, |
| 71 |
-1 /* end of list */); |
| 72 |
question_setvalue(question, value); |
| 73 |
g_free(value); |
| 74 |
} else { |
| 75 |
question_setvalue(question, ""); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** Setter function for the select handler in single question form. |
| 80 |
* |
| 81 |
* @param question question being set |
| 82 |
* @param view handler's tree view |
| 83 |
*/ |
| 84 |
static void set_value_from_select(struct question * question, |
| 85 |
GtkTreeView * view) |
| 86 |
{ |
| 87 |
GtkTreeSelection * selection; |
| 88 |
GtkTreeModel * model; |
| 89 |
GtkTreeIter iter; |
| 90 |
char * value; |
| 91 |
|
| 92 |
selection = gtk_tree_view_get_selection(view); |
| 93 |
if (gtk_tree_selection_get_selected(selection, &model, &iter)) { |
| 94 |
gtk_tree_model_get(model, &iter, |
| 95 |
/* column: */ CHOICE_MODEL_VALUE, &value, |
| 96 |
-1 /* end of list */); |
| 97 |
question_setvalue(question, value); |
| 98 |
g_free(value); |
| 99 |
} else { |
| 100 |
question_setvalue(question, ""); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** Determine if a choice is top-level for partman/choose_partition. |
| 105 |
* |
| 106 |
* @param index choice index |
| 107 |
* @param choice choice canonical value |
| 108 |
* @param choice_translated choice translated value |
| 109 |
* @return TRUE if top-level, FALSE otherwise |
| 110 |
*/ |
| 111 |
static gboolean is_disk(int index, const char * choice, |
| 112 |
const char * choice_translated) |
| 113 |
{ |
| 114 |
return '>' != choice_translated[0]; |
| 115 |
} |
| 116 |
|
| 117 |
/** List of predicates used for questions renderered as tree. */ |
| 118 |
static const struct { |
| 119 |
/** question name */ |
| 120 |
const char * tag; |
| 121 |
/** associated predicate */ |
| 122 |
parent_predicate parent_predicate; |
| 123 |
/** expand all parents */ |
| 124 |
gboolean expand_all; |
| 125 |
} special_questions[] = { |
| 126 |
{ "partman/choose_partition", is_disk, TRUE }, |
| 127 |
{ NULL, NULL, FALSE } |
| 128 |
}; |
| 129 |
|
| 130 |
/** Returns the predicate for a question better rendered as a tree. |
| 131 |
* |
| 132 |
* @param tag question name |
| 133 |
* @return the predicate if found, NULL if there is no specific behaviour |
| 134 |
* @see special_questions |
| 135 |
*/ |
| 136 |
static parent_predicate get_special_predicate(const char * tag) { |
| 137 |
int i; |
| 138 |
|
| 139 |
for (i = 0; NULL != special_questions[i].tag; i++) { |
| 140 |
if (0 == strcmp(tag, special_questions[i].tag)) { |
| 141 |
return special_questions[i].parent_predicate; |
| 142 |
} |
| 143 |
} |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
/** Returns TRUE if a question is "special". |
| 148 |
* |
| 149 |
* @see special_question |
| 150 |
*/ |
| 151 |
#define IS_SPECIAL_QUESTION(Question) \ |
| 152 |
(NULL != get_special_predicate(Question->tag)) |
| 153 |
|
| 154 |
/** Should we expand all nodes for a question better rendered as a tree? |
| 155 |
* |
| 156 |
* @param tag question name |
| 157 |
* @return TRUE if all parents should be expanded, FALSE otherwise |
| 158 |
* @see special_questions |
| 159 |
*/ |
| 160 |
static gboolean should_expand_all(const char * tag) { |
| 161 |
int i; |
| 162 |
|
| 163 |
for (i = 0; NULL != special_questions[i].tag; i++) { |
| 164 |
if (0 == strcmp(tag, special_questions[i].tag)) { |
| 165 |
return special_questions[i].expand_all; |
| 166 |
} |
| 167 |
} |
| 168 |
return FALSE; |
| 169 |
} |
| 170 |
|
| 171 |
/** Hide expanders in the given GtkTreeView. |
| 172 |
* |
| 173 |
* @todo |
| 174 |
* This should be replaced by a gtk_tree_view_set_show_expanders when |
| 175 |
* we switch to GTK+ 2.12. |
| 176 |
* |
| 177 |
* @param view the given GtkTreeView |
| 178 |
*/ |
| 179 |
static void hide_expanders(GtkTreeView * view) |
| 180 |
{ |
| 181 |
GtkTreeViewColumn * column; |
| 182 |
|
| 183 |
column = gtk_tree_view_column_new(); |
| 184 |
gtk_tree_view_column_set_visible(column, FALSE); |
| 185 |
gtk_tree_view_insert_column(view, column, -1 /* at the end */); |
| 186 |
gtk_tree_view_set_expander_column(view, column); |
| 187 |
} |
| 188 |
|
| 189 |
/** Focus the given GtkTreeView to the given GtkTreePath. |
| 190 |
* |
| 191 |
* This will scroll and move the cursor appropriately. |
| 192 |
* |
| 193 |
* This function is required because there is no point in scrolling the widget |
| 194 |
* before it has been realized (= shown on screen) and so we need to do it |
| 195 |
* in a callback connected after the default "expose-event" signal. |
| 196 |
* See #340007 for more details. |
| 197 |
* |
| 198 |
* Once run, it will disconnect itself from the signal and free the given path. |
| 199 |
* |
| 200 |
* @param view the widget where the focus will be set |
| 201 |
* @param path path to the row that will be selected |
| 202 |
* @return FALSE to propage the event further |
| 203 |
*/ |
| 204 |
static gboolean focus_path(GtkTreeView * view, GdkEventExpose * event, |
| 205 |
GtkTreePath * path) |
| 206 |
{ |
| 207 |
gtk_tree_view_scroll_to_cell( |
| 208 |
view, path, NULL /* don't scroll to a particular column */, |
| 209 |
TRUE /* please align */, 0.5 /* center row */, 0 /* left column */); |
| 210 |
g_signal_handlers_disconnect_by_func(view, focus_path, path); |
| 211 |
gtk_tree_path_free(path); |
| 212 |
return FALSE; /* propagate the event */ |
| 213 |
} |
| 214 |
|
| 215 |
/** Private structure for adjust_tabs and adjust_tabs_for_choice. */ |
| 216 |
struct adjust_data { |
| 217 |
/** Widget where the tabs will be renderered. */ |
| 218 |
GtkWidget * widget; |
| 219 |
/** Tab array to update that will be updated. */ |
| 220 |
PangoTabArray * tab_array; |
| 221 |
}; |
| 222 |
|
| 223 |
/** Adjust tab stops for the given node to display all the given values. |
| 224 |
* |
| 225 |
* @param model model being handled |
| 226 |
* @param path path of the current node |
| 227 |
* @param iter iter of the current node |
| 228 |
* @param adjust_data data prepared by adjust_tabs |
| 229 |
* @return always FALSE (= never stop) |
| 230 |
*/ |
| 231 |
static gboolean adjust_tabs_for_node(GtkTreeModel * model, GtkTreePath * path, |
| 232 |
GtkTreeIter * iter, |
| 233 |
struct adjust_data * adjust_data) |
| 234 |
{ |
| 235 |
char * choice; |
| 236 |
char ** values; |
| 237 |
gint columns; |
| 238 |
gint value_width; |
| 239 |
gint previous_location; |
| 240 |
gint location; |
| 241 |
gint i; |
| 242 |
|
| 243 |
gtk_tree_model_get(model, iter, |
| 244 |
/* column: */ CHOICE_MODEL_TRANSLATED_VALUE, &choice, |
| 245 |
-1 /* end of list */); |
| 246 |
values = g_strsplit(choice, "\t", 0 /* split all */); |
| 247 |
|
| 248 |
columns = g_strv_length(values); |
| 249 |
if (pango_tab_array_get_size(adjust_data->tab_array) < columns - 1) { |
| 250 |
pango_tab_array_resize(adjust_data->tab_array, columns - 1); |
| 251 |
} |
| 252 |
previous_location = 0; |
| 253 |
for (i = 0; columns > i + 1; i++) { |
| 254 |
value_width = cdebconf_gtk_get_text_width( |
| 255 |
adjust_data->widget, values[i]) + COLUMN_SPACING; |
| 256 |
pango_tab_array_get_tab(adjust_data->tab_array, i, |
| 257 |
NULL /* don't get alignment */, &location); |
| 258 |
if (location - previous_location < value_width) { |
| 259 |
location = previous_location + value_width; |
| 260 |
pango_tab_array_set_tab(adjust_data->tab_array, i, PANGO_TAB_LEFT, |
| 261 |
location); |
| 262 |
} |
| 263 |
previous_location = location; |
| 264 |
} |
| 265 |
|
| 266 |
#if 0 |
| 267 |
/* DEBUG: dump tabs */ |
| 268 |
g_warning("dump after choice: %s", choice); |
| 269 |
for (i = 0; pango_tab_array_get_size(adjust_data->tab_array) > i; i++) { |
| 270 |
pango_tab_array_get_tab(adjust_data->tab_array, i, |
| 271 |
NULL /* don't get alignment */, |
| 272 |
&location); |
| 273 |
g_warning("%d: %d", i, location); |
| 274 |
} |
| 275 |
#endif |
| 276 |
|
| 277 |
g_free(choice); |
| 278 |
g_strfreev(values); |
| 279 |
|
| 280 |
return FALSE; |
| 281 |
} |
| 282 |
|
| 283 |
/** Adjust tab stops in the given tab array to render all the "columns" |
| 284 |
* (separated by a tab) in the translated choice of the given model. |
| 285 |
* |
| 286 |
* @param widget widget where the choices will be rendered |
| 287 |
* @param tab_array tab array to update |
| 288 |
* @param model a choice model |
| 289 |
*/ |
| 290 |
static void adjust_tabs(GtkWidget * widget, PangoTabArray * tab_array, |
| 291 |
GtkTreeModel * model) |
| 292 |
{ |
| 293 |
struct adjust_data adjust_data; |
| 294 |
|
| 295 |
adjust_data.widget = widget; |
| 296 |
adjust_data.tab_array = tab_array; |
| 297 |
gtk_tree_model_foreach( |
| 298 |
model, (GtkTreeModelForeachFunc) adjust_tabs_for_node, &adjust_data); |
| 299 |
} |
| 300 |
|
| 301 |
/** Insert the column displaying translated choices in the given GtkTreeView. |
| 302 |
* |
| 303 |
* @param fe frontend |
| 304 |
* @param view column destination |
| 305 |
*/ |
| 306 |
static void insert_choice_column(struct frontend * fe, GtkTreeView * view) |
| 307 |
{ |
| 308 |
GtkCellRenderer * renderer; |
| 309 |
PangoTabArray * tab_array; |
| 310 |
|
| 311 |
if (CAN_ALIGN(fe)) { |
| 312 |
tab_array = pango_tab_array_new(0 /* start with no tabs */, |
| 313 |
FALSE /* use pango unit */); |
| 314 |
adjust_tabs(GTK_WIDGET(view), tab_array, |
| 315 |
gtk_tree_view_get_model(view)); |
| 316 |
renderer = cdebconf_gtk_align_text_renderer_new(); |
| 317 |
cdebconf_gtk_align_text_renderer_set_tab_array( |
| 318 |
ALIGN_TEXT_RENDERER(renderer), tab_array); |
| 319 |
pango_tab_array_free(tab_array); |
| 320 |
} else { |
| 321 |
renderer = gtk_cell_renderer_text_new(); |
| 322 |
} |
| 323 |
gtk_tree_view_insert_column_with_attributes( |
| 324 |
view, -1 /* insert at the end */, |
| 325 |
NULL /* no title */, renderer, |
| 326 |
"text", CHOICE_MODEL_TRANSLATED_VALUE, |
| 327 |
NULL /* end of attribute list */); |
| 328 |
} |
| 329 |
|
| 330 |
/** Create widget for select question in single question form. |
| 331 |
* |
| 332 |
* This will also register the corresponding setter function. |
| 333 |
* |
| 334 |
* @param fe cdebconf frontend |
| 335 |
* @param question handled question |
| 336 |
* @param question_box the container for question widgets |
| 337 |
* @param model model for the possible choices |
| 338 |
* @return the created widget, NULL if something went wrong |
| 339 |
* @see focus_path() |
| 340 |
*/ |
| 341 |
static int create_select_list(struct frontend * fe, struct question * question, |
| 342 |
GtkWidget * question_box, GtkTreeModel * model) |
| 343 |
{ |
| 344 |
GtkTreePath * path = NULL; |
| 345 |
GtkWidget * view; |
| 346 |
GtkWidget * scroll; |
| 347 |
GtkWidget * frame; |
| 348 |
|
| 349 |
view = gtk_tree_view_new_with_model(model); |
| 350 |
|
| 351 |
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), |
| 352 |
FALSE /* no headers */); |
| 353 |
gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), |
| 354 |
TRUE /* enable typeahead */); |
| 355 |
gtk_tree_view_set_search_column(GTK_TREE_VIEW(view), |
| 356 |
CHOICE_MODEL_TRANSLATED_VALUE); |
| 357 |
gtk_tree_selection_set_mode( |
| 358 |
gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), |
| 359 |
GTK_SELECTION_BROWSE); |
| 360 |
|
| 361 |
if (!IS_SPECIAL_QUESTION(question)) { |
| 362 |
hide_expanders(GTK_TREE_VIEW(view)); |
| 363 |
} |
| 364 |
|
| 365 |
insert_choice_column(fe, GTK_TREE_VIEW(view)); |
| 366 |
|
| 367 |
g_signal_connect_swapped(G_OBJECT(view), "row-activated", |
| 368 |
G_CALLBACK(cdebconf_gtk_set_answer_ok), fe); |
| 369 |
|
| 370 |
if (should_expand_all(question->tag)) { |
| 371 |
gtk_tree_view_expand_all(GTK_TREE_VIEW(view)); |
| 372 |
} |
| 373 |
|
| 374 |
path = cdebconf_gtk_choice_model_get_first_selected(model); |
| 375 |
/* Select first row if there is no other default */ |
| 376 |
if (NULL == path) { |
| 377 |
path = gtk_tree_path_new_first(); |
| 378 |
} else { |
| 379 |
/* Only expand path when there was a previous selection. */ |
| 380 |
gtk_tree_view_expand_to_path(GTK_TREE_VIEW(view), path); |
| 381 |
} |
| 382 |
gtk_tree_view_set_cursor(GTK_TREE_VIEW(view), path, |
| 383 |
NULL /* don't focus a particular column */, |
| 384 |
FALSE /* don't start editing */); |
| 385 |
/* We need to focus the row *after* the widget realization, see #340007. */ |
| 386 |
g_signal_connect_after(view, "expose-event", G_CALLBACK(focus_path), path); |
| 387 |
/* path will be free'd in focus_path() */ |
| 388 |
|
| 389 |
scroll = gtk_scrolled_window_new(NULL /* create horizontal adjustement */, |
| 390 |
NULL /* create vertical adjustement */); |
| 391 |
gtk_container_add(GTK_CONTAINER(scroll), view); |
| 392 |
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), |
| 393 |
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
| 394 |
frame = gtk_frame_new(NULL); |
| 395 |
gtk_container_add(GTK_CONTAINER(frame), scroll); |
| 396 |
|
| 397 |
cdebconf_gtk_add_common_layout(fe, question, question_box, frame); |
| 398 |
|
| 399 |
gtk_widget_grab_focus(view); |
| 400 |
|
| 401 |
cdebconf_gtk_register_setter(fe, SETTER_FUNCTION(set_value_from_select), |
| 402 |
question, view); |
| 403 |
|
| 404 |
return DC_OK; |
| 405 |
} |
| 406 |
|
| 407 |
/** Update the model when a multiselect option is toggled. |
| 408 |
* |
| 409 |
* @param cell toggled cell |
| 410 |
* @param path_string location of the cell in the model |
| 411 |
* @param model corresponding model |
| 412 |
*/ |
| 413 |
static void update_selection_for_toggle(GtkCellRendererToggle * cell, |
| 414 |
const gchar * path_string, |
| 415 |
GtkTreeModel * model) |
| 416 |
{ |
| 417 |
GtkTreeIter iter; |
| 418 |
gboolean value; |
| 419 |
|
| 420 |
if (gtk_tree_model_get_iter_from_string(model, &iter, path_string)) { |
| 421 |
gtk_tree_model_get(model, &iter, |
| 422 |
/* column: */ CHOICE_MODEL_SELECTED, &value, |
| 423 |
-1 /* end of list */); |
| 424 |
value ^= TRUE; |
| 425 |
cdebconf_gtk_choice_model_set( |
| 426 |
model, &iter, |
| 427 |
/* column: */ CHOICE_MODEL_SELECTED, value, |
| 428 |
-1 /* end of list */); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
/** Setter function for multiselect question. |
| 433 |
* |
| 434 |
* @todo This function won't work if there is more than one level |
| 435 |
* in the model. |
| 436 |
* |
| 437 |
* @param question question being set |
| 438 |
* @param model choices model |
| 439 |
*/ |
| 440 |
static void set_value_from_multiselect(struct question * question, |
| 441 |
GtkTreeModel * model) |
| 442 |
{ |
| 443 |
GtkTreeIter iter; |
| 444 |
gboolean valid; |
| 445 |
gboolean selected; |
| 446 |
gchar ** selected_values; |
| 447 |
guint selected_index; |
| 448 |
gchar * result; |
| 449 |
|
| 450 |
selected_values = g_malloc0( |
| 451 |
sizeof (char *) * (1 /* terminating NULL */ + |
| 452 |
cdebconf_gtk_choice_model_get_length(model))); |
| 453 |
selected_index = 0; |
| 454 |
valid = gtk_tree_model_get_iter_first(model, &iter); |
| 455 |
while (valid) { |
| 456 |
gtk_tree_model_get(model, &iter, |
| 457 |
/* column: */ CHOICE_MODEL_SELECTED, &selected, |
| 458 |
-1 /* end of list */); |
| 459 |
if (selected) { |
| 460 |
gtk_tree_model_get(model, &iter, |
| 461 |
/* column: */ CHOICE_MODEL_VALUE, |
| 462 |
&selected_values[selected_index], |
| 463 |
-1 /* end of list */); |
| 464 |
selected_index++; |
| 465 |
} |
| 466 |
valid = gtk_tree_model_iter_next(model, &iter); |
| 467 |
} |
| 468 |
if (0 < selected_index) { |
| 469 |
result = g_strjoinv(", ", selected_values); |
| 470 |
} else { |
| 471 |
/* no selections */ |
| 472 |
result = g_strdup(""); |
| 473 |
} |
| 474 |
question_setvalue(question, result); |
| 475 |
g_free(result); |
| 476 |
|
| 477 |
for (selected_index = 0; NULL != selected_values[selected_index]; |
| 478 |
selected_index++) { |
| 479 |
g_free(selected_values[selected_index]); |
| 480 |
} |
| 481 |
g_free(selected_values); |
| 482 |
} |
| 483 |
|
| 484 |
/** Update the model when a toggle button is toggled. |
| 485 |
* |
| 486 |
* @param toggle_button the button which have been toggled |
| 487 |
* @param row_reference associated row in the model |
| 488 |
*/ |
| 489 |
static void update_model_from_toggle_button( |
| 490 |
GtkToggleButton * toggle_button, GtkTreeRowReference * row_reference) |
| 491 |
{ |
| 492 |
GtkTreeModel * model = gtk_tree_row_reference_get_model(row_reference); |
| 493 |
GtkTreePath * path = gtk_tree_row_reference_get_path(row_reference); |
| 494 |
GtkTreeIter iter; |
| 495 |
|
| 496 |
g_assert(NULL != model); |
| 497 |
g_assert(NULL != path); |
| 498 |
|
| 499 |
if (gtk_tree_model_get_iter(model, &iter, path)) { |
| 500 |
gtk_list_store_set( |
| 501 |
GTK_LIST_STORE(model), &iter, |
| 502 |
/* column: */ CHOICE_MODEL_SELECTED, |
| 503 |
gtk_toggle_button_get_active(toggle_button), |
| 504 |
-1 /* end of list */); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
static void closure_gtk_tree_row_reference_free(GtkTreeRowReference *row_reference, |
| 509 |
GClosure *closure) { |
| 510 |
gtk_tree_row_reference_free(row_reference); |
| 511 |
} |
| 512 |
|
| 513 |
/** Connect a signal to an object for a specific row in the model. |
| 514 |
* |
| 515 |
* @param object object which will be connected |
| 516 |
* @param signal signal name |
| 517 |
* @param model model to be associated |
| 518 |
* @param iter location of the row in the model |
| 519 |
* @param handler signal handler |
| 520 |
* @return signal identifier |
| 521 |
*/ |
| 522 |
static gulong connect_signal_to_row(GObject * object, const gchar * signal, |
| 523 |
GtkTreeModel * model, GtkTreeIter * iter, |
| 524 |
GCallback handler) |
| 525 |
{ |
| 526 |
GtkTreePath * path; |
| 527 |
GtkTreeRowReference * row_reference; |
| 528 |
|
| 529 |
path = gtk_tree_model_get_path(model, iter); |
| 530 |
row_reference = gtk_tree_row_reference_new(model, path); |
| 531 |
gtk_tree_path_free(path); |
| 532 |
return g_signal_connect_data(object, "toggled", handler, row_reference, |
| 533 |
(GClosureNotify) closure_gtk_tree_row_reference_free, |
| 534 |
0 /* no connect options */); |
| 535 |
} |
| 536 |
|
| 537 |
/** Handler for the "cursor-changed" signal that forces the cursor to stay on |
| 538 |
* the first column. |
| 539 |
* |
| 540 |
* This will disallow the selection of option descriptions during multiselect. |
| 541 |
* |
| 542 |
* @param view the tree view |
| 543 |
* @param fe cdebconf frontend |
| 544 |
*/ |
| 545 |
static void handle_cursor_changed(GtkTreeView * view, |
| 546 |
struct frontend * fe) |
| 547 |
{ |
| 548 |
GtkTreePath * path; |
| 549 |
|
| 550 |
gtk_tree_view_get_cursor(view, &path, NULL); |
| 551 |
g_signal_handlers_block_by_func(view, handle_cursor_changed, fe); |
| 552 |
gtk_tree_view_set_cursor(view, path, gtk_tree_view_get_column(view, 0), |
| 553 |
FALSE /* no editing */); |
| 554 |
g_signal_handlers_unblock_by_func(view, handle_cursor_changed, fe); |
| 555 |
} |
| 556 |
|
| 557 |
/** Create widget for multiselect question in single question form. |
| 558 |
* |
| 559 |
* This will also register the corresponding setter function. |
| 560 |
* |
| 561 |
* @param fe cdebconf frontend |
| 562 |
* @param question handled question |
| 563 |
* @param question_box container for the question widgets |
| 564 |
* @param model model for the possible choices |
| 565 |
* @return the created widget, NULL if something went wrong |
| 566 |
*/ |
| 567 |
static int create_multiselect_list(struct frontend * fe, |
| 568 |
struct question * question, |
| 569 |
GtkWidget * question_box, |
| 570 |
GtkTreeModel * model) |
| 571 |
{ |
| 572 |
GtkTreeIter iter; |
| 573 |
GtkWidget * view; |
| 574 |
GtkWidget * scroll; |
| 575 |
GtkWidget * frame; |
| 576 |
GtkCellRenderer * toggle_renderer; |
| 577 |
GtkTreePath * path; |
| 578 |
|
| 579 |
view = gtk_tree_view_new_with_model(model); |
| 580 |
|
| 581 |
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE); |
| 582 |
|
| 583 |
toggle_renderer = gtk_cell_renderer_toggle_new(); |
| 584 |
g_signal_connect(G_OBJECT(toggle_renderer), "toggled", |
| 585 |
G_CALLBACK(update_selection_for_toggle), model); |
| 586 |
gtk_tree_view_insert_column_with_attributes( |
| 587 |
GTK_TREE_VIEW(view), -1 /* insert at the end */, |
| 588 |
NULL /* no title */, toggle_renderer, |
| 589 |
"active", CHOICE_MODEL_SELECTED, |
| 590 |
NULL /* end of attribute list */); |
| 591 |
|
| 592 |
insert_choice_column(fe, GTK_TREE_VIEW(view)); |
| 593 |
|
| 594 |
if (!IS_SPECIAL_QUESTION(question)) { |
| 595 |
hide_expanders(GTK_TREE_VIEW(view)); |
| 596 |
} |
| 597 |
|
| 598 |
g_signal_connect(G_OBJECT(view), "cursor-changed", |
| 599 |
G_CALLBACK(handle_cursor_changed), fe); |
| 600 |
|
| 601 |
/* select the first row */ |
| 602 |
gtk_tree_model_get_iter_first(model, &iter); |
| 603 |
path = gtk_tree_model_get_path(model, &iter); |
| 604 |
gtk_tree_view_set_cursor(GTK_TREE_VIEW(view), path, |
| 605 |
NULL /* don't focus on a particular column */, |
| 606 |
FALSE /* do not start editing */); |
| 607 |
gtk_tree_path_free(path); |
| 608 |
|
| 609 |
scroll = gtk_scrolled_window_new(NULL /* create horizontal adjustement */, |
| 610 |
NULL /* create vertical adjustement */); |
| 611 |
gtk_container_add(GTK_CONTAINER(scroll), view); |
| 612 |
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), |
| 613 |
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); |
| 614 |
frame = gtk_frame_new(NULL /* no label */); |
| 615 |
gtk_container_add(GTK_CONTAINER(frame), scroll); |
| 616 |
|
| 617 |
cdebconf_gtk_add_common_layout(fe, question, question_box, frame); |
| 618 |
gtk_widget_grab_focus(view); |
| 619 |
|
| 620 |
cdebconf_gtk_register_setter( |
| 621 |
fe, SETTER_FUNCTION(set_value_from_multiselect), question, model); |
| 622 |
return DC_OK; |
| 623 |
} |
| 624 |
|
| 625 |
/** Set the focus on the first child of the given container. |
| 626 |
* |
| 627 |
* @param container the container where the child will be found |
| 628 |
*/ |
| 629 |
static void focus_first_child(GtkContainer * container) |
| 630 |
{ |
| 631 |
GList * children; |
| 632 |
|
| 633 |
children = gtk_container_get_children(container); |
| 634 |
gtk_widget_grab_focus(GTK_WIDGET(children->data)); |
| 635 |
g_list_free(children); |
| 636 |
} |
| 637 |
|
| 638 |
/** Create a widget for multiselect question in multiple question form. |
| 639 |
* |
| 640 |
* This will also register the corresponding setter function. |
| 641 |
* |
| 642 |
* @param fe cdebconf frontend |
| 643 |
* @param question handled question |
| 644 |
* @param question_box container for the question widgets |
| 645 |
* @param model model for possible choices |
| 646 |
* @return the created widget, NULL if something went wrong |
| 647 |
*/ |
| 648 |
static int create_multiselect_checkboxes(struct frontend * fe, |
| 649 |
struct question * question, |
| 650 |
GtkWidget * question_box, |
| 651 |
GtkTreeModel * model) |
| 652 |
{ |
| 653 |
GtkWidget * check_container; |
| 654 |
GtkWidget * check; |
| 655 |
GtkTreeIter iter; |
| 656 |
gboolean valid; |
| 657 |
gchar * label; |
| 658 |
gboolean selected; |
| 659 |
|
| 660 |
/* XXX: should we return DC_OK? */ |
| 661 |
g_assert(0 < cdebconf_gtk_choice_model_get_length(model)); |
| 662 |
|
| 663 |
check_container = gtk_vbox_new(FALSE /* don't make children equal */, |
| 664 |
0 /* padding */); |
| 665 |
|
| 666 |
valid = gtk_tree_model_get_iter_first(model, &iter); |
| 667 |
while (valid) { |
| 668 |
gtk_tree_model_get(model, &iter, |
| 669 |
/* column: */ CHOICE_MODEL_TRANSLATED_VALUE, &label, |
| 670 |
/* column: */ CHOICE_MODEL_SELECTED, &selected, |
| 671 |
-1 /* end of list */); |
| 672 |
|
| 673 |
check = gtk_check_button_new_with_label(label); |
| 674 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), selected); |
| 675 |
|
| 676 |
connect_signal_to_row(G_OBJECT(check), "toggled", model, &iter, |
| 677 |
G_CALLBACK(update_model_from_toggle_button)); |
| 678 |
|
| 679 |
gtk_box_pack_start(GTK_BOX(check_container), check, |
| 680 |
FALSE /* don't expand */, FALSE /* don't fill */, |
| 681 |
0 /* padding */); |
| 682 |
|
| 683 |
g_free(label); |
| 684 |
valid = gtk_tree_model_iter_next(model, &iter); |
| 685 |
} |
| 686 |
|
| 687 |
cdebconf_gtk_add_common_layout(fe, question, question_box, |
| 688 |
check_container); |
| 689 |
|
| 690 |
if (cdebconf_gtk_is_first_question(question)) { |
| 691 |
focus_first_child(GTK_CONTAINER(check_container)); |
| 692 |
} |
| 693 |
|
| 694 |
cdebconf_gtk_register_setter( |
| 695 |
fe, SETTER_FUNCTION(set_value_from_multiselect), question, model); |
| 696 |
|
| 697 |
return DC_OK; |
| 698 |
} |
| 699 |
|
| 700 |
/** Activate the selected choice in the combo box. |
| 701 |
* |
| 702 |
* @param combo_box combo box to modify |
| 703 |
* @param model model to lookup |
| 704 |
*/ |
| 705 |
static void set_selected_active(GtkWidget * combo_box, GtkTreeModel * model) |
| 706 |
{ |
| 707 |
GtkTreePath * path; |
| 708 |
GtkTreeIter iter; |
| 709 |
gboolean valid; |
| 710 |
|
| 711 |
path = cdebconf_gtk_choice_model_get_first_selected(model); |
| 712 |
if (NULL != path) { |
| 713 |
valid = gtk_tree_model_get_iter(model, &iter, path); |
| 714 |
if (valid) { |
| 715 |
gtk_combo_box_set_active_iter(GTK_COMBO_BOX(combo_box), &iter); |
| 716 |
} |
| 717 |
gtk_tree_path_free(path); |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
/** Create widget for select question in multiple question form. |
| 722 |
* |
| 723 |
* This will also register the corresponding setter function. |
| 724 |
* |
| 725 |
* @param fe cdebconf frontend |
| 726 |
* @param question handled question |
| 727 |
* @param question_box container for the question widgets |
| 728 |
* @param model model for the possible choices |
| 729 |
* @return the created widget, NULL if something went wrong |
| 730 |
*/ |
| 731 |
static int create_select_combo(struct frontend * fe, |
| 732 |
struct question * question, |
| 733 |
GtkWidget * question_box, |
| 734 |
GtkTreeModel * model) |
| 735 |
{ |
| 736 |
GtkWidget * combo_box; |
| 737 |
GtkCellRenderer * text_renderer; |
| 738 |
|
| 739 |
combo_box = gtk_combo_box_new_with_model(model); |
| 740 |
text_renderer = gtk_cell_renderer_text_new(); |
| 741 |
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo_box), text_renderer, |
| 742 |
TRUE /* expand */); |
| 743 |
gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo_box), text_renderer, |
| 744 |
"text", CHOICE_MODEL_TRANSLATED_VALUE, |
| 745 |
NULL /* end of list */); |
| 746 |
set_selected_active(combo_box, model); |
| 747 |
|
| 748 |
cdebconf_gtk_add_common_layout(fe, question, question_box, combo_box); |
| 749 |
|
| 750 |
if (cdebconf_gtk_is_first_question(question)) { |
| 751 |
gtk_widget_grab_focus(combo_box); |
| 752 |
} |
| 753 |
|
| 754 |
cdebconf_gtk_register_setter(fe, SETTER_FUNCTION(set_value_from_combo), |
| 755 |
question, combo_box); |
| 756 |
|
| 757 |
return DC_OK; |
| 758 |
} |
| 759 |
|
| 760 |
/** Handle all possible select and multiselect questions. |
| 761 |
* |
| 762 |
* This will dispatch between the different handlers. |
| 763 |
* |
| 764 |
* @param fe cdebconf frontend |
| 765 |
* @param question handled question |
| 766 |
* @param question_box container for the question widgets |
| 767 |
* @param multiselect should be TRUE if the question is a multiselect |
| 768 |
* @return DC_NOTOK if something went wrong, DC_OK otherwise |
| 769 |
*/ |
| 770 |
static int handle_all(struct frontend * fe, struct question * question, |
| 771 |
GtkWidget * question_box, gboolean multiselect) |
| 772 |
{ |
| 773 |
GtkTreeModel * model; |
| 774 |
|
| 775 |
model = cdebconf_gtk_choice_model_create_full( |
| 776 |
fe, question, get_special_predicate(question->tag)); |
| 777 |
if (NULL == model) { |
| 778 |
g_warning("cdebconf_gtk_choice_model_create_full failed."); |
| 779 |
return DC_NOTOK; |
| 780 |
} |
| 781 |
|
| 782 |
if (multiselect && IS_QUESTION_SINGLE(question)) { |
| 783 |
return create_multiselect_list(fe, question, question_box, model); |
| 784 |
} |
| 785 |
if (multiselect) { |
| 786 |
return create_multiselect_checkboxes(fe, question, question_box, |
| 787 |
model); |
| 788 |
} |
| 789 |
if (IS_QUESTION_SINGLE(question)) { |
| 790 |
return create_select_list(fe, question, question_box, model); |
| 791 |
} |
| 792 |
return create_select_combo(fe, question, question_box, model); |
| 793 |
} |
| 794 |
|
| 795 |
/** Handler for select questions. |
| 796 |
* |
| 797 |
* @param fe cdebconf frontend |
| 798 |
* @param question handled question |
| 799 |
* @param question_box container for handler widgets |
| 800 |
* @return DC_NOTOK if something went wrong, DC_OK otherwise |
| 801 |
*/ |
| 802 |
int cdebconf_gtk_handle_select(struct frontend * fe, |
| 803 |
struct question * question, |
| 804 |
GtkWidget * question_box) |
| 805 |
{ |
| 806 |
return handle_all(fe, question, question_box, FALSE /* simple select */); |
| 807 |
} |
| 808 |
|
| 809 |
/** Handler for multiselect questions. |
| 810 |
* |
| 811 |
* @param fe cdebconf frontend |
| 812 |
* @param question handled question |
| 813 |
* @param question_box container for handler widgets |
| 814 |
* @return DC_NOTOK if something went wrong, DC_OK otherwise |
| 815 |
*/ |
| 816 |
int cdebconf_gtk_handle_multiselect(struct frontend * fe, |
| 817 |
struct question * question, |
| 818 |
GtkWidget * question_box) |
| 819 |
{ |
| 820 |
return handle_all(fe, question, question_box, TRUE /* multiselect */); |
| 821 |
} |
| 822 |
|
| 823 |
/* vim: et sw=4 si |
| 824 |
*/ |