forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnsNativeThemeWin.cpp
4016 lines (3625 loc) · 137 KB
/
nsNativeThemeWin.cpp
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
/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <windows.h>
#include "nsNativeThemeWin.h"
#include "nsRenderingContext.h"
#include "nsRect.h"
#include "nsSize.h"
#include "nsTransform2D.h"
#include "nsThemeConstants.h"
#include "nsIPresShell.h"
#include "nsPresContext.h"
#include "nsIContent.h"
#include "nsIFrame.h"
#include "nsEventStates.h"
#include "nsINameSpaceManager.h"
#include "nsIDOMHTMLInputElement.h"
#include "nsMenuFrame.h"
#include "nsGkAtoms.h"
#include <malloc.h>
#include "nsWindow.h"
#include "nsIComboboxControlFrame.h"
#include "prinrval.h"
#include "WinUtils.h"
#include "gfxPlatform.h"
#include "gfxContext.h"
#include "gfxMatrix.h"
#include "gfxWindowsPlatform.h"
#include "gfxWindowsSurface.h"
#include "gfxWindowsNativeDrawing.h"
#include "nsUXThemeData.h"
#include "nsUXThemeConstants.h"
#include <algorithm>
using namespace mozilla::widget;
#ifdef PR_LOGGING
extern PRLogModuleInfo* gWindowsLog;
#endif
NS_IMPL_ISUPPORTS_INHERITED1(nsNativeThemeWin, nsNativeTheme, nsITheme)
nsNativeThemeWin::nsNativeThemeWin() :
mProgressDeterminateTimeStamp(TimeStamp::Now()),
mProgressIndeterminateTimeStamp(TimeStamp::Now())
{
// If there is a relevant change in forms.css for windows platform,
// static widget style variables (e.g. sButtonBorderSize) should be
// reinitialized here.
}
nsNativeThemeWin::~nsNativeThemeWin()
{
nsUXThemeData::Invalidate();
}
static int32_t
GetTopLevelWindowActiveState(nsIFrame *aFrame)
{
// Get the widget. nsIFrame's GetNearestWidget walks up the view chain
// until it finds a real window.
nsIWidget* widget = aFrame->GetNearestWidget();
nsWindowBase * window = static_cast<nsWindowBase*>(widget);
if (!window)
return mozilla::widget::themeconst::FS_INACTIVE;
if (widget && !window->IsTopLevelWidget() &&
!(window = window->GetParentWindowBase(false)))
return mozilla::widget::themeconst::FS_INACTIVE;
if (window->GetWindowHandle() == ::GetActiveWindow())
return mozilla::widget::themeconst::FS_ACTIVE;
return mozilla::widget::themeconst::FS_INACTIVE;
}
static int32_t
GetWindowFrameButtonState(nsIFrame *aFrame, nsEventStates eventState)
{
if (GetTopLevelWindowActiveState(aFrame) ==
mozilla::widget::themeconst::FS_INACTIVE) {
if (eventState.HasState(NS_EVENT_STATE_HOVER))
return mozilla::widget::themeconst::BS_HOT;
return mozilla::widget::themeconst::BS_INACTIVE;
}
if (eventState.HasState(NS_EVENT_STATE_HOVER)) {
if (eventState.HasState(NS_EVENT_STATE_ACTIVE))
return mozilla::widget::themeconst::BS_PUSHED;
return mozilla::widget::themeconst::BS_HOT;
}
return mozilla::widget::themeconst::BS_NORMAL;
}
static int32_t
GetClassicWindowFrameButtonState(nsEventStates eventState)
{
if (eventState.HasState(NS_EVENT_STATE_ACTIVE) &&
eventState.HasState(NS_EVENT_STATE_HOVER))
return DFCS_BUTTONPUSH|DFCS_PUSHED;
return DFCS_BUTTONPUSH;
}
static bool
IsTopLevelMenu(nsIFrame *aFrame)
{
bool isTopLevel(false);
nsMenuFrame *menuFrame = do_QueryFrame(aFrame);
if (menuFrame) {
isTopLevel = menuFrame->IsOnMenuBar();
}
return isTopLevel;
}
static MARGINS
GetCheckboxMargins(HANDLE theme, HDC hdc)
{
MARGINS checkboxContent = {0};
GetThemeMargins(theme, hdc, MENU_POPUPCHECK, MCB_NORMAL,
TMT_CONTENTMARGINS, NULL, &checkboxContent);
return checkboxContent;
}
static SIZE
GetCheckboxBGSize(HANDLE theme, HDC hdc)
{
SIZE checkboxSize;
GetThemePartSize(theme, hdc, MENU_POPUPCHECK, MC_CHECKMARKNORMAL,
NULL, TS_TRUE, &checkboxSize);
MARGINS checkboxMargins = GetCheckboxMargins(theme, hdc);
int leftMargin = checkboxMargins.cxLeftWidth;
int rightMargin = checkboxMargins.cxRightWidth;
int topMargin = checkboxMargins.cyTopHeight;
int bottomMargin = checkboxMargins.cyBottomHeight;
int width = leftMargin + checkboxSize.cx + rightMargin;
int height = topMargin + checkboxSize.cy + bottomMargin;
SIZE ret;
ret.cx = width;
ret.cy = height;
return ret;
}
static SIZE
GetCheckboxBGBounds(HANDLE theme, HDC hdc)
{
MARGINS checkboxBGSizing = {0};
MARGINS checkboxBGContent = {0};
GetThemeMargins(theme, hdc, MENU_POPUPCHECKBACKGROUND, MCB_NORMAL,
TMT_SIZINGMARGINS, NULL, &checkboxBGSizing);
GetThemeMargins(theme, hdc, MENU_POPUPCHECKBACKGROUND, MCB_NORMAL,
TMT_CONTENTMARGINS, NULL, &checkboxBGContent);
#define posdx(d) ((d) > 0 ? d : 0)
int dx = posdx(checkboxBGContent.cxRightWidth -
checkboxBGSizing.cxRightWidth) +
posdx(checkboxBGContent.cxLeftWidth -
checkboxBGSizing.cxLeftWidth);
int dy = posdx(checkboxBGContent.cyTopHeight -
checkboxBGSizing.cyTopHeight) +
posdx(checkboxBGContent.cyBottomHeight -
checkboxBGSizing.cyBottomHeight);
#undef posdx
SIZE ret(GetCheckboxBGSize(theme, hdc));
ret.cx += dx;
ret.cy += dy;
return ret;
}
static SIZE
GetGutterSize(HANDLE theme, HDC hdc)
{
SIZE gutterSize;
GetThemePartSize(theme, hdc, MENU_POPUPGUTTER, 0, NULL, TS_TRUE, &gutterSize);
SIZE checkboxBGSize(GetCheckboxBGBounds(theme, hdc));
SIZE itemSize;
GetThemePartSize(theme, hdc, MENU_POPUPITEM, MPI_NORMAL, NULL, TS_TRUE, &itemSize);
// Figure out how big the menuitem's icon will be (if present) at current DPI
double scaleFactor = nsIWidget::DefaultScaleOverride();
if (scaleFactor <= 0.0) {
scaleFactor = gfxWindowsPlatform::GetPlatform()->GetDPIScale();
}
int iconDevicePixels = NSToIntRound(16 * scaleFactor);
SIZE iconSize = {
iconDevicePixels, iconDevicePixels
};
// Not really sure what margins should be used here, but this seems to work in practice...
MARGINS margins = {0};
GetThemeMargins(theme, hdc, MENU_POPUPCHECKBACKGROUND, MCB_NORMAL,
TMT_CONTENTMARGINS, NULL, &margins);
iconSize.cx += margins.cxLeftWidth + margins.cxRightWidth;
iconSize.cy += margins.cyTopHeight + margins.cyBottomHeight;
int width = std::max(itemSize.cx, std::max(iconSize.cx, checkboxBGSize.cx) + gutterSize.cx);
int height = std::max(itemSize.cy, std::max(iconSize.cy, checkboxBGSize.cy));
SIZE ret;
ret.cx = width;
ret.cy = height;
return ret;
}
/* DrawThemeBGRTLAware - render a theme part based on rtl state.
* Some widgets are not direction-neutral and need to be drawn reversed for
* RTL. Windows provides a way to do this with SetLayout, but this reverses
* the entire drawing area of a given device context, which means that its
* use will also affect the positioning of the widget. There are two ways
* to work around this:
*
* Option 1: Alter the position of the rect that we send so that we cancel
* out the positioning effects of SetLayout
* Option 2: Create a memory DC with the widgetRect's dimensions, draw onto
* that, and then transfer the results back to our DC
*
* This function tries to implement option 1, under the assumption that the
* correct way to reverse the effects of SetLayout is to translate the rect
* such that the offset from the DC bitmap's left edge to the old rect's
* left edge is equal to the offset from the DC bitmap's right edge to the
* new rect's right edge. In other words,
* (oldRect.left + vpOrg.x) == ((dcBMP.width - vpOrg.x) - newRect.right)
*/
static HRESULT
DrawThemeBGRTLAware(HANDLE aTheme, HDC aHdc, int aPart, int aState,
const RECT *aWidgetRect, const RECT *aClipRect,
bool aIsRtl)
{
NS_ASSERTION(aTheme, "Bad theme handle.");
NS_ASSERTION(aHdc, "Bad hdc.");
NS_ASSERTION(aWidgetRect, "Bad rect.");
NS_ASSERTION(aClipRect, "Bad clip rect.");
if (!aIsRtl) {
return DrawThemeBackground(aTheme, aHdc, aPart, aState,
aWidgetRect, aClipRect);
}
HGDIOBJ hObj = GetCurrentObject(aHdc, OBJ_BITMAP);
BITMAP bitmap;
POINT vpOrg;
if (hObj && GetObject(hObj, sizeof(bitmap), &bitmap) &&
GetViewportOrgEx(aHdc, &vpOrg)) {
RECT newWRect(*aWidgetRect);
newWRect.left = bitmap.bmWidth - (aWidgetRect->right + 2*vpOrg.x);
newWRect.right = bitmap.bmWidth - (aWidgetRect->left + 2*vpOrg.x);
RECT newCRect;
RECT *newCRectPtr = NULL;
if (aClipRect) {
newCRect.top = aClipRect->top;
newCRect.bottom = aClipRect->bottom;
newCRect.left = bitmap.bmWidth - (aClipRect->right + 2*vpOrg.x);
newCRect.right = bitmap.bmWidth - (aClipRect->left + 2*vpOrg.x);
newCRectPtr = &newCRect;
}
SetLayout(aHdc, LAYOUT_RTL);
HRESULT hr = DrawThemeBackground(aTheme, aHdc, aPart, aState, &newWRect,
newCRectPtr);
SetLayout(aHdc, 0);
if (SUCCEEDED(hr)) {
return hr;
}
}
return DrawThemeBackground(aTheme, aHdc, aPart, aState,
aWidgetRect, aClipRect);
}
/*
* Caption button padding data - 'hot' button padding.
* These areas are considered hot, in that they activate
* a button when hovered or clicked. The button graphic
* is drawn inside the padding border. Unrecognized themes
* are treated as their recognized counterparts for now.
* left top right bottom
* classic min 1 2 0 1
* classic max 0 2 1 1
* classic close 1 2 2 1
*
* aero basic min 1 2 0 2
* aero basic max 0 2 1 2
* aero basic close 1 2 1 2
*
* xp theme min 0 2 0 2
* xp theme max 0 2 1 2
* xp theme close 1 2 2 2
*
* 'cold' button padding - generic button padding, should
* be handled in css.
* left top right bottom
* classic min 0 0 0 0
* classic max 0 0 0 0
* classic close 0 0 0 0
*
* aero basic min 0 0 1 0
* aero basic max 1 0 0 0
* aero basic close 0 0 0 0
*
* xp theme min 0 0 1 0
* xp theme max 1 0 0 0
* xp theme close 0 0 0 0
*/
enum CaptionDesktopTheme {
CAPTION_CLASSIC = 0,
CAPTION_BASIC,
CAPTION_XPTHEME,
};
enum CaptionButton {
CAPTIONBUTTON_MINIMIZE = 0,
CAPTIONBUTTON_RESTORE,
CAPTIONBUTTON_CLOSE,
};
struct CaptionButtonPadding {
RECT hotPadding[3];
};
// RECT: left, top, right, bottom
static CaptionButtonPadding buttonData[3] = {
{
{ { 1, 2, 0, 1 }, { 0, 2, 1, 1 }, { 1, 2, 2, 1 } }
},
{
{ { 1, 2, 0, 2 }, { 0, 2, 1, 2 }, { 1, 2, 2, 2 } }
},
{
{ { 0, 2, 0, 2 }, { 0, 2, 1, 2 }, { 1, 2, 2, 2 } }
}
};
// Adds "hot" caption button padding to minimum widget size.
static void
AddPaddingRect(nsIntSize* aSize, CaptionButton button) {
if (!aSize)
return;
RECT offset;
if (!IsAppThemed())
offset = buttonData[CAPTION_CLASSIC].hotPadding[button];
else if (WinUtils::GetWindowsVersion() < WinUtils::VISTA_VERSION)
offset = buttonData[CAPTION_XPTHEME].hotPadding[button];
else
offset = buttonData[CAPTION_BASIC].hotPadding[button];
aSize->width += offset.left + offset.right;
aSize->height += offset.top + offset.bottom;
}
// If we've added padding to the minimum widget size, offset
// the area we draw into to compensate.
static void
OffsetBackgroundRect(RECT& rect, CaptionButton button) {
RECT offset;
if (!IsAppThemed())
offset = buttonData[CAPTION_CLASSIC].hotPadding[button];
else if (WinUtils::GetWindowsVersion() < WinUtils::VISTA_VERSION)
offset = buttonData[CAPTION_XPTHEME].hotPadding[button];
else
offset = buttonData[CAPTION_BASIC].hotPadding[button];
rect.left += offset.left;
rect.top += offset.top;
rect.right -= offset.right;
rect.bottom -= offset.bottom;
}
/*
* Notes on progress track and meter part constants:
* xp and up:
* PP_BAR(_VERT) - base progress track
* PP_TRANSPARENTBAR(_VERT) - transparent progress track. this only works if
* the underlying surface supports alpha. otherwise
* theme lib's DrawThemeBackground falls back on
* opaque PP_BAR. we currently don't use this.
* PP_CHUNK(_VERT) - xp progress meter. this does not draw an xp style
* progress w/chunks, it draws fill using the chunk
* graphic.
* vista and up:
* PP_FILL(_VERT) - progress meter. these have four states/colors.
* PP_PULSEOVERLAY(_VERT) - white reflection - an overlay, not sure what this
* is used for.
* PP_MOVEOVERLAY(_VERT) - green pulse - the pulse effect overlay on
* determined progress bars. we also use this for
* indeterminate chunk.
*
* Notes on state constants:
* PBBS_NORMAL - green progress
* PBBVS_PARTIAL/PBFVS_ERROR - red error progress
* PBFS_PAUSED - yellow paused progress
*
* There is no common controls style indeterminate part on vista and up.
*/
/*
* Progress bar related constants. These values are found by experimenting and
* comparing against native widgets used by the system. They are very unlikely
* exact but try to not be too wrong.
*/
// The amount of time we animate progress meters parts across the frame.
static const double kProgressDeterminateTimeSpan = 3.0;
static const double kProgressIndeterminateTimeSpan = 5.0;
// The width of the overlay used to animate the horizontal progress bar (Vista and later).
static const int32_t kProgressHorizontalVistaOverlaySize = 120;
// The width of the overlay used for the horizontal indeterminate progress bars on XP.
static const int32_t kProgressHorizontalXPOverlaySize = 55;
// The height of the overlay used to animate the vertical progress bar (Vista and later).
static const int32_t kProgressVerticalOverlaySize = 45;
// The height of the overlay used for the vertical indeterminate progress bar (Vista and later).
static const int32_t kProgressVerticalIndeterminateOverlaySize = 60;
// The width of the overlay used to animate the indeterminate progress bar (Windows Classic).
static const int32_t kProgressClassicOverlaySize = 40;
/*
* GetProgressOverlayStyle - returns the proper overlay part for themed
* progress bars based on os and orientation.
*/
static int32_t
GetProgressOverlayStyle(bool aIsVertical)
{
if (aIsVertical) {
if (WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION) {
return PP_MOVEOVERLAYVERT;
}
return PP_CHUNKVERT;
} else {
if (WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION) {
return PP_MOVEOVERLAY;
}
return PP_CHUNK;
}
}
/*
* GetProgressOverlaySize - returns the minimum width or height for themed
* progress bar overlays. This includes the width of indeterminate chunks
* and vista pulse overlays.
*/
static int32_t
GetProgressOverlaySize(bool aIsVertical, bool aIsIndeterminate)
{
if (WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION) {
if (aIsVertical) {
return aIsIndeterminate ? kProgressVerticalIndeterminateOverlaySize
: kProgressVerticalOverlaySize;
}
return kProgressHorizontalVistaOverlaySize;
}
return kProgressHorizontalXPOverlaySize;
}
/*
* IsProgressMeterFilled - Determines if a progress meter is at 100% fill based
* on a comparison of the current value and maximum.
*/
static bool
IsProgressMeterFilled(nsIFrame* aFrame)
{
NS_ENSURE_TRUE(aFrame, false);
nsIFrame* parentFrame = aFrame->GetParent();
NS_ENSURE_TRUE(parentFrame, false);
return nsNativeTheme::GetProgressValue(parentFrame) ==
nsNativeTheme::GetProgressMaxValue(parentFrame);
}
/*
* CalculateProgressOverlayRect - returns the padded overlay animation rect
* used in rendering progress bars. Resulting rects are used in rendering
* vista+ pulse overlays and indeterminate progress meters. Graphics should
* be rendered at the origin.
*/
RECT
nsNativeThemeWin::CalculateProgressOverlayRect(nsIFrame* aFrame,
RECT* aWidgetRect,
bool aIsVertical,
bool aIsIndeterminate,
bool aIsClassic)
{
NS_ASSERTION(aFrame, "bad frame pointer");
NS_ASSERTION(aWidgetRect, "bad rect pointer");
int32_t frameSize = aIsVertical ? aWidgetRect->bottom - aWidgetRect->top
: aWidgetRect->right - aWidgetRect->left;
// Recycle a set of progress pulse timers - these timers control the position
// of all progress overlays and indeterminate chunks that get rendered.
double span = aIsIndeterminate ? kProgressIndeterminateTimeSpan
: kProgressDeterminateTimeSpan;
TimeDuration period;
if (!aIsIndeterminate) {
if (TimeStamp::Now() > (mProgressDeterminateTimeStamp +
TimeDuration::FromSeconds(span))) {
mProgressDeterminateTimeStamp = TimeStamp::Now();
}
period = TimeStamp::Now() - mProgressDeterminateTimeStamp;
} else {
if (TimeStamp::Now() > (mProgressIndeterminateTimeStamp +
TimeDuration::FromSeconds(span))) {
mProgressIndeterminateTimeStamp = TimeStamp::Now();
}
period = TimeStamp::Now() - mProgressIndeterminateTimeStamp;
}
double percent = period / TimeDuration::FromSeconds(span);
if (!aIsVertical && IsFrameRTL(aFrame))
percent = 1 - percent;
RECT overlayRect = *aWidgetRect;
int32_t overlaySize;
if (!aIsClassic) {
overlaySize = GetProgressOverlaySize(aIsVertical, aIsIndeterminate);
} else {
overlaySize = kProgressClassicOverlaySize;
}
// Calculate a bounds that is larger than the meters frame such that the
// overlay starts and ends completely off the edge of the frame:
// [overlay][frame][overlay]
// This also yields a nice delay on rotation. Use overlaySize as the minimum
// size for [overlay] based on the graphics dims. If [frame] is larger, use
// the frame size instead.
int trackWidth = frameSize > overlaySize ? frameSize : overlaySize;
if (!aIsVertical) {
int xPos = aWidgetRect->left - trackWidth;
xPos += (int)ceil(((double)(trackWidth*2) * percent));
overlayRect.left = xPos;
overlayRect.right = xPos + overlaySize;
} else {
int yPos = aWidgetRect->bottom + trackWidth;
yPos -= (int)ceil(((double)(trackWidth*2) * percent));
overlayRect.bottom = yPos;
overlayRect.top = yPos - overlaySize;
}
return overlayRect;
}
/*
* DrawChunkProgressMeter - renders an xp style chunked progress meter. Called
* by DrawProgressMeter.
*
* @param aTheme progress theme handle
* @param aHdc hdc returned by gfxWindowsNativeDrawing
* @param aPart the PP_X progress part
* @param aState the theme state
* @param aFrame the elements frame
* @param aWidgetRect bounding rect for the widget
* @param aClipRect dirty rect that needs drawing.
* @param aAppUnits app units per device pixel
* @param aIsIndeterm is an indeterminate progress?
* @param aIsVertical render a vertical progress?
* @param aIsRtl direction is rtl
*/
static void
DrawChunkProgressMeter(HTHEME aTheme, HDC aHdc, int aPart,
int aState, nsIFrame* aFrame, RECT* aWidgetRect,
RECT* aClipRect, gfxFloat aAppUnits, bool aIsIndeterm,
bool aIsVertical, bool aIsRtl)
{
NS_ASSERTION(aTheme, "Bad theme.");
NS_ASSERTION(aHdc, "Bad hdc.");
NS_ASSERTION(aWidgetRect, "Bad rect.");
NS_ASSERTION(aClipRect, "Bad clip rect.");
NS_ASSERTION(aFrame, "Bad frame.");
// For horizontal meters, the theme lib paints the right graphic but doesn't
// paint the chunks, so we do that manually. For vertical meters, the theme
// library draws everything correctly.
if (aIsVertical) {
DrawThemeBackground(aTheme, aHdc, aPart, aState, aWidgetRect, aClipRect);
return;
}
// query for the proper chunk metrics
int chunkSize, spaceSize;
if (FAILED(GetThemeMetric(aTheme, aHdc, aPart, aState,
TMT_PROGRESSCHUNKSIZE, &chunkSize)) ||
FAILED(GetThemeMetric(aTheme, aHdc, aPart, aState,
TMT_PROGRESSSPACESIZE, &spaceSize))) {
DrawThemeBackground(aTheme, aHdc, aPart, aState, aWidgetRect, aClipRect);
return;
}
// render chunks
if (!aIsRtl || aIsIndeterm) {
for (int chunk = aWidgetRect->left; chunk <= aWidgetRect->right;
chunk += (chunkSize+spaceSize)) {
if (!aIsIndeterm && ((chunk + chunkSize) > aWidgetRect->right)) {
// aWidgetRect->right represents the end of the meter. Partial blocks
// don't get rendered with one exception, so exit here if we don't have
// a full chunk to draw.
// The above is true *except* when the meter is at 100% fill, in which
// case Windows renders any remaining partial block. Query the parent
// frame to find out if we're at 100%.
if (!IsProgressMeterFilled(aFrame)) {
break;
}
}
RECT bounds =
{ chunk, aWidgetRect->top, chunk + chunkSize, aWidgetRect->bottom };
DrawThemeBackground(aTheme, aHdc, aPart, aState, &bounds, aClipRect);
}
} else {
// rtl needs to grow in the opposite direction to look right.
for (int chunk = aWidgetRect->right; chunk >= aWidgetRect->left;
chunk -= (chunkSize+spaceSize)) {
if ((chunk - chunkSize) < aWidgetRect->left) {
if (!IsProgressMeterFilled(aFrame)) {
break;
}
}
RECT bounds =
{ chunk - chunkSize, aWidgetRect->top, chunk, aWidgetRect->bottom };
DrawThemeBackground(aTheme, aHdc, aPart, aState, &bounds, aClipRect);
}
}
}
/*
* DrawProgressMeter - render an appropriate progress meter based on progress
* meter style, orientation, and os. Note, this does not render the underlying
* progress track.
*
* @param aFrame the widget frame
* @param aWidgetType type of widget
* @param aTheme progress theme handle
* @param aHdc hdc returned by gfxWindowsNativeDrawing
* @param aPart the PP_X progress part
* @param aState the theme state
* @param aWidgetRect bounding rect for the widget
* @param aClipRect dirty rect that needs drawing.
* @param aAppUnits app units per device pixel
*/
void
nsNativeThemeWin::DrawThemedProgressMeter(nsIFrame* aFrame, int aWidgetType,
HANDLE aTheme, HDC aHdc,
int aPart, int aState,
RECT* aWidgetRect, RECT* aClipRect,
gfxFloat aAppUnits)
{
if (!aFrame || !aTheme || !aHdc)
return;
NS_ASSERTION(aWidgetRect, "bad rect pointer");
NS_ASSERTION(aClipRect, "bad clip rect pointer");
RECT adjWidgetRect, adjClipRect;
adjWidgetRect = *aWidgetRect;
adjClipRect = *aClipRect;
if (WinUtils::GetWindowsVersion() < WinUtils::VISTA_VERSION) {
// Adjust clipping out by one pixel. XP progress meters are inset,
// Vista+ are not.
InflateRect(&adjWidgetRect, 1, 1);
InflateRect(&adjClipRect, 1, 1);
}
nsIFrame* parentFrame = aFrame->GetParent();
if (!parentFrame) {
// We have no parent to work with, just bail.
NS_WARNING("No parent frame for progress rendering. Can't paint.");
return;
}
nsEventStates eventStates = GetContentState(parentFrame, aWidgetType);
bool vertical = IsVerticalProgress(parentFrame) ||
aWidgetType == NS_THEME_PROGRESSBAR_CHUNK_VERTICAL;
bool indeterminate = IsIndeterminateProgress(parentFrame, eventStates);
bool animate = indeterminate;
if (WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION) {
// Vista and up progress meter is fill style, rendered here. We render
// the pulse overlay in the follow up section below.
DrawThemeBackground(aTheme, aHdc, aPart, aState,
&adjWidgetRect, &adjClipRect);
if (!IsProgressMeterFilled(aFrame)) {
animate = true;
}
} else if (!indeterminate) {
// XP progress meters are 'chunk' style.
DrawChunkProgressMeter(aTheme, aHdc, aPart, aState, aFrame,
&adjWidgetRect, &adjClipRect, aAppUnits,
indeterminate, vertical, IsFrameRTL(aFrame));
}
if (animate) {
// Indeterminate rendering
int32_t overlayPart = GetProgressOverlayStyle(vertical);
RECT overlayRect =
CalculateProgressOverlayRect(aFrame, &adjWidgetRect, vertical,
indeterminate, false);
if (WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION) {
DrawThemeBackground(aTheme, aHdc, overlayPart, aState, &overlayRect,
&adjClipRect);
} else {
DrawChunkProgressMeter(aTheme, aHdc, overlayPart, aState, aFrame,
&overlayRect, &adjClipRect, aAppUnits,
indeterminate, vertical, IsFrameRTL(aFrame));
}
if (!QueueAnimatedContentForRefresh(aFrame->GetContent(), 60)) {
NS_WARNING("unable to animate progress widget!");
}
}
}
HANDLE
nsNativeThemeWin::GetTheme(uint8_t aWidgetType)
{
if (WinUtils::GetWindowsVersion() < WinUtils::VISTA_VERSION) {
// On XP or earlier, render dropdowns as textfields;
// doing it the right way works fine with the MS themes,
// but breaks on a lot of custom themes (presumably because MS
// apps do the textfield border business as well).
if (aWidgetType == NS_THEME_DROPDOWN)
aWidgetType = NS_THEME_TEXTFIELD;
}
switch (aWidgetType) {
case NS_THEME_BUTTON:
case NS_THEME_RADIO:
case NS_THEME_CHECKBOX:
case NS_THEME_GROUPBOX:
return nsUXThemeData::GetTheme(eUXButton);
case NS_THEME_TEXTFIELD:
case NS_THEME_TEXTFIELD_MULTILINE:
return nsUXThemeData::GetTheme(eUXEdit);
case NS_THEME_TOOLTIP:
// XP/2K3 should force a classic treatment of tooltips
return WinUtils::GetWindowsVersion() < WinUtils::VISTA_VERSION ?
NULL : nsUXThemeData::GetTheme(eUXTooltip);
case NS_THEME_TOOLBOX:
return nsUXThemeData::GetTheme(eUXRebar);
case NS_THEME_WIN_MEDIA_TOOLBOX:
return nsUXThemeData::GetTheme(eUXMediaRebar);
case NS_THEME_WIN_COMMUNICATIONS_TOOLBOX:
return nsUXThemeData::GetTheme(eUXCommunicationsRebar);
case NS_THEME_WIN_BROWSER_TAB_BAR_TOOLBOX:
return nsUXThemeData::GetTheme(eUXBrowserTabBarRebar);
case NS_THEME_TOOLBAR:
case NS_THEME_TOOLBAR_BUTTON:
case NS_THEME_TOOLBAR_SEPARATOR:
return nsUXThemeData::GetTheme(eUXToolbar);
case NS_THEME_PROGRESSBAR:
case NS_THEME_PROGRESSBAR_VERTICAL:
case NS_THEME_PROGRESSBAR_CHUNK:
case NS_THEME_PROGRESSBAR_CHUNK_VERTICAL:
return nsUXThemeData::GetTheme(eUXProgress);
case NS_THEME_TAB:
case NS_THEME_TAB_PANEL:
case NS_THEME_TAB_PANELS:
return nsUXThemeData::GetTheme(eUXTab);
case NS_THEME_SCROLLBAR:
case NS_THEME_SCROLLBAR_SMALL:
case NS_THEME_SCROLLBAR_TRACK_VERTICAL:
case NS_THEME_SCROLLBAR_TRACK_HORIZONTAL:
case NS_THEME_SCROLLBAR_BUTTON_UP:
case NS_THEME_SCROLLBAR_BUTTON_DOWN:
case NS_THEME_SCROLLBAR_BUTTON_LEFT:
case NS_THEME_SCROLLBAR_BUTTON_RIGHT:
case NS_THEME_SCROLLBAR_THUMB_VERTICAL:
case NS_THEME_SCROLLBAR_THUMB_HORIZONTAL:
return nsUXThemeData::GetTheme(eUXScrollbar);
case NS_THEME_RANGE:
case NS_THEME_RANGE_THUMB:
case NS_THEME_SCALE_HORIZONTAL:
case NS_THEME_SCALE_VERTICAL:
case NS_THEME_SCALE_THUMB_HORIZONTAL:
case NS_THEME_SCALE_THUMB_VERTICAL:
return nsUXThemeData::GetTheme(eUXTrackbar);
case NS_THEME_SPINNER_UP_BUTTON:
case NS_THEME_SPINNER_DOWN_BUTTON:
return nsUXThemeData::GetTheme(eUXSpin);
case NS_THEME_STATUSBAR:
case NS_THEME_STATUSBAR_PANEL:
case NS_THEME_STATUSBAR_RESIZER_PANEL:
case NS_THEME_RESIZER:
return nsUXThemeData::GetTheme(eUXStatus);
case NS_THEME_DROPDOWN:
case NS_THEME_DROPDOWN_BUTTON:
return nsUXThemeData::GetTheme(eUXCombobox);
case NS_THEME_TREEVIEW_HEADER_CELL:
case NS_THEME_TREEVIEW_HEADER_SORTARROW:
return nsUXThemeData::GetTheme(eUXHeader);
case NS_THEME_LISTBOX:
case NS_THEME_LISTBOX_LISTITEM:
case NS_THEME_TREEVIEW:
case NS_THEME_TREEVIEW_TWISTY_OPEN:
case NS_THEME_TREEVIEW_TREEITEM:
return nsUXThemeData::GetTheme(eUXListview);
case NS_THEME_MENUBAR:
case NS_THEME_MENUPOPUP:
case NS_THEME_MENUITEM:
case NS_THEME_CHECKMENUITEM:
case NS_THEME_RADIOMENUITEM:
case NS_THEME_MENUCHECKBOX:
case NS_THEME_MENURADIO:
case NS_THEME_MENUSEPARATOR:
case NS_THEME_MENUARROW:
case NS_THEME_MENUIMAGE:
case NS_THEME_MENUITEMTEXT:
return nsUXThemeData::GetTheme(eUXMenu);
case NS_THEME_WINDOW_TITLEBAR:
case NS_THEME_WINDOW_TITLEBAR_MAXIMIZED:
case NS_THEME_WINDOW_FRAME_LEFT:
case NS_THEME_WINDOW_FRAME_RIGHT:
case NS_THEME_WINDOW_FRAME_BOTTOM:
case NS_THEME_WINDOW_BUTTON_CLOSE:
case NS_THEME_WINDOW_BUTTON_MINIMIZE:
case NS_THEME_WINDOW_BUTTON_MAXIMIZE:
case NS_THEME_WINDOW_BUTTON_RESTORE:
case NS_THEME_WINDOW_BUTTON_BOX:
case NS_THEME_WINDOW_BUTTON_BOX_MAXIMIZED:
case NS_THEME_WIN_GLASS:
case NS_THEME_WIN_BORDERLESS_GLASS:
return nsUXThemeData::GetTheme(eUXWindowFrame);
}
return NULL;
}
int32_t
nsNativeThemeWin::StandardGetState(nsIFrame* aFrame, uint8_t aWidgetType,
bool wantFocused)
{
nsEventStates eventState = GetContentState(aFrame, aWidgetType);
if (eventState.HasAllStates(NS_EVENT_STATE_HOVER | NS_EVENT_STATE_ACTIVE))
return TS_ACTIVE;
if (eventState.HasState(NS_EVENT_STATE_HOVER))
return TS_HOVER;
if (wantFocused && eventState.HasState(NS_EVENT_STATE_FOCUS))
return TS_FOCUSED;
return TS_NORMAL;
}
bool
nsNativeThemeWin::IsMenuActive(nsIFrame *aFrame, uint8_t aWidgetType)
{
nsIContent* content = aFrame->GetContent();
if (content->IsXUL() &&
content->NodeInfo()->Equals(nsGkAtoms::richlistitem))
return CheckBooleanAttr(aFrame, nsGkAtoms::selected);
return CheckBooleanAttr(aFrame, nsGkAtoms::menuactive);
}
/**
* aPart is filled in with the UXTheme part code. On return, values > 0
* are the actual UXTheme part code; -1 means the widget will be drawn by
* us; 0 means that we should use part code 0, which isn't a real part code
* but elicits some kind of default behaviour from UXTheme when drawing
* (but isThemeBackgroundPartiallyTransparent may not work).
*/
nsresult
nsNativeThemeWin::GetThemePartAndState(nsIFrame* aFrame, uint8_t aWidgetType,
int32_t& aPart, int32_t& aState)
{
if (WinUtils::GetWindowsVersion() < WinUtils::VISTA_VERSION) {
// See GetTheme
if (aWidgetType == NS_THEME_DROPDOWN)
aWidgetType = NS_THEME_TEXTFIELD;
}
switch (aWidgetType) {
case NS_THEME_BUTTON: {
aPart = BP_BUTTON;
if (!aFrame) {
aState = TS_NORMAL;
return NS_OK;
}
nsEventStates eventState = GetContentState(aFrame, aWidgetType);
if (IsDisabled(aFrame, eventState)) {
aState = TS_DISABLED;
return NS_OK;
} else if (IsOpenButton(aFrame) ||
IsCheckedButton(aFrame)) {
aState = TS_ACTIVE;
return NS_OK;
}
aState = StandardGetState(aFrame, aWidgetType, true);
// Check for default dialog buttons. These buttons should always look
// focused.
if (aState == TS_NORMAL && IsDefaultButton(aFrame))
aState = TS_FOCUSED;
return NS_OK;
}
case NS_THEME_CHECKBOX:
case NS_THEME_RADIO: {
bool isCheckbox = (aWidgetType == NS_THEME_CHECKBOX);
aPart = isCheckbox ? BP_CHECKBOX : BP_RADIO;
enum InputState {
UNCHECKED = 0, CHECKED, INDETERMINATE
};
InputState inputState = UNCHECKED;
bool isXULCheckboxRadio = false;
if (!aFrame) {
aState = TS_NORMAL;
} else {
if (GetCheckedOrSelected(aFrame, !isCheckbox)) {
inputState = CHECKED;
} if (isCheckbox && GetIndeterminate(aFrame)) {
inputState = INDETERMINATE;
}
nsEventStates eventState = GetContentState(isXULCheckboxRadio ? aFrame->GetParent()
: aFrame,
aWidgetType);
if (IsDisabled(aFrame, eventState)) {
aState = TS_DISABLED;
} else {
aState = StandardGetState(aFrame, aWidgetType, false);
}
}
// 4 unchecked states, 4 checked states, 4 indeterminate states.
aState += inputState * 4;
return NS_OK;
}
case NS_THEME_GROUPBOX: {
aPart = BP_GROUPBOX;
aState = TS_NORMAL;
// Since we don't support groupbox disabled and GBS_DISABLED looks the
// same as GBS_NORMAL don't bother supporting GBS_DISABLED.
return NS_OK;
}
case NS_THEME_TEXTFIELD:
case NS_THEME_TEXTFIELD_MULTILINE: {
nsEventStates eventState = GetContentState(aFrame, aWidgetType);
if (WinUtils::GetWindowsVersion() >= WinUtils::VISTA_VERSION) {
/* Note: the NOSCROLL type has a rounded corner in each
* corner. The more specific HSCROLL, VSCROLL, HVSCROLL types
* have side and/or top/bottom edges rendered as straight
* horizontal lines with sharp corners to accommodate a
* scrollbar. However, the scrollbar gets rendered on top of
* this for us, so we don't care, and can just use NOSCROLL
* here.
*/
aPart = TFP_EDITBORDER_NOSCROLL;
if (!aFrame) {
aState = TFS_EDITBORDER_NORMAL;
} else if (IsDisabled(aFrame, eventState)) {
aState = TFS_EDITBORDER_DISABLED;
} else if (IsReadOnly(aFrame)) {
/* no special read-only state */
aState = TFS_EDITBORDER_NORMAL;
} else {
nsIContent* content = aFrame->GetContent();
/* XUL textboxes don't get focused themselves, because they have child
* html:input.. but we can check the XUL focused attributes on them
*/
if (content && content->IsXUL() && IsFocused(aFrame))
aState = TFS_EDITBORDER_FOCUSED;
else if (eventState.HasAtLeastOneOfStates(NS_EVENT_STATE_ACTIVE | NS_EVENT_STATE_FOCUS))
aState = TFS_EDITBORDER_FOCUSED;
else if (eventState.HasState(NS_EVENT_STATE_HOVER))
aState = TFS_EDITBORDER_HOVER;
else
aState = TFS_EDITBORDER_NORMAL;
}
} else {
aPart = TFP_TEXTFIELD;
if (!aFrame)
aState = TS_NORMAL;
else if (IsDisabled(aFrame, eventState))
aState = TS_DISABLED;
else if (IsReadOnly(aFrame))
aState = TFS_READONLY;
else
aState = StandardGetState(aFrame, aWidgetType, true);
}
return NS_OK;
}
case NS_THEME_TOOLTIP: {
aPart = TTP_STANDARD;
aState = TS_NORMAL;
return NS_OK;
}
case NS_THEME_PROGRESSBAR:
case NS_THEME_PROGRESSBAR_VERTICAL: {
// Note IsVerticalProgress only tests for orient css attrribute,
// NS_THEME_PROGRESSBAR_VERTICAL is dedicated to -moz-appearance:
// progressbar-vertical.