Programmatic filtering in OpenDialog possible? lazarus / 31-LCL ---------------------------------------------------------------------- outlyer · 2015-03-10 02:51 Hi all, I've been unable to find anything about this online. In short, I'm looking for a way to filter files in an "open file" dialog by criteria other than file extension. Is that possible? More details: I'd like to open files in a format that has no standardised extension, so my idea was to pre-filter the list of files based on their contents (the first few bytes give away the format). Apparently Delphi's TOpenDialog has an OnIncludeItem event handler that could serve exactly that purpose. But Lazarus doesn't appear to have that (although it does have the associated option ofEnableIncludeNotify, but not actually used as far as I can see). Can this be done in some way? ---------------------------------------------------------------------- Ocye · 2015-03-10 13:28 You can use the FileName property. For instance OpenDialog1.FileName:='MyFile?.*' But it sounds not like a good idea to abuse a filedialog in this way. Users expect a standard dialog, and other OS work differently anyway (Linux without extensions, MacOS dims filtered items). You should rather rethink your workflow. ---------------------------------------------------------------------- outlyer · 2015-03-10 15:46 Thanks for your reply, but I don't understand your reasoning. Maybe I didn't explain clearly what my goal is. Using a FileName pattern instead of extension filters doesn't get me any closer to my target. It doesn't act as a filter either, just as a file name "suggestion". My main target is Linux BTW. I'm looking to filter files based on contents instead of filename, which is not so weird on Linux, and I'm trying to use a standard dialog. There's no abuse I can see there  Not to mention as far as I can tell Delphi provides a way to filter programmatically. In the eyes of the user, that dialog would act exactly as expected, only the default list of files would have filtered (or dimmed, or whatever is the standard in each system) irrelevant files, no matter what their name or extension is. The program (not mine) that saves those files is written in GTK and uses a MIME-type filter (which GTK allows to do) in its open file dialog. ---------------------------------------------------------------------- ChrisF · 2015-03-10 16:57 Sorry, I don't know if it's possible with Linux. But even for Windows, having the same functionality for the Lazarus version of the TOpenDialog class requires some additional code. Delphi (i.e. Windows) uses the CDN_INCLUDEITEM notification message (https://msdn.microsoft.com/en- us/library/windows/desktop/ms646862%28v=vs.85%29.aspx) inside a hook procedure for the "old" (i.e. before Windows Vista) style of its dialog boxes. And probably uses the IShellItemFilter::IncludeItem method (https://msdn.microsoft.com/fr- fr/library/windows/apps/xaml/bb761088.aspx) for the "new" (i.e. Vista+) one. ---------------------------------------------------------------------- JuhaManninen · 2015-03-10 17:10 I think it should be reported as a feature request. The implementation does not depend on OS (Linux) but on widgetset (GTK2, QT, etc.). I don't know how well the dialogs of each widgetset support this filtering. A patch that implements it for even one widgetset would be nice. The other widgetsets can be added later. ---------------------------------------------------------------------- Ocye · 2015-03-11 12:43 Quote from: outlyer on March 10, 2015, 04:46:25 pmMy main target is Linux BTW. I'm looking to filter files based on contents instead of filename, which is not so weird on Linux, and I'm trying to use a standard dialog.So with content you mean, for instance, all xml files (or rather text files with markup encoding) within a directory irrespectively from the filename? I've never seen such a dialog. And as a user I'd expect a dialog with filter option, maybe fuzzy in terms of 'all picture' which is filter='*.png|*.bmp|*.jpg', but with the opportunity to show all files. Wouldn't your users be confused when content is only shown partially? That's why I think your ides is an "abuse". Of course, it depends on scope, audience, and expertise. Alternatively you could create your own file dialog that is clearly discriminated from the standard dialog. ---------------------------------------------------------------------- mm7 · 2015-03-11 14:31 I would agree with outlyer. Such filter would be helpful. Though there is a convention to name JPEG files with using .jpg "extension", it is not required. Many "formatted" files have a "magic word" inside that tells about their format better than the extension tells. So, a filter would look 'Image files', 'JPEG, PNG, BMP, GIF, TIFF...'. ---------------------------------------------------------------------- outlyer · 2015-03-11 14:40 Quote from: ChrisF on March 10, 2015, 05:57:10 pmSorry, I don't know if it's possible with Linux. But even for Windows, having the same functionality for the Lazarus version of the TOpenDialog class requires some additional code. [...] Yes, it definitely would need considerable extra code, and as you point out, it looks like it even works in different ways across different versions of Windows. Quote from: JuhaManninen on March 10, 2015, 06:10:05 pmI think it should be reported as a feature request. The implementation does not depend on OS (Linux) but on widgetset (GTK2, QT, etc.). I don't know how well the dialogs of each widgetset support this filtering. A patch that implements it for even one widgetset would be nice. The other widgetsets can be added later. Good point. Although I'm not sure either wether that's a feature that every widgetset supports  (which may mean it's undoable in the LCL I guess). Quote from: Ocye on March 11, 2015, 01:43:26 pmSo with content you mean, for instance, all xml files (or rather text files with markup encoding) within a directory irrespectively from the filename? I've never seen such a dialog. And as a user I'd expect a dialog with filter option, maybe fuzzy in terms of 'all picture' which is filter='*.png|*.bmp|*.jpg', but with the opportunity to show all files. Wouldn't your users be confused when content is only shown partially? That's why I think your ides is an "abuse". Of course, it depends on scope, audience, and expertise. Alternatively you could create your own file dialog that is clearly discriminated from the standard dialog. With content I mean peeking into the file, using file magic so to speak, in the XML example I might read the first five bytes and decide it's being included in the list if they're