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