[zenity] Added combobox support on forms dialog



commit 8888114fed6e65da0bf9d4a1642a0b591f14d32f
Author: Arx Cruz <arxcruz gnome org>
Date:   Wed Sep 25 09:34:25 2013 -0300

    Added combobox support on forms dialog

 src/forms.c  |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/option.c |   34 +++++++++++++++++++++++++++
 src/zenity.h |    4 ++-
 3 files changed, 108 insertions(+), 1 deletions(-)
---
diff --git a/src/forms.c b/src/forms.c
index 0bac667..59c6461 100644
--- a/src/forms.c
+++ b/src/forms.c
@@ -44,6 +44,46 @@ static void zenity_forms_dialog_get_selected (GtkTreeModel *model, GtkTreePath *
   }
 }
 
+static GtkWidget *
+zenity_forms_create_and_fill_combo (ZenityFormsData *forms_data, int combo_number)
+{
+  GtkListStore *list_store;
+  GtkWidget *combo_box;
+  GtkCellRenderer *renderer;
+  gchar *combo_values;
+
+  list_store = gtk_list_store_new (1, G_TYPE_STRING);
+
+  if (forms_data->combo_values) {
+    combo_values = g_slist_nth_data (forms_data->combo_values, combo_number);
+    if (combo_values) {
+      gchar **row_values = g_strsplit_set (combo_values, "|", -1);
+      if (row_values) {
+        gint i = 0;
+        GtkTreeIter iter;
+        gchar *row = row_values[i];
+
+        while (row != NULL) {
+          gtk_list_store_append (list_store, &iter);
+          gtk_list_store_set (list_store, &iter, 0, row, -1);
+          row = row_values[++i];
+        }
+        g_strfreev (row_values);
+      }
+      g_free (combo_values);
+    }
+  }
+
+  combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list_store));
+  g_object_unref (G_OBJECT (list_store));
+
+  renderer = gtk_cell_renderer_text_new ();
+  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
+  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer, "text", 0, NULL);
+
+  return combo_box;
+}
+
 static GtkWidget * 
 zenity_forms_create_and_fill_list (ZenityFormsData        *forms_data, 
                                            int list_number, gchar *header)
@@ -141,6 +181,7 @@ void zenity_forms_dialog (ZenityData *data, ZenityFormsData *forms_data)
 
   gint number_of_widgets = g_slist_length (forms_data->list);
   int list_count = 0;
+  int combo_count = 0;
   int i = 0;
 
   zen_data = data;
@@ -271,6 +312,21 @@ void zenity_forms_dialog (ZenityData *data, ZenityFormsData *forms_data)
                       0);
           list_count++;                                                                           
         break;
+      case ZENITY_FORMS_COMBO:
+          zenity_value->forms_widget = zenity_forms_create_and_fill_combo (forms_data, combo_count);
+          gtk_alignment_set (GTK_ALIGNMENT (align), 0.0, 0.02, 0.0, 0.0);
+          gtk_table_attach (GTK_TABLE (table),
+                      GTK_WIDGET (zenity_value->forms_widget),
+                      1,
+                      2,
+                      i,
+                      i+1,
+                      GTK_EXPAND | GTK_FILL,
+                      GTK_EXPAND | GTK_FILL,
+                      0,
+                      0);
+          combo_count++;
+        break;             
       default:
         zenity_value->forms_widget = gtk_entry_new();
         gtk_table_attach (GTK_TABLE (table),
@@ -307,7 +363,10 @@ zenity_forms_dialog_output (ZenityFormsData *forms_data)
   guint day, year, month;
   GDate *date = NULL;
   gchar time_string[128];
+  gchar *combo_value = NULL;
   GtkTreeSelection *selection;
+  GtkListStore *list_store;
+  GtkTreeIter iter;
 
   for (tmp = forms_data->list; tmp; tmp = tmp->next) {
     ZenityFormsValue *zenity_value = (ZenityFormsValue *) tmp->data;
@@ -340,6 +399,18 @@ zenity_forms_dialog_output (ZenityFormsData *forms_data)
         g_date_strftime (time_string, 127, forms_data->date_format, date);
         g_print ("%s", time_string);
         break;
+      case ZENITY_FORMS_COMBO:
+        if (gtk_combo_box_get_active_iter (GTK_COMBO_BOX (zenity_value->forms_widget), &iter)) {
+          list_store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (zenity_value->forms_widget)));
+          gtk_tree_model_get (GTK_TREE_MODEL (list_store), &iter, 0, &combo_value, -1);
+          g_object_unref (G_OBJECT (list_store));
+ 
+          g_print ("%s", combo_value);
+          g_free (combo_value);
+        } else
+          g_print (" ");
+         break;
+        
     }
     if (tmp->next != NULL)
       g_print("%s", forms_data->separator);
diff --git a/src/option.c b/src/option.c
index 7658e8b..d3004fe 100644
--- a/src/option.c
+++ b/src/option.c
@@ -138,6 +138,7 @@ static gchar   *zenity_forms_date_format;
 //static gchar   *zenity_forms_hide_column;
 static gchar  **zenity_forms_list_values;
 static gchar  **zenity_forms_column_values;
+static gchar  **zenity_forms_combo_values;
 
 /* Miscelaneus Options */
 static gboolean zenity_misc_about;
@@ -1064,6 +1065,24 @@ static GOptionEntry forms_dialog_options[] = {
     N_("List of values for columns"),
     N_("List of values separated by |")
   },
+  {
+    "add-combo",
+    '\0',
+    0,
+    G_OPTION_ARG_CALLBACK,
+    zenity_forms_callback,
+    N_("Add a new combo box in forms dialog"),
+    N_("Combo box field name")
+  },
+  {
+    "combo-values",
+    '\0',
+    0,
+    G_OPTION_ARG_STRING_ARRAY,
+    &zenity_forms_combo_values,
+    N_("List of values for combo box"),
+    N_("List of values separated by |")
+  },
  /* TODO: Implement how to hide specifc column 
   {
     "hide-column",
@@ -1246,6 +1265,8 @@ zenity_option_free (void) {
     g_free (zenity_forms_date_format);
   if (zenity_forms_list_values)
     g_strfreev (zenity_forms_list_values);
+  if (zenity_forms_combo_values)
+    g_strfreev (zenity_forms_combo_values);
   if (zenity_forms_column_values)
     g_strfreev (zenity_forms_column_values);
 //  if (zenity_forms_hide_column)
@@ -1321,6 +1342,8 @@ zenity_forms_callback (const gchar *option_name,
     forms_value->type = ZENITY_FORMS_PASSWORD;
   else if (g_strcmp0 (option_name, "--add-list") == 0)
     forms_value->type = ZENITY_FORMS_LIST;
+  else if (g_strcmp0 (option_name, "--add-combo") == 0)
+    forms_value->type = ZENITY_FORMS_COMBO;
 
   results->forms_data->list = g_slist_append(results->forms_data->list, forms_value);
 
@@ -2018,6 +2041,14 @@ zenity_forms_post_callback (GOptionContext *context,
     } else
       results->forms_data->column_values = g_slist_append (NULL, "column");
 
+    if (zenity_forms_combo_values) {
+      i = 0;
+      values = zenity_forms_combo_values[0];
+      while (values != NULL) {
+        results->forms_data->combo_values = g_slist_append (results->forms_data->combo_values, values);
+        values = zenity_forms_combo_values[++i];
+      }
+    }
     if (zenity_forms_date_format)
       results->forms_data->date_format = zenity_forms_date_format;
     else
@@ -2035,6 +2066,9 @@ zenity_forms_post_callback (GOptionContext *context,
     if (zenity_forms_column_values)
       zenity_option_error (zenity_option_get_name (forms_dialog_options, &zenity_forms_column_values),
                           ERROR_SUPPORT);
+    if (zenity_forms_combo_values)
+      zenity_option_error (zenity_option_get_name (forms_dialog_options, &zenity_forms_combo_values),
+                          ERROR_SUPPORT);
     if (zenity_forms_show_header)
       zenity_option_error (zenity_option_get_name (forms_dialog_options, &zenity_forms_show_header),
                           ERROR_SUPPORT);
diff --git a/src/zenity.h b/src/zenity.h
index 501109a..acde39b 100644
--- a/src/zenity.h
+++ b/src/zenity.h
@@ -151,6 +151,7 @@ typedef struct {
   GSList *list_widgets;
   GSList *list_values;
   GSList *column_values;
+  GSList *combo_values;
   gchar *dialog_text;
   gchar *separator;
   gchar *date_format;
@@ -162,7 +163,8 @@ typedef enum {
   ZENITY_FORMS_ENTRY,
   ZENITY_FORMS_PASSWORD,
   ZENITY_FORMS_CALENDAR,
-  ZENITY_FORMS_LIST
+  ZENITY_FORMS_LIST,
+  ZENITY_FORMS_COMBO
 } ZenityFormsType;
 
 typedef struct {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]