X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=gtk%2Fgtkfilefilter.c;h=5b060960c07e1848439fb501d02d9a02ac87cbb7;hb=bda5987335b8c7828ebf1d289a91accfe2e74dbe;hp=f64c5761bfcef110aee3b3e4446b2abbd804da18;hpb=eee7bb2b2ec87081b16d8a708853c9396c7695ec;p=~andy%2Fgtk
diff --git a/gtk/gtkfilefilter.c b/gtk/gtkfilefilter.c
index f64c5761b..5b060960c 100644
--- a/gtk/gtkfilefilter.c
+++ b/gtk/gtkfilefilter.c
@@ -13,20 +13,69 @@
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see .
+ */
+
+/**
+ * SECTION:gtkfilefilter
+ * @Short_description: A filter for selecting a file subset
+ * @Title: GtkFileFilter
+ * @see_also: #GtkFileChooser
+ *
+ * A GtkFileFilter can be used to restrict the files being shown in a
+ * #GtkFileChooser. Files can be filtered based on their name (with
+ * gtk_file_filter_add_pattern()), on their mime type (with
+ * gtk_file_filter_add_mime_type()), or by a custom filter function
+ * (with gtk_file_filter_add_custom()).
+ *
+ * Filtering by mime types handles aliasing and subclassing of mime
+ * types; e.g. a filter for text/plain also matches a file with mime
+ * type application/rtf, since application/rtf is a subclass of
+ * text/plain. Note that #GtkFileFilter allows wildcards for the
+ * subtype of a mime type, so you can e.g. filter for image/*.
+ *
+ * Normally, filters are used by adding them to a #GtkFileChooser,
+ * see gtk_file_chooser_add_filter(), but it is also possible
+ * to manually use a filter on a file with gtk_file_filter_filter().
+ *
+ *
+ * GtkFileFilter as GtkBuildable
+ *
+ * The GtkFileFilter implementation of the GtkBuildable interface
+ * supports adding rules using the <mime-types>, <patterns> and
+ * <applications> elements and listing the rules within. Specifying
+ * a <mime-type> or <pattern> is the same
+ * as calling gtk_recent_filter_add_mime_type() or gtk_recent_filter_add_pattern()
+ *
+ *
+ * A UI definition fragment specifying GtkFileFilter rules
+ *
+ *
+ * text/plain
+ * image/*
+ *
+ *
+ * *.txt
+ * *.png
+ *
+ *
+ * ]]>
+ *
+ *
+ *
*/
#include "config.h"
#include
+#include
+
#include "gtkfilefilter.h"
+#include "gtkbuildable.h"
#include "gtkintl.h"
#include "gtkprivate.h"
-#include "gtkalias.h"
-
typedef struct _GtkFileFilterClass GtkFileFilterClass;
typedef struct _FilterRule FilterRule;
@@ -43,13 +92,13 @@ typedef enum {
struct _GtkFileFilterClass
{
- GtkObjectClass parent_class;
+ GInitiallyUnownedClass parent_class;
};
struct _GtkFileFilter
{
- GtkObject parent_instance;
-
+ GInitiallyUnowned parent_instance;
+
gchar *name;
GSList *rules;
@@ -76,7 +125,22 @@ struct _FilterRule
static void gtk_file_filter_finalize (GObject *object);
-G_DEFINE_TYPE (GtkFileFilter, gtk_file_filter, GTK_TYPE_OBJECT)
+static void gtk_file_filter_buildable_init (GtkBuildableIface *iface);
+static gboolean gtk_file_filter_buildable_custom_tag_start (GtkBuildable *buildable,
+ GtkBuilder *builder,
+ GObject *child,
+ const gchar *tagname,
+ GMarkupParser *parser,
+ gpointer *data);
+static void gtk_file_filter_buildable_custom_tag_end (GtkBuildable *buildable,
+ GtkBuilder *builder,
+ GObject *child,
+ const gchar *tagname,
+ gpointer *data);
+
+G_DEFINE_TYPE_WITH_CODE (GtkFileFilter, gtk_file_filter, G_TYPE_INITIALLY_UNOWNED,
+ G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
+ gtk_file_filter_buildable_init))
static void
gtk_file_filter_init (GtkFileFilter *object)
@@ -129,6 +193,155 @@ gtk_file_filter_finalize (GObject *object)
G_OBJECT_CLASS (gtk_file_filter_parent_class)->finalize (object);
}
+/*
+ * GtkBuildable implementation
+ */
+static void
+gtk_file_filter_buildable_init (GtkBuildableIface *iface)
+{
+ iface->custom_tag_start = gtk_file_filter_buildable_custom_tag_start;
+ iface->custom_tag_end = gtk_file_filter_buildable_custom_tag_end;
+}
+
+typedef enum {
+ PARSE_MIME_TYPES,
+ PARSE_PATTERNS
+} ParserType;
+
+typedef struct {
+ GtkFileFilter *filter;
+ ParserType type;
+ GString *string;
+ gboolean parsing;
+} SubParserData;
+
+static void
+parser_start_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ const gchar **names,
+ const gchar **values,
+ gpointer user_data,
+ GError **error)
+{
+ SubParserData *parser_data = (SubParserData*)user_data;
+
+ if (strcmp (element_name, "mime-types") == 0)
+ return;
+ else if (strcmp (element_name, "mime-type") == 0)
+ {
+ parser_data->parsing = TRUE;
+ return;
+ }
+ else if (strcmp (element_name, "patterns") == 0)
+ return;
+ else if (strcmp (element_name, "pattern") == 0)
+ {
+ parser_data->parsing = TRUE;
+ return;
+ }
+ else
+ g_warning ("Unsupported tag for GtkFileFilter: %s\n", element_name);
+}
+
+static void
+parser_text_element (GMarkupParseContext *context,
+ const gchar *text,
+ gsize text_len,
+ gpointer user_data,
+ GError **error)
+{
+ SubParserData *parser_data = (SubParserData*)user_data;
+
+ if (parser_data->parsing)
+ g_string_append_len (parser_data->string, text, text_len);
+}
+
+static void
+parser_end_element (GMarkupParseContext *context,
+ const gchar *element_name,
+ gpointer user_data,
+ GError **error)
+{
+ SubParserData *parser_data = (SubParserData*)user_data;
+
+ if (parser_data->string)
+ {
+ switch (parser_data->type)
+ {
+ case PARSE_MIME_TYPES:
+ gtk_file_filter_add_mime_type (parser_data->filter, parser_data->string->str);
+ break;
+ case PARSE_PATTERNS:
+ gtk_file_filter_add_pattern (parser_data->filter, parser_data->string->str);
+ break;
+ default:
+ break;
+ }
+ }
+
+ g_string_set_size (parser_data->string, 0);
+ parser_data->parsing = FALSE;
+}
+
+static const GMarkupParser sub_parser =
+ {
+ parser_start_element,
+ parser_end_element,
+ parser_text_element,
+ };
+
+static gboolean
+gtk_file_filter_buildable_custom_tag_start (GtkBuildable *buildable,
+ GtkBuilder *builder,
+ GObject *child,
+ const gchar *tagname,
+ GMarkupParser *parser,
+ gpointer *data)
+{
+ SubParserData *parser_data = NULL;
+
+ if (strcmp (tagname, "mime-types") == 0)
+ {
+ parser_data = g_slice_new0 (SubParserData);
+ parser_data->string = g_string_new ("");
+ parser_data->type = PARSE_MIME_TYPES;
+ parser_data->filter = GTK_FILE_FILTER (buildable);
+
+ *parser = sub_parser;
+ *data = parser_data;
+ }
+ else if (strcmp (tagname, "patterns") == 0)
+ {
+ parser_data = g_slice_new0 (SubParserData);
+ parser_data->string = g_string_new ("");
+ parser_data->type = PARSE_PATTERNS;
+ parser_data->filter = GTK_FILE_FILTER (buildable);
+
+ *parser = sub_parser;
+ *data = parser_data;
+ }
+
+ return parser_data != NULL;
+}
+
+static void
+gtk_file_filter_buildable_custom_tag_end (GtkBuildable *buildable,
+ GtkBuilder *builder,
+ GObject *child,
+ const gchar *tagname,
+ gpointer *data)
+{
+ if (strcmp (tagname, "mime-types") == 0 ||
+ strcmp (tagname, "patterns") == 0)
+ {
+ SubParserData *parser_data = (SubParserData*)data;
+
+ g_string_free (parser_data->string, TRUE);
+ g_slice_free (SubParserData, parser_data);
+ }
+}
+
+
/**
* gtk_file_filter_new:
*
@@ -188,7 +401,7 @@ gtk_file_filter_set_name (GtkFileFilter *filter,
*
* Since: 2.4
**/
-G_CONST_RETURN gchar *
+const gchar *
gtk_file_filter_get_name (GtkFileFilter *filter)
{
g_return_val_if_fail (GTK_IS_FILE_FILTER (filter), NULL);
@@ -421,6 +634,3 @@ gtk_file_filter_filter (GtkFileFilter *filter,
return FALSE;
}
-
-#define __GTK_FILE_FILTER_C__
-#include "gtkaliasdef.c"