File: extdlgs.pas

package info (click to toggle)
lazarus 2.0.10%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 219,188 kB
  • sloc: pascal: 1,867,962; xml: 265,716; cpp: 56,595; sh: 3,005; java: 609; makefile: 568; perl: 297; sql: 222; ansic: 137
file content (874 lines) | stat: -rw-r--r-- 24,192 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
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
{
 /***************************************************************************
                               extdlgs.pas
                               -----------
                Component Library Extended dialogs Controls


 ***************************************************************************/

 *****************************************************************************
  This file is part of the Lazarus Component Library (LCL)

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}
unit ExtDlgs;

{$mode objfpc}{$H+}

interface

uses
  Types, Classes, SysUtils, LCLProc, LResources, LCLType, LCLStrConsts,
  FileUtil, LazFileUtils, Controls, Dialogs, GraphType, Graphics, ExtCtrls,
  StdCtrls, Forms, Calendar, Buttons, Masks, CalcForm;

type

  { TPreviewFileControl }

  TPreviewFileDialog = class;

  TPreviewFileControl = class(TWinControl)
  private
    FPreviewFileDialog: TPreviewFileDialog;
  protected
    class procedure WSRegisterClass; override;
    class function GetControlClassDefaultSize: TSize; override;
    procedure SetPreviewFileDialog(const AValue: TPreviewFileDialog);
    procedure CreateParams(var Params: TCreateParams); override;
  public
    constructor Create(TheOwner: TComponent); override;
    property PreviewFileDialog: TPreviewFileDialog read FPreviewFileDialog
                                                   write SetPreviewFileDialog;
  end;

  { TPreviewFileDialog }

  TPreviewFileDialog = class(TOpenDialog)
  private
    FPreviewFileControl: TPreviewFileControl;
  protected
    class procedure WSRegisterClass; override;
    procedure CreatePreviewControl; virtual;
    procedure InitPreviewControl; virtual;
    function DoExecute: boolean; override;
  public
    constructor Create(TheOwner: TComponent); override;
    property PreviewFileControl: TPreviewFileControl read FPreviewFileControl;
  end;

  { TOpenPictureDialog }

  TOpenPictureDialog = class(TPreviewFileDialog)
  private
    FDefaultFilter: string;
    FImageCtrl: TImage;
    FPictureGroupBox: TGroupBox;
    FPreviewFilename: string;
  protected
    class procedure WSRegisterClass; override;
    function  IsFilterStored: Boolean; virtual;
    property ImageCtrl: TImage read FImageCtrl;
    property PictureGroupBox: TGroupBox read FPictureGroupBox;
    procedure InitPreviewControl; override;
    procedure ClearPreview; virtual;
    procedure UpdatePreview; virtual;
  public
    constructor Create(TheOwner: TComponent); override;
    procedure DoClose; override;
    procedure DoSelectionChange; override;
    procedure DoShow; override;
    function GetFilterExt: String;
    property DefaultFilter: string read FDefaultFilter;
  published
    property Filter stored IsFilterStored;
  end;

  { TSavePictureDialog }

  TSavePictureDialog = class(TOpenPictureDialog)
  protected
    class procedure WSRegisterClass; override;
    function DefaultTitle: string; override;
  public
    constructor Create(TheOwner: TComponent); override;
  end;

  { TExtCommonDialog }

  // A common base class for custom drawn dialogs (Calculator and Calendar).
  TExtCommonDialog = class(TCommonDialog)
  private
    FDialogPosition: TPosition;
    FLeft: Integer;
    FTop: Integer;
    FDlgForm: TCustomForm;
  protected
    function GetLeft: Integer; virtual;
    function GetHeight: Integer; override;
    function GetTop: Integer; virtual;
    function GetWidth: Integer; override;
    procedure SetLeft(AValue: Integer); virtual;
    procedure SetTop(AValue: Integer); virtual;
    property DlgForm: TCustomForm read FDlgForm write FDlgForm;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Left: Integer read GetLeft write SetLeft;
    property Top: Integer read GetTop write SetTop;
  published
    property DialogPosition: TPosition read FDialogPosition write FDialogPosition default poMainFormCenter;
  end;

  { TCalculatorDialog }

  TCalculatorDialog = class(TExtCommonDialog)
  private
    FLayout: TCalculatorLayout;
    FValue: Double;
    FMemory: Double;
    FPrecision: Byte;
    FBeepOnError: Boolean;
    FOnChange: TNotifyEvent;
    FOnCalcKey: TKeyPressEvent;
    FOnDisplayChange: TNotifyEvent;
    FDialogScale: integer;
    FColorBtnDigits,
    FColorBtnOthers,
    FColorBtnMemory,
    FColorBtnOk,
    FColorBtnCancel,
    FColorBtnClear,
    FColorDisplayText,
    FColorDisplayBack: TColor;
    function GetDisplay: Double;
    procedure SetDialogScale(AValue: integer);
  protected
    class procedure WSRegisterClass; override;
    procedure OnDialogClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure OnDialogShow(Sender: TObject);
    procedure OnDialogCloseQuery(Sender : TObject; var CanClose : boolean);
    procedure Change; virtual;
    procedure CalcKey(var Key: char); virtual;
    function DefaultTitle: string; override;
    procedure DisplayChange; virtual;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function Execute: Boolean; override;
    property CalcDisplay: Double read GetDisplay;
    property Memory: Double read FMemory;
  published
    property BeepOnError: Boolean read FBeepOnError write FBeepOnError default True;
    property CalculatorLayout: TCalculatorLayout read FLayout write FLayout default clNormal;
    property Precision: Byte read FPrecision write FPrecision default CalcDefPrecision;
    property Title;
    property Value: Double read FValue write FValue;
    property OnCalcKey: TKeyPressEvent read FOnCalcKey write FOnCalcKey;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    property OnDisplayChange: TNotifyEvent read FOnDisplayChange write FOnDisplayChange;
    property DialogScale: integer read FDialogScale write SetDialogScale default 100;
    property ColorBtnDigits: TColor read FColorBtnDigits write FColorBtnDigits;
    property ColorBtnMemory: TColor read FColorBtnMemory write FColorBtnMemory;
    property ColorBtnOk: TColor read FColorBtnOk write FColorBtnOk;
    property ColorBtnCancel: TColor read FColorBtnCancel write FColorBtnCancel;
    property ColorBtnClear: TColor read FColorBtnClear write FColorBtnClear;
    property ColorBtnOthers: TColor read FColorBtnOthers write FColorBtnOthers;
    property ColorDisplayText: TColor read FColorDisplayText write FColorDisplayText;
    property ColorDisplayBack: TColor read FColorDisplayBack write FColorDisplayBack;
  end;

  { TCalendarDialog }

  TCalendarDialog = class(TExtCommonDialog)
  private
    FDate: TDateTime;
    FDayChanged: TNotifyEvent;
    FDisplaySettings: TDisplaySettings;
    FMonthChanged: TNotifyEvent;
    FYearChanged: TNotifyEvent;
    FOnChange: TNotifyEvent;
    FOKCaption: TCaption;
    FCancelCaption: TCaption;
    FCalendar: TCalendar;
    procedure OnDialogClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure OnDialogCloseQuery(Sender : TObject; var CanClose : boolean);
    procedure OnDialogShow(Sender: TObject);
    procedure OnCalendarDayChanged(Sender: TObject);
    procedure OnCalendarMonthChanged(Sender: TObject);
    procedure OnCalendarYearChanged(Sender: TObject);
    procedure OnCalendarChange(Sender: TObject);
  protected
    class procedure WSRegisterClass; override;
    procedure GetNewDate(Sender:TObject);//or onClick
    procedure CalendarDblClick(Sender: TObject);
    function DefaultTitle: string; override;
  public
    constructor Create(AOwner: TComponent); override;
    function Execute: Boolean; override;
    property Left: Integer read GetLeft write SetLeft;
    property Top: Integer read GetTop write SetTop;
  published
    property Date: TDateTime read FDate write FDate;
    property DisplaySettings: TDisplaySettings read FDisplaySettings write FDisplaySettings default DefaultDisplaySettings;
    property OnDayChanged: TNotifyEvent read FDayChanged write FDayChanged;
    property OnMonthChanged: TNotifyEvent read FMonthChanged write FMonthChanged;
    property OnYearChanged: TNotifyEvent read FYearChanged write FYearChanged;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
    property OKCaption: TCaption read FOKCaption write FOKCaption;
    property CancelCaption: TCaption read FCancelCaption write FCancelCaption;
  end;

procedure Register;

implementation

//no need as buttons don't have glyphs now
//{$R lcl_calc_images.res}

uses
  WSExtDlgs, Math;

procedure Register;
begin
  RegisterComponents('Dialogs',[TOpenPictureDialog,TSavePictureDialog,
                                TCalendarDialog,TCalculatorDialog]);
end;

{ TPreviewFileControl }

class procedure TPreviewFileControl.WSRegisterClass;
begin
  inherited WSRegisterClass;
  RegisterPreviewFileControl;
end;

procedure TPreviewFileControl.SetPreviewFileDialog(
  const AValue: TPreviewFileDialog);
begin
  if FPreviewFileDialog=AValue then exit;
  FPreviewFileDialog:=AValue;
end;

procedure TPreviewFileControl.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  if Params.WndParent = 0 then
    Params.Style := Params.Style and not WS_CHILD;
end;

class function TPreviewFileControl.GetControlClassDefaultSize: TSize;
begin
  Result.CX := 200;
  Result.CY := 200;
end;

constructor TPreviewFileControl.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  FCompStyle:=csPreviewFileControl;
  SetInitialBounds(0, 0, GetControlClassDefaultSize.CX, GetControlClassDefaultSize.CY);
end;

{ TPreviewFileDialog }

class procedure TPreviewFileDialog.WSRegisterClass;
begin
  inherited WSRegisterClass;
  RegisterPreviewFileDialog;
end;

procedure TPreviewFileDialog.CreatePreviewControl;
begin
  if FPreviewFileControl<>nil then exit;
  FPreviewFileControl:=TPreviewFileControl.Create(Self);
  FPreviewFileControl.PreviewFileDialog:=Self;
  InitPreviewControl;
end;

procedure TPreviewFileDialog.InitPreviewControl;
begin
  FPreviewFileControl.Name:='PreviewFileControl';
end;

function TPreviewFileDialog.DoExecute: boolean;
begin
  CreatePreviewControl;
  Result:=inherited DoExecute;
end;

constructor TPreviewFileDialog.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  FCompStyle:=csPreviewFileDialog;
end;

{ TOpenPictureDialog }

class procedure TOpenPictureDialog.WSRegisterClass;
begin
  inherited WSRegisterClass;
  RegisterOpenPictureDialog;
end;

function TOpenPictureDialog.IsFilterStored: Boolean;
begin
  Result := (Filter<>FDefaultFilter);
end;

procedure TOpenPictureDialog.DoClose;
begin
  ClearPreview;
  inherited DoClose;
end;

procedure TOpenPictureDialog.DoSelectionChange;
begin
  UpdatePreview;
  inherited DoSelectionChange;
end;

procedure TOpenPictureDialog.DoShow;
begin
  ClearPreview;
  inherited DoShow;
end;

procedure TOpenPictureDialog.InitPreviewControl;
begin
  inherited InitPreviewControl;
  FPictureGroupBox.Parent:=PreviewFileControl;
end;

procedure TOpenPictureDialog.ClearPreview;
begin
  FPictureGroupBox.Caption:='None';
  FImageCtrl.Picture:=nil;
end;

procedure TOpenPictureDialog.UpdatePreview;
var
  CurFilename: String;
  FileIsValid: boolean;
begin
  CurFilename := FileName;
  if CurFilename = FPreviewFilename then exit;

  FPreviewFilename := CurFilename;
  FileIsValid := FileExistsUTF8(FPreviewFilename)
                 and (not DirPathExists(FPreviewFilename))
                 and FileIsReadable(FPreviewFilename);
  if FileIsValid then
    try
      FImageCtrl.Picture.LoadFromFile(FPreviewFilename);
      FPictureGroupBox.Caption := Format('(%dx%d)',
        [FImageCtrl.Picture.Width, FImageCtrl.Picture.Height]);
    except
      FileIsValid := False;
    end;
  if not FileIsValid then
    ClearPreview;
end;

constructor TOpenPictureDialog.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  FDefaultFilter := GraphicFilter(TGraphic)+'|'+
                       Format(rsAllFiles,[GetAllFilesMask, GetAllFilesMask,'']);
  Filter:=FDefaultFilter;

  FPictureGroupBox:=TGroupBox.Create(Self);
  with FPictureGroupBox do begin
    Name:='FPictureGroupBox';
    Align:=alClient;
  end;

  FImageCtrl:=TImage.Create(Self);
  with FImageCtrl do begin
    Name:='FImageCtrl';
    Parent:=FPictureGroupBox;
    Align:=alClient;
    Center:=true;
    Proportional:=true;
  end;
end;

function TOpenPictureDialog.GetFilterExt: String;
var
  ParsedFilter: TParseStringList;
begin
  Result := '';
  
  ParsedFilter := TParseStringList.Create(Filter, '|');
  try
    if (FilterIndex > 0) and (FilterIndex * 2 <= ParsedFilter.Count) then
    begin
      Result := AnsiLowerCase(ParsedFilter[FilterIndex * 2 - 1]);
      // remove *.*
      if (Result <> '') and (Result[1] = '*') then Delete(Result, 1, 1);
      if (Result <> '') and (Result[1] = '.') then Delete(Result, 1, 1);
      if (Result <> '') and (Result[1] = '*') then Delete(Result, 1, 1);
      // remove all after ;
      if Pos(';', Result) > 0 then Delete(Result, Pos(';', Result), MaxInt);
    end;
    
    if Result = '' then Result := DefaultExt;
  finally
    ParsedFilter.Free;
  end;
end;

{ TSavePictureDialog }

class procedure TSavePictureDialog.WSRegisterClass;
begin
  inherited WSRegisterClass;
  RegisterSavePictureDialog;
end;

function TSavePictureDialog.DefaultTitle: string;
begin
  Result := rsfdFileSaveAs;
end;

constructor TSavePictureDialog.Create(TheOwner: TComponent);
begin
  inherited Create(TheOwner);
  fCompStyle:=csSaveFileDialog;
end;

{ ---------------------------------------------------------------------
  Auxiliary
  ---------------------------------------------------------------------}

procedure SetDefaultFont(AFont: TFont; Layout: TCalculatorLayout);

begin
  with AFont do
  begin
    Color := clWindowText;
    Name := 'MS Sans Serif';
    Size := 8;
    Style := [fsBold];
  end;
end;


{ TExtCommonDialog }

function TExtCommonDialog.GetLeft: Integer;
begin
  if Assigned(FDlgForm) then FLeft := FDlgForm.Left;
  Result := FLeft;
end;

function TExtCommonDialog.GetHeight: Integer;
begin
  if Assigned(DlgForm) then
    Result := DlgForm.Height
  else
    Result := inherited GetHeight;
end;

function TExtCommonDialog.GetTop: Integer;
begin
  if Assigned(FDlgForm) then FTop := FDlgForm.Top;
  Result := FTop;
end;

function TExtCommonDialog.GetWidth: Integer;
begin
  if Assigned(DlgForm) then
    Result := DlgForm.Width
  else
    Result := inherited GetWidth;
end;

procedure TExtCommonDialog.SetLeft(AValue: Integer);
begin
  if Assigned(FDlgForm) then FDlgForm.Left := AValue;
  FLeft := AValue;
end;

procedure TExtCommonDialog.SetTop(AValue: Integer);
begin
  if Assigned(FDlgForm) then FDlgForm.Top := AValue;
  FTop := AValue;
end;

constructor TExtCommonDialog.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FDialogPosition := poMainFormCenter;     // Set the initial location on screen.
end;

destructor TExtCommonDialog.Destroy;
begin
  inherited Destroy;
end;


{ TCalculatorDialog }

constructor TCalculatorDialog.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FPrecision:=CalcDefPrecision;
  FBeepOnError:=True;
  FDialogScale:=100;
  FLayout:=clNormal;

  FColorBtnDigits:=cColorBtnDigits;
  FColorBtnOthers:=cColorBtnOthers;
  FColorBtnMemory:=cColorBtnMemory;
  FColorBtnOk:=cColorBtnOk;
  FColorBtnCancel:=cColorBtnCancel;
  FColorBtnClear:=cColorBtnClear;
  FColorDisplayText:=cColorDisplayText;
  FColorDisplayBack:=cColorDisplayBack;
end;

destructor TCalculatorDialog.Destroy;
begin
  FOnChange:=nil;
  FOnDisplayChange:=nil;
  inherited Destroy;
end;

class procedure TCalculatorDialog.WSRegisterClass;
begin
  inherited WSRegisterClass;
  RegisterCalculatorDialog;
end;

procedure TCalculatorDialog.OnDialogClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  DoClose;
end;

procedure TCalculatorDialog.OnDialogShow(Sender: TObject);
begin
  DoShow;
end;

procedure TCalculatorDialog.OnDialogCloseQuery(Sender: TObject;
  var CanClose: boolean);
begin
  UserChoice := DlgForm.ModalResult;
  DoCanClose(CanClose);
end;

function TCalculatorDialog.GetDisplay: Double;
begin
  if Assigned(DlgForm) then
    Result:=TCalculatorForm(DlgForm).CalcPanel.DisplayValue
  else Result:=FValue;
end;

procedure TCalculatorDialog.SetDialogScale(AValue: integer);
const
  cMinSize = 80;
  cMaxSize = 400;
begin
  if FDialogScale=AValue then Exit;
  FDialogScale:=Max(cMinSize, Min(cMaxSize, AValue));
end;

procedure TCalculatorDialog.CalcKey(var Key: char);
begin
  if Assigned(FOnCalcKey) then FOnCalcKey(Self, Key);
end;

function TCalculatorDialog.DefaultTitle: string;
begin
  Result := rsCalculator;
end;

procedure TCalculatorDialog.DisplayChange;
begin
  if Assigned(FOnDisplayChange) then FOnDisplayChange(Self);
end;


procedure TCalculatorDialog.Change;
begin
  if Assigned(FOnChange) then FOnChange(Self);
end;

function TCalculatorDialog.Execute: Boolean;
var
  CPanel: TCalculatorPanel;
begin
  cColorBtnDigits:=FColorBtnDigits;
  cColorBtnOthers:=FColorBtnOthers;
  cColorBtnMemory:=FColorBtnMemory;
  cColorBtnOk:=FColorBtnOk;
  cColorBtnCancel:=FColorBtnCancel;
  cColorBtnClear:=FColorBtnClear;
  cColorDisplayText:=FColorDisplayText;
  cColorDisplayBack:=FColorDisplayBack;

  DlgForm:=CreateCalculatorForm(Application, FLayout, HelpContext);
  try
    ResetShowCloseFlags;
    (DlgForm as TCalculatorForm).OnCalcKey:= @Self.CalcKey;
    (DlgForm as TCalculatorForm).OnDisplayChange:= @Self.DisplayChange;
    (DlgForm as TCalculatorForm).OnShow := @Self.OnDialogShow;
    (DlgForm as TCalculatorForm).OnClose := @Self.OnDialogClose;
    (DlgForm as TCalculatorForm).OnCloseQuery :=@Self.OnDialogCloseQuery;

    if FDialogScale<>100 then
      DlgForm.ScaleBy(FDialogScale,100);
    if (csDesigning in ComponentState) then
      DlgForm.Position:=poScreenCenter
    else
      DlgForm.Position:=DialogPosition;
    if (DlgForm.Position=poDesigned) then begin
      DlgForm.Left:=FLeft;
      DlgForm.Top:=FTop;
    end else begin
      FLeft:=DlgForm.Left;
      FTop:=DlgForm.Top;
    end;
    CPanel:=TCalculatorForm(DlgForm).CalcPanel;

    DlgForm.Caption:=Title;
    CPanel.Memory:=FMemory;
    CPanel.UpdateMemoryLabel;
    If Precision>2 then
      CPanel.Precision:=Precision
    else
      CPanel.Precision:=2;
    CPanel.BeepOnError:=BeepOnError;
    if FValue <> 0 then begin
      CPanel.DisplayValue:=FValue;
      CPanel.Status:=csFirst;
      CPanel.OperatorChar:='=';
    end;
    Result := (DlgForm.ShowModal = mrOk);
    FLeft := DlgForm.Left;
    FTop := DlgForm.Top;
    //update private fields FHeight and FWidth of ancestor
    SetHeight(DlgForm.Height);
    SetWidth(DlgForm.Width);
    if Result then begin
      FMemory:=CPanel.Memory;
      if CPanel.DisplayValue <> FValue then begin
        FValue:=CPanel.DisplayValue;
        Change;
      end;
    end;
  finally
    DlgForm.Free;
    DlgForm:=nil;
  end;
end;


{ ---------------------------------------------------------------------
  TCalendarDialog
  ---------------------------------------------------------------------}

{ TCalendarDialog }

constructor TCalendarDialog.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  DisplaySettings := DefaultDisplaySettings;
  Date := trunc(Now);
  OKCaption := rsMbOK;
  CancelCaption := rsMbCancel;
end;

procedure TCalendarDialog.GetNewDate(Sender:TObject);//or onClick
begin
  Date:=FCalendar.DateTime;
end;

procedure TCalendarDialog.CalendarDblClick(Sender: TObject);
var
  CalendarForm: TForm;
  P: TPoint;
  htRes: TCalendarPart;
begin
  P := FCalendar.ScreenToClient(Mouse.CursorPos);
  //if FCalendar.HitTest(P) in [cpNoWhere, cpDate] then
  htRes := FCalendar.HitTest(P);
  if {(htRes = cpNoWhere) or }((htRes = cpDate) and (FCalendar.GetCalendarView = cvMonth)) then
  begin
    GetNewDate(Sender);
    CalendarForm:=TForm(TComponent(Sender).Owner);
    // close the calendar dialog
    CalendarForm.ModalResult:=mrOk;
  end;
end;

function TCalendarDialog.DefaultTitle: string;
begin
  Result := rsPickDate;
end;

procedure TCalendarDialog.OnDialogClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  //if Assigned(OnClose) then OnClose(Self);
  DoClose;
end;

procedure TCalendarDialog.OnDialogCloseQuery(Sender: TObject;
  var CanClose: boolean);
begin
  //if Assigned(OnCanClose) then OnCanClose(Sender, CanClose);
  if DlgForm.ModalResult = mrOK then
    UserChoice := mrOk
  else
    UserChoice := mrCancel;
  DoCanClose(CanClose);
end;

procedure TCalendarDialog.OnDialogShow(Sender: TObject);
begin
  DoShow;
end;

procedure TCalendarDialog.OnCalendarDayChanged(Sender: TObject);
begin
  GetNewDate(Self);
  if Assigned(FDayChanged) then FDayChanged(Self);
end;

procedure TCalendarDialog.OnCalendarMonthChanged(Sender: TObject);
begin
  GetNewDate(Self);
  if Assigned(FMonthChanged) then FMonthChanged(Self);
end;

procedure TCalendarDialog.OnCalendarYearChanged(Sender: TObject);
begin
  GetNewDate(Self);
  if Assigned(FYearChanged) then FYearChanged(Self);
end;

procedure TCalendarDialog.OnCalendarChange(Sender: TObject);
begin
  //Date already updated in OnCalendarXXXChanged
  if Assigned(FOnChange) then FOnChange(Self);
end;


class procedure TCalendarDialog.WSRegisterClass;
begin
  inherited WSRegisterClass;
  RegisterCalendarDialog;
end;

function TCalendarDialog.Execute:boolean;
const
  dw=8;
  bbs=2;
var
  okButton,cancelButton: TButton;
  panel: TPanel;
begin
  DlgForm:=TForm.CreateNew(Application, 0);
  try
    ResetShowCloseFlags;
    DlgForm.DisableAlign;
    DlgForm.Caption:=Title;
    if (csDesigning in ComponentState) then
      DlgForm.Position:=poScreenCenter
    else
      DlgForm.Position:=DialogPosition;
    if (DlgForm.Position=poDesigned) then begin
      DlgForm.Left:=FLeft;
      DlgForm.Top:=FTop;
    end else begin
      FLeft:=DlgForm.Left;
      FTop:=DlgForm.Top;
    end;
    DlgForm.BorderStyle:=bsDialog;
    DlgForm.AutoScroll:=false;
    DlgForm.AutoSize:=true;
    DlgForm.OnShow := @OnDialogShow;
    DlgForm.OnClose:=@OnDialogClose;
    DlgForm.OnCloseQuery:=@OnDialogCloseQuery;

    FCalendar:=TCalendar.Create(DlgForm);
    with FCalendar do begin
      Parent:=DlgForm;
      Align:=alTop;
      DateTime:=Self.Date;
      TabStop:=True;
      DisplaySettings:=Self.DisplaySettings;
      OnDayChanged:=@Self.OnCalendarDayChanged;
      OnMonthChanged:=@Self.OnCalendarMonthChanged;
      OnYearChanged:=@Self.OnCalendarYearChanged;
      OnChange:=@Self.OnCalendarChange;
      OnDblClick:=@CalendarDblClick;
    end;

    panel:=TPanel.Create(DlgForm);
    with panel do begin
      Parent:=DlgForm;
      Caption:='';
      Height:=32;
      AnchorToCompanion(akTop, 0, FCalendar);
      BevelOuter:=bvLowered;
    end;

    okButton:=TButton.Create(DlgForm);
    with okButton do begin
      Parent:=panel;
      Caption:=OKCaption;
      Constraints.MinWidth:=75;
      Constraints.MaxWidth:=FCalendar.Width div 2 - bbs;
      Width:=DlgForm.Canvas.TextWidth(OKCaption)+2*dw;
      ModalResult:=mrOK;
      OnClick:=@GetNewDate;
      //Align:=alRight;
      Anchors := [akTop,akRight];
      BorderSpacing.Right:=bbs;
      AnchorSide[akRight].Side:=asrRight;
      AnchorSide[akRight].Control:=panel;
      AnchorVerticalCenterTo(panel);
      Default:=True;
    end;

    cancelButton:=TButton.Create(DlgForm);
    with cancelButton do begin
      Parent:=panel;
      Caption:=CancelCaption;
      Constraints.MinWidth:=75;
      Constraints.MaxWidth:=FCalendar.Width div 2;
      Width:=DlgForm.Canvas.TextWidth(CancelCaption)+2*dw;;
      ModalResult:=mrCancel;
      //Align:=alLeft;
      BorderSpacing.Left:=bbs;
      Anchors:=[akLeft,akTop];
      AnchorSide[akLeft].Side:=asrLeft;
      AnchorSide[akLeft].Control:=panel;
      AnchorVerticalCenterTo(panel);
      Cancel:=True;
    end;
    DlgForm.ClientWidth := FCalendar.Width;
    DlgForm.ClientHeight := panel.Top+panel.Height;

    DlgForm.EnableAlign;
    Result:=DlgForm.ShowModal=mrOK;
    FLeft:=DlgForm.Left;
    FTop:=DlgForm.Top;
    //update private fields FHeight and FWidth of ancestor
    SetHeight(DlgForm.Height);
    SetWidth(DlgForm.Width);
  finally
    DlgForm.Free;
    DlgForm := nil;
  end;
end;


end.