1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/univ/combobox.cpp
3 // Purpose: wxComboBox implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
31 #include "wx/button.h"
32 #include "wx/combobox.h"
33 #include "wx/listbox.h"
34 #include "wx/textctrl.h"
35 #include "wx/bmpbuttn.h"
37 #include "wx/validate.h"
40 #include "wx/tooltip.h"
43 #include "wx/univ/renderer.h"
44 #include "wx/univ/inphand.h"
45 #include "wx/univ/theme.h"
47 // ----------------------------------------------------------------------------
48 // wxStdComboBoxInputHandler: allows the user to open/close the combo from kbd
49 // ----------------------------------------------------------------------------
51 class WXDLLEXPORT wxStdComboBoxInputHandler
: public wxStdInputHandler
54 wxStdComboBoxInputHandler(wxInputHandler
*inphand
);
56 virtual bool HandleKey(wxInputConsumer
*consumer
,
57 const wxKeyEvent
& event
,
61 // ----------------------------------------------------------------------------
62 // wxComboListBox is a listbox modified to be used as a popup window in a
64 // ----------------------------------------------------------------------------
66 class wxComboListBox
: public wxListBox
, public wxComboPopup
71 virtual ~wxComboListBox();
73 // implement wxComboPopup methods
74 virtual bool Create(wxWindow
* parent
);
75 virtual void SetStringValue(const wxString
& s
);
76 virtual wxString
GetStringValue() const;
77 virtual wxWindow
*GetControl() { return this; }
78 virtual void OnPopup();
79 virtual wxSize
GetAdjustedSize(int minWidth
, int prefHeight
, int maxHeight
);
81 // fix virtual function hiding
82 virtual void SetSelection(int n
) { DoSetSelection(n
, true); }
83 void SetSelection(int n
, bool select
) { DoSetSelection(n
, select
); }
85 // used to process wxUniv actions
86 bool PerformAction(const wxControlAction
& action
,
88 const wxString
& strArg
);
91 // set m_clicked value from here
92 void OnLeftUp(wxMouseEvent
& event
);
95 friend class wxComboBox
; // it accesses our DoGetItemClientData()
100 // ----------------------------------------------------------------------------
101 // event tables and such
102 // ----------------------------------------------------------------------------
104 BEGIN_EVENT_TABLE(wxComboListBox
, wxListBox
)
105 EVT_LEFT_UP(wxComboListBox::OnLeftUp
)
108 IMPLEMENT_DYNAMIC_CLASS2(wxComboBox
, wxControl
, wxComboCtrl
)
110 // ============================================================================
112 // ============================================================================
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 wxComboListBox::wxComboListBox() : wxListBox(), wxComboPopup()
122 bool wxComboListBox::Create(wxWindow
* parent
)
124 if ( !wxListBox::Create(parent
, wxID_ANY
,
125 wxDefaultPosition
, wxDefaultSize
,
128 ( m_combo
->GetWindowStyle() & wxCB_SORT
? wxLB_SORT
: 0 ) ) )
131 // we don't react to the mouse events outside the window at all
137 wxComboListBox::~wxComboListBox()
141 wxString
wxComboListBox::GetStringValue() const
143 return wxListBox::GetStringSelection();
146 void wxComboListBox::SetStringValue(const wxString
& value
)
148 if ( !value
.empty() )
150 if (FindString(value
) != wxNOT_FOUND
)
151 wxListBox::SetStringSelection(value
);
154 wxListBox::SetSelection(-1);
157 void wxComboListBox::OnPopup()
161 bool wxComboListBox::PerformAction(const wxControlAction
& action
,
163 const wxString
& strArg
)
166 if ( action
== wxACTION_LISTBOX_FIND
)
168 // we don't let the listbox handle this as instead of just using the
169 // single key presses, as usual, we use the text ctrl value as prefix
170 // and this is done by wxComboCtrl itself
174 return wxListBox::PerformAction(action
, numArg
, strArg
);
177 void wxComboListBox::OnLeftUp(wxMouseEvent
& event
)
179 // we should dismiss the combo now
180 // first update the combo and close the listbox
182 m_combo
->SetValue(wxListBox::GetStringSelection());
184 // next let the user code have the event
185 wxCommandEvent
evt(wxEVT_COMMAND_COMBOBOX_SELECTED
,m_combo
->GetId());
186 evt
.SetInt(wxListBox::GetSelection());
187 evt
.SetEventObject(m_combo
);
188 m_combo
->ProcessEvent(evt
);
193 wxSize
wxComboListBox::GetAdjustedSize(int minWidth
,
194 int WXUNUSED(prefHeight
),
197 wxSize bestSize
= wxListBox::GetBestSize();
198 return wxSize(wxMax(bestSize
.x
,minWidth
),
199 wxMin(bestSize
.y
,maxHeight
));
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
206 void wxComboBox::Init()
208 m_lbox
= (wxListBox
*)NULL
;
211 wxComboBox::wxComboBox(wxWindow
*parent
,
213 const wxString
& value
,
216 const wxArrayString
& choices
,
218 const wxValidator
& validator
,
219 const wxString
& name
)
223 Create(parent
, id
, value
, pos
, size
, choices
, style
, validator
, name
);
226 bool wxComboBox::Create(wxWindow
*parent
,
228 const wxString
& value
,
231 const wxArrayString
& choices
,
233 const wxValidator
& validator
,
234 const wxString
& name
)
236 wxCArrayString
chs(choices
);
238 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
239 chs
.GetStrings(), style
, validator
, name
);
242 bool wxComboBox::Create(wxWindow
*parent
,
244 const wxString
& value
,
248 const wxString choices
[],
250 const wxValidator
& validator
,
251 const wxString
& name
)
253 if ( !wxComboCtrl::Create(parent
, id
, value
, pos
, size
, style
,
259 wxComboListBox
*combolbox
= new wxComboListBox();
260 SetPopupControl(combolbox
);
263 m_lbox
->Set(n
, choices
);
268 wxComboBox::~wxComboBox()
272 // ----------------------------------------------------------------------------
273 // wxComboBox methods forwarded to wxTextCtrl
274 // ----------------------------------------------------------------------------
276 wxString
wxComboBox::GetValue() const
278 return wxComboCtrl::GetValue();
281 void wxComboBox::SetValue(const wxString
& value
)
283 wxComboCtrl::SetValue(value
);
286 void wxComboBox::Copy()
288 if ( GetTextCtrl() ) GetTextCtrl()->Copy();
291 void wxComboBox::Cut()
293 if ( GetTextCtrl() ) GetTextCtrl()->Cut();
296 void wxComboBox::Paste()
298 if ( GetTextCtrl() ) GetTextCtrl()->Paste();
301 void wxComboBox::SetInsertionPoint(long pos
)
303 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPoint(pos
);
306 void wxComboBox::SetInsertionPointEnd()
308 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPointEnd();
311 long wxComboBox::GetInsertionPoint() const
314 return GetTextCtrl()->GetInsertionPoint();
318 wxTextPos
wxComboBox::GetLastPosition() const
321 return GetTextCtrl()->GetLastPosition();
325 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
327 if ( GetTextCtrl() ) GetTextCtrl()->Replace(from
, to
, value
);
330 void wxComboBox::Remove(long from
, long to
)
332 if ( GetTextCtrl() ) GetTextCtrl()->Remove(from
, to
);
335 void wxComboBox::SetSelection(long from
, long to
)
337 if ( GetTextCtrl() ) GetTextCtrl()->SetSelection(from
, to
);
340 void wxComboBox::SetEditable(bool editable
)
342 if ( GetTextCtrl() ) GetTextCtrl()->SetEditable(editable
);
345 // ----------------------------------------------------------------------------
346 // wxComboBox methods forwarded to wxListBox
347 // ----------------------------------------------------------------------------
349 void wxComboBox::DoClear()
352 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString
);
355 void wxComboBox::DoDeleteOneItem(unsigned int n
)
357 wxCHECK_RET( IsValid(n
), _T("invalid index in wxComboBox::Delete") );
359 if (GetSelection() == (int)n
)
360 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString
);
362 GetLBox()->Delete(n
);
365 unsigned int wxComboBox::GetCount() const
367 return GetLBox()->GetCount();
370 wxString
wxComboBox::GetString(unsigned int n
) const
372 wxCHECK_MSG( IsValid(n
), wxEmptyString
, _T("invalid index in wxComboBox::GetString") );
374 return GetLBox()->GetString(n
);
377 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
379 wxCHECK_RET( IsValid(n
), _T("invalid index in wxComboBox::SetString") );
381 GetLBox()->SetString(n
, s
);
384 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
386 return GetLBox()->FindString(s
, bCase
);
389 void wxComboBox::SetSelection(int n
)
391 wxCHECK_RET( (n
== wxNOT_FOUND
|| IsValid(n
)), _T("invalid index in wxComboBox::Select") );
393 GetLBox()->SetSelection(n
);
396 if ( n
!= wxNOT_FOUND
)
397 str
= GetLBox()->GetString(n
);
402 int wxComboBox::GetSelection() const
404 #if 1 // FIXME:: What is the correct behavior?
405 // if the current value isn't one of the listbox strings, return -1
406 return GetLBox()->GetSelection();
408 // Why oh why is this done this way?
409 // It is not because the value displayed in the text can be found
410 // in the list that it is the item that is selected!
411 return FindString(if ( GetTextCtrl() ) GetTextCtrl()->GetValue());
415 void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType
)
417 GetLBox()->SetClientDataType(clientDataItemsType
);
420 int wxComboBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
422 void **clientData
, wxClientDataType type
)
424 return GetLBox()->DoInsertItems(items
, pos
, clientData
, type
);
427 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
429 GetLBox()->DoSetItemClientData(n
, clientData
);
432 void *wxComboBox::DoGetItemClientData(unsigned int n
) const
434 return GetLBox()->DoGetItemClientData(n
);
437 bool wxComboBox::IsEditable() const
439 return GetTextCtrl() != NULL
&& (!HasFlag(wxCB_READONLY
) || GetTextCtrl()->IsEditable() );
442 void wxComboBox::Undo()
445 if ( GetTextCtrl() ) GetTextCtrl()->Undo();
448 void wxComboBox::Redo()
451 if ( GetTextCtrl() ) GetTextCtrl()->Redo();
454 void wxComboBox::SelectAll()
456 if ( GetTextCtrl() ) GetTextCtrl()->SelectAll();
459 bool wxComboBox::CanCopy() const
461 if (GetTextCtrl() != NULL
)
462 return GetTextCtrl()->CanCopy();
467 bool wxComboBox::CanCut() const
469 if (GetTextCtrl() != NULL
)
470 return GetTextCtrl()->CanCut();
475 bool wxComboBox::CanPaste() const
478 return GetTextCtrl()->CanPaste();
483 bool wxComboBox::CanUndo() const
486 return GetTextCtrl()->CanUndo();
491 bool wxComboBox::CanRedo() const
494 return GetTextCtrl()->CanRedo();
500 // ----------------------------------------------------------------------------
501 // wxStdComboBoxInputHandler
502 // ----------------------------------------------------------------------------
504 wxStdComboBoxInputHandler::wxStdComboBoxInputHandler(wxInputHandler
*inphand
)
505 : wxStdInputHandler(inphand
)
509 bool wxStdComboBoxInputHandler::HandleKey(wxInputConsumer
*consumer
,
510 const wxKeyEvent
& event
,
515 wxControlAction action
;
516 switch ( event
.GetKeyCode() )
519 action
= wxACTION_COMBOBOX_POPUP
;
523 action
= wxACTION_COMBOBOX_DISMISS
;
527 if ( !action
.IsEmpty() )
529 consumer
->PerformAction(action
);
535 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
539 wxInputHandler
*wxComboBox::GetStdInputHandler(wxInputHandler
*handlerDef
)
541 static wxStdComboBoxInputHandler
s_handler(handlerDef
);
546 #endif // wxUSE_COMBOBOX