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"
48 // ----------------------------------------------------------------------------
49 // wxComboListBox is a listbox modified to be used as a popup window in a
51 // ----------------------------------------------------------------------------
53 class wxComboListBox
: public wxListBox
, public wxComboPopup
58 virtual ~wxComboListBox();
60 // implement wxComboPopup methods
61 virtual bool Create(wxWindow
* parent
);
62 virtual void SetStringValue(const wxString
& s
);
63 virtual wxString
GetStringValue() const;
64 virtual wxWindow
*GetControl() { return this; }
65 virtual void OnPopup();
66 virtual wxSize
GetAdjustedSize(int minWidth
, int prefHeight
, int maxHeight
);
68 // fix virtual function hiding
69 virtual void SetSelection(int n
) { DoSetSelection(n
, true); }
70 void SetSelection(int n
, bool select
) { DoSetSelection(n
, select
); }
72 // used to process wxUniv actions
73 bool PerformAction(const wxControlAction
& action
,
75 const wxString
& strArg
);
78 // set m_clicked value from here
79 void OnLeftUp(wxMouseEvent
& event
);
85 // ----------------------------------------------------------------------------
86 // event tables and such
87 // ----------------------------------------------------------------------------
89 BEGIN_EVENT_TABLE(wxComboListBox
, wxListBox
)
90 EVT_LEFT_UP(wxComboListBox::OnLeftUp
)
93 IMPLEMENT_DYNAMIC_CLASS2(wxComboBox
, wxControl
, wxComboControl
)
95 // ============================================================================
97 // ============================================================================
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 wxComboListBox::wxComboListBox() : wxListBox(), wxComboPopup()
107 bool wxComboListBox::Create(wxWindow
* parent
)
109 if ( !wxListBox::Create(parent
, wxID_ANY
,
110 wxDefaultPosition
, wxDefaultSize
,
112 wxBORDER_SIMPLE
| wxLB_INT_HEIGHT
|
113 m_combo
->GetWindowStyle() & wxCB_SORT
? wxLB_SORT
: 0) )
116 // we don't react to the mouse events outside the window at all
122 wxComboListBox::~wxComboListBox()
126 wxString
wxComboListBox::GetStringValue() const
128 return wxListBox::GetStringSelection();
131 void wxComboListBox::SetStringValue(const wxString
& value
)
133 if ( !value
.empty() )
134 wxListBox::SetStringSelection(value
);
136 wxListBox::SetSelection(-1);
139 void wxComboListBox::OnPopup()
143 bool wxComboListBox::PerformAction(const wxControlAction
& action
,
145 const wxString
& strArg
)
148 if ( action
== wxACTION_LISTBOX_FIND
)
150 // we don't let the listbox handle this as instead of just using the
151 // single key presses, as usual, we use the text ctrl value as prefix
152 // and this is done by wxComboControl itself
156 return wxListBox::PerformAction(action
, numArg
, strArg
);
159 void wxComboListBox::OnLeftUp(wxMouseEvent
& event
)
161 // we should dismiss the combo now
162 // first update the combo and close the listbox
164 m_combo
->SetValue(wxListBox::GetStringSelection());
166 // next let the user code have the event
167 wxCommandEvent
evt(wxEVT_COMMAND_COMBOBOX_SELECTED
,m_combo
->GetId());
168 evt
.SetInt(wxListBox::GetSelection());
169 evt
.SetEventObject(m_combo
);
170 m_combo
->ProcessEvent(evt
);
175 wxSize
wxComboListBox::GetAdjustedSize(int minWidth
,
176 int WXUNUSED(prefHeight
),
179 wxSize bestSize
= wxListBox::GetBestSize();
180 return wxSize(wxMax(bestSize
.x
,minWidth
),
181 wxMin(bestSize
.y
,maxHeight
));
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
188 void wxComboBox::Init()
190 m_lbox
= (wxListBox
*)NULL
;
193 wxComboBox::wxComboBox(wxWindow
*parent
,
195 const wxString
& value
,
198 const wxArrayString
& choices
,
200 const wxValidator
& validator
,
201 const wxString
& name
)
205 Create(parent
, id
, value
, pos
, size
, choices
, style
, validator
, name
);
208 bool wxComboBox::Create(wxWindow
*parent
,
210 const wxString
& value
,
213 const wxArrayString
& choices
,
215 const wxValidator
& validator
,
216 const wxString
& name
)
218 wxCArrayString
chs(choices
);
220 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
221 chs
.GetStrings(), style
, validator
, name
);
224 bool wxComboBox::Create(wxWindow
*parent
,
226 const wxString
& value
,
230 const wxString choices
[],
232 const wxValidator
& validator
,
233 const wxString
& name
)
235 if ( !wxComboControl::Create(parent
, id
, value
, pos
, size
, style
,
241 wxComboListBox
*combolbox
= new wxComboListBox();
242 SetPopupControl(combolbox
);
245 m_lbox
->Set(n
, choices
);
250 wxComboBox::~wxComboBox()
254 // ----------------------------------------------------------------------------
255 // wxComboBox methods forwarded to wxTextCtrl
256 // ----------------------------------------------------------------------------
258 wxString
wxComboBox::GetValue() const
260 return wxComboControl::GetValue();
263 void wxComboBox::SetValue(const wxString
& value
)
265 wxComboControl::SetValue(value
);
268 void wxComboBox::Copy()
270 if ( GetTextCtrl() ) GetTextCtrl()->Copy();
273 void wxComboBox::Cut()
275 if ( GetTextCtrl() ) GetTextCtrl()->Cut();
278 void wxComboBox::Paste()
280 if ( GetTextCtrl() ) GetTextCtrl()->Paste();
283 void wxComboBox::SetInsertionPoint(long pos
)
285 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPoint(pos
);
288 void wxComboBox::SetInsertionPointEnd()
290 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPointEnd();
293 long wxComboBox::GetInsertionPoint() const
296 return GetTextCtrl()->GetInsertionPoint();
300 wxTextPos
wxComboBox::GetLastPosition() const
303 return GetTextCtrl()->GetLastPosition();
307 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
309 if ( GetTextCtrl() ) GetTextCtrl()->Replace(from
, to
, value
);
312 void wxComboBox::Remove(long from
, long to
)
314 if ( GetTextCtrl() ) GetTextCtrl()->Remove(from
, to
);
317 void wxComboBox::SetSelection(long from
, long to
)
319 if ( GetTextCtrl() ) GetTextCtrl()->SetSelection(from
, to
);
322 void wxComboBox::SetEditable(bool editable
)
324 if ( GetTextCtrl() ) GetTextCtrl()->SetEditable(editable
);
327 // ----------------------------------------------------------------------------
328 // wxComboBox methods forwarded to wxListBox
329 // ----------------------------------------------------------------------------
331 void wxComboBox::Clear()
334 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString
);
337 void wxComboBox::Delete(unsigned int n
)
339 wxCHECK_RET( IsValid(n
), _T("invalid index in wxComboBox::Delete") );
341 if (GetSelection() == (int)n
)
342 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString
);
344 GetLBox()->Delete(n
);
347 unsigned int wxComboBox::GetCount() const
349 return GetLBox()->GetCount();
352 wxString
wxComboBox::GetString(unsigned int n
) const
354 wxCHECK_MSG( IsValid(n
), wxEmptyString
, _T("invalid index in wxComboBox::GetString") );
356 return GetLBox()->GetString(n
);
359 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
361 wxCHECK_RET( IsValid(n
), _T("invalid index in wxComboBox::SetString") );
363 GetLBox()->SetString(n
, s
);
366 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
368 return GetLBox()->FindString(s
, bCase
);
371 void wxComboBox::SetSelection(int n
)
373 wxCHECK_RET( IsValid(n
), _T("invalid index in wxComboBox::Select") );
375 GetLBox()->SetSelection(n
);
376 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(GetLBox()->GetString(n
));
379 int wxComboBox::GetSelection() const
381 #if 1 // FIXME:: What is the correct behavior?
382 // if the current value isn't one of the listbox strings, return -1
383 return GetLBox()->GetSelection();
385 // Why oh why is this done this way?
386 // It is not because the value displayed in the text can be found
387 // in the list that it is the item that is selected!
388 return FindString(if ( GetTextCtrl() ) GetTextCtrl()->GetValue());
392 int wxComboBox::DoAppend(const wxString
& item
)
394 return GetLBox()->Append(item
);
397 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
399 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
400 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
402 if (pos
== GetCount())
403 return DoAppend(item
);
405 GetLBox()->Insert(item
, pos
);
409 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
411 GetLBox()->SetClientData(n
, clientData
);
414 void *wxComboBox::DoGetItemClientData(unsigned int n
) const
416 return GetLBox()->GetClientData(n
);
419 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
421 GetLBox()->SetClientObject(n
, clientData
);
424 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
426 return GetLBox()->GetClientObject(n
);
429 bool wxComboBox::IsEditable() const
431 return GetTextCtrl() != NULL
&& (!HasFlag(wxCB_READONLY
) || GetTextCtrl()->IsEditable() );
434 void wxComboBox::Undo()
437 if ( GetTextCtrl() ) GetTextCtrl()->Undo();
440 void wxComboBox::Redo()
443 if ( GetTextCtrl() ) GetTextCtrl()->Redo();
446 void wxComboBox::SelectAll()
448 if ( GetTextCtrl() ) GetTextCtrl()->SelectAll();
451 bool wxComboBox::CanCopy() const
453 if (GetTextCtrl() != NULL
)
454 return GetTextCtrl()->CanCopy();
459 bool wxComboBox::CanCut() const
461 if (GetTextCtrl() != NULL
)
462 return GetTextCtrl()->CanCut();
467 bool wxComboBox::CanPaste() const
470 return GetTextCtrl()->CanPaste();
475 bool wxComboBox::CanUndo() const
478 return GetTextCtrl()->CanUndo();
483 bool wxComboBox::CanRedo() const
486 return GetTextCtrl()->CanRedo();
492 // ----------------------------------------------------------------------------
493 // wxStdComboBoxInputHandler
494 // ----------------------------------------------------------------------------
496 wxStdComboBoxInputHandler::wxStdComboBoxInputHandler(wxInputHandler
*inphand
)
497 : wxStdInputHandler(inphand
)
501 bool wxStdComboBoxInputHandler::HandleKey(wxInputConsumer
*consumer
,
502 const wxKeyEvent
& event
,
507 wxControlAction action
;
508 switch ( event
.GetKeyCode() )
511 action
= wxACTION_COMBOBOX_POPUP
;
515 action
= wxACTION_COMBOBOX_DISMISS
;
519 if ( !action
.IsEmpty() )
521 consumer
->PerformAction(action
);
527 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
531 #endif // wxUSE_COMBOBOX