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. 4 5 #ifndef UI_NATIVE_THEME_NATIVE_THEME_H_ 6 #define UI_NATIVE_THEME_NATIVE_THEME_H_ 7 8 #include "base/observer_list.h" 9 #include "third_party/skia/include/core/SkColor.h" 10 #include "ui/gfx/native_widget_types.h" 11 #include "ui/native_theme/native_theme_export.h" 12 13 class SkCanvas; 14 15 namespace gfx { 16 class Rect; 17 class Size; 18 } 19 20 namespace ui { 21 22 class NativeThemeObserver; 23 24 // This class supports drawing UI controls (like buttons, text fields, lists, 25 // comboboxes, etc) that look like the native UI controls of the underlying 26 // platform, such as Windows or Linux. It also supplies default colors for 27 // dialog box backgrounds, etc., which are obtained from the system theme where 28 // possible. 29 // 30 // The supported control types are listed in the Part enum. These parts can be 31 // in any state given by the State enum, where the actual definition of the 32 // state is part-specific. The supported colors are listed in the ColorId enum. 33 // 34 // Some parts require more information than simply the state in order to be 35 // drawn correctly, and this information is given to the Paint() method via the 36 // ExtraParams union. Each part that requires more information has its own 37 // field in the union. 38 // 39 // NativeTheme also supports getting the default size of a given part with 40 // the GetPartSize() method. 41 class NATIVE_THEME_EXPORT NativeTheme { 42 public: 43 // The part to be painted / sized. 44 enum Part { 45 kComboboxArrow, 46 kCheckbox, 47 kInnerSpinButton, 48 kMenuList, 49 kMenuCheck, 50 kMenuCheckBackground, 51 kMenuPopupArrow, 52 kMenuPopupBackground, 53 kMenuPopupGutter, 54 kMenuPopupSeparator, 55 kMenuItemBackground, 56 kProgressBar, 57 kPushButton, 58 kRadio, 59 60 // The order of the arrow enums is important, do not change without also 61 // changing the code in platform implementations. 62 kScrollbarDownArrow, 63 kScrollbarLeftArrow, 64 kScrollbarRightArrow, 65 kScrollbarUpArrow, 66 67 kScrollbarHorizontalThumb, 68 kScrollbarVerticalThumb, 69 kScrollbarHorizontalTrack, 70 kScrollbarVerticalTrack, 71 kScrollbarHorizontalGripper, 72 kScrollbarVerticalGripper, 73 // The corner is drawn when there is both a horizontal and vertical 74 // scrollbar. 75 kScrollbarCorner, 76 kSliderTrack, 77 kSliderThumb, 78 kTabPanelBackground, 79 kTextField, 80 kTrackbarThumb, 81 kTrackbarTrack, 82 kWindowResizeGripper, 83 kMaxPart, 84 }; 85 86 // The state of the part. 87 enum State { 88 // IDs defined as specific values for use in arrays. 89 kDisabled = 0, 90 kHovered = 1, 91 kNormal = 2, 92 kPressed = 3, 93 kMaxState = 4, 94 }; 95 96 // Each structure below holds extra information needed when painting a given 97 // part. 98 99 struct ButtonExtraParams { 100 bool checked; 101 bool indeterminate; // Whether the button state is indeterminate. 102 bool is_default; // Whether the button is default button. 103 bool is_focused; 104 bool has_border; 105 int classic_state; // Used on Windows when uxtheme is not available. 106 SkColor background_color; 107 }; 108 109 struct InnerSpinButtonExtraParams { 110 bool spin_up; 111 bool read_only; 112 int classic_state; // Used on Windows when uxtheme is not available. 113 }; 114 115 struct MenuArrowExtraParams { 116 bool pointing_right; 117 // Used for the disabled state to indicate if the item is both disabled and 118 // selected. 119 bool is_selected; 120 }; 121 122 struct MenuCheckExtraParams { 123 bool is_radio; 124 // Used for the disabled state to indicate if the item is both disabled and 125 // selected. 126 bool is_selected; 127 }; 128 129 struct MenuItemExtraParams { 130 bool is_selected; 131 }; 132 133 struct MenuListExtraParams { 134 bool has_border; 135 bool has_border_radius; 136 int arrow_x; 137 int arrow_y; 138 SkColor background_color; 139 int classic_state; // Used on Windows when uxtheme is not available. 140 }; 141 142 struct MenuSeparatorExtraParams { 143 bool has_gutter; 144 }; 145 146 struct MenuBackgroundExtraParams { 147 int corner_radius; 148 }; 149 150 struct ProgressBarExtraParams { 151 double animated_seconds; 152 bool determinate; 153 int value_rect_x; 154 int value_rect_y; 155 int value_rect_width; 156 int value_rect_height; 157 }; 158 159 struct ScrollbarArrowExtraParams { 160 bool is_hovering; 161 }; 162 163 struct ScrollbarTrackExtraParams { 164 bool is_upper; 165 int track_x; 166 int track_y; 167 int track_width; 168 int track_height; 169 int classic_state; // Used on Windows when uxtheme is not available. 170 }; 171 172 struct ScrollbarThumbExtraParams { 173 bool is_hovering; 174 }; 175 176 struct SliderExtraParams { 177 bool vertical; 178 bool in_drag; 179 }; 180 181 struct TextFieldExtraParams { 182 bool is_text_area; 183 bool is_listbox; 184 SkColor background_color; 185 bool is_read_only; 186 bool is_focused; 187 bool fill_content_area; 188 bool draw_edges; 189 int classic_state; // Used on Windows when uxtheme is not available. 190 }; 191 192 struct TrackbarExtraParams { 193 bool vertical; 194 int classic_state; // Used on Windows when uxtheme is not available. 195 }; 196 197 union ExtraParams { 198 ButtonExtraParams button; 199 InnerSpinButtonExtraParams inner_spin; 200 MenuArrowExtraParams menu_arrow; 201 MenuCheckExtraParams menu_check; 202 MenuItemExtraParams menu_item; 203 MenuListExtraParams menu_list; 204 MenuSeparatorExtraParams menu_separator; 205 MenuBackgroundExtraParams menu_background; 206 ProgressBarExtraParams progress_bar; 207 ScrollbarArrowExtraParams scrollbar_arrow; 208 ScrollbarTrackExtraParams scrollbar_track; 209 ScrollbarThumbExtraParams scrollbar_thumb; 210 SliderExtraParams slider; 211 TextFieldExtraParams text_field; 212 TrackbarExtraParams trackbar; 213 }; 214 215 // Return the size of the part. 216 virtual gfx::Size GetPartSize(Part part, 217 State state, 218 const ExtraParams& extra) const = 0; 219 220 // Paint the part to the canvas. 221 virtual void Paint(SkCanvas* canvas, 222 Part part, 223 State state, 224 const gfx::Rect& rect, 225 const ExtraParams& extra) const = 0; 226 227 // Paint part during state transition, used for overlay scrollbar state 228 // transition animation. 229 virtual void PaintStateTransition(SkCanvas* canvas, 230 Part part, 231 State startState, 232 State endState, 233 double progress, 234 const gfx::Rect& rect) const { } 235 236 // Supports theme specific colors. 237 void SetScrollbarColors(unsigned inactive_color, 238 unsigned active_color, 239 unsigned track_color); 240 241 // Colors for GetSystemColor(). 242 enum ColorId { 243 // Windows 244 kColorId_WindowBackground, 245 // Dialogs 246 kColorId_DialogBackground, 247 // FocusableBorder 248 kColorId_FocusedBorderColor, 249 kColorId_UnfocusedBorderColor, 250 // Button 251 kColorId_ButtonBackgroundColor, 252 kColorId_ButtonEnabledColor, 253 kColorId_ButtonDisabledColor, 254 kColorId_ButtonHighlightColor, 255 kColorId_ButtonHoverColor, 256 kColorId_ButtonHoverBackgroundColor, 257 kColorId_BlueButtonEnabledColor, 258 kColorId_BlueButtonDisabledColor, 259 kColorId_BlueButtonHighlightColor, 260 kColorId_BlueButtonHoverColor, 261 // MenuItem 262 kColorId_EnabledMenuItemForegroundColor, 263 kColorId_DisabledMenuItemForegroundColor, 264 kColorId_DisabledEmphasizedMenuItemForegroundColor, 265 kColorId_SelectedMenuItemForegroundColor, 266 kColorId_FocusedMenuItemBackgroundColor, 267 kColorId_HoverMenuItemBackgroundColor, 268 kColorId_MenuSeparatorColor, 269 kColorId_MenuBackgroundColor, 270 kColorId_MenuBorderColor, 271 // MenuButton - buttons in wrench menu 272 kColorId_EnabledMenuButtonBorderColor, 273 kColorId_FocusedMenuButtonBorderColor, 274 kColorId_HoverMenuButtonBorderColor, 275 // Label 276 kColorId_LabelEnabledColor, 277 kColorId_LabelDisabledColor, 278 kColorId_LabelBackgroundColor, 279 // Textfield 280 kColorId_TextfieldDefaultColor, 281 kColorId_TextfieldDefaultBackground, 282 kColorId_TextfieldReadOnlyColor, 283 kColorId_TextfieldReadOnlyBackground, 284 kColorId_TextfieldSelectionColor, 285 kColorId_TextfieldSelectionBackgroundFocused, 286 // Tooltip 287 kColorId_TooltipBackground, 288 kColorId_TooltipText, 289 // Tree 290 kColorId_TreeBackground, 291 kColorId_TreeText, 292 kColorId_TreeSelectedText, 293 kColorId_TreeSelectedTextUnfocused, 294 kColorId_TreeSelectionBackgroundFocused, 295 kColorId_TreeSelectionBackgroundUnfocused, 296 kColorId_TreeArrow, 297 // Table 298 kColorId_TableBackground, 299 kColorId_TableText, 300 kColorId_TableSelectedText, 301 kColorId_TableSelectedTextUnfocused, 302 kColorId_TableSelectionBackgroundFocused, 303 kColorId_TableSelectionBackgroundUnfocused, 304 kColorId_TableGroupingIndicatorColor, 305 // Results Tables, such as the chrome omnibox. 306 kColorId_ResultsTableNormalBackground, 307 kColorId_ResultsTableHoveredBackground, 308 kColorId_ResultsTableSelectedBackground, 309 kColorId_ResultsTableNormalText, 310 kColorId_ResultsTableHoveredText, 311 kColorId_ResultsTableSelectedText, 312 kColorId_ResultsTableNormalDimmedText, 313 kColorId_ResultsTableHoveredDimmedText, 314 kColorId_ResultsTableSelectedDimmedText, 315 kColorId_ResultsTableNormalUrl, 316 kColorId_ResultsTableHoveredUrl, 317 kColorId_ResultsTableSelectedUrl, 318 kColorId_ResultsTableNormalDivider, 319 kColorId_ResultsTableHoveredDivider, 320 kColorId_ResultsTableSelectedDivider, 321 // TODO(benrg): move other hardcoded colors here. 322 }; 323 324 // Return a color from the system theme. 325 virtual SkColor GetSystemColor(ColorId color_id) const = 0; 326 327 // Returns a shared instance of the native theme. 328 // The returned object should not be deleted by the caller. This function 329 // is not thread safe and should only be called from the UI thread. 330 // Each port of NativeTheme should provide its own implementation of this 331 // function, returning the port's subclass. 332 static NativeTheme* instance(); 333 334 // Add or remove observers to be notified when the native theme changes. 335 void AddObserver(NativeThemeObserver* observer); 336 void RemoveObserver(NativeThemeObserver* observer); 337 338 // Notify observers of native theme changes. 339 void NotifyObservers(); 340 341 protected: 342 NativeTheme(); 343 virtual ~NativeTheme(); 344 345 unsigned int thumb_inactive_color_; 346 unsigned int thumb_active_color_; 347 unsigned int track_color_; 348 349 private: 350 // Observers to notify when the native theme changes. 351 ObserverList<NativeThemeObserver> native_theme_observers_; 352 353 DISALLOW_COPY_AND_ASSIGN(NativeTheme); 354 }; 355 356 } // namespace ui 357 358 #endif // UI_NATIVE_THEME_NATIVE_THEME_H_ 359