/usr/lib/lazarus/0.9.30.4/designer/changeclassdialog.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | { /***************************************************************************
ChangeClassDialog.pas - Lazarus IDE unit
----------------------------------------
***************************************************************************/
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code 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 *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
Abstract:
Functions and Dialog to change the class of a designer component.
}
unit ChangeClassDialog;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LCLProc, LResources, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons, AVGLvlTree, LFMTrees, CodeCache, CodeToolManager, LCLType,
// IDE
SrcEditorIntf, PropEdits, LazarusIDEStrConsts, ComponentReg, ComponentEditors,
FormEditingIntf, CheckLFMDlg, Project, MainIntf, ExtCtrls, ButtonPanel;
type
{ TChangeClassDlg }
TChangeClassDlg = class(TForm)
BtnPanel: TButtonPanel;
NewClassComboBox: TComboBox;
NewAncestorsListBox: TListBox;
OldAncestorsListBox: TListBox;
OldClassLabel: TLabel;
NewGroupBox: TGroupBox;
OldGroupBox: TGroupBox;
procedure ChangeClassDlgCreate(Sender: TObject);
procedure NewClassComboBoxEditingDone(Sender: TObject);
procedure NewClassComboBoxKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
FClasses: TAvgLvlTree;
FNewClass: TClass;
FThePersistent: TPersistent;
procedure SetNewClass(const AValue: TClass);
procedure SetThePersistent(const AValue: TPersistent);
procedure UpdateInfo;
procedure UpdateOldInfo;
procedure UpdateNewInfo;
procedure FillAncestorListBox(AClass: TClass; AListBox: TListBox);
procedure AddClass(const AClass: TPersistentClass);
procedure AddComponentClass(const AClass: TComponentClass);
function CompareClasses(Tree: TAvgLvlTree; Class1, Class2: TClass): integer;
public
destructor Destroy; override;
procedure FillNewClassComboBox;
property ThePersistent: TPersistent read FThePersistent write SetThePersistent;
property NewClass: TClass read FNewClass write SetNewClass;
end;
function ShowChangeClassDialog(ADesigner: TIDesigner;
APersistent: TPersistent): TModalResult;
function ChangePersistentClass(ADesigner: TIDesigner;
APersistent: TPersistent; NewClass: TClass): TModalResult;
implementation
{$R *.lfm}
function ShowChangeClassDialog(ADesigner: TIDesigner;
APersistent: TPersistent): TModalResult;
var
ChangeClassDlg: TChangeClassDlg;
begin
Result:=mrCancel;
//MessageDlg('Not implemented yet','Not implemented yet',mtInformation,[mbOk],0);
//exit;
ChangeClassDlg:=TChangeClassDlg.Create(nil);
try
ChangeClassDlg.ThePersistent:=APersistent;
ChangeClassDlg.FillNewClassComboBox;
if ChangeClassDlg.ShowModal=mrOk then begin
Result:=ChangePersistentClass(ADesigner,APersistent,
ChangeClassDlg.NewClass);
end;
finally
ChangeClassDlg.Free;
end;
end;
function ChangePersistentClass(ADesigner: TIDesigner;
APersistent: TPersistent; NewClass: TClass): TModalResult;
var
ComponentStream: TMemoryStream;
PersistentName: String;
UnitCode: TCodeBuffer;
LFMBuffer: TCodeBuffer;
LFMTree: TLFMTree;
UnitInfo: TUnitInfo;
OldParents: TStrings; // Name=OldParent pairs
procedure ShowAbortMessage(const Msg: string);
begin
MessageDlg('Error',
Format(lisUnableToChangeClassOfTo, [Msg, #13, PersistentName,
NewClass.ClassName]),
mtError,[mbCancel],0);
end;
function StreamSelection: boolean;
begin
Result:=false;
// select only this persistent
GlobalDesignHook.SelectOnlyThis(APersistent);
if (APersistent is TControl)
and (TControl(APersistent).Parent<>nil) then begin
if OldParents=nil then
OldParents:=TStringList.Create;
OldParents.Values[TControl(APersistent).Name]:=
TControl(APersistent).Parent.Name;
end;
// stream selection
ComponentStream:=TMemoryStream.Create;
if (not FormEditingHook.SaveSelectionToStream(ComponentStream))
or (ComponentStream.Size=0) then begin
ShowAbortMessage(lisUnableToStreamSelectedComponents2);
exit;
end;
Result:=true;
end;
function ParseLFMStream: boolean;
var
SrcEdit: TSourceEditorInterface;
Msg: String;
begin
Result:=false;
if not CodeToolBoss.GatherExternalChanges then begin
ShowAbortMessage(lisUnableToGatherEditorChanges);
exit;
end;
MainIDEInterface.GetUnitInfoForDesigner(ADesigner,SrcEdit,UnitInfo);
if UnitInfo=nil then begin
ShowAbortMessage(lisUnableToGetSourceForDesigner);
exit;
end;
UnitCode:=UnitInfo.Source;
LFMBuffer:=CodeToolBoss.CreateTempFile('changeclass.lfm');
if (LFMBuffer=nil) or (ComponentStream.Size=0) then begin
ShowAbortMessage(lisUnableToCreateTemporaryLfmBuffer);
exit;
end;
ComponentStream.Position:=0;
LFMBuffer.LoadFromStream(ComponentStream);
//debugln('ChangePersistentClass-Before-Checking--------------------------------------------');
//debugln(LFMBuffer.Source);
//debugln('ChangePersistentClass-Before-Checking-------------------------------------------');
if not CodeToolBoss.CheckLFM(UnitCode,LFMBuffer,LFMTree,false,false,false) then
begin
debugln('ChangePersistentClass-Before--------------------------------------------');
debugln(LFMBuffer.Source);
debugln('ChangePersistentClass-Before--------------------------------------------');
if CodeToolBoss.ErrorMessage<>'' then
MainIDEInterface.DoJumpToCodeToolBossError
else begin
Msg:=lisErrorParsingLfmComponentStream;
if LFMTree<>nil then Msg:=Msg+#13#13+LFMTree.FirstErrorAsString+#13;
ShowAbortMessage(Msg);
end;
exit;
end;
Result:=true;
end;
function ChangeClassName: boolean;
var
CurNode: TLFMTreeNode;
ObjectNode: TLFMObjectNode;
begin
Result:=false;
// find classname position
CurNode:=LFMTree.Root;
while CurNode<>nil do begin
if (CurNode is TLFMObjectNode) then begin
ObjectNode:=TLFMObjectNode(CurNode);
if (CompareText(ObjectNode.Name,(APersistent as TComponent).Name)=0)
and (CompareText(ObjectNode.TypeName,APersistent.ClassName)=0) then begin
// replace classname
LFMBuffer.Replace(ObjectNode.TypeNamePosition,length(ObjectNode.TypeName),
NewClass.ClassName);
Result:=true;
exit;
end;
end;
CurNode:=CurNode.NextSibling;
end;
ShowAbortMessage(Format(lisUnableToFindInLFMStream, [PersistentName]));
end;
function CheckProperties: boolean;
begin
Result:=RepairLFMBuffer(UnitCode,LFMBuffer,nil,false,false,false)=mrOk;
if not Result and (CodeToolBoss.ErrorMessage<>'') then
MainIDEInterface.DoJumpToCodeToolBossError;
end;
function InsertStreamedSelection: boolean;
var
MemStream: TMemoryStream;
LFMType, LFMComponentName, LFMClassName: string;
AComponent: TComponent;
NewParent: TWinControl;
NewParentName: string;
begin
Result:=false;
if LFMBuffer.SourceLength=0 then exit;
MemStream:=TMemoryStream.Create;
try
debugln('ChangePersistentClass-After--------------------------------------------');
debugln(LFMBuffer.Source);
debugln('ChangePersistentClass-After--------------------------------------------');
LFMBuffer.SaveToStream(MemStream);
MemStream.Position:=0;
NewParent:=nil;
if OldParents<>nil then begin
ReadLFMHeader(MemStream,LFMType,LFMComponentName,LFMClassName);
if (LFMType='') or (LFMClassName='') then ;
MemStream.Position:=0;
if LFMComponentName<>'' then begin
NewParentName:=OldParents.Values[LFMComponentName];
if NewParentName<>'' then begin
AComponent:=GlobalDesignHook.GetComponent(NewParentName);
if AComponent is TWinControl then
NewParent:=TWinControl(AComponent);
end;
end;
end;
Result:=FormEditingHook.InsertFromStream(MemStream,NewParent,
[cpsfReplace]);
if not Result then
ShowAbortMessage(lisReplacingSelectionFailed);
finally
MemStream.Free;
end;
end;
begin
Result:=mrCancel;
if CompareText(APersistent.ClassName,NewClass.ClassName)=0 then begin
Result:=mrOk;
exit;
end;
PersistentName:=APersistent.ClassName;
if APersistent is TComponent then begin
PersistentName:=TComponent(APersistent).Name+':'+PersistentName;
end else begin
ShowAbortMessage(lisCanOnlyChangeTheClassOfTComponents);
exit;
end;
ComponentStream:=nil;
LFMTree:=nil;
OldParents:=nil;
try
if not StreamSelection then exit;
if not ParseLFMStream then exit;
if not ChangeClassName then exit;
if not CheckProperties then exit;
if not InsertStreamedSelection then exit;
finally
ComponentStream.Free;
OldParents.Free;
// Note: do not free LFMTree, it is cached by the codetools
end;
Result:=mrOk;
end;
{ TChangeClassDlg }
procedure TChangeClassDlg.ChangeClassDlgCreate(Sender: TObject);
begin
OldGroupBox.Caption:=lisOldClass;
NewGroupBox.Caption:=lisNewClass;
end;
procedure TChangeClassDlg.NewClassComboBoxEditingDone(Sender: TObject);
begin
UpdateNewInfo;
end;
procedure TChangeClassDlg.NewClassComboBoxKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key = VK_RETURN then
UpdateNewInfo;
end;
procedure TChangeClassDlg.SetThePersistent(const AValue: TPersistent);
begin
if FThePersistent=AValue then exit;
FThePersistent:=AValue;
UpdateInfo;
end;
procedure TChangeClassDlg.SetNewClass(const AValue: TClass);
begin
if FNewClass=AValue then exit;
FNewClass:=AValue;
UpdateNewInfo;
end;
procedure TChangeClassDlg.UpdateInfo;
begin
UpdateNewInfo;
UpdateOldInfo;
end;
procedure TChangeClassDlg.UpdateOldInfo;
begin
FillAncestorListBox(ThePersistent.ClassType,OldAncestorsListBox);
if ThePersistent<>nil then begin
if ThePersistent is TComponent then
OldClassLabel.Caption:=
TComponent(ThePersistent).Name+':'+ThePersistent.ClassName
else
OldClassLabel.Caption:=ThePersistent.ClassName;
Caption:=Format(lisCCDChangeClassOf, [OldClassLabel.Caption]);
end else begin
OldClassLabel.Caption:=lisCCDNoClass;
Caption:=lisChangeClass;
end;
end;
procedure TChangeClassDlg.UpdateNewInfo;
var
ANode: TAvgLvlTreeNode;
begin
FNewClass:=nil;
if FClasses<>nil then begin
ANode:=FClasses.FindLowest;
while (ANode<>nil) do begin
FNewClass:=TClass(ANode.Data);
if (CompareText(NewClass.ClassName,NewClassComboBox.Text)=0) then
break
else
FNewClass:=nil;
ANode:=FClasses.FindSuccessor(ANode);
end;
end;
FillAncestorListBox(NewClass,NewAncestorsListBox);
if NewClass<>nil then
NewClassComboBox.Text:=NewClass.ClassName
else
NewClassComboBox.Text:='';
end;
procedure TChangeClassDlg.FillAncestorListBox(AClass: TClass; AListBox: TListBox
);
var
List: TStringList;
procedure AddAncestor(CurClass: TClass);
begin
if CurClass=nil then exit;
List.Insert(0,CurClass.ClassName);
AddAncestor(CurClass.ClassParent);
end;
begin
List:=TStringList.Create;
AddAncestor(AClass);
AListBox.Items.Assign(List);
List.Free;
end;
procedure TChangeClassDlg.AddClass(const AClass: TPersistentClass);
begin
if FClasses.FindPointer(AClass)<>nil then exit;
FClasses.Add(AClass);
end;
procedure TChangeClassDlg.AddComponentClass(const AClass: TComponentClass);
begin
AddClass(AClass);
end;
function TChangeClassDlg.CompareClasses(Tree: TAvgLvlTree; Class1,
Class2: TClass): integer;
// sort:
// transforming ThePersistent to descending classes is easy
// transforming ThePersistent to ascending classes is medium
//
// count distance between, that means: find nearest shared ancestor, then
// give two points for every step from ThePersistent to ancestor and one point
// for every step from ancestor to class
//
// otherwise sort for classnames
function AncestorDistance(ChildClass, AncestorClass: TClass): integer;
begin
Result:=0;
while (ChildClass<>nil) and (ChildClass<>AncestorClass) do begin
ChildClass:=ChildClass.ClassParent;
inc(Result);
end;
end;
function RelationDistance(SrcClass, DestClass: TClass): integer;
var
Ancestor: TClass;
begin
// find shared ancestor of
Ancestor:=SrcClass;
while (Ancestor<>nil) and (not DestClass.InheritsFrom(Ancestor)) do
Ancestor:=Ancestor.ClassParent;
// going to the ancestor is normally more difficult than going away
Result:=2*AncestorDistance(SrcClass,Ancestor)
+AncestorDistance(DestClass,Ancestor);
end;
var
Dist1: LongInt;
Dist2: LongInt;
begin
Result:=0;
if (ThePersistent<>nil) then begin
Dist1:=RelationDistance(ThePersistent.ClassType,Class1);
Dist2:=RelationDistance(ThePersistent.ClassType,Class2);
Result:=Dist1-Dist2;
if Result<>0 then exit;
end;
Result:=CompareText(Class1.ClassName,Class2.ClassName);
end;
destructor TChangeClassDlg.Destroy;
begin
FClasses.Free;
FClasses:=nil;
inherited Destroy;
end;
procedure TChangeClassDlg.FillNewClassComboBox;
var
ANode: TAvgLvlTreeNode;
List: TStringList;
begin
// create/clear tree
if FClasses=nil then
FClasses:=TAvgLvlTree.CreateObjectCompare(TObjectSortCompare(@CompareClasses))
else
FClasses.Clear;
// add class of ThePersistent
if ThePersistent<>nil then
AddClass(TPersistentClass(ThePersistent.ClassType));
// add all registered component classes
if (IDEComponentPalette<>nil) then
IDEComponentPalette.IterateRegisteredClasses(@AddComponentClass);
// add list of classnames
List:=TStringList.Create;
ANode:=FClasses.FindLowest;
while ANode<>nil do begin
List.Add(TClass(ANode.Data).ClassName);
ANode:=FClasses.FindSuccessor(ANode);
end;
// assign to combobox
NewClassComboBox.Items.Assign(List);
if (NewClassComboBox.Items.IndexOf(NewClassComboBox.Text)<0)
and (NewClassComboBox.Items.Count>0) then
NewClassComboBox.Text:=NewClassComboBox.Items[0];
UpdateNewInfo;
// clean up
List.Free;
end;
end.
|