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
|
// 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.
#ifndef UI_NATIVE_THEME_NATIVE_THEME_WIN_H_
#define UI_NATIVE_THEME_NATIVE_THEME_WIN_H_
// A wrapper class for working with custom XP/Vista themes provided in
// uxtheme.dll. This is a singleton class that can be grabbed using
// NativeThemeWin::instance().
// For more information on visual style parts and states, see:
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/userex/topics/partsandstates.asp
#include <map>
#include <windows.h>
#include <uxtheme.h>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/sys_color_change_listener.h"
#include "ui/native_theme/native_theme.h"
class SkCanvas;
namespace ui {
// Windows implementation of native theme class.
//
// At the moment, this class in in transition from an older API that consists
// of several PaintXXX methods to an API, inherited from the NativeTheme base
// class, that consists of a single Paint() method with a argument to indicate
// what kind of part to paint.
class NATIVE_THEME_EXPORT NativeThemeWin : public NativeTheme,
public gfx::SysColorChangeListener {
public:
enum ThemeName {
BUTTON,
LIST,
MENU,
MENULIST,
SCROLLBAR,
STATUS,
TAB,
TEXTFIELD,
TRACKBAR,
WINDOW,
PROGRESS,
SPIN,
LAST
};
bool IsThemingActive() const;
// Returns true if a high contrast theme is being used.
bool IsUsingHighContrastTheme() const;
HRESULT GetThemeColor(ThemeName theme,
int part_id,
int state_id,
int prop_id,
SkColor* color) const;
// Get the theme color if theming is enabled. If theming is unsupported
// for this part, use Win32's GetSysColor to find the color specified
// by default_sys_color.
SkColor GetThemeColorWithDefault(ThemeName theme,
int part_id,
int state_id,
int prop_id,
int default_sys_color) const;
// Get the thickness of the border associated with the specified theme,
// defaulting to GetSystemMetrics edge size if themes are disabled.
// In Classic Windows, borders are typically 2px; on XP+, they are 1px.
gfx::Size GetThemeBorderSize(ThemeName theme) const;
// Disables all theming for top-level windows in the entire process, from
// when this method is called until the process exits. All the other
// methods in this class will continue to work, but their output will ignore
// the user's theme. This is meant for use when running tests that require
// consistent visual results.
void DisableTheming() const;
// Closes cached theme handles so we can unload the DLL or update our UI
// for a theme change.
void CloseHandles() const;
// Returns true if classic theme is in use.
bool IsClassicTheme(ThemeName name) const;
// Gets our singleton instance.
static NativeThemeWin* instance();
HRESULT PaintTextField(HDC hdc,
int part_id,
int state_id,
int classic_state,
RECT* rect,
COLORREF color,
bool fill_content_area,
bool draw_edges) const;
// NativeTheme implementation:
virtual gfx::Size GetPartSize(Part part,
State state,
const ExtraParams& extra) const override;
virtual void Paint(SkCanvas* canvas,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const override;
virtual SkColor GetSystemColor(ColorId color_id) const override;
private:
NativeThemeWin();
~NativeThemeWin();
// gfx::SysColorChangeListener implementation:
virtual void OnSysColorChange() override;
// Update the locally cached set of system colors.
void UpdateSystemColors();
// Paint directly to canvas' HDC.
void PaintDirect(SkCanvas* canvas,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const;
// Create a temporary HDC, paint to that, clean up the alpha values in the
// temporary HDC, and then blit the result to canvas. This is to work around
// the fact that Windows XP and some classic themes give bogus alpha values.
void PaintIndirect(SkCanvas* canvas,
Part part,
State state,
const gfx::Rect& rect,
const ExtraParams& extra) const;
HRESULT GetThemePartSize(ThemeName themeName,
HDC hdc,
int part_id,
int state_id,
RECT* rect,
int ts,
SIZE* size) const;
HRESULT PaintButton(HDC hdc,
State state,
const ButtonExtraParams& extra,
int part_id,
int state_id,
RECT* rect) const;
HRESULT PaintMenuSeparator(HDC hdc,
const gfx::Rect& rect) const;
HRESULT PaintMenuGutter(HDC hdc, const gfx::Rect& rect) const;
// |arrow_direction| determines whether the arrow is pointing to the left or
// to the right. In RTL locales, sub-menus open from right to left and
// therefore the menu arrow should point to the left and not to the right.
HRESULT PaintMenuArrow(HDC hdc,
State state,
const gfx::Rect& rect,
const MenuArrowExtraParams& extra) const;
HRESULT PaintMenuBackground(HDC hdc, const gfx::Rect& rect) const;
HRESULT PaintMenuCheck(HDC hdc,
State state,
const gfx::Rect& rect,
const MenuCheckExtraParams& extra) const;
HRESULT PaintMenuCheckBackground(HDC hdc,
State state,
const gfx::Rect& rect) const;
HRESULT PaintMenuItemBackground(HDC hdc,
State state,
const gfx::Rect& rect,
const MenuItemExtraParams& extra) const;
HRESULT PaintPushButton(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ButtonExtraParams& extra) const;
HRESULT PaintRadioButton(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ButtonExtraParams& extra) const;
HRESULT PaintCheckbox(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ButtonExtraParams& extra) const;
HRESULT PaintMenuList(HDC hdc,
State state,
const gfx::Rect& rect,
const MenuListExtraParams& extra) const;
// Paints a scrollbar arrow. |classic_state| should have the appropriate
// classic part number ORed in already.
HRESULT PaintScrollbarArrow(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ScrollbarArrowExtraParams& extra) const;
HRESULT PaintScrollbarThumb(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ScrollbarThumbExtraParams& extra) const;
// This method is deprecated and will be removed in the near future.
// Paints a scrollbar track section. |align_rect| is only used in classic
// mode, and makes sure the checkerboard pattern in |target_rect| is aligned
// with one presumed to be in |align_rect|.
HRESULT PaintScrollbarTrack(SkCanvas* canvas,
HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const ScrollbarTrackExtraParams& extra) const;
HRESULT PaintSpinButton(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const InnerSpinButtonExtraParams& extra) const;
HRESULT PaintTrackbar(SkCanvas* canvas,
HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const TrackbarExtraParams& extra) const;
HRESULT PaintProgressBar(HDC hdc,
const gfx::Rect& rect,
const ProgressBarExtraParams& extra) const;
HRESULT PaintWindowResizeGripper(HDC hdc, const gfx::Rect& rect) const;
HRESULT PaintTabPanelBackground(HDC hdc, const gfx::Rect& rect) const;
HRESULT PaintTextField(HDC hdc,
Part part,
State state,
const gfx::Rect& rect,
const TextFieldExtraParams& extra) const;
// Paints a theme part, with support for scene scaling in high-DPI mode.
// |theme| is the theme handle. |hdc| is the handle for the device context.
// |part_id| is the identifier for the part (e.g. thumb gripper). |state_id|
// is the identifier for the rendering state of the part (e.g. hover). |rect|
// is the bounds for rendering, expressed in logical coordinates.
HRESULT PaintScaledTheme(HANDLE theme,
HDC hdc,
int part_id,
int state_id,
const gfx::Rect& rect) const;
// Get the windows theme name/part/state. These three helper functions are
// used only by GetPartSize(), as each of the corresponding PaintXXX()
// methods do further validation of the part and state that is required for
// getting the size.
static ThemeName GetThemeName(Part part);
static int GetWindowsPart(Part part, State state, const ExtraParams& extra);
static int GetWindowsState(Part part, State state, const ExtraParams& extra);
HRESULT GetThemeInt(ThemeName theme,
int part_id,
int state_id,
int prop_id,
int *result) const;
HRESULT PaintFrameControl(HDC hdc,
const gfx::Rect& rect,
UINT type,
UINT state,
bool is_selected,
State control_state) const;
// Returns a handle to the theme data.
HANDLE GetThemeHandle(ThemeName theme_name) const;
typedef HRESULT (WINAPI* DrawThemeBackgroundPtr)(HANDLE theme,
HDC hdc,
int part_id,
int state_id,
const RECT* rect,
const RECT* clip_rect);
typedef HRESULT (WINAPI* DrawThemeBackgroundExPtr)(HANDLE theme,
HDC hdc,
int part_id,
int state_id,
const RECT* rect,
const DTBGOPTS* opts);
typedef HRESULT (WINAPI* GetThemeColorPtr)(HANDLE hTheme,
int part_id,
int state_id,
int prop_id,
COLORREF* color);
typedef HRESULT (WINAPI* GetThemeContentRectPtr)(HANDLE hTheme,
HDC hdc,
int part_id,
int state_id,
const RECT* rect,
RECT* content_rect);
typedef HRESULT (WINAPI* GetThemePartSizePtr)(HANDLE hTheme,
HDC hdc,
int part_id,
int state_id,
RECT* rect,
int ts,
SIZE* size);
typedef HANDLE (WINAPI* OpenThemeDataPtr)(HWND window,
LPCWSTR class_list);
typedef HRESULT (WINAPI* CloseThemeDataPtr)(HANDLE theme);
typedef void (WINAPI* SetThemeAppPropertiesPtr) (DWORD flags);
typedef BOOL (WINAPI* IsThemeActivePtr)();
typedef HRESULT (WINAPI* GetThemeIntPtr)(HANDLE hTheme,
int part_id,
int state_id,
int prop_id,
int *value);
// Function pointers into uxtheme.dll.
DrawThemeBackgroundPtr draw_theme_;
DrawThemeBackgroundExPtr draw_theme_ex_;
GetThemeColorPtr get_theme_color_;
GetThemeContentRectPtr get_theme_content_rect_;
GetThemePartSizePtr get_theme_part_size_;
OpenThemeDataPtr open_theme_;
CloseThemeDataPtr close_theme_;
SetThemeAppPropertiesPtr set_theme_properties_;
IsThemeActivePtr is_theme_active_;
GetThemeIntPtr get_theme_int_;
// Handle to uxtheme.dll.
HMODULE theme_dll_;
// A cache of open theme handles.
mutable HANDLE theme_handles_[LAST];
// The system color change listener and the updated cache of system colors.
gfx::ScopedSysColorChangeListener color_change_listener_;
mutable std::map<int, SkColor> system_colors_;
// Is a high contrast theme active?
mutable bool is_using_high_contrast_;
// Is |is_using_high_contrast_| valid?
mutable bool is_using_high_contrast_valid_;
DISALLOW_COPY_AND_ASSIGN(NativeThemeWin);
};
} // namespace ui
#endif // UI_NATIVE_THEME_NATIVE_THEME_WIN_H_
|