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 ui::TextInputClient
* GetTextInputClient() override
;
106 void Layout() override
;
108 // Overridden from MenuDelegate:
109 bool IsItemChecked(int id
) const override
;
110 bool IsCommandEnabled(int id
) const override
;
111 void ExecuteCommand(int id
) override
;
112 bool GetAccelerator(int id
, ui::Accelerator
* accelerator
) const override
;
114 // Overridden from PrefixDelegate:
115 int GetRowCount() override
;
116 int GetSelectedRow() override
;
117 void SetSelectedRow(int row
) override
;
118 base::string16
GetTextForRow(int row
) override
;
120 // Overriden from ComboboxModelObserver:
121 void OnComboboxModelChanged(ui::ComboboxModel
* model
) override
;
123 // Overriden from ButtonListener:
124 void ButtonPressed(Button
* sender
, const ui::Event
& event
) override
;
127 FRIEND_TEST_ALL_PREFIXES(ComboboxTest
, Click
);
128 FRIEND_TEST_ALL_PREFIXES(ComboboxTest
, ClickButDisabled
);
129 FRIEND_TEST_ALL_PREFIXES(ComboboxTest
, NotifyOnClickWithMouse
);
130 FRIEND_TEST_ALL_PREFIXES(ComboboxTest
, ContentWidth
);
132 // Updates the combobox's content from its model.
133 void UpdateFromModel();
135 // Updates the border according to the current state.
138 // Given bounds within our View, this helper mirrors the bounds if necessary.
139 void AdjustBoundsForRTLUI(gfx::Rect
* rect
) const;
141 // Draws the selected value of the drop down list
142 void PaintText(gfx::Canvas
* canvas
);
144 // Draws the button images.
145 void PaintButtons(gfx::Canvas
* canvas
);
147 // Show the drop down list
148 void ShowDropDownMenu(ui::MenuSourceType source_type
);
150 // Called when the selection is changed by the user.
151 void OnPerformAction();
152 void NotifyPerformAction();
153 void AfterPerformAction();
155 // Converts a menu command ID to a menu item index.
156 int MenuCommandToIndex(int menu_command_id
) const;
158 int GetDisclosureArrowLeftPadding() const;
159 int GetDisclosureArrowRightPadding() const;
161 // Returns the size of the disclosure arrow.
162 gfx::Size
ArrowSize() const;
164 // Handles the clicking event.
165 void HandleClickEvent();
167 // Our model. Not owned.
168 ui::ComboboxModel
* model_
;
170 // The visual style of this combobox.
173 // Our listener. Not owned. Notified when the selected index change.
174 ComboboxListener
* listener_
;
176 // The current selected index; -1 and means no selection.
179 // True when the selection is visually denoted as invalid.
182 // The accessible name of this combobox.
183 base::string16 accessible_name_
;
185 // A helper used to select entries by keyboard input.
186 scoped_ptr
<PrefixSelector
> selector_
;
188 // Responsible for showing the context menu.
189 scoped_ptr
<MenuRunner
> dropdown_list_menu_runner_
;
191 // Weak. Owned by dropdown_list_menu_runner_.
194 // Is the drop down list showing
197 // Like MenuButton, we use a time object in order to keep track of when the
198 // combobox was closed. The time is used for simulating menu behavior; that
199 // is, if the menu is shown and the button is pressed, we need to close the
200 // menu. There is no clean way to get the second click event because the
201 // menu is displayed using a modal loop and, unlike regular menus in Windows,
202 // the button is not part of the displayed menu.
203 base::Time closed_time_
;
205 // The maximum dimensions of the content in the dropdown
206 mutable gfx::Size content_size_
;
208 // The painters or images that are used when |style_| is STYLE_BUTTONS. The
209 // first index means the state of unfocused or focused.
210 // The images are owned by ResourceBundle.
211 scoped_ptr
<Painter
> body_button_painters_
[2][Button::STATE_COUNT
];
212 std::vector
<const gfx::ImageSkia
*>
213 menu_button_images_
[2][Button::STATE_COUNT
];
215 // The transparent buttons to handle events and render buttons. These are
216 // placed on top of this combobox as child views, accept event and manage the
217 // button states. These are not rendered but when |style_| is
218 // STYLE_NOTIFY_ON_CLICK, a Combobox renders the button images according to
219 // these button states.
220 // The base View takes the ownerships of these as child views.
221 CustomButton
* text_button_
;
222 CustomButton
* arrow_button_
;
224 // Used for making calbacks.
225 base::WeakPtrFactory
<Combobox
> weak_ptr_factory_
;
227 DISALLOW_COPY_AND_ASSIGN(Combobox
);
232 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_