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/time/time.h"
11 #include "ui/base/models/combobox_model_observer.h"
12 #include "ui/gfx/native_widget_types.h"
13 #include "ui/views/controls/menu/menu_delegate.h"
14 #include "ui/views/controls/prefix_delegate.h"
26 class ComboboxListener;
27 class FocusableBorder;
31 // A non-editable combobox (aka a drop-down list or selector).
32 class VIEWS_EXPORT Combobox
33 : public MenuDelegate,
34 public PrefixDelegate,
35 public ui::ComboboxModelObserver {
37 // The combobox's class name.
38 static const char kViewClassName[];
40 // |model| is not owned by the combobox.
41 explicit Combobox(ui::ComboboxModel* model);
44 static const gfx::Font& GetFont();
46 // Sets the listener which will be called when a selection has been made.
47 void set_listener(ComboboxListener* listener) { listener_ = listener; }
49 // Informs the combobox that its model changed.
52 // Gets/Sets the selected index.
53 int selected_index() const { return selected_index_; }
54 void SetSelectedIndex(int index);
56 ui::ComboboxModel* model() const { return model_; }
58 // Set the accessible name of the combobox.
59 void SetAccessibleName(const string16& name);
61 // Visually marks the combobox as having an invalid value selected.
62 // When invalid, it paints with white text on a red background.
63 // Callers are responsible for restoring validity with selection changes.
64 void SetInvalid(bool invalid);
65 bool invalid() const { return invalid_; }
67 // Overridden from View:
68 virtual gfx::Size GetPreferredSize() OVERRIDE;
69 virtual const char* GetClassName() const OVERRIDE;
70 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE;
71 virtual bool OnMousePressed(const ui::MouseEvent& mouse_event) OVERRIDE;
72 virtual bool OnMouseDragged(const ui::MouseEvent& mouse_event) OVERRIDE;
73 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE;
74 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE;
75 virtual void OnGestureEvent(ui::GestureEvent* gesture) OVERRIDE;
76 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
77 virtual void OnFocus() OVERRIDE;
78 virtual void OnBlur() OVERRIDE;
79 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
80 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE;
82 // Overridden from MenuDelegate:
83 virtual bool IsItemChecked(int id) const OVERRIDE;
84 virtual bool IsCommandEnabled(int id) const OVERRIDE;
85 virtual void ExecuteCommand(int id) OVERRIDE;
86 virtual bool GetAccelerator(int id, ui::Accelerator* accelerator) OVERRIDE;
88 // Overridden from PrefixDelegate:
89 virtual int GetRowCount() OVERRIDE;
90 virtual int GetSelectedRow() OVERRIDE;
91 virtual void SetSelectedRow(int row) OVERRIDE;
92 virtual string16 GetTextForRow(int row) OVERRIDE;
94 // Overriden from ComboboxModelObserver:
95 virtual void OnModelChanged() OVERRIDE;
98 // Updates the combobox's content from its model.
99 void UpdateFromModel();
101 // Given bounds within our View, this helper mirrors the bounds if necessary.
102 void AdjustBoundsForRTLUI(gfx::Rect* rect) const;
104 // Draw the selected value of the drop down list
105 void PaintText(gfx::Canvas* canvas);
107 // Show the drop down list
108 void ShowDropDownMenu(ui::MenuSourceType source_type);
110 // Called when the selection is changed by the user.
111 void OnSelectionChanged();
113 // Converts a menu command ID to a menu item index.
114 int MenuCommandToIndex(int menu_command_id) const;
116 // Our model. Not owned.
117 ui::ComboboxModel* model_;
119 // Our listener. Not owned. Notified when the selected index change.
120 ComboboxListener* listener_;
122 // The current selected index; -1 and means no selection.
125 // True when the selection is visually denoted as invalid.
128 // The accessible name of this combobox.
129 string16 accessible_name_;
131 // A helper used to select entries by keyboard input.
132 scoped_ptr<PrefixSelector> selector_;
134 // The reference to the border class. The object is owned by View::border_.
135 FocusableBorder* text_border_;
137 // The disclosure arrow next to the currently selected item from the list.
138 const gfx::ImageSkia* disclosure_arrow_;
140 // Responsible for showing the context menu.
141 scoped_ptr<MenuRunner> dropdown_list_menu_runner_;
143 // Is the drop down list showing
146 // Like MenuButton, we use a time object in order to keep track of when the
147 // combobox was closed. The time is used for simulating menu behavior; that
148 // is, if the menu is shown and the button is pressed, we need to close the
149 // menu. There is no clean way to get the second click event because the
150 // menu is displayed using a modal loop and, unlike regular menus in Windows,
151 // the button is not part of the displayed menu.
152 base::Time closed_time_;
154 // The maximum dimensions of the content in the dropdown
155 gfx::Size content_size_;
157 DISALLOW_COPY_AND_ASSIGN(Combobox);
162 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_