[gtk+/portal] Add combo box support to GtkFileChooserNative
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/portal] Add combo box support to GtkFileChooserNative
- Date: Mon, 27 Jun 2016 20:53:30 +0000 (UTC)
commit e6fcadf374675666c7e2edbf27789b79d8f115b4
Author: Matthias Clasen <mclasen redhat com>
Date: Mon Jun 27 16:47:11 2016 -0400
Add combo box support to GtkFileChooserNative
This commit adds API for adding combo boxes to a native file chooser,
and getting the selected value back in ::response.
gtk/gtkfilechoosernative.c | 152 +++++++++++++++++++++++++++++++++++++
gtk/gtkfilechoosernative.h | 17 ++++
gtk/gtkfilechoosernativeprivate.h | 9 ++
3 files changed, 178 insertions(+), 0 deletions(-)
---
diff --git a/gtk/gtkfilechoosernative.c b/gtk/gtkfilechoosernative.c
index 6e347eb..81ac0c9 100644
--- a/gtk/gtkfilechoosernative.c
+++ b/gtk/gtkfilechoosernative.c
@@ -303,6 +303,157 @@ gtk_file_chooser_native_set_cancel_label (GtkFileChooserNative *self,
g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_CANCEL_LABEL]);
}
+static GtkFileChooserNativeChoice *
+find_choice (GtkFileChooserNative *self,
+ const char *id)
+{
+ GSList *l;
+
+ for (l = self->choices; l; l = l->next)
+ {
+ GtkFileChooserNativeChoice *choice = l->data;
+
+ if (strcmp (choice->id, id) == 0)
+ return choice;
+ }
+
+ return NULL;
+}
+
+static void
+gtk_file_chooser_native_choice_free (GtkFileChooserNativeChoice *choice)
+{
+ g_free (choice->id);
+ g_free (choice->label);
+ g_strfreev (choice->options);
+ g_strfreev (choice->option_labels);
+ g_free (choice->selected);
+ g_free (choice);
+}
+
+/**
+ * gtk_file_chooser_native_add_choice:
+ * @self: a #GtkFileChooserNative
+ * @id: id for the added choice
+ * @label: user-visible label for the added choice
+ * @options: ids for the options of the choice
+ * @option_labels: user-visible labels for the options, must be the same length as @options
+ *
+ * Adds a 'choice' to the native file chooser. This is typically implemented
+ * as a combobox. You can select a value using gtk_file_chooser_native_set_choice()
+ * before the dialog is shown, and you can obtain the user-selected value in the
+ * #GtkFileChooserNative::response signal handler using gtk_file_chooser_native_get_choice().
+ *
+ * Compare gtk_file_chooser_set_extra_widget().
+ *
+ * Since: 3.22
+ */
+void
+gtk_file_chooser_native_add_choice (GtkFileChooserNative *self,
+ const char *id,
+ const char *label,
+ const char **options,
+ const char **option_labels)
+{
+ GtkFileChooserNativeChoice *choice = find_choice (self, id);
+
+ if (choice != NULL)
+ {
+ g_warning ("Choice with id %s already added to %s %p", id, G_OBJECT_TYPE_NAME (self), self);
+ return;
+ }
+
+ g_assert (g_strv_length (options) == g_strv_length (option_labels));
+
+ choice = g_new0 (GtkFileChooserNativeChoice, 1);
+ choice->id = g_strdup (id);
+ choice->label = g_strdup (label);
+ choice->options = g_strdupv (options);
+ choice->option_labels = g_strdupv (option_labels);
+ choice->selected = NULL;
+
+ self->choices = g_slist_prepend (self->choices, choice);
+}
+
+/**
+ * gtk_file_chooser_native_remove_choice:
+ * @self: a #GtkFileChooserNative
+ * @id: the ID of the choice to remove
+ *
+ * Removes a 'choice' that has been added with gtk_file_chooser_native_add_choice().
+ *
+ * Since: 3.22
+ */
+void
+gtk_file_chooser_native_remove_choice (GtkFileChooserNative *self,
+ const char *id)
+{
+ GtkFileChooserNativeChoice *choice = find_choice (self, id);
+
+ if (choice == NULL)
+ {
+ g_warning ("No choice with id %s found in %s %p", id, G_OBJECT_TYPE_NAME (self), self);
+ return;
+ }
+
+ g_slist_remove (self->choices, choice);
+
+ gtk_file_chooser_native_choice_free (choice);
+}
+
+/**
+ * gtk_file_chooser_native_set_choice:
+ * @self: a #GtkFileChooserNative
+ * @id: the ID of the choice to remove
+ * @selected: the ID of the option to select
+ *
+ * Selects an option in a 'choice' that has been added
+ * with gtk_file_chooser_native_add_choice().
+ *
+ * Since: 3.22
+ */
+void
+gtk_file_chooser_native_set_choice (GtkFileChooserNative *self,
+ const char *id,
+ const char *selected)
+{
+ GtkFileChooserNativeChoice *choice = find_choice (self, id);
+
+ if (choice == NULL)
+ {
+ g_warning ("No choice with id %s found in %s %p", id, G_OBJECT_TYPE_NAME (self), self);
+ return;
+ }
+
+ g_free (choice->selected);
+ choice->selected = g_strdup (selected);
+}
+
+/**
+ * gtk_file_chooser_native_get_choice:
+ * @self: a #GtkFileChooserNative
+ * @id: the ID of the choice to remove
+ *
+ * Gets the currently selected option in the 'choice' with the given ID.
+ *
+ * Returns: the ID of the currenly selected option
+ * Since: 3.22
+ */
+const char *
+gtk_file_chooser_native_get_choice (GtkFileChooserNative *self,
+ const char *id)
+{
+ GtkFileChooserNativeChoice *choice = find_choice (self, id);
+
+ if (choice == NULL)
+ {
+ g_warning ("No choice with id %s found in %s %p", id, G_OBJECT_TYPE_NAME (self), self);
+ return NULL;
+ }
+
+ return choice->selected;
+}
+
static void
gtk_file_chooser_native_set_property (GObject *object,
guint prop_id,
@@ -366,6 +517,7 @@ gtk_file_chooser_native_finalize (GObject *object)
gtk_widget_destroy (self->dialog);
g_slist_free_full (self->custom_files, g_object_unref);
+ g_slist_free_full (self->choices, gtk_file_chooser_native_choice_free);
G_OBJECT_CLASS (gtk_file_chooser_native_parent_class)->finalize (object);
}
diff --git a/gtk/gtkfilechoosernative.h b/gtk/gtkfilechoosernative.h
index c2c21d7..97cb565 100644
--- a/gtk/gtkfilechoosernative.h
+++ b/gtk/gtkfilechoosernative.h
@@ -50,6 +50,23 @@ const char *gtk_file_chooser_native_get_cancel_label (GtkFileChooserNative *self
GDK_AVAILABLE_IN_3_20
void gtk_file_chooser_native_set_cancel_label (GtkFileChooserNative *self,
const char *cancel_label);
+GDK_AVAILABLE_IN_3_22
+void gtk_file_chooser_native_add_choice (GtkFileChooserNative *self,
+ const char *id,
+ const char *label,
+ const char **options,
+ const char **option_labels);
+GDK_AVAILABLE_IN_3_22
+void gtk_file_chooser_native_remove_choice (GtkFileChooserNative *self,
+ const char *id);
+GDK_AVAILABLE_IN_3_22
+void gtk_file_chooser_native_set_choice (GtkFileChooserNative *self,
+ const char *id,
+ const char *option);
+GDK_AVAILABLE_IN_3_22
+const char *gtk_file_chooser_native_get_choice (GtkFileChooserNative *self,
+ const char *id);
+
G_END_DECLS
diff --git a/gtk/gtkfilechoosernativeprivate.h b/gtk/gtkfilechoosernativeprivate.h
index d2eedba..9a932ec 100644
--- a/gtk/gtkfilechoosernativeprivate.h
+++ b/gtk/gtkfilechoosernativeprivate.h
@@ -23,6 +23,14 @@
G_BEGIN_DECLS
+typedef struct {
+ char *id;
+ char *label;
+ char **options;
+ char **option_labels;
+ char *selected;
+} GtkFileChooserNativeChoice;
+
struct _GtkFileChooserNative
{
GtkNativeDialog parent_instance;
@@ -36,6 +44,7 @@ struct _GtkFileChooserNative
GFile *current_folder;
GFile *current_file;
char *current_name;
+ GSList *choices;
/* Fallback mode */
GtkWidget *dialog;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]