Chromium Code Reviews| 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; |
| 27 class FocusableBorder; | 30 class FocusableBorder; |
| 28 class MenuRunner; | 31 class MenuRunner; |
| 29 class PrefixSelector; | 32 class PrefixSelector; |
| 30 | 33 |
| 31 // A non-editable combobox (aka a drop-down list or selector). | 34 // A non-editable combobox (aka a drop-down list or selector). |
|
sky
2013/12/02 21:28:55
Add description here of different behaviors. Maybe
hajimehoshi
2013/12/03 10:30:52
Done. (also changed enum Style).
| |
| 32 class VIEWS_EXPORT Combobox | 35 class VIEWS_EXPORT Combobox : public MenuDelegate, |
| 33 : public MenuDelegate, | 36 public PrefixDelegate, |
| 34 public PrefixDelegate, | 37 public ui::ComboboxModelObserver, |
| 35 public ui::ComboboxModelObserver { | 38 public gfx::AnimationDelegate { |
| 36 public: | 39 public: |
| 40 // The visual style of the combobox. When the style is STYLE_BUTTONS, this | |
| 41 // combobox becames a clickable buttons. | |
| 42 enum Style { | |
| 43 STYLE_NORMAL, | |
| 44 STYLE_BUTTONS, | |
| 45 }; | |
| 46 | |
| 37 // The combobox's class name. | 47 // The combobox's class name. |
| 38 static const char kViewClassName[]; | 48 static const char kViewClassName[]; |
| 39 | 49 |
| 40 // |model| is not owned by the combobox. | 50 // |model| is not owned by the combobox. |
| 41 explicit Combobox(ui::ComboboxModel* model); | 51 explicit Combobox(ui::ComboboxModel* model); |
| 42 virtual ~Combobox(); | 52 virtual ~Combobox(); |
| 43 | 53 |
| 44 static const gfx::Font& GetFont(); | 54 static const gfx::Font& GetFont(); |
| 45 | 55 |
| 46 // Sets the listener which will be called when a selection has been made. | 56 // Sets the listener which will be called when a selection has been made. |
| 47 void set_listener(ComboboxListener* listener) { listener_ = listener; } | 57 void set_listener(ComboboxListener* listener) { listener_ = listener; } |
| 48 | 58 |
| 59 void SetStyle(Style style); | |
| 60 | |
| 49 // Informs the combobox that its model changed. | 61 // Informs the combobox that its model changed. |
| 50 void ModelChanged(); | 62 void ModelChanged(); |
| 51 | 63 |
| 52 // Gets/Sets the selected index. | 64 // Gets/Sets the selected index. |
| 53 int selected_index() const { return selected_index_; } | 65 int selected_index() const { return selected_index_; } |
| 54 void SetSelectedIndex(int index); | 66 void SetSelectedIndex(int index); |
| 55 | 67 |
| 56 // Looks for the first occurrence of |value| in |model()|. If found, selects | 68 // 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. | 69 // the found index and returns true. Otherwise simply noops and returns false. |
| 58 bool SelectValue(const base::string16& value); | 70 bool SelectValue(const base::string16& value); |
| 59 | 71 |
| 60 ui::ComboboxModel* model() const { return model_; } | 72 ui::ComboboxModel* model() const { return model_; } |
| 61 | 73 |
| 62 // Set the accessible name of the combobox. | 74 // Set the accessible name of the combobox. |
| 63 void SetAccessibleName(const string16& name); | 75 void SetAccessibleName(const string16& name); |
| 64 | 76 |
| 65 // Visually marks the combobox as having an invalid value selected. | 77 // Visually marks the combobox as having an invalid value selected. |
| 66 // When invalid, it paints with white text on a red background. | 78 // When invalid, it paints with white text on a red background. |
| 67 // Callers are responsible for restoring validity with selection changes. | 79 // Callers are responsible for restoring validity with selection changes. |
| 68 void SetInvalid(bool invalid); | 80 void SetInvalid(bool invalid); |
| 69 bool invalid() const { return invalid_; } | 81 bool invalid() const { return invalid_; } |
| 70 | 82 |
| 71 // Overridden from View: | 83 // Overridden from View: |
| 72 virtual gfx::Size GetPreferredSize() OVERRIDE; | 84 virtual gfx::Size GetPreferredSize() OVERRIDE; |
| 73 virtual const char* GetClassName() const OVERRIDE; | 85 virtual const char* GetClassName() const OVERRIDE; |
| 74 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE; | 86 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE; |
| 75 virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE; | 87 virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE; |
| 76 virtual bool OnMouseDragged(const ui::MouseEvent& mouse_event) OVERRIDE; | 88 virtual bool OnMouseDragged(const ui::MouseEvent& mouse_event) OVERRIDE; |
| 89 virtual void OnMouseReleased(const ui::MouseEvent& mouse_event) OVERRIDE; | |
| 90 virtual void OnMouseCaptureLost() OVERRIDE; | |
| 91 virtual void OnMouseEntered(const ui::MouseEvent& mouse_event) OVERRIDE; | |
| 92 virtual void OnMouseExited(const ui::MouseEvent& mouse_event) OVERRIDE; | |
| 93 virtual void OnMouseMoved(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; | 96 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE; |
| 80 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | 97 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; |
| 81 virtual void OnFocus() OVERRIDE; | 98 virtual void OnFocus() OVERRIDE; |
| 82 virtual void OnBlur() OVERRIDE; | 99 virtual void OnBlur() OVERRIDE; |
| 83 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; | 100 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; |
| 84 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; | 101 virtual ui::TextInputClient* GetTextInputClient() 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 AnimationDelegate: | |
| 119 virtual void AnimationProgressed(const gfx::Animation* animation) 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 | |
| 149 // Sets the button states. | |
| 150 void SetButtonStates(Button::ButtonState text_button_state, | |
| 151 Button::ButtonState arrow_button_state); | |
| 152 | |
| 153 // Manipulate |animation| according to the button states. | |
| 154 void AnimateStateTransition(gfx::SlideAnimation* animation, | |
| 155 Button::ButtonState from, | |
| 156 Button::ButtonState to); | |
| 157 | |
| 158 // Returns true if the point is in the text button when using the button | |
| 159 // sytle. Returns false if the style is not the button style. | |
| 160 bool InTextButton(const gfx::Point& point); | |
| 161 | |
| 162 // Return true if the point is in the text button when using the button | |
| 163 // sytle. Returns true if the style is not the button style. | |
| 164 bool InArrowButton(const gfx::Point& point); | |
| 165 | |
| 120 // Our model. Not owned. | 166 // Our model. Not owned. |
| 121 ui::ComboboxModel* model_; | 167 ui::ComboboxModel* model_; |
| 122 | 168 |
| 169 // The visual style of this combobox. | |
| 170 Style style_; | |
| 171 | |
| 123 // Our listener. Not owned. Notified when the selected index change. | 172 // Our listener. Not owned. Notified when the selected index change. |
| 124 ComboboxListener* listener_; | 173 ComboboxListener* listener_; |
| 125 | 174 |
| 126 // The current selected index; -1 and means no selection. | 175 // The current selected index; -1 and means no selection. |
| 127 int selected_index_; | 176 int selected_index_; |
| 128 | 177 |
| 129 // True when the selection is visually denoted as invalid. | 178 // True when the selection is visually denoted as invalid. |
| 130 bool invalid_; | 179 bool invalid_; |
| 131 | 180 |
| 132 // The accessible name of this combobox. | 181 // 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 | 200 // 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 | 201 // 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 | 202 // 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, | 203 // menu is displayed using a modal loop and, unlike regular menus in Windows, |
| 155 // the button is not part of the displayed menu. | 204 // the button is not part of the displayed menu. |
| 156 base::Time closed_time_; | 205 base::Time closed_time_; |
| 157 | 206 |
| 158 // The maximum dimensions of the content in the dropdown | 207 // The maximum dimensions of the content in the dropdown |
| 159 gfx::Size content_size_; | 208 gfx::Size content_size_; |
| 160 | 209 |
| 210 // The images that are used when |style_| is STYLE_BUTTONS. The first index | |
| 211 // means the state of unfocused or focused. | |
| 212 std::vector<const gfx::ImageSkia*> | |
| 213 body_button_images_[2][Button::STATE_COUNT]; | |
| 214 std::vector<const gfx::ImageSkia*> | |
| 215 menu_button_images_[2][Button::STATE_COUNT]; | |
| 216 | |
| 217 // True if dragging started. | |
| 218 bool drag_started_; | |
| 219 | |
| 220 // The location where dragging started. | |
| 221 gfx::Point drag_start_point_; | |
| 222 | |
| 223 // The states of buttons like pressed or hovered. | |
| 224 Button::ButtonState text_button_state_; | |
| 225 Button::ButtonState arrow_button_state_; | |
| 226 | |
| 227 // The animations to render the hovered buttons. | |
| 228 scoped_ptr<gfx::SlideAnimation> text_button_hover_animation_; | |
| 229 scoped_ptr<gfx::SlideAnimation> arrow_button_hover_animation_; | |
| 230 | |
| 161 DISALLOW_COPY_AND_ASSIGN(Combobox); | 231 DISALLOW_COPY_AND_ASSIGN(Combobox); |
| 162 }; | 232 }; |
| 163 | 233 |
| 164 } // namespace views | 234 } // namespace views |
| 165 | 235 |
| 166 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ | 236 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ |
| OLD | NEW |