class Gtk::FileFilter

Overview

Gtk::FileFilter filters files by name or mime type.

Gtk::FileFilter can be used to restrict the files being shown in a Gtk::FileChooser. Files can be filtered based on their name (with Gtk::FileFilter#add_pattern or Gtk::FileFilter#add_suffix) or on their mime type (with Gtk::FileFilter#add_mime_type).

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 Gtk::FileFilter allows wildcards for the subtype of a mime type, so you can e.g. filter for image/*.

Normally, file filters are used by adding them to a Gtk::FileChooser (see Gtk::FileChooser#add_filter), but it is also possible to manually use a file filter on any Gtk::FilterListModel containing GFileInfo objects.

Gtk::FileFilter as Gtk::Buildable

The Gtk::FileFilter implementation of the Gtk::Buildable interface supports adding rules using the <mime-types> and <patterns> and <suffixes> elements and listing the rules within. Specifying a <mime-type> or <pattern> or <suffix> has the same effect as as calling Gtk::FileFilter#add_mime_type or Gtk::FileFilter#add_pattern or Gtk::FileFilter#add_suffix.

An example of a UI definition fragment specifying Gtk::FileFilter rules:

WARNING ⚠️ The following code is in xml ⚠️

<object class="Gtk::FileFilter">
  <property name="name" translatable="yes">Text and Images</property>
  <mime-types>
    <mime-type>text/plain</mime-type>
    <mime-type>image/ *</mime-type>
  </mime-types>
  <patterns>
    <pattern>*.txt</pattern>
  </patterns>
  <suffixes>
    <suffix>png</suffix>
  </suffixes>
</object>

Included Modules

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/file_filter.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Gtk::Buildable

buildable_id : String | Nil buildable_id, to_unsafe to_unsafe

Constructor methods inherited from module Gtk::Buildable

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gtk::Buildable

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from class Gtk::Filter

==(other : self) ==, changed(change : Gtk::FilterChange) : Nil changed, changed_signal changed_signal, hash(hasher) hash, match(item : GObject::Object) : Bool match, strictness : Gtk::FilterMatch strictness

Constructor methods inherited from class Gtk::Filter

new new

Class methods inherited from class Gtk::Filter

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

==(other : self) ==, bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new : self #

Creates a new Gtk::FileFilter with no rules added to it.

Such a filter doesn’t accept any files, so is not particularly useful until you add rules with Gtk::FileFilter#add_mime_type, Gtk::FileFilter#add_pattern, Gtk::FileFilter#add_suffix or Gtk::FileFilter#add_pixbuf_formats.

To create a filter that accepts any file, use:

WARNING ⚠️ The following code is in c ⚠️

Gtk::FileFilter *filter = gtk_file_filter_new ();
gtk_file_filter_add_pattern (filter, "*");

[View source]
def self.new(*, name : String | Nil = nil) #

[View source]
def self.new_from_gvariant(variant : _) : self #

Deserialize a file filter from a GVariant.

The variant must be in the format produced by Gtk::FileFilter#to_gvariant.


[View source]

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


def add_mime_type(mime_type : String) : Nil #

Adds a rule allowing a given mime type to filter.


[View source]
def add_pattern(pattern : String) : Nil #

Adds a rule allowing a shell style glob to a filter.

Note that it depends on the platform whether pattern matching ignores case or not. On Windows, it does, on other platforms, it doesn't.


[View source]
def add_pixbuf_formats : Nil #

Adds a rule allowing image files in the formats supported by Gdk::Pixbuf.

This is equivalent to calling Gtk::FileFilter#add_mime_type for all the supported mime types.


[View source]
def add_suffix(suffix : String) : Nil #

Adds a suffix match rule to a filter.

This is similar to adding a match for the pattern "*.@suffix".

In contrast to pattern matches, suffix matches are always case-insensitive.


[View source]
def attributes : Enumerable(String) #

Gets the attributes that need to be filled in for the GFileInfo passed to this filter.

This function will not typically be used by applications; it is intended principally for use in the implementation of Gtk::FileChooser.


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


def name : String | Nil #

Gets the human-readable name for the filter.

See Gtk::FileFilter#name=.


[View source]
def name=(value : String) : String #

[View source]
def name=(name : String | Nil) : Nil #

Sets a human-readable name of the filter.

This is the string that will be displayed in the file chooser if there is a selectable list of filters.


[View source]
def to_gvariant : GLib::Variant #

Serialize a file filter to an a{sv} variant.


[View source]