| 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 Painter; |
| 29 class PrefixSelector; | 35 class PrefixSelector; |
| 30 | 36 |
| 31 // A non-editable combobox (aka a drop-down list or selector). | 37 // A non-editable combobox (aka a drop-down list or selector). |
| 32 class VIEWS_EXPORT Combobox | 38 // Combobox has two distinct parts, the drop down arrow and the text. When the |
| 33 : public MenuDelegate, | 39 // user clicks on the text the drop down is either shown |
| 34 public PrefixDelegate, | 40 // (STYLE_SHOW_DROP_DOWN_ON_CLICK) or the listener is notified |
| 35 public ui::ComboboxModelObserver { | 41 // (STYLE_NOTIFY_ON_CLICK). |
| 42 class VIEWS_EXPORT Combobox : public MenuDelegate, |
| 43 public PrefixDelegate, |
| 44 public ui::ComboboxModelObserver, |
| 45 public ButtonListener { |
| 36 public: | 46 public: |
| 47 // The style of the combobox. |
| 48 enum Style { |
| 49 STYLE_SHOW_DROP_DOWN_ON_CLICK, |
| 50 STYLE_NOTIFY_ON_CLICK, |
| 51 }; |
| 52 |
| 37 // The combobox's class name. | 53 // The combobox's class name. |
| 38 static const char kViewClassName[]; | 54 static const char kViewClassName[]; |
| 39 | 55 |
| 40 // |model| is not owned by the combobox. | 56 // |model| is not owned by the combobox. |
| 41 explicit Combobox(ui::ComboboxModel* model); | 57 explicit Combobox(ui::ComboboxModel* model); |
| 42 virtual ~Combobox(); | 58 virtual ~Combobox(); |
| 43 | 59 |
| 44 static const gfx::Font& GetFont(); | 60 static const gfx::Font& GetFont(); |
| 45 | 61 |
| 46 // Sets the listener which will be called when a selection has been made. | 62 // Sets the listener which will be called when a selection has been made. |
| 47 void set_listener(ComboboxListener* listener) { listener_ = listener; } | 63 void set_listener(ComboboxListener* listener) { listener_ = listener; } |
| 48 | 64 |
| 65 void SetStyle(Style style); |
| 66 |
| 49 // Informs the combobox that its model changed. | 67 // Informs the combobox that its model changed. |
| 50 void ModelChanged(); | 68 void ModelChanged(); |
| 51 | 69 |
| 52 // Gets/Sets the selected index. | 70 // Gets/Sets the selected index. |
| 53 int selected_index() const { return selected_index_; } | 71 int selected_index() const { return selected_index_; } |
| 54 void SetSelectedIndex(int index); | 72 void SetSelectedIndex(int index); |
| 55 | 73 |
| 56 // Looks for the first occurrence of |value| in |model()|. If found, selects | 74 // 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. | 75 // the found index and returns true. Otherwise simply noops and returns false. |
| 58 bool SelectValue(const base::string16& value); | 76 bool SelectValue(const base::string16& value); |
| 59 | 77 |
| 60 ui::ComboboxModel* model() const { return model_; } | 78 ui::ComboboxModel* model() const { return model_; } |
| 61 | 79 |
| 62 // Set the accessible name of the combobox. | 80 // Set the accessible name of the combobox. |
| 63 void SetAccessibleName(const string16& name); | 81 void SetAccessibleName(const string16& name); |
| 64 | 82 |
| 65 // Visually marks the combobox as having an invalid value selected. | 83 // Visually marks the combobox as having an invalid value selected. |
| 66 // When invalid, it paints with white text on a red background. | 84 // When invalid, it paints with white text on a red background. |
| 67 // Callers are responsible for restoring validity with selection changes. | 85 // Callers are responsible for restoring validity with selection changes. |
| 68 void SetInvalid(bool invalid); | 86 void SetInvalid(bool invalid); |
| 69 bool invalid() const { return invalid_; } | 87 bool invalid() const { return invalid_; } |
| 70 | 88 |
| 89 // Sets the menu runner for the combobox. This combobox takes the ownership of |
| 90 // |combobox_menu_runner|. This setter is intended to be used for testing. |
| 91 void SetComboboxMenuRunner(ComboboxMenuRunner* combobox_menu_runner); |
| 92 |
| 71 // Overridden from View: | 93 // Overridden from View: |
| 72 virtual gfx::Size GetPreferredSize() OVERRIDE; | 94 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 73 virtual const char* GetClassName() const OVERRIDE; | 95 virtual const char* GetClassName() const OVERRIDE; |
| 74 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE; | 96 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; | 97 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE; |
| 78 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; | 98 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; |
| 79 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE; | |
| 80 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 99 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
| 81 virtual void OnFocus() OVERRIDE; | 100 virtual void OnFocus() OVERRIDE; |
| 82 virtual void OnBlur() OVERRIDE; | 101 virtual void OnBlur() OVERRIDE; |
| 83 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; | 102 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; |
| 84 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; | 103 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; |
| 104 virtual void Layout() OVERRIDE; |
| 85 | 105 |
| 86 // Overridden from MenuDelegate: | 106 // Overridden from MenuDelegate: |
| 87 virtual bool IsItemChecked(int id) const OVERRIDE; | 107 virtual bool IsItemChecked(int id) const OVERRIDE; |
| 88 virtual bool IsCommandEnabled(int id) const OVERRIDE; | 108 virtual bool IsCommandEnabled(int id) const OVERRIDE; |
| 89 virtual void ExecuteCommand(int id) OVERRIDE; | 109 virtual void ExecuteCommand(int id) OVERRIDE; |
| 90 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE; | 110 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE; |
| 91 | 111 |
| 92 // Overridden from PrefixDelegate: | 112 // Overridden from PrefixDelegate: |
| 93 virtual int GetRowCount() OVERRIDE; | 113 virtual int GetRowCount() OVERRIDE; |
| 94 virtual int GetSelectedRow() OVERRIDE; | 114 virtual int GetSelectedRow() OVERRIDE; |
| 95 virtual void SetSelectedRow(int row) OVERRIDE; | 115 virtual void SetSelectedRow(int row) OVERRIDE; |
| 96 virtual string16 GetTextForRow(int row) OVERRIDE; | 116 virtual string16 GetTextForRow(int row) OVERRIDE; |
| 97 | 117 |
| 98 // Overriden from ComboboxModelObserver: | 118 // Overriden from ComboboxModelObserver: |
| 99 virtual void OnModelChanged() OVERRIDE; | 119 virtual void OnModelChanged() OVERRIDE; |
| 100 | 120 |
| 121 // Overriden from ButtonListener: |
| 122 virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE; |
| 123 |
| 101 private: | 124 private: |
| 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 |
| 182 // The implementation of a function to run the dropdown menu. |
| 183 scoped_ptr<ComboboxMenuRunner> combobox_menu_runner_; |
| 184 |
| 147 // Is the drop down list showing | 185 // Is the drop down list showing |
| 148 bool dropdown_open_; | 186 bool dropdown_open_; |
| 149 | 187 |
| 150 // Like MenuButton, we use a time object in order to keep track of when the | 188 // 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 | 189 // 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 | 190 // 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 | 191 // 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, | 192 // menu is displayed using a modal loop and, unlike regular menus in Windows, |
| 155 // the button is not part of the displayed menu. | 193 // the button is not part of the displayed menu. |
| 156 base::Time closed_time_; | 194 base::Time closed_time_; |
| 157 | 195 |
| 158 // The maximum dimensions of the content in the dropdown | 196 // The maximum dimensions of the content in the dropdown |
| 159 gfx::Size content_size_; | 197 gfx::Size content_size_; |
| 160 | 198 |
| 199 // The painters or images that are used when |style_| is STYLE_BUTTONS. The |
| 200 // first index means the state of unfocused or focused. |
| 201 // The images are owned by ResourceBundle. |
| 202 scoped_ptr<Painter> body_button_painters_[2][Button::STATE_COUNT]; |
| 203 std::vector<const gfx::ImageSkia*> |
| 204 menu_button_images_[2][Button::STATE_COUNT]; |
| 205 |
| 206 // The transparent buttons to handle events and render buttons. These are |
| 207 // placed on top of this combobox as child views, accept event and manage the |
| 208 // button states. These are not rendered but when |style_| is |
| 209 // STYLE_NOTIFY_ON_CLICK, a Combobox renders the button images according to |
| 210 // these button states. |
| 211 // The base View takes the ownerships of these as child views. |
| 212 CustomButton* text_button_; |
| 213 CustomButton* arrow_button_; |
| 214 |
| 161 DISALLOW_COPY_AND_ASSIGN(Combobox); | 215 DISALLOW_COPY_AND_ASSIGN(Combobox); |
| 162 }; | 216 }; |
| 163 | 217 |
| 164 } // namespace views | 218 } // namespace views |
| 165 | 219 |
| 166 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ | 220 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ |
| OLD | NEW |