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
57 wxComboListBox(wxComboControlBase
*combo
);
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(wxComboControlBase
*combo
)
109 bool wxComboListBox::Create(wxWindow
* parent
)
111 if ( !wxListBox::Create(parent
, wxID_ANY
,
112 wxDefaultPosition
, wxDefaultSize
,
114 wxBORDER_SIMPLE
| wxLB_INT_HEIGHT
|
115 m_combo
->GetWindowStyle() & wxCB_SORT
? wxLB_SORT
: 0) )
118 // we don't react to the mouse events outside the window at all
124 wxComboListBox::~wxComboListBox()
128 wxString
wxComboListBox::GetStringValue() const
130 return wxListBox::GetStringSelection();
133 void wxComboListBox::SetStringValue(const wxString
& value
)
135 if ( !value
.empty() )
136 wxListBox::SetStringSelection(value
);
138 wxListBox::SetSelection(-1);
141 // PRE-GLOBAL WXCOMBOCONTROL CODE:
143 // FindItem() would just find the current item for an empty string (it
144 // always matches), but we want to show the first one in such case
147 if ( GetCount() > 0 )
149 wxListBox::SetSelection(0);
151 //else: empty listbox - nothing to do
153 else if ( !FindItem(value) )
160 void wxComboListBox::OnPopup()
164 bool wxComboListBox::PerformAction(const wxControlAction
& action
,
166 const wxString
& strArg
)
169 if ( action
== wxACTION_LISTBOX_FIND
)
171 // we don't let the listbox handle this as instead of just using the
172 // single key presses, as usual, we use the text ctrl value as prefix
173 // and this is done by wxComboControl itself
177 return wxListBox::PerformAction(action
, numArg
, strArg
);
180 void wxComboListBox::OnLeftUp(wxMouseEvent
& event
)
182 // we should dismiss the combo now
183 // first update the combo and close the listbox
185 m_combo
->SetValue(wxListBox::GetStringSelection());
187 // next let the user code have the event
188 wxCommandEvent
evt(wxEVT_COMMAND_COMBOBOX_SELECTED
,m_combo
->GetId());
189 evt
.SetInt(wxListBox::GetSelection());
190 evt
.SetEventObject(m_combo
);
191 m_combo
->ProcessEvent(evt
);
196 wxSize
wxComboListBox::GetAdjustedSize(int minWidth
,
197 int WXUNUSED(prefHeight
),
200 wxSize bestSize
= wxListBox::GetBestSize();
201 return wxSize(wxMax(bestSize
.x
,minWidth
),
202 wxMin(bestSize
.y
,maxHeight
));
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 void wxComboBox::Init()
211 m_lbox
= (wxListBox
*)NULL
;
214 wxComboBox::wxComboBox(wxWindow
*parent
,
216 const wxString
& value
,
219 const wxArrayString
& choices
,
221 const wxValidator
& validator
,
222 const wxString
& name
)
226 Create(parent
, id
, value
, pos
, size
, choices
, style
, validator
, name
);
229 bool wxComboBox::Create(wxWindow
*parent
,
231 const wxString
& value
,
234 const wxArrayString
& choices
,
236 const wxValidator
& validator
,
237 const wxString
& name
)
239 wxCArrayString
chs(choices
);
241 return Create(parent
, id
, value
, pos
, size
, chs
.GetCount(),
242 chs
.GetStrings(), style
, validator
, name
);
245 bool wxComboBox::Create(wxWindow
*parent
,
247 const wxString
& value
,
251 const wxString choices
[],
253 const wxValidator
& validator
,
254 const wxString
& name
)
256 if ( !wxComboControl::Create(parent
, id
, value
, pos
, size
, style
,
262 wxComboListBox
*combolbox
= new wxComboListBox(this);
263 SetPopupControl(combolbox
);
266 m_lbox
->Set(n
, choices
);
271 wxComboBox::~wxComboBox()
275 // ----------------------------------------------------------------------------
276 // wxComboBox methods forwarded to wxTextCtrl
277 // ----------------------------------------------------------------------------
279 wxString
wxComboBox::GetValue() const
281 return wxComboControl::GetValue();
284 void wxComboBox::SetValue(const wxString
& value
)
286 wxComboControl::SetValue(value
);
289 void wxComboBox::Copy()
291 if ( GetTextCtrl() ) GetTextCtrl()->Copy();
294 void wxComboBox::Cut()
296 if ( GetTextCtrl() ) GetTextCtrl()->Cut();
299 void wxComboBox::Paste()
301 if ( GetTextCtrl() ) GetTextCtrl()->Paste();
304 void wxComboBox::SetInsertionPoint(long pos
)
306 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPoint(pos
);
309 void wxComboBox::SetInsertionPointEnd()
311 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPointEnd();
314 long wxComboBox::GetInsertionPoint() const
317 return GetTextCtrl()->GetInsertionPoint();
321 wxTextPos
wxComboBox::GetLastPosition() const
324 return GetTextCtrl()->GetLastPosition();
328 void wxComboBox::Replace(long from
, long to
, const wxString
& value
)
330 if ( GetTextCtrl() ) GetTextCtrl()->Replace(from
, to
, value
);
333 void wxComboBox::Remove(long from
, long to
)
335 if ( GetTextCtrl() ) GetTextCtrl()->Remove(from
, to
);
338 void wxComboBox::SetSelection(long from
, long to
)
340 if ( GetTextCtrl() ) GetTextCtrl()->SetSelection(from
, to
);
343 void wxComboBox::SetEditable(bool editable
)
345 if ( GetTextCtrl() ) GetTextCtrl()->SetEditable(editable
);
348 // ----------------------------------------------------------------------------
349 // wxComboBox methods forwarded to wxListBox
350 // ----------------------------------------------------------------------------
352 void wxComboBox::Clear()
355 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString
);
358 void wxComboBox::Delete(unsigned int n
)
360 wxCHECK_RET( IsValid(n
), _T("invalid index in wxComboBox::Delete") );
362 if (GetSelection() == (int)n
)
363 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString
);
365 GetLBox()->Delete(n
);
368 unsigned int wxComboBox::GetCount() const
370 return GetLBox()->GetCount();
373 wxString
wxComboBox::GetString(unsigned int n
) const
375 wxCHECK_MSG( IsValid(n
), wxEmptyString
, _T("invalid index in wxComboBox::GetString") );
377 return GetLBox()->GetString(n
);
380 void wxComboBox::SetString(unsigned int n
, const wxString
& s
)
382 wxCHECK_RET( IsValid(n
), _T("invalid index in wxComboBox::SetString") );
384 GetLBox()->SetString(n
, s
);
387 int wxComboBox::FindString(const wxString
& s
, bool bCase
) const
389 return GetLBox()->FindString(s
, bCase
);
392 void wxComboBox::SetSelection(int n
)
394 wxCHECK_RET( IsValid(n
), _T("invalid index in wxComboBox::Select") );
396 GetLBox()->SetSelection(n
);
397 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(GetLBox()->GetString(n
));
400 int wxComboBox::GetSelection() const
402 #if 1 // FIXME:: What is the correct behavior?
403 // if the current value isn't one of the listbox strings, return -1
404 return GetLBox()->GetSelection();
406 // Why oh why is this done this way?
407 // It is not because the value displayed in the text can be found
408 // in the list that it is the item that is selected!
409 return FindString(if ( GetTextCtrl() ) GetTextCtrl()->GetValue());
413 int wxComboBox::DoAppend(const wxString
& item
)
415 return GetLBox()->Append(item
);
418 int wxComboBox::DoInsert(const wxString
& item
, unsigned int pos
)
420 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT
), -1, wxT("can't insert into sorted list"));
421 wxCHECK_MSG(IsValidInsert(pos
), -1, wxT("invalid index"));
423 if (pos
== GetCount())
424 return DoAppend(item
);
426 GetLBox()->Insert(item
, pos
);
430 void wxComboBox::DoSetItemClientData(unsigned int n
, void* clientData
)
432 GetLBox()->SetClientData(n
, clientData
);
435 void *wxComboBox::DoGetItemClientData(unsigned int n
) const
437 return GetLBox()->GetClientData(n
);
440 void wxComboBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
442 GetLBox()->SetClientObject(n
, clientData
);
445 wxClientData
* wxComboBox::DoGetItemClientObject(unsigned int n
) const
447 return GetLBox()->GetClientObject(n
);
450 bool wxComboBox::IsEditable() const
452 return GetTextCtrl() != NULL
&& (!HasFlag(wxCB_READONLY
) || GetTextCtrl()->IsEditable() );
455 void wxComboBox::Undo()
458 if ( GetTextCtrl() ) GetTextCtrl()->Undo();
461 void wxComboBox::Redo()
464 if ( GetTextCtrl() ) GetTextCtrl()->Redo();
467 void wxComboBox::SelectAll()
469 if ( GetTextCtrl() ) GetTextCtrl()->SelectAll();
472 bool wxComboBox::CanCopy() const
474 if (GetTextCtrl() != NULL
)
475 return GetTextCtrl()->CanCopy();
480 bool wxComboBox::CanCut() const
482 if (GetTextCtrl() != NULL
)
483 return GetTextCtrl()->CanCut();
488 bool wxComboBox::CanPaste() const
491 return GetTextCtrl()->CanPaste();
496 bool wxComboBox::CanUndo() const
499 return GetTextCtrl()->CanUndo();
504 bool wxComboBox::CanRedo() const
507 return GetTextCtrl()->CanRedo();
513 // ----------------------------------------------------------------------------
514 // wxStdComboBoxInputHandler
515 // ----------------------------------------------------------------------------
517 wxStdComboBoxInputHandler::wxStdComboBoxInputHandler(wxInputHandler
*inphand
)
518 : wxStdInputHandler(inphand
)
522 bool wxStdComboBoxInputHandler::HandleKey(wxInputConsumer
*consumer
,
523 const wxKeyEvent
& event
,
528 wxControlAction action
;
529 switch ( event
.GetKeyCode() )
532 action
= wxACTION_COMBOBOX_POPUP
;
536 action
= wxACTION_COMBOBOX_DISMISS
;
540 if ( !action
.IsEmpty() )
542 consumer
->PerformAction(action
);
548 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
552 #endif // wxUSE_COMBOBOX