1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_NATIVE_THEME_NATIVE_THEME_WIN_H_
6 #define UI_NATIVE_THEME_NATIVE_THEME_WIN_H_
8 // A wrapper class for working with custom XP/Vista themes provided in
9 // uxtheme.dll. This is a singleton class that can be grabbed using
10 // NativeThemeWin::instance().
11 // For more information on visual style parts and states, see:
12 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/userex/topics/partsandstates.asp
19 #include "base/basictypes.h"
20 #include "base/compiler_specific.h"
21 #include "third_party/skia/include/core/SkColor.h"
22 #include "ui/gfx/size.h"
23 #include "ui/gfx/sys_color_change_listener.h"
24 #include "ui/native_theme/native_theme.h"
30 // Windows implementation of native theme class.
32 // At the moment, this class in in transition from an older API that consists
33 // of several PaintXXX methods to an API, inherited from the NativeTheme base
34 // class, that consists of a single Paint() method with a argument to indicate
35 // what kind of part to paint.
36 class NATIVE_THEME_EXPORT NativeThemeWin
: public NativeTheme
,
37 public gfx::SysColorChangeListener
{
55 bool IsThemingActive() const;
57 // Returns true if a high contrast theme is being used.
58 bool IsUsingHighContrastTheme() const;
60 HRESULT
GetThemeColor(ThemeName theme
,
64 SkColor
* color
) const;
66 // Get the theme color if theming is enabled. If theming is unsupported
67 // for this part, use Win32's GetSysColor to find the color specified
68 // by default_sys_color.
69 SkColor
GetThemeColorWithDefault(ThemeName theme
,
73 int default_sys_color
) const;
75 // Get the thickness of the border associated with the specified theme,
76 // defaulting to GetSystemMetrics edge size if themes are disabled.
77 // In Classic Windows, borders are typically 2px; on XP+, they are 1px.
78 gfx::Size
GetThemeBorderSize(ThemeName theme
) const;
80 // Disables all theming for top-level windows in the entire process, from
81 // when this method is called until the process exits. All the other
82 // methods in this class will continue to work, but their output will ignore
83 // the user's theme. This is meant for use when running tests that require
84 // consistent visual results.
85 void DisableTheming() const;
87 // Closes cached theme handles so we can unload the DLL or update our UI
88 // for a theme change.
89 void CloseHandles() const;
91 // Returns true if classic theme is in use.
92 bool IsClassicTheme(ThemeName name
) const;
94 // Gets our singleton instance.
95 static NativeThemeWin
* instance();
97 HRESULT
PaintTextField(HDC hdc
,
103 bool fill_content_area
,
104 bool draw_edges
) const;
106 // NativeTheme implementation:
107 virtual gfx::Size
GetPartSize(Part part
,
109 const ExtraParams
& extra
) const OVERRIDE
;
110 virtual void Paint(SkCanvas
* canvas
,
113 const gfx::Rect
& rect
,
114 const ExtraParams
& extra
) const OVERRIDE
;
115 virtual SkColor
GetSystemColor(ColorId color_id
) const OVERRIDE
;
121 // gfx::SysColorChangeListener implementation:
122 virtual void OnSysColorChange() OVERRIDE
;
124 // Update the locally cached set of system colors.
125 void UpdateSystemColors();
127 // Paint directly to canvas' HDC.
128 void PaintDirect(SkCanvas
* canvas
,
131 const gfx::Rect
& rect
,
132 const ExtraParams
& extra
) const;
134 // Create a temporary HDC, paint to that, clean up the alpha values in the
135 // temporary HDC, and then blit the result to canvas. This is to work around
136 // the fact that Windows XP and some classic themes give bogus alpha values.
137 void PaintIndirect(SkCanvas
* canvas
,
140 const gfx::Rect
& rect
,
141 const ExtraParams
& extra
) const;
143 HRESULT
GetThemePartSize(ThemeName themeName
,
151 HRESULT
PaintButton(HDC hdc
,
153 const ButtonExtraParams
& extra
,
158 HRESULT
PaintMenuSeparator(HDC hdc
,
159 const gfx::Rect
& rect
,
160 const MenuSeparatorExtraParams
& extra
) const;
162 HRESULT
PaintMenuGutter(HDC hdc
, const gfx::Rect
& rect
) const;
164 // |arrow_direction| determines whether the arrow is pointing to the left or
165 // to the right. In RTL locales, sub-menus open from right to left and
166 // therefore the menu arrow should point to the left and not to the right.
167 HRESULT
PaintMenuArrow(HDC hdc
,
169 const gfx::Rect
& rect
,
170 const MenuArrowExtraParams
& extra
) const;
172 HRESULT
PaintMenuBackground(HDC hdc
, const gfx::Rect
& rect
) const;
174 HRESULT
PaintMenuCheck(HDC hdc
,
176 const gfx::Rect
& rect
,
177 const MenuCheckExtraParams
& extra
) const;
179 HRESULT
PaintMenuCheckBackground(HDC hdc
,
181 const gfx::Rect
& rect
) const;
183 HRESULT
PaintMenuItemBackground(HDC hdc
,
185 const gfx::Rect
& rect
,
186 const MenuItemExtraParams
& extra
) const;
188 HRESULT
PaintPushButton(HDC hdc
,
191 const gfx::Rect
& rect
,
192 const ButtonExtraParams
& extra
) const;
194 HRESULT
PaintRadioButton(HDC hdc
,
197 const gfx::Rect
& rect
,
198 const ButtonExtraParams
& extra
) const;
200 HRESULT
PaintCheckbox(HDC hdc
,
203 const gfx::Rect
& rect
,
204 const ButtonExtraParams
& extra
) const;
206 HRESULT
PaintMenuList(HDC hdc
,
208 const gfx::Rect
& rect
,
209 const MenuListExtraParams
& extra
) const;
211 // Paints a scrollbar arrow. |classic_state| should have the appropriate
212 // classic part number ORed in already.
213 HRESULT
PaintScrollbarArrow(HDC hdc
,
216 const gfx::Rect
& rect
,
217 const ScrollbarArrowExtraParams
& extra
) const;
219 HRESULT
PaintScrollbarThumb(HDC hdc
,
222 const gfx::Rect
& rect
,
223 const ScrollbarThumbExtraParams
& extra
) const;
225 // This method is deprecated and will be removed in the near future.
226 // Paints a scrollbar track section. |align_rect| is only used in classic
227 // mode, and makes sure the checkerboard pattern in |target_rect| is aligned
228 // with one presumed to be in |align_rect|.
229 HRESULT
PaintScrollbarTrack(SkCanvas
* canvas
,
233 const gfx::Rect
& rect
,
234 const ScrollbarTrackExtraParams
& extra
) const;
236 HRESULT
PaintSpinButton(HDC hdc
,
239 const gfx::Rect
& rect
,
240 const InnerSpinButtonExtraParams
& extra
) const;
242 HRESULT
PaintTrackbar(SkCanvas
* canvas
,
246 const gfx::Rect
& rect
,
247 const TrackbarExtraParams
& extra
) const;
249 HRESULT
PaintProgressBar(HDC hdc
,
250 const gfx::Rect
& rect
,
251 const ProgressBarExtraParams
& extra
) const;
253 HRESULT
PaintWindowResizeGripper(HDC hdc
, const gfx::Rect
& rect
) const;
255 HRESULT
PaintTabPanelBackground(HDC hdc
, const gfx::Rect
& rect
) const;
257 HRESULT
PaintTextField(HDC hdc
,
260 const gfx::Rect
& rect
,
261 const TextFieldExtraParams
& extra
) const;
263 // Paints a theme part, with support for scene scaling in high-DPI mode.
264 // |theme| is the theme handle. |hdc| is the handle for the device context.
265 // |part_id| is the identifier for the part (e.g. thumb gripper). |state_id|
266 // is the identifier for the rendering state of the part (e.g. hover). |rect|
267 // is the bounds for rendering, expressed in logical coordinates.
268 HRESULT
PaintScaledTheme(HANDLE theme
,
272 const gfx::Rect
& rect
) const;
274 // Get the windows theme name/part/state. These three helper functions are
275 // used only by GetPartSize(), as each of the corresponding PaintXXX()
276 // methods do further validation of the part and state that is required for
278 static ThemeName
GetThemeName(Part part
);
279 static int GetWindowsPart(Part part
, State state
, const ExtraParams
& extra
);
280 static int GetWindowsState(Part part
, State state
, const ExtraParams
& extra
);
282 HRESULT
GetThemeInt(ThemeName theme
,
288 HRESULT
PaintFrameControl(HDC hdc
,
289 const gfx::Rect
& rect
,
293 State control_state
) const;
295 // Returns a handle to the theme data.
296 HANDLE
GetThemeHandle(ThemeName theme_name
) const;
298 typedef HRESULT (WINAPI
* DrawThemeBackgroundPtr
)(HANDLE theme
,
303 const RECT
* clip_rect
);
304 typedef HRESULT (WINAPI
* DrawThemeBackgroundExPtr
)(HANDLE theme
,
309 const DTBGOPTS
* opts
);
310 typedef HRESULT (WINAPI
* GetThemeColorPtr
)(HANDLE hTheme
,
315 typedef HRESULT (WINAPI
* GetThemeContentRectPtr
)(HANDLE hTheme
,
321 typedef HRESULT (WINAPI
* GetThemePartSizePtr
)(HANDLE hTheme
,
328 typedef HANDLE (WINAPI
* OpenThemeDataPtr
)(HWND window
,
330 typedef HRESULT (WINAPI
* CloseThemeDataPtr
)(HANDLE theme
);
332 typedef void (WINAPI
* SetThemeAppPropertiesPtr
) (DWORD flags
);
333 typedef BOOL (WINAPI
* IsThemeActivePtr
)();
334 typedef HRESULT (WINAPI
* GetThemeIntPtr
)(HANDLE hTheme
,
340 // Function pointers into uxtheme.dll.
341 DrawThemeBackgroundPtr draw_theme_
;
342 DrawThemeBackgroundExPtr draw_theme_ex_
;
343 GetThemeColorPtr get_theme_color_
;
344 GetThemeContentRectPtr get_theme_content_rect_
;
345 GetThemePartSizePtr get_theme_part_size_
;
346 OpenThemeDataPtr open_theme_
;
347 CloseThemeDataPtr close_theme_
;
348 SetThemeAppPropertiesPtr set_theme_properties_
;
349 IsThemeActivePtr is_theme_active_
;
350 GetThemeIntPtr get_theme_int_
;
352 // Handle to uxtheme.dll.
355 // A cache of open theme handles.
356 mutable HANDLE theme_handles_
[LAST
];
358 // The system color change listener and the updated cache of system colors.
359 gfx::ScopedSysColorChangeListener color_change_listener_
;
360 mutable std::map
<int, SkColor
> system_colors_
;
362 // Is a high contrast theme active?
363 mutable bool is_using_high_contrast_
;
365 // Is |is_using_high_contrast_| valid?
366 mutable bool is_using_high_contrast_valid_
;
368 DISALLOW_COPY_AND_ASSIGN(NativeThemeWin
);
373 #endif // UI_NATIVE_THEME_NATIVE_THEME_WIN_H_