1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/univ/combobox.h
3 // Purpose: the universal combobox
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
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 wxWidgets
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_
39 class WXDLLEXPORT wxComboControl
;
40 class WXDLLEXPORT wxListBox
;
41 class WXDLLEXPORT wxPopupComboWindow
;
42 class WXDLLEXPORT wxTextCtrl
;
43 class WXDLLEXPORT wxButton
;
45 // ----------------------------------------------------------------------------
46 // the actions supported by this control
47 // ----------------------------------------------------------------------------
49 // all actions of single line text controls are supported
51 // popup/dismiss the choice window
52 #define wxACTION_COMBOBOX_POPUP _T("popup")
53 #define wxACTION_COMBOBOX_DISMISS _T("dismiss")
55 // choose the next/prev/specified (by numArg) item
56 #define wxACTION_COMBOBOX_SELECT_NEXT _T("next")
57 #define wxACTION_COMBOBOX_SELECT_PREV _T("prev")
58 #define wxACTION_COMBOBOX_SELECT _T("select")
60 // ----------------------------------------------------------------------------
61 // wxComboPopup is the interface which must be implemented by a control to be
62 // used as a popup by wxComboControl
63 // ----------------------------------------------------------------------------
65 class WXDLLEXPORT wxComboPopup
68 wxComboPopup(wxComboControl
*combo
) { m_combo
= combo
; }
69 virtual ~wxComboPopup() {}
71 // we must have an associated control which is subclassed by the combobox
72 virtual wxControl
*GetControl() = 0;
74 // called before showing the control to set the initial selection - notice
75 // that the text passed to this method might not correspond to any valid
76 // item (if the user edited it directly), in which case the method should
77 // just return false but not emit any errors
78 virtual bool SetSelection(const wxString
& value
) = 0;
80 // called immediately after the control is shown
81 virtual void OnShow() = 0;
83 virtual wxCoord
GetBestWidth() const {return 0; }
86 wxComboControl
*m_combo
;
89 // ----------------------------------------------------------------------------
90 // wxComboControl: a combination of a (single line) text control with a button
91 // opening a popup window which contains the control from which the user can
92 // choose the value directly.
93 // ----------------------------------------------------------------------------
95 class WXDLLEXPORT wxComboControl
: public wxControl
104 wxComboControl(wxWindow
*parent
,
106 const wxString
& value
= wxEmptyString
,
107 const wxPoint
& pos
= wxDefaultPosition
,
108 const wxSize
& size
= wxDefaultSize
,
110 const wxValidator
& validator
= wxDefaultValidator
,
111 const wxString
& name
= wxComboBoxNameStr
)
115 (void)Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
118 bool Create(wxWindow
*parent
,
120 const wxString
& value
= wxEmptyString
,
121 const wxPoint
& pos
= wxDefaultPosition
,
122 const wxSize
& size
= wxDefaultSize
,
124 const wxValidator
& validator
= wxDefaultValidator
,
125 const wxString
& name
= wxComboBoxNameStr
);
127 virtual ~wxComboControl();
129 // a combo control needs a control for popup window it displays
130 void SetPopupControl(wxComboPopup
*popup
);
131 wxComboPopup
*GetPopupControl() const { return m_popup
; }
133 // show/hide popup window
137 // return true if the popup is currently shown
138 bool IsPopupShown() const { return m_isPopupShown
; }
140 // get the popup window containing the popup control
141 wxPopupComboWindow
*GetPopupWindow() const { return m_winPopup
; }
143 // get the text control which is part of the combobox
144 wxTextCtrl
*GetText() const { return m_text
; }
146 // implementation only from now on
147 // -------------------------------
149 // notifications from wxComboPopup (shouldn't be called by anybody else)
151 // called when the user selects something in the popup: this normally hides
152 // the popup and sets the text to the new value
153 virtual void OnSelect(const wxString
& value
);
155 // called when the user dismisses the popup
156 virtual void OnDismiss();
158 // forward these functions to all subcontrols
159 virtual bool Enable(bool enable
= true);
160 virtual bool Show(bool show
= true);
163 virtual void DoSetToolTip( wxToolTip
*tip
);
164 #endif // wxUSE_TOOLTIPS
166 // we have our own input handler and our own actions
167 virtual bool PerformAction(const wxControlAction
& action
,
169 const wxString
& strArg
= wxEmptyString
);
172 // override the base class virtuals involved into geometry calculations
173 virtual wxSize
DoGetBestClientSize() const;
174 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
175 virtual void DoSetSize(int x
, int y
,
176 int width
, int height
,
177 int sizeFlags
= wxSIZE_AUTO
);
180 void OnKey(wxKeyEvent
& event
);
182 // common part of all ctors
186 // the text control and button we show all the time
191 wxComboPopup
*m_popup
;
193 // and the popup window containing it
194 wxPopupComboWindow
*m_winPopup
;
196 // the height of the combobox popup as calculated in Create()
197 wxCoord m_heightPopup
;
199 // is the popup window currenty shown?
202 DECLARE_EVENT_TABLE()
205 // ----------------------------------------------------------------------------
206 // wxComboBox: a combination of text control and a listbox
207 // ----------------------------------------------------------------------------
209 class WXDLLEXPORT wxComboBox
: public wxComboControl
, public wxComboBoxBase
213 wxComboBox() { Init(); }
215 wxComboBox(wxWindow
*parent
,
217 const wxString
& value
= wxEmptyString
,
218 const wxPoint
& pos
= wxDefaultPosition
,
219 const wxSize
& size
= wxDefaultSize
,
221 const wxString choices
[] = (const wxString
*) NULL
,
223 const wxValidator
& validator
= wxDefaultValidator
,
224 const wxString
& name
= wxComboBoxNameStr
)
228 (void)Create(parent
, id
, value
, pos
, size
, n
, choices
,
229 style
, validator
, name
);
231 wxComboBox(wxWindow
*parent
,
233 const wxString
& value
,
236 const wxArrayString
& choices
,
238 const wxValidator
& validator
= wxDefaultValidator
,
239 const wxString
& name
= wxComboBoxNameStr
);
241 bool Create(wxWindow
*parent
,
243 const wxString
& value
= wxEmptyString
,
244 const wxPoint
& pos
= wxDefaultPosition
,
245 const wxSize
& size
= wxDefaultSize
,
247 const wxString choices
[] = (const wxString
*) NULL
,
249 const wxValidator
& validator
= wxDefaultValidator
,
250 const wxString
& name
= wxComboBoxNameStr
);
251 bool Create(wxWindow
*parent
,
253 const wxString
& value
,
256 const wxArrayString
& choices
,
258 const wxValidator
& validator
= wxDefaultValidator
,
259 const wxString
& name
= wxComboBoxNameStr
);
261 virtual ~wxComboBox();
263 // the wxUniversal-specific methods
264 // --------------------------------
266 // implement the combobox interface
268 // wxTextCtrl methods
269 virtual wxString
GetValue() const;
270 virtual void SetValue(const wxString
& value
);
273 virtual void Paste();
274 virtual void SetInsertionPoint(long pos
);
275 virtual void SetInsertionPointEnd();
276 virtual long GetInsertionPoint() const;
277 virtual wxTextPos
GetLastPosition() const;
278 virtual void Replace(long from
, long to
, const wxString
& value
);
279 virtual void Remove(long from
, long to
);
280 virtual void SetSelection(long from
, long to
);
281 virtual void SetEditable(bool editable
);
282 virtual bool IsEditable() const;
286 virtual void SelectAll();
288 virtual bool CanCopy() const;
289 virtual bool CanCut() const;
290 virtual bool CanPaste() const;
291 virtual bool CanUndo() const;
292 virtual bool CanRedo() const;
294 // wxControlWithItems methods
295 virtual void Clear();
296 virtual void Delete(unsigned int n
);
297 virtual unsigned int GetCount() const;
298 virtual wxString
GetString(unsigned int n
) const;
299 virtual void SetString(unsigned int n
, const wxString
& s
);
300 virtual int FindString(const wxString
& s
, bool bCase
= false) const;
301 virtual void SetSelection(int n
);
302 virtual int GetSelection() const;
304 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
307 virtual int DoAppend(const wxString
& item
);
308 virtual int DoInsert(const wxString
& item
, unsigned int pos
);
309 virtual void DoSetItemClientData(unsigned int n
, void* clientData
);
310 virtual void* DoGetItemClientData(unsigned int n
) const;
311 virtual void DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
);
312 virtual wxClientData
* DoGetItemClientObject(unsigned int n
) const;
314 // common part of all ctors
317 // get the associated listbox
318 wxListBox
*GetLBox() const { return m_lbox
; }
324 //DECLARE_EVENT_TABLE()
325 DECLARE_DYNAMIC_CLASS(wxComboBox
)
328 // ----------------------------------------------------------------------------
329 // wxStdComboBoxInputHandler: allows the user to open/close the combo from kbd
330 // ----------------------------------------------------------------------------
332 class WXDLLEXPORT wxStdComboBoxInputHandler
: public wxStdInputHandler
335 wxStdComboBoxInputHandler(wxInputHandler
*inphand
);
337 virtual bool HandleKey(wxInputConsumer
*consumer
,
338 const wxKeyEvent
& event
,
342 #endif // _WX_UNIV_COMBOBOX_H_