Advanced usage: GTK+ 2 Reference Manual

## Advanced usage

The GtkRecentChooser widgets might display items sorted and filtered, either with already supplied or custom sorting and filtering functions. The biggest difference from the EggRecentView widgets in EggRecent is that the GtkRecentChooser widgets will use their own copy of the list and will apply the sorting and filtering functions only on the copy; this allows the creation of many viewers with a single controller, like using many GtkTreeView with a single GtkTreeModel instance.

Available sorting methods are:

    1
    2
    3
    4
    5
    6
    7
    8
    /* no sorting */
        gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_NONE);

    /* most recently used first */
        gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_MRU);

        /* most recently used last */
        gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_LRU);

You can create your own sorting function, and the use the GTK_RECENT_SORT_CUSTOM method:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    /* custom sorting function, based on the registration count
     * (most used first)
     */
    static void
    sort_by_usage_count (GtkRecentInfo *a,
                                 GtkRecentInfo *b,
                                 gpointer       data)
            {
      gint count_a, count_b;

      count_a = count_b = 0;

      if (gtk_recent_info_has_application (a, APP_NAME))
        gtk_recent_info_get_application_info (a, APP_NAME, NULL, &count_a, NULL);

      if (gtk_recent_info_has_application (b, APP_NAME))
        gtk_recent_info_get_application_info (b, APP_NAME, NULL, &count_b, NULL);

      return count_a < count_b;
    }

    ...

      /* set custom sorting and set the custom sorting function */
      gtk_recent_chooser_set_sort_type (GTK_RECENT_CHOOSER (chooser),
                                        GTK_RECENT_SORT_CUSTOM);
      gtk_recent_chooser_set_sort_func (GTK_RECENT_CHOOSER,
                                        sort_by_usage_count,
                        NULL, /* sort function data */
                        NULL  /* destroy notify for the data */);

Filtering is done using the GtkRecentFilter object, similar to the GtkFileFilter object used by the GtkFileChooser widgets. The GtkRecentFilter object has a set of pre-defined options based on the meta-data exposed by the GtkRecentInfo object. It also allows custom filtering function:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    GtkRecentFilter *filter;

        filter = gtk_recent_filter_new ();

        /* set the user visible name of the filter */
        gtk_recent_filter_set_name (filter, "Since Last Month");

        /* set the maximum age of a recently used document */
        gtk_recent_filter_set_age (filter, 31);

        /* the chooser takes the ownership of the object */
        gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (chooser), filter);

        /* set the currently used filter */
        gtk_recent_chooser_set_filter (GTK_RECENT_CHOOSER (chooser), filter);

        filter = gtk_recent_filter_new ();
        gtk_recent_filter_set_name (filter, "Every text file");
        gtk_recent_filter_set_mime_type (filter, "text/plain");

        gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (chooser), filter);

The GtkRecentChooserWidget and GtkRecentChooserDialog widgets allow multiple filters and the selection of an appropriate one; the GtkRecentChooserMenu widget allows just a single filter object.