1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
unit env_file_filters;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
// LazUtils
LazFileUtils, LazConfigStorage,
// LCL
LCLProc, LCLType, LResources, Grids, Dialogs, Controls, StdCtrls, Menus,
// IdeIntf
IDEOptionsIntf, IDEOptEditorIntf, BaseIDEIntf, IDEDialogs,
// IDE
EnvironmentOpts, LazarusIDEStrConsts;
const
FileDialogFilterConfigFile = 'filefilters.xml';
type
{ TFileFiltersOptionsFrame }
TFileFiltersOptionsFrame = class(TAbstractIDEOptionsEditor)
grdFileFilters: TStringGrid;
MenuItem1: TMenuItem;
SetDefaultMenuItem: TMenuItem;
pmGrid: TPopupMenu;
pmiAddRow: TMenuItem;
pmiDelRow: TMenuItem;
pmiInsRow: TMenuItem;
lblTitle: TLabel;
procedure grdFileFiltersKeyDown(Sender: TObject; var Key: Word; {%H-}Shift: TShiftState);
procedure pmiAddRowClick(Sender: TObject);
procedure pmiDelRowClick(Sender: TObject);
procedure pmiInsRowClick(Sender: TObject);
procedure SetDefaultMenuItemClick(Sender: TObject);
private
FList: TStringList;
fLoaded: boolean;
fSaved: boolean;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
function GetTitle: String; override;
procedure Setup({%H-}ADialog: TAbstractOptionsEditorDialog); override;
procedure ReadSettings({%H-}AOptions: TAbstractIDEOptions); override;
procedure WriteSettings({%H-}AOptions: TAbstractIDEOptions); override;
class function SupportedOptionsClass: TAbstractIDEOptionsClass; override;
end;
procedure LoadFileDialogFilter;
procedure SaveFileDialogFilter;
function GetDefaultFileDialogFilter: string;
function GetFileDialogFilterFromGrid(Grid: TStringGrid): string;
procedure LoadGridFromFileDialogFilter(Grid: TStringGrid; Filter: string;
AddEmptyRow: boolean);
implementation
{$R *.lfm}
const
KeyFilter = 'Filter';
KeyFilterCount = 'Count';
KeyFilterName = 'Name';
KeyFilterMask = 'Mask';
procedure LoadFileDialogFilter;
var
cfg: TConfigStorage;
cnt: integer;
i: integer;
lName, lMask: string;
Filter: String;
begin
Filter:='';
cfg := GetIDEConfigStorage(FileDialogFilterConfigFile, True);
try
cnt := cfg.GetValue(KeyFilterCount, 0);
// read values
for i := 1 to cnt do
begin
lName := cfg.GetValue(KeyFilter+IntToStr(i) + '/' + KeyFilterName, '');
lMask := cfg.GetValue(KeyFilter+IntToStr(i) + '/' + KeyFilterMask, '*');
if (lName='') or (lMask='') then continue;
if Filter<>'' then
Filter:=Filter+'|';
Filter:=Filter+lName+'|'+lMask;
end;
if Filter='' then
Filter:=GetDefaultFileDialogFilter;
finally
cfg.Free;
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;
if Cnt>0 then begin
cfg.SetValue(KeyFilterCount, Cnt);
cfg.WriteToDisk;
end else if FileExistsUTF8(cfg.GetFilename) then begin
// delete unneeded file
DeleteFileUTF8(cfg.GetFilename);
end;
finally
cfg.Free;
end;
end;
function GetDefaultFileDialogFilter: string;
begin
Result := dlgFilterLazarusUnit + ' (*.pas;*.pp)|*.pas;*.pp'
+ '|' + dlgFilterLazarusProject + ' (*.lpi)|*.lpi'
+ '|' + dlgFilterLazarusForm + ' (*.lfm;*.dfm)|*.lfm;*.dfm'
+ '|' + dlgFilterLazarusPackage + ' (*.lpk)|*.lpk'
+ '|' + dlgFilterLazarusProjectSource + ' (*.lpr)|*.lpr'
+ '|' + dlgFilterLazarusOtherFile + ' (*.inc;*.lrs;*.lpl)|*.inc;*.lrs;*.lpl';
end;
function GetFileDialogFilterFromGrid(Grid: TStringGrid): string;
var
i: Integer;
CurCaption: String;
CurMask: String;
begin
Result:='';
for i := 1 to Grid.RowCount-1 do
begin
CurCaption:=Grid.Cells[1, i];
CurMask:=Grid.Cells[2, i];
CurCaption:=StringReplace(CurCaption,'|',',',[rfReplaceAll]);
CurMask:=StringReplace(CurMask,'|',',',[rfReplaceAll]);
if (CurCaption='') or (CurMask='') then continue;
if Result<>'' then
Result:=Result+'|';
Result:=Result+CurCaption+'|'+CurMask;
end;
end;
procedure LoadGridFromFileDialogFilter(Grid: TStringGrid; Filter: string;
AddEmptyRow: boolean);
procedure ReadList(var Cnt: integer; Scan: boolean);
var
p: PChar;
CaptionStart: PChar;
CurCaption: String;
MaskStart: PChar;
CurMask: String;
begin
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;
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 begin
Grid.Cells[1, Cnt] := CurCaption;
Grid.Cells[2, Cnt] := CurMask;
end;
end;
if p^='|' then break;
until p^=#0;
inc(p);
end;
end;
end;
var
Cnt: Integer;
begin
Cnt:=0;
ReadList(Cnt,true);
Grid.BeginUpdate;
try
inc(Cnt,Grid.FixedRows);
if AddEmptyRow then inc(Cnt);
Grid.RowCount := Cnt;
if AddEmptyRow then begin
Grid.Cells[1, Cnt-1] := '';
Grid.Cells[2, Cnt-1] := '';
end;
ReadList(Cnt,false);
finally
Grid.EndUpdate(true);
end;
end;
{ TFileFiltersOptionsFrame }
procedure TFileFiltersOptionsFrame.grdFileFiltersKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_INSERT then
grdFileFilters.RowCount := grdFileFilters.RowCount + 1;
end;
procedure TFileFiltersOptionsFrame.pmiAddRowClick(Sender: TObject);
begin
grdFileFilters.RowCount := grdFileFilters.RowCount + 1;
end;
procedure TFileFiltersOptionsFrame.pmiDelRowClick(Sender: TObject);
begin
grdFileFilters.DeleteColRow(False, grdFileFilters.Row);
end;
procedure TFileFiltersOptionsFrame.pmiInsRowClick(Sender: TObject);
begin
grdFileFilters.InsertColRow(False, grdFileFilters.Row);
end;
procedure TFileFiltersOptionsFrame.SetDefaultMenuItemClick(Sender: TObject);
begin
if IDEMessageDialog(lisConfirm,
lisResetAllFileFiltersToDefaults, mtConfirmation, [mbCancel, mbOK])<>mrOk
then exit;
LoadGridFromFileDialogFilter(grdFileFilters,GetDefaultFileDialogFilter,false);
end;
constructor TFileFiltersOptionsFrame.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FList := TStringList.Create;
end;
destructor TFileFiltersOptionsFrame.Destroy;
begin
FList.Free;
inherited Destroy;
end;
function TFileFiltersOptionsFrame.GetTitle: String;
begin
Result := lisFileFilters;
end;
procedure TFileFiltersOptionsFrame.Setup(ADialog: TAbstractOptionsEditorDialog);
begin
lblTitle.Caption := lisFileFiltersTitle;
grdFileFilters.DefaultColWidth := 40;
grdFileFilters.RowCount := 1;
grdFileFilters.Columns[0].Title.Caption := lisName;
grdFileFilters.Columns[1].Title.Caption := lisFileFiltersMask;
pmiAddRow.Caption := lisFileFiltersAddRow;
pmiDelRow.Caption := lisFileFiltersDeleteRow;
pmiInsRow.Caption := lisFileFiltersInsertRow;
SetDefaultMenuItem.Caption := lisFileFiltersSetDefaults;
end;
procedure TFileFiltersOptionsFrame.ReadSettings(AOptions: TAbstractIDEOptions);
begin
if fLoaded then exit;
fLoaded:=true;
LoadGridFromFileDialogFilter(grdFileFilters,EnvironmentOptions.FileDialogFilter,false);
end;
procedure TFileFiltersOptionsFrame.WriteSettings(AOptions: TAbstractIDEOptions);
var
Filter: String;
begin
if fSaved then exit;
fSaved:=true;
Filter:=GetFileDialogFilterFromGrid(grdFileFilters);
if EnvironmentOptions.FileDialogFilter<>Filter then begin
//debugln(['TFileFiltersOptionsFrame.WriteSettings ']);
EnvironmentOptions.FileDialogFilter:=Filter;
SaveFileDialogFilter;
end;
end;
class function TFileFiltersOptionsFrame.SupportedOptionsClass: TAbstractIDEOptionsClass;
begin
Result := TEnvironmentOptions;
end;
initialization
RegisterIDEOptionsEditor(GroupEnvironment, TFileFiltersOptionsFrame, EnvOptionsFileFilters);
end.
|