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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
|
{
/***************************************************************************
packagelisteditordlg.pas
------------------------
***************************************************************************/
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Abstract:
Defines the TPkgListEditorDialog, which is a form to edit search lists of packages
}
unit PackageListEditorDlg;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
// LCL
LCLType, Forms, Controls, Buttons, StdCtrls, Dialogs, Menus, Graphics,
ButtonPanel, Clipbrd, Grids,
// LazUtils
FileUtil, LazFileUtils, LazStringUtils, LazFileCache, LazUTF8,
// IdeIntf
IdeIntfStrConsts, IDEImagesIntf,
// IDE
TransferMacros, GenericListSelect, LazarusIDEStrConsts;
type
TPkgPatternPrefix = (
pppInclude,
pppIncludeRecursive,
pppExclude
);
const
PkgPatternPrefixes: array[TPkgPatternPrefix] of string = (
'',
'>',
'-'
);
type
{ TPkgListEditorDialog }
TPkgListEditorDialog = class(TForm)
AddTemplateButton: TBitBtn;
AvailableGroupBox: TGroupBox;
AvailableListBox: TListBox;
ButtonPanel1: TButtonPanel;
CopyMenuItem: TMenuItem;
MoveDownButton: TSpeedButton;
MoveUpButton: TSpeedButton;
OpenDialog1: TOpenDialog;
PkgsStringGrid: TStringGrid;
SaveDialog1: TSaveDialog;
ExportMenuItem: TMenuItem;
ImportMenuItem: TMenuItem;
SeparMenuItem: TMenuItem;
PasteMenuItem: TMenuItem;
PopupMenu1: TPopupMenu;
AddButton: TBitBtn;
DeleteButton: TBitBtn;
PkgsGroupBox: TGroupBox;
procedure AddButtonClick(Sender: TObject);
procedure AddTemplateButtonClick(Sender: TObject);
procedure CopyMenuItemClick(Sender: TObject);
procedure ExportMenuItemClick(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure PasteMenuItemClick(Sender: TObject);
procedure DeleteButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure MoveDownButtonClick(Sender: TObject);
procedure MoveUpButtonClick(Sender: TObject);
procedure ImportMenuItemClick(Sender: TObject);
private
FTemplateList: TStringListUTF8Fast;
procedure AddPkg(PkgName: string; PkgAction: TPkgPatternPrefix);
function GetPath: string;
procedure SetPath(const AValue: string);
procedure SetTemplates(const AValue: string);
procedure UpdateButtons;
function SaveToList: TStringListUTF8Fast;
procedure LoadFromList(sl: TStrings);
function PatternActionToCaption(PkgAction: TPkgPatternPrefix): string;
function CaptionToPatternAction(const PkgAction: string): TPkgPatternPrefix;
function RowOfPkgName(const PkgName: string): integer;
public
property Path: string read GetPath write SetPath;
property Templates: string {read GetTemplates} write SetTemplates;
end;
TOnPkgListEditorExecuted = function (Context: String; var NewPath: String): Boolean of object;
{ TPkgListEditorButton }
TPkgListEditorButton = class(TButton)
private
FAssociatedComboBox: TCustomComboBox;
FCurrentPkgListEditor: TPkgListEditorDialog;
FAssociatedEdit: TCustomEdit;
FContextCaption: String;
FTemplates: String;
FOnExecuted: TOnPkgListEditorExecuted;
function GetAssociatedText: string;
protected
procedure DoOnPathEditorExecuted;
public
procedure Click; override;
property CurrentPathEditor: TPkgListEditorDialog read FCurrentPkgListEditor;
property AssociatedComboBox: TCustomComboBox read FAssociatedComboBox write FAssociatedComboBox;
property AssociatedEdit: TCustomEdit read FAssociatedEdit write FAssociatedEdit;
property AssociatedText: string read GetAssociatedText;
property ContextCaption: String read FContextCaption write FContextCaption;
property Templates: String read FTemplates write FTemplates;
property OnExecuted: TOnPkgListEditorExecuted read FOnExecuted write FOnExecuted;
end;
function PkgListEditorDialog: TPkgListEditorDialog;
procedure SetPkgListTextAndHint(aPath: String; aEdit: TWinControl);
function ParsePkgPattern(const Pattern: string; out Action: TPkgPatternPrefix): string; // returns packagename
function MakeParsePkgPattern(const PkgName: string; Action: TPkgPatternPrefix): string;
implementation
{$R *.lfm}
var PkgListEditor: TPkgListEditorDialog;
function PkgListEditorDialog: TPkgListEditorDialog;
begin
if PkgListEditor=nil then
PkgListEditor:=TPkgListEditorDialog.Create(Application);
Result:=PkgListEditor;
end;
procedure SetPkgListTextAndHint(aPath: String; aEdit: TWinControl);
var
sl: TStrings;
begin
if aEdit is TCustomEdit then
TCustomEdit(aEdit).Text := aPath
else if aEdit is TCustomComboBox then
TCustomComboBox(aEdit).Text := aPath;
if Pos(';', aPath) > 0 then
begin
sl := SplitString(aPath, ';');
aEdit.Hint := sl.Text;
sl.Free;
end
else
aEdit.Hint := lisDelimiterIsSemicolon;
end;
function ParsePkgPattern(const Pattern: string; out Action: TPkgPatternPrefix
): string;
var
ap: String;
begin
for Action in TPkgPatternPrefix do
begin
ap:=PkgPatternPrefixes[Action];
if ap='' then continue;
if LeftStr(Pattern,length(ap))=ap then
begin
Result:=copy(Pattern,length(ap)+1,length(Pattern));
exit;
end;
end;
Result:=Pattern;
Action:=pppInclude;
end;
function MakeParsePkgPattern(const PkgName: string; Action: TPkgPatternPrefix
): string;
begin
if PkgName='' then
exit('');
Result:=PkgPatternPrefixes[Action]+PkgName;
end;
{ TPkgListEditorDialog }
procedure TPkgListEditorDialog.AddButtonClick(Sender: TObject);
begin
// ToDo
end;
procedure TPkgListEditorDialog.DeleteButtonClick(Sender: TObject);
begin
PkgsStringGrid.DeleteRow(PkgsStringGrid.Row);
UpdateButtons;
end;
procedure TPkgListEditorDialog.AddTemplateButtonClick(Sender: TObject);
var
TemplateForm: TGenericListSelectForm;
i, j: Integer;
begin
TemplateForm := TGenericListSelectForm.Create(Nil);
try
TemplateForm.Caption := lisPathEditPathTemplates;
// Let a user select only templates which are not in the list already.
for i := 0 to FTemplateList.Count-1 do
begin
j:=PkgsStringGrid.RowCount-1;
while (j>0) and not SameText(PkgsStringGrid.Cells[0,j],FTemplateList[i]) do dec(j);
if j<1 then
TemplateForm.ListBox.Items.Add(FTemplateList[i]);
end;
if TemplateForm.ShowModal = mrOK then
with TemplateForm.ListBox do
AddPkg(Items[ItemIndex], pppInclude);
finally
TemplateForm.Free;
end;
end;
procedure TPkgListEditorDialog.CopyMenuItemClick(Sender: TObject);
var
Paths: TStringList;
begin
Paths := SaveToList;
try
Clipboard.AsText := Paths.Text;
finally
Paths.Free;
end;
end;
procedure TPkgListEditorDialog.ExportMenuItemClick(Sender: TObject);
var
Paths: TStringList;
begin
if not SaveDialog1.Execute then Exit;
Paths := SaveToList;
try
Paths.SaveToFile(SaveDialog1.FileName);
finally
Paths.Free;
end;
end;
procedure TPkgListEditorDialog.LoadFromList(sl: TStrings);
var
PkgName: string;
i: integer;
PkgAction: TPkgPatternPrefix;
begin
for i:=sl.Count-1 downto 0 do
begin
PkgName:=ParsePkgPattern(Trim(sl[i]),PkgAction);
if PkgName='' then sl.Delete(i);
end;
PkgsStringGrid.RowCount:=sl.Count+1;
for i:=0 to sl.Count-1 do
begin
PkgName:=ParsePkgPattern(Trim(sl[i]),PkgAction);
PkgsStringGrid.Cells[0,i+1]:=PkgName;
PkgsStringGrid.Cells[1,i+1]:=PatternActionToCaption(PkgAction);
end;
UpdateButtons;
end;
procedure TPkgListEditorDialog.PasteMenuItemClick(Sender: TObject);
var
Paths: TStringList;
begin
Paths := TStringList.Create;
try
Paths.Text := Clipboard.AsText;
LoadFromList(Paths);
finally
Paths.Free;
end;
end;
procedure TPkgListEditorDialog.ImportMenuItemClick(Sender: TObject);
var
Paths: TStringList;
begin
if not OpenDialog1.Execute then Exit;
Paths := TStringList.Create;
try
Paths.LoadFromFile(OpenDialog1.FileName);
LoadFromList(Paths);
finally
Paths.Free;
end;
end;
procedure TPkgListEditorDialog.AddPkg(PkgName: string;
PkgAction: TPkgPatternPrefix);
var
i: Integer;
begin
for i:=1 to PkgsStringGrid.RowCount-1 do
begin
if not SameText(PkgsStringGrid.Cells[0,i],PkgName) then continue;
PkgsStringGrid.Cells[1,i]:=PatternActionToCaption(PkgAction);
exit;
end;
i:=PkgsStringGrid.RowCount;
PkgsStringGrid.RowCount:=i+1;
PkgsStringGrid.Cells[0,i]:=PkgName;
PkgsStringGrid.Cells[1,i]:=PatternActionToCaption(PkgAction);
end;
procedure TPkgListEditorDialog.FormCreate(Sender: TObject);
const
Filt = 'Text file (*.txt)|*.txt|All files (*)|*';
var
PkgAction: TPkgPatternPrefix;
s: String;
begin
FTemplateList := TStringListUTF8Fast.Create;
Caption:='Search in Packages Editor';
PkgsGroupBox.Caption:=lisPathEditSearchPaths;
MoveUpButton.Hint:=lisPathEditMovePathUp;
MoveDownButton.Hint:=lisPathEditMovePathDown;
AddButton.Caption:=lisAdd;
AddButton.Hint:=lisPathEditorAddHint;
DeleteButton.Caption:=lisDelete;
DeleteButton.Hint:=lisPathEditorDeleteHint;
AddTemplateButton.Caption:=lisCodeTemplAdd;
AddTemplateButton.Hint:=lisPathEditorTemplAddHint;
s:='';
for PkgAction in TPkgPatternPrefix do
s:=s+PatternActionToCaption(PkgAction)+sLineBreak;
PkgsStringGrid.Columns[1].PickList.Text:=s;
PopupMenu1.Images:=IDEImages.Images_16;
CopyMenuItem.Caption:=lisCopyAllItemsToClipboard;
CopyMenuItem.ImageIndex:=IDEImages.LoadImage('laz_copy');
PasteMenuItem.Caption:=lisMenuPasteFromClipboard;
PasteMenuItem.ImageIndex:=IDEImages.LoadImage('laz_paste');
ExportMenuItem.Caption:=lisExportAllItemsToFile;
ExportMenuItem.ImageIndex:=IDEImages.LoadImage('laz_save');
ImportMenuItem.Caption:=lisImportFromFile;
ImportMenuItem.ImageIndex:=IDEImages.LoadImage('laz_open');
OpenDialog1.Filter:=Filt;
SaveDialog1.Filter:=Filt;
IDEImages.AssignImage(MoveUpButton, 'arrow_up');
IDEImages.AssignImage(MoveDownButton, 'arrow_down');
//IDEImages.AssignImage(ReplaceButton, 'menu_reportingbug');
IDEImages.AssignImage(AddButton, 'laz_add');
IDEImages.AssignImage(DeleteButton, 'laz_delete');
//IDEImages.AssignImage(DeleteInvalidPkgsButton, 'menu_clean');
IDEImages.AssignImage(AddTemplateButton, 'laz_add');
end;
procedure TPkgListEditorDialog.FormDestroy(Sender: TObject);
begin
FTemplateList.Free;
end;
procedure TPkgListEditorDialog.FormShow(Sender: TObject);
begin
UpdateButtons;
end;
procedure TPkgListEditorDialog.MoveDownButtonClick(Sender: TObject);
var
y: integer;
begin
y:=PkgsStringGrid.Row;
if (y>0) and (y<PkgsStringGrid.RowCount-1) then begin
PkgsStringGrid.MoveColRow(false,y,y+1);
UpdateButtons;
end;
end;
procedure TPkgListEditorDialog.MoveUpButtonClick(Sender: TObject);
var
y: integer;
begin
y:=PkgsStringGrid.Row;
if (y>2) and (y<PkgsStringGrid.RowCount) then begin
PkgsStringGrid.MoveColRow(false,y+1,y);
UpdateButtons;
end;
end;
function TPkgListEditorDialog.GetPath: string;
var
i: Integer;
PkgAction: TPkgPatternPrefix;
begin
Result:='';
for i:=1 to PkgsStringGrid.RowCount-1 do
begin
PkgAction:=CaptionToPatternAction(PkgsStringGrid.Cells[1,i]);
if Result<>'' then
Result:=Result+';';
Result:=PkgPatternPrefixes[PkgAction]+Trim(PkgsStringGrid.Cells[0,i]);
end;
end;
procedure TPkgListEditorDialog.SetPath(const AValue: string);
var
sl: TStrings;
begin
sl := SplitString(AValue, ';');
try
LoadFromList(sl);
finally
sl.Free;
end;
end;
procedure TPkgListEditorDialog.SetTemplates(const AValue: string);
begin
SplitString(GetForcedPathDelims(AValue), ';', FTemplateList, True);
AddTemplateButton.Enabled := FTemplateList.Count > 0;
end;
procedure TPkgListEditorDialog.UpdateButtons;
var
i: integer;
begin
AddButton.Enabled:=(AvailableListBox.ItemIndex>=0)
and (RowOfPkgName(AvailableListBox.Items[AvailableListBox.ItemIndex])<1);
DeleteButton.Enabled:=PkgsStringGrid.Row>0;
// Move up / down buttons
i := PkgsStringGrid.Row;
MoveUpButton.Enabled := i > 1;
MoveDownButton.Enabled := (i > -1) and (i < PkgsStringGrid.RowCount-1);
end;
function TPkgListEditorDialog.SaveToList: TStringListUTF8Fast;
var
i: Integer;
PkgAction: TPkgPatternPrefix;
begin
Result:=TStringListUTF8Fast.Create;
for i:=1 to PkgsStringGrid.RowCount-1 do
begin
PkgAction:=CaptionToPatternAction(PkgsStringGrid.Cells[1,i]);
Result.Add(PkgPatternPrefixes[PkgAction]+PkgsStringGrid.Cells[0,i]);
end;
end;
function TPkgListEditorDialog.PatternActionToCaption(
PkgAction: TPkgPatternPrefix): string;
begin
case PkgAction of
pppIncludeRecursive: Result:=lisIncludeRecursive;
pppExclude: Result:=lisExclude;
else
Result:=lisPckOptsInclude;
end;
end;
function TPkgListEditorDialog.CaptionToPatternAction(const PkgAction: string
): TPkgPatternPrefix;
begin
if PkgAction=lisIncludeRecursive then
Result:=pppIncludeRecursive
else if PkgAction=lisExclude then
Result:=pppExclude
else
Result:=pppInclude;
end;
function TPkgListEditorDialog.RowOfPkgName(const PkgName: string): integer;
begin
for Result:=1 to PkgsStringGrid.RowCount-1 do
if SameText(PkgsStringGrid.Cells[0,Result],PkgName) then
exit;
Result:=-1;
end;
{ TPkgListEditorButton }
procedure TPkgListEditorButton.Click;
begin
FCurrentPkgListEditor:=PkgListEditorDialog;
try
inherited Click;
FCurrentPkgListEditor.Templates := FTemplates;
FCurrentPkgListEditor.Path := AssociatedText;
FCurrentPkgListEditor.ShowModal;
DoOnPathEditorExecuted;
finally
FCurrentPkgListEditor:=nil;
end;
end;
function TPkgListEditorButton.GetAssociatedText: string;
begin
if AssociatedEdit<>nil then
Result:=AssociatedEdit.Text
else if AssociatedComboBox<>nil then
Result:=AssociatedComboBox.Text
else
Result:='';
end;
procedure TPkgListEditorButton.DoOnPathEditorExecuted;
var
Ok: Boolean;
NewPath: String;
begin
NewPath := FCurrentPkgListEditor.Path;
Ok := (FCurrentPkgListEditor.ModalResult = mrOk) and (AssociatedText <> NewPath);
if Ok and Assigned(OnExecuted) then
Ok := OnExecuted(ContextCaption, NewPath);
// Assign value only if old <> new and OnExecuted allows it.
if not Ok then exit;
if AssociatedEdit<>nil then
SetPkgListTextAndHint(NewPath, AssociatedEdit)
else if AssociatedComboBox<>nil then
SetPkgListTextAndHint(NewPath, AssociatedComboBox);
end;
end.
|