Recent

Author Topic: Programmatic filtering in OpenDialog possible?  (Read 9039 times)

outlyer

  • Newbie
  • Posts: 4
Programmatic filtering in OpenDialog possible?
« on: March 10, 2015, 03:51:19 am »
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

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: Programmatic filtering in OpenDialog possible?
« Reply #1 on: March 10, 2015, 02:28:05 pm »
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.
Lazarus 1.7 (SVN) FPC 3.0.0

outlyer

  • Newbie
  • Posts: 4
Re: Programmatic filtering in OpenDialog possible?
« Reply #2 on: March 10, 2015, 04:46:25 pm »
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

  • Hero Member
  • *****
  • Posts: 542
Re: Programmatic filtering in OpenDialog possible?
« Reply #3 on: March 10, 2015, 05:57:10 pm »
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.
« Last Edit: March 10, 2015, 06:02:17 pm by ChrisF »

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4650
  • I like bugs.
Re: Programmatic filtering in OpenDialog possible?
« Reply #4 on: March 10, 2015, 06:10:05 pm »
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.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: Programmatic filtering in OpenDialog possible?
« Reply #5 on: March 11, 2015, 01:43:26 pm »
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.
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.
Lazarus 1.7 (SVN) FPC 3.0.0

mm7

  • Full Member
  • ***
  • Posts: 231
  • PDP-11 RSX Pascal, Turbo Pascal, Delphi, Lazarus
Re: Programmatic filtering in OpenDialog possible?
« Reply #6 on: March 11, 2015, 03:31:58 pm »
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

  • Newbie
  • Posts: 4
Re: Programmatic filtering in OpenDialog possible?
« Reply #7 on: March 11, 2015, 03:40:28 pm »
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.
[...]

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.

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.

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).

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.

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 <?xml.

Look at Revelation, for example, to see such a dialog; but you won't notice any difference with a traditional dialog. It has two filters: "Revelation files", which shows files with the appropriate MIME type, no matter what their extension is, and "All files", which of course shows all files.

Continuing with your examples, imagine an "all picture" filter that was 'filter=*.*', when such a filter was selected, the OpenDialog would get a list of all the files in the directory, and would be able to allow or reject each file based on their contents (e.g. if the JPEG has extension JPE or JPEG -both valid-, they'd also be included). Of course there would be an "all files" filter that would show every single file, everyone expects that ;)

EDIT:

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...'.

Well, you beated me to that answer :D
« Last Edit: March 11, 2015, 03:45:20 pm by outlyer »

piola

  • Full Member
  • ***
  • Posts: 157
  • Lazarus 2.2, 64bit on Windows 8.1 x64
Re: Programmatic filtering in OpenDialog possible?
« Reply #8 on: November 13, 2025, 03:12:50 pm »
I know this topic is 10 years old, but as far as I can see, it is still unsolved.

I'm also voting for the implementation of the OnIncludeItem event because it seems to be the only way to filter for files without extension (at least on Windows).

Bart

  • Hero Member
  • *****
  • Posts: 5640
    • Bart en Mariska's Webstek
Re: Programmatic filtering in OpenDialog possible?
« Reply #9 on: November 13, 2025, 04:00:49 pm »
You can file a feature request in the bugtracker.
Please then also provide a link to the relevant section in Delphi's DocWiki.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 7379
Re: Programmatic filtering in OpenDialog possible?
« Reply #10 on: November 13, 2025, 11:30:59 pm »
ShellTreeview and ShellListView both have that event that allows you to filter and do whatever you need .

The only true wisdom is knowing you know nothing

n7800

  • Hero Member
  • *****
  • Posts: 583
  • Lazarus IDE contributor
    • GitLab profile
Re: Programmatic filtering in OpenDialog possible?
« Reply #11 on: November 14, 2025, 06:34:51 pm »

jamie

  • Hero Member
  • *****
  • Posts: 7379
Re: Programmatic filtering in OpenDialog possible?
« Reply #12 on: November 14, 2025, 10:57:56 pm »
No, do not do what Delphi did, lose a lot of old-time users that still needs to support older OS.s

I looked at that request, and it appears some don't care.

Please don't use simple features like that and expect is to be great, that will just kill off older Systems in a single swoop.

I still write code that is expected to run on 2k and up. yes, believe or not, we have industrial systems still running
w2k, which is why I used Lazarus.

  There is no advantage of using that newer feature when it can be done already as is, with a little leg work.

Jamie






The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5640
    • Bart en Mariska's Webstek
Re: Programmatic filtering in OpenDialog possible?
« Reply #13 on: November 14, 2025, 11:07:05 pm »
If Delphi has it, and we can implement it, there really isn't a good reason not to.
You're not obliged to use it.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 7379
Re: Programmatic filtering in OpenDialog possible?
« Reply #14 on: November 14, 2025, 11:27:29 pm »
Sure, but the request suggested killing off older support in favor of this.
But Delphi just killed off a whole bunch of support with its latest addition.

We use that too at work and now the last update will no longer build apps that run on Xp, etc. I don't think they will even run on Vista anymore.

  It would a shame to see the same happen here.

I have no issue with this if it will be ignored in the unsupported systems.

Jamie
« Last Edit: November 14, 2025, 11:29:56 pm by jamie »
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018