-
Notifications
You must be signed in to change notification settings - Fork 1
/
JvBalloonHint.pas
2376 lines (2107 loc) · 69.8 KB
/
JvBalloonHint.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
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-----------------------------------------------------------------------------
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/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvBalloonHint.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is Remko Bonte <remkobonte att myrealbox dott com>
Portions created by Remko Bonte are Copyright (C) 2002 Remko Bonte.
All Rights Reserved.
Contributor(s):
2006-01-17 - J. Vignoles - Added support for Unicode hint and header
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.delphi-jedi.org
Known Issues:
* Only dropdown shadow for windows xp systems.
* Only custom animation for windows xp systems, because of use of window region.
-----------------------------------------------------------------------------}
// $Id$
unit JvBalloonHint;
{$I jvcl.inc}
{$I windowsonly.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
Windows, Messages, Classes, Forms, Controls, Graphics, ImgList,
{$IFDEF HAS_UNIT_SYSTEM_UITYPES}
System.UITypes,
{$ENDIF HAS_UNIT_SYSTEM_UITYPES}
{$IFDEF JVCLThemesEnabled}
UxTheme,
{$ENDIF JVCLThemesEnabled}
JvComponentBase;
const
cJvBallonHintVisibleTimeDefault = 5000;
type
TJvStemSize = (ssExtraSmall, ssSmall, ssNormal, ssLarge);
TJvIconKind = (ikCustom, ikNone, ikApplication, ikError, ikInformation, ikQuestion, ikWarning);
TJvBalloonOption = (boUseDefaultHeader, boUseDefaultIcon, boUseDefaultImageIndex,
boShowCloseBtn, boCustomAnimation, boPlaySound);
TJvBalloonOptions = set of TJvBalloonOption;
TJvApplicationHintOption = (ahShowHeaderInHint, ahShowIconInHint, ahPlaySound);
TJvApplicationHintOptions = set of TJvApplicationHintOption;
TJvBalloonPosition = (bpAuto, bpLeftDown, bpRightDown, bpLeftUp, bpRightUp);
TJvAnimationStyle = (atNone, atSlide, atRoll, atRollHorNeg, atRollHorPos, atRollVerNeg,
atRollVerPos, atSlideHorNeg, atSlideHorPos, atSlideVerNeg, atSlideVerPos, atCenter, atBlend);
TJvBalloonHint = class;
PHintData = ^THintData;
THintData = record
RAnchorWindow: TCustomForm;
{ Position of the top-left edge of the window balloon inside the client
rect of the anchor window (Used to move the balloon window if the
anchor window moves): }
RAnchorPosition: TPoint;
{ Position of the stem point inside the client rect of the balloon window
(Used the check on resize of the anchor window whether the stem point is
still inside the balloon window): }
RStemPointPosition: TPoint;
RUTF8Header: {$IFDEF RTL200_UP}UTF8String{$ELSE}string{$ENDIF RTL200_UP};
RUTF8Hint: {$IFDEF RTL200_UP}UTF8String{$ELSE}string{$ENDIF RTL200_UP};
RIconKind: TJvIconKind;
RImageIndex: TImageIndex;
RVisibleTime: Integer;
RShowCloseBtn: Boolean;
RAnimationStyle: TJvAnimationStyle;
RAnimationTime: Cardinal;
{ If the position of the balloon needs to be changed - for example if
DefaultBalloonPosition = bpAuto - RSwitchHeight indicates how much we
change the vertical position; if the balloon is an application hint,
RSwitchHeight is the height of the cursor; if the balloon is attached to
a control, RSwitchHeight is the height of that control }
RSwitchHeight: Integer;
end;
TJvBalloonWindow = class(THintWindow)
private
FCurrentPosition: TJvBalloonPosition;
FSwitchHeight: Integer;
FShowIcon: Boolean;
FShowHeader: Boolean;
FMsg: WideString;
FHeader: WideString;
FTipHeight: Integer;
FTipWidth: Integer;
FTipDelta: Integer;
FImageSize: TSize;
FIconPos: TPoint;
FRoundRect: TRect;
FStemRect: TRect;
FMsgRect: TRect;
FHeaderRect: TRect;
FCloseBtnRect: TRect;
FShowCloseBtn: Boolean;
FIsMultiLineMsg: Boolean;
FUseRegion: Boolean;
function GetStemPointPosition: TPoint;
function GetStemPointPositionInRect(const ARect: TRect): TPoint;
function MultiLineWidth(const Value: string): Integer;
protected
procedure CMTextChanged(var Msg: TMessage); message CM_TEXTCHANGED;
procedure CMShowingChanged(var Msg: TMessage); message CM_SHOWINGCHANGED;
procedure WMEraseBkgnd(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;
procedure CreateParams(var Params: TCreateParams); override;
procedure NCPaint(DC: HDC); override;
procedure Paint; override;
{$IFDEF JVCLThemesEnabled}
function CreateThemedRegion: HRGN;
{$ENDIF JVCLThemesEnabled}
function CreateRegion: HRGN;
procedure UpdateRegion;
procedure CalcAutoPosition(var ARect: TRect);
procedure CheckPosition(var ARect: TRect);
function CalcOffset(const ARect: TRect): TPoint;
procedure MeasureHeader(const MaxWidth: Integer; var AWidth, AHeight: Integer); virtual;
procedure MeasureMsg(const MaxWidth: Integer; var AWidth, AHeight: Integer); virtual;
procedure Init(AData: Pointer); virtual;
procedure CreateWnd; override;
public
constructor Create(AOwner: TComponent); override;
procedure ActivateHint(Rect: TRect; const AHint: string); override;
function CalcHintRect(MaxWidth: Integer; const AHint: string;
AData: Pointer): TRect; override;
function CalcHintRectUTF8(MaxWidth: Integer; const AUTF8Hint: {$IFDEF RTL200_UP}UTF8String{$ELSE}string{$ENDIF RTL200_UP};
AData: Pointer): TRect; virtual;
function CalcHintRectW(MaxWidth: Integer; const AHint: WideString;
AData: Pointer): TRect; virtual;
property StemPointPosition: TPoint read GetStemPointPosition;
end;
TJvBalloonWindowEx = class(TJvBalloonWindow)
private
FCtrl: TJvBalloonHint;
FCloseState: Cardinal;
FImageIndex: TImageIndex;
FIconKind: TJvIconKind;
FAnimationTime: Cardinal;
FAnimationStyle: TJvAnimationStyle;
FIsAnchored: Boolean;
protected
procedure WMNCHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;
procedure WMMouseMove(var Msg: TWMMouseMove); message WM_MOUSEMOVE;
procedure WMLButtonDown(var Msg: TWMLButtonDown); message WM_LBUTTONDOWN;
procedure WMLButtonUp(var Msg: TWMLButtonUp); message WM_LBUTTONUP;
procedure WMActivateApp(var Msg: TWMActivateApp); message WM_ACTIVATEAPP;
procedure Paint; override;
{ Either calls NormalizeTopMost or RestoreTopMost depending on whether the
anchor window has focus }
procedure EnsureTopMost;
{ Sets the balloon on top of anchor window; but below other windows }
procedure NormalizeTopMost;
{ Sets the balloon top most }
procedure RestoreTopMost;
procedure InternalActivateHint(var Rect: TRect; const AHint: string);
procedure MoveWindow(NewPos: TPoint);
procedure ChangeCloseState(const AState: Cardinal);
procedure Init(AData: Pointer); override;
end;
{$IFDEF RTL230_UP}
[ComponentPlatformsAttribute(pidWin32 or pidWin64)]
{$ENDIF RTL230_UP}
TJvBalloonHint = class(TJvComponent)
private
FHint: TJvBalloonWindowEx;
FActive: Boolean;
FOptions: TJvBalloonOptions;
FImages: TCustomImageList;
FDefaultHeader: WideString;
FDefaultIcon: TJvIconKind;
FDefaultImageIndex: TImageIndex;
FData: THintData;
FApplicationHintOptions: TJvApplicationHintOptions;
FDefaultBalloonPosition: TJvBalloonPosition;
FCustomAnimationTime: Cardinal;
FCustomAnimationStyle: TJvAnimationStyle;
FOnBalloonClick: TNotifyEvent;
FOnClose: TNotifyEvent;
FOnCloseBtnClick: TCloseQueryEvent;
FOnDblClick: TNotifyEvent;
FOnMouseDown: TMouseEvent;
FOnMouseMove: TMouseMoveEvent;
FOnMouseUp: TMouseEvent;
FHandle: THandle;
FTimerActive: Boolean;
FMaxWidth: Integer;
function GetHandle: THandle;
function GetUseBalloonAsApplicationHint: Boolean;
procedure SetImages(const Value: TCustomImageList);
procedure SetOptions(const Value: TJvBalloonOptions);
procedure SetUseBalloonAsApplicationHint(const Value: Boolean);
protected
function HookProc(var Msg: TMessage): Boolean;
procedure Hook;
procedure UnHook;
procedure HandleMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure HandleMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
procedure HandleMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure HandleClick(Sender: TObject);
procedure HandleDblClick(Sender: TObject);
function HandleCloseBtnClick: Boolean;
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure StartHintTimer(Value: Integer);
procedure StopHintTimer;
procedure InternalActivateHintPos;
procedure InternalActivateHint(ACtrl: TControl);
procedure WndProc(var Msg: TMessage);
property Handle: THandle read GetHandle;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ActivateHint(ACtrl: TControl; const AHint: WideString; const AHeader: WideString = '';
const VisibleTime: Integer = cJvBallonHintVisibleTimeDefault); overload;
procedure ActivateHint(ACtrl: TControl; const AHint: WideString; const AImageIndex: TImageIndex;
const AHeader: WideString = ''; const VisibleTime: Integer = cJvBallonHintVisibleTimeDefault); overload;
procedure ActivateHint(ACtrl: TControl; const AHint: WideString; const AIconKind: TJvIconKind;
const AHeader: WideString = ''; const VisibleTime: Integer = cJvBallonHintVisibleTimeDefault); overload;
procedure ActivateHintPos(AAnchorWindow: TCustomForm; AAnchorPosition: TPoint;
const AHeader, AHint: WideString; const VisibleTime: Integer = cJvBallonHintVisibleTimeDefault;
const AIconKind: TJvIconKind = ikInformation; const AImageIndex: TImageIndex = -1);
procedure ActivateHintRect(ARect: TRect; const AHeader, AHint: WideString;
const VisibleTime: Integer = cJvBallonHintVisibleTimeDefault; const AIconKind: TJvIconKind = ikInformation;
const AImageIndex: TImageIndex = -1);
procedure CancelHint;
property Active: Boolean read FActive;
published
property CustomAnimationStyle: TJvAnimationStyle read FCustomAnimationStyle write
FCustomAnimationStyle default atBlend;
property CustomAnimationTime: Cardinal read FCustomAnimationTime write FCustomAnimationTime
default 100;
property DefaultBalloonPosition: TJvBalloonPosition read FDefaultBalloonPosition write
FDefaultBalloonPosition default bpAuto;
property DefaultImageIndex: TImageIndex read FDefaultImageIndex write FDefaultImageIndex
default -1;
property DefaultHeader: WideString read FDefaultHeader write FDefaultHeader;
property DefaultIcon: TJvIconKind read FDefaultIcon write FDefaultIcon default ikInformation;
property Images: TCustomImageList read FImages write SetImages;
property Options: TJvBalloonOptions read FOptions write SetOptions default [boShowCloseBtn];
property ApplicationHintOptions: TJvApplicationHintOptions read FApplicationHintOptions write
FApplicationHintOptions default [ahShowHeaderInHint, ahShowIconInHint];
property UseBalloonAsApplicationHint: Boolean read GetUseBalloonAsApplicationHint write
SetUseBalloonAsApplicationHint default False;
property MaxWidth: Integer read FMaxWidth write FMaxWidth default 0;
property OnBalloonClick: TNotifyEvent read FOnBalloonClick write FOnBalloonClick;
property OnCloseBtnClick: TCloseQueryEvent read FOnCloseBtnClick write FOnCloseBtnClick;
property OnClose: TNotifyEvent read FOnClose write FOnClose;
property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
property OnMouseDown: TMouseEvent read FOnMouseDown write FOnMouseDown;
property OnMouseMove: TMouseMoveEvent read FOnMouseMove write FOnMouseMove;
property OnMouseUp: TMouseEvent read FOnMouseUp write FOnMouseUp;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$URL$';
Revision: '$Revision$';
Date: '$Date$';
LogPath: 'JVCL\run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
SysUtils, Math, AppEvnts,
Registry, CommCtrl, MMSystem,
{$IFDEF JVCLThemesEnabled}
{$IFNDEF COMPILER7_UP}
TmSchema,
{$ENDIF !COMPILER7_UP}
{$ENDIF JVCLThemesEnabled}
{$IFDEF SUPPORTS_INLINE}
Types,
{$ENDIF SUPPORTS_INLINE}
ComCtrls, // needed for GetComCtlVersion
JclSysInfo,
{$IFNDEF COMPILER12_UP}
JvJCLUtils,
{$ENDIF ~COMPILER12_UP}
JvJVCLUtils, JvThemes, JvWndProcHook, JvWin32,
JclStringConversions, JclUnicode;
const
{ TJvStemSize = (ssSmall, ssNormal, ssLarge);
ssLarge isn't used (yet)
}
cTipHeight: array [TJvStemSize] of Integer = (12, 19, 21, 24);
cTipWidth: array [TJvStemSize] of Integer = (12, 19, 21, 24);
cTipDelta: array [TJvStemSize] of Integer = (16, 16, 16, 17);
DefaultTextFlags: Longint = DT_LEFT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX;
{$IFDEF JVCLThemesEnabled}
const
{$IFNDEF DELPHI11_UP}
TTBSS_POINTINGUPLEFTWALL = 1;
TTBSS_POINTINGUPCENTERED = 2;
TTBSS_POINTINGUPRIGHTWALL = 3;
TTBSS_POINTINGDOWNRIGHTWALL = 4;
TTBSS_POINTINGDOWNCENTERED = 5;
TTBSS_POINTINGDOWNLEFTWALL = 6;
TTP_BALLOONSTEM = 6;
{$ENDIF !DELPHI11_UP}
cBalloonStemState: array [TJvBalloonPosition] of Integer = (
TTBSS_POINTINGUPRIGHTWALL, // bpAuto
TTBSS_POINTINGUPRIGHTWALL, // bpLeftDown
TTBSS_POINTINGUPLEFTWALL, // bpRightDown
TTBSS_POINTINGDOWNRIGHTWALL, // bpLeftUp
TTBSS_POINTINGDOWNLEFTWALL // bpRightUp
);
{$ENDIF JVCLThemesEnabled}
// Unicode wrapping around DrawTextW so that if ran under Win98/Me, it
// continues to work.
type
TDrawTextW = function(hDC: HDC; lpString: PWideChar; nCount: Integer;
var lpRect: TRect; uFormat: UINT): Integer; stdcall;
var
_DrawTextW: TDrawTextW = nil;
{$IFDEF JVCLStylesEnabled}
_ToolTipThemeHandle: HTHEME = 0;
{$ENDIF JVCLStylesEnabled}
procedure InitUnicodeWrap;
var
UserHandle: HMODULE;
begin
{ The system DLLs for Windows 98 have export symbols for wide character
functions as well, but they all return FALSE, and GetLastError would return
ERROR_CALL_NOT_IMPLEMENTED (120), so don't try to load DrawTextW for
Windows 98 }
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
{ All Windows programs already load user32.dll so we can use GetModuleHandle }
UserHandle := GetModuleHandle('USER32');
if UserHandle <> 0 then
@_DrawTextW := GetProcAddress(UserHandle, 'DrawTextW');
end;
end;
function DrawTextW(hDC: HDC; const WS: WideString; var lpRect: TRect; uFormat: UINT): Integer;
var
S: string;
begin
if Assigned(_DrawTextW) then
Result := _DrawTextW(hDC, PWideChar(WS), Length(WS), lpRect, uFormat)
else
begin
{ The Microsoft Layer for Unicode dll UNICOWS.DLL does probably the same as
the following: }
S := WideCharLenToString(PWideChar(WS), Length(WS));
Result := DrawTextA(hDC, PAnsiChar(AnsiString(S)), Length(S), lpRect, uFormat);
end;
end;
function WorkAreaRect: TRect;
begin
SystemParametersInfo(SPI_GETWORKAREA, 0, @Result, 0);
end;
function DesktopRect: TRect;
begin
Result := Rect(GetSystemMetrics(SM_XVIRTUALSCREEN),
GetSystemMetrics(SM_YVIRTUALSCREEN),
GetSystemMetrics(SM_CXVIRTUALSCREEN),
GetSystemMetrics(SM_CYVIRTUALSCREEN));
end;
function IsWinXP_UP: Boolean;
begin
Result := (Win32Platform = VER_PLATFORM_WIN32_NT) and JclCheckWinVersion(5, 1);
end;
function IsWinVista_UP: Boolean;
begin
Result := (Win32Platform = VER_PLATFORM_WIN32_NT) and JclCheckWinVersion(6, 0);
end;
function IsWinSeven_UP: Boolean;
begin
Result := (Win32Platform = VER_PLATFORM_WIN32_NT) and JclCheckWinVersion(6, 1);
end;
function InternalClientToParent(AControl: TControl; const Point: TPoint;
AParent: TWinControl): TPoint;
begin
Result := AControl.ClientToParent(Point, AParent);
end;
{$IFDEF JVCLThemesEnabled}
function GetToolTipThemeHandle: HTHEME;
begin
{$IFDEF JVCLStylesEnabled}
if not StyleServices.IsSystemStyle then
begin
// Styles don't implement BaloonTips, so we use the System style
if (_ToolTipThemeHandle = 0) and Assigned(OpenThemeData) then
_ToolTipThemeHandle := OpenThemeData(Application.Handle, 'tooltip');
Result := _ToolTipThemeHandle;
end
else
{$ENDIF JVCLStylesEnabled}
Result := StyleServices.Theme[teToolTip];
end;
{$IFDEF JVCLStylesEnabled}
procedure CloseToolTipThemeHandle;
begin
if (_ToolTipThemeHandle <> 0) and Assigned(CloseThemeData) then
begin
CloseThemeData(_ToolTipThemeHandle);
_ToolTipThemeHandle := 0;
end;
end;
{$ENDIF JVCLStylesEnabled}
{$ENDIF JVCLThemesEnabled}
procedure GetHintMessageFont(AFont: TFont);
begin
AFont.Assign(Screen.HintFont);
AFont.Style := AFont.Style - [fsBold];
end;
procedure GetHintTitleFont(AFont: TFont);
(** )
{$IFDEF JVCLThemesEnabled}
var
AThemedTextColor: Integer;
Result: Boolean;
LogFontW: TLogFontW;
{$ENDIF JVCLThemesEnabled}
(**)
begin
(** )
{$IFDEF JVCLThemesEnabled}
if IsWinVista_UP and StyleServices.Enabled then
begin
Result := GetThemeEnumValue(GetToolTipThemeHandle, TTP_BALLOONTITLE, 0, TMT_TEXTCOLOR, AThemedTextColor) = S_OK;
if Result then
begin
// GetThemeFont is defined wrong; so cast it
Result := GetThemeFont(GetToolTipThemeHandle, 0, TTP_BALLOONTITLE, 0,
TMT_FONT, {$IFDEF COMPILER12_UP}PLogFontW{$ELSE}PLogFontA{$ENDIF COMPILER12_UP}(@LogFontW)^) = S_OK;
if Result then
begin
AFont.Color := AThemedTextColor;
AFont.Handle := CreateFontIndirectW(LogFontW);
Exit;
end;
end;
end;
{$ENDIF JVCLThemesEnabled}
(**)
AFont.Assign(Screen.HintFont);
AFont.Style := AFont.Style + [fsBold];
end;
function IsMultiLineStr(const Value: WideString): Boolean;
var
Head, Tail: PWideChar;
LineCount: Integer;
begin
// stripped copy of TWideStrings.SetText
LineCount := 0;
Head := PWideChar(Value);
while (Head^ <> WideNull) and (LineCount < 2) do
begin
Tail := Head;
while True do
begin
case Tail^ of
WideNull, WideLineFeed, WideCarriageReturn, WideVerticalTab, WideFormFeed,
WideLineSeparator, WideParagraphSeparator:
Break;
end;
Inc(Tail);
end;
Inc(LineCount);
Head := Tail;
if Head^ <> WideNull then
begin
Inc(Head);
if (Tail^ = WideCarriageReturn) and (Head^ = WideLineFeed) then
Inc(Head);
end;
end;
Result := LineCount >= 2;
end;
type
TGlobalCtrl = class(TComponent)
private
FBkColor: TColor;
FMainCtrl: TJvBalloonHint;
FDefaultImages: TImageList;
FNeedUpdateBkColor: Boolean;
FOldHintWindowClass: THintWindowClass;
FSounds: array [TJvIconKind] of string;
FUseBalloonAsApplicationHint: Boolean;
FDesigning: Boolean;
FAppEvents: TApplicationEvents;
function GetMainCtrl: TJvBalloonHint;
procedure GetDefaultImages;
procedure GetDefaultSounds;
procedure SetBkColor(const Value: TColor);
procedure SetUseBalloonAsApplicationHint(const Value: Boolean);
procedure SetMainCtrl(const Value: TJvBalloonHint);
procedure AppEventsShowHint(var HintStr: string; var CanShow: Boolean; var HintInfo: THintInfo);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function HintImageSize: TSize; overload;
function HintImageSize(const AIconKind: TJvIconKind;
const AImageIndex: TImageIndex): TSize; overload;
procedure DrawHintImage(Canvas: TCanvas; X, Y: Integer; const ABkColor: TColor); overload;
procedure DrawHintImage(Canvas: TCanvas; X, Y: Integer; const AIconKind: TJvIconKind;
const AImageIndex: TImageIndex; const ABkColor: TColor); overload;
procedure PlaySound(const AIconKind: TJvIconKind);
property BkColor: TColor read FBkColor write SetBkColor;
property MainCtrl: TJvBalloonHint read GetMainCtrl write SetMainCtrl;
property UseBalloonAsApplicationHint: Boolean read FUseBalloonAsApplicationHint
write SetUseBalloonAsApplicationHint;
end;
var
GGlobalCtrl: TGlobalCtrl = nil;
{ A TJvBalloonHint may be needed, while there isn't an instance of it around.
For example, if the user sets HintWindowClass to TJvBalloonWindow.
}
GMainCtrl: TJvBalloonHint = nil;
function GlobalCtrl: TGlobalCtrl;
begin
if not Assigned(GGlobalCtrl) then
GGlobalCtrl := TGlobalCtrl.Create(nil);
Result := GGlobalCtrl;
end;
//=== { TJvBalloonWindow } ===================================================
constructor TJvBalloonWindow.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := [csCaptureMouse, csClickEvents, csDoubleClicks];
end;
var
OldAnimateWindowProc: TAnimateWindowProc;
function JvAnimateWindowProc(hWnd: HWND; dwTime: DWORD; dwFlags: DWORD): BOOL; stdcall;
begin
SendMessage(hWnd, CM_RECREATEWND, 0, 0);
Result := OldAnimateWindowProc(hWnd, dwTime, dwFlags);
end;
{$IFNDEF COMPILER7_UP}
const
ComCtlVersionIE6 = $00060000;
{$ENDIF !COMPILER7_UP}
procedure TJvBalloonWindow.ActivateHint(Rect: TRect; const AHint: string);
var
Delta: Integer;
begin
ParentWindow := Application.Handle;
if IsWinVista_UP and (GetComCtlVersion >= ComCtlVersionIE6) then
RecreateWnd;
if HandleAllocated and IsWindowVisible(Handle) then
ShowWindow(Handle, SW_HIDE);
if UseRightToLeftAlignment then
begin
// Remove the offset set by TApplication.ActivateHint
Delta := MultiLineWidth(AHint) + 5;
Inc(Rect.Left, Delta);
Inc(Rect.Right, Delta);
end;
CheckPosition(Rect);
UpdateRegion;
Inc(Rect.Bottom, 4);
UpdateBoundsRect(Rect);
Dec(Rect.Bottom, 4);
if ahPlaySound in GlobalCtrl.MainCtrl.ApplicationHintOptions then
GlobalCtrl.PlaySound(GlobalCtrl.MainCtrl.DefaultIcon);
if IsWinVista_UP and (GetComCtlVersion < ComCtlVersionIE6) then
begin
OldAnimateWindowProc := AnimateWindowProc;
AnimateWindowProc := JvAnimateWindowProc;
try
inherited ActivateHint(Rect, AHint);
finally
AnimateWindowProc := OldAnimateWindowProc;
end;
end
else
inherited ActivateHint(Rect, AHint);
end;
procedure TJvBalloonWindow.CalcAutoPosition(var ARect: TRect);
var
NewPosition: TJvBalloonPosition;
ScreenRect: TRect;
LStemPointPosition: TPoint;
Pt: TPoint;
begin
{ bpAuto returns the same value as bpLeftDown; bpLeftDown is choosen
arbitrary }
FCurrentPosition := bpLeftDown;
ScreenRect := {>>>WorkAreaRect}DesktopRect{<<<};
{ Note: 2*(Left + Width div 2) = 2*(Left + (Right-Left) div 2) ~=
2*Left + (Right-Left) = Left + Right;
Thus multiply everything with 2
Monitor:
|---------------|
| | |
| 1 | 2 |
| | |
|---------------|
| | |
| 3 | 4 |
| | |
|---------------|
}
Pt := GetStemPointPositionInRect(ARect);
LStemPointPosition := Point(Pt.X * 2, Pt.Y * 2);
if LStemPointPosition.Y < ScreenRect.Top + ScreenRect.Bottom then
begin
if LStemPointPosition.X < ScreenRect.Left + ScreenRect.Right then
{ 1 }
NewPosition := bpLeftUp
else
{ 2 }
NewPosition := bpRightUp;
end
else
begin
if LStemPointPosition.X < ScreenRect.Left + ScreenRect.Right then
{ 3 }
NewPosition := bpLeftDown
else
{ 4 }
NewPosition := bpRightDown;
end;
if NewPosition <> FCurrentPosition then
begin
{ Reset the offset.. }
Pt := CalcOffset(ARect);
OffsetRect(ARect, -Pt.X, -Pt.Y);
FCurrentPosition := NewPosition;
{ ..and set the offset }
Pt := CalcOffset(ARect);
OffsetRect(ARect, Pt.X, Pt.Y);
end;
end;
function TJvBalloonWindow.CalcHintRect(MaxWidth: Integer; const AHint: string;
AData: Pointer): TRect;
begin
// Mantis 3855: CalcHintRect is called by the VCL code and gives a non
// UTF-8 string. However, this string may contain characters above 127 which
// would then be interpreted as UTF-8 markers. So when you are sure the
// string for the hint is UTF-8, use CalcHintRectUTF8 below. This is what is
// done by the TJvBalloonHint.InternalActivateHintPos code.
// In any case the CalcHintRectW function is called in the end.
Result := CalcHintRectW(MaxWidth, WideString(AHint), AData);
end;
function TJvBalloonWindow.CalcHintRectUTF8(MaxWidth: Integer;
const AUTF8Hint: {$IFDEF RTL200_UP}UTF8String{$ELSE}string{$ENDIF RTL200_UP}; AData: Pointer): TRect;
begin
Result := CalcHintRectW(MaxWidth, {$IFDEF RTL200_UP}System.{$ENDIF RTL200_UP}UTF8ToWideString(AUTF8Hint), AData);
end;
function TJvBalloonWindow.CalcHintRectW(MaxWidth: Integer;
const AHint: WideString; AData: Pointer): TRect;
var
ASize: TSize;
StemSize: TJvStemSize;
Pt: TPoint;
begin
FUseRegion := False;
Init(AData);
FMsg := AHint;
FIsMultiLineMsg := IsMultiLineStr(FMsg);
if FShowIcon then
begin
if IsWinVista_UP and (GetComCtlVersion >= ComCtlVersionIE6) then
FIconPos := Point(12, 12)
else
FIconPos := Point(12, 9);
end;
SetRectEmpty(FHeaderRect);
MeasureHeader(MaxWidth, FHeaderRect.Right, FHeaderRect.Bottom);
if not IsRectEmpty(FHeaderRect) then
begin
if IsWinVista_UP and (GetComCtlVersion >= ComCtlVersionIE6) then
OffsetRect(FHeaderRect, 12, 9)
else
OffsetRect(FHeaderRect, 12, 10)
end;
SetRectEmpty(FMsgRect);
MeasureMsg(MaxWidth, FMsgRect.Right, FMsgRect.Bottom);
if not IsRectEmpty(FMsgRect) then
begin
if IsWinVista_UP then
begin
{$IFDEF JVCLThemesEnabled}
if StyleServices.Enabled then
OffsetRect(FMsgRect, 12, Max(9, FHeaderRect.Bottom))
else
{$ENDIF JVCLThemesEnabled}
if GetComCtlVersion >= ComCtlVersionIE6 then
OffsetRect(FMsgRect, 12, FHeaderRect.Bottom + 3)
else
begin
if FShowIcon then
OffsetRect(FMsgRect, 12, Max(FIconPos.Y + FImageSize.cy + 6, FHeaderRect.Bottom + 5))
else
OffsetRect(FMsgRect, 12, Max(9, FHeaderRect.Bottom + 5));
end;
end
else
begin
if FShowIcon then
OffsetRect(FMsgRect, 12, Max(FIconPos.Y + FImageSize.cy + 5, FHeaderRect.Bottom + 4))
else
OffsetRect(FMsgRect, 12, Max(9, FHeaderRect.Bottom + 4));
end;
end;
if FShowIcon then
begin
// move the right position of the header
if IsWinVista_UP and (GetComCtlVersion >= ComCtlVersionIE6) then
OffsetRect(FHeaderRect, FImageSize.cx + 5, 0)
else
OffsetRect(FHeaderRect, FImageSize.cx + 8, 0);
// move the right position of the msg; only for vista
if IsWinVista_UP and (GetComCtlVersion >= ComCtlVersionIE6) then
OffsetRect(FMsgRect, FImageSize.cx + 5, 0);
end;
{$IFDEF JVCLThemesEnabled}
if IsWinVista_UP and StyleServices.Enabled and FIsMultiLineMsg then
begin
GetThemePartSize(GetToolTipThemeHandle, 0, TTP_BALLOONSTEM, cBalloonStemState[FCurrentPosition],
nil, TS_TRUE, ASize);
FStemRect := Rect(0, 0, ASize.cx, ASize.cy);
FTipHeight := ASize.cy;
FTipWidth := ASize.cx;
FTipDelta := $10;
end
else
{$ENDIF JVCLThemesEnabled}
begin
if IsRectEmpty(FHeaderRect) then
StemSize := ssExtraSmall
else
if not FIsMultiLineMsg then
StemSize := ssSmall
else
StemSize := ssNormal;
FTipHeight := cTipHeight[StemSize];
FTipWidth := cTipWidth[StemSize];
FStemRect := Rect(0, 0, FTipWidth, FTipHeight);
FTipDelta := cTipDelta[StemSize];
end;
if FShowCloseBtn then
begin
{$IFDEF JVCLThemesEnabled}
if IsWinXP_UP and StyleServices.Enabled then
GetThemePartSize(GetToolTipThemeHandle, 0, TTP_CLOSE, TTCS_NORMAL, nil, TS_DRAW, ASize)
else
{$ENDIF JVCLThemesEnabled}
begin
ASize.cx := GetSystemMetrics(SM_CXSMICON);
ASize.cy := GetSystemMetrics(SM_CYSMICON);
end;
FCloseBtnRect := Rect(0, 0, ASize.cx, ASize.cy);
Inc(FHeaderRect.Right, ASize.cx);
Inc(FMsgRect.Right, ASize.cx);
end;
if IsWinVista_UP and (GetComCtlVersion >= ComCtlVersionIE6) then
begin
FRoundRect := Rect(0, 0, Max(13 + FMsgRect.Right, 13 + FHeaderRect.Right),
Max(FMsgRect.Bottom + 10, FHeaderRect.Bottom + 10));
OffsetRect(FStemRect, FTipDelta, FRoundRect.Bottom - 1);
end
else
begin
FRoundRect := Rect(0, 0, Max(14 + FMsgRect.Right, 14 + FHeaderRect.Right),
Max(FMsgRect.Bottom + 11, FHeaderRect.Bottom + 11));
OffsetRect(FStemRect, FTipDelta, FRoundRect.Bottom - 1);
end;
UnionRect(Result, FRoundRect, FStemRect);
Pt := CalcOffset(Result);
OffsetRect(Result, Pt.X, Pt.Y);
OffsetRect(FCloseBtnRect, FRoundRect.Right - (FCloseBtnRect.Right - FCloseBtnRect.Left) - 6, 6);
FUseRegion := True;
end;
function TJvBalloonWindow.CalcOffset(const ARect: TRect): TPoint;
begin
case FCurrentPosition of
{ bpAuto returns the same value as bpLeftDown; bpLeftDown is choosen
arbitrary }
bpAuto, bpLeftDown:
Result := Point(ARect.Left - ARect.Right + FTipDelta, 0);
bpRightDown:
Result := Point(-FTipDelta, 0);
bpLeftUp:
Result := Point(ARect.Left - ARect.Right + FTipDelta, ARect.Top - ARect.Bottom - FSwitchHeight);
bpRightUp:
Result := Point(-FTipDelta, ARect.Top - ARect.Bottom - FSwitchHeight);
end;
end;
procedure TJvBalloonWindow.CheckPosition(var ARect: TRect);
var
NewPosition: TJvBalloonPosition;
ScreenRect: TRect;
Pt: TPoint;
begin
if FCurrentPosition = bpAuto then
CalcAutoPosition(ARect);
NewPosition := FCurrentPosition;
ScreenRect := {>>>WorkAreaRect}DesktopRect{<<<};
if ARect.Bottom > ScreenRect.Bottom - ScreenRect.Top then
begin
if NewPosition = bpLeftDown then
NewPosition := bpLeftUp
else
if NewPosition = bpRightDown then
NewPosition := bpRightUp;
end;
if ARect.Right > ScreenRect.Right - ScreenRect.Left then
begin
if NewPosition = bpRightDown then
NewPosition := bpLeftDown
else
if NewPosition = bpRightUp then
NewPosition := bpLeftUp;
end;
if ARect.Left < ScreenRect.Left then
begin
if NewPosition = bpLeftDown then
NewPosition := bpRightDown
else
if NewPosition = bpLeftUp then
NewPosition := bpRightUp;
end;
if ARect.Top < ScreenRect.Top then
begin
if NewPosition = bpLeftUp then
NewPosition := bpLeftDown
else
if NewPosition = bpRightUp then
NewPosition := bpRightDown;
end;
if NewPosition <> FCurrentPosition then
begin
{ Reset the offset.. }
Pt := CalcOffset(ARect);
OffsetRect(ARect, -Pt.X, -Pt.Y);
FCurrentPosition := NewPosition;
{ ..and set the offset }
Pt := CalcOffset(ARect);
OffsetRect(ARect, Pt.X, Pt.Y);
end;
{ final adjustment - just make sure no part is disappearing outside the top/left edge }
if ARect.Left < ScreenRect.Left then
begin
Pt := CalcOffset(ARect);
OffsetRect(ARect, -Pt.X, -Pt.Y);
if FCurrentPosition = bpLeftUp then
FCurrentPosition := bpRightUp
else
if FCurrentPosition = bpLeftDown then
FCurrentPosition := bpRightDown;
Pt := CalcOffset(ARect);
OffsetRect(ARect, Pt.X, Pt.Y);
end;
if ARect.Top < ScreenRect.Top then
begin
Pt := CalcOffset(ARect);
OffsetRect(ARect, -Pt.X, -Pt.Y);
if FCurrentPosition = bpLeftUp then
FCurrentPosition := bpLeftDown
else
if FCurrentPosition = bpRightUp then
FCurrentPosition := bpRightDown;
Pt := CalcOffset(ARect);
OffsetRect(ARect, Pt.X, Pt.Y);
end;
case FCurrentPosition of
bpLeftDown, bpRightDown:
begin
OffsetRect(FRoundRect, 0, FTipHeight - 1);
OffsetRect(FStemRect, 0, -FStemRect.Top);
OffsetRect(FMsgRect, 0, FTipHeight - 1);
OffsetRect(FHeaderRect, 0, FTipHeight - 1);
Inc(FIconPos.y, FTipHeight);
OffsetRect(FCloseBtnRect, 0, FTipHeight);
if FCurrentPosition = bpLeftDown then
OffsetRect(FStemRect, -2 * FTipDelta + (FRoundRect.Right - FRoundRect.Left) - (FStemRect.Right - FStemRect.Left), 0);
end;
bpLeftUp, bpRightUp:
begin
if FCurrentPosition = bpLeftUp then
OffsetRect(FStemRect, -2 * FTipDelta + (FRoundRect.Right - FRoundRect.Left) - (FStemRect.Right - FStemRect.Left), 0);
end;
end;
end;
procedure TJvBalloonWindow.CMShowingChanged(var Msg: TMessage);
begin
{ In response of RecreateWnd, SetParentWindow calls, only respond when visible }
{ Actually only necessairy for TJvBalloonWindow not for TJvBalloonWindowEx }
if Showing then
UpdateRegion;
inherited;
end;
procedure TJvBalloonWindow.CMTextChanged(var Msg: TMessage);
begin
{inherited;}
end;