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

This commit is contained in:
Arx Cruz 2011-10-13 13:40:40 -03:00
parent 09d3fdbc8e
commit ed825cf92b
4 changed files with 168 additions and 32 deletions

View File

@ -18,7 +18,7 @@
* Free Software Foundation, Inc., 121 Franklin Street, Fifth Floor, * Free Software Foundation, Inc., 121 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA. * Boston, MA 02110-1301, USA.
* *
* Authors: Arx Cruz <arxcruz@gmail.com> * Authors: Arx Cruz <arxcruz@gnome.org>
*/ */
#include "config.h" #include "config.h"
@ -27,9 +27,84 @@
#include "util.h" #include "util.h"
static ZenityData *zen_data; static ZenityData *zen_data;
static GSList *selected;
static void zenity_forms_dialog_response (GtkWidget *widget, int response, gpointer data); 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) void zenity_forms_dialog (ZenityData *data, ZenityFormsData *forms_data)
{ {
GtkBuilder *builder = NULL; GtkBuilder *builder = NULL;
@ -41,6 +116,7 @@ void zenity_forms_dialog (ZenityData *data, ZenityFormsData *forms_data)
GSList *tmp; GSList *tmp;
gint number_of_widgets = g_slist_length (forms_data->list); gint number_of_widgets = g_slist_length (forms_data->list);
int list_count = 0;
zen_data = data; zen_data = data;
@ -156,6 +232,22 @@ void zenity_forms_dialog (ZenityData *data, ZenityFormsData *forms_data)
0, 0,
0); 0);
break; 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: default:
zenity_value->forms_widget = gtk_entry_new(); zenity_value->forms_widget = gtk_entry_new();
gtk_table_attach (GTK_TABLE (table), gtk_table_attach (GTK_TABLE (table),
@ -193,6 +285,7 @@ zenity_forms_dialog_response (GtkWidget *widget, int response, gpointer data)
guint day, year, month; guint day, year, month;
GDate *date = NULL; GDate *date = NULL;
gchar time_string[128]; gchar time_string[128];
GtkTreeSelection *selection;
switch (response) { switch (response) {
case GTK_RESPONSE_OK: case GTK_RESPONSE_OK:
@ -204,6 +297,13 @@ zenity_forms_dialog_response (GtkWidget *widget, int response, gpointer data)
case ZENITY_FORMS_ENTRY: case ZENITY_FORMS_ENTRY:
g_print("%s", gtk_entry_get_text (GTK_ENTRY (zenity_value->forms_widget))); g_print("%s", gtk_entry_get_text (GTK_ENTRY (zenity_value->forms_widget)));
break; 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: case ZENITY_FORMS_CALENDAR:
gtk_calendar_get_date (GTK_CALENDAR (zenity_value->forms_widget), &day, &month, &year); gtk_calendar_get_date (GTK_CALENDAR (zenity_value->forms_widget), &day, &month, &year);
date = g_date_new_dmy (year, month + 1, day); date = g_date_new_dmy (year, month + 1, day);

View File

@ -129,6 +129,7 @@ static gboolean zenity_password_show_username;
/* Forms Dialog Options */ /* Forms Dialog Options */
static gboolean zenity_forms_active; static gboolean zenity_forms_active;
static gchar *zenity_forms_date_format; static gchar *zenity_forms_date_format;
static gchar **zenity_forms_list_values;
/* Miscelaneus Options */ /* Miscelaneus Options */
static gboolean zenity_misc_about; static gboolean zenity_misc_about;
@ -956,6 +957,24 @@ static GOptionEntry forms_dialog_options[] = {
N_("Add a new Calendar in forms dialog"), N_("Add a new Calendar in forms dialog"),
N_("Calendar field name") N_("Calendar field name")
}, },
{
"add-list",
'\0',
0,
G_OPTION_ARG_CALLBACK,
zenity_forms_callback,
N_("Add a new List in forms dialog"),
N_("List field and header name")
},
{
"list-values",
'\0',
0,
G_OPTION_ARG_STRING_ARRAY,
&zenity_forms_list_values,
N_("List of values for List"),
N_("List of values separated by |")
},
{ {
"text", "text",
'\0', '\0',
@ -1117,6 +1136,8 @@ zenity_option_free (void) {
if (zenity_forms_date_format) if (zenity_forms_date_format)
g_free (zenity_forms_date_format); g_free (zenity_forms_date_format);
if (zenity_forms_list_values)
g_strfreev (zenity_forms_list_values);
if (zenity_entry_entry_text) if (zenity_entry_entry_text)
g_free (zenity_entry_entry_text); g_free (zenity_entry_entry_text);
@ -1173,13 +1194,17 @@ zenity_forms_callback (const gchar *option_name,
GError **error) GError **error)
{ {
ZenityFormsValue *forms_value = g_new0 (ZenityFormsValue, 1); ZenityFormsValue *forms_value = g_new0 (ZenityFormsValue, 1);
forms_value->option_value = g_strdup(value);
if (g_strcmp0(option_name, "--add-entry") == 0) forms_value->option_value = g_strdup (value);
if (g_strcmp0 (option_name, "--add-entry") == 0)
forms_value->type = ZENITY_FORMS_ENTRY; forms_value->type = ZENITY_FORMS_ENTRY;
else if (g_strcmp0(option_name, "--add-calendar") == 0) else if (g_strcmp0 (option_name, "--add-calendar") == 0)
forms_value->type = ZENITY_FORMS_CALENDAR; forms_value->type = ZENITY_FORMS_CALENDAR;
else if (g_strcmp0(option_name, "--add-password") == 0) else if (g_strcmp0 (option_name, "--add-password") == 0)
forms_value->type = ZENITY_FORMS_PASSWORD; forms_value->type = ZENITY_FORMS_PASSWORD;
else if (g_strcmp0 (option_name, "--add-list") == 0)
forms_value->type = ZENITY_FORMS_LIST;
results->forms_data->list = g_slist_append(results->forms_data->list, forms_value); results->forms_data->list = g_slist_append(results->forms_data->list, forms_value);
@ -1833,10 +1858,20 @@ zenity_forms_post_callback (GOptionContext *context,
gpointer data, gpointer data,
GError **error) GError **error)
{ {
gchar *values;
int i = 0;
zenity_option_set_dialog_mode (zenity_forms_active, MODE_FORMS); zenity_option_set_dialog_mode (zenity_forms_active, MODE_FORMS);
if (results->mode == MODE_FORMS) { if (results->mode == MODE_FORMS) {
results->forms_data->dialog_text = zenity_general_dialog_text; results->forms_data->dialog_text = zenity_general_dialog_text;
results->forms_data->separator = zenity_general_separator; results->forms_data->separator = zenity_general_separator;
if (zenity_forms_list_values) {
values = zenity_forms_list_values[0];
while (values != NULL) {
results->forms_data->list_values = g_slist_append (results->forms_data->list_values, values);
values = zenity_forms_list_values[++i];
}
}
if (zenity_forms_date_format) if (zenity_forms_date_format)
results->forms_data->date_format = zenity_forms_date_format; results->forms_data->date_format = zenity_forms_date_format;
else else

View File

@ -144,6 +144,7 @@ typedef struct {
typedef struct { typedef struct {
GSList *list; GSList *list;
GSList *list_widgets; GSList *list_widgets;
GSList *list_values;
gchar *dialog_text; gchar *dialog_text;
gchar *separator; gchar *separator;
gchar *date_format; gchar *date_format;
@ -152,7 +153,8 @@ typedef struct {
typedef enum { typedef enum {
ZENITY_FORMS_ENTRY, ZENITY_FORMS_ENTRY,
ZENITY_FORMS_PASSWORD, ZENITY_FORMS_PASSWORD,
ZENITY_FORMS_CALENDAR ZENITY_FORMS_CALENDAR,
ZENITY_FORMS_LIST
} ZenityFormsType; } ZenityFormsType;
typedef struct { typedef struct {

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<!-- interface-requires gtk+ 2.6 --> <!-- interface-requires gtk+ 2.6 -->
<!-- interface-naming-policy toplevel-contextual -->
<object class="GtkAdjustment" id="adjustment1"> <object class="GtkAdjustment" id="adjustment1">
<property name="upper">100</property> <property name="upper">100</property>
<property name="step_increment">1</property> <property name="step_increment">1</property>
@ -15,12 +16,12 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox2"> <object class="GtkVBox" id="dialog-vbox2">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">2</property> <property name="spacing">2</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area2"> <object class="GtkHButtonBox" id="dialog-action_area2">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -149,12 +150,12 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox4"> <object class="GtkVBox" id="dialog-vbox4">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">2</property> <property name="spacing">2</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area4"> <object class="GtkHButtonBox" id="dialog-action_area4">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -256,12 +257,12 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox7"> <object class="GtkVBox" id="dialog-vbox7">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">14</property> <property name="spacing">14</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area7"> <object class="GtkHButtonBox" id="dialog-action_area7">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -355,12 +356,12 @@
<property name="border_width">5</property> <property name="border_width">5</property>
<property name="type_hint">normal</property> <property name="type_hint">normal</property>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox12"> <object class="GtkVBox" id="dialog-vbox12">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">2</property> <property name="spacing">2</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area12"> <object class="GtkHButtonBox" id="dialog-action_area12">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -412,6 +413,7 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="top_padding">12</property> <property name="top_padding">12</property>
<property name="bottom_padding">12</property>
<property name="left_padding">12</property> <property name="left_padding">12</property>
<property name="right_padding">6</property> <property name="right_padding">6</property>
<child> <child>
@ -461,12 +463,12 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox9"> <object class="GtkVBox" id="dialog-vbox9">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">14</property> <property name="spacing">14</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area3"> <object class="GtkHButtonBox" id="dialog-action_area3">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -551,12 +553,12 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox6"> <object class="GtkVBox" id="dialog-vbox6">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">2</property> <property name="spacing">2</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area6"> <object class="GtkHButtonBox" id="dialog-action_area6">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -656,12 +658,12 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox3"> <object class="GtkVBox" id="dialog-vbox3">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">14</property> <property name="spacing">14</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="zenity_question_button_box"> <object class="GtkHButtonBox" id="zenity_question_button_box">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -730,11 +732,11 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox11"> <object class="GtkVBox" id="dialog-vbox11">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area11"> <object class="GtkHButtonBox" id="dialog-action_area11">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -837,12 +839,12 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox5"> <object class="GtkVBox" id="dialog-vbox5">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">2</property> <property name="spacing">2</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area5"> <object class="GtkHButtonBox" id="dialog-action_area5">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -954,11 +956,11 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox8"> <object class="GtkVBox" id="dialog-vbox8">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area8"> <object class="GtkHButtonBox" id="dialog-action_area8">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>
@ -1032,9 +1034,6 @@
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="has_focus">True</property> <property name="has_focus">True</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
</object> </object>
</child> </child>
</object> </object>
@ -1066,12 +1065,12 @@
<property name="type_hint">dialog</property> <property name="type_hint">dialog</property>
<signal name="destroy" handler="gtk_main_quit" swapped="no"/> <signal name="destroy" handler="gtk_main_quit" swapped="no"/>
<child internal-child="vbox"> <child internal-child="vbox">
<object class="GtkBox" id="dialog-vbox1"> <object class="GtkVBox" id="dialog-vbox1">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="spacing">14</property> <property name="spacing">14</property>
<child internal-child="action_area"> <child internal-child="action_area">
<object class="GtkButtonBox" id="dialog-action_area1"> <object class="GtkHButtonBox" id="dialog-action_area1">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">False</property> <property name="can_focus">False</property>
<property name="layout_style">end</property> <property name="layout_style">end</property>