File: dockedresizer.pas

package info (click to toggle)
lazarus 4.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 275,760 kB
  • sloc: pascal: 2,341,904; xml: 509,420; makefile: 348,726; cpp: 93,608; sh: 3,387; java: 609; perl: 297; sql: 222; ansic: 137
file content (316 lines) | stat: -rw-r--r-- 10,512 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
{
 *****************************************************************************
  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************

 Authors: Maciej Izak
          Michael W. Vogel

 The Resizer is a visual control that own two ScrollBars and the ResizeControl
 that shows the design form.
}

unit DockedResizer;

{$mode objfpc}{$H+}
{ $define DEBUGDOCKEDFORMEDITOR}

interface

uses
  // RTL, FCL
  Classes, SysUtils, Math,
  // LCL
  LCLType, Controls, ExtCtrls, Forms, StdCtrls, Buttons, Dialogs, LCLIntf,
  LCLProc,
  // DockedFormEditor
  DockedResizeControl, DockedDesignForm, DockedStrConsts;

type

  { TResizer }

  TResizer = class(TWinControl)
  private
    FDesignScroll: array[0..1] of Boolean;
    FDesignForm: TDesignForm;
    FPostponedAdjustResizeControl: Boolean;
    // 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;
    FResizeControl: TResizeControl;
    FScrollBarHorz: TScrollBar;
    FScrollBarVert: TScrollBar;
    FScrollPos: TPoint;
    procedure FormResized(Sender: TObject);
    function GetFormContainer: TWinControl;
    procedure ScrollBarHorzMouseWheel(Sender: TObject; {%H-}Shift: TShiftState;
      WheelDelta: Integer; {%H-}MousePos: TPoint; var {%H-}Handled: Boolean);
    procedure ScrollBarVertMouseWheel(Sender: TObject; {%H-}Shift: TShiftState;
      WheelDelta: Integer; {%H-}MousePos: TPoint; var {%H-}Handled: Boolean);
    procedure SetDesignForm(AValue: TDesignForm);
    procedure SetDesignScroll(AIndex: Integer; AValue: Boolean);
    procedure ScrollBarScroll(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
  public
    constructor Create(TheOwner: TWinControl); reintroduce;
    destructor Destroy; override;
    procedure AdjustResizer(Sender: TObject);
    procedure DesignerSetFocus;
  public
    property DesignForm: TDesignForm read FDesignForm write SetDesignForm;
    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 FormContainer: TWinControl read GetFormContainer;
    property ResizeControl: TResizeControl read FResizeControl;
  end;

implementation

{ TResizer }

procedure TResizer.FormResized(Sender: TObject);
begin
  DesignForm.Form.Width  := ResizeControl.NewFormSize.X;
  DesignForm.Form.Height := ResizeControl.NewFormSize.Y;
  SetTimer(DesignForm.Form.Handle, WM_BOUNDTODESIGNTABSHEET, 10, nil);
end;

function TResizer.GetFormContainer: TWinControl;
begin
  Result := ResizeControl.FormContainer;
end;

procedure TResizer.ScrollBarHorzMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  LScrollPos: Integer;
begin
  LScrollPos := FScrollPos.x - WheelDelta;
  FScrollBarHorz.Position := LScrollPos;
  ScrollBarScroll(FScrollBarHorz, scEndScroll, LScrollPos);
  Handled := True;
end;

procedure TResizer.ScrollBarVertMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  LScrollPos: Integer;
begin
  LScrollPos := FScrollPos.y - WheelDelta;
  FScrollBarVert.Position := LScrollPos;
  ScrollBarScroll(FScrollBarVert, scEndScroll, LScrollPos);
  Handled := True;
end;

procedure TResizer.SetDesignForm(AValue: TDesignForm);
begin
  {$IFDEF DEBUGDOCKEDFORMEDITOR}
  if Assigned(AValue) then DebugLn('TResizer.SetDesignForm: New Designform: ', DbgSName(AValue.Form))
                      else DebugLn('TResizer.SetDesignForm: New Designform: nil');
  {$ENDIF}
  if FDesignForm <> nil then
    FDesignForm.OnChangeHackedBounds := nil;
  FDesignForm := AValue;
  if Assigned(FDesignForm) then
  begin
    FDesignForm.BeginUpdate;
    FDesignForm.Form.Parent := ResizeControl.FormContainer;
    FDesignForm.EndUpdate;
    FDesignForm.OnChangeHackedBounds := @AdjustResizer;
    if Assigned(FDesignForm.AnchorDesigner) then
      FDesignForm.AnchorDesigner.Parent := ResizeControl.AnchorContainer;
  end;
  ResizeControl.DesignForm := AValue;
end;

procedure TResizer.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(FScrollBarHorz);
    SB_Vert: PerformScroll(FScrollBarVert);
  else
    raise EArgumentOutOfRangeException.CreateRes(@SArgumentOutOfRange);
  end;
end;

procedure TResizer.ScrollBarScroll(Sender: TObject; ScrollCode: TScrollCode; var ScrollPos: Integer);
begin
  case ScrollCode of
    scLineDown: ScrollPos := ScrollPos + 50;
    scLineUp:   ScrollPos := ScrollPos - 50;
    scPageDown:
      begin
        if Sender = FScrollBarHorz then ScrollPos := ScrollPos + ResizeControl.Width;
        if Sender = FScrollBarVert then ScrollPos := ScrollPos + ResizeControl.Height;
      end;
    scPageUp:
      begin
        if Sender = FScrollBarHorz then ScrollPos := ScrollPos - ResizeControl.Width;
        if Sender = FScrollBarVert then ScrollPos := ScrollPos - ResizeControl.Height;
      end;
  end;

  DesignForm.BeginUpdate;
  if Sender = FScrollBarVert then
  begin
    // Warning - don't overflow the range! (go to description for FRealMaxV)
    ScrollPos := Min(ScrollPos, FRealMaxV);
    ScrollPos := Max(ScrollPos, 0);
    FScrollPos.y := ScrollPos;
  end;
  if Sender = FScrollBarHorz then
  begin
    ScrollPos := Min(ScrollPos, FRealMaxH);
    ScrollPos := Max(ScrollPos, 0);
    FScrollPos.x := ScrollPos;
  end;
  DesignForm.EndUpdate;
  if not FPostponedAdjustResizeControl then
  begin
    ResizeControl.AdjustBounds(FScrollPos);
    ResizeControl.DesignerSetFocus;
  end;
  DesignForm.Form.Invalidate;
end;

constructor TResizer.Create(TheOwner: TWinControl);
begin
  inherited Create(TheOwner);

  Align := alClient;
  FPostponedAdjustResizeControl := False;
  FScrollPos := Point(0, 0);

  FScrollBarVert := TScrollBar.Create(nil);
  FScrollBarVert.Kind := sbVertical;
  FScrollBarVert.Parent := Self;
  FScrollBarVert.AnchorSideTop.Control := Self;
  FScrollBarVert.AnchorSideRight.Control := Self;
  FScrollBarVert.AnchorSideRight.Side := asrRight;
  FScrollBarVert.AnchorSideBottom.Side := asrBottom;
  FScrollBarVert.Anchors := [akTop, akRight, akBottom];
  FScrollBarVert.Visible := False;
  FScrollBarVert.OnScroll := @ScrollBarScroll;
  FScrollBarVert.AddHandlerOnMouseWheel(@ScrollBarVertMouseWheel);

  FScrollBarHorz := TScrollBar.Create(nil);
  FScrollBarHorz.Parent := Self;
  FScrollBarHorz.AnchorSideLeft.Control := Self;
  FScrollBarHorz.AnchorSideRight.Side := asrRight;
  FScrollBarHorz.AnchorSideBottom.Control := Self;
  FScrollBarHorz.AnchorSideBottom.Side := asrBottom;
  FScrollBarHorz.Anchors := [akLeft, akRight, akBottom];
  FScrollBarHorz.Visible := False;
  FScrollBarHorz.OnScroll := @ScrollBarScroll;
  FScrollBarHorz.AddHandlerOnMouseWheel(@ScrollBarHorzMouseWheel);

  FResizeControl := TResizeControl.Create(nil);
  FResizeControl.Name := '';
  FResizeControl.Parent := Self;
  FResizeControl.AnchorSideLeft.Control := Self;
  FResizeControl.AnchorSideTop.Control := Self;
  FResizeControl.AnchorSideRight.Control := FScrollBarVert;
  FResizeControl.AnchorSideBottom.Control := FScrollBarHorz;
  FResizeControl.Anchors := [akTop, akLeft, akRight, akBottom];

  FScrollBarVert.AnchorSideBottom.Control := ResizeControl;
  FScrollBarHorz.AnchorSideRight.Control := ResizeControl;

  FResizeControl.OnResized := @FormResized;
  FResizeControl.OnChangeBounds := @AdjustResizer;
end;

destructor TResizer.Destroy;
begin
  Pointer(FDesignForm) := nil;
  FreeAndNil(FResizeControl);
  FreeAndNil(FScrollBarVert);
  FreeAndNil(FScrollBarHorz);
  inherited Destroy;
end;

procedure TResizer.AdjustResizer(Sender: TObject);
var
  LWidth, LHeight: Integer;
  LScrollPos: Integer;
begin
  if not Assigned(FDesignForm) then Exit;
  if ResizeControl.Resizing then
  begin
    DesignForm.BeginUpdate;
    DesignForm.EndUpdate;
    ResizeControl.AdjustBounds(FScrollPos);
    Exit;
  end;

  LWidth  := FDesignForm.Width  + 2 * ResizeControl.SizerGripSize;
  LHeight := FDesignForm.Height + 2 * ResizeControl.SizerGripSize + ResizeControl.FakeMenu.Height;
  {$IFDEF DEBUGDOCKEDFORMEDITOR} DebugLn('TResizer.AdjustResizer Resizer Width:', DbgS(LWidth), ' Height:', DbgS(LHeight)); {$ENDIF}

  FPostponedAdjustResizeControl := True;
  if ResizeControl.Width < LWidth then
  begin
    // if designer frame is smaller as scrollbar, show scrollbar
    DesignScrollBottom := True;
    FScrollBarHorz.Max := LWidth;
    FRealMaxH := LWidth - ResizeControl.Width;
    FScrollBarHorz.PageSize := ResizeControl.Width;
    if FScrollPos.x > FRealMaxH then
    begin
      FScrollPos.x := FRealMaxH;
      LScrollPos := FScrollPos.x;
      ScrollBarScroll(FScrollBarHorz, scEndScroll, LScrollPos);
    end;
  end else begin
    // invisible ScrollBar
    DesignScrollBottom := False;
    LScrollPos := 0;
    ScrollBarScroll(FScrollBarHorz, scEndScroll, LScrollPos);
  end;

  if ResizeControl.Height < LHeight then
  begin
    // if designer frame is higher as scrollbar, show scrollbar
    DesignScrollRight := True;
    FScrollBarVert.Max := LHeight;
    FRealMaxV := LHeight - ResizeControl.Height;
    FScrollBarVert.PageSize := ResizeControl.Height;
    if FScrollPos.y > FRealMaxV then
    begin
      FScrollPos.y := FRealMaxV;
      LScrollPos := FScrollPos.y;
      ScrollBarScroll(FScrollBarVert, scEndScroll, LScrollPos);
    end;
  end else begin
    DesignScrollRight := False;
    LScrollPos := 0;
    ScrollBarScroll(FScrollBarVert, scEndScroll, LScrollPos);
  end;
  FPostponedAdjustResizeControl := False;

  ResizeControl.AdjustBounds(FScrollPos);
  ResizeControl.ClientChangeBounds(nil);
end;

procedure TResizer.DesignerSetFocus;
begin
  ResizeControl.DesignerSetFocus;
  if Assigned(FDesignForm) and Assigned(FDesignForm.AnchorDesigner) then
    FDesignForm.AnchorDesigner.OnMouseWheel := @ScrollBarVertMouseWheel;
end;

end.