-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcasizemovepanel.pas
727 lines (651 loc) · 18.9 KB
/
casizemovepanel.pas
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
unit caSizeMovePanel;
{$INCLUDE ca.inc}
interface
uses
// Standard Delphi units
SysUtils,
Windows,
Messages,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
ExtCtrls,
Menus,
// ca units
caIni,
caConsts,
caControls;
type
TcaSizeMoveOption = (smSizing, smMoving);
TcaSizeMoveOptions = set of TcaSizeMoveOption;
//```````````````````````````````````````````````````````````````````````````
// TcaSizingBand
//```````````````````````````````````````````````````````````````````````````
TcaSizingBand = class(TCustomControl)
protected
procedure Paint; override;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaSizeMovePanelCaptionProperties
//```````````````````````````````````````````````````````````````````````````
TcaSizeMovePanelCaptionProperties = class(TPersistent)
private
// Property fields
FBrushColor: TColor;
FFont: TFont;
FText: TCaption;
FTextBorderWidth: Integer;
// Event property fields
FOnChanged: TNotifyEvent;
// Property methods
procedure SetBrushColor(const Value: TColor);
procedure SetFont(const Value: TFont);
procedure SetText(const Value: TCaption);
procedure SetTextBorderWidth(const Value: Integer);
// Private methods
procedure Changed;
// Event handlers
procedure FontChangedEvent(Sender: TObject);
protected
// Event triggers
procedure DoChanged; virtual;
public
// Create/Destroy
constructor Create;
destructor Destroy; override;
// Public methods
procedure Assign(Source: TPersistent); override;
// Event properties
property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
published
// Published properties
property BrushColor: TColor read FBrushColor write SetBrushColor;
property Font: TFont read FFont write SetFont;
property Text: TCaption read FText write SetText;
property TextBorderWidth: Integer read FTextBorderWidth write SetTextBorderWidth;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaSizeMovePanel
//```````````````````````````````````````````````````````````````````````````
TcaSizeMovePanel = class(TcaCustomFormPanel)
private
// Private fields
FBands: array[1..4] of TcaSizingBand;
FCurX: Integer;
FCurY: Integer;
FFixX: Integer;
FFixY: Integer;
FMoving: Boolean;
FMovX: Integer;
FMovY: Integer;
FSizing: Boolean;
// Property fields
FCaptionProperties: TcaSizeMovePanelCaptionProperties;
FIsLoaded: Boolean;
FIsLoading: Boolean;
FMainControl: TWinControl;
FMainGapBottom: Integer;
FMainGapLeft: Integer;
FMainGapRight: Integer;
FMainGapTop: Integer;
FShowGrabHandle: Boolean;
FOptions: TcaSizeMoveOptions;
FTitle: TCaption;
FSizingPixels: Integer;
FStoreState: Boolean;
// Event property fields
FOnMovePanel: TNotifyEvent;
FOnSizePanel: TNotifyEvent;
// Property methods
procedure SetCaptionProperties(const Value: TcaSizeMovePanelCaptionProperties);
procedure SetMainControl(const Value: TWinControl);
procedure SetMainGapBottom(const Value: Integer);
procedure SetMainGapLeft(const Value: Integer);
procedure SetMainGapRight(const Value: Integer);
procedure SetMainGapTop(const Value: Integer);
procedure SetShowGrabHandle(const Value: Boolean);
procedure SetSizingPixels(const Value: Integer);
procedure SetTitle(const Value: TCaption);
// Private methods
function BuildStateStoreName: string;
function GetNewHeight: Integer;
function GetNewWidth: Integer;
function LoadState: Boolean;
procedure SaveState;
procedure Rectangle(X1, Y1, X2, Y2: Integer);
// Event handlers
procedure CaptionPropertiesChangedEvent(Sender: TObject);
protected
// Protected methods
procedure AlignControls(AControl: TControl; var Rect: TRect); override;
procedure AlignMain; virtual;
procedure DoBeforeShowForm(AForm: TForm); override;
procedure DoMovePanel; virtual;
procedure DoSizePanel; virtual;
procedure Loaded; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure Paint; override;
public
// Create/Destroy
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// Public methods
procedure Resize; override;
procedure SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer); override;
published
// New properties
property CaptionProperties: TcaSizeMovePanelCaptionProperties read FCaptionProperties write SetCaptionProperties;
property MainControl: TWinControl read FMainControl write SetMainControl;
property MainGapBottom: Integer read FMainGapBottom write SetMainGapBottom;
property MainGapLeft: Integer read FMainGapLeft write SetMainGapLeft;
property MainGapRight: Integer read FMainGapRight write SetMainGapRight;
property MainGapTop: Integer read FMainGapTop write SetMainGapTop;
property Options: TcaSizeMoveOptions read FOptions write FOptions;
property ShowGrabHandle: Boolean read FShowGrabHandle write SetShowGrabHandle;
property SizingPixels: Integer read FSizingPixels write SetSizingPixels;
property StoreState: Boolean read FStoreState write FStoreState;
property Title: TCaption read FTitle write SetTitle;
// New event properties
property OnMovePanel: TNotifyEvent read FOnMovePanel write FOnMovePanel;
property OnSizePanel: TNotifyEvent read FOnSizePanel write FOnSizePanel;
// Promoted inherited properties
// TcaCustomPanel
property Frame;
property SubClassForm;
property Transparent;
property OnClickOutside;
// TcaCustomFormPanel event properties
property OnCreateForm;
// TCustomPanel
property Align;
property Alignment;
property Anchors;
property AutoSize;
property BiDiMode;
property BorderStyle;
property BorderWidth;
// property Caption;
property Color;
property Constraints;
property Ctl3D;
property DockSite;
property DoubleBuffered;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property FullRepaint;
property Locked;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property UseDockManager default True;
property Visible;
// Promoted inherited event properties
property OnCanResize;
property OnClick;
property OnConstrainedResize;
property OnContextPopup;
property OnDblClick;
property OnDockDrop;
property OnDockOver;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetSiteInfo;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
end;
implementation
//```````````````````````````````````````````````````````````````````````````
// TcaSizingBand
//```````````````````````````````````````````````````````````````````````````
procedure TcaSizingBand.Paint;
var
R: TRect;
begin
inherited Paint;
Canvas.Brush.Color := clTeal;
R := ClientRect;
Canvas.FillRect(R);
end;
//```````````````````````````````````````````````````````````````````````````
// TcaSizeMovePanelCaptionProperties
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaSizeMovePanelCaptionProperties.Create;
begin
inherited;
FFont := TFont.Create;
FFont.OnChange := FontChangedEvent;
end;
destructor TcaSizeMovePanelCaptionProperties.Destroy;
begin
FFont.Free;
inherited;
end;
// Public methods
procedure TcaSizeMovePanelCaptionProperties.Assign(Source: TPersistent);
var
SourceProperties: TcaSizeMovePanelCaptionProperties;
begin
if Source is TcaSizeMovePanelCaptionProperties then
begin
SourceProperties := TcaSizeMovePanelCaptionProperties(Source);
FBrushColor := SourceProperties.BrushColor;
FFont.Assign(SourceProperties.Font);
FText := SourceProperties.Text;
FTextBorderWidth := SourceProperties.TextBorderWidth;
end
else
inherited;
end;
// Event triggers
procedure TcaSizeMovePanelCaptionProperties.DoChanged;
begin
if Assigned(FOnChanged) then FOnChanged(Self);
end;
// Private methods
procedure TcaSizeMovePanelCaptionProperties.Changed;
begin
DoChanged;
end;
// Event handlers
procedure TcaSizeMovePanelCaptionProperties.FontChangedEvent(Sender: TObject);
begin
Changed;
end;
// Property methods
procedure TcaSizeMovePanelCaptionProperties.SetBrushColor(const Value: TColor);
begin
if Value <> FBrushColor then
begin
FBrushColor := Value;
Changed;
end;
end;
procedure TcaSizeMovePanelCaptionProperties.SetFont(const Value: TFont);
begin
FFont.Assign(Value);
Changed;
end;
procedure TcaSizeMovePanelCaptionProperties.SetText(const Value: TCaption);
begin
if Value <> FText then
begin
FText := Value;
Changed;
end;
end;
procedure TcaSizeMovePanelCaptionProperties.SetTextBorderWidth(const Value: Integer);
begin
if Value <> FTextBorderWidth then
begin
FTextBorderWidth := Value;
Changed;
end;
end;
//```````````````````````````````````````````````````````````````````````````
// TcaSizeMovePanel
//```````````````````````````````````````````````````````````````````````````
// Create/Destroy
constructor TcaSizeMovePanel.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCaptionProperties := TcaSizeMovePanelCaptionProperties.Create;
FCaptionProperties.OnChanged := CaptionPropertiesChangedEvent;
ControlStyle := ControlStyle - [csSetCaption];
FSizingPixels := 24;
FMainGapLeft := 5;
FMainGapRight := 5;
FMainGapTop := 24;
FMainGapBottom := 36;
FOptions := [smSizing, smMoving];
end;
destructor TcaSizeMovePanel.Destroy;
begin
inherited Destroy;
end;
// Public methods
procedure TcaSizeMovePanel.Resize;
begin
inherited Resize;
AlignMain;
end;
procedure TcaSizeMovePanel.SetBounds(ALeft: Integer; ATop: Integer; AWidth: Integer; AHeight: Integer);
begin
inherited;
if FStoreState then SaveState;
end;
// Protected methods
procedure TcaSizeMovePanel.AlignControls(AControl: TControl; var Rect: TRect);
begin
inherited AlignControls(AControl, Rect);
end;
procedure TcaSizeMovePanel.AlignMain;
begin
if FMainControl <> nil then
begin
FMainControl.Left := FMainGapLeft;
FMainControl.Width := ClientWidth - FMainGapLeft - FMainGapRight;
FMainControl.Top := FMainGapTop;
FMainControl.Height := ClientHeight - FMainGapTop - FMainGapBottom;
end;
end;
procedure TcaSizeMovePanel.DoMovePanel;
begin
if FStoreState and (not FIsLoading) and FIsLoaded then SaveState;
if Assigned(FOnMovePanel) then FOnMovePanel(Self);
end;
procedure TcaSizeMovePanel.DoBeforeShowForm(AForm: TForm);
begin
SetMainControl(AForm);
AForm.Align := alNone;
end;
procedure TcaSizeMovePanel.DoSizePanel;
begin
if Assigned(FOnSizePanel) then FOnSizePanel(Self);
end;
procedure TcaSizeMovePanel.Loaded;
begin
inherited;
if FStoreState then
begin
LoadState;
end;
Resize;
end;
procedure TcaSizeMovePanel.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
const
SC_DRAGMOVE = $F012;
var
Band: TcaSizingBand;
CaptionHeight: Integer;
OwnerForm: TWinControl;
Index: Integer;
begin
inherited MouseDown(Button, Shift, X, Y);
BringToFront;
if smSizing in FOptions then
begin
if (X > Width - FSizingPixels) and (Y > Height - FSizingPixels) then
begin
OwnerForm := Application.MainForm;
CaptionHeight := GetSystemMetrics(SM_CYCAPTION);
FFixX := ClientOrigin.x - OwnerForm.Left - 4;
FFixY := ClientOrigin.y - CaptionHeight - OwnerForm.Top - 4;
FMovX := FFixX + Width - 1;
FMovY := FFixY + Height - 1;
FCurX := X;
FCurY := Y;
FSizing := True;
for Index := 1 to 4 do
begin
FBands[Index] := TcaSizingBand.Create(Owner);
Band := FBands[Index];
Band.Visible := False;
Band.Parent := OwnerForm;
Band.Color := clBlack;
Band.Height := 1;
Band.Width := 1;
Band.Visible := True;
end;
Rectangle(FFixX, FFixY, FMovX, FMovY);
end;
end;
if (smMoving in FOptions) and (not FSizing) then
begin
FSizing := False;
FMoving := True;
ReleaseCapture;
Self.Perform(WM_SYSCOMMAND, SC_DRAGMOVE, 0);
end;
end;
procedure TcaSizeMovePanel.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
inherited MouseMove(Shift, X, Y);
if FSizing then
begin
if not (akRight in Anchors) then
begin
Inc(FMovX, X - FCurX);
FCurX := X;
end;
if not (akBottom in Anchors) then
begin
Inc(FMovY, Y - FCurY);
FCurY := Y;
Rectangle(FFixX, FFixY, FMovX, FMovY);
end;
DoSizePanel;
end;
if FMoving then DoMovePanel;
end;
procedure TcaSizeMovePanel.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Index: Integer;
begin
inherited MouseUp(Button, Shift, X, Y);
FMoving := False;
for Index := 1 to 4 do
if FBands[Index] <> nil then
begin
FBands[Index].Free;
FBands[Index] := nil;
end;
if FSizing and (smSizing in FOptions) then
begin
Width := GetNewWidth;
Height := GetNewHeight;
AlignMain;
FSizing := False;
end;
end;
procedure TcaSizeMovePanel.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited Notification(AComponent, Operation);
if (Operation = opRemove) and (MainControl = AComponent) then
MainControl := nil;
end;
procedure TcaSizeMovePanel.Paint;
procedure DoLine(C: TColor; Px: Integer);
begin
// Canvas.Pen.Width := 2;
Canvas.Pen.Color := C;
Canvas.MoveTo(Width - Px, Height - 4);
Canvas.LineTo(Width - 3, Height - Px - 1);
end;
procedure DrawCaption;
var
TextH: Integer;
CaptL: Integer;
CaptR: Integer;
CaptT: Integer;
CaptB: Integer;
Gap: Integer;
begin
Gap := FCaptionProperties.TextBorderWidth;
Canvas.Font.Assign(FCaptionProperties.Font);
TextH := Canvas.TextHeight(FCaptionProperties.Text);
CaptL := FMainGapLeft;
CaptR := Width - Gap * 2 - 2;
CaptT := 3;
CaptB := CaptT + TextH + Gap * 2;
Canvas.Brush.Color := FCaptionProperties.BrushColor;
Canvas.FillRect(Rect(CaptL, CaptT, CaptR, CaptB));
Canvas.Font.Assign(FCaptionProperties.Font);
Canvas.TextOut(CaptL + Gap, CaptT + Gap, FCaptionProperties.Text);
end;
begin
inherited Paint;
if FShowGrabHandle then
begin
DoLine(clDkGray, 13);
DoLine(clDkGray, 9);
DoLine(clDkGray, 5);
end;
DrawCaption;
end;
// Private methods
function TcaSizeMovePanel.BuildStateStoreName: string;
var
Control: TControl;
begin
Result := '';
Control := Self;
while Control <> nil do
begin
Result := Control.Name + '.' + Result;
Control := Control.Parent;
end;
if Length(Result) > 0 then
System.SetLength(Result, Length(Result) - 1);
end;
function TcaSizeMovePanel.GetNewHeight: Integer;
begin
Result := FMovY - FFixY + 1;
end;
function TcaSizeMovePanel.GetNewWidth: Integer;
begin
Result := FMovX - FFixX + 1;
end;
function TcaSizeMovePanel.LoadState: Boolean;
var
Ini: IcaIni;
ALeft: Integer;
ATop: Integer;
AWidth: Integer;
AHeight: Integer;
begin
FIsLoading := True;
Result := False;
Ini := TcaIni.Create(BuildStateStoreName);
ALeft := Ini.Integers[cLeft];
ATop := Ini.Integers[cTop];
AWidth := Ini.Integers[cWidth];
AHeight := Ini.Integers[cHeight];
if (ALeft <> 0) and (ATop <> 0) and (AWidth <> 0) and (AHeight <> 0) then
begin
SetBounds(ALeft, ATop, AWidth, AHeight);
Result := True;
end;
FIsLoading := False;
FIsLoaded := True;
end;
procedure TcaSizeMovePanel.SaveState;
var
Ini: IcaIni;
begin
Ini := TcaIni.Create(BuildStateStoreName);
Ini.Integers[cLeft] := Left;
Ini.Integers[cTop] := Top;
Ini.Integers[cWidth] := Width;
Ini.Integers[cHeight] := Height;
end;
procedure TcaSizeMovePanel.Rectangle(X1, Y1, X2, Y2: Integer);
begin
// Left
FBands[1].SetBounds(X1, Y1, 1, Y2 - Y1 + 1);
// Top
FBands[2].SetBounds(X1, Y1, X2 - X1 + 1, 1);
// Right
FBands[3].SetBounds(X2, Y1, 1, FBands[1].Height);
// Bottom
FBands[4].SetBounds(X1, Y2, FBands[2].Width, 1);
end;
// Event handlers
procedure TcaSizeMovePanel.CaptionPropertiesChangedEvent(Sender: TObject);
begin
Invalidate;
end;
// Property methods
procedure TcaSizeMovePanel.SetCaptionProperties(const Value: TcaSizeMovePanelCaptionProperties);
begin
FCaptionProperties.Assign(Value);
end;
procedure TcaSizeMovePanel.SetMainControl(const Value: TWinControl);
begin
if Value <> FMainControl then
begin
FMainControl := Value;
if FMainControl <> nil then
FMainControl.Parent := Self;
AlignMain;
end;
end;
procedure TcaSizeMovePanel.SetMainGapBottom(const Value: Integer);
begin
if Value <> FMainGapBottom then
begin
FMainGapBottom := Value;
AlignMain;
end;
end;
procedure TcaSizeMovePanel.SetMainGapLeft(const Value: Integer);
begin
if Value <> FMainGapLeft then
begin
FMainGapLeft := Value;
AlignMain;
end;
end;
procedure TcaSizeMovePanel.SetMainGapRight(const Value: Integer);
begin
if Value <> FMainGapRight then
begin
FMainGapRight := Value;
AlignMain;
end;
end;
procedure TcaSizeMovePanel.SetMainGapTop(const Value: Integer);
begin
if Value <> FMainGapTop then
begin
FMainGapTop := Value;
AlignMain;
end;
end;
procedure TcaSizeMovePanel.SetShowGrabHandle(const Value: Boolean);
begin
if Value <> FShowGrabHandle then
begin
FShowGrabHandle := Value;
Invalidate;
end;
end;
procedure TcaSizeMovePanel.SetSizingPixels(const Value: Integer);
begin
if Value <> FSizingPixels then
begin
FSizingPixels := Value;
end;
end;
procedure TcaSizeMovePanel.SetTitle(const Value: TCaption);
begin
if Value <> FTitle then
begin
FTitle := Value;
Invalidate;
end;
end;
end.