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
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Author: Mattias Gaertner
Abstract:
Extension for the Object Inspector.
- Favorites properties
}
unit ObjInspExt;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,
// LCL
LCLProc, Forms, Controls, Buttons, StdCtrls,
ExtCtrls, Dialogs, Menus, ComCtrls, Grids, CustomTimer,
// LazUtils
LazFileUtils, LazConfigStorage,
// CodeTools
CodeToolManager, CodeCache,
// IdeIntf
BaseIDEIntf, LazIDEIntf, ProjectIntf, ObjectInspector, OIFavoriteProperties,
PropEdits, IDEDialogs,
// IDE
DialogProcs, LazConf, LazarusIDEStrConsts;
type
{ TOIAddRemoveFavoriteDlg }
TOIAddRemoveFavoriteDlg = class(TForm)
NoteLabel: TLabel;
ClassCombobox: TComboBox;
OkButton: TButton;
CancelButton: TButton;
procedure OkButtonClick(Sender: TObject);
private
FAddMode: Boolean;
FObjectInspector: TObjectInspectorDlg;
FPropertyName: string;
procedure SetAddMode(const AValue: Boolean);
procedure SetObjectInspector(const AValue: TObjectInspectorDlg);
procedure UpdateLabel;
procedure UpdateComboBox;
procedure UpdateMode;
public
constructor Create(TheOwner: TComponent); override;
public
property ObjectInspector: TObjectInspectorDlg read FObjectInspector
write SetObjectInspector;
property PropertyName: string read FPropertyName;
property AddMode: Boolean read FAddMode write SetAddMode;
end;
const
DefaultOIFavoriteConfigFilename = 'objectinspectorfavorites.xml';
var
DefaultOIFavoriteProperties: TOIFavoriteProperties;
function ShowAddRemoveFavoriteDialog(ObjInspector: TObjectInspectorDlg;
Add: Boolean): TModalResult;
function CreateDefaultOIFavoriteProperties: TOIFavoriteProperties;
function LoadOIFavoriteProperties: TOIFavoriteProperties;
procedure SaveOIFavoriteProperties(Favorites: TOIFavoriteProperties);
function GetOIFavoriteConfigFilename: string;
function FindDeclarationOfOIProperty(AnInspector: TObjectInspectorDlg;
Row: TOIPropertyGridRow; out Code: TCodeBuffer; out Caret: TPoint;
out NewTopLine: integer): Boolean;
implementation
function CreateDefaultOIFavoriteProperties: TOIFavoriteProperties;
procedure Add(ABaseClass: TPersistentClass; const APropertyName: string);
begin
Result.Add(TOIFavoriteProperty.Create(ABaseClass,APropertyName,true));
end;
begin
Result:=TOIFavoriteProperties.Create;
// TControl
Add(TComponent,'Name');
Add(TComponent,'Caption');
Add(TControl,'Anchors');
Add(TControl,'AutoSize');
Add(TControl,'OnClick');
Add(TControl,'OnEditingDone');
// miscellaneous
Add(TCustomGroupBox,'Align');
Add(TCustomImage,'Align');
Add(TCustomButton,'ModalResult');
Add(TCustomLabel,'WordWrap');
Add(TCustomEdit,'Text');
Add(TCustomMemo,'Lines');
Add(TCustomMemo,'Align');
Add(TCustomMemo,'ScrollBars');
Add(TCustomCheckBox,'Checked');
Add(TCustomRadioGroup,'Items');
Add(TCustomRadioGroup,'ItemIndex');
Add(TCustomForm,'OnCreate');
Add(TCustomForm,'OnDestroy');
Add(TCustomForm,'OnResize');
Add(TCustomListBox,'Items');
Add(TCustomListBox,'Align');
Add(TCustomTreeView,'Align');
Add(TCustomTreeView,'Options');
Add(TCustomPanel,'Align');
Add(TMenuItem,'OnClick');
Add(TCustomSpeedButton,'GroupIndex');
Add(TCustomSpeedButton,'Glyph');
Add(TCustomImage,'Picture');
Add(TCustomImage,'Align');
Add(TCustomTabControl,'Align');
Add(TScrollBox,'Align');
Add(TCustomGrid,'Align');
Add(TCustomGrid,'Options');
Add(TCustomGrid,'Columns');
Add(TCustomGrid,'ColCount');
Add(TCustomTreeView,'Align');
Add(TCustomTreeView,'Options');
Add(TCustomTimer,'OnTimer');
Result.DeleteDoubles;
end;
function ShowAddRemoveFavoriteDialog(ObjInspector: TObjectInspectorDlg;
Add: Boolean): TModalResult;
var
OIAddRemoveFavoriteDlg: TOIAddRemoveFavoriteDlg;
begin
OIAddRemoveFavoriteDlg:=TOIAddRemoveFavoriteDlg.Create(nil);
OIAddRemoveFavoriteDlg.ObjectInspector:=ObjInspector;
OIAddRemoveFavoriteDlg.AddMode:=Add;
Result:=OIAddRemoveFavoriteDlg.ShowModal;
OIAddRemoveFavoriteDlg.Free;
end;
function LoadOIFavoriteProperties: TOIFavoriteProperties;
var
ConfigStore: TConfigStorage;
begin
Result:=DefaultOIFavoriteProperties.CreateCopy;
{$IFDEF DebugFavoriteroperties}
debugln('LoadOIFavoriteProperties A FileExistsUTF8(GetOIFavoriteConfigFilename)=',dbgs(FileExistsUTF8(GetOIFavoriteConfigFilename)));
Result.WriteDebugReport;
{$ENDIF}
if not FileExistsUTF8(GetOIFavoriteConfigFilename) then exit;
try
ConfigStore:=DefaultConfigClass.Create(GetOIFavoriteConfigFilename,true);
try
Result.MergeConfig(ConfigStore,'ObjectInspector/Favorites/');
Result.Modified:=false;
finally
ConfigStore.Free;
end;
except
on E: Exception do begin
debugln('Error: LoadOIFavoriteProperties: unable to read ',
GetOIFavoriteConfigFilename);
end;
end;
end;
procedure SaveOIFavoriteProperties(Favorites: TOIFavoriteProperties);
var
ConfigStore: TConfigStorage;
DefaultFavorites: TOIFavoriteProperties;
begin
{$IFDEF DebugFavoriteroperties}
debugln('SaveOIFavoriteProperties Favorites.Modified=',dbgs(Favorites.Modified),
' FileExistsUTF8(GetOIFavoriteConfigFilename)=',dbgs(FileExistsUTF8(GetOIFavoriteConfigFilename)));
{$ENDIF}
if (not Favorites.Modified) and FileExistsUTF8(GetOIFavoriteConfigFilename)
then
exit;
DefaultFavorites:=CreateDefaulTOIFavoriteProperties;
try
if DefaultFavorites.IsEqual(Favorites) then exit;
{$IFDEF DebugFavoriteroperties}
debugln('SaveOIFavoriteProperties is not default');
DefaultFavorites.WriteDebugReport;
Favorites.WriteDebugReport;
{$ENDIF}
try
ConfigStore:=DefaultConfigClass.Create(GetOIFavoriteConfigFilename,false);
try
Favorites.SaveNewItemsToConfig(ConfigStore,'ObjectInspector/Favorites/',
DefaultFavorites);
ConfigStore.WriteToDisk;
Favorites.Modified:=false;
finally
ConfigStore.Free;
end;
except
on E: Exception do begin
debugln('Error: LoadOIFavoriteProperties: unable to write ',
GetOIFavoriteConfigFilename);
end;
end;
finally
DefaultFavorites.Free;
end;
end;
function GetOIFavoriteConfigFilename: string;
begin
Result:=AppendPathDelim(GetPrimaryConfigPath)+DefaultOIFavoriteConfigFilename;
end;
function FindDeclarationOfOIProperty(AnInspector: TObjectInspectorDlg;
Row: TOIPropertyGridRow; out Code: TCodeBuffer; out Caret: TPoint;
out NewTopLine: integer): Boolean;
var
PropPath: String;
LookupRoot: TPersistent;
AFile: TLazProjectFile;
NewCode: TCodeBuffer;
NewX, NewY: integer;
APersistent: TPersistent;
AnUnitName: String;
InFilename: String;
FilenameOfClass: string;
begin
Result:=false;
Code:=nil;
Caret:=Point(0,0);
// check Row
if AnInspector=nil then begin
DebugLn('FindDeclarationOfOIProperty AnInspector=nil');
exit;
end;
if Row=nil then
Row:=AnInspector.GetActivePropertyRow;
if Row=nil then begin
DebugLn('FindDeclarationOfOIProperty Row=nil');
exit;
end;
if Row.Editor=nil then begin
DebugLn('FindDeclarationOfOIProperty Row.Editor=nil Row=',Row.Name);
exit;
end;
// get first instance of property
APersistent:=Row.Editor.GetComponent(0);
if APersistent=nil then begin
DebugLn('FindDeclarationOfOIProperty APersistent=nil Row=',Row.Name);
exit;
end;
// get unit name of first instance
AnUnitName:=GetClassUnitName(APersistent.ClassType);
if AnUnitName='' then begin
DebugLn('FindDeclarationOfOIProperty no RTTI unit found for APersistent.ClassType=',DbgSName(APersistent.ClassType));
exit;
end;
// get lookup root
if Row.Editor.PropertyHook=nil then begin
debugln(['FindDeclarationOfOIProperty Row.Editor.PropertyHook=nil Row=',Row.Name]);
exit;
end;
LookupRoot:=Row.Editor.PropertyHook.LookupRoot;
if LookupRoot=nil then begin
debugln(['FindDeclarationOfOIProperty Row.Editor.PropertyHook.LookupRoot=nil Row=',Row.Name]);
exit;
end;
// get file of lookup root
AFile:=LazarusIDE.GetProjectFileWithRootComponent(TComponent(LookupRoot));
if AFile=nil then begin
DebugLn('FindDeclarationOfOIProperty AFile=nil Row=',Row.Name,' LookupRoot=',DbgSName(LookupRoot));
exit;
end;
InFilename:='';
FilenameOfClass:=CodeToolBoss.DirectoryCachePool.FindUnitSourceInCompletePath(
ExtractFilePath(AFile.Filename),AnUnitName,InFilename);
if FilenameOfClass='' then begin
debugln(['FindDeclarationOfOIProperty FindUnitSourceInCompletePath failed: Row=',Row.Name,' Instance=',DbgSName(APersistent),' LookupRoot=',DbgSName(LookupRoot),' Unit not found: ',AnUnitName,' started search in directory of lookuproot: ',AFile.Filename]);
exit;
end;
if not LazarusIDE.BeginCodeTools then begin
DebugLn('FindDeclarationOfOIProperty LazarusIDE.BeginCodeTools failed');
exit;
end;
Code:=nil;
if LoadCodeBuffer(Code,FilenameOfClass,[],false)<>mrOk then begin
debugln(['FindDeclarationOfOIProperty LoadCodeBuffer failed of ',FilenameOfClass]);
exit;
end;
// find the property declaration
PropPath:=APersistent.ClassName+'.'+Row.Name;
if Row.Editor is TNestedPropertyEditor then begin
if Row.Parent=nil then begin
debugln(['FindDeclarationOfOIProperty missing parent row ',PropPath,' in unit ',Code.Filename,' Row.Editor=',DbgSName(Row.Editor)]);
exit;
end;
PropPath:=APersistent.ClassName+'.'+Row.Parent.Name+'.'+Row.Name;
end;
if not CodeToolBoss.FindDeclarationOfPropertyPath(Code,PropPath,NewCode,
NewX,NewY,NewTopLine) then
begin
debugln(['FindDeclarationOfOIProperty failed to find property ',PropPath,' in unit ',Code.Filename]);
exit;
end;
Code:=NewCode;
Caret:=Point(NewX,NewY);
//DebugLn('FindDeclarationOfOIProperty SUCCESS ',Code.Filename,' ',dbgs(Caret));
Result:=true;
end;
{ TOIAddRemoveFavoriteDlg }
procedure TOIAddRemoveFavoriteDlg.OkButtonClick(Sender: TObject);
var
NewClassName: String;
CurClass: TClass;
NewFavorite: TOIFavoriteProperty;
begin
NewClassName:=ClassCombobox.Text;
if (ObjectInspector<>nil) and (ObjectInspector.Selection<>nil)
and (ObjectInspector.Selection.Count>0) then begin
CurClass:=ObjectInspector.Selection[0].ClassType;
while CurClass.InheritsFrom(TPersistent) do begin
if CompareText(NewClassName,CurClass.ClassName)=0 then begin
NewFavorite:=TOIFavoriteProperty.Create(TPersistentClass(CurClass),
PropertyName,AddMode);
ObjectInspector.Favorites.DeleteConstraints(NewFavorite);
ObjectInspector.Favorites.Add(NewFavorite);
ObjectInspector.FavoriteGrid.BuildPropertyList;
ModalResult:=mrOk;
exit;
end;
CurClass:=CurClass.ClassParent;
end;
end;
IDEMessageDialog(lisClassNotFound,
Format(lisOIFClassNotFound, [NewClassName]), mtError, [mbOk]);
end;
procedure TOIAddRemoveFavoriteDlg.SetObjectInspector(const AValue: TObjectInspectorDlg);
var
CurRow: TOIPropertyGridRow;
begin
if FObjectInspector=AValue then exit;
FObjectInspector:=AValue;
CurRow:=ObjectInspector.GetActivePropertyRow;
if (CurRow<>nil) and (CurRow.Editor<>nil) then
FPropertyName:=CurRow.Editor.GetName;
UpdateLabel;
UpdateComboBox;
end;
procedure TOIAddRemoveFavoriteDlg.SetAddMode(const AValue: Boolean);
begin
if FAddMode=AValue then exit;
FAddMode:=AValue;
UpdateMode;
end;
procedure TOIAddRemoveFavoriteDlg.UpdateLabel;
begin
NoteLabel.Caption:=Format(lisOIFChooseABaseClassForTheFavoriteProperty, [PropertyName]);
end;
procedure TOIAddRemoveFavoriteDlg.UpdateComboBox;
var
CurClass: TClass;
NewItems: TStringList;
begin
NewItems:=TStringList.Create;
if (ObjectInspector<>nil) and (ObjectInspector.Selection<>nil)
and (ObjectInspector.Selection.Count>0) then begin
CurClass:=ObjectInspector.Selection[0].ClassType;
// add only classes, that are TPersistent and have a registered class
while CurClass.InheritsFrom(TPersistent) do begin
// add only registered classes
if GetClass(CurClass.ClassName)<>nil then
NewItems.Add(CurClass.ClassName);
CurClass:=CurClass.ClassParent;
end;
end;
ClassCombobox.Items.Assign(NewItems);
if ClassCombobox.Items.Count>0 then
ClassCombobox.ItemIndex:=0;
NewItems.Free;
end;
procedure TOIAddRemoveFavoriteDlg.UpdateMode;
begin
if AddMode then begin
Caption:=lisOIFAddToFavoriteProperties;
OkButton.Caption:=lisAdd;
end else begin
Caption:=lisOIFRemoveFromFavoriteProperties;
OkButton.Caption:=lisRemove;
end;
end;
constructor TOIAddRemoveFavoriteDlg.Create(TheOwner: TComponent);
begin
inherited CreateNew(TheOwner);
Name:='OIAddToFavoriteDlg';
Width:=300;
Height:=150;
Position:=poScreenCenter;
NoteLabel:=TLabel.Create(Self);
with NoteLabel do begin
Name:='NoteLabel';
SetBounds(5,5,Self.ClientWidth-10,50);
WordWrap:=true;
Parent:=Self;
end;
ClassCombobox:=TComboBox.Create(Self);
with ClassCombobox do begin
Name:='ClassCombobox';
SetBounds(5,60,200,Height);
Parent:=Self;
end;
OkButton:=TButton.Create(Self);
with OkButton do begin
Name:='AddButton';
SetBounds(5,100,80,25);
Caption:=lisAdd;
Parent:=Self;
OnClick:=@OkButtonClick;
end;
DefaultControl:=OkButton;
CancelButton:=TButton.Create(Self);
with CancelButton do begin
Name:='CancelButton';
SetBounds(120,100,80,25);
Caption:=lisCancel;
Parent:=Self;
ModalResult:=mrCancel;
end;
CancelControl:=CancelButton;
UpdateMode;
end;
initialization
DefaultOIFavoriteProperties:=CreateDefaultOIFavoriteProperties;
finalization
FreeAndNil(DefaultOIFavoriteProperties)
end.
|