How to clear/replace filter list in File Chooser?



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?

What I want to do, is to set up a file chooser that has exactly two filters at any given time - one that allows selection of any file, and one that matches files based on a given list of extensions. This set of extensions may change between each time the file chooser is used (based on configuration of other parts of the GUI), so I may need to reset and set the filter list several times throughout the lifetime of the FileChooser object. (The same chooser for multiple file open operations.)

To be able to do this, I've written the following method (in a class derived from FileChooserDialog.) The problem is, it doesn't quite work. It is OK the first time around, but if I call it again in order to change the extension list, the program dumps core.

So, can anyone tell me what is wrong here?

Oh, and before anyone suggests it: I realise that I might "fix" the problem by creating a new FileChooserDialog every time instead of trying to reuse the same, of course, but I'd really like to know why exactly the below approach fails.

void FileSelection::setValidExtensions(const std::vector<std::string> &validExtensions, const std::string &filterId)
{
 std::vector<const Gtk::FileFilter*> filters=list_filters();
for(std::vector<const Gtk::FileFilter*>::iterator f=filters.begin(); f!=filters.end(); f++) {
   remove_filter(*(*f));
 }
Gtk::FileFilter allFilter;

 allFilter.add_pattern("*");
 allFilter.set_name("All Files");

 add_filter(allFilter);
if(!validExtensions.empty()) {
   Gtk::FileFilter extFilter;
   std::string extName="All valid (";
for(std::vector<std::string>::const_iterator p=validExtensions.begin();
	p!=validExtensions.end(); p++) {
     if(!p->empty()) {
	std::string pattern="*";
	if((*p)[0]!='.')
	  pattern+=".";
	pattern+=*p;
	extFilter.add_pattern(pattern);
	
	extName+=pattern;
	extName+=",";
     }
   }
   *extName.rbegin()=')';	// Replace ',' after last item with ')'

   if(filterId.empty())
     extFilter.set_name(extName);
   else
     extFilter.set_name(filterId);
   add_filter(extFilter);
set_filter(extFilter);
 }
}





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