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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
{ PropertyEditor
Copyright (C) <year> <name of author> <contact>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
unit SynPropertyEditObjectList;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LCLProc,
SynEdit, SynGutterBase, SynEditMiscClasses, SynEditMouseCmds, SynEditKeyCmds,
SynDesignStringConstants,
PropEdits, PropEditUtils, Forms, StdCtrls, ComCtrls, Dialogs, ComponentEditors,
ObjInspStrConsts, Controls, IDEImagesIntf, typinfo, FormEditingIntf;
type
{ TSynEdOptionsPropertyEditor }
TSynEdOptionsPropertyEditor = class(TSetPropertyEditor)
public
procedure GetProperties(Proc: TGetPropEditProc); override;
end;
{ TSynPropertEditClassList }
TSynPropertyEditObjectList = class(TListPropertyEditor)
protected
function ReadElementCount: integer; override;
function ReadElement(Index: integer): TPersistent; override;
class function ClassList: TStringList; virtual; abstract;
public
function GetAttributes: TPropertyAttributes; override;
procedure Edit; override;
class function ShowClassListEditor(AClassList: TSynObjectList;
OwnerPersistent: TPersistent; const PropName: String): TCustomForm;
end;
{ TSynPropertyEditGutterPartList }
TSynPropertyEditGutterPartList = class(TSynPropertyEditObjectList)
protected
class function ClassList: TStringList; override;
end;
{ TSynObjectPartListPropertyEditorForm }
TSynObjectPartListPropertyEditorForm = class(TForm)
SynObjectPartsListBox: TListBox;
ToolBar1: TToolBar;
AddButton: TToolButton;
ClassComboBox: TComboBox;
DeleteButton: TToolButton;
ToolButton3: TToolButton;
MoveUpButton: TToolButton;
MoveDownButton: TToolButton;
procedure AddButtonClick(Sender: TObject);
procedure SynObjectPartsListBoxClick(Sender: TObject);
procedure DeleteButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure MoveDownButtonClick(Sender: TObject);
procedure MoveUpButtonClick(Sender: TObject);
private
FSynObjectPartList: TSynObjectList;
FOwnerPersistent: TPersistent;
FPropertyName: String;
FClassesList: TStringList;
protected
procedure UpdateCaption;
procedure UpdateButtons;
procedure ComponentRenamed(AComponent: TComponent);
procedure PersistentDeleting(APersistent: TPersistent);
procedure RefreshPropertyValues;
public
procedure FillSynObjectPartsListBox;
procedure SelectInObjectInspector(UnselectAll: Boolean);
procedure SetSynObjectPartList(NewSynObjectPartList: TSynObjectList;
NewClassesList: TStringList;
NewOwnerPersistent: TPersistent; const NewPropName: String);
procedure Modified;
public
property SynObjectPartList: TSynObjectList read FSynObjectPartList;
property OwnerPersistent: TPersistent read FOwnerPersistent;
property PropertyName: String read FPropertyName;
end;
{ TSynKeyCommandPropertyEditor }
TSynKeyCommandPropertyEditor = class(TIntegerPropertyEditor)
public
function GetAttributes: TPropertyAttributes; override;
function OrdValueToVisualValue(OrdValue: longint): string; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure SetValue(const NewValue: ansistring); override;
end;
{ TSynMouseCommandPropertyEditor }
TSynMouseCommandPropertyEditor = class(TIntegerPropertyEditor)
public
function GetAttributes: TPropertyAttributes; override;
function OrdValueToVisualValue(OrdValue: longint): string; override;
procedure GetValues(Proc: TGetStrProc); override;
procedure SetValue(const NewValue: ansistring); override;
end;
{ TSynEditComponentEditor }
TSynEditComponentEditor = class(TDefaultComponentEditor)
public
function GetVerbCount: Integer; override;
function GetVerb(Index: Integer): string; override;
procedure ExecuteVerb(Index: Integer); override;
end;
procedure RegisterGutterPartClass(AClass: TSynGutterPartBaseClass; AName: String);
implementation
{$R *.lfm}
const
SynObjectPartListForm: TSynObjectPartListPropertyEditorForm = nil;
KnownSynGutterPartClasses: TStringList = nil;
procedure RegisterGutterPartClass(AClass: TSynGutterPartBaseClass; AName: String);
begin
if KnownSynGutterPartClasses = nil then
KnownSynGutterPartClasses := TStringList.Create;
KnownSynGutterPartClasses.AddObject(AName, TObject(Pointer(AClass)));
end;
{ TSynEditComponentEditor }
function TSynEditComponentEditor.GetVerbCount: Integer;
begin
Result := inherited GetVerbCount;
Result := Result + 1;
end;
function TSynEditComponentEditor.GetVerb(Index: Integer): string;
begin
case Index - (inherited GetVerbCount) of
0: Result := syndsResetMouseActions;
else
Result := inherited GetVerb(Index);
end;
end;
procedure TSynEditComponentEditor.ExecuteVerb(Index: Integer);
var
Hook: TPropertyEditorHook;
begin
case Index - (inherited GetVerbCount) of
0: begin
if (Component <> nil) and (Component is TCustomSynEdit) then
TCustomSynEdit(Component).ResetMouseActions;
Modified;
GetHook(Hook);
if Assigned(Hook) then Hook.Modified(Self);
end;
else
inherited ExecuteVerb(Index);
end;
end;
{ TSynEdOptionsPropertyEditor }
procedure TSynEdOptionsPropertyEditor.GetProperties(Proc: TGetPropEditProc);
var
I: Integer;
begin
with GetTypeData(GetTypeData(GetPropType)^.CompType)^ do
for I := MinValue to MaxValue do
if not(TSynEditorOption(I) in SYNEDIT_UNIMPLEMENTED_OPTIONS)
then
Proc(TSetElementPropertyEditor.Create(Self, I));
end;
{ TSynKeyCommandPropertyEditor }
function TSynKeyCommandPropertyEditor.GetAttributes: TPropertyAttributes;
begin
Result := [paMultiSelect, paValueList, paRevertable];
end;
function TSynKeyCommandPropertyEditor.OrdValueToVisualValue(OrdValue: longint): string;
begin
Result:='';
if not EditorCommandToIdent(OrdValue, Result) then
Result := inherited OrdValueToVisualValue(OrdValue);
end;
procedure TSynKeyCommandPropertyEditor.GetValues(Proc: TGetStrProc);
var
CValue: Integer;
begin
CValue:=0;
if not IdentToSynMouseCmd(GetVisualValue, CValue) then Proc(GetVisualValue);
GetEditorCommandValues(Proc);
end;
procedure TSynKeyCommandPropertyEditor.SetValue(const NewValue: ansistring);
var
CValue: Integer;
begin
CValue:=0;
if IdentToEditorCommand(NewValue, CValue) then SetOrdValue(CValue)
else inherited SetValue(NewValue);
end;
{ TSynMouseCommandPropertyEditor }
function TSynMouseCommandPropertyEditor.GetAttributes: TPropertyAttributes;
begin
Result := [paMultiSelect, paValueList, paRevertable];
end;
function TSynMouseCommandPropertyEditor.OrdValueToVisualValue(OrdValue: longint): string;
begin
Result:='';
if not SynMouseCmdToIdent(OrdValue, Result) then
Result := inherited OrdValueToVisualValue(OrdValue);
end;
procedure TSynMouseCommandPropertyEditor.GetValues(Proc: TGetStrProc);
var
CValue: Integer;
CName: String;
i: TSynEditorMouseCommand;
begin
CValue:=0;
CName:='';
if not IdentToSynMouseCmd(GetVisualValue, CValue) then Proc(GetVisualValue);
for i := 0 to emcMax do
if SynMouseCmdToIdent(i, CName) then Proc(CName);
end;
procedure TSynMouseCommandPropertyEditor.SetValue(const NewValue: ansistring);
var
CValue: Integer;
begin
CValue:=0;
if IdentToSynMouseCmd(NewValue, CValue) then SetOrdValue(CValue)
else inherited SetValue(NewValue);
end;
{ TSynObjectPartListPropertyEditorForm }
procedure TSynObjectPartListPropertyEditorForm.AddButtonClick(Sender: TObject);
var
i: Integer;
NewPart: TSynObjectListItem;
begin
if (SynObjectPartList = nil) or (FClassesList = nil) then Exit;
i := ClassComboBox.ItemIndex;
if (i < 0) or (i >= FClassesList.Count) then
exit;
NewPart := TSynObjectListItemClass(Pointer(FClassesList.Objects[i])).Create(SynObjectPartList);
NewPart.Name := FormEditingHook.CreateUniqueComponentName(NewPart.ClassName, SynObjectPartList);
FillSynObjectPartsListBox;
if SynObjectPartsListBox.Items.Count > 0 then
SynObjectPartsListBox.ItemIndex := SynObjectPartsListBox.Items.Count - 1;
SelectInObjectInspector(False);
UpdateButtons;
UpdateCaption;
Modified;
end;
procedure TSynObjectPartListPropertyEditorForm.SynObjectPartsListBoxClick(Sender: TObject);
begin
UpdateButtons;
UpdateCaption;
SelectInObjectInspector(False);
end;
procedure TSynObjectPartListPropertyEditorForm.DeleteButtonClick(Sender: TObject);
var
I : Integer;
NewItemIndex: Integer;
begin
if SynObjectPartList = nil then Exit;
I := SynObjectPartsListBox.ItemIndex;
if (I >= 0) and (I < SynObjectPartList.Count) then
begin
if MessageDlg(oisConfirmDelete,
Format(oisDeleteItem, [SynObjectPartList.BaseItems[I].DisplayName]),
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
// select other item, or unselect
NewItemIndex := I + 1;
while (NewItemIndex < SynObjectPartsListBox.Items.Count)
and (SynObjectPartsListBox.Selected[NewItemIndex]) do Inc(NewItemIndex);
if NewItemIndex = SynObjectPartsListBox.Items.Count then
begin
NewItemIndex := 0;
while (NewItemIndex < Pred(I))
and not (SynObjectPartsListBox.Selected[NewItemIndex]) do Inc(NewItemIndex);
if NewItemIndex = I then NewItemIndex := -1;
end;
SynObjectPartsListBox.ItemIndex := -1;
if NewItemIndex > I then Dec(NewItemIndex);
// unselect all items in OI
SelectInObjectInspector(True);
// now delete
SynObjectPartList.BaseItems[I].Free;
// update listbox after whatever happened
FillSynObjectPartsListBox;
// set NewItemIndex
if NewItemIndex < SynObjectPartsListBox.Items.Count then
begin
SynObjectPartsListBox.ItemIndex := NewItemIndex;
SelectInObjectInspector(False);
end;
Modified;
end;
end;
UpdateButtons;
UpdateCaption;
end;
procedure TSynObjectPartListPropertyEditorForm.FormCreate(Sender: TObject);
begin
ToolBar1.Images := IDEImages.Images_16;
AddButton.Caption := oiColEditAdd;
DeleteButton.Caption := oiColEditDelete;
MoveUpButton.Caption := oiColEditUp;
MoveDownButton.Caption := oiColEditDown;
AddButton.ImageIndex := IDEImages.LoadImage('laz_add');
DeleteButton.ImageIndex := IDEImages.LoadImage('laz_delete');
MoveUpButton.ImageIndex := IDEImages.LoadImage('arrow_up');
MoveDownButton.ImageIndex := IDEImages.LoadImage('arrow_down');
end;
procedure TSynObjectPartListPropertyEditorForm.FormDestroy(Sender: TObject);
begin
if GlobalDesignHook <> nil then
GlobalDesignHook.RemoveAllHandlersForObject(Self);
end;
procedure TSynObjectPartListPropertyEditorForm.MoveDownButtonClick(Sender: TObject);
var
I: Integer;
begin
if SynObjectPartList = nil then Exit;
I := SynObjectPartsListBox.ItemIndex;
if I >= SynObjectPartList.Count - 1 then Exit;
SynObjectPartList.BaseItems[I].Index := I + 1;
SynObjectPartsListBox.ItemIndex := I + 1;
FillSynObjectPartsListBox;
SelectInObjectInspector(False);
Modified;
end;
procedure TSynObjectPartListPropertyEditorForm.MoveUpButtonClick(Sender: TObject);
var
I: Integer;
begin
if SynObjectPartList = nil then Exit;
I := SynObjectPartsListBox.ItemIndex;
if I < 0 then Exit;
SynObjectPartList.BaseItems[I].Index := I - 1;
SynObjectPartsListBox.ItemIndex := I - 1;
FillSynObjectPartsListBox;
SelectInObjectInspector(False);
Modified;
end;
procedure TSynObjectPartListPropertyEditorForm.UpdateCaption;
var
NewCaption: String;
begin
//"Editing ComponentName.PropertyName[Index]"
if OwnerPersistent is TComponent then
NewCaption := TComponent(OwnerPersistent).Name
else
if OwnerPersistent <> nil then
NewCaption := OwnerPersistent.GetNamePath
else
NewCaption := '';
if NewCaption <> '' then NewCaption := NewCaption + '.';
NewCaption := oiColEditEditing + ' ' + NewCaption + PropertyName;
if SynObjectPartsListBox.ItemIndex > -1 then
NewCaption := NewCaption + '[' + IntToStr(SynObjectPartsListBox.ItemIndex) + ']';
Caption := NewCaption;
end;
procedure TSynObjectPartListPropertyEditorForm.UpdateButtons;
var
I: Integer;
begin
if SynObjectPartList = nil then begin
AddButton.Enabled := False;
DeleteButton.Enabled := False;
MoveUpButton.Enabled := False;
MoveDownButton.Enabled := False;
exit;
end;
I := SynObjectPartsListBox.ItemIndex;
AddButton.Enabled := True;
DeleteButton.Enabled := I > -1;
MoveUpButton.Enabled := I > 0;
MoveDownButton.Enabled := (I >= 0) and (I < SynObjectPartList.Count - 1);
end;
procedure TSynObjectPartListPropertyEditorForm.ComponentRenamed(AComponent: TComponent);
begin
if AComponent = OwnerPersistent then UpdateCaption;
end;
procedure TSynObjectPartListPropertyEditorForm.PersistentDeleting(APersistent: TPersistent);
var
OldSynObjectPartList: TSynObjectList;
I: Integer;
begin
debugln(['TSynObjectPartListPropertyEditorForm.PersistentDeleting ']);
if APersistent = OwnerPersistent then
begin
OldSynObjectPartList := SynObjectPartList;
SetSynObjectPartList(nil, nil, nil, '');
GlobalDesignHook.Unselect(OldSynObjectPartList);
for I := 0 to OldSynObjectPartList.Count - 1 do
GlobalDesignHook.Unselect(OldSynObjectPartList.BaseItems[I]);
if GlobalDesignHook.LookupRoot = OldSynObjectPartList then
GlobalDesignHook.LookupRoot := nil;
Hide;
end;
end;
procedure TSynObjectPartListPropertyEditorForm.RefreshPropertyValues;
begin
FillSynObjectPartsListBox;
Modified;
end;
procedure TSynObjectPartListPropertyEditorForm.FillSynObjectPartsListBox;
var
I: Integer;
CurItem: String;
Cnt: Integer;
begin
SynObjectPartsListBox.Items.BeginUpdate;
try
if SynObjectPartList <> nil then Cnt := SynObjectPartList.Count
else Cnt := 0;
// add or replace list items
for I := 0 to Cnt - 1 do
begin
CurItem := IntToStr(I) + ' - ' + SynObjectPartList.BaseItems[I].DisplayName;
if I >= SynObjectPartsListBox.Items.Count then
SynObjectPartsListBox.Items.Add(CurItem)
else
SynObjectPartsListBox.Items[I] := CurItem;
end;
// delete unneeded list items
if Cnt > 0 then
begin
while SynObjectPartsListBox.Items.Count > Cnt do
begin
SynObjectPartsListBox.Items.Delete(SynObjectPartsListBox.Items.Count - 1);
end;
end
else
begin
SynObjectPartsListBox.Items.Clear;
end;
finally
SynObjectPartsListBox.Items.EndUpdate;
UpdateButtons;
UpdateCaption;
end;
end;
procedure TSynObjectPartListPropertyEditorForm.SelectInObjectInspector(UnselectAll: Boolean);
var
I: Integer;
NewSelection: TPersistentSelectionList;
begin
if SynObjectPartList = nil then Exit;
// select in OI
NewSelection := TPersistentSelectionList.Create;
try
if not UnselectAll then
begin
for I := 0 to SynObjectPartsListBox.Items.Count - 1 do
if SynObjectPartsListBox.Selected[I] then
NewSelection.Add(SynObjectPartList.BaseItems[I]);
end;
GlobalDesignHook.SetSelection(NewSelection);
GlobalDesignHook.LookupRoot := GetLookupRootForComponent(OwnerPersistent);
finally
NewSelection.Free;
end;
end;
procedure TSynObjectPartListPropertyEditorForm.SetSynObjectPartList
(NewSynObjectPartList: TSynObjectList; NewClassesList: TStringList;
NewOwnerPersistent: TPersistent; const NewPropName: String);
begin
if (FSynObjectPartList = NewSynObjectPartList)
and (FClassesList = NewClassesList)
and (FOwnerPersistent = NewOwnerPersistent)
and (FPropertyName = NewPropName) then Exit;
FSynObjectPartList := NewSynObjectPartList;
FClassesList := NewClassesList;
if assigned(FClassesList) then
ClassComboBox.Items.Assign(FClassesList)
else
ClassComboBox.Clear;
if ClassComboBox.Items.Count >0 then
ClassComboBox.ItemIndex := 0;
// Can not use NewOwnerPersistent since it points to the SynGutter
if NewSynObjectPartList <> nil then
FOwnerPersistent := NewSynObjectPartList.Owner // The SynEdit
else
FOwnerPersistent := nil;
FPropertyName := NewPropName;
//debugln('TSynObjectPartListPropertyEditorForm.SetSynObjectPartList A SynObjectPartList=',dbgsName(FSynObjectPartList),' OwnerPersistent=',dbgsName(OwnerPersistent),' PropName=',PropertyName);
if GlobalDesignHook <> nil then
begin
if FOwnerPersistent <> nil then
begin
GlobalDesignHook.AddHandlerComponentRenamed(@ComponentRenamed);
GlobalDesignHook.AddHandlerPersistentDeleting(@PersistentDeleting);
GlobalDesignHook.AddHandlerRefreshPropertyValues(@RefreshPropertyValues);
end
else
begin
GlobalDesignHook.RemoveAllHandlersForObject(Self);
end;
end;
FillSynObjectPartsListBox;
UpdateCaption;
end;
procedure TSynObjectPartListPropertyEditorForm.Modified;
begin
GlobalDesignHook.Modified(Self);
//SelectInObjectInspector(False);
end;
{ TSynPropertyEditObjectList }
function TSynPropertyEditObjectList.ReadElementCount: integer;
var
SynObjectList: TObject;
begin
SynObjectList := GetObjectValue;
if (SynObjectList <> nil) and (SynObjectList is TSynObjectList) then
Result := TSynObjectList(SynObjectList).Count
else
Result:=0;
end;
function TSynPropertyEditObjectList.ReadElement(Index: integer): TPersistent;
var
SynObjectList: TSynObjectList;
begin
SynObjectList := TSynObjectList(GetObjectValue);
Result := SynObjectList.BaseItems[Index];
end;
function TSynPropertyEditObjectList.GetAttributes: TPropertyAttributes;
begin
Result := [paDialog, paReadOnly];
end;
procedure TSynPropertyEditObjectList.Edit;
var
TheSynObjectList: TSynObjectList;
begin
TheSynObjectList := TSynObjectList(GetObjectValue);
if TheSynObjectList = nil then
raise Exception.Create('ObjectPartList=nil');
ShowClassListEditor(TheSynObjectList, GetComponent(0), GetName);
end;
class function TSynPropertyEditObjectList.ShowClassListEditor(AClassList: TSynObjectList;
OwnerPersistent: TPersistent; const PropName: String): TCustomForm;
begin
if SynObjectPartListForm = nil then
SynObjectPartListForm := TSynObjectPartListPropertyEditorForm.Create(Application);
SynObjectPartListForm.SetSynObjectPartList(AClassList, ClassList, OwnerPersistent, PropName);
SynObjectPartListForm.EnsureVisible;
Result := SynObjectPartListForm;
end;
{ TSynPropertyEditGutterPartList }
class function TSynPropertyEditGutterPartList.ClassList: TStringList;
begin
Result := KnownSynGutterPartClasses;
end;
initialization
if KnownSynGutterPartClasses = nil then
KnownSynGutterPartClasses := TStringList.Create;
finalization
FreeAndNil(KnownSynGutterPartClasses);
end.
|