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 CustomButton; | |
27 class FocusableBorder; | 31 class FocusableBorder; |
28 class MenuRunner; | 32 class MenuRunner; |
29 class PrefixSelector; | 33 class PrefixSelector; |
30 | 34 |
31 // A non-editable combobox (aka a drop-down list or selector). | 35 // A non-editable combobox (aka a drop-down list or selector). |
32 class VIEWS_EXPORT Combobox | 36 // Combobox has two distinct parts, the drop down arrow and the text. When the |
33 : public MenuDelegate, | 37 // user clicks on the text the drop down is either shown |
34 public PrefixDelegate, | 38 // (STYLE_SHOW_DROP_DOWN_ON_CLICK) or the listener is notified |
35 public ui::ComboboxModelObserver { | 39 // (STYLE_NOTIFY_ON_CLICK). |
40 class VIEWS_EXPORT Combobox : public MenuDelegate, | |
41 public PrefixDelegate, | |
42 public ui::ComboboxModelObserver, | |
43 public ButtonListener { | |
36 public: | 44 public: |
45 // The style of the combobox. | |
46 enum Style { | |
47 STYLE_SHOW_DROP_DOWN_ON_CLICK, | |
48 STYLE_NOTIFY_ON_CLICK, | |
49 }; | |
50 | |
37 // The combobox's class name. | 51 // The combobox's class name. |
38 static const char kViewClassName[]; | 52 static const char kViewClassName[]; |
39 | 53 |
40 // |model| is not owned by the combobox. | 54 // |model| is not owned by the combobox. |
41 explicit Combobox(ui::ComboboxModel* model); | 55 explicit Combobox(ui::ComboboxModel* model); |
42 virtual ~Combobox(); | 56 virtual ~Combobox(); |
43 | 57 |
44 static const gfx::Font& GetFont(); | 58 static const gfx::Font& GetFont(); |
45 | 59 |
46 // Sets the listener which will be called when a selection has been made. | 60 // Sets the listener which will be called when a selection has been made. |
47 void set_listener(ComboboxListener* listener) { listener_ = listener; } | 61 void set_listener(ComboboxListener* listener) { listener_ = listener; } |
48 | 62 |
63 void SetStyle(Style style); | |
64 | |
49 // Informs the combobox that its model changed. | 65 // Informs the combobox that its model changed. |
50 void ModelChanged(); | 66 void ModelChanged(); |
51 | 67 |
52 // Gets/Sets the selected index. | 68 // Gets/Sets the selected index. |
53 int selected_index() const { return selected_index_; } | 69 int selected_index() const { return selected_index_; } |
54 void SetSelectedIndex(int index); | 70 void SetSelectedIndex(int index); |
55 | 71 |
56 // Looks for the first occurrence of |value| in |model()|. If found, selects | 72 // 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. | 73 // the found index and returns true. Otherwise simply noops and returns false. |
58 bool SelectValue(const base::string16& value); | 74 bool SelectValue(const base::string16& value); |
(...skipping 16 matching lines...) Expand all Loading... | |
75 virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE; | 91 virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE; |
76 virtual bool OnMouseDragged(const ui::MouseEvent& mouse_event) OVERRIDE; | 92 virtual bool OnMouseDragged(const ui::MouseEvent& mouse_event) OVERRIDE; |
77 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE; | 93 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE; |
78 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; | 94 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; |
79 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE; | 95 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE; |
80 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 96 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
81 virtual void OnFocus() OVERRIDE; | 97 virtual void OnFocus() OVERRIDE; |
82 virtual void OnBlur() OVERRIDE; | 98 virtual void OnBlur() OVERRIDE; |
83 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; | 99 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; |
84 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; | 100 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; |
101 virtual void PreferredSizeChanged() OVERRIDE; | |
85 | 102 |
86 // Overridden from MenuDelegate: | 103 // Overridden from MenuDelegate: |
87 virtual bool IsItemChecked(int id) const OVERRIDE; | 104 virtual bool IsItemChecked(int id) const OVERRIDE; |
88 virtual bool IsCommandEnabled(int id) const OVERRIDE; | 105 virtual bool IsCommandEnabled(int id) const OVERRIDE; |
89 virtual void ExecuteCommand(int id) OVERRIDE; | 106 virtual void ExecuteCommand(int id) OVERRIDE; |
90 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE; | 107 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE; |
91 | 108 |
92 // Overridden from PrefixDelegate: | 109 // Overridden from PrefixDelegate: |
93 virtual int GetRowCount() OVERRIDE; | 110 virtual int GetRowCount() OVERRIDE; |
94 virtual int GetSelectedRow() OVERRIDE; | 111 virtual int GetSelectedRow() OVERRIDE; |
95 virtual void SetSelectedRow(int row) OVERRIDE; | 112 virtual void SetSelectedRow(int row) OVERRIDE; |
96 virtual string16 GetTextForRow(int row) OVERRIDE; | 113 virtual string16 GetTextForRow(int row) OVERRIDE; |
97 | 114 |
98 // Overriden from ComboboxModelObserver: | 115 // Overriden from ComboboxModelObserver: |
99 virtual void OnModelChanged() OVERRIDE; | 116 virtual void OnModelChanged() OVERRIDE; |
100 | 117 |
118 // Overriden from ButtonListener: | |
119 virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE; | |
120 | |
101 private: | 121 private: |
102 // Updates the combobox's content from its model. | 122 // Updates the combobox's content from its model. |
103 void UpdateFromModel(); | 123 void UpdateFromModel(); |
104 | 124 |
105 // Given bounds within our View, this helper mirrors the bounds if necessary. | 125 // Given bounds within our View, this helper mirrors the bounds if necessary. |
106 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; | 126 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; |
107 | 127 |
108 // Draw the selected value of the drop down list | 128 // Draws the selected value of the drop down list |
109 void PaintText(gfx::Canvas* canvas); | 129 void PaintText(gfx::Canvas* canvas); |
110 | 130 |
131 // Draws the button images. | |
132 void PaintButtons(gfx::Canvas* canvas); | |
133 | |
111 // Show the drop down list | 134 // Show the drop down list |
112 void ShowDropDownMenu(ui::MenuSourceType source_type); | 135 void ShowDropDownMenu(ui::MenuSourceType source_type); |
113 | 136 |
114 // Called when the selection is changed by the user. | 137 // Called when the selection is changed by the user. |
115 void OnSelectionChanged(); | 138 void OnSelectionChanged(); |
116 | 139 |
117 // Converts a menu command ID to a menu item index. | 140 // Converts a menu command ID to a menu item index. |
118 int MenuCommandToIndex(int menu_command_id) const; | 141 int MenuCommandToIndex(int menu_command_id) const; |
119 | 142 |
143 int GetDisclosureArrowLeftPadding() const; | |
144 int GetDisclosureArrowRightPadding() const; | |
145 | |
146 // Handles the clicking event. | |
147 void HandleClickEvent(); | |
148 | |
120 // Our model. Not owned. | 149 // Our model. Not owned. |
121 ui::ComboboxModel* model_; | 150 ui::ComboboxModel* model_; |
122 | 151 |
152 // The visual style of this combobox. | |
153 Style style_; | |
154 | |
123 // Our listener. Not owned. Notified when the selected index change. | 155 // Our listener. Not owned. Notified when the selected index change. |
124 ComboboxListener* listener_; | 156 ComboboxListener* listener_; |
125 | 157 |
126 // The current selected index; -1 and means no selection. | 158 // The current selected index; -1 and means no selection. |
127 int selected_index_; | 159 int selected_index_; |
128 | 160 |
129 // True when the selection is visually denoted as invalid. | 161 // True when the selection is visually denoted as invalid. |
130 bool invalid_; | 162 bool invalid_; |
131 | 163 |
132 // The accessible name of this combobox. | 164 // The accessible name of this combobox. |
(...skipping 18 matching lines...) Expand all Loading... | |
151 // combobox was closed. The time is used for simulating menu behavior; that | 183 // 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 | 184 // 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 | 185 // 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, | 186 // menu is displayed using a modal loop and, unlike regular menus in Windows, |
155 // the button is not part of the displayed menu. | 187 // the button is not part of the displayed menu. |
156 base::Time closed_time_; | 188 base::Time closed_time_; |
157 | 189 |
158 // The maximum dimensions of the content in the dropdown | 190 // The maximum dimensions of the content in the dropdown |
159 gfx::Size content_size_; | 191 gfx::Size content_size_; |
160 | 192 |
193 // The images that are used when |style_| is STYLE_BUTTONS. The first index | |
194 // means the state of unfocused or focused. | |
195 std::vector<const gfx::ImageSkia*> | |
196 body_button_images_[2][Button::STATE_COUNT]; | |
197 std::vector<const gfx::ImageSkia*> | |
198 menu_button_images_[2][Button::STATE_COUNT]; | |
199 | |
200 // The transparent buttons for button rendering. These are used only when | |
201 // |style_| is STYLE_SHOW_DROP_DOWN_ON_CLICK. These are placed on top of this | |
202 // combobox as child views, accept event and manage the button states. These | |
203 // are not rendered but a Combobox renders the button images according to | |
204 // these button states. | |
205 CustomButton* text_button_; | |
sky
2013/12/03 20:52:30
Document ownership.
hajimehoshi
2013/12/04 06:30:02
Done.
| |
206 CustomButton* arrow_button_; | |
207 | |
161 DISALLOW_COPY_AND_ASSIGN(Combobox); | 208 DISALLOW_COPY_AND_ASSIGN(Combobox); |
162 }; | 209 }; |
163 | 210 |
164 } // namespace views | 211 } // namespace views |
165 | 212 |
166 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ | 213 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ |
OLD | NEW |