1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/combobox.h
3 // Purpose: the universal combobox
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 A few words about all the classes defined in this file are probably in
14 order: why do we need extra wxComboControl and wxComboPopup classes?
16 This is because a traditional combobox is a combination of a text control
17 (with a button allowing to open the pop down list) with a listbox and
18 wxComboBox class is exactly such control, however we want to also have other
19 combinations - in fact, we want to allow anything at all to be used as pop
20 down list, not just a wxListBox.
22 So we define a base wxComboControl which can use any control as pop down
23 list and wxComboBox deriving from it which implements the standard wxWindows
24 combobox API. wxComboControl needs to be told somehow which control to use
25 and this is done by SetPopupControl(). However, we need something more than
26 just a wxControl in this method as, for example, we need to call
27 SetSelection("initial text value") and wxControl doesn't have such method.
28 So we also need a wxComboPopup which is just a very simple interface which
29 must be implemented by a control to be usable as a popup.
31 We couldn't derive wxComboPopup from wxControl as this would make it
32 impossible to have a class deriving from both wxListBx and from it, so
33 instead it is just a mix-in.
36 #ifndef _WX_UNIV_COMBOBOX_H_
37 #define _WX_UNIV_COMBOBOX_H_
40 #pragma implementation "univcombobox.h"
43 class WXDLLEXPORT wxComboControl
;
44 class WXDLLEXPORT wxListBox
;
45 class WXDLLEXPORT wxPopupComboWindow
;
47 // ----------------------------------------------------------------------------
48 // the actions supported by this control
49 // ----------------------------------------------------------------------------
51 // all actions of single line text controls are supported
53 // popup/dismiss the choice window
54 #define wxACTION_COMBOBOX_POPUP _T("popup")
55 #define wxACTION_COMBOBOX_DISMISS _T("dismiss")
57 // choose the next/prev/specified (by numArg) item
58 #define wxACTION_COMBOBOX_SELECT_NEXT _T("next")
59 #define wxACTION_COMBOBOX_SELECT_PREV _T("prev")
60 #define wxACTION_COMBOBOX_SELECT _T("select")
62 // ----------------------------------------------------------------------------
63 // wxComboPopup is the interface which must be implemented by a control to be
64 // used as a popup by wxComboControl
65 // ----------------------------------------------------------------------------
67 class WXDLLEXPORT wxComboPopup
70 wxComboPopup(wxComboControl
*combo
) { m_combo
= combo
; }
72 // we must have an associated control which is subclassed by the combobox
73 virtual wxControl
*GetControl() = 0;
75 // called before showing the control to set the initial selection - notice
76 // that the text passed to this method might not correspond to any valid
77 // item (if the user edited it directly), in which case the method should
78 // just return FALSE but not emit any errors
79 virtual bool SetSelection(const wxString
& value
) = 0;
81 // called immediately after the control is shown
82 virtual void OnShow() = 0;
85 wxComboControl
*m_combo
;
88 // ----------------------------------------------------------------------------
89 // wxComboControl: a combination of a (single line) text control with a button
90 // opening a popup window which contains the control from which the user can
91 // choose the value directly.
92 // ----------------------------------------------------------------------------
94 class WXDLLEXPORT wxComboControl
: public wxControl
103 wxComboControl(wxWindow
*parent
,
105 const wxString
& value
= wxEmptyString
,
106 const wxPoint
& pos
= wxDefaultPosition
,
107 const wxSize
& size
= wxDefaultSize
,
109 const wxValidator
& validator
= wxDefaultValidator
,
110 const wxString
& name
= wxComboBoxNameStr
)
114 (void)Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
117 bool Create(wxWindow
*parent
,
119 const wxString
& value
= wxEmptyString
,
120 const wxPoint
& pos
= wxDefaultPosition
,
121 const wxSize
& size
= wxDefaultSize
,
123 const wxValidator
& validator
= wxDefaultValidator
,
124 const wxString
& name
= wxComboBoxNameStr
);
126 virtual ~wxComboControl();
128 // a combo control needs a control for popup window it displays
129 void SetPopupControl(wxComboPopup
*popup
);
130 wxComboPopup
*GetPopupControl() const { return m_popup
; }
132 // show/hide popup window
136 // return TRUE if the popup is currently shown
137 bool IsPopupShown() const { return m_isPopupShown
; }
139 // get the popup window containing the popup control
140 wxPopupComboWindow
*GetPopupWindow() const { return m_winPopup
; }
142 // get the text control which is part of the combobox
143 wxTextCtrl
*GetText() const { return m_text
; }
145 // implementation only from now on
146 // -------------------------------
148 // notifications from wxComboPopup (shouldn't be called by anybody else)
150 // called when the user selects something in the popup: this normally hides
151 // the popup and sets the text to the new value
152 virtual void OnSelect(const wxString
& value
);
154 // called when the user dismisses the popup
155 virtual void OnDismiss();
157 // forward these functions to all subcontrols
158 virtual bool Enable(bool enable
= TRUE
);
159 virtual bool Show(bool show
= TRUE
);
162 // override the base class virtuals involved into geometry calculations
163 virtual wxSize
DoGetBestClientSize() const;
164 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
165 virtual void DoSetSize(int x
, int y
,
166 int width
, int height
,
167 int sizeFlags
= wxSIZE_AUTO
);
169 // we have our own input handler and our own actions
170 virtual bool PerformAction(const wxControlAction
& action
,
172 const wxString
& strArg
= wxEmptyString
);
175 void OnKey(wxCommandEvent
& event
);
177 // common part of all ctors
181 // the text control and button we show all the time
186 wxComboPopup
*m_popup
;
188 // and the popup window containing it
189 wxPopupComboWindow
*m_winPopup
;
191 // the height of the combobox popup as calculated in Create()
192 wxCoord m_heightPopup
;
194 // is the popup window currenty shown?
197 DECLARE_EVENT_TABLE()
200 // ----------------------------------------------------------------------------
201 // wxComboBox: a combination of text control and a listbox
202 // ----------------------------------------------------------------------------
204 class WXDLLEXPORT wxComboBox
: public wxComboControl
, public wxComboBoxBase
208 wxComboBox() { Init(); }
210 wxComboBox(wxWindow
*parent
,
212 const wxString
& value
= wxEmptyString
,
213 const wxPoint
& pos
= wxDefaultPosition
,
214 const wxSize
& size
= wxDefaultSize
,
216 const wxString
*choices
= (const wxString
*) NULL
,
218 const wxValidator
& validator
= wxDefaultValidator
,
219 const wxString
& name
= wxComboBoxNameStr
)
223 (void)Create(parent
, id
, value
, pos
, size
, n
, choices
,
224 style
, validator
, name
);
227 bool Create(wxWindow
*parent
,
229 const wxString
& value
= wxEmptyString
,
230 const wxPoint
& pos
= wxDefaultPosition
,
231 const wxSize
& size
= wxDefaultSize
,
233 const wxString choices
[] = (const wxString
*) NULL
,
235 const wxValidator
& validator
= wxDefaultValidator
,
236 const wxString
& name
= wxComboBoxNameStr
);
239 virtual ~wxComboBox();
241 // the wxUniversal-specific methods
242 // --------------------------------
244 // implement the combobox interface
246 // wxTextCtrl methods
247 virtual wxString
GetValue() const;
248 virtual void SetValue(const wxString
& value
);
251 virtual void Paste();
252 virtual void SetInsertionPoint(long pos
);
253 virtual void SetInsertionPointEnd();
254 virtual long GetInsertionPoint() const;
255 virtual long GetLastPosition() const;
256 virtual void Replace(long from
, long to
, const wxString
& value
);
257 virtual void Remove(long from
, long to
);
258 virtual void SetSelection(long from
, long to
);
259 virtual void SetEditable(bool editable
);
261 // wxControlWithItems methods
262 virtual void Clear();
263 virtual void Delete(int n
);
264 virtual int GetCount() const;
265 virtual wxString
GetString(int n
) const;
266 virtual void SetString(int n
, const wxString
& s
);
267 virtual int FindString(const wxString
& s
) const;
268 virtual void Select(int n
);
269 virtual int GetSelection() const;
270 void SetSelection(int n
) { Select(n
); }
273 virtual int DoAppend(const wxString
& item
);
274 virtual void DoSetItemClientData(int n
, void* clientData
);
275 virtual void* DoGetItemClientData(int n
) const;
276 virtual void DoSetItemClientObject(int n
, wxClientData
* clientData
);
277 virtual wxClientData
* DoGetItemClientObject(int n
) const;
279 // common part of all ctors
282 // get the associated listbox
283 wxListBox
*GetLBox() const { return m_lbox
; }
289 //DECLARE_EVENT_TABLE()
290 DECLARE_DYNAMIC_CLASS(wxComboBox
)
293 // ----------------------------------------------------------------------------
294 // wxStdComboBoxInputHandler: allows the user to open/close the combo from kbd
295 // ----------------------------------------------------------------------------
297 class WXDLLEXPORT wxStdComboBoxInputHandler
: public wxStdInputHandler
300 wxStdComboBoxInputHandler(wxInputHandler
*inphand
);
302 virtual bool HandleKey(wxControl
*control
,
303 const wxKeyEvent
& event
,
307 #endif // _WX_UNIV_COMBOBOX_H_