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 HRESULT
GetThemeColor(ThemeName theme
,
61 SkColor
* color
) const;
63 // Get the theme color if theming is enabled. If theming is unsupported
64 // for this part, use Win32's GetSysColor to find the color specified
65 // by default_sys_color.
66 SkColor
GetThemeColorWithDefault(ThemeName theme
,
70 int default_sys_color
) const;
72 // Get the thickness of the border associated with the specified theme,
73 // defaulting to GetSystemMetrics edge size if themes are disabled.
74 // In Classic Windows, borders are typically 2px; on XP+, they are 1px.
75 gfx::Size
GetThemeBorderSize(ThemeName theme
) const;
77 // Disables all theming for top-level windows in the entire process, from
78 // when this method is called until the process exits. All the other
79 // methods in this class will continue to work, but their output will ignore
80 // the user's theme. This is meant for use when running tests that require
81 // consistent visual results.
82 void DisableTheming() const;
84 // Closes cached theme handles so we can unload the DLL or update our UI
85 // for a theme change.
86 void CloseHandles() const;
88 // Returns true if classic theme is in use.
89 bool IsClassicTheme(ThemeName name
) const;
91 // Gets our singleton instance.
92 static NativeThemeWin
* instance();
94 HRESULT
PaintTextField(HDC hdc
,
100 bool fill_content_area
,
101 bool draw_edges
) const;
103 // NativeTheme implementation:
104 virtual gfx::Size
GetPartSize(Part part
,
106 const ExtraParams
& extra
) const OVERRIDE
;
107 virtual void Paint(SkCanvas
* canvas
,
110 const gfx::Rect
& rect
,
111 const ExtraParams
& extra
) const OVERRIDE
;
112 virtual SkColor
GetSystemColor(ColorId color_id
) const OVERRIDE
;
118 // gfx::SysColorChangeListener implementation:
119 virtual void OnSysColorChange() OVERRIDE
;
121 // Update the locally cached set of system colors.
122 void UpdateSystemColors();
124 // Paint directly to canvas' HDC.
125 void PaintDirect(SkCanvas
* canvas
,
128 const gfx::Rect
& rect
,
129 const ExtraParams
& extra
) const;
131 // Create a temporary HDC, paint to that, clean up the alpha values in the
132 // temporary HDC, and then blit the result to canvas. This is to work around
133 // the fact that Windows XP and some classic themes give bogus alpha values.
134 void PaintIndirect(SkCanvas
* canvas
,
137 const gfx::Rect
& rect
,
138 const ExtraParams
& extra
) const;
140 HRESULT
GetThemePartSize(ThemeName themeName
,
148 HRESULT
PaintButton(HDC hdc
,
150 const ButtonExtraParams
& extra
,
155 HRESULT
PaintMenuSeparator(HDC hdc
,
156 const gfx::Rect
& rect
,
157 const MenuSeparatorExtraParams
& extra
) const;
159 HRESULT
PaintMenuGutter(HDC hdc
, const gfx::Rect
& rect
) const;
161 // |arrow_direction| determines whether the arrow is pointing to the left or
162 // to the right. In RTL locales, sub-menus open from right to left and
163 // therefore the menu arrow should point to the left and not to the right.
164 HRESULT
PaintMenuArrow(HDC hdc
,
166 const gfx::Rect
& rect
,
167 const MenuArrowExtraParams
& extra
) const;
169 HRESULT
PaintMenuBackground(HDC hdc
, const gfx::Rect
& rect
) const;
171 HRESULT
PaintMenuCheck(HDC hdc
,
173 const gfx::Rect
& rect
,
174 const MenuCheckExtraParams
& extra
) const;
176 HRESULT
PaintMenuCheckBackground(HDC hdc
,
178 const gfx::Rect
& rect
) const;
180 HRESULT
PaintMenuItemBackground(HDC hdc
,
182 const gfx::Rect
& rect
,
183 const MenuItemExtraParams
& extra
) const;
185 HRESULT
PaintPushButton(HDC hdc
,
188 const gfx::Rect
& rect
,
189 const ButtonExtraParams
& extra
) const;
191 HRESULT
PaintRadioButton(HDC hdc
,
194 const gfx::Rect
& rect
,
195 const ButtonExtraParams
& extra
) const;
197 HRESULT
PaintCheckbox(HDC hdc
,
200 const gfx::Rect
& rect
,
201 const ButtonExtraParams
& extra
) const;
203 HRESULT
PaintMenuList(HDC hdc
,
205 const gfx::Rect
& rect
,
206 const MenuListExtraParams
& extra
) const;
208 // Paints a scrollbar arrow. |classic_state| should have the appropriate
209 // classic part number ORed in already.
210 HRESULT
PaintScrollbarArrow(HDC hdc
,
213 const gfx::Rect
& rect
,
214 const ScrollbarArrowExtraParams
& extra
) const;
216 HRESULT
PaintScrollbarThumb(HDC hdc
,
219 const gfx::Rect
& rect
,
220 const ScrollbarThumbExtraParams
& extra
) const;
222 // This method is deprecated and will be removed in the near future.
223 // Paints a scrollbar track section. |align_rect| is only used in classic
224 // mode, and makes sure the checkerboard pattern in |target_rect| is aligned
225 // with one presumed to be in |align_rect|.
226 HRESULT
PaintScrollbarTrack(SkCanvas
* canvas
,
230 const gfx::Rect
& rect
,
231 const ScrollbarTrackExtraParams
& extra
) const;
233 HRESULT
PaintSpinButton(HDC hdc
,
236 const gfx::Rect
& rect
,
237 const InnerSpinButtonExtraParams
& extra
) const;
239 HRESULT
PaintTrackbar(SkCanvas
* canvas
,
243 const gfx::Rect
& rect
,
244 const TrackbarExtraParams
& extra
) const;
246 HRESULT
PaintProgressBar(HDC hdc
,
247 const gfx::Rect
& rect
,
248 const ProgressBarExtraParams
& extra
) const;
250 HRESULT
PaintWindowResizeGripper(HDC hdc
, const gfx::Rect
& rect
) const;
252 HRESULT
PaintTabPanelBackground(HDC hdc
, const gfx::Rect
& rect
) const;
254 HRESULT
PaintTextField(HDC hdc
,
257 const gfx::Rect
& rect
,
258 const TextFieldExtraParams
& extra
) const;
260 // Paints a theme part, with support for scene scaling in high-DPI mode.
261 // |theme| is the theme handle. |hdc| is the handle for the device context.
262 // |part_id| is the identifier for the part (e.g. thumb gripper). |state_id|
263 // is the identifier for the rendering state of the part (e.g. hover). |rect|
264 // is the bounds for rendering, expressed in logical coordinates.
265 HRESULT
PaintScaledTheme(HANDLE theme
,
269 const gfx::Rect
& rect
) const;
271 // Get the windows theme name/part/state. These three helper functions are
272 // used only by GetPartSize(), as each of the corresponding PaintXXX()
273 // methods do further validation of the part and state that is required for
275 static ThemeName
GetThemeName(Part part
);
276 static int GetWindowsPart(Part part
, State state
, const ExtraParams
& extra
);
277 static int GetWindowsState(Part part
, State state
, const ExtraParams
& extra
);
279 HRESULT
GetThemeInt(ThemeName theme
,
285 HRESULT
PaintFrameControl(HDC hdc
,
286 const gfx::Rect
& rect
,
290 State control_state
) const;
292 // Returns a handle to the theme data.
293 HANDLE
GetThemeHandle(ThemeName theme_name
) const;
295 typedef HRESULT (WINAPI
* DrawThemeBackgroundPtr
)(HANDLE theme
,
300 const RECT
* clip_rect
);
301 typedef HRESULT (WINAPI
* DrawThemeBackgroundExPtr
)(HANDLE theme
,
306 const DTBGOPTS
* opts
);
307 typedef HRESULT (WINAPI
* GetThemeColorPtr
)(HANDLE hTheme
,
312 typedef HRESULT (WINAPI
* GetThemeContentRectPtr
)(HANDLE hTheme
,
318 typedef HRESULT (WINAPI
* GetThemePartSizePtr
)(HANDLE hTheme
,
325 typedef HANDLE (WINAPI
* OpenThemeDataPtr
)(HWND window
,
327 typedef HRESULT (WINAPI
* CloseThemeDataPtr
)(HANDLE theme
);
329 typedef void (WINAPI
* SetThemeAppPropertiesPtr
) (DWORD flags
);
330 typedef BOOL (WINAPI
* IsThemeActivePtr
)();
331 typedef HRESULT (WINAPI
* GetThemeIntPtr
)(HANDLE hTheme
,
337 // Function pointers into uxtheme.dll.
338 DrawThemeBackgroundPtr draw_theme_
;
339 DrawThemeBackgroundExPtr draw_theme_ex_
;
340 GetThemeColorPtr get_theme_color_
;
341 GetThemeContentRectPtr get_theme_content_rect_
;
342 GetThemePartSizePtr get_theme_part_size_
;
343 OpenThemeDataPtr open_theme_
;
344 CloseThemeDataPtr close_theme_
;
345 SetThemeAppPropertiesPtr set_theme_properties_
;
346 IsThemeActivePtr is_theme_active_
;
347 GetThemeIntPtr get_theme_int_
;
349 // Handle to uxtheme.dll.
352 // A cache of open theme handles.
353 mutable HANDLE theme_handles_
[LAST
];
355 // The system color change listener and the updated cache of system colors.
356 gfx::ScopedSysColorChangeListener color_change_listener_
;
357 mutable std::map
<int, SkColor
> system_colors_
;
359 DISALLOW_COPY_AND_ASSIGN(NativeThemeWin
);
364 #endif // UI_NATIVE_THEME_NATIVE_THEME_WIN_H_