From ed825cf92b9d786b9b13361db4a0e696af347a59 Mon Sep 17 00:00:00 2001 From: Arx Cruz Date: Thu, 13 Oct 2011 13:40:40 -0300 Subject: Initial support for list/tree on --forms option Added zenity --add-list and --list-values on --forms option. This is an initial support. Next steps add support to multiple selections and multiple columns --- src/forms.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 2 deletions(-) (limited to 'src/forms.c') diff --git a/src/forms.c b/src/forms.c index c2b9f851..56c6f5ab 100644 --- a/src/forms.c +++ b/src/forms.c @@ -18,7 +18,7 @@ * Free Software Foundation, Inc., 121 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * - * Authors: Arx Cruz + * Authors: Arx Cruz */ #include "config.h" @@ -27,9 +27,84 @@ #include "util.h" static ZenityData *zen_data; - +static GSList *selected; static void zenity_forms_dialog_response (GtkWidget *widget, int response, gpointer data); +static void zenity_forms_dialog_get_selected (GtkTreeModel *model, GtkTreePath *path_buf, GtkTreeIter *iter, GtkTreeView *tree_view) +{ + GValue value = {0, }; + gtk_tree_model_get_value (model, iter, 0, &value); + selected = g_slist_append (selected, g_value_dup_string (&value)); + g_value_unset (&value); +} + +static void zenity_forms_dialog_output (void) +{ + GSList *tmp; + + for (tmp = selected; tmp; tmp = tmp->next) { + if (tmp->next != NULL) { + g_print ("%s,", (gchar *) tmp->data); + } + else + g_print ("%s", (gchar *) tmp->data); + } + + g_slist_foreach (selected, (GFunc) g_free, NULL); + selected = NULL; +} + +static GtkWidget * +zenity_forms_create_and_fill_list (ZenityFormsData *forms_data, + int list_number, gchar *header) +{ + GtkListStore *list_store; + GtkWidget *tree_view; + GtkWidget *scrolled_window; + GtkCellRenderer *renderer; + + gchar *values; + int i = 0; + + list_store = gtk_list_store_new (1, G_TYPE_STRING); + if (forms_data->list_values) { + values = g_slist_nth_data (forms_data->list_values, list_number); + if (values) { + gchar **row_values = g_strsplit_set (values, "|", -1); + if (row_values) { + GtkTreeIter iter; + gchar *row = row_values[0]; + 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); + } + } + } + tree_view = gtk_tree_view_new (); + renderer = gtk_cell_renderer_text_new (); + gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view), + -1, + header, + renderer, + "text", + 0, + NULL); + + gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (list_store)); + g_object_unref (list_store); + scrolled_window = gtk_scrolled_window_new (NULL, NULL); + //gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled_window), + // GTK_WIDGET (tree_view)); + gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (tree_view)); + gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), -1, 100); + gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree_view), FALSE); + + return scrolled_window; +} + void zenity_forms_dialog (ZenityData *data, ZenityFormsData *forms_data) { GtkBuilder *builder = NULL; @@ -41,6 +116,7 @@ void zenity_forms_dialog (ZenityData *data, ZenityFormsData *forms_data) GSList *tmp; gint number_of_widgets = g_slist_length (forms_data->list); + int list_count = 0; zen_data = data; @@ -156,6 +232,22 @@ void zenity_forms_dialog (ZenityData *data, ZenityFormsData *forms_data) 0, 0); break; + case ZENITY_FORMS_LIST: + zenity_value->forms_widget = zenity_forms_create_and_fill_list (forms_data, list_count, + zenity_value->option_value); + 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); + list_count++; + break; default: zenity_value->forms_widget = gtk_entry_new(); gtk_table_attach (GTK_TABLE (table), @@ -193,6 +285,7 @@ zenity_forms_dialog_response (GtkWidget *widget, int response, gpointer data) guint day, year, month; GDate *date = NULL; gchar time_string[128]; + GtkTreeSelection *selection; switch (response) { case GTK_RESPONSE_OK: @@ -204,6 +297,13 @@ zenity_forms_dialog_response (GtkWidget *widget, int response, gpointer data) case ZENITY_FORMS_ENTRY: g_print("%s", gtk_entry_get_text (GTK_ENTRY (zenity_value->forms_widget))); break; + case ZENITY_FORMS_LIST: + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (gtk_bin_get_child (GTK_BIN (zenity_value->forms_widget)))); + gtk_tree_selection_selected_foreach (selection, + (GtkTreeSelectionForeachFunc) zenity_forms_dialog_get_selected, + GTK_TREE_VIEW (gtk_bin_get_child (GTK_BIN (zenity_value->forms_widget)))); + zenity_forms_dialog_output (); + break; case ZENITY_FORMS_CALENDAR: gtk_calendar_get_date (GTK_CALENDAR (zenity_value->forms_widget), &day, &month, &year); date = g_date_new_dmy (year, month + 1, day); -- cgit