[Fwd: Re: How to clear/replace filter list in File Chooser?]




--- Begin Message ---
On 2/14/07, Toralf Lund <toralf procaptura com> wrote:
Toralf Lund wrote:
> Does anyone here know how to clear the list of user selectable filters
> from a file selector and/or replace the list with a new one? Or
> differently put, can someone spot the mistake(s) in the code included below?
>
> [ ... ]
>
I think I can answer that question myself, now. The problem is that the
filter is set up as a temporary object (on the stack), where it
apparently must be allocated - because add_filter() stores a reference
to it. In other words,
>
>   if(!validExtensions.empty()) {
>     Gtk::FileFilter extFilter;
>
>     ...
>
>
>     add_filter(extFilter);
>
>     set_filter(extFilter);
>   }
> }
>
should be changed to
>
>   if(!validExtensions.empty()) {
>     Gtk::FileFilter *extFilter=new Gtk::FileFilter;
>
>     ...
>
>
>     add_filter(*extFilter);
>
>     set_filter(*extFilter);
>   }
> }
I had somehow concluded that add_filter would copy data rather than
store a reference. Not sure why, but it seems clear now that I was wrong.


It is easy to make that assumption seeing as its a const reference. I
would've done the same thing.

What strikes me as odd, though, is that the application wouldn't crash
right away (as the temporary Gtk::FileFilter was destroyed), but worked
just fine until it tried to remove the filters in order to set up a new
list at a much later stage.


Is it possible you had something along the lines of:

void
open_file()
{
  // init non local dialog
  // create local filter
  // add filter
  // use dialog
}

In which case the added filter is still in scope and would work until
you tried to access the reference after the local filter was destroyed
(ie, when you were trying to remove the filters).

I'm also wondering why add_filter() takes a const reference when the
FileChooser actually takes ownership of the object and may destroy it
automatically, according to the docs (I also overlooked that bit of
information earlier ;-() Seems like most adds in Gtk uses non-const
references in such cases...


From the docs:
Note that the chooser takes ownership of the filter, so you have to
ref and sink it if you want to keep a reference.

Is the line I think you're referring to.  The 'ref and sink it' seemed
rather un-C++ like if that makes any sense.  I checked the Gtk docs
and that line is copied verbatim from:
http://developer.gnome.org/doc/API/2.0/gtk/GtkFileChooser.html#gtk-file-chooser-add-filter

Which makes me think that maybe its not quite applicable here.

- Toralf

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list


Looking at the Gtk api there's also no unwrapped clear_filters method
as I was hoping. So the question is, what exactly is the proper method
for adding and removing filters?  I'm assuming that since the method
Toraf came up with doesn't segfault, that its probably correct.
Perhaps a patch to the docs is in order?

HTH,
Paul Davis

--- End Message ---


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