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_
8 #include "base/memory/weak_ptr.h"
9 #include "base/strings/string16.h"
10 #include "base/time/time.h"
11 #include "ui/views/controls/button/button.h"
12 #include "ui/views/controls/prefix_delegate.h"
25 class ComboboxTestApi
;
28 class ComboboxListener
;
34 // A non-editable combobox (aka a drop-down list or selector).
35 // Combobox has two distinct parts, the drop down arrow and the text. Combobox
36 // offers two distinct behaviors:
37 // * STYLE_NORMAL: typical combobox, clicking on the text and/or button shows
38 // the drop down, arrow keys change selection, selected index can be changed by
39 // the user to something other than the first item.
40 // * STYLE_ACTION: clicking on the text notifies the listener. The menu can be
41 // shown only by clicking on the arrow. The selected index is always reverted to
42 // 0 after the listener is notified.
43 class VIEWS_EXPORT Combobox
: public PrefixDelegate
, public ButtonListener
{
45 // The style of the combobox.
51 // The combobox's class name.
52 static const char kViewClassName
[];
54 // |model| is not owned by the combobox.
55 explicit Combobox(ui::ComboboxModel
* model
);
58 static const gfx::FontList
& GetFontList();
60 // Sets the listener which will be called when a selection has been made.
61 void set_listener(ComboboxListener
* listener
) { listener_
= listener
; }
63 void SetStyle(Style style
);
65 // Informs the combobox that its model changed.
68 // Gets/Sets the selected index.
69 int selected_index() const { return selected_index_
; }
70 void SetSelectedIndex(int index
);
72 // Looks for the first occurrence of |value| in |model()|. If found, selects
73 // the found index and returns true. Otherwise simply noops and returns false.
74 bool SelectValue(const base::string16
& value
);
76 ui::ComboboxModel
* model() const { return model_
; }
78 // Set the accessible name of the combobox.
79 void SetAccessibleName(const base::string16
& name
);
81 // Visually marks the combobox as having an invalid value selected.
82 // When invalid, it paints with white text on a red background.
83 // Callers are responsible for restoring validity with selection changes.
84 void SetInvalid(bool invalid
);
85 bool invalid() const { return invalid_
; }
87 // Overridden from View:
88 gfx::Size
GetPreferredSize() const override
;
89 const char* GetClassName() const override
;
90 bool SkipDefaultKeyEventProcessing(const ui::KeyEvent
& e
) override
;
91 bool OnKeyPressed(const ui::KeyEvent
& e
) override
;
92 bool OnKeyReleased(const ui::KeyEvent
& e
) override
;
93 void OnPaint(gfx::Canvas
* canvas
) override
;
94 void OnFocus() override
;
95 void OnBlur() override
;
96 void GetAccessibleState(ui::AXViewState
* state
) override
;
97 void Layout() override
;
99 // Overridden from PrefixDelegate:
100 int GetRowCount() override
;
101 int GetSelectedRow() override
;
102 void SetSelectedRow(int row
) override
;
103 base::string16
GetTextForRow(int row
) override
;
105 // Overriden from ButtonListener:
106 void ButtonPressed(Button
* sender
, const ui::Event
& event
) override
;
109 friend class test::ComboboxTestApi
;
111 class ComboboxMenuModelAdapter
;
113 // Updates the border according to the current state.
116 // Given bounds within our View, this helper mirrors the bounds if necessary.
117 void AdjustBoundsForRTLUI(gfx::Rect
* rect
) const;
119 // Draws the selected value of the drop down list
120 void PaintText(gfx::Canvas
* canvas
);
122 // Draws the button images.
123 void PaintButtons(gfx::Canvas
* canvas
);
125 // Show the drop down list
126 void ShowDropDownMenu(ui::MenuSourceType source_type
);
128 // Called when the selection is changed by the user.
129 void OnPerformAction();
131 int GetDisclosureArrowLeftPadding() const;
132 int GetDisclosureArrowRightPadding() const;
134 // Returns the size of the disclosure arrow.
135 gfx::Size
ArrowSize() const;
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;
141 // Handles the clicking event.
142 void HandleClickEvent();
144 PrefixSelector
* GetPrefixSelector();
146 // Our model. Not owned.
147 ui::ComboboxModel
* model_
;
149 // The visual style of this combobox.
152 // Our listener. Not owned. Notified when the selected index change.
153 ComboboxListener
* listener_
;
155 // The current selected index; -1 and means no selection.
158 // True when the selection is visually denoted as invalid.
161 // The accessible name of this combobox.
162 base::string16 accessible_name_
;
164 // A helper used to select entries by keyboard input.
165 scoped_ptr
<PrefixSelector
> selector_
;
167 // Adapts a ComboboxModel for use by a views MenuRunner.
168 scoped_ptr
<ui::MenuModel
> menu_model_adapter_
;
170 // Like MenuButton, we use a time object in order to keep track of when the
171 // combobox was closed. The time is used for simulating menu behavior; that
172 // is, if the menu is shown and the button is pressed, we need to close the
173 // menu. There is no clean way to get the second click event because the
174 // menu is displayed using a modal loop and, unlike regular menus in Windows,
175 // the button is not part of the displayed menu.
176 base::Time closed_time_
;
178 // The maximum dimensions of the content in the dropdown.
179 gfx::Size content_size_
;
181 // The painters or images that are used when |style_| is STYLE_BUTTONS. The
182 // first index means the state of unfocused or focused.
183 // The images are owned by ResourceBundle.
184 scoped_ptr
<Painter
> body_button_painters_
[2][Button::STATE_COUNT
];
185 std::vector
<const gfx::ImageSkia
*>
186 menu_button_images_
[2][Button::STATE_COUNT
];
188 // The transparent buttons to handle events and render buttons. These are
189 // placed on top of this combobox as child views, accept event and manage the
190 // button states. These are not rendered but when |style_| is
191 // STYLE_NOTIFY_ON_CLICK, a Combobox renders the button images according to
192 // these button states.
193 // The base View takes the ownerships of these as child views.
194 CustomButton
* text_button_
;
195 CustomButton
* arrow_button_
;
197 // Set while the dropdown is showing. Ensures the menu is closed if |this| is
199 scoped_ptr
<views::MenuRunner
> menu_runner_
;
201 // Used for making calbacks.
202 base::WeakPtrFactory
<Combobox
> weak_ptr_factory_
;
204 DISALLOW_COPY_AND_ASSIGN(Combobox
);
209 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_