Question about refs on filters for gtk_file_chooser



I wanted to have a filter on my gtk_file_chooser which examines the
files so as to only display files with certain contents.

Noting that gtk_file_filter wasn't dependent on any particular instance
of a gtk_file_chooser I thought I could possibly just allocate it once
thus:

static  GtkFileFilter  *isit_ok_filt = 0;
/* etc */

fsb = gtk_file_chooser_dialog_new("Select file", GTK_WINDOW(toplevel),
GTK_FILE_CHOOSER_ACTION_OPEN, etc
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fsb),
starting_directory);
if  (!isit_ok_filt)  {
        isit_ok_filt = gtk_file_filter_new();
        gtk_file_filter_set_name(isit_ok_filt, "Suitable files");
        gtk_file_filter_add_custom(isit_ok_filt, GTK_FILE_FILTER_FILENAME,
(GtkFileFilterFunc) isit_ok_func, NULL, NULL);
}
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(fsb), isit_ok_filt);

This does exactly the right thing - once - but the filter gets
deallocated when "fsb" is destroyed - however it seems to be OK and
works perfectly if I add the line

g_object_ref(G_OBJECT(isit_ok_filt));

Is all this safe? Or should I actually allocate the filter each time?

I can see it all might be different if I had different stuff as the last
two parameters of "gtk_file_filter_add_custom" rather than NULL but all
"isit_ok_func" does is to open the file and look around and return "yea"
or "nay".

My reason for checking is mainly that it seems to me to be inconsistent
with adding a "List Store" to a Tree View Widget wherein the list store
doesn't get deallocated when the tree view is destroyed, you have to
explicitly "g_object_unref" it.


John Collins Xi Software Ltd www.xisl.com



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]