[gtk+/portal] Add check button support to GtkFileChooserNative
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/portal] Add check button support to GtkFileChooserNative
- Date: Tue, 28 Jun 2016 00:57:17 +0000 (UTC)
commit 5e35a9eb49f68c145c059e852b7483c5b0c433cd
Author: Matthias Clasen <mclasen redhat com>
Date: Mon Jun 27 20:56:15 2016 -0400
Add check button support to GtkFileChooserNative
This commit adds API for adding check buttons to a native file chooser,
and getting the selected value back in ::response.
gtk/gtkfilechoosernative.c | 99 ++++++++++++++++++++++++++++++++++++++++----
gtk/gtkfilechoosernative.h | 14 ++++++
2 files changed, 105 insertions(+), 8 deletions(-)
---
diff --git a/gtk/gtkfilechoosernative.c b/gtk/gtkfilechoosernative.c
index 81ac0c9..0c100bc 100644
--- a/gtk/gtkfilechoosernative.c
+++ b/gtk/gtkfilechoosernative.c
@@ -363,16 +363,17 @@ gtk_file_chooser_native_add_choice (GtkFileChooserNative *self,
return;
}
- g_assert (g_strv_length (options) == g_strv_length (option_labels));
+ g_assert ((options == NULL && option_labels == NULL) ||
+ g_strv_length ((char **)options) == g_strv_length ((char **)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->options = g_strdupv ((char **)options);
+ choice->option_labels = g_strdupv ((char **)option_labels);
choice->selected = NULL;
- self->choices = g_slist_prepend (self->choices, choice);
+ self->choices = g_slist_append (self->choices, choice);
}
/**
@@ -396,7 +397,7 @@ gtk_file_chooser_native_remove_choice (GtkFileChooserNative *self,
return;
}
- g_slist_remove (self->choices, choice);
+ self->choices = g_slist_remove (self->choices, choice);
gtk_file_chooser_native_choice_free (choice);
}
@@ -404,7 +405,7 @@ gtk_file_chooser_native_remove_choice (GtkFileChooserNative *self,
/**
* gtk_file_chooser_native_set_choice:
* @self: a #GtkFileChooserNative
- * @id: the ID of the choice to remove
+ * @id: the ID of the choice to set
* @selected: the ID of the option to select
*
* Selects an option in a 'choice' that has been added
@@ -425,6 +426,13 @@ gtk_file_chooser_native_set_choice (GtkFileChooserNative *self,
return;
}
+ if ((choice->options && !g_strv_contains ((const char *const*)choice->options, selected)) ||
+ (!choice->options && !g_str_equal (selected, "true") && !g_str_equal (selected, "false")))
+ {
+ g_warning ("Not a valid option for %s: %s", id, selected);
+ return;
+ }
+
g_free (choice->selected);
choice->selected = g_strdup (selected);
}
@@ -432,7 +440,7 @@ gtk_file_chooser_native_set_choice (GtkFileChooserNative *self,
/**
* gtk_file_chooser_native_get_choice:
* @self: a #GtkFileChooserNative
- * @id: the ID of the choice to remove
+ * @id: the ID of the choice to get
*
* Gets the currently selected option in the 'choice' with the given ID.
*
@@ -454,6 +462,81 @@ gtk_file_chooser_native_get_choice (GtkFileChooserNative *self,
return choice->selected;
}
+/**
+ * gtk_file_chooser_native_add_option:
+ * @self: a #GtkFileChooserNative
+ * @id: id for the added option
+ * @label: user-visible label for the added option
+ *
+ * Adds a boolean 'option' to the native file chooser. This is typically implemented
+ * as a check button. You can set the value using gtk_file_chooser_native_set_option()
+ * 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_option().
+ *
+ * Compare gtk_file_chooser_set_extra_widget().
+ *
+ * Since: 3.22
+ */
+void
+gtk_file_chooser_native_add_option (GtkFileChooserNative *self,
+ const char *id,
+ const char *label)
+{
+ gtk_file_chooser_native_add_choice (self, id, label, NULL, NULL);
+}
+
+/**
+ * gtk_file_chooser_native_remove_option:
+ * @self: a #GtkFileChooserNative
+ * @id: the ID of the option to remove
+ *
+ * Removes an 'option' that has been added with gtk_file_chooser_native_add_option().
+ *
+ * Since: 3.22
+ */
+void
+gtk_file_chooser_native_remove_option (GtkFileChooserNative *self,
+ const char *id)
+{
+ gtk_file_chooser_native_remove_option (self, id);
+}
+
+/**
+ * gtk_file_chooser_native_set_option:
+ * @self: a #GtkFileChooserNative
+ * @id: the ID of the option to set
+ * @value: the new value for the option
+ *
+ * Sets the value of an 'option' that has been added
+ * with gtk_file_chooser_native_add_option().
+ *
+ * Since: 3.22
+ */
+void
+gtk_file_chooser_native_set_option (GtkFileChooserNative *self,
+ const char *id,
+ gboolean value)
+{
+ gtk_file_chooser_native_set_choice (self, id, value ? "true" : "false");
+}
+
+/**
+ * gtk_file_chooser_native_get_option:
+ * @self: a #GtkFileChooserNative
+ * @id: the ID of the option to get
+ *
+ * Gets the value of the 'option' with the given ID.
+ *
+ * Returns: the value of the 'option'
+ * Since: 3.22
+ */
+gboolean
+gtk_file_chooser_native_get_option (GtkFileChooserNative *self,
+ const char *id)
+{
+ return g_str_equal (gtk_file_chooser_native_get_choice (self, id), "true");
+}
+
static void
gtk_file_chooser_native_set_property (GObject *object,
guint prop_id,
@@ -517,7 +600,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_slist_free_full (self->choices, (GDestroyNotify)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 97cb565..384a814 100644
--- a/gtk/gtkfilechoosernative.h
+++ b/gtk/gtkfilechoosernative.h
@@ -66,6 +66,20 @@ void gtk_file_chooser_native_set_choice (GtkFileChooserNative *sel
GDK_AVAILABLE_IN_3_22
const char *gtk_file_chooser_native_get_choice (GtkFileChooserNative *self,
const char *id);
+GDK_AVAILABLE_IN_3_22
+void gtk_file_chooser_native_add_option (GtkFileChooserNative *self,
+ const char *id,
+ const char *label);
+GDK_AVAILABLE_IN_3_22
+void gtk_file_chooser_native_remove_option (GtkFileChooserNative *self,
+ const char *id);
+GDK_AVAILABLE_IN_3_22
+void gtk_file_chooser_native_set_option (GtkFileChooserNative *self,
+ const char *id,
+ gboolean value);
+GDK_AVAILABLE_IN_3_22
+gboolean gtk_file_chooser_native_get_option (GtkFileChooserNative *self,
+ const char *id);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]