Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: ui/views/controls/combobox/combobox.h

Issue 59383003: Add the button style for combobox (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: sky's review (2) Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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);
(...skipping 16 matching lines...) Expand all
75 virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE; 92 virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE;
76 virtual bool OnMouseDragged(const ui::MouseEvent& mouse_event) OVERRIDE; 93 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; 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;
102 virtual void Layout() OVERRIDE;
85 103
86 // Overridden from MenuDelegate: 104 // Overridden from MenuDelegate:
87 virtual bool IsItemChecked(int id) const OVERRIDE; 105 virtual bool IsItemChecked(int id) const OVERRIDE;
88 virtual bool IsCommandEnabled(int id) const OVERRIDE; 106 virtual bool IsCommandEnabled(int id) const OVERRIDE;
89 virtual void ExecuteCommand(int id) OVERRIDE; 107 virtual void ExecuteCommand(int id) OVERRIDE;
90 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE; 108 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE;
91 109
92 // Overridden from PrefixDelegate: 110 // Overridden from PrefixDelegate:
93 virtual int GetRowCount() OVERRIDE; 111 virtual int GetRowCount() OVERRIDE;
94 virtual int GetSelectedRow() OVERRIDE; 112 virtual int GetSelectedRow() OVERRIDE;
95 virtual void SetSelectedRow(int row) OVERRIDE; 113 virtual void SetSelectedRow(int row) OVERRIDE;
96 virtual string16 GetTextForRow(int row) OVERRIDE; 114 virtual string16 GetTextForRow(int row) OVERRIDE;
97 115
98 // Overriden from ComboboxModelObserver: 116 // Overriden from ComboboxModelObserver:
99 virtual void OnModelChanged() OVERRIDE; 117 virtual void OnModelChanged() OVERRIDE;
100 118
119 // Overriden from ButtonListener:
120 virtual void ButtonPressed(Button* sender, const ui::Event& event) OVERRIDE;
121
101 private: 122 private:
102 // Updates the combobox's content from its model. 123 // Updates the combobox's content from its model.
103 void UpdateFromModel(); 124 void UpdateFromModel();
104 125
105 // Given bounds within our View, this helper mirrors the bounds if necessary. 126 // Given bounds within our View, this helper mirrors the bounds if necessary.
106 void AdjustBoundsForRTLUI(gfx::Rect* rect) const; 127 void AdjustBoundsForRTLUI(gfx::Rect* rect) const;
107 128
108 // Draw the selected value of the drop down list 129 // Draws the selected value of the drop down list
109 void PaintText(gfx::Canvas* canvas); 130 void PaintText(gfx::Canvas* canvas);
110 131
132 // Draws the button images.
133 void PaintButtons(gfx::Canvas* canvas);
134
111 // Show the drop down list 135 // Show the drop down list
112 void ShowDropDownMenu(ui::MenuSourceType source_type); 136 void ShowDropDownMenu(ui::MenuSourceType source_type);
113 137
114 // Called when the selection is changed by the user. 138 // Called when the selection is changed by the user.
115 void OnSelectionChanged(); 139 void OnSelectionChanged();
116 140
117 // Converts a menu command ID to a menu item index. 141 // Converts a menu command ID to a menu item index.
118 int MenuCommandToIndex(int menu_command_id) const; 142 int MenuCommandToIndex(int menu_command_id) const;
119 143
144 int GetDisclosureArrowLeftPadding() const;
145 int GetDisclosureArrowRightPadding() const;
146
147 // Handles the clicking event.
148 void HandleClickEvent();
149
120 // Our model. Not owned. 150 // Our model. Not owned.
121 ui::ComboboxModel* model_; 151 ui::ComboboxModel* model_;
122 152
153 // The visual style of this combobox.
154 Style style_;
155
123 // Our listener. Not owned. Notified when the selected index change. 156 // Our listener. Not owned. Notified when the selected index change.
124 ComboboxListener* listener_; 157 ComboboxListener* listener_;
125 158
126 // The current selected index; -1 and means no selection. 159 // The current selected index; -1 and means no selection.
127 int selected_index_; 160 int selected_index_;
128 161
129 // True when the selection is visually denoted as invalid. 162 // True when the selection is visually denoted as invalid.
130 bool invalid_; 163 bool invalid_;
131 164
132 // The accessible name of this combobox. 165 // The accessible name of this combobox.
(...skipping 18 matching lines...) Expand all
151 // combobox was closed. The time is used for simulating menu behavior; that 184 // 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 185 // 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 186 // 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, 187 // menu is displayed using a modal loop and, unlike regular menus in Windows,
155 // the button is not part of the displayed menu. 188 // the button is not part of the displayed menu.
156 base::Time closed_time_; 189 base::Time closed_time_;
157 190
158 // The maximum dimensions of the content in the dropdown 191 // The maximum dimensions of the content in the dropdown
159 gfx::Size content_size_; 192 gfx::Size content_size_;
160 193
194 // The painters or images that are used when |style_| is STYLE_BUTTONS. The
195 // first index means the state of unfocused or focused.
196 // The images are owned by ResourceBundle.
197 scoped_ptr<Painter> body_button_painters_[2][Button::STATE_COUNT];
198 std::vector<const gfx::ImageSkia*>
199 menu_button_images_[2][Button::STATE_COUNT];
200
201 // The transparent buttons for button rendering. These are used only when
202 // |style_| is STYLE_SHOW_DROP_DOWN_ON_CLICK. These are placed on top of this
203 // combobox as child views, accept event and manage the button states. These
204 // are not rendered but a Combobox renders the button images according to
205 // these button states.
206 // The base View takes the ownerships of these as child views.
207 CustomButton* text_button_;
208 CustomButton* arrow_button_;
209
161 DISALLOW_COPY_AND_ASSIGN(Combobox); 210 DISALLOW_COPY_AND_ASSIGN(Combobox);
162 }; 211 };
163 212
164 } // namespace views 213 } // namespace views
165 214
166 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ 215 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698