OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ | 5 #ifndef UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ |
6 #define UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ | 6 #define UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
11 #include "ui/base/models/combobox_model_observer.h" | 11 #include "ui/base/models/combobox_model_observer.h" |
| 12 #include "ui/gfx/animation/animation_delegate.h" |
12 #include "ui/gfx/native_widget_types.h" | 13 #include "ui/gfx/native_widget_types.h" |
| 14 #include "ui/views/controls/button/button.h" |
13 #include "ui/views/controls/menu/menu_delegate.h" | 15 #include "ui/views/controls/menu/menu_delegate.h" |
14 #include "ui/views/controls/prefix_delegate.h" | 16 #include "ui/views/controls/prefix_delegate.h" |
15 | 17 |
16 namespace gfx { | 18 namespace gfx { |
17 class Font; | 19 class Font; |
| 20 class SlideAnimation; |
18 } | 21 } |
19 | 22 |
20 namespace ui { | 23 namespace ui { |
21 class ComboboxModel; | 24 class ComboboxModel; |
22 } | 25 } |
23 | 26 |
24 namespace views { | 27 namespace views { |
25 | 28 |
26 class ComboboxListener; | 29 class ComboboxListener; |
| 30 class ComboboxMenuRunner; |
| 31 class CustomButton; |
27 class FocusableBorder; | 32 class FocusableBorder; |
28 class MenuRunner; | 33 class MenuRunner; |
| 34 class MenuRunnerCore; |
| 35 class Painter; |
29 class PrefixSelector; | 36 class PrefixSelector; |
30 | 37 |
31 // A non-editable combobox (aka a drop-down list or selector). | 38 // A non-editable combobox (aka a drop-down list or selector). |
32 class VIEWS_EXPORT Combobox | 39 // Combobox has two distinct parts, the drop down arrow and the text. When the |
33 : public MenuDelegate, | 40 // user clicks on the text the drop down is either shown |
34 public PrefixDelegate, | 41 // (STYLE_SHOW_DROP_DOWN_ON_CLICK) or the listener is notified |
35 public ui::ComboboxModelObserver { | 42 // (STYLE_NOTIFY_ON_CLICK). |
| 43 class VIEWS_EXPORT Combobox : public MenuDelegate, |
| 44 public PrefixDelegate, |
| 45 public ui::ComboboxModelObserver, |
| 46 public ButtonListener { |
36 public: | 47 public: |
| 48 // The style of the combobox. |
| 49 enum Style { |
| 50 STYLE_SHOW_DROP_DOWN_ON_CLICK, |
| 51 STYLE_NOTIFY_ON_CLICK, |
| 52 }; |
| 53 |
37 // The combobox's class name. | 54 // The combobox's class name. |
38 static const char kViewClassName[]; | 55 static const char kViewClassName[]; |
39 | 56 |
40 // |model| is not owned by the combobox. | 57 // |model| is not owned by the combobox. |
41 explicit Combobox(ui::ComboboxModel* model); | 58 explicit Combobox(ui::ComboboxModel* model); |
42 virtual ~Combobox(); | 59 virtual ~Combobox(); |
43 | 60 |
44 static const gfx::Font& GetFont(); | 61 static const gfx::Font& GetFont(); |
45 | 62 |
46 // Sets the listener which will be called when a selection has been made. | 63 // Sets the listener which will be called when a selection has been made. |
47 void set_listener(ComboboxListener* listener) { listener_ = listener; } | 64 void set_listener(ComboboxListener* listener) { listener_ = listener; } |
48 | 65 |
| 66 void SetStyle(Style style); |
| 67 |
49 // Informs the combobox that its model changed. | 68 // Informs the combobox that its model changed. |
50 void ModelChanged(); | 69 void ModelChanged(); |
51 | 70 |
52 // Gets/Sets the selected index. | 71 // Gets/Sets the selected index. |
53 int selected_index() const { return selected_index_; } | 72 int selected_index() const { return selected_index_; } |
54 void SetSelectedIndex(int index); | 73 void SetSelectedIndex(int index); |
55 | 74 |
56 // Looks for the first occurrence of |value| in |model()|. If found, selects | 75 // Looks for the first occurrence of |value| in |model()|. If found, selects |
57 // the found index and returns true. Otherwise simply noops and returns false. | 76 // the found index and returns true. Otherwise simply noops and returns false. |
58 bool SelectValue(const base::string16& value); | 77 bool SelectValue(const base::string16& value); |
59 | 78 |
60 ui::ComboboxModel* model() const { return model_; } | 79 ui::ComboboxModel* model() const { return model_; } |
61 | 80 |
62 // Set the accessible name of the combobox. | 81 // Set the accessible name of the combobox. |
63 void SetAccessibleName(const string16& name); | 82 void SetAccessibleName(const string16& name); |
64 | 83 |
65 // Visually marks the combobox as having an invalid value selected. | 84 // Visually marks the combobox as having an invalid value selected. |
66 // When invalid, it paints with white text on a red background. | 85 // When invalid, it paints with white text on a red background. |
67 // Callers are responsible for restoring validity with selection changes. | 86 // Callers are responsible for restoring validity with selection changes. |
68 void SetInvalid(bool invalid); | 87 void SetInvalid(bool invalid); |
69 bool invalid() const { return invalid_; } | 88 bool invalid() const { return invalid_; } |
70 | 89 |
| 90 // Sets an alternative implementation of the combobox dropdown menu runner. |
| 91 // This combobox takes the ownership of |menu_runner_core|. This setter is |
| 92 // intended to be used for testing. |
| 93 void SetMenuRunnerCore(MenuRunnerCore* menu_runner_core); |
| 94 |
71 // Overridden from View: | 95 // Overridden from View: |
72 virtual gfx::Size GetPreferredSize() OVERRIDE; | 96 virtual gfx::Size GetPreferredSize() OVERRIDE; |
73 virtual const char* GetClassName() const OVERRIDE; | 97 virtual const char* GetClassName() const OVERRIDE; |
74 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE; | 98 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE; |
75 virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE; | |
76 virtual bool OnMouseDragged(const ui::MouseEvent& mouse_event) OVERRIDE; | |
77 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE; | 99 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE; |
78 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; | 100 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; |
79 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE; | |
80 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 101 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
81 virtual void OnFocus() OVERRIDE; | 102 virtual void OnFocus() OVERRIDE; |
82 virtual void OnBlur() OVERRIDE; | 103 virtual void OnBlur() OVERRIDE; |
83 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; | 104 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; |
84 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; | 105 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; |
| 106 virtual void Layout() OVERRIDE; |
85 | 107 |
86 // Overridden from MenuDelegate: | 108 // Overridden from MenuDelegate: |
87 virtual bool IsItemChecked(int id) const OVERRIDE; | 109 virtual bool IsItemChecked(int id) const OVERRIDE; |
88 virtual bool IsCommandEnabled(int id) const OVERRIDE; | 110 virtual bool IsCommandEnabled(int id) const OVERRIDE; |
89 virtual void ExecuteCommand(int id) OVERRIDE; | 111 virtual void ExecuteCommand(int id) OVERRIDE; |
90 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE; | 112 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE; |
91 | 113 |
92 // Overridden from PrefixDelegate: | 114 // Overridden from PrefixDelegate: |
93 virtual int GetRowCount() OVERRIDE; | 115 virtual int GetRowCount() OVERRIDE; |
94 virtual int GetSelectedRow() OVERRIDE; | 116 virtual int GetSelectedRow() OVERRIDE; |
95 virtual void SetSelectedRow(int row) OVERRIDE; | 117 virtual void SetSelectedRow(int row) OVERRIDE; |
96 virtual string16 GetTextForRow(int row) OVERRIDE; | 118 virtual string16 GetTextForRow(int row) OVERRIDE; |
97 | 119 |
98 // Overriden from ComboboxModelObserver: | 120 // Overriden from ComboboxModelObserver: |
99 virtual void OnModelChanged() OVERRIDE; | 121 virtual void OnModelChanged() OVERRIDE; |
100 | 122 |
| 123 // Overriden from ButtonListener: |
| 124 virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE; |
| 125 |
101 private: | 126 private: |
102 // Updates the combobox's content from its model. | 127 // Updates the combobox's content from its model. |
103 void UpdateFromModel(); | 128 void UpdateFromModel(); |
104 | 129 |
| 130 // Updates the border according to the current state. |
| 131 void UpdateBorder(); |
| 132 |
105 // Given bounds within our View, this helper mirrors the bounds if necessary. | 133 // Given bounds within our View, this helper mirrors the bounds if necessary. |
106 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; | 134 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; |
107 | 135 |
108 // Draw the selected value of the drop down list | 136 // Draws the selected value of the drop down list |
109 void PaintText(gfx::Canvas* canvas); | 137 void PaintText(gfx::Canvas* canvas); |
110 | 138 |
| 139 // Draws the button images. |
| 140 void PaintButtons(gfx::Canvas* canvas); |
| 141 |
111 // Show the drop down list | 142 // Show the drop down list |
112 void ShowDropDownMenu(ui::MenuSourceType source_type); | 143 void ShowDropDownMenu(ui::MenuSourceType source_type); |
113 | 144 |
114 // Called when the selection is changed by the user. | 145 // Called when the selection is changed by the user. |
115 void OnSelectionChanged(); | 146 void OnSelectionChanged(); |
116 | 147 |
117 // Converts a menu command ID to a menu item index. | 148 // Converts a menu command ID to a menu item index. |
118 int MenuCommandToIndex(int menu_command_id) const; | 149 int MenuCommandToIndex(int menu_command_id) const; |
119 | 150 |
| 151 int GetDisclosureArrowLeftPadding() const; |
| 152 int GetDisclosureArrowRightPadding() const; |
| 153 |
| 154 // Handles the clicking event. |
| 155 void HandleClickEvent(); |
| 156 |
120 // Our model. Not owned. | 157 // Our model. Not owned. |
121 ui::ComboboxModel* model_; | 158 ui::ComboboxModel* model_; |
122 | 159 |
| 160 // The visual style of this combobox. |
| 161 Style style_; |
| 162 |
123 // Our listener. Not owned. Notified when the selected index change. | 163 // Our listener. Not owned. Notified when the selected index change. |
124 ComboboxListener* listener_; | 164 ComboboxListener* listener_; |
125 | 165 |
126 // The current selected index; -1 and means no selection. | 166 // The current selected index; -1 and means no selection. |
127 int selected_index_; | 167 int selected_index_; |
128 | 168 |
129 // True when the selection is visually denoted as invalid. | 169 // True when the selection is visually denoted as invalid. |
130 bool invalid_; | 170 bool invalid_; |
131 | 171 |
132 // The accessible name of this combobox. | 172 // The accessible name of this combobox. |
133 string16 accessible_name_; | 173 string16 accessible_name_; |
134 | 174 |
135 // A helper used to select entries by keyboard input. | 175 // A helper used to select entries by keyboard input. |
136 scoped_ptr<PrefixSelector> selector_; | 176 scoped_ptr<PrefixSelector> selector_; |
137 | 177 |
138 // The reference to the border class. The object is owned by View::border_. | |
139 FocusableBorder* text_border_; | |
140 | |
141 // The disclosure arrow next to the currently selected item from the list. | 178 // The disclosure arrow next to the currently selected item from the list. |
142 const gfx::ImageSkia* disclosure_arrow_; | 179 const gfx::ImageSkia* disclosure_arrow_; |
143 | 180 |
144 // Responsible for showing the context menu. | 181 // Responsible for showing the context menu. |
145 scoped_ptr<MenuRunner> dropdown_list_menu_runner_; | 182 scoped_ptr<MenuRunner> dropdown_list_menu_runner_; |
146 | 183 |
| 184 // The alternative implementation of a function to run the dropdown menu. |
| 185 scoped_ptr<MenuRunnerCore> menu_runner_core_; |
| 186 |
147 // Is the drop down list showing | 187 // Is the drop down list showing |
148 bool dropdown_open_; | 188 bool dropdown_open_; |
149 | 189 |
150 // Like MenuButton, we use a time object in order to keep track of when the | 190 // Like MenuButton, we use a time object in order to keep track of when the |
151 // combobox was closed. The time is used for simulating menu behavior; that | 191 // combobox was closed. The time is used for simulating menu behavior; that |
152 // is, if the menu is shown and the button is pressed, we need to close the | 192 // is, if the menu is shown and the button is pressed, we need to close the |
153 // menu. There is no clean way to get the second click event because the | 193 // menu. There is no clean way to get the second click event because the |
154 // menu is displayed using a modal loop and, unlike regular menus in Windows, | 194 // menu is displayed using a modal loop and, unlike regular menus in Windows, |
155 // the button is not part of the displayed menu. | 195 // the button is not part of the displayed menu. |
156 base::Time closed_time_; | 196 base::Time closed_time_; |
157 | 197 |
158 // The maximum dimensions of the content in the dropdown | 198 // The maximum dimensions of the content in the dropdown |
159 gfx::Size content_size_; | 199 gfx::Size content_size_; |
160 | 200 |
| 201 // The painters or images that are used when |style_| is STYLE_BUTTONS. The |
| 202 // first index means the state of unfocused or focused. |
| 203 // The images are owned by ResourceBundle. |
| 204 scoped_ptr<Painter> body_button_painters_[2][Button::STATE_COUNT]; |
| 205 std::vector<const gfx::ImageSkia*> |
| 206 menu_button_images_[2][Button::STATE_COUNT]; |
| 207 |
| 208 // The transparent buttons to handle events and render buttons. These are |
| 209 // placed on top of this combobox as child views, accept event and manage the |
| 210 // button states. These are not rendered but when |style_| is |
| 211 // STYLE_NOTIFY_ON_CLICK, a Combobox renders the button images according to |
| 212 // these button states. |
| 213 // The base View takes the ownerships of these as child views. |
| 214 CustomButton* text_button_; |
| 215 CustomButton* arrow_button_; |
| 216 |
161 DISALLOW_COPY_AND_ASSIGN(Combobox); | 217 DISALLOW_COPY_AND_ASSIGN(Combobox); |
162 }; | 218 }; |
163 | 219 |
164 } // namespace views | 220 } // namespace views |
165 | 221 |
166 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ | 222 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ |
OLD | NEW |