Sometimes is filtering by mime type or file name pattern not enough, and you need to define your own method that checks a file for validity. add_custom allows exactly this.
The callback function referred in the callback parameter needs by default one parameter; it's an array of four values containing either null or a string - depending on the settings in flags. The first element is the file name, second is the URI of the file. Third array element is the "display name" that is displayed in the file chooser, and the fourth element is the mime type of the file.
array(4) { [0]=> string(48) "/data/cvs/phpgtk2/test/window_set_icon_list.phpw" [1]=> string(55) "file:///data/cvs/phpgtk2/test/window_set_icon_list.phpw" [2]=> string(25) "window_set_icon_list.phpw" [3]=> string(17) "application/x-php" } |
You may pass any number of user defined parameters after the callback parameter; they get also passed to the callback function.
Your callback needs to return true if the file matches your filter and shall be displayed, false if not.
Example 65. Filtering files by characters
<?php /** * Our file check function. It returns * true whenever a file matches our requirements. */ function fileCheck($info, $char) { $display_name = $info[2]; return strpos($display_name, $char) !== false; } //Create a new file filter $filterA = new GtkFileFilter(); //Set a name so we can recognize it in the file dialog $filterA->set_name('My first custom filter "a"'); /** * Add our filter: * - first the information we need about the files * - second the callback function's name * - the third parameter is normally not needed; * here it's a user-defined parameter that lets us * customize the filter function a bit. */ $filterA->add_custom( Gtk::FILE_FILTER_DISPLAY_NAME | Gtk::FILE_FILTER_MIME_TYPE, 'fileCheck', 'a' ); //Add a second one with different filter parameter $filterB = new GtkFileFilter(); $filterB->set_name('My first custom filter "b"'); $filterB->add_custom( Gtk::FILE_FILTER_DISPLAY_NAME | Gtk::FILE_FILTER_MIME_TYPE, 'fileCheck', 'b' ); //Now add those filters to the file dialog $filedlg = new GtkFileChooserDialog('custom filter demo'); $filedlg->add_filter($filterA); $filedlg->add_filter($filterB); $filedlg->run(); ?> |