Menu

[r660]: / trunk / Src / UMessageBox.pas  Maximize  Restore  History

Download this file

840 lines (782 with data), 30.6 kB

  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
{
* UMessageBox.pas
*
* Implements a static class that can display message and confirmation dialog
* boxes at an appropriate position on screen.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UMessageBox.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UMessageBox;
interface
uses
// Delphi
Classes, Controls, Dialogs,
// Project
UBaseObjects, UIStringList;
resourcestring
// Button captions
sBtnYes = '&Yes'; // Yes button caption
sBtnNo = '&No'; // No button caption
sBtnOK = 'OK'; // OK button caption
sBtnCancel = 'Cancel'; // Cancel button caption
type
{
TMessageBoxButton:
Record that contains attributes of a button that appears in a TMessageBox
dialog box.
}
TMessageBoxButton = record
Caption: TCaption; // button's caption
ModalResult: Integer; // button's modal result (0 to prevent form closure)
Default: Boolean; // true if button is default
Cancel: Boolean; // true if button is cancel button
constructor Create(const ACaption: TCaption; const AModalResult: Integer;
const ADefault: Boolean = False; const ACancel: Boolean = False);
{Initialises fields of record.
@param ACaption [in] Button's caption.
@param AModalResult [in] Modal result returned when button is clicked.
@param Default [in] Optional: value of button's Default property.
@param Cancel [in] Optional: value of button's Cancel property.
}
end;
{
TMessageBox:
Static class that can display message and confirmation dialog boxes at an
appropriate position on screen.
}
TMessageBox = class sealed(TNoConstructObject)
strict private
class function Display(const Parent: TComponent;
const MsgLines: IStringList; const DlgType: TMsgDlgType;
const Buttons: array of TMessageBoxButton): Word; overload;
{Displays a message in a customised dialog box located over the calling
form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active
form is used.
@param MsgLines [in] Message displayed in dialog. Can store mulitple
lines. Use empty lines to insert space between paragraphs.
@param DlgType [in] Type of dialog box.
@param Buttons [in] Array of records decribing buttons to be displayed.
@return Value indicating which button was pressed to close dialog box.
This will be value of ModalResult field of one of the buttons
specified in Buttons.
}
class function Display(const Parent: TComponent; const Msg: string;
const DlgType: TMsgDlgType;
const Buttons: array of TMessageBoxButton): Word; overload;
{Displays a message in a customised dialog box located over the calling
form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active
form is used.
@param Msg [in] Message displayed in dialog. Separate lines with LF or
CRLF. Separate paragraphs with two line breaks.
@param DlgType [in] Type of dialog box.
@param Buttons [in] Array of records decribing buttons to be displayed.
@return Value indicating which button was pressed to close dialog box.
This will be value of ModalResult field of one of the buttons
specified in Buttons.
}
public
class procedure Information(const Parent: TComponent; const Msg: string);
{Displays a message in an information dialog box located relative to owner
form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active
form is used.
@param Msg [in] Message displayed in dialog. Multi-line messages should
be separated by LF or CRLF.
}
class procedure Error(const Parent: TComponent; const Msg: string);
{Displays a message in an error dialog box located relative to owner form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active
form is used.
@param Msg [in] Message displayed in dialog. Multi-line messages should
be separated by LF or CRLF.
}
class function Confirm(const Parent: TComponent;
const Msg: string): Boolean;
{Displays a message in a confirmation dialog box located relative to owner
form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active
form is used.
@param Msg [in] Message displayed in dialog. Multi-line messages should
be separated by LF or CRLF.
@return True if user clicks Yes and false otherwise.
}
class function Custom(const Parent: TComponent; const Msg: string;
const Buttons: array of TMessageBoxButton): Word;
{Displays a message in a dialog box with custom buttons and no icon that
is located relative to owner form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active
form is used.
@param Msg [in] Message displayed in dialog. Multi-line messages should
be separated by LF or CRLF.
@param Buttons [in] Array of records decribing buttons to be displayed.
@return Value indicating which button was pressed to close dialog box.
This will be value of ModalResult field of one of the buttons
specified in the Buttons array.
}
end;
implementation
uses
// Delphi
SysUtils, Windows, Forms, StdCtrls, ExtCtrls, Consts, Math,
// Project
UConsts, UDlgHelper, UFontHelper, UGraphicUtils, UStructs, UUtils;
type
{
TMessageBoxForm:
Implements a message dialog box that has configurable lines and paragraphs
of text, buttons and icon.
}
TMessageBoxForm = class sealed(TForm)
strict private
var
fImage: TImage; // Image control: displays any icon
fButtons: array of TButton; // Buttons displayed in dialog box
fLabels: array of TLabel; // Labels that display text in dialog box.
const
cXPadding = 16; // Left and right dialog box margins
cYPadding = 16; // Top and bottom dialog box margins
cParaSpacing = 8; // Spacing between paragraphs
cBtnSpacing = 8; // Spacing between buttons
cBtnStripTopMargin = 24; // Margin above button strip
function MaxTextWidth: Integer;
{Calculates maximum horizontal space that can be occupied by the dialog's
text.
@return Required width in pixels.
}
function LabelsHeight: Integer;
{Finds height required to display all labels.
@return Required height in pixels.
}
function LabelHeight(const Lbl: TLabel): Integer;
{Calculates height of a label.
@param Lbl [in] Label for which height is required.
@return Label height in pixels.
}
function LabelsWidth: Integer;
{Finds width of text in widest label.
@return Required width in pixels.
}
function ButtonsHeight: Integer;
{Calculates vertical space required to display button strip.
@return Required height in pixels.
}
function ButtonsWidth: Integer;
{Calculates horizontal space required to display strip of all buttons.
@return Required width in pixels.
}
function TextLeftMargin: Integer;
{Calculates offset of text from dialog box's left hand margin. Depends on
whether dialog box displays an icon.
@return Required margin in pixels.
}
function TextAndIconHeight: Integer;
{Calculates vertical space required to accommodate text and any icon.
@return Required height in pixels.
}
function TextAndIconWidth: Integer;
{Calculates horizontal spaces required to accommodate text and any icon.
@return Required width in pixels.
}
procedure InitCaption(const Title: string; const DlgType: TMsgDlgType);
{Sets text of dialog's caption.
@param Title [in] Title to display in dialog's caption. If '' a default
caption dependent on dialog type is used.
@param DlgType [in] Type of dialog box. Used to determine default
caption if Title is ''.
}
procedure InitImage(const DlgType: TMsgDlgType);
{Creates and initialises and locates image control required to display any
icon. No image control is created if no icon is required.
@param DlgType [in] Type of dialog to be displayed. Determines if icon
is required.
}
procedure InitButtons(const Buttons: array of TMessageBoxButton);
{Creates and sizes dialog's buttons.
@param Buttons [in] Array of records describing buttons to display.
}
procedure InitLabels(const Lines: IStringList);
{Creates and sizes labels used to display text in dialog box.
@param Lines [in] String list containing lines of text to display. Blank
lines indicate a paragraph break.
}
procedure SizeDialogBox;
{Sets size of dialog box to accomodate text, optional icon and buttons.
}
procedure ArrangeControls;
{Arranges all controls within dialog box.
}
public
constructor Create(const Owner: TComponent;
const Text: IStringList;
const Buttons: array of TMessageBoxButton;
const DlgType: TMsgDlgType;
const Title: string = ''); reintroduce;
{Class constructor. Reintroduced constructor that creates dialog box form
with components required to display text, buttons and optional icon.
@param Owner [in] Control that owns this dialog box.
@param Text [in] List of lines of text to display. Blank lines indicate
a paragraph spacer.
@param Buttons [in] Array of records that define buttons to be
displayed.
@param DlgType [in] Type of dialog box to be displayed.
@param Title [in] Optional dialog box caption text. Default text that
depends on DlgType is used if no title is specified.
}
end;
{ TMessageBox }
class function TMessageBox.Confirm(const Parent: TComponent;
const Msg: string): Boolean;
{Displays a message in a confirmation dialog box located relative to owner
form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active form
is used.
@param Msg [in] Message displayed in dialog. Multi-line messages should be
separated by LF or CRLF.
@return True if user clicks Yes and false otherwise.
}
begin
Result := Display(
Parent,
Msg,
mtConfirmation,
[
TMessageBoxButton.Create(sBtnYes, mrYes, True),
TMessageBoxButton.Create(sBtnNo, mrNo, False, True)
]
) = mrYes;
end;
class function TMessageBox.Custom(const Parent: TComponent; const Msg: string;
const Buttons: array of TMessageBoxButton): Word;
{Displays a message in a dialog box with custom buttons and no icon that is
located relative to owner form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active form
is used.
@param Msg [in] Message displayed in dialog. Multi-line messages should be
separated by LF or CRLF.
@param Buttons [in] Array of records decribing buttons to be displayed.
@return Value indicating which button was pressed to close dialog box. This
will be value of ModalResult field of one of the buttons specified in the
Buttons array.
}
begin
Result := Display(Parent, Msg, mtCustom, Buttons);
end;
class function TMessageBox.Display(const Parent: TComponent;
const MsgLines: IStringList; const DlgType: TMsgDlgType;
const Buttons: array of TMessageBoxButton): Word;
{Displays a message in a customised dialog box located over the calling form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active form is
used.
@param MsgLines [in] Message displayed in dialog. Can store mulitple lines.
Use empty lines to insert space between paragraphs.
@param DlgType [in] Type of dialog box.
@param Buttons [in] Array of records decribing buttons to be displayed.
@return Value indicating which button was pressed to close dialog box. This
will be value of ModalResult field of one of the buttons specified in the
Buttons array.
}
var
Dlg: TForm; // dialog box instance
begin
// Create a dialog box of required type
Dlg := TMessageBoxForm.Create(Parent, MsgLines, Buttons, DlgType);
try
// Make sure "Parent" control is parent of dialog and align over it
TDlgHelper.SetDlgParent(Dlg, Parent);
TDlgAligner.Align(Dlg, Parent);
// Display the dialog and return result
Result := Dlg.ShowModal;
finally
FreeAndNil(Dlg);
end;
end;
class function TMessageBox.Display(const Parent: TComponent; const Msg: string;
const DlgType: TMsgDlgType; const Buttons: array of TMessageBoxButton): Word;
{Displays a message in a customised dialog box located over the calling form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active form is
used.
@param Msg [in] Message displayed in dialog. Separate lines with LF or CRLF.
Separate paragraphs with two line breaks.
@param DlgType [in] Type of dialog box.
@param Buttons [in] Array of records decribing buttons to be displayed.
@return Value indicating which button was pressed to close dialog box. This
will be value of ModalResult field of one of the buttons specified in
Buttons.
}
begin
Result := Display(
Parent,
// convert line breaks in Msg to LF only, remove leading and trailing LFs,
// ensure proper sentence and then convert text string list.
TIStringList.Create(
MakeSentence(TrimChar(UnixLineBreaks(Msg), LF)),
LF,
True
),
DlgType,
Buttons
);
end;
class procedure TMessageBox.Error(const Parent: TComponent; const Msg: string);
{Displays a message in an error dialog box located relative to owner form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active form
is used.
@param Msg [in] Message displayed in dialog. Multi-line messages should be
separated by LF or CRLF.
}
begin
MessageBeep(MB_ICONERROR);
Display(
Parent, Msg, mtError, [TMessageBoxButton.Create(sBtnOK, mrOK, True, True)]
);
end;
class procedure TMessageBox.Information(const Parent: TComponent;
const Msg: string);
{Displays a message in an information dialog box located relative to owner
form.
@param Parent [in] Component that dialog box is aligned over and that
becomes parent of dialog box. If Parent is nil then current active form
is used.
@param Msg [in] Message displayed in dialog. Multi-line messages should be
separated by LF or CRLF.
}
begin
Display(
Parent,
Msg,
mtInformation,
[TMessageBoxButton.Create(sBtnOK, mrOK, True, True)]
);
end;
{ TMessageBoxButton }
constructor TMessageBoxButton.Create(const ACaption: TCaption;
const AModalResult: Integer; const ADefault, ACancel: Boolean);
{Initialises fields of record.
@param ACaption [in] Button's caption.
@param AModalResult [in] Modal result returned when button is clicked.
@param Default [in] Optional: value of button's Default property.
@param Cancel [in] Optional: value of button's Cancel property.
}
begin
Caption := ACaption;
ModalResult := AModalResult;
Default := ADefault;
Cancel := ACancel;
end;
{ TMessageBoxForm }
procedure TMessageBoxForm.ArrangeControls;
{Arranges all controls within dialog box.
}
// ---------------------------------------------------------------------------
procedure ArrangeLabels(const Left, Top: Integer);
{Arranges labels within dialog box.
@param Left [in] Left margin for all labels.
@param Top [in] Vertical position of first label.
}
var
Idx: Integer; // loops through all labels
NextTop: Integer; // vertical position of next label
begin
NextTop := Top;
for Idx := Low(fLabels) to High(fLabels) do
begin
fLabels[Idx].Left := Left;
fLabels[Idx].Top := NextTop;
Inc(NextTop, LabelHeight(fLabels[Idx]));
end;
end;
procedure ArrangeButtons(const Width, Top: Integer);
{Arranges buttons in a centred strip in dialog box.
@param Width [in] Width of button strip.
@param Top [in] Top of button strip.
}
var
Idx: Integer; // loops through all buttons
NextLeft: Integer; // horizontal position of each button
begin
NextLeft := (Width - ButtonsWidth) div 2;
for Idx := Low(fButtons) to High(fButtons) do
begin
fButtons[Idx].Top := Top;
fButtons[Idx].Left := NextLeft;
Inc(NextLeft, fButtons[Idx].Width + cBtnSpacing);
end;
end;
// ---------------------------------------------------------------------------
begin
ArrangeLabels(TextLeftMargin, cYPadding);
ArrangeButtons(
ClientWidth, TextAndIconHeight + cYPadding + cBtnStripTopMargin
);
end;
function TMessageBoxForm.ButtonsHeight: Integer;
{Calculates vertical space required to display button strip.
@return Required height in pixels.
}
begin
Result := fButtons[0].Height;
end;
function TMessageBoxForm.ButtonsWidth: Integer;
{Calculates horizontal space required to display strip of all buttons.
@return Required width in pixels.
}
begin
Result := Length(fButtons) * fButtons[0].Width
+ (Length(fButtons) - 1) * cBtnSpacing;
end;
constructor TMessageBoxForm.Create(const Owner: TComponent;
const Text: IStringList; const Buttons: array of TMessageBoxButton;
const DlgType: TMsgDlgType; const Title: string);
{Class constructor. Reintroduced constructor that creates dialog box form with
components required to display text, buttons and optional icon.
@param Owner [in] Control that owns this dialog box.
@param Text [in] List of lines of text to display. Blank lines indicate
a paragraph spacer.
@param Buttons [in] Array of records that define buttons to be displayed.
@param DlgType [in] Type of dialog box to be displayed.
@param Title [in] Optional dialog box caption text. Default text that
depends on DlgType is used if no title is specified.
}
begin
Assert(Length(Buttons) > 0,
ClassName + '.Create: Buttons array is empty');
Assert(Assigned(Text) and (Text.Count > 0),
ClassName + '.Ctreate: No message text provided');
inherited CreateNew(Owner);
Position := poDesigned; // must be poDesgined to enable alignment
BorderStyle := bsDialog; // it's a dialog box
// Set font to OS default
TFontHelper.SetDefaultFont(Font, False);
// Initialise controls
InitCaption(Title, DlgType);
InitImage(DlgType);
InitButtons(Buttons);
InitLabels(Text);
// Size the dialog box
SizeDialogBox;
// Arrange controls within dialog box
ArrangeControls;
end;
procedure TMessageBoxForm.InitButtons(
const Buttons: array of TMessageBoxButton);
{Creates and sizes dialog's buttons.
@param Buttons [in] Array of records describing buttons to display.
}
// ---------------------------------------------------------------------------
procedure CreateButtons;
{Creates each required button control and stores reference in fButtons.
}
var
Idx: Integer; // loops through all button definition records
Btn: TButton; // reference to a created button control
begin
SetLength(fButtons, Length(Buttons));
for Idx := Low(Buttons) to High(Buttons) do
begin
Btn := TButton.Create(Self);
Btn.Parent := Self;
Btn.Name := Format('Button%d', [Idx]);
// set values from button definition record
Btn.Caption := Buttons[Idx].Caption;
Btn.ModalResult := Buttons[Idx].ModalResult;
Btn.Default := Buttons[Idx].Default;
Btn.Cancel := Buttons[Idx].Cancel;
fButtons[Idx] := Btn;
end;
end;
function ButtonTextExtent: TSize;
{Calculates text size of largest button caption. MUST be called after
CreateButtons.
@return Size structure that can hold largest caption.
}
var
Idx: Integer; // loops thru all buttons
Btn: TButton; // references each button
BtnTextExtent: TSize; // text extent of each button caption
begin
Result.cx := -1;
Result.cy := -1;
for Idx := Low(fButtons) to High(fButtons) do
begin
Btn := fButtons[Idx];
BtnTextExtent := StringExtent(Btn.Caption, Btn.Font);
Result.cx := Max(Result.cx, BtnTextExtent.cx);
Result.cy := Max(Result.cy, BtnTextExtent.cy);
end;
end;
procedure SizeButtons(const Size: TSize);
{Sets size of every button to a specified size. MUST be called after
CreateButtons.
@param Size [in] Required button size.
}
var
Idx: Integer; // loops thru all buttons
begin
for Idx := Low(fButtons) to High(fButtons) do
begin
fButtons[Idx].Width := Size.cx;
fButtons[Idx].Height := Size.cy;
end;
end;
// ---------------------------------------------------------------------------
var
TextExtent: TSize; // size of largest button caption
BtnSize: TSize; // size of every button
begin
// Create all required button controls
CreateButtons;
// Find size of largest button caption
TextExtent := ButtonTextExtent;
// Size of each button to be large enough to hold largest caption, but ensure
// buttons are no smaller than default size. All buttons have same size.
BtnSize.cx := Max(
fButtons[0].Width,
TextExtent.cx + fButtons[0].Margins.Left + fButtons[0].Margins.Right
);
BtnSize.cy := Max(
fButtons[0].Height,
TextExtent.cy + fButtons[0].Margins.Top + fButtons[0].Margins.Bottom
);
SizeButtons(BtnSize);
end;
procedure TMessageBoxForm.InitCaption(const Title: string;
const DlgType: TMsgDlgType);
{Sets text of dialog's caption.
@param Title [in] Title to display in dialog's caption. If '' a default
caption dependent on dialog type is used.
@param DlgType [in] Type of dialog box. Used to determine default caption
if Title is ''.
}
const
// Map of dialog types onto resource id of default caption
cCaptions: array[TMsgDlgType] of Pointer = (
@SMsgDlgWarning, @SMsgDlgError, @SMsgDlgInformation, @SMsgDlgConfirm, nil
);
begin
if Title <> '' then
// Caller specified a title: use it
Caption := Title
else
begin
// Caller did not specify a caption: use default
if Assigned(cCaptions[DlgType]) then
// get default from a resource string
Caption := LoadResString(cCaptions[DlgType])
else
// no default resource string: use application's title
Caption := Application.Title;
end;
end;
procedure TMessageBoxForm.InitImage(const DlgType: TMsgDlgType);
{Creates and initialises and locates image control required to display any
icon. No image control is created if no icon is required.
@param DlgType [in] Type of dialog to be displayed. Determines if icon is
required.
}
const
// Map of dialog types onto icon resources
cIconIDs: array[TMsgDlgType] of PChar = (
IDI_EXCLAMATION, IDI_HAND, IDI_ASTERISK, IDI_QUESTION, nil
);
var
IconId: PChar; // resource id of icon (nil if no icon required)
begin
IconId := cIconIDs[DlgType];
if Assigned(IconId) then
begin
fImage := TImage.Create(Self);
fImage.Parent := Self;
fImage.Name := 'Image';
fImage.Picture.Icon.Handle := LoadIcon(0, IconID);
// icon is positioned at top left of dialog box, inside dialog's margins
fImage.BoundsRect := TRectEx.CreateBounds(
cXPadding, cYPadding,
fImage.Picture.Icon.Width, fImage.Picture.Icon.Height
);
end;
end;
procedure TMessageBoxForm.InitLabels(const Lines: IStringList);
{Creates and sizes labels used to display text in dialog box.
@param Lines [in] String list containing lines of text to display. Blank
lines indicate a paragraph break.
}
// ---------------------------------------------------------------------------
procedure CreateLabels;
{Creates all required labels and stores reference in fLabels[].
}
var
Idx: Integer; // loops through all lines of text
Lbl: TLabel; // reference to a label
begin
SetLength(fLabels, Lines.Count);
for Idx := 0 to Pred(Lines.Count) do
begin
Lbl := TLabel.Create(Self);
Lbl.Parent := Self;
Lbl.Name := Format('Label%d', [Idx]);
Lbl.Caption := Lines[Idx];
Lbl.AutoSize := False;
Lbl.WordWrap := True;
Lbl.ShowAccelChar := False;
Lbl.Width := 0; // width and height MUST be initialised to 0 here
Lbl.Height := 0;
fLabels[Idx] := Lbl;
end;
end;
procedure SizeLabels;
{Sets the width and height of the labels. MUST be called after CreateLabels.
}
var
Idx: Integer; // loops through all labels
TextRect: TRectEx; // bounding rectangle of text in label
const
// Text formatting flags
cTextFlags = DT_EXPANDTABS or DT_WORDBREAK or DT_NOPREFIX;
begin
for Idx := Low(fLabels) to High(fLabels) do
begin
// Treat empty labels differently: leave width and height at 0
if fLabels[Idx].Caption = '' then
Continue;
// Get size of rectangle required to display a label
TextRect := GetTextRect(
fLabels[Idx].Caption,
Canvas,
Rect(0, 0, MaxTextWidth, 0),
cTextFlags or fLabels[Idx].DrawTextBiDiModeFlagsReadingOnly
);
// Size the label
fLabels[Idx].Width := TextRect.Width;
fLabels[Idx].Height := TextRect.Height;
end;
end;
// ---------------------------------------------------------------------------
begin
CreateLabels;
SizeLabels;
end;
function TMessageBoxForm.LabelHeight(const Lbl: TLabel): Integer;
{Calculates height of a label.
@param Lbl [in] Label for which height is required.
@return Label height in pixels.
}
begin
// We treat blank labels differently: they represent paragraph breaks
if Lbl.Caption <> '' then
Result := Lbl.Height
else
Result := cParaSpacing;
end;
function TMessageBoxForm.LabelsHeight: Integer;
{Finds height required to display all labels.
@return Required height in pixels.
}
var
Idx: Integer; // loops thru all labels
begin
Result := 0;
for Idx := Low(fLabels) to High(fLabels) do
Inc(Result, LabelHeight(fLabels[Idx]));
end;
function TMessageBoxForm.LabelsWidth: Integer;
{Finds width of text in widest label.
@return Required width in pixels.
}
var
Idx: Integer; // loops thru all labels
begin
Result := 0;
for Idx := Low(fLabels) to High(fLabels) do
begin
if fLabels[Idx].Width > Result then
Result := fLabels[Idx].Width;
end;
end;
function TMessageBoxForm.MaxTextWidth: Integer;
{Calculates maximum horizontal space that can be occupied by the dialog's
text.
@return Required width in pixels.
}
begin
// For smaller screens, max space is half of screen width. For larger screens
// max space is 512 pixels.
Result := Min(512, Screen.Width div 2);
end;
procedure TMessageBoxForm.SizeDialogBox;
{Sets size of dialog box to accomodate text, optional icon and buttons.
}
begin
ClientWidth := Max(ButtonsWidth, TextAndIconWidth) + 2 * cXPadding;
ClientHeight := TextAndIconHeight + ButtonsHeight
+ 2 * cyPadding + cBtnStripTopMargin;
end;
function TMessageBoxForm.TextAndIconHeight: Integer;
{Calculates vertical space required to accommodate text and any icon.
@return Required height in pixels.
}
begin
if Assigned(fImage) then
Result := Max(LabelsHeight, fImage.Height)
else
Result := LabelsHeight;
end;
function TMessageBoxForm.TextAndIconWidth: Integer;
{Calculates horizontal spaces required to accommodate text and any icon.
@return Required width in pixels.
}
begin
Result := LabelsWidth;
if Assigned(fImage) then
Inc(Result, fImage.Width + cXPadding);
end;
function TMessageBoxForm.TextLeftMargin: Integer;
{Calculates offset of text from dialog box's left hand margin. Depends on
whether dialog box displays an icon.
@return Required margin in pixels.
}
begin
if Assigned(fImage) then
Result := fImage.Width + 2 * cXPadding
else
Result := cXPadding;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.