File: formeditingintf.pas

package info (click to toggle)
lazarus 0.9.30.4-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 114,420 kB
  • sloc: pascal: 1,108,945; xml: 249,481; makefile: 120,941; sh: 2,651; perl: 395; sql: 174; ansic: 137
file content (542 lines) | stat: -rw-r--r-- 19,514 bytes parent folder | download
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
{
 *****************************************************************************
 *                                                                           *
 *  See the file COPYING.modifiedLGPL.txt, included in this distribution,    *
 *  for details about the copyright.                                         *
 *                                                                           *
 *  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.                     *
 *                                                                           *
 *****************************************************************************

  Author: Shane Miller, Mattias Gaertner

  Abstract:
    Methods to access the form editing of the IDE.
}
unit FormEditingIntf;

{$mode objfpc}{$H+}

interface

uses
  Math, Classes, SysUtils, LCLProc, TypInfo, types, Forms, Controls,
  ProjectIntf, ComponentEditors;
  
const
  ComponentPaletteImageWidth = 24;
  ComponentPaletteImageHeight = 24;
  ComponentPaletteBtnWidth  = ComponentPaletteImageWidth + 3;
  ComponentPaletteBtnHeight = ComponentPaletteImageHeight + 3;

type
  TDMCompAtPosFlag = (
    dmcapfOnlyVisible,
    dmcapfOnlySelectable
    );
  TDMCompAtPosFlags = set of TDMCompAtPosFlag;

  { TDesignerMediator
    To edit designer forms which do not use the LCL, register a TDesignerMediator,
    which will emulate the painting, handle the mouse and editing bounds. }

  TDesignerMediator = class(TComponent)
  private
    FDesigner: TComponentEditorDesigner;
    FLCLForm: TForm;
    FRoot: TComponent;
  protected
    FCollectedChilds: TFPList;
    procedure SetDesigner(const AValue: TComponentEditorDesigner); virtual;
    procedure SetLCLForm(const AValue: TForm); virtual;
    procedure SetRoot(const AValue: TComponent); virtual;
    procedure CollectChildren(Child: TComponent); virtual;
    procedure Notification(AComponent: TComponent; Operation: TOperation);
          override;
  public
    class function FormClass: TComponentClass; virtual; abstract;
    class function CreateMediator(TheOwner, aForm: TComponent): TDesignerMediator; virtual;
    class procedure InitFormInstance(aForm: TComponent); virtual; // called after NewInstance, before constructor
  public
    procedure SetBounds(AComponent: TComponent; NewBounds: TRect); virtual;
    procedure GetBounds(AComponent: TComponent; out CurBounds: TRect); virtual;
    procedure SetFormBounds(RootComponent: TComponent; NewBounds, ClientRect: TRect); virtual;
    procedure GetFormBounds(RootComponent: TComponent; out CurBounds, CurClientRect: TRect); virtual;
    procedure GetClientArea(AComponent: TComponent; out CurClientArea: TRect;
                            out ScrollOffset: TPoint); virtual;
    function GetComponentOriginOnForm(AComponent: TComponent): TPoint; virtual;
    function ComponentIsIcon(AComponent: TComponent): boolean; virtual;
    function ParentAcceptsChild(Parent: TComponent; Child: TComponentClass): boolean; virtual;
    function ComponentIsVisible(AComponent: TComponent): Boolean; virtual;
    function ComponentIsSelectable(AComponent: TComponent): Boolean; virtual;
    function ComponentAtPos(p: TPoint; MinClass: TComponentClass;
                            Flags: TDMCompAtPosFlags): TComponent; virtual;
    procedure GetChilds(Parent: TComponent; ChildComponents: TFPList); virtual;

    // events
    procedure InitComponent(AComponent, NewParent: TComponent; NewBounds: TRect); virtual;
    procedure Paint; virtual;
    procedure KeyDown(Sender: TControl; var Key: word; Shift: TShiftState); virtual;
    procedure KeyUp(Sender: TControl; var Key: word; Shift: TShiftState); virtual;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; p: TPoint; var Handled: boolean); virtual;
    procedure MouseMove(Shift: TShiftState; p: TPoint; var Handled: boolean); virtual;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState; p: TPoint; var Handled: boolean); virtual;

    property LCLForm: TForm read FLCLForm write SetLCLForm;
    property Designer: TComponentEditorDesigner read FDesigner write SetDesigner;
    property Root: TComponent read FRoot write SetRoot;
  end;
  TDesignerMediatorClass = class of TDesignerMediator;


  { TAbstractFormEditor }
  
  TAbstractFormEditor = class
  protected
    function GetDesignerBaseClasses(Index: integer): TComponentClass; virtual; abstract;
    function GetDesigner(Index: integer): TIDesigner; virtual; abstract;
    function GetDesignerMediators(Index: integer): TDesignerMediatorClass; virtual; abstract;
  public
    // components
    function FindComponentByName(const Name: ShortString
                                 ): TComponent; virtual; abstract;

    function CreateUniqueComponentName(AComponent: TComponent): string; virtual; abstract;
    function CreateUniqueComponentName(const AClassName: string;
                                       OwnerComponent: TComponent): string; virtual; abstract;
    function GetDefaultComponentParent(TypeClass: TComponentClass
                                       ): TComponent; virtual; abstract;
    function GetDefaultComponentPosition(TypeClass: TComponentClass;
                                         ParentComp: TComponent;
                                         var X,Y: integer): boolean; virtual; abstract;
    function CreateComponent(ParentComp: TComponent;
                             TypeClass: TComponentClass;
                             const AUnitName: shortstring;
                             X,Y,W,H: Integer;
                             DisableAutoSize: boolean): TComponent; virtual; abstract;
    function CreateComponentFromStream(BinStream: TStream;
                      AncestorType: TComponentClass;
                      const NewUnitName: ShortString;
                      Interactive: boolean;
                      Visible: boolean = true;
                      DisableAutoSize: boolean = false;
                      ContextObj: TObject = nil): TComponent; virtual; abstract;
    function CreateChildComponentFromStream(BinStream: TStream;
                                     ComponentClass: TComponentClass;
                                     Root: TComponent;
                                     ParentControl: TWinControl
                                     ): TComponent; virtual; abstract;

    // ancestors
    function GetAncestorLookupRoot(AComponent: TComponent): TComponent; virtual; abstract;
    function GetAncestorInstance(AComponent: TComponent): TComponent; virtual; abstract;
    function RegisterDesignerBaseClass(AClass: TComponentClass): integer; virtual; abstract;
    function DesignerBaseClassCount: Integer; virtual; abstract;
    property DesignerBaseClasses[Index: integer]: TComponentClass read GetDesignerBaseClasses;
    procedure UnregisterDesignerBaseClass(AClass: TComponentClass); virtual; abstract;
    function IndexOfDesignerBaseClass(AClass: TComponentClass): integer; virtual; abstract;
    function DescendFromDesignerBaseClass(AClass: TComponentClass): integer; virtual; abstract;
    function FindDesignerBaseClassByName(const AClassName: shortstring; WithDefaults: boolean): TComponentClass; virtual; abstract;

    // designers
    function DesignerCount: integer; virtual; abstract;
    property Designer[Index: integer]: TIDesigner read GetDesigner;
    function GetCurrentDesigner: TIDesigner; virtual; abstract;
    function GetDesignerForm(APersistent: TPersistent): TCustomForm; virtual; abstract;
    function GetDesignerByComponent(AComponent: TComponent
                                    ): TIDesigner; virtual; abstract;

    // mediators for non LCL forms
    procedure RegisterDesignerMediator(MediatorClass: TDesignerMediatorClass); virtual; abstract; // auto calls RegisterDesignerBaseClass
    procedure UnregisterDesignerMediator(MediatorClass: TDesignerMediatorClass); virtual; abstract; // auto calls UnregisterDesignerBaseClass
    function DesignerMediatorCount: integer; virtual; abstract;
    property DesignerMediators[Index: integer]: TDesignerMediatorClass read GetDesignerMediators;
    function GetDesignerMediatorByComponent(AComponent: TComponent): TDesignerMediator; virtual; abstract;

    // selection
    function SaveSelectionToStream(s: TStream): Boolean; virtual; abstract;
    function InsertFromStream(s: TStream; Parent: TWinControl;
                              Flags: TComponentPasteSelectionFlags
                              ): Boolean; virtual; abstract;
    function ClearSelection: Boolean; virtual; abstract;
    function DeleteSelection: Boolean; virtual; abstract;
    function CopySelectionToClipboard: Boolean; virtual; abstract;
    function CutSelectionToClipboard: Boolean; virtual; abstract;
    function PasteSelectionFromClipboard(Flags: TComponentPasteSelectionFlags
                                         ): Boolean; virtual; abstract;
  end;

type
  TDesignerIDECommandForm = class(TCustomForm)
    // dummy form class, use by the IDE commands for keys in the designers
  end;

var
  FormEditingHook: TAbstractFormEditor; // will be set by the IDE

procedure GetComponentLeftTopOrDesignInfo(AComponent: TComponent; out aLeft, aTop: integer); // get properties if exists, otherwise get DesignInfo
procedure SetComponentLeftTopOrDesignInfo(AComponent: TComponent; aLeft, aTop: integer); // set properties if exists, otherwise set DesignInfo
function TrySetOrdProp(Instance: TPersistent; const PropName: string;
                       Value: integer): boolean;
function TryGetOrdProp(Instance: TPersistent; const PropName: string;
                       out Value: integer): boolean;
function LeftFromDesignInfo(ADesignInfo: LongInt): SmallInt;
function TopFromDesignInfo(ADesignInfo: LongInt): SmallInt;
function LeftTopToDesignInfo(const ALeft, ATop: SmallInt): LongInt;
procedure DesignInfoToLeftTop(ADesignInfo: LongInt; out ALeft, ATop: SmallInt);


implementation


procedure GetComponentLeftTopOrDesignInfo(AComponent: TComponent; out aLeft,
  aTop: integer);
var
  Info: LongInt;
begin
  Info:=AComponent.DesignInfo;
  if not TryGetOrdProp(AComponent,'Left',aLeft) then
    aLeft:=LeftFromDesignInfo(Info);
  if not TryGetOrdProp(AComponent,'Top',aTop) then
    aTop:=TopFromDesignInfo(Info);
end;

procedure SetComponentLeftTopOrDesignInfo(AComponent: TComponent;
  aLeft, aTop: integer);
var
  HasLeft: Boolean;
  HasTop: Boolean;
begin
  HasLeft:=TrySetOrdProp(AComponent,'Left',aLeft);
  HasTop:=TrySetOrdProp(AComponent,'Top',aTop);
  if HasLeft and HasTop then exit;
  ALeft := Max(Low(SmallInt), Min(ALeft, High(SmallInt)));
  ATop := Max(Low(SmallInt), Min(ATop, High(SmallInt)));
  AComponent.DesignInfo:=LeftTopToDesignInfo(aLeft,aTop);
end;

function TrySetOrdProp(Instance: TPersistent; const PropName: string;
  Value: integer): boolean;
var
  PropInfo: PPropInfo;
begin
  PropInfo:=GetPropInfo(Instance.ClassType,PropName);
  if PropInfo=nil then exit(false);
  SetOrdProp(Instance,PropInfo,Value);
  Result:=true;
end;

function TryGetOrdProp(Instance: TPersistent; const PropName: string; out
  Value: integer): boolean;
var
  PropInfo: PPropInfo;
begin
  PropInfo:=GetPropInfo(Instance.ClassType,PropName);
  if PropInfo=nil then exit(false);
  Value:=GetOrdProp(Instance,PropInfo);
  Result:=true;
end;

function LeftFromDesignInfo(ADesignInfo: LongInt): SmallInt;
var
  DesignInfoRec: packed record
    Left: SmallInt;
    Top: SmallInt;
  end absolute ADesignInfo;
begin
  Result := DesignInfoRec.Left;
end;

function TopFromDesignInfo(ADesignInfo: LongInt): SmallInt;
var
  DesignInfoRec: packed record
    Left: SmallInt;
    Top: SmallInt;
  end absolute ADesignInfo;
begin
  Result := DesignInfoRec.Top;
end;

function LeftTopToDesignInfo(const ALeft, ATop: SmallInt): LongInt;
var
  ResultRec: packed record
    Left: SmallInt;
    Top: SmallInt;
  end absolute Result;
begin
  ResultRec.Left := ALeft;
  ResultRec.Top := ATop;
end;

procedure DesignInfoToLeftTop(ADesignInfo: LongInt; out ALeft, ATop: SmallInt);
var
  DesignInfoRec: packed record
    Left: SmallInt;
    Top: SmallInt;
  end absolute ADesignInfo;
begin
  ALeft := DesignInfoRec.Left;
  ATop := DesignInfoRec.Top;
end;

{ TDesignerMediator }

procedure TDesignerMediator.SetRoot(const AValue: TComponent);
begin
  if FRoot=AValue then exit;
  if FRoot<>nil then
    FRoot.RemoveFreeNotification(Self);
  FRoot:=AValue;
  if FRoot<>nil then
    FRoot.FreeNotification(Self);
end;

procedure TDesignerMediator.CollectChildren(Child: TComponent);
begin
  FCollectedChilds.Add(Child);
end;

procedure TDesignerMediator.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if Operation=opRemove then begin
    if AComponent=FLCLForm then FLCLForm:=nil;
    if AComponent=FRoot then FRoot:=nil;
  end;
end;

class function TDesignerMediator.CreateMediator(TheOwner, aForm: TComponent
  ): TDesignerMediator;
begin
  Result:=Create(TheOwner);
  Result.FRoot:=aForm;
end;

procedure TDesignerMediator.SetDesigner(const AValue: TComponentEditorDesigner
  );
begin
  if FDesigner=AValue then exit;
  if FDesigner<>nil then begin

  end;
  FDesigner:=AValue;
end;

procedure TDesignerMediator.SetLCLForm(const AValue: TForm);
begin
  if FLCLForm=AValue then exit;
  if FLCLForm<>nil then
    FLCLForm.RemoveFreeNotification(Self);
  FLCLForm:=AValue;
  if FLCLForm<>nil then
    FLCLForm.FreeNotification(Self);
end;

class procedure TDesignerMediator.InitFormInstance(aForm: TComponent);
begin

end;

procedure TDesignerMediator.SetBounds(AComponent: TComponent; NewBounds: TRect
  );
begin
  SetComponentLeftTopOrDesignInfo(AComponent,NewBounds.Left,NewBounds.Top);
end;

procedure TDesignerMediator.GetBounds(AComponent: TComponent; out
  CurBounds: TRect);
var
  aLeft: integer;
  aTop: integer;
begin
  GetComponentLeftTopOrDesignInfo(AComponent,aLeft,aTop);
  CurBounds:=Rect(aLeft,aTop,aLeft+ComponentPaletteBtnWidth,aTop+ComponentPaletteBtnHeight);
end;

procedure TDesignerMediator.SetFormBounds(RootComponent: TComponent; NewBounds,
  ClientRect: TRect);
// default: use NewBounds as position and the ClientRect as size
var
  r: TRect;
begin
  r:=Bounds(NewBounds.Left,NewBounds.Top,
            ClientRect.Right-ClientRect.Left,ClientRect.Bottom-ClientRect.Top);
  //debugln(['TDesignerMediator.SetFormBounds NewBounds=',dbgs(NewBounds),' ClientRect=',dbgs(ClientRect),' r=',dbgs(r)]);
  SetBounds(RootComponent,r);
end;

procedure TDesignerMediator.GetFormBounds(RootComponent: TComponent; out
  CurBounds, CurClientRect: TRect);
// default: clientarea is whole bounds and CurBounds.Width/Height=0
// The IDE will use the clientarea to determine the size of the form
begin
  GetBounds(RootComponent,CurBounds);
  //debugln(['TDesignerMediator.GetFormBounds ',dbgs(CurBounds)]);
  CurClientRect:=Rect(0,0,CurBounds.Right-CurBounds.Left,
                      CurBounds.Bottom-CurBounds.Top);
  CurBounds.Right:=CurBounds.Left;
  CurBounds.Bottom:=CurBounds.Top;
  //debugln(['TDesignerMediator.GetFormBounds ',dbgs(CurBounds),' ',dbgs(CurClientRect)]);
end;

procedure TDesignerMediator.GetClientArea(AComponent: TComponent; out
  CurClientArea: TRect; out ScrollOffset: TPoint);
// default: no ScrollOffset and client area is whole bounds
begin
  GetBounds(AComponent,CurClientArea);
  OffsetRect(CurClientArea,-CurClientArea.Left,-CurClientArea.Top);
  ScrollOffset:=Point(0,0);
end;

function TDesignerMediator.GetComponentOriginOnForm(AComponent: TComponent): TPoint;
var
  Parent: TComponent;
  ClientArea: TRect;
  ScrollOffset: TPoint;
  CurBounds: TRect;
begin
  Result:=Point(0,0);
  while AComponent<>nil do begin
    Parent:=AComponent.GetParentComponent;
    if Parent=nil then break;
    GetBounds(AComponent,CurBounds);
    inc(Result.X,CurBounds.Left);
    inc(Result.Y,CurBounds.Top);
    GetClientArea(Parent,ClientArea,ScrollOffset);
    inc(Result.X,ClientArea.Left+ScrollOffset.X);
    inc(Result.Y,ClientArea.Top+ScrollOffset.Y);
    AComponent:=Parent;
  end;
end;

procedure TDesignerMediator.Paint;
begin

end;

function TDesignerMediator.ComponentIsIcon(AComponent: TComponent): boolean;
begin
  Result:=true;
end;

function TDesignerMediator.ParentAcceptsChild(Parent: TComponent;
  Child: TComponentClass): boolean;
begin
  Result:=false;
end;

function TDesignerMediator.ComponentIsVisible(AComponent: TComponent): Boolean;
begin
  Result:=true;
end;

function TDesignerMediator.ComponentIsSelectable(AComponent: TComponent
  ): Boolean;
begin
  Result:=true;
end;

function TDesignerMediator.ComponentAtPos(p: TPoint; MinClass: TComponentClass;
  Flags: TDMCompAtPosFlags): TComponent;
var
  i: Integer;
  Child: TComponent;
  ClientArea: TRect;
  ScrollOffset: TPoint;
  ChildBounds: TRect;
  Found: Boolean;
  Children: TFPList;
  Offset: TPoint;
begin
  Result:=Root;
  while Result<>nil do begin
    GetClientArea(Result,ClientArea,ScrollOffset);
    Offset:=GetComponentOriginOnForm(Result);
    //DebugLn(['TDesignerMediator.ComponentAtPos Parent=',DbgSName(Result),' Offset=',dbgs(Offset)]);
    OffsetRect(ClientArea,Offset.X,Offset.Y);
    Children:=TFPList.Create;
    try
      GetChilds(Result,Children);
      //DebugLn(['TDesignerMediator.ComponentAtPos Result=',DbgSName(Result),' ChildCount=',children.Count,' ClientArea=',dbgs(ClientArea)]);
      Found:=false;
      // iterate backwards (z-order)
      for i:=Children.Count-1 downto 0 do begin
        Child:=TComponent(Children[i]);
        //DebugLn(['TDesignerMediator.ComponentAtPos Child ',DbgSName(Child)]);
        if (MinClass<>nil) and (not Child.InheritsFrom(MinClass)) then
          continue;
        if (dmcapfOnlyVisible in Flags) and (not ComponentIsVisible(Child)) then
          continue;
        if (dmcapfOnlySelectable in Flags)
        and (not ComponentIsSelectable(Child)) then
          continue;
        GetBounds(Child,ChildBounds);
        OffsetRect(ChildBounds,ClientArea.Left+ScrollOffset.X,
                               ClientArea.Top+ScrollOffset.Y);
        //DebugLn(['TDesignerMediator.ComponentAtPos ChildBounds=',dbgs(ChildBounds),' p=',dbgs(p)]);
        if PtInRect(ChildBounds,p) then begin
          Found:=true;
          Result:=Child;
          break;
        end;
      end;
      if not Found then exit;
    finally
      Children.Free;
    end;
  end;
end;

procedure TDesignerMediator.GetChilds(Parent: TComponent;
  ChildComponents: TFPList);
begin
  FCollectedChilds:=ChildComponents;
  try
    TDesignerMediator(Parent).GetChildren(@CollectChildren,Root);
  finally
    FCollectedChilds:=nil;
  end;
end;

procedure TDesignerMediator.InitComponent(AComponent, NewParent: TComponent;
  NewBounds: TRect);
begin
  SetBounds(AComponent,NewBounds);
  TDesignerMediator(AComponent).SetParentComponent(NewParent);
end;

procedure TDesignerMediator.KeyDown(Sender: TControl; var Key: word;
  Shift: TShiftState);
begin

end;

procedure TDesignerMediator.KeyUp(Sender: TControl; var Key: word;
  Shift: TShiftState);
begin

end;

procedure TDesignerMediator.MouseDown(Button: TMouseButton; Shift: TShiftState;
  p: TPoint; var Handled: boolean);
begin

end;

procedure TDesignerMediator.MouseMove(Shift: TShiftState; p: TPoint;
  var Handled: boolean);
begin

end;

procedure TDesignerMediator.MouseUp(Button: TMouseButton; Shift: TShiftState;
  p: TPoint; var Handled: boolean);
begin

end;

end.