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
3 // found in the LICENSE file.
5 #ifndef UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_
6 #define UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_
10 #include "base/memory/weak_ptr.h"
11 #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"
16 #include "ui/views/controls/menu/menu_delegate.h"
17 #include "ui/views/controls/prefix_delegate.h"
30 class ComboboxListener
;
31 class ComboboxMenuRunner
;
33 class FocusableBorder
;
35 class MenuRunnerHandler
;
39 // 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
41 // offers two distinct behaviors:
42 // * 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
44 // the user to something other than the first item.
45 // * 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
47 // 0 after the listener is notified.
48 class VIEWS_EXPORT Combobox
: public MenuDelegate
,
49 public PrefixDelegate
,
50 public ui::ComboboxModelObserver
,
51 public ButtonListener
{
53 // The style of the combobox.
59 // The combobox's class name.
60 static const char kViewClassName
[];
62 // |model| is not owned by the combobox.
63 explicit Combobox(ui::ComboboxModel
* model
);
66 static const gfx::FontList
& GetFontList();
68 // Sets the listener which will be called when a selection has been made.
69 void set_listener(ComboboxListener
* listener
) { listener_
= listener
; }
71 void SetStyle(Style style
);
73 // Informs the combobox that its model changed.
76 // Gets/Sets the selected index.
77 int selected_index() const { return selected_index_
; }
78 void SetSelectedIndex(int index
);
80 // Looks for the first occurrence of |value| in |model()|. If found, selects
81 // the found index and returns true. Otherwise simply noops and returns false.
82 bool SelectValue(const base::string16
& value
);
84 ui::ComboboxModel
* model() const { return model_
; }
86 // Set the accessible name of the combobox.
87 void SetAccessibleName(const base::string16
& name
);
89 // Visually marks the combobox as having an invalid value selected.
90 // When invalid, it paints with white text on a red background.
91 // Callers are responsible for restoring validity with selection changes.
92 void SetInvalid(bool invalid
);
93 bool invalid() const { return invalid_
; }
95 // Overridden from View:
96 gfx::Size
GetPreferredSize() const override
;
97 const char* GetClassName() const override
;
98 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent
& e
) override
;
99 bool OnKeyPressed(const ui::KeyEvent
& e
) override
;
100 bool OnKeyReleased(const ui::KeyEvent
& e
) override
;
101 void OnPaint(gfx::Canvas
* canvas
) override
;
102 void OnFocus() override
;
103 void OnBlur() override
;
104 void GetAccessibleState(ui::AXViewState
* state
) override
;
105 void Layout() override
;
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
;
113 // Overridden from PrefixDelegate:
114 int GetRowCount() override
;
115 int GetSelectedRow() override
;
116 void SetSelectedRow(int row
) override
;
117 base::string16
GetTextForRow(int row
) override
;
119 // Overriden from ComboboxModelObserver:
120 void OnComboboxModelChanged(ui::ComboboxModel
* model
) override
;
122 // Overriden from ButtonListener:
123 void ButtonPressed(Button
* sender
, const ui::Event
& event
) override
;
126 FRIEND_TEST_ALL_PREFIXES(ComboboxTest
, Click
);
127 FRIEND_TEST_ALL_PREFIXES(ComboboxTest
, ClickButDisabled
);
128 FRIEND_TEST_ALL_PREFIXES(ComboboxTest
, NotifyOnClickWithMouse
);
129 FRIEND_TEST_ALL_PREFIXES(ComboboxTest
, ContentWidth
);
131 // Updates the combobox's content from its model.
132 void UpdateFromModel();
134 // Updates the border according to the current state.
137 // Given bounds within our View, this helper mirrors the bounds if necessary.
138 void AdjustBoundsForRTLUI(gfx::Rect
* rect
) const;
140 // Draws the selected value of the drop down list
141 void PaintText(gfx::Canvas
* canvas
);
143 // Draws the button images.
144 void PaintButtons(gfx::Canvas
* canvas
);
146 // Show the drop down list
147 void ShowDropDownMenu(ui::MenuSourceType source_type
);
149 // Called when the selection is changed by the user.
150 void OnPerformAction();
151 void NotifyPerformAction();
152 void AfterPerformAction();
154 // Converts a menu command ID to a menu item index.
155 int MenuCommandToIndex(int menu_command_id
) const;
157 int GetDisclosureArrowLeftPadding() const;
158 int GetDisclosureArrowRightPadding() const;
160 // Returns the size of the disclosure arrow.
161 gfx::Size
ArrowSize() const;
163 // Handles the clicking event.
164 void HandleClickEvent();
166 PrefixSelector
* GetPrefixSelector();
168 // Our model. Not owned.
169 ui::ComboboxModel
* model_
;
171 // The visual style of this combobox.
174 // Our listener. Not owned. Notified when the selected index change.
175 ComboboxListener
* listener_
;
177 // The current selected index; -1 and means no selection.
180 // True when the selection is visually denoted as invalid.
183 // The accessible name of this combobox.
184 base::string16 accessible_name_
;
186 // A helper used to select entries by keyboard input.
187 scoped_ptr
<PrefixSelector
> selector_
;
189 // Responsible for showing the context menu.
190 scoped_ptr
<MenuRunner
> dropdown_list_menu_runner_
;
192 // Weak. Owned by dropdown_list_menu_runner_.
195 // Is the drop down list showing
198 // 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
200 // 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
202 // menu is displayed using a modal loop and, unlike regular menus in Windows,
203 // the button is not part of the displayed menu.
204 base::Time closed_time_
;
206 // The maximum dimensions of the content in the dropdown
207 mutable gfx::Size content_size_
;
209 // The painters or images that are used when |style_| is STYLE_BUTTONS. The
210 // first index means the state of unfocused or focused.
211 // The images are owned by ResourceBundle.
212 scoped_ptr
<Painter
> body_button_painters_
[2][Button::STATE_COUNT
];
213 std::vector
<const gfx::ImageSkia
*>
214 menu_button_images_
[2][Button::STATE_COUNT
];
216 // 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
218 // button states. These are not rendered but when |style_| is
219 // STYLE_NOTIFY_ON_CLICK, a Combobox renders the button images according to
220 // these button states.
221 // The base View takes the ownerships of these as child views.
222 CustomButton
* text_button_
;
223 CustomButton
* arrow_button_
;
225 // Used for making calbacks.
226 base::WeakPtrFactory
<Combobox
> weak_ptr_factory_
;
228 DISALLOW_COPY_AND_ASSIGN(Combobox
);
233 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_