IDE: file dialog filter: reduced loading/saving, auto clean up, do not save default

git-svn-id: trunk@33764 -
This commit is contained in:
mattias 2011-11-24 23:04:54 +00:00
parent 7fb1ba33f4
commit 40857d72fc
3 changed files with 160 additions and 83 deletions

View File

@ -314,7 +314,7 @@ type
// 'new items' // 'new items'
FNewFormTemplate: string; FNewFormTemplate: string;
FNewUnitTemplate: string; FNewUnitTemplate: string;
FFileFilters: TStringList; FFileDialogFilter: string;
function GetDebuggerEventLogColors(AIndex: TDBGEventType): TDebuggerEventLogColor; function GetDebuggerEventLogColors(AIndex: TDBGEventType): TDebuggerEventLogColor;
procedure SetCompilerFilename(const AValue: string); procedure SetCompilerFilename(const AValue: string);
@ -573,7 +573,7 @@ type
property NewUnitTemplate: string read FNewUnitTemplate write FNewUnitTemplate; property NewUnitTemplate: string read FNewUnitTemplate write FNewUnitTemplate;
property NewFormTemplate: string read FNewFormTemplate write FNewFormTemplate; property NewFormTemplate: string read FNewFormTemplate write FNewFormTemplate;
property FileFilters: TStringList read FFileFilters write FFileFilters; property FileDialogFilter: string read FFileDialogFilter write FFileDialogFilter;
end; end;
var var
@ -836,15 +836,12 @@ begin
// lazdoc // lazdoc
FLazDocPaths:=SetDirSeparators(DefaultLazDocPath); FLazDocPaths:=SetDirSeparators(DefaultLazDocPath);
FFileFilters := TStringList.Create;
end; end;
destructor TEnvironmentOptions.Destroy; destructor TEnvironmentOptions.Destroy;
var var
i: Integer; i: Integer;
begin begin
FreeAndNil(FFileFilters);
FreeAndNil(fExternalTools); FreeAndNil(fExternalTools);
FreeAndNil(FRecentOpenFiles); FreeAndNil(FRecentOpenFiles);
FreeAndNil(FRecentProjectFiles); FreeAndNil(FRecentProjectFiles);

View File

@ -5,11 +5,17 @@ unit env_file_filters;
interface interface
uses uses
Classes, SysUtils, FileUtil, LResources, Forms, Grids, Classes, SysUtils, FileUtil, LResources, Forms, Grids, Dialogs, Controls,
EnvironmentOpts, IDEOptionDefs, LCLProc, LCLType, Menus, StdCtrls, LazConfigStorage,
IDEOptionsIntf, Controls, Menus, StdCtrls; IDEOptionsIntf, BaseIDEIntf,
EnvironmentOpts, IDEOptionDefs, LazarusIDEStrConsts;
const
FileDialogFilterConfigFile = 'filefilters.xml';
type type
{ TFileFiltersOptionsFrame }
TFileFiltersOptionsFrame = class(TAbstractIDEOptionsEditor) TFileFiltersOptionsFrame = class(TAbstractIDEOptionsEditor)
grdFileFilters: TStringGrid; grdFileFilters: TStringGrid;
pmGrid: TPopupMenu; pmGrid: TPopupMenu;
@ -23,6 +29,8 @@ type
procedure pmiInsRowClick(Sender: TObject); procedure pmiInsRowClick(Sender: TObject);
private private
FList: TStringList; FList: TStringList;
fLoaded: boolean;
fSaved: boolean;
public public
constructor Create(TheOwner: TComponent); override; constructor Create(TheOwner: TComponent); override;
destructor Destroy; override; destructor Destroy; override;
@ -34,65 +42,114 @@ type
end; end;
procedure LoadFileFiltersList; procedure LoadFileDialogFilter;
procedure SaveFileDialogFilter;
function GetDefaultFileDialogFilter: string;
implementation implementation
{$R *.lfm} {$R *.lfm}
uses
LazarusIDEStrConsts,
BaseIDEIntf,
LazConfigStorage,
LCLType;
const const
cSettingsFile = 'filefilters.xml'; KeyFilter = 'Filter';
KeyFilter = 'Filter%2.2d';
KeyFilterCount = 'Count'; KeyFilterCount = 'Count';
KeyFilterName = 'Name'; KeyFilterName = 'Name';
KeyFilterMask = 'Mask'; KeyFilterMask = 'Mask';
procedure LoadFileFiltersList; procedure LoadFileDialogFilter;
const const
cFilter = '%s (%s)|%s|'; // each filter is seperated by another | sign cFilter = '%s (%s)|%s|'; // each filter is seperated by another | sign
var var
cfg: TConfigStorage; cfg: TConfigStorage;
c: integer; cnt: integer;
i: integer; i: integer;
lName, lMask: string; lName, lMask: string;
Filter: String;
begin begin
EnvironmentOptions.FileFilters.Clear; Filter:='';
cfg := GetIDEConfigStorage(cSettingsFile, True); cfg := GetIDEConfigStorage(FileDialogFilterConfigFile, True);
try try
c := cfg.GetValue(KeyFilterCount, 0); cnt := cfg.GetValue(KeyFilterCount, 0);
if c = 0 then if cnt = 0 then
begin begin
// create default values // create default values
EnvironmentOptions.FileFilters.Text:= Filter:=GetDefaultFileDialogFilter;
lisLazarusUnit + ' (*.pas;*.pp)|*.pas;*.pp'
+ '|' + lisLazarusProject + ' (*.lpi)|*.lpi'
+ '|' + lisLazarusForm + ' (*.lfm;*.dfm)|*.lfm;*.dfm'
+ '|' + lisLazarusPackage + ' (*.lpk)|*.lpk'
+ '|' + lisLazarusProjectSource + ' (*.lpr)|*.lpr'
+ '|' + lisLazarusOtherFile + ' (*.inc;*.lrs;*.lpl)|*.inc;*.lrs;*.lpl'
end end
else else
begin begin
// read values // read values
for i := 1 to c do for i := 1 to cnt do
begin begin
lName := cfg.GetValue(Format(KeyFilter, [i]) + '/' + KeyFilterName, ''); lName := cfg.GetValue(KeyFilter+IntToStr(i) + '/' + KeyFilterName, '');
lMask := cfg.GetValue(Format(KeyFilter, [i]) + '/' + KeyFilterMask, '*'); lMask := cfg.GetValue(KeyFilter+IntToStr(i) + '/' + KeyFilterMask, '*');
if (lName<>'') and (lMask<>'') then if (lName='') or (lMask='') then continue;
EnvironmentOptions.FileFilters.Add(Format(cFilter, [lName, lMask, lMask])); if Filter<>'' then
Filter:=Filter+'|';
Filter:=Filter+lName+'|'+lMask;
end; end;
end; end;
finally finally
cfg.Free; cfg.Free;
end; end;
EnvironmentOptions.FileDialogFilter:=Filter;
end;
procedure SaveFileDialogFilter;
var
cfg: TConfigStorage;
Cnt: Integer;
p: PChar;
MaskStart: PChar;
CaptionStart: PChar;
Filter: String;
Caption: String;
CurMask: String;
begin
Filter:=EnvironmentOptions.FileDialogFilter;
if Filter=GetDefaultFileDialogFilter then
Filter:='';
cfg := GetIDEConfigStorage(FileDialogFilterConfigFile,false);
try
Cnt:=0;
if Filter<>'' then begin
p:=PChar(Filter);
while p^<>#0 do
begin
// caption
CaptionStart:=p;
while not (p^ in ['|',#0]) do inc(p);
if p^=#0 then break;
Caption:=copy(Filter,CaptionStart-PChar(Filter)+1,p-CaptionStart);
// parse masks
inc(p);
MaskStart:=p;
while not (p^ in ['|',#0]) do inc(p);
if p>MaskStart then begin
inc(Cnt);
CurMask:=copy(Filter,MaskStart-PChar(Filter)+1,p-MaskStart);
cfg.SetValue(KeyFilter+IntToStr(Cnt) + '/' + KeyFilterName, Caption);
cfg.SetValue(KeyFilter+IntToStr(Cnt) + '/' + KeyFilterMask, CurMask);
end;
inc(p);
end;
end;
cfg.SetValue(KeyFilterCount, Cnt);
cfg.WriteToDisk;
finally
cfg.Free;
end;
end;
function GetDefaultFileDialogFilter: string;
begin
Result := lisLazarusUnit + ' (*.pas;*.pp)|*.pas;*.pp'
+ '|' + lisLazarusProject + ' (*.lpi)|*.lpi'
+ '|' + lisLazarusForm + ' (*.lfm;*.dfm)|*.lfm;*.dfm'
+ '|' + lisLazarusPackage + ' (*.lpk)|*.lpk'
+ '|' + lisLazarusProjectSource + ' (*.lpr)|*.lpr'
+ '|' + lisLazarusOtherFile + ' (*.inc;*.lrs;*.lpl)|*.inc;*.lrs;*.lpl';
end; end;
{ TFileFiltersOptionsFrame } { TFileFiltersOptionsFrame }
@ -151,11 +208,6 @@ begin
end; end;
procedure TFileFiltersOptionsFrame.ReadSettings(AOptions: TAbstractIDEOptions); procedure TFileFiltersOptionsFrame.ReadSettings(AOptions: TAbstractIDEOptions);
var
cfg: TConfigStorage;
c: integer;
i: integer;
lName, lMask: string;
procedure AddRowItem(const ARow: integer; const AName, AMask: String); procedure AddRowItem(const ARow: integer; const AName, AMask: String);
begin begin
@ -163,57 +215,85 @@ var
grdFileFilters.Cells[2, ARow] := AMask; grdFileFilters.Cells[2, ARow] := AMask;
end; end;
begin procedure ReadList(Filter: String; var Cnt: integer; Scan: boolean);
grdFileFilters.RowCount := 1; { don't call Clear because it will remove fixed columns too } var
cfg := GetIDEConfigStorage(cSettingsFile, True); p: PChar;
try CaptionStart: PChar;
c := cfg.GetValue(KeyFilterCount, 0); CurCaption: String;
if c = 0 then MaskStart: PChar;
begin CurMask: String;
// create default vaulues begin
grdFileFilters.RowCount := grdFileFilters.RowCount + 7; Cnt:=0;
AddRowItem(1, lisLazarusFile, '*.lpi;*.lpr;*.lpk;*.pas;*.pp;*.inc;*.lfm;*.dfm'); if Filter<>'' then begin
AddRowItem(2, lisLazarusUnit, '*.pas;*.pp'); p:=PChar(Filter);
AddRowItem(3, lisLazarusProject, '*.lpi'); while p^<>#0 do
AddRowItem(4, lisLazarusForm, '*.lfm;*.dfm');
AddRowItem(5, lisLazarusPackage, '*.lpk');
AddRowItem(6, lisLazarusProjectSource, '*.lpr');
end
else
begin
// read values
grdFileFilters.RowCount := c+1;
for i := 1 to c do
begin begin
lName := cfg.GetValue(Format(KeyFilter, [i]) + '/' + KeyFilterName, 'N' + IntToStr(i)); // N1, N2 etc if no name specified // caption
lMask := cfg.GetValue(Format(KeyFilter, [i]) + '/' + KeyFilterMask, AllFilesMask); CaptionStart:=p;
AddRowItem(i, lName, lMask); while not (p^ in ['|',#0]) do inc(p);
if p^=#0 then break;
CurCaption:=copy(Filter,CaptionStart-PChar(Filter)+1,p-CaptionStart);
// parse masks
repeat
inc(p);
MaskStart:=p;
while not (p^ in ['|',#0]) do inc(p);
if p>MaskStart then begin
CurMask:=copy(Filter,MaskStart-PChar(Filter)+1,p-MaskStart);
inc(Cnt);
if not Scan then
AddRowItem(Cnt,CurCaption,CurMask);
end;
if p^='|' then break;
until p^=#0;
inc(p);
end; end;
end; end;
finally
cfg.Free;
end; end;
LoadFileFiltersList;
var
Filter: String;
Cnt: Integer;
begin
if fLoaded then exit;
fLoaded:=true;
Filter:=EnvironmentOptions.FileDialogFilter;
Cnt:=0;
ReadList(Filter,Cnt,true);
grdFileFilters.RowCount := Cnt+2; // +1 for header, +1 for a new item
ReadList(Filter,Cnt,false);
end; end;
procedure TFileFiltersOptionsFrame.WriteSettings(AOptions: TAbstractIDEOptions); procedure TFileFiltersOptionsFrame.WriteSettings(AOptions: TAbstractIDEOptions);
var var
cfg: TConfigStorage; Filter: String;
i: integer; i: Integer;
CurCaption: String;
CurMask: String;
begin begin
cfg := GetIDEConfigStorage(cSettingsFile, False); if fSaved then exit;
try fSaved:=true;
cfg.SetValue(KeyFilterCount, grdFileFilters.RowCount-1);
for i := 1 to grdFileFilters.RowCount-1 do Filter:='';
begin for i := 1 to grdFileFilters.RowCount-1 do
cfg.SetValue(Format(KeyFilter, [i]) + '/' + KeyFilterName, grdFileFilters.Cells[1, i]); begin
cfg.SetValue(Format(KeyFilter, [i]) + '/' + KeyFilterMask, grdFileFilters.Cells[2, i]); CurCaption:=grdFileFilters.Cells[1, i];
end; CurMask:=grdFileFilters.Cells[2, i];
cfg.WriteToDisk; CurCaption:=StringReplace(CurCaption,'|',',',[rfReplaceAll]);
finally CurMask:=StringReplace(CurMask,'|',',',[rfReplaceAll]);
cfg.Free; if (CurCaption='') or (CurMask='') then continue;
if Filter<>'' then
Filter:=Filter+'|';
Filter:=Filter+CurCaption+'|'+CurMask;
end;
if EnvironmentOptions.FileDialogFilter<>Filter then begin
//debugln(['TFileFiltersOptionsFrame.WriteSettings ']);
EnvironmentOptions.FileDialogFilter:=Filter;
SaveFileDialogFilter;
end; end;
LoadFileFiltersList;
end; end;
class function TFileFiltersOptionsFrame.SupportedOptionsClass: TAbstractIDEOptionsClass; class function TFileFiltersOptionsFrame.SupportedOptionsClass: TAbstractIDEOptionsClass;

View File

@ -1312,7 +1312,7 @@ begin
ExternalTools.OnNeedsOutputFilter := @OnExtToolNeedsOutputFilter; ExternalTools.OnNeedsOutputFilter := @OnExtToolNeedsOutputFilter;
ExternalTools.OnFreeOutputFilter := @OnExtToolFreeOutputFilter; ExternalTools.OnFreeOutputFilter := @OnExtToolFreeOutputFilter;
UpdateDefaultPascalFileExtensions; UpdateDefaultPascalFileExtensions;
LoadFileFiltersList; LoadFileDialogFilter;
EditorOpts := TEditorOptions.Create; EditorOpts := TEditorOptions.Create;
EditorOpts.OnBeforeRead := @DoEditorOptionsBeforeRead; EditorOpts.OnBeforeRead := @DoEditorOptionsBeforeRead;
@ -2846,7 +2846,7 @@ begin
if Assigned(AnUnitInfo) and (not AnUnitInfo.IsVirtual) then if Assigned(AnUnitInfo) and (not AnUnitInfo.IsVirtual) then
OpenDialog.InitialDir:=ExtractFilePath(AnUnitInfo.Filename); OpenDialog.InitialDir:=ExtractFilePath(AnUnitInfo.Filename);
Filter := EnvironmentOptions.FileFilters.Text; Filter := EnvironmentOptions.FileDialogFilter;
// append a filter for all file types of the open files in the source editor // append a filter for all file types of the open files in the source editor
CreateFileDialogFilterForSourceEditorFiles(Filter,AllEditorMask,AllMask); CreateFileDialogFilterForSourceEditorFiles(Filter,AllEditorMask,AllMask);