From 1e731a34007fc6e11307b59635a15f946c5f10cc Mon Sep 17 00:00:00 2001 From: Benjamin Berg Date: Wed, 28 Dec 2011 01:02:28 +0100 Subject: [PATCH] Improve PICKONE_* handling in GtkPrinterOptionWidget (bug #543520) This patch modifies GtkPrinterOptionWidget to support loading them again. It also allows the user to enter values from the dropdown list to select the item. --- gtk/gtkprinteroptionwidget.c | 93 +++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 23 deletions(-) diff --git a/gtk/gtkprinteroptionwidget.c b/gtk/gtkprinteroptionwidget.c index fb4640f08d..2f43edfde4 100644 --- a/gtk/gtkprinteroptionwidget.c +++ b/gtk/gtkprinteroptionwidget.c @@ -366,25 +366,52 @@ combo_box_set (GtkWidget *combo, gtk_tree_model_foreach (model, set_cb, &set_data); } -static char * -combo_box_get (GtkWidget *combo) +static gchar * +combo_box_get (GtkWidget *combo, gboolean *custom) { GtkTreeModel *model; gchar *value; GtkTreeIter iter; - if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo))) + model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo)); + + value = NULL; + if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter)) { - value = gtk_combo_box_get_active_text(GTK_COMBO_BOX (combo)); + gtk_tree_model_get (model, &iter, VALUE_COLUMN, &value, -1); + *custom = FALSE; } else { - model = gtk_combo_box_get_model (GTK_COMBO_BOX (combo)); + if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (combo))) + { + value = g_strdup (gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (combo))))); + *custom = TRUE; + } - value = NULL; - if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combo), &iter)) - gtk_tree_model_get (model, &iter, VALUE_COLUMN, &value, -1); - } + if (!value || !gtk_tree_model_get_iter_first (model, &iter)) + return value; + + /* If the user entered an item from the dropdown list manually, return + * the non-custom option instead. */ + do + { + gchar *val, *name; + gtk_tree_model_get (model, &iter, VALUE_COLUMN, &val, + NAME_COLUMN, &name, -1); + if (g_str_equal (value, name)) + { + *custom = FALSE; + g_free (name); + g_free (value); + return val; + } + + g_free (val); + g_free (name); + } + while (gtk_tree_model_iter_next (model, &iter)); + } return value; } @@ -560,25 +587,29 @@ combo_changed_cb (GtkWidget *combo, gchar *value; gchar *filtered_val = NULL; gboolean changed; + gboolean custom = TRUE; g_signal_handler_block (priv->source, priv->source_changed_handler); - value = combo_box_get (combo); + value = combo_box_get (combo, &custom); - /* handle some constraints */ - switch (priv->source->type) + /* Handle constraints if the user entered a custom value. */ + if (custom) { - case GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE: - filtered_val = filter_numeric (value, FALSE, FALSE, &changed); - break; - case GTK_PRINTER_OPTION_TYPE_PICKONE_INT: - filtered_val = filter_numeric (value, TRUE, FALSE, &changed); - break; - case GTK_PRINTER_OPTION_TYPE_PICKONE_REAL: - filtered_val = filter_numeric (value, TRUE, TRUE, &changed); - break; - default: - break; + switch (priv->source->type) + { + case GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE: + filtered_val = filter_numeric (value, FALSE, FALSE, &changed); + break; + case GTK_PRINTER_OPTION_TYPE_PICKONE_INT: + filtered_val = filter_numeric (value, TRUE, FALSE, &changed); + break; + case GTK_PRINTER_OPTION_TYPE_PICKONE_REAL: + filtered_val = filter_numeric (value, TRUE, TRUE, &changed); + break; + default: + break; + } } if (filtered_val) @@ -869,6 +900,22 @@ update_widgets (GtkPrinterOptionWidget *widget) case GTK_PRINTER_OPTION_TYPE_STRING: gtk_entry_set_text (GTK_ENTRY (priv->entry), source->value); break; + case GTK_PRINTER_OPTION_TYPE_PICKONE_PASSWORD: + case GTK_PRINTER_OPTION_TYPE_PICKONE_PASSCODE: + case GTK_PRINTER_OPTION_TYPE_PICKONE_REAL: + case GTK_PRINTER_OPTION_TYPE_PICKONE_INT: + case GTK_PRINTER_OPTION_TYPE_PICKONE_STRING: + { + GtkEntry *entry; + + entry = GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->combo))); + if (gtk_printer_option_has_choice (source, source->value)) + combo_box_set (priv->combo, source->value); + else + gtk_entry_set_text (entry, source->value); + + break; + } case GTK_PRINTER_OPTION_TYPE_FILESAVE: { gchar *filename = g_filename_from_uri (source->value, NULL, NULL);