- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / controls / combobox / combobox.h
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 "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"
15
16 namespace gfx {
17 class Font;
18 }
19
20 namespace ui {
21 class ComboboxModel;
22 }
23
24 namespace views {
25
26 class ComboboxListener;
27 class FocusableBorder;
28 class MenuRunner;
29 class PrefixSelector;
30
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 {
36  public:
37   // The combobox's class name.
38   static const char kViewClassName[];
39
40   // |model| is not owned by the combobox.
41   explicit Combobox(ui::ComboboxModel* model);
42   virtual ~Combobox();
43
44   static const gfx::Font& GetFont();
45
46   // Sets the listener which will be called when a selection has been made.
47   void set_listener(ComboboxListener* listener) { listener_ = listener; }
48
49   // Informs the combobox that its model changed.
50   void ModelChanged();
51
52   // Gets/Sets the selected index.
53   int selected_index() const { return selected_index_; }
54   void SetSelectedIndex(int index);
55
56   ui::ComboboxModel* model() const { return model_; }
57
58   // Set the accessible name of the combobox.
59   void SetAccessibleName(const string16& name);
60
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_; }
66
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;
81
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;
87
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;
93
94   // Overriden from ComboboxModelObserver:
95   virtual void OnModelChanged() OVERRIDE;
96
97  private:
98   // Updates the combobox's content from its model.
99   void UpdateFromModel();
100
101   // Given bounds within our View, this helper mirrors the bounds if necessary.
102   void AdjustBoundsForRTLUI(gfx::Rect* rect) const;
103
104   // Draw the selected value of the drop down list
105   void PaintText(gfx::Canvas* canvas);
106
107   // Show the drop down list
108   void ShowDropDownMenu(ui::MenuSourceType source_type);
109
110   // Called when the selection is changed by the user.
111   void OnSelectionChanged();
112
113   // Converts a menu command ID to a menu item index.
114   int MenuCommandToIndex(int menu_command_id) const;
115
116   // Our model. Not owned.
117   ui::ComboboxModel* model_;
118
119   // Our listener. Not owned. Notified when the selected index change.
120   ComboboxListener* listener_;
121
122   // The current selected index; -1 and means no selection.
123   int selected_index_;
124
125   // True when the selection is visually denoted as invalid.
126   bool invalid_;
127
128   // The accessible name of this combobox.
129   string16 accessible_name_;
130
131   // A helper used to select entries by keyboard input.
132   scoped_ptr<PrefixSelector> selector_;
133
134   // The reference to the border class. The object is owned by View::border_.
135   FocusableBorder* text_border_;
136
137   // The disclosure arrow next to the currently selected item from the list.
138   const gfx::ImageSkia* disclosure_arrow_;
139
140   // Responsible for showing the context menu.
141   scoped_ptr<MenuRunner> dropdown_list_menu_runner_;
142
143   // Is the drop down list showing
144   bool dropdown_open_;
145
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_;
153
154   // The maximum dimensions of the content in the dropdown
155   gfx::Size content_size_;
156
157   DISALLOW_COPY_AND_ASSIGN(Combobox);
158 };
159
160 }  // namespace views
161
162 #endif  // UI_VIEWS_CONTROLS_COMBOBOX_COMBOBOX_H_