| 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> | |
| 9 | |
| 10 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/strings/string16.h" |
| 11 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 12 #include "ui/base/models/combobox_model_observer.h" | |
| 13 #include "ui/gfx/animation/animation_delegate.h" | |
| 14 #include "ui/gfx/native_widget_types.h" | |
| 15 #include "ui/views/controls/button/button.h" | 11 #include "ui/views/controls/button/button.h" |
| 16 #include "ui/views/controls/menu/menu_delegate.h" | |
| 17 #include "ui/views/controls/prefix_delegate.h" | 12 #include "ui/views/controls/prefix_delegate.h" |
| 18 | 13 |
| 19 namespace gfx { | 14 namespace gfx { |
| 20 class FontList; | 15 class FontList; |
| 21 class SlideAnimation; | |
| 22 } | 16 } |
| 23 | 17 |
| 24 namespace ui { | 18 namespace ui { |
| 25 class ComboboxModel; | 19 class ComboboxModel; |
| 20 class MenuModel; |
| 26 } | 21 } |
| 27 | 22 |
| 28 namespace views { | 23 namespace views { |
| 24 namespace test { |
| 25 class ComboboxTestApi; |
| 26 } |
| 29 | 27 |
| 30 class ComboboxListener; | 28 class ComboboxListener; |
| 31 class ComboboxMenuRunner; | |
| 32 class CustomButton; | 29 class CustomButton; |
| 33 class FocusableBorder; | |
| 34 class MenuRunner; | 30 class MenuRunner; |
| 35 class MenuRunnerHandler; | |
| 36 class Painter; | 31 class Painter; |
| 37 class PrefixSelector; | 32 class PrefixSelector; |
| 38 | 33 |
| 39 // A non-editable combobox (aka a drop-down list or selector). | 34 // A non-editable combobox (aka a drop-down list or selector). |
| 40 // Combobox has two distinct parts, the drop down arrow and the text. Combobox | 35 // Combobox has two distinct parts, the drop down arrow and the text. Combobox |
| 41 // offers two distinct behaviors: | 36 // offers two distinct behaviors: |
| 42 // * STYLE_NORMAL: typical combobox, clicking on the text and/or button shows | 37 // * STYLE_NORMAL: typical combobox, clicking on the text and/or button shows |
| 43 // the drop down, arrow keys change selection, selected index can be changed by | 38 // the drop down, arrow keys change selection, selected index can be changed by |
| 44 // the user to something other than the first item. | 39 // the user to something other than the first item. |
| 45 // * STYLE_ACTION: clicking on the text notifies the listener. The menu can be | 40 // * STYLE_ACTION: clicking on the text notifies the listener. The menu can be |
| 46 // shown only by clicking on the arrow. The selected index is always reverted to | 41 // shown only by clicking on the arrow. The selected index is always reverted to |
| 47 // 0 after the listener is notified. | 42 // 0 after the listener is notified. |
| 48 class VIEWS_EXPORT Combobox : public MenuDelegate, | 43 class VIEWS_EXPORT Combobox : public PrefixDelegate, public ButtonListener { |
| 49 public PrefixDelegate, | |
| 50 public ui::ComboboxModelObserver, | |
| 51 public ButtonListener { | |
| 52 public: | 44 public: |
| 53 // The style of the combobox. | 45 // The style of the combobox. |
| 54 enum Style { | 46 enum Style { |
| 55 STYLE_NORMAL, | 47 STYLE_NORMAL, |
| 56 STYLE_ACTION, | 48 STYLE_ACTION, |
| 57 }; | 49 }; |
| 58 | 50 |
| 59 // The combobox's class name. | 51 // The combobox's class name. |
| 60 static const char kViewClassName[]; | 52 static const char kViewClassName[]; |
| 61 | 53 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 const char* GetClassName() const override; | 89 const char* GetClassName() const override; |
| 98 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) override; | 90 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) override; |
| 99 bool OnKeyPressed(const ui::KeyEvent& e) override; | 91 bool OnKeyPressed(const ui::KeyEvent& e) override; |
| 100 bool OnKeyReleased(const ui::KeyEvent& e) override; | 92 bool OnKeyReleased(const ui::KeyEvent& e) override; |
| 101 void OnPaint(gfx::Canvas* canvas) override; | 93 void OnPaint(gfx::Canvas* canvas) override; |
| 102 void OnFocus() override; | 94 void OnFocus() override; |
| 103 void OnBlur() override; | 95 void OnBlur() override; |
| 104 void GetAccessibleState(ui::AXViewState* state) override; | 96 void GetAccessibleState(ui::AXViewState* state) override; |
| 105 void Layout() override; | 97 void Layout() override; |
| 106 | 98 |
| 107 // Overridden from MenuDelegate: | |
| 108 bool IsItemChecked(int id) const override; | |
| 109 bool IsCommandEnabled(int id) const override; | |
| 110 void ExecuteCommand(int id) override; | |
| 111 bool GetAccelerator(int id, ui::Accelerator* accelerator) const override; | |
| 112 | |
| 113 // Overridden from PrefixDelegate: | 99 // Overridden from PrefixDelegate: |
| 114 int GetRowCount() override; | 100 int GetRowCount() override; |
| 115 int GetSelectedRow() override; | 101 int GetSelectedRow() override; |
| 116 void SetSelectedRow(int row) override; | 102 void SetSelectedRow(int row) override; |
| 117 base::string16 GetTextForRow(int row) override; | 103 base::string16 GetTextForRow(int row) override; |
| 118 | 104 |
| 119 // Overriden from ComboboxModelObserver: | |
| 120 void OnComboboxModelChanged(ui::ComboboxModel* model) override; | |
| 121 | |
| 122 // Overriden from ButtonListener: | 105 // Overriden from ButtonListener: |
| 123 void ButtonPressed(Button* sender, const ui::Event& event) override; | 106 void ButtonPressed(Button* sender, const ui::Event& event) override; |
| 124 | 107 |
| 125 private: | 108 private: |
| 126 FRIEND_TEST_ALL_PREFIXES(ComboboxTest, Click); | 109 friend class test::ComboboxTestApi; |
| 127 FRIEND_TEST_ALL_PREFIXES(ComboboxTest, ClickButDisabled); | |
| 128 FRIEND_TEST_ALL_PREFIXES(ComboboxTest, NotifyOnClickWithMouse); | |
| 129 FRIEND_TEST_ALL_PREFIXES(ComboboxTest, ContentWidth); | |
| 130 | 110 |
| 131 // Updates the combobox's content from its model. | 111 class ComboboxMenuModelAdapter; |
| 132 void UpdateFromModel(); | |
| 133 | 112 |
| 134 // Updates the border according to the current state. | 113 // Updates the border according to the current state. |
| 135 void UpdateBorder(); | 114 void UpdateBorder(); |
| 136 | 115 |
| 137 // Given bounds within our View, this helper mirrors the bounds if necessary. | 116 // Given bounds within our View, this helper mirrors the bounds if necessary. |
| 138 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; | 117 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; |
| 139 | 118 |
| 140 // Draws the selected value of the drop down list | 119 // Draws the selected value of the drop down list |
| 141 void PaintText(gfx::Canvas* canvas); | 120 void PaintText(gfx::Canvas* canvas); |
| 142 | 121 |
| 143 // Draws the button images. | 122 // Draws the button images. |
| 144 void PaintButtons(gfx::Canvas* canvas); | 123 void PaintButtons(gfx::Canvas* canvas); |
| 145 | 124 |
| 146 // Show the drop down list | 125 // Show the drop down list |
| 147 void ShowDropDownMenu(ui::MenuSourceType source_type); | 126 void ShowDropDownMenu(ui::MenuSourceType source_type); |
| 148 | 127 |
| 149 // Called when the selection is changed by the user. | 128 // Called when the selection is changed by the user. |
| 150 void OnPerformAction(); | 129 void OnPerformAction(); |
| 151 void NotifyPerformAction(); | |
| 152 void AfterPerformAction(); | |
| 153 | |
| 154 // Converts a menu command ID to a menu item index. | |
| 155 int MenuCommandToIndex(int menu_command_id) const; | |
| 156 | 130 |
| 157 int GetDisclosureArrowLeftPadding() const; | 131 int GetDisclosureArrowLeftPadding() const; |
| 158 int GetDisclosureArrowRightPadding() const; | 132 int GetDisclosureArrowRightPadding() const; |
| 159 | 133 |
| 160 // Returns the size of the disclosure arrow. | 134 // Returns the size of the disclosure arrow. |
| 161 gfx::Size ArrowSize() const; | 135 gfx::Size ArrowSize() const; |
| 162 | 136 |
| 137 // Finds the size of the largest menu label or, for STYLE_ACTION, the size of |
| 138 // the selected label. |
| 139 gfx::Size GetContentSize() const; |
| 140 |
| 163 // Handles the clicking event. | 141 // Handles the clicking event. |
| 164 void HandleClickEvent(); | 142 void HandleClickEvent(); |
| 165 | 143 |
| 166 PrefixSelector* GetPrefixSelector(); | 144 PrefixSelector* GetPrefixSelector(); |
| 167 | 145 |
| 168 // Our model. Not owned. | 146 // Our model. Not owned. |
| 169 ui::ComboboxModel* model_; | 147 ui::ComboboxModel* model_; |
| 170 | 148 |
| 171 // The visual style of this combobox. | 149 // The visual style of this combobox. |
| 172 Style style_; | 150 Style style_; |
| 173 | 151 |
| 174 // Our listener. Not owned. Notified when the selected index change. | 152 // Our listener. Not owned. Notified when the selected index change. |
| 175 ComboboxListener* listener_; | 153 ComboboxListener* listener_; |
| 176 | 154 |
| 177 // The current selected index; -1 and means no selection. | 155 // The current selected index; -1 and means no selection. |
| 178 int selected_index_; | 156 int selected_index_; |
| 179 | 157 |
| 180 // True when the selection is visually denoted as invalid. | 158 // True when the selection is visually denoted as invalid. |
| 181 bool invalid_; | 159 bool invalid_; |
| 182 | 160 |
| 183 // The accessible name of this combobox. | 161 // The accessible name of this combobox. |
| 184 base::string16 accessible_name_; | 162 base::string16 accessible_name_; |
| 185 | 163 |
| 186 // A helper used to select entries by keyboard input. | 164 // A helper used to select entries by keyboard input. |
| 187 scoped_ptr<PrefixSelector> selector_; | 165 scoped_ptr<PrefixSelector> selector_; |
| 188 | 166 |
| 189 // Responsible for showing the context menu. | 167 // Adapts a ComboboxModel for use by a views MenuRunner. |
| 190 scoped_ptr<MenuRunner> dropdown_list_menu_runner_; | 168 scoped_ptr<ui::MenuModel> menu_model_adapter_; |
| 191 | |
| 192 // Weak. Owned by dropdown_list_menu_runner_. | |
| 193 MenuItemView* menu_; | |
| 194 | |
| 195 // Is the drop down list showing | |
| 196 bool dropdown_open_; | |
| 197 | 169 |
| 198 // Like MenuButton, we use a time object in order to keep track of when the | 170 // Like MenuButton, we use a time object in order to keep track of when the |
| 199 // combobox was closed. The time is used for simulating menu behavior; that | 171 // combobox was closed. The time is used for simulating menu behavior; that |
| 200 // is, if the menu is shown and the button is pressed, we need to close the | 172 // is, if the menu is shown and the button is pressed, we need to close the |
| 201 // menu. There is no clean way to get the second click event because the | 173 // menu. There is no clean way to get the second click event because the |
| 202 // menu is displayed using a modal loop and, unlike regular menus in Windows, | 174 // menu is displayed using a modal loop and, unlike regular menus in Windows, |
| 203 // the button is not part of the displayed menu. | 175 // the button is not part of the displayed menu. |
| 204 base::Time closed_time_; | 176 base::Time closed_time_; |
| 205 | 177 |
| 206 // The maximum dimensions of the content in the dropdown | 178 // The maximum dimensions of the content in the dropdown. |
| 207 mutable gfx::Size content_size_; | 179 gfx::Size content_size_; |
| 208 | 180 |
| 209 // The painters or images that are used when |style_| is STYLE_BUTTONS. The | 181 // The painters or images that are used when |style_| is STYLE_BUTTONS. The |
| 210 // first index means the state of unfocused or focused. | 182 // first index means the state of unfocused or focused. |
| 211 // The images are owned by ResourceBundle. | 183 // The images are owned by ResourceBundle. |
| 212 scoped_ptr<Painter> body_button_painters_[2][Button::STATE_COUNT]; | 184 scoped_ptr<Painter> body_button_painters_[2][Button::STATE_COUNT]; |
| 213 std::vector<const gfx::ImageSkia*> | 185 std::vector<const gfx::ImageSkia*> |
| 214 menu_button_images_[2][Button::STATE_COUNT]; | 186 menu_button_images_[2][Button::STATE_COUNT]; |
| 215 | 187 |
| 216 // The transparent buttons to handle events and render buttons. These are | 188 // The transparent buttons to handle events and render buttons. These are |
| 217 // placed on top of this combobox as child views, accept event and manage the | 189 // placed on top of this combobox as child views, accept event and manage the |
| 218 // button states. These are not rendered but when |style_| is | 190 // button states. These are not rendered but when |style_| is |
| 219 // STYLE_NOTIFY_ON_CLICK, a Combobox renders the button images according to | 191 // STYLE_NOTIFY_ON_CLICK, a Combobox renders the button images according to |
| 220 // these button states. | 192 // these button states. |
| 221 // The base View takes the ownerships of these as child views. | 193 // The base View takes the ownerships of these as child views. |
| 222 CustomButton* text_button_; | 194 CustomButton* text_button_; |
| 223 CustomButton* arrow_button_; | 195 CustomButton* arrow_button_; |
| 224 | 196 |
| 197 // Set while the dropdown is showing. Ensures the menu is closed if |this| is |
| 198 // destroyed. |
| 199 scoped_ptr<views::MenuRunner> menu_runner_; |
| 200 |
| 225 // Used for making calbacks. | 201 // Used for making calbacks. |
| 226 base::WeakPtrFactory<Combobox> weak_ptr_factory_; | 202 base::WeakPtrFactory<Combobox> weak_ptr_factory_; |
| 227 | 203 |
| 228 DISALLOW_COPY_AND_ASSIGN(Combobox); | 204 DISALLOW_COPY_AND_ASSIGN(Combobox); |
| 229 }; | 205 }; |
| 230 | 206 |
| 231 } // namespace views | 207 } // namespace views |
| 232 | 208 |
| 233 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ | 209 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ |
| OLD | NEW |