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. 4 5 #ifndef UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ 6 #define UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ 7 8 #include <string> 9 10 #include "ui/gfx/native_widget_types.h" 11 #include "ui/views/controls/combobox/native_combobox_wrapper.h" 12 #include "ui/views/controls/prefix_delegate.h" 13 #include "ui/views/view.h" 14 15 namespace gfx { 16 class Font; 17 } 18 19 namespace ui { 20 class ComboboxModel; 21 } 22 23 namespace views { 24 25 class ComboboxListener; 26 class PrefixSelector; 27 28 // A non-editable combobox (aka a drop-down list). 29 class VIEWS_EXPORT Combobox : public PrefixDelegate { 30 public: 31 // The combobox's class name. 32 static const char kViewClassName[]; 33 34 // |model| is not owned by the combobox. 35 explicit Combobox(ui::ComboboxModel* model); 36 virtual ~Combobox(); 37 38 static const gfx::Font& GetFont(); 39 40 // Sets the listener which will be called when a selection has been made. 41 void set_listener(ComboboxListener* listener) { 42 listener_ = listener; 43 } 44 45 // Informs the combobox that its model changed. 46 void ModelChanged(); 47 48 // Gets/Sets the selected index. 49 int selected_index() const { return selected_index_; } 50 void SetSelectedIndex(int index); 51 52 // Called when the combobox's selection is changed by the user. 53 void SelectionChanged(); 54 55 ui::ComboboxModel* model() const { return model_; } 56 57 // Set the accessible name of the combobox. 58 void SetAccessibleName(const string16& name); 59 60 // Provided only for testing: 61 gfx::NativeView GetTestingHandle() const { 62 return native_wrapper_ ? native_wrapper_->GetTestingHandle() : NULL; 63 } 64 NativeComboboxWrapper* GetNativeWrapperForTesting() const { 65 return native_wrapper_; 66 } 67 68 // Visually marks the combobox as having an invalid value selected. The caller 69 // is responsible for flipping it back to valid if the selection changes. 70 void SetInvalid(bool invalid); 71 72 bool invalid() const { 73 return invalid_; 74 } 75 76 // Overridden from View: 77 virtual gfx::Size GetPreferredSize() OVERRIDE; 78 virtual void Layout() OVERRIDE; 79 virtual void OnEnabledChanged() OVERRIDE; 80 virtual bool SkipDefaultKeyEventProcessing(const ui::KeyEvent& e) OVERRIDE; 81 virtual void OnPaintFocusBorder(gfx::Canvas* canvas) OVERRIDE; 82 virtual bool OnKeyPressed(const ui::KeyEvent& e) OVERRIDE; 83 virtual bool OnKeyReleased(const ui::KeyEvent& e) OVERRIDE; 84 virtual void OnFocus() OVERRIDE; 85 virtual void OnBlur() OVERRIDE; 86 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 87 virtual ui::TextInputClient* GetTextInputClient() OVERRIDE; 88 89 // Overridden from PrefixDelegate: 90 virtual int GetRowCount() OVERRIDE; 91 virtual int GetSelectedRow() OVERRIDE; 92 virtual void SetSelectedRow(int row) OVERRIDE; 93 virtual string16 GetTextForRow(int row) OVERRIDE; 94 95 protected: 96 // Overridden from View: 97 virtual void ViewHierarchyChanged( 98 const ViewHierarchyChangedDetails& details) OVERRIDE; 99 virtual const char* GetClassName() const OVERRIDE; 100 101 // The object that actually implements the native combobox. 102 NativeComboboxWrapper* native_wrapper_; 103 104 private: 105 // Our model. Not owned. 106 ui::ComboboxModel* model_; 107 108 // Our listener. Not owned. Notified when the selected index change. 109 ComboboxListener* listener_; 110 111 // The current selected index. 112 int selected_index_; 113 114 // True when the selection is visually denoted as invalid. 115 bool invalid_; 116 117 // The accessible name of this combobox. 118 string16 accessible_name_; 119 120 scoped_ptr<PrefixSelector> selector_; 121 122 DISALLOW_COPY_AND_ASSIGN(Combobox); 123 }; 124 125 } // namespace views 126 127 #endif // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_ 128