| 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 MenuRunnerHandler; |
| 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 |
| 71 // Overridden from View: | 90 // Overridden from View: |
| 72 virtual gfx::Size GetPreferredSize() OVERRIDE; | 91 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 73 virtual const char* GetClassName() const OVERRIDE; | 92 virtual const char* GetClassName() const OVERRIDE; |
| 74 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE; | 93 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; | 94 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE; |
| 78 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; | 95 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; |
| 79 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 Layout() 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: |
| 122 FRIEND_TEST_ALL_PREFIXES(ComboboxTest, Click); |
| 123 FRIEND_TEST_ALL_PREFIXES(ComboboxTest, NotifyOnClickWithMouse); |
| 124 |
| 102 // Updates the combobox's content from its model. | 125 // Updates the combobox's content from its model. |
| 103 void UpdateFromModel(); | 126 void UpdateFromModel(); |
| 104 | 127 |
| 128 // Updates the border according to the current state. |
| 129 void UpdateBorder(); |
| 130 |
| 105 // Given bounds within our View, this helper mirrors the bounds if necessary. | 131 // Given bounds within our View, this helper mirrors the bounds if necessary. |
| 106 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; | 132 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; |
| 107 | 133 |
| 108 // Draw the selected value of the drop down list | 134 // Draws the selected value of the drop down list |
| 109 void PaintText(gfx::Canvas* canvas); | 135 void PaintText(gfx::Canvas* canvas); |
| 110 | 136 |
| 137 // Draws the button images. |
| 138 void PaintButtons(gfx::Canvas* canvas); |
| 139 |
| 111 // Show the drop down list | 140 // Show the drop down list |
| 112 void ShowDropDownMenu(ui::MenuSourceType source_type); | 141 void ShowDropDownMenu(ui::MenuSourceType source_type); |
| 113 | 142 |
| 114 // Called when the selection is changed by the user. | 143 // Called when the selection is changed by the user. |
| 115 void OnSelectionChanged(); | 144 void OnSelectionChanged(); |
| 116 | 145 |
| 117 // Converts a menu command ID to a menu item index. | 146 // Converts a menu command ID to a menu item index. |
| 118 int MenuCommandToIndex(int menu_command_id) const; | 147 int MenuCommandToIndex(int menu_command_id) const; |
| 119 | 148 |
| 149 int GetDisclosureArrowLeftPadding() const; |
| 150 int GetDisclosureArrowRightPadding() const; |
| 151 |
| 152 // Handles the clicking event. |
| 153 void HandleClickEvent(); |
| 154 |
| 120 // Our model. Not owned. | 155 // Our model. Not owned. |
| 121 ui::ComboboxModel* model_; | 156 ui::ComboboxModel* model_; |
| 122 | 157 |
| 158 // The visual style of this combobox. |
| 159 Style style_; |
| 160 |
| 123 // Our listener. Not owned. Notified when the selected index change. | 161 // Our listener. Not owned. Notified when the selected index change. |
| 124 ComboboxListener* listener_; | 162 ComboboxListener* listener_; |
| 125 | 163 |
| 126 // The current selected index; -1 and means no selection. | 164 // The current selected index; -1 and means no selection. |
| 127 int selected_index_; | 165 int selected_index_; |
| 128 | 166 |
| 129 // True when the selection is visually denoted as invalid. | 167 // True when the selection is visually denoted as invalid. |
| 130 bool invalid_; | 168 bool invalid_; |
| 131 | 169 |
| 132 // The accessible name of this combobox. | 170 // The accessible name of this combobox. |
| 133 string16 accessible_name_; | 171 string16 accessible_name_; |
| 134 | 172 |
| 135 // A helper used to select entries by keyboard input. | 173 // A helper used to select entries by keyboard input. |
| 136 scoped_ptr<PrefixSelector> selector_; | 174 scoped_ptr<PrefixSelector> selector_; |
| 137 | 175 |
| 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. | 176 // The disclosure arrow next to the currently selected item from the list. |
| 142 const gfx::ImageSkia* disclosure_arrow_; | 177 const gfx::ImageSkia* disclosure_arrow_; |
| 143 | 178 |
| 144 // Responsible for showing the context menu. | 179 // Responsible for showing the context menu. |
| 145 scoped_ptr<MenuRunner> dropdown_list_menu_runner_; | 180 scoped_ptr<MenuRunner> dropdown_list_menu_runner_; |
| 146 | 181 |
| 147 // Is the drop down list showing | 182 // Is the drop down list showing |
| 148 bool dropdown_open_; | 183 bool dropdown_open_; |
| 149 | 184 |
| 150 // Like MenuButton, we use a time object in order to keep track of when the | 185 // 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 | 186 // 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 | 187 // 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 | 188 // 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, | 189 // menu is displayed using a modal loop and, unlike regular menus in Windows, |
| 155 // the button is not part of the displayed menu. | 190 // the button is not part of the displayed menu. |
| 156 base::Time closed_time_; | 191 base::Time closed_time_; |
| 157 | 192 |
| 158 // The maximum dimensions of the content in the dropdown | 193 // The maximum dimensions of the content in the dropdown |
| 159 gfx::Size content_size_; | 194 gfx::Size content_size_; |
| 160 | 195 |
| 196 // The painters or images that are used when |style_| is STYLE_BUTTONS. The |
| 197 // first index means the state of unfocused or focused. |
| 198 // The images are owned by ResourceBundle. |
| 199 scoped_ptr<Painter> body_button_painters_[2][Button::STATE_COUNT]; |
| 200 std::vector<const gfx::ImageSkia*> |
| 201 menu_button_images_[2][Button::STATE_COUNT]; |
| 202 |
| 203 // The transparent buttons to handle events and render buttons. These are |
| 204 // placed on top of this combobox as child views, accept event and manage the |
| 205 // button states. These are not rendered but when |style_| is |
| 206 // STYLE_NOTIFY_ON_CLICK, a Combobox renders the button images according to |
| 207 // these button states. |
| 208 // The base View takes the ownerships of these as child views. |
| 209 CustomButton* text_button_; |
| 210 CustomButton* arrow_button_; |
| 211 |
| 161 DISALLOW_COPY_AND_ASSIGN(Combobox); | 212 DISALLOW_COPY_AND_ASSIGN(Combobox); |
| 162 }; | 213 }; |
| 163 | 214 |
| 164 } // namespace views | 215 } // namespace views |
| 165 | 216 |
| 166 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ | 217 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ |
| OLD | NEW |