Recent

Author Topic: Have TSaveDialog change selected filename file extension  (Read 4000 times)

MISV

  • Hero Member
  • *****
  • Posts: 783
Have TSaveDialog change selected filename file extension
« on: August 29, 2019, 04:29:43 pm »
Is there any way to configure/use a TSaveDialog to change the selected filename file extension when the selected filter/format is changed by the user?

i.e. I have a default filename "default.csv" and "Comma separated values fil (*.csv)" is also the default selected filter/format

I would like to have the file extension changed whenever a new filter/format is changed - is that possible?

(This is how the dialog behaves on Delphi/Windows/VCL at least which is where I am coming from)


wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Have TSaveDialog change selected filename file extension
« Reply #1 on: August 29, 2019, 11:19:08 pm »
The Open/SaveDialogs have an event OnTypeChange which fires when a different file type is selected in the dialog. If you have a flexible Filter list then you must determine the filter expression which belongs to the new type. Note that, against the usual convention, the FilterIndex begins with the value 1 here (not with 0):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.OpenDialog1TypeChange(Sender: TObject);
  2. var
  3.   sa: TStringArray;
  4.   index: Integer;
  5. begin
  6.   sa := OpenDialog1.Filter.Split('|');
  7.   index := (OpenDialog1.FilterIndex - 1) * 2;
  8.   OpenDialog1.DefaultExt := sa[index];
  9. end;
  10.  
  11. procedure TForm1.Button1Click(Sender: TObject);
  12. begin
  13.   with OpenDialog1 do begin
  14.     Filter := 'CSV files (*.csv)|*.csv|Excel files (*.xls)|*.xls|Open Document Files (*.ods)|*.ods';
  15.     DefaultExt := '.csv';
  16.     FilterIndex := 1;
  17.     FileName := '';
  18.     if Execute then
  19.       Edit1.Text := FileName;
  20.   end;
  21. end;

I tested this works on Windows, and I don't see a reason that it should not work on Mac. The code is correct as long as a single extension is assigned to each file type (i.e. ('*.xls' is ok, but '*.xls;*.xlsx' is not) - you certainly can find the code for multiple extension by yourself.

MISV

  • Hero Member
  • *****
  • Posts: 783
Re: Have TSaveDialog change selected filename file extension
« Reply #2 on: August 29, 2019, 11:28:35 pm »
Thanks -

But I just tried - and it seems *OnTypeChange* does not fire on LCLCocoa when user changes filter (called "Format:" in Mac dialog where you can select between the different choices listed in *Filter*)

I set the event handler runtime (making sure code was executed by setting a breakpoint at the assignment code) - not that it should matter


« Last Edit: August 29, 2019, 11:32:06 pm by MISV »

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Have TSaveDialog change selected filename file extension
« Reply #3 on: August 29, 2019, 11:54:43 pm »
Oh, I did not think of the event - well, a typical widgetset issue... Maybe the event can be added, I don't know, I don't have a Mac. File a feature request in the bugtracker.

MISV

  • Hero Member
  • *****
  • Posts: 783
Re: Have TSaveDialog change selected filename file extension
« Reply #4 on: August 30, 2019, 12:38:26 am »
I have submitted preliminary bug report here: https://bugs.freepascal.org/view.php?id=36024

I hope to later add a complete code demo ready to paste into anywhere.


 

TinyPortal © 2005-2018