forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnative_theme_win.cc
1893 lines (1740 loc) · 61.5 KB
/
native_theme_win.cc
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/native_theme/native_theme_win.h"
#include <windows.h>
#include <stddef.h>
#include <uxtheme.h>
#include <vsstyle.h>
#include <vssym32.h>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/win/scoped_gdi_object.h"
#include "base/win/scoped_hdc.h"
#include "base/win/scoped_select_object.h"
#include "base/win/win_util.h"
#include "cc/paint/paint_canvas.h"
#include "cc/paint/paint_flags.h"
#include "skia/ext/platform_canvas.h"
#include "skia/ext/skia_utils_win.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkColorPriv.h"
#include "third_party/skia/include/core/SkPath.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkShader.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "ui/base/ui_base_switches.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/gdi_util.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/skia_util.h"
#include "ui/native_theme/common_theme.h"
// This was removed from Winvers.h but is still used.
#if !defined(COLOR_MENUHIGHLIGHT)
#define COLOR_MENUHIGHLIGHT 29
#endif
namespace {
// Windows system color IDs cached and updated by the native theme.
const int kSystemColors[] = {
COLOR_3DFACE,
COLOR_BTNFACE,
COLOR_BTNTEXT,
COLOR_GRAYTEXT,
COLOR_HIGHLIGHT,
COLOR_HIGHLIGHTTEXT,
COLOR_HOTLIGHT,
COLOR_MENUHIGHLIGHT,
COLOR_SCROLLBAR,
COLOR_WINDOW,
COLOR_WINDOWTEXT,
};
void SetCheckerboardShader(SkPaint* paint, const RECT& align_rect) {
// Create a 2x2 checkerboard pattern using the 3D face and highlight colors.
const SkColor face = color_utils::GetSysSkColor(COLOR_3DFACE);
const SkColor highlight = color_utils::GetSysSkColor(COLOR_3DHILIGHT);
SkColor buffer[] = { face, highlight, highlight, face };
// Confusing bit: we first create a temporary bitmap with our desired pattern,
// then copy it to another bitmap. The temporary bitmap doesn't take
// ownership of the pixel data, and so will point to garbage when this
// function returns. The copy will copy the pixel data into a place owned by
// the bitmap, which is in turn owned by the shader, etc., so it will live
// until we're done using it.
SkImageInfo info = SkImageInfo::MakeN32Premul(2, 2);
SkBitmap temp_bitmap;
temp_bitmap.installPixels(info, buffer, info.minRowBytes());
SkBitmap bitmap;
if (bitmap.tryAllocPixels(info))
temp_bitmap.readPixels(info, bitmap.getPixels(), bitmap.rowBytes(), 0, 0);
// Align the pattern with the upper corner of |align_rect|.
SkMatrix local_matrix;
local_matrix.setTranslate(SkIntToScalar(align_rect.left),
SkIntToScalar(align_rect.top));
paint->setShader(
SkShader::MakeBitmapShader(bitmap, SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode, &local_matrix));
}
// <-a->
// [ ***** ]
// ____ | |
// <-a-> <------b----->
// a: object_width
// b: frame_width
// *: animating object
//
// - the animation goes from "[" to "]" repeatedly.
// - the animation offset is at first "|"
//
int ComputeAnimationProgress(int frame_width,
int object_width,
int pixels_per_second,
double animated_seconds) {
int animation_width = frame_width + object_width;
double interval = static_cast<double>(animation_width) / pixels_per_second;
double ratio = fmod(animated_seconds, interval) / interval;
return static_cast<int>(animation_width * ratio) - object_width;
}
// Custom scoped object for storing DC and a bitmap that was selected into it,
// and making sure that they are deleted in the right order.
class ScopedCreateDCWithBitmap {
public:
explicit ScopedCreateDCWithBitmap(base::win::ScopedCreateDC::Handle hdc)
: dc_(hdc) {}
~ScopedCreateDCWithBitmap() {
// Delete DC before the bitmap, since objects should not be deleted while
// selected into a DC.
dc_.Close();
}
bool IsValid() const { return dc_.IsValid(); }
base::win::ScopedCreateDC::Handle Get() const { return dc_.Get(); }
// Selects |handle| to bitmap into DC. Returns false if handle is not valid.
bool SelectBitmap(base::win::ScopedBitmap::element_type handle) {
bitmap_.reset(handle);
if (!bitmap_.is_valid())
return false;
SelectObject(dc_.Get(), bitmap_.get());
return true;
}
private:
base::win::ScopedCreateDC dc_;
base::win::ScopedBitmap bitmap_;
DISALLOW_COPY_AND_ASSIGN(ScopedCreateDCWithBitmap);
};
} // namespace
namespace ui {
NativeTheme* NativeTheme::GetInstanceForNativeUi() {
return NativeThemeWin::instance();
}
// static
void NativeThemeWin::CloseHandles() {
instance()->CloseHandlesInternal();
}
// static
NativeThemeWin* NativeThemeWin::instance() {
static base::NoDestructor<NativeThemeWin> s_native_theme;
return s_native_theme.get();
}
gfx::Size NativeThemeWin::GetPartSize(Part part,
State state,
const ExtraParams& extra) const {
// The GetThemePartSize call below returns the default size without
// accounting for user customization (crbug/218291).
switch (part) {
case kScrollbarDownArrow:
case kScrollbarLeftArrow:
case kScrollbarRightArrow:
case kScrollbarUpArrow:
case kScrollbarHorizontalThumb:
case kScrollbarVerticalThumb:
case kScrollbarHorizontalTrack:
case kScrollbarVerticalTrack: {
int size = display::win::ScreenWin::GetSystemMetricsInDIP(SM_CXVSCROLL);
if (size == 0)
size = 17;
return gfx::Size(size, size);
}
default:
break;
}
int part_id = GetWindowsPart(part, state, extra);
int state_id = GetWindowsState(part, state, extra);
base::win::ScopedGetDC screen_dc(NULL);
SIZE size;
if (SUCCEEDED(GetThemePartSize(GetThemeName(part), screen_dc, part_id,
state_id, NULL, TS_TRUE, &size)))
return gfx::Size(size.cx, size.cy);
// TODO(rogerta): For now, we need to support radio buttons and checkboxes
// when theming is not enabled. Support for other parts can be added
// if/when needed.
return (part == kCheckbox || part == kRadio) ?
gfx::Size(13, 13) : gfx::Size();
}
void NativeThemeWin::Paint(cc::PaintCanvas* canvas,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const {
if (rect.IsEmpty())
return;
switch (part) {
case kMenuPopupGutter:
PaintMenuGutter(canvas, rect);
return;
case kMenuPopupSeparator:
PaintMenuSeparator(canvas, extra.menu_separator);
return;
case kMenuPopupBackground:
PaintMenuBackground(canvas, rect);
return;
case kMenuItemBackground:
CommonThemePaintMenuItemBackground(this, canvas, state, rect,
extra.menu_item);
return;
default:
PaintIndirect(canvas, part, state, rect, extra);
return;
}
}
NativeThemeWin::NativeThemeWin()
: draw_theme_(NULL),
draw_theme_ex_(NULL),
get_theme_content_rect_(NULL),
get_theme_part_size_(NULL),
open_theme_(NULL),
close_theme_(NULL),
theme_dll_(LoadLibrary(L"uxtheme.dll")),
color_change_listener_(this),
is_using_high_contrast_(false),
is_using_high_contrast_valid_(false) {
if (theme_dll_) {
draw_theme_ = reinterpret_cast<DrawThemeBackgroundPtr>(
GetProcAddress(theme_dll_, "DrawThemeBackground"));
draw_theme_ex_ = reinterpret_cast<DrawThemeBackgroundExPtr>(
GetProcAddress(theme_dll_, "DrawThemeBackgroundEx"));
get_theme_content_rect_ = reinterpret_cast<GetThemeContentRectPtr>(
GetProcAddress(theme_dll_, "GetThemeBackgroundContentRect"));
get_theme_part_size_ = reinterpret_cast<GetThemePartSizePtr>(
GetProcAddress(theme_dll_, "GetThemePartSize"));
open_theme_ = reinterpret_cast<OpenThemeDataPtr>(
GetProcAddress(theme_dll_, "OpenThemeData"));
close_theme_ = reinterpret_cast<CloseThemeDataPtr>(
GetProcAddress(theme_dll_, "CloseThemeData"));
}
memset(theme_handles_, 0, sizeof(theme_handles_));
// Initialize the cached system colors.
UpdateSystemColors();
}
NativeThemeWin::~NativeThemeWin() {
if (theme_dll_) {
// TODO(https://crbug.com/787692): Calling CloseHandles() here breaks
// certain tests and the reliability bots.
// CloseHandles();
FreeLibrary(theme_dll_);
}
}
bool NativeThemeWin::IsUsingHighContrastThemeInternal() const {
if (is_using_high_contrast_valid_)
return is_using_high_contrast_;
HIGHCONTRAST result;
result.cbSize = sizeof(HIGHCONTRAST);
is_using_high_contrast_ =
SystemParametersInfo(SPI_GETHIGHCONTRAST, result.cbSize, &result, 0) &&
(result.dwFlags & HCF_HIGHCONTRASTON) == HCF_HIGHCONTRASTON;
is_using_high_contrast_valid_ = true;
return is_using_high_contrast_;
}
void NativeThemeWin::CloseHandlesInternal() {
if (!close_theme_)
return;
for (int i = 0; i < LAST; ++i) {
if (theme_handles_[i]) {
close_theme_(theme_handles_[i]);
theme_handles_[i] = nullptr;
}
}
}
void NativeThemeWin::OnSysColorChange() {
UpdateSystemColors();
is_using_high_contrast_valid_ = false;
NotifyObservers();
}
void NativeThemeWin::UpdateSystemColors() {
for (int kSystemColor : kSystemColors)
system_colors_[kSystemColor] = color_utils::GetSysSkColor(kSystemColor);
}
void NativeThemeWin::PaintMenuSeparator(
cc::PaintCanvas* canvas,
const MenuSeparatorExtraParams& params) const {
const gfx::RectF rect(*params.paint_rect);
gfx::PointF start = rect.CenterPoint();
gfx::PointF end = start;
if (params.type == ui::VERTICAL_SEPARATOR) {
start.set_y(rect.y());
end.set_y(rect.bottom());
} else {
start.set_x(rect.x());
end.set_x(rect.right());
}
cc::PaintFlags flags;
flags.setColor(GetSystemColor(NativeTheme::kColorId_MenuSeparatorColor));
canvas->drawLine(start.x(), start.y(), end.x(), end.y(), flags);
}
void NativeThemeWin::PaintMenuGutter(cc::PaintCanvas* canvas,
const gfx::Rect& rect) const {
cc::PaintFlags flags;
flags.setColor(GetSystemColor(NativeTheme::kColorId_MenuSeparatorColor));
int position_x = rect.x() + rect.width() / 2;
canvas->drawLine(position_x, rect.y(), position_x, rect.bottom(), flags);
}
void NativeThemeWin::PaintMenuBackground(cc::PaintCanvas* canvas,
const gfx::Rect& rect) const {
cc::PaintFlags flags;
flags.setColor(GetSystemColor(NativeTheme::kColorId_MenuBackgroundColor));
canvas->drawRect(gfx::RectToSkRect(rect), flags);
}
void NativeThemeWin::PaintDirect(SkCanvas* destination_canvas,
HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const {
switch (part) {
case kCheckbox:
PaintCheckbox(hdc, part, state, rect, extra.button);
return;
case kInnerSpinButton:
PaintSpinButton(hdc, part, state, rect, extra.inner_spin);
return;
case kMenuList:
PaintMenuList(hdc, state, rect, extra.menu_list);
return;
case kMenuCheck:
PaintMenuCheck(hdc, state, rect, extra.menu_check);
return;
case kMenuCheckBackground:
PaintMenuCheckBackground(hdc, state, rect);
return;
case kMenuPopupArrow:
PaintMenuArrow(hdc, state, rect, extra.menu_arrow);
return;
case kProgressBar:
PaintProgressBar(hdc, rect, extra.progress_bar);
return;
case kPushButton:
PaintPushButton(hdc, part, state, rect, extra.button);
return;
case kRadio:
PaintRadioButton(hdc, part, state, rect, extra.button);
return;
case kScrollbarDownArrow:
case kScrollbarUpArrow:
case kScrollbarLeftArrow:
case kScrollbarRightArrow:
PaintScrollbarArrow(hdc, part, state, rect, extra.scrollbar_arrow);
return;
case kScrollbarHorizontalThumb:
case kScrollbarVerticalThumb:
case kScrollbarHorizontalGripper:
case kScrollbarVerticalGripper:
PaintScrollbarThumb(hdc, part, state, rect, extra.scrollbar_thumb);
return;
case kScrollbarHorizontalTrack:
case kScrollbarVerticalTrack:
PaintScrollbarTrack(destination_canvas, hdc, part, state, rect,
extra.scrollbar_track);
return;
case kScrollbarCorner:
destination_canvas->drawColor(SK_ColorWHITE, SkBlendMode::kSrc);
return;
case kTabPanelBackground:
PaintTabPanelBackground(hdc, rect);
return;
case kTextField:
PaintTextField(hdc, part, state, rect, extra.text_field);
return;
case kTrackbarThumb:
case kTrackbarTrack:
PaintTrackbar(destination_canvas, hdc, part, state, rect, extra.trackbar);
return;
case kWindowResizeGripper:
PaintWindowResizeGripper(hdc, rect);
return;
case kMenuPopupBackground:
case kMenuPopupGutter:
case kMenuPopupSeparator:
case kMenuItemBackground:
case kSliderTrack:
case kSliderThumb:
case kMaxPart:
NOTREACHED();
}
}
SkColor NativeThemeWin::GetSystemColor(ColorId color_id) const {
// TODO: Obtain the correct colors for these using GetSysColor.
// Button:
constexpr SkColor kButtonHoverColor = SkColorSetRGB(6, 45, 117);
constexpr SkColor kProminentButtonColorInvert = gfx::kGoogleBlue300;
// MenuItem:
constexpr SkColor kMenuSchemeHighlightBackgroundColorInvert =
SkColorSetRGB(0x30, 0x30, 0x30);
// Label:
constexpr SkColor kLabelTextSelectionBackgroundFocusedColor =
gfx::kGoogleBlue700;
switch (color_id) {
// Windows
case kColorId_WindowBackground:
return system_colors_[COLOR_WINDOW];
// Dialogs
case kColorId_DialogBackground:
case kColorId_BubbleBackground:
break;
// FocusableBorder
case kColorId_FocusedBorderColor:
case kColorId_UnfocusedBorderColor:
break;
// Button
case kColorId_ButtonEnabledColor:
return system_colors_[COLOR_BTNTEXT];
case kColorId_ButtonHoverColor:
return kButtonHoverColor;
// Label
case kColorId_LabelEnabledColor:
return system_colors_[COLOR_BTNTEXT];
case kColorId_LabelDisabledColor:
return system_colors_[COLOR_GRAYTEXT];
case kColorId_LabelTextSelectionColor:
return system_colors_[COLOR_HIGHLIGHTTEXT];
case kColorId_LabelTextSelectionBackgroundFocused:
return kLabelTextSelectionBackgroundFocusedColor;
// Textfield
case kColorId_TextfieldDefaultColor:
return system_colors_[COLOR_WINDOWTEXT];
case kColorId_TextfieldDefaultBackground:
return system_colors_[COLOR_WINDOW];
case kColorId_TextfieldReadOnlyColor:
return system_colors_[COLOR_GRAYTEXT];
case kColorId_TextfieldReadOnlyBackground:
return system_colors_[COLOR_3DFACE];
case kColorId_TextfieldSelectionColor:
return system_colors_[COLOR_HIGHLIGHTTEXT];
case kColorId_TextfieldSelectionBackgroundFocused:
return system_colors_[COLOR_HIGHLIGHT];
// Tooltip
case kColorId_TooltipBackground:
case kColorId_TooltipText:
NOTREACHED();
return gfx::kPlaceholderColor;
// Tree
// NOTE: these aren't right for all themes, but as close as I could get.
case kColorId_TreeBackground:
return system_colors_[COLOR_WINDOW];
case kColorId_TreeText:
return system_colors_[COLOR_WINDOWTEXT];
case kColorId_TreeSelectedText:
return system_colors_[COLOR_HIGHLIGHTTEXT];
case kColorId_TreeSelectedTextUnfocused:
return system_colors_[COLOR_BTNTEXT];
case kColorId_TreeSelectionBackgroundFocused:
return system_colors_[COLOR_HIGHLIGHT];
case kColorId_TreeSelectionBackgroundUnfocused:
return system_colors_[UsesHighContrastColors() ? COLOR_MENUHIGHLIGHT
: COLOR_BTNFACE];
// Table
case kColorId_TableBackground:
return system_colors_[COLOR_WINDOW];
case kColorId_TableText:
return system_colors_[COLOR_WINDOWTEXT];
case kColorId_TableSelectedText:
return system_colors_[COLOR_HIGHLIGHTTEXT];
case kColorId_TableSelectedTextUnfocused:
return system_colors_[COLOR_BTNTEXT];
case kColorId_TableSelectionBackgroundFocused:
return system_colors_[COLOR_HIGHLIGHT];
case kColorId_TableSelectionBackgroundUnfocused:
return system_colors_[UsesHighContrastColors() ? COLOR_MENUHIGHLIGHT
: COLOR_BTNFACE];
case kColorId_TableGroupingIndicatorColor:
return system_colors_[COLOR_GRAYTEXT];
// Results Tables
case kColorId_ResultsTableNormalBackground:
return system_colors_[COLOR_WINDOW];
case kColorId_ResultsTableHoveredBackground:
return color_utils::AlphaBlend(system_colors_[COLOR_HIGHLIGHT],
system_colors_[COLOR_WINDOW], 0x40);
case kColorId_ResultsTableNormalText:
return system_colors_[COLOR_WINDOWTEXT];
case kColorId_ResultsTableDimmedText:
return color_utils::AlphaBlend(system_colors_[COLOR_WINDOWTEXT],
system_colors_[COLOR_WINDOW], 0x80);
default:
break;
}
if (color_utils::IsInvertedColorScheme()) {
switch (color_id) {
case NativeTheme::kColorId_FocusedMenuItemBackgroundColor:
return kMenuSchemeHighlightBackgroundColorInvert;
case NativeTheme::kColorId_ProminentButtonColor:
return kProminentButtonColorInvert;
default:
return color_utils::InvertColor(GetAuraColor(color_id, this));
}
}
return GetAuraColor(color_id, this);
}
bool NativeThemeWin::SupportsNinePatch(Part part) const {
// The only nine-patch resources currently supported (overlay scrollbar) are
// painted by NativeThemeAura on Windows.
return false;
}
gfx::Size NativeThemeWin::GetNinePatchCanvasSize(Part part) const {
NOTREACHED() << "NativeThemeWin doesn't support nine-patch resources.";
return gfx::Size();
}
gfx::Rect NativeThemeWin::GetNinePatchAperture(Part part) const {
NOTREACHED() << "NativeThemeWin doesn't support nine-patch resources.";
return gfx::Rect();
}
bool NativeThemeWin::UsesHighContrastColors() const {
bool force_enabled = base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForceHighContrast);
return force_enabled || IsUsingHighContrastThemeInternal();
}
void NativeThemeWin::PaintIndirect(cc::PaintCanvas* destination_canvas,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const {
// TODO(asvitkine): This path is pretty inefficient - for each paint operation
// it creates a new offscreen bitmap Skia canvas. This can be sped up by doing
// it only once per part/state and keeping a cache of the resulting bitmaps.
//
// TODO(enne): This could also potentially be sped up for software raster
// by moving these draw ops into PaintRecord itself and then moving the
// PaintDirect code to be part of the raster for PaintRecord.
// If this process doesn't have access to GDI, we'd need to use shared memory
// segment instead but that is not supported right now.
if (!base::win::IsUser32AndGdi32Available())
return;
ScopedCreateDCWithBitmap offscreen_hdc(CreateCompatibleDC(nullptr));
if (!offscreen_hdc.IsValid())
return;
skia::InitializeDC(offscreen_hdc.Get());
HRGN clip = CreateRectRgn(0, 0, rect.width(), rect.height());
if ((SelectClipRgn(offscreen_hdc.Get(), clip) == ERROR) ||
!DeleteObject(clip)) {
return;
}
if (!offscreen_hdc.SelectBitmap(skia::CreateHBitmap(
rect.width(), rect.height(), false, nullptr, nullptr))) {
return;
}
// Will be NULL if lower-level Windows calls fail, or if the backing
// allocated is 0 pixels in size (which should never happen according to
// Windows documentation).
sk_sp<SkSurface> offscreen_surface =
skia::MapPlatformSurface(offscreen_hdc.Get());
if (!offscreen_surface)
return;
SkCanvas* offscreen_canvas = offscreen_surface->getCanvas();
DCHECK(offscreen_canvas);
// Some of the Windows theme drawing operations do not write correct alpha
// values for fully-opaque pixels; instead the pixels get alpha 0. This is
// especially a problem on Windows XP or when using the Classic theme.
//
// To work-around this, mark all pixels with a placeholder value, to detect
// which pixels get touched by the paint operation. After paint, set any
// pixels that have alpha 0 to opaque and placeholders to fully-transparent.
constexpr SkColor placeholder = SkColorSetARGB(1, 0, 0, 0);
offscreen_canvas->clear(placeholder);
// Offset destination rects to have origin (0,0).
gfx::Rect adjusted_rect(rect.size());
ExtraParams adjusted_extra(extra);
switch (part) {
case kProgressBar:
adjusted_extra.progress_bar.value_rect_x = 0;
adjusted_extra.progress_bar.value_rect_y = 0;
break;
case kScrollbarHorizontalTrack:
case kScrollbarVerticalTrack:
adjusted_extra.scrollbar_track.track_x = 0;
adjusted_extra.scrollbar_track.track_y = 0;
break;
default:
break;
}
// Draw the theme controls using existing HDC-drawing code.
PaintDirect(offscreen_canvas, offscreen_hdc.Get(), part, state,
adjusted_rect, adjusted_extra);
SkBitmap offscreen_bitmap = skia::MapPlatformBitmap(offscreen_hdc.Get());
// Post-process the pixels to fix up the alpha values (see big comment above).
const SkPMColor placeholder_value = SkPreMultiplyColor(placeholder);
const int pixel_count = rect.width() * rect.height();
SkPMColor* pixels = offscreen_bitmap.getAddr32(0, 0);
for (int i = 0; i < pixel_count; i++) {
if (pixels[i] == placeholder_value) {
// Pixel wasn't touched - make it fully transparent.
pixels[i] = SkPackARGB32(0, 0, 0, 0);
} else if (SkGetPackedA32(pixels[i]) == 0) {
// Pixel was touched but has incorrect alpha of 0, make it fully opaque.
pixels[i] = SkPackARGB32(0xFF,
SkGetPackedR32(pixels[i]),
SkGetPackedG32(pixels[i]),
SkGetPackedB32(pixels[i]));
}
}
destination_canvas->drawImage(
cc::PaintImage::CreateFromBitmap(std::move(offscreen_bitmap)), rect.x(),
rect.y());
}
HRESULT NativeThemeWin::GetThemePartSize(ThemeName theme_name,
HDC hdc,
int part_id,
int state_id,
RECT* rect,
int ts,
SIZE* size) const {
HANDLE handle = GetThemeHandle(theme_name);
return (handle && get_theme_part_size_) ?
get_theme_part_size_(handle, hdc, part_id, state_id, rect, ts, size) :
E_NOTIMPL;
}
HRESULT NativeThemeWin::PaintButton(HDC hdc,
State state,
const ButtonExtraParams& extra,
int part_id,
int state_id,
RECT* rect) const {
HANDLE handle = GetThemeHandle(BUTTON);
if (handle && draw_theme_)
return draw_theme_(handle, hdc, part_id, state_id, rect, NULL);
// Adjust classic_state based on part, state, and extras.
int classic_state = extra.classic_state;
switch (part_id) {
case BP_CHECKBOX:
classic_state |= DFCS_BUTTONCHECK;
break;
case BP_RADIOBUTTON:
classic_state |= DFCS_BUTTONRADIO;
break;
case BP_PUSHBUTTON:
classic_state |= DFCS_BUTTONPUSH;
break;
default:
NOTREACHED();
break;
}
switch (state) {
case kDisabled:
classic_state |= DFCS_INACTIVE;
break;
case kHovered:
case kNormal:
break;
case kPressed:
classic_state |= DFCS_PUSHED;
break;
case kNumStates:
NOTREACHED();
break;
}
if (extra.checked)
classic_state |= DFCS_CHECKED;
// Draw it manually.
// All pressed states have both low bits set, and no other states do.
const bool focused = ((state_id & ETS_FOCUSED) == ETS_FOCUSED);
const bool pressed = ((state_id & PBS_PRESSED) == PBS_PRESSED);
if ((BP_PUSHBUTTON == part_id) && (pressed || focused)) {
// BP_PUSHBUTTON has a focus rect drawn around the outer edge, and the
// button itself is shrunk by 1 pixel.
HBRUSH brush = GetSysColorBrush(COLOR_3DDKSHADOW);
if (brush) {
FrameRect(hdc, rect, brush);
InflateRect(rect, -1, -1);
}
}
DrawFrameControl(hdc, rect, DFC_BUTTON, classic_state);
// Draw the focus rectangle (the dotted line box) only on buttons. For radio
// and checkboxes, we let webkit draw the focus rectangle (orange glow).
if ((BP_PUSHBUTTON == part_id) && focused) {
// The focus rect is inside the button. The exact number of pixels depends
// on whether we're in classic mode or using uxtheme.
if (handle && get_theme_content_rect_) {
get_theme_content_rect_(handle, hdc, part_id, state_id, rect, rect);
} else {
InflateRect(rect, -GetSystemMetrics(SM_CXEDGE),
-GetSystemMetrics(SM_CYEDGE));
}
DrawFocusRect(hdc, rect);
}
// Classic theme doesn't support indeterminate checkboxes. We draw
// a recangle inside a checkbox like IE10 does.
if (part_id == BP_CHECKBOX && extra.indeterminate) {
RECT inner_rect = *rect;
// "4 / 13" is same as IE10 in classic theme.
int padding = (inner_rect.right - inner_rect.left) * 4 / 13;
InflateRect(&inner_rect, -padding, -padding);
int color_index = state == kDisabled ? COLOR_GRAYTEXT : COLOR_WINDOWTEXT;
FillRect(hdc, &inner_rect, GetSysColorBrush(color_index));
}
return S_OK;
}
HRESULT NativeThemeWin::PaintMenuArrow(
HDC hdc,
State state,
const gfx::Rect& rect,
const MenuArrowExtraParams& extra) const {
int state_id = MSM_NORMAL;
if (state == kDisabled)
state_id = MSM_DISABLED;
HANDLE handle = GetThemeHandle(MENU);
RECT rect_win = rect.ToRECT();
if (handle && draw_theme_) {
if (extra.pointing_right) {
return draw_theme_(handle, hdc, MENU_POPUPSUBMENU, state_id, &rect_win,
NULL);
}
// There is no way to tell the uxtheme API to draw a left pointing arrow; it
// doesn't have a flag equivalent to DFCS_MENUARROWRIGHT. But they are
// needed for RTL locales on Vista. So use a memory DC and mirror the
// region with GDI's StretchBlt.
gfx::Rect r(rect);
base::win::ScopedCreateDC mem_dc(CreateCompatibleDC(hdc));
base::win::ScopedBitmap mem_bitmap(CreateCompatibleBitmap(hdc, r.width(),
r.height()));
base::win::ScopedSelectObject select_bitmap(mem_dc.Get(), mem_bitmap.get());
// Copy and horizontally mirror the background from hdc into mem_dc. Use
// a negative-width source rect, starting at the rightmost pixel.
StretchBlt(mem_dc.Get(), 0, 0, r.width(), r.height(),
hdc, r.right()-1, r.y(), -r.width(), r.height(), SRCCOPY);
// Draw the arrow.
RECT theme_rect = {0, 0, r.width(), r.height()};
HRESULT result = draw_theme_(handle, mem_dc.Get(), MENU_POPUPSUBMENU,
state_id, &theme_rect, NULL);
// Copy and mirror the result back into mem_dc.
StretchBlt(hdc, r.x(), r.y(), r.width(), r.height(),
mem_dc.Get(), r.width()-1, 0, -r.width(), r.height(), SRCCOPY);
return result;
}
// For some reason, Windows uses the name DFCS_MENUARROWRIGHT to indicate a
// left pointing arrow. This makes the following statement counterintuitive.
UINT pfc_state = extra.pointing_right ? DFCS_MENUARROW : DFCS_MENUARROWRIGHT;
return PaintFrameControl(hdc, rect, DFC_MENU, pfc_state, extra.is_selected,
state);
}
HRESULT NativeThemeWin::PaintMenuCheck(
HDC hdc,
State state,
const gfx::Rect& rect,
const MenuCheckExtraParams& extra) const {
HANDLE handle = GetThemeHandle(MENU);
if (handle && draw_theme_) {
const int state_id = extra.is_radio ?
((state == kDisabled) ? MC_BULLETDISABLED : MC_BULLETNORMAL) :
((state == kDisabled) ? MC_CHECKMARKDISABLED : MC_CHECKMARKNORMAL);
RECT rect_win = rect.ToRECT();
return draw_theme_(handle, hdc, MENU_POPUPCHECK, state_id, &rect_win, NULL);
}
return PaintFrameControl(hdc, rect, DFC_MENU,
extra.is_radio ? DFCS_MENUBULLET : DFCS_MENUCHECK,
extra.is_selected, state);
}
HRESULT NativeThemeWin::PaintMenuCheckBackground(HDC hdc,
State state,
const gfx::Rect& rect) const {
HANDLE handle = GetThemeHandle(MENU);
if (!handle || !draw_theme_)
return S_OK; // Nothing to do for background.
int state_id = state == kDisabled ? MCB_DISABLED : MCB_NORMAL;
RECT rect_win = rect.ToRECT();
return draw_theme_(handle, hdc, MENU_POPUPCHECKBACKGROUND, state_id,
&rect_win, NULL);
}
HRESULT NativeThemeWin::PaintPushButton(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ButtonExtraParams& extra) const {
int state_id = extra.is_default ? PBS_DEFAULTED : PBS_NORMAL;
switch (state) {
case kDisabled:
state_id = PBS_DISABLED;
break;
case kHovered:
state_id = PBS_HOT;
break;
case kNormal:
break;
case kPressed:
state_id = PBS_PRESSED;
break;
case kNumStates:
NOTREACHED();
break;
}
RECT rect_win = rect.ToRECT();
return PaintButton(hdc, state, extra, BP_PUSHBUTTON, state_id, &rect_win);
}
HRESULT NativeThemeWin::PaintRadioButton(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ButtonExtraParams& extra) const {
int state_id = extra.checked ? RBS_CHECKEDNORMAL : RBS_UNCHECKEDNORMAL;
switch (state) {
case kDisabled:
state_id = extra.checked ? RBS_CHECKEDDISABLED : RBS_UNCHECKEDDISABLED;
break;
case kHovered:
state_id = extra.checked ? RBS_CHECKEDHOT : RBS_UNCHECKEDHOT;
break;
case kNormal:
break;
case kPressed:
state_id = extra.checked ? RBS_CHECKEDPRESSED : RBS_UNCHECKEDPRESSED;
break;
case kNumStates:
NOTREACHED();
break;
}
RECT rect_win = rect.ToRECT();
return PaintButton(hdc, state, extra, BP_RADIOBUTTON, state_id, &rect_win);
}
HRESULT NativeThemeWin::PaintCheckbox(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ButtonExtraParams& extra) const {
int state_id = extra.checked ?
CBS_CHECKEDNORMAL :
(extra.indeterminate ? CBS_MIXEDNORMAL : CBS_UNCHECKEDNORMAL);
switch (state) {
case kDisabled:
state_id = extra.checked ?
CBS_CHECKEDDISABLED :
(extra.indeterminate ? CBS_MIXEDDISABLED : CBS_UNCHECKEDDISABLED);
break;
case kHovered:
state_id = extra.checked ?
CBS_CHECKEDHOT :
(extra.indeterminate ? CBS_MIXEDHOT : CBS_UNCHECKEDHOT);
break;
case kNormal:
break;
case kPressed:
state_id = extra.checked ?
CBS_CHECKEDPRESSED :
(extra.indeterminate ? CBS_MIXEDPRESSED : CBS_UNCHECKEDPRESSED);
break;
case kNumStates:
NOTREACHED();
break;
}
RECT rect_win = rect.ToRECT();
return PaintButton(hdc, state, extra, BP_CHECKBOX, state_id, &rect_win);
}
HRESULT NativeThemeWin::PaintMenuList(HDC hdc,
State state,
const gfx::Rect& rect,
const MenuListExtraParams& extra) const {
HANDLE handle = GetThemeHandle(MENULIST);
RECT rect_win = rect.ToRECT();
int state_id = CBXS_NORMAL;
switch (state) {
case kDisabled:
state_id = CBXS_DISABLED;
break;
case kHovered:
state_id = CBXS_HOT;
break;
case kNormal:
break;
case kPressed:
state_id = CBXS_PRESSED;
break;
case kNumStates:
NOTREACHED();
break;
}
if (handle && draw_theme_)
return draw_theme_(handle, hdc, CP_DROPDOWNBUTTON, state_id, &rect_win,
NULL);
// Draw it manually.
DrawFrameControl(hdc, &rect_win, DFC_SCROLL,
DFCS_SCROLLCOMBOBOX | extra.classic_state);
return S_OK;
}
HRESULT NativeThemeWin::PaintScrollbarArrow(
HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ScrollbarArrowExtraParams& extra) const {
static const int state_id_matrix[4][kNumStates] = {
{ABS_DOWNDISABLED, ABS_DOWNHOT, ABS_DOWNNORMAL, ABS_DOWNPRESSED},
{ABS_LEFTDISABLED, ABS_LEFTHOT, ABS_LEFTNORMAL, ABS_LEFTPRESSED},
{ABS_RIGHTDISABLED, ABS_RIGHTHOT, ABS_RIGHTNORMAL, ABS_RIGHTPRESSED},
{ABS_UPDISABLED, ABS_UPHOT, ABS_UPNORMAL, ABS_UPPRESSED},
};
HANDLE handle = GetThemeHandle(SCROLLBAR);
RECT rect_win = rect.ToRECT();
if (handle && draw_theme_) {
int index = part - kScrollbarDownArrow;
DCHECK_GE(index, 0);
DCHECK_LT(static_cast<size_t>(index), arraysize(state_id_matrix));
int state_id = state_id_matrix[index][state];
// Hovering means that the cursor is over the scroolbar, but not over the
// specific arrow itself. We don't want to show it "hot" mode, but only
// in "hover" mode.
if (state == kHovered && extra.is_hovering) {
switch (part) {
case kScrollbarDownArrow:
state_id = ABS_DOWNHOVER;
break;
case kScrollbarLeftArrow:
state_id = ABS_LEFTHOVER;
break;
case kScrollbarRightArrow:
state_id = ABS_RIGHTHOVER;
break;
case kScrollbarUpArrow: