File: sparta_abstractresizer.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (412 lines) | stat: -rw-r--r-- 11,844 bytes parent folder | download | duplicates (2)
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
unit sparta_AbstractResizer;

{$mode delphi}{$H+}

interface

uses
  Classes, SysUtils, Controls, ExtCtrls, Forms, Math, StdCtrls, Buttons, Dialogs,
  LCLType,
  sparta_InterfacesMDI, sparta_BasicResizeFrame, sparta_MDI_StrConsts;

type

  { TAbstractResizer }

  TAbstractResizer = class(TComponent, IResizer)
  private
    procedure FunnyButtonClick(Sender: TObject);
  protected { IResizer }
    function GetActiveResizeFrame: IResizeFrame; virtual; abstract;
    function GetActiveDesignedForm: IDesignedForm; virtual; abstract;
  public { IResizer }
    procedure TryBoundSizerToDesignedForm(Sender: TObject); virtual;
  protected
    // To perform proper behaviour for scroolbar with "PageSize" we need to remember real
    // maximal values (is possible to scroll outside of range 0..(Max - PageSize),
    // after mouse click in button responsible for changing value of scrollbar,
    // our value is equal to Max :\). Workaround: we need to remember real max value in our own place
    FRealMaxH: Integer;
    FRealMaxV: Integer;

    FSpecialMargin: array[0..3] of Integer;
    FDesignScroll: array[0..1] of Boolean;

    FParent: TWinControl;
    FResizerFrameClass: TResizerFrameClass;

    function CreateResizeFrame: TBasicResizeFrame; virtual;
    procedure NodePositioning(Sender: TObject; {%H-}PositioningKind: TPositioningKind; PositioningCode: TPositioningCode);

    function GetActiveFormAndFrame(out AForm: IDesignedForm; out AFrame: IResizeFrame): Boolean;

    procedure SetDesignScroll(AIndex: Integer; AValue: Boolean);

    procedure sbScroll(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
  public
    pMainDTU: TPanel;
    pMain: TPanel;
    pAddons: TPanel;
    pComponents: TPanel;
    lInfo: TLabel;
    sbShowComponents  : TSpeedButton;
    sbShowFormEditor: TSpeedButton;
    sbShowAnchorEditor: TSpeedButton;
    sbShowNonVisualEditor: TSpeedButton;
    pDesignTimeUtils: TPanel;
    sbV: TScrollBar;
    sbH: TScrollBar;
    bR: TButton;

    constructor Create(AParent: TWinControl; AResizerFrameClass: TResizerFrameClass); virtual; reintroduce;
    property DesignScrollRight: Boolean index SB_Vert read FDesignScroll[SB_Vert] write SetDesignScroll;
    property DesignScrollBottom: Boolean index SB_Horz read FDesignScroll[SB_Horz] write SetDesignScroll;

    property ActiveResizeFrame: IResizeFrame read GetActiveResizeFrame;
    property ActiveDesignedForm: IDesignedForm read GetActiveDesignedForm;
  end;

implementation

{ TAbstractResizer }

procedure TAbstractResizer.FunnyButtonClick(Sender: TObject);
begin
  ShowMessage('Funny button with no functionality!'
              + sLineBreak
              + sLineBreak +
              'Regards'
              + sLineBreak +
              'Maciej Izak'
              + sLineBreak
              + sLineBreak + 'DaThoX team FreeSparta.com project');
end;

procedure TAbstractResizer.TryBoundSizerToDesignedForm(Sender: TObject);
var
  LWidth, LHeight: Integer;
  LScrollPos: Integer;
  LResizeFrame: IResizeFrame;
  LFrame: TCustomFrame;
  LForm: IDesignedForm;
begin
  if not GetActiveFormAndFrame(LForm, LResizeFrame) then
    Exit;

  LFrame := LResizeFrame.Frame;
  LFrame.Constraints.MaxWidth := pMain.Width;
  LFrame.Constraints.MaxHeight := pMain.Height;

  LWidth  := LForm.Width + LResizeFrame.BgLeftMargin + LResizeFrame.BgRightMargin + 2*LResizeFrame.SizerRectSize;
  LHeight := LForm.Height + LResizeFrame.BgTopMargin + LResizeFrame.BgBottomMargin + 2*LResizeFrame.SizerRectSize;
  if not LResizeFrame.NodePositioning then
  begin
    LFrame.Width := LWidth;
    LFrame.Height := LHeight;
    // after enlargement and after reducing constrait not work for frame (LCL bug)
    if LFrame.Width > LFrame.Constraints.MaxWidth then
      LFrame.Width := LFrame.Constraints.MaxWidth;
    if LFrame.Height > LFrame.Constraints.MaxHeight then
      LFrame.Height := LFrame.Constraints.MaxHeight;
  end;

  LResizeFrame.PositionNodes;

  DesignScrollBottom := LFrame.Width < LWidth;
  sbH.Max := LWidth;
  FRealMaxH := LWidth - LFrame.Width;
  sbH.PageSize := LFrame.Width;
  if LResizeFrame.HorizontalScrollPos > FRealMaxH then
  begin
    LResizeFrame.HorizontalScrollPos := FRealMaxH;
    LScrollPos := LResizeFrame.HorizontalScrollPos;
    sbScroll(sbH, scEndScroll, LScrollPos);
  end;

  DesignScrollRight := LFrame.Height < LHeight;
  sbV.Max := LHeight;
  FRealMaxV := LHeight - LFrame.Height;
  sbV.PageSize := LFrame.Height;
  if LResizeFrame.VerticalScrollPos > FRealMaxV then
  begin
    LResizeFrame.VerticalScrollPos := FRealMaxV;
    LScrollPos := LResizeFrame.VerticalScrollPos;
    sbScroll(sbV, scEndScroll, LScrollPos);
  end;

  {!}
  LResizeFrame.ClientChangeBounds;

  // each editor can have scrolls in different positions.
  // this is our place where we can call event to set scroll positions.
  LScrollPos := LResizeFrame.VerticalScrollPos;
  sbScroll(sbV, scEndScroll, LScrollPos);
  LScrollPos := LResizeFrame.HorizontalScrollPos;
  sbScroll(sbH, scEndScroll, LScrollPos);

  if Supports(LForm, IDesignedFormBackground) then
    (LForm as IDesignedFormBackground).RefreshValues;

  LResizeFrame.DesignerSetFocus;
end;

procedure TAbstractResizer.sbScroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
var
  LScrollPos: Integer;
  LFrame: IResizeFrame;
  LForm: IDesignedForm;
begin
  if not GetActiveFormAndFrame(LForm, LFrame) then
    Exit;

  if ScrollCode <> scEndScroll then
    LFrame.HideSizeRects
  else
    LFrame.ShowSizeRects;


  LForm.BeginUpdate;
  if Sender = sbV then
  begin
    // Warning - don't overflow the range! (go to description for FRealMaxV)
    ScrollPos := Min(ScrollPos, FRealMaxV);
    LFrame.VerticalScrollPos := ScrollPos;
    // scroll for form
    with LFrame do // -8 when we scaling the form and we don't need to scroll -> there is Max
      LScrollPos := Max(ifthen(BgPanel.Top + BgTopMargin <= 0, ScrollPos - SizerRectSize - BgTopMargin, 0), 0);
    LForm.VertScrollPosition := LScrollPos;
  end;
  if Sender = sbH then
  begin
    ScrollPos := Min(ScrollPos, FRealMaxH);
    LFrame.HorizontalScrollPos := ScrollPos;
    // scroll for form
    with LFrame do
      LScrollPos := Max(ifthen(BgPanel.Left + BgLeftMargin <= 0, ScrollPos - SizerRectSize - BgLeftMargin, 0), 0);
    LForm.HorzScrollPosition := LScrollPos;
  end;
  LForm.EndUpdate;

  LFrame.PositionNodes;

  LForm.Form.Invalidate;
end;

function TAbstractResizer.CreateResizeFrame: TBasicResizeFrame;
begin
  Result := FResizerFrameClass.Create(FParent);
  Result.Name := '';
  Result.Parent := pMain;
  Result.Left := 0;
  Result.Top := 0;
  Result.OnNodePositioning := NodePositioning;
end;

procedure TAbstractResizer.NodePositioning(Sender: TObject;
  PositioningKind: TPositioningKind; PositioningCode: TPositioningCode);

var
  LForm: IDesignedForm;
  LFrame: IResizeFrame;

  (*procedure Positioning;
  var
    LHiddenHeight, LNewHeight: Integer;
    LHiddenWidth, LNewWidth: Integer;
  begin
    LForm.BeginUpdate;

    //if pkRight in PositioningKind then
    begin
      LHiddenWidth := sbH.Position;
      if LHiddenWidth > LFrame.DesignedWidthToScroll then
        LHiddenWidth := LFrame.DesignedWidthToScroll;

      // TODO - better handling of min width - same in TDesignedFormImpl.SetPublishedBounds (sparta_FakeCustom.pas)

      LNewWidth := LFrame.NewSize.X + LHiddenWidth;
      LForm.Width := LNewWidth;
      LForm.RealWidth := LNewWidth;

      // perform minimal width (TODO)
      {if LNewWidth < DesignedForm.Width then
      begin
        FResizerFrame.pClient.Width := DesignedForm.Width;
        Application.HandleMessage;
        Application.ProcessMessages;
      end;}
    end;

    //if pkBottom in PositioningKind then
    begin
      LHiddenHeight := sbV.Position;
      if LHiddenHeight > LFrame.DesignedHeightToScroll then
        LHiddenHeight := LFrame.DesignedHeightToScroll;

      LNewHeight := LFrame.NewSize.Y+ LHiddenHeight;
      LForm.Height := LNewHeight;
      LForm.RealHeight := LNewHeight;

      // perform minimal height (TODO)
      {if LNewHeight < DesignedForm.RealHeight then
      begin
        if FResizerFrame.pClient.Height < DesignedForm.RealHeight then
          FResizerFrame.pClient.Height := DesignedForm.RealHeight;
        Application.ProcessMessages;
      end;}
    end;

    LForm.EndUpdate;
  end;*)

  procedure PositioningEnd;
  var
    LHiddenHeight, LNewHeight: Integer;
    LHiddenWidth, LNewWidth: Integer;
  begin
    LHiddenWidth := sbH.Position;
    if LHiddenWidth > LFrame.DesignedWidthToScroll then
      LHiddenWidth := LFrame.DesignedWidthToScroll;

    LNewWidth := LFrame.NewSize.X + LHiddenWidth;

    LHiddenHeight := sbV.Position;
    if LHiddenHeight > LFrame.DesignedHeightToScroll then
      LHiddenHeight := LFrame.DesignedHeightToScroll;

    LNewHeight := LFrame.NewSize.Y + LHiddenHeight;

    LForm.Form.Width := LNewWidth;
    LForm.Form.Height := LNewHeight;
  end;

begin
  if not GetActiveFormAndFrame(LForm, LFrame) then
    Exit;

  case PositioningCode of
    pcPositioningEnd: PositioningEnd;
    //pcPositioning: Positioning;
  end;
end;

function TAbstractResizer.GetActiveFormAndFrame(out AForm: IDesignedForm; out
  AFrame: IResizeFrame): Boolean;
begin
  AForm := GetActiveDesignedForm;
  if AForm = nil then
    Exit(False);

  AFrame := GetActiveResizeFrame;
  Result := True;
end;

procedure TAbstractResizer.SetDesignScroll(AIndex: Integer; AValue: Boolean);

  procedure PerformScroll(AScroll: TScrollBar);
  begin
    AScroll.Visible := AValue;
    AScroll.Position:=0;
  end;

begin
  if FDesignScroll[AIndex] = AValue then
    Exit;

  FDesignScroll[AIndex] := AValue;

  case AIndex of
    SB_Horz: PerformScroll(sbH);
    SB_Vert: PerformScroll(sbV);
  else
    raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
  end;
end;

constructor TAbstractResizer.Create(AParent: TWinControl;
  AResizerFrameClass: TResizerFrameClass);
begin
  inherited Create(AParent);

  FResizerFrameClass := AResizerFrameClass;
  FParent := AParent;
  // create layout

  pMainDTU := TPanel.Create(Self);
  with pMainDTU do
  begin
    Parent := AParent;
    Align := alTop;
    BevelOuter := bvNone;
    Height := 0;
  end;

  pAddons := TPanel.Create(Self);
  pAddons.Parent := AParent;
  pAddons.Align := alRight;
  pAddons.BevelOuter := bvNone;
  pAddons.Width:=0;

  // Funny button
  bR := TButton.Create(Self);
  with bR do
  begin
    Parent := AParent;
    Height := 17;
    Width := 17;
    AnchorSideRight.Control := pAddons;
    AnchorSideBottom.Control := AParent;
    AnchorSideBottom.Side := asrBottom;
    Anchors := [akRight, akBottom];
    Caption := 'R';
    Visible := True;
    OnClick := FunnyButtonClick;
  end;

  sbV := TScrollBar.Create(Self);
  with sbV do
  begin
    Kind := sbVertical;
    Parent := AParent;
    AnchorSideTop.Control := pMainDTU;
    AnchorSideTop.Side := asrBottom;
    AnchorSideRight.Control := pAddons;
    AnchorSideBottom.Control := bR;
    Width := 17;
    Anchors := [akTop, akRight, akBottom];
    Visible := False;
    OnScroll := sbScroll;
  end;

  sbH := TScrollBar.Create(Self);
  with sbH do
  begin
    Parent := AParent;
    AnchorSideLeft.Control := AParent;
    AnchorSideRight.Control := bR;
    AnchorSideBottom.Control := AParent;
    AnchorSideBottom.Side := asrBottom;
    Anchors := [akLeft, akRight, akBottom];
    Visible := False;
    OnScroll := sbScroll;
  end;

  pMain := TPanel.Create(Self);
  with pMain do
  begin
    Parent := AParent;
    AnchorSideLeft.Control := AParent;
    AnchorSideTop.Control := pMainDTU;
    AnchorSideTop.Side := asrBottom;
    AnchorSideRight.Control := sbV;
    AnchorSideBottom.Control := sbH;
    Anchors := [akTop, akLeft, akRight, akBottom];
    BevelOuter := bvNone;
  end;

  pMain.OnChangeBounds:=TryBoundSizerToDesignedForm;
end;

end.