]> git.saurik.com Git - wxWidgets.git/blame - src/univ/combobox.cpp
Compile fixes for wxUSE_COLLPANE=0
[wxWidgets.git] / src / univ / combobox.cpp
CommitLineData
1e6feb95 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/univ/combobox.cpp
6d0ce565 3// Purpose: wxComboBox implementation
1e6feb95
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 15.12.00
7// RCS-ID: $Id$
442b35b5 8// Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
65571936 9// Licence: wxWindows licence
1e6feb95
VZ
10/////////////////////////////////////////////////////////////////////////////
11
1e6feb95
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
1e6feb95
VZ
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#if wxUSE_COMBOBOX
27
28#ifndef WX_PRECOMP
29 #include "wx/log.h"
30
31 #include "wx/button.h"
32 #include "wx/combobox.h"
33 #include "wx/listbox.h"
34 #include "wx/textctrl.h"
8cb172b4 35 #include "wx/bmpbuttn.h"
1e6feb95
VZ
36
37 #include "wx/validate.h"
38#endif
39
d4e5272b 40#include "wx/tooltip.h"
a340b80d 41#include "wx/combo.h"
1e6feb95
VZ
42
43#include "wx/univ/renderer.h"
44#include "wx/univ/inphand.h"
45#include "wx/univ/theme.h"
46
9467bdb7
VZ
47// ----------------------------------------------------------------------------
48// wxStdComboBoxInputHandler: allows the user to open/close the combo from kbd
49// ----------------------------------------------------------------------------
50
51class WXDLLEXPORT wxStdComboBoxInputHandler : public wxStdInputHandler
52{
53public:
54 wxStdComboBoxInputHandler(wxInputHandler *inphand);
55
56 virtual bool HandleKey(wxInputConsumer *consumer,
57 const wxKeyEvent& event,
58 bool pressed);
59};
1e6feb95
VZ
60
61// ----------------------------------------------------------------------------
62// wxComboListBox is a listbox modified to be used as a popup window in a
63// combobox
64// ----------------------------------------------------------------------------
65
66class wxComboListBox : public wxListBox, public wxComboPopup
67{
68public:
69 // ctor and dtor
e051d008 70 wxComboListBox();
1e6feb95
VZ
71 virtual ~wxComboListBox();
72
73 // implement wxComboPopup methods
a340b80d
VZ
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);
1e6feb95 80
d2fde247
VZ
81 // fix virtual function hiding
82 virtual void SetSelection(int n) { DoSetSelection(n, true); }
83 void SetSelection(int n, bool select) { DoSetSelection(n, select); }
84
6f02a879
VZ
85 // used to process wxUniv actions
86 bool PerformAction(const wxControlAction& action,
87 long numArg,
88 const wxString& strArg);
89
1e6feb95 90protected:
55f095d4
VZ
91 // set m_clicked value from here
92 void OnLeftUp(wxMouseEvent& event);
93
1e6feb95 94private:
131b1fba
VZ
95 friend class wxComboBox; // it accesses our DoGetItemClientData()
96
1e6feb95
VZ
97 DECLARE_EVENT_TABLE()
98};
99
100// ----------------------------------------------------------------------------
101// event tables and such
102// ----------------------------------------------------------------------------
103
1e6feb95 104BEGIN_EVENT_TABLE(wxComboListBox, wxListBox)
55f095d4 105 EVT_LEFT_UP(wxComboListBox::OnLeftUp)
1e6feb95
VZ
106END_EVENT_TABLE()
107
1e6feb95
VZ
108// ============================================================================
109// implementation
110// ============================================================================
111
112// ----------------------------------------------------------------------------
a340b80d 113// wxComboListBox
1e6feb95
VZ
114// ----------------------------------------------------------------------------
115
e051d008 116wxComboListBox::wxComboListBox() : wxListBox(), wxComboPopup()
1e6feb95 117{
1e6feb95
VZ
118}
119
a340b80d 120bool wxComboListBox::Create(wxWindow* parent)
1e6feb95 121{
a340b80d
VZ
122 if ( !wxListBox::Create(parent, wxID_ANY,
123 wxDefaultPosition, wxDefaultSize,
124 0, NULL,
750040fa
WS
125 wxBORDER_SIMPLE |
126 ( m_combo->GetWindowStyle() & wxCB_SORT ? wxLB_SORT : 0 ) ) )
a290fa5a 127 return false;
1e6feb95 128
a340b80d
VZ
129 // we don't react to the mouse events outside the window at all
130 StopAutoScrolling();
1e6feb95 131
a290fa5a 132 return true;
1e6feb95
VZ
133}
134
a340b80d 135wxComboListBox::~wxComboListBox()
1e6feb95 136{
1e6feb95
VZ
137}
138
a340b80d 139wxString wxComboListBox::GetStringValue() const
1e6feb95 140{
a340b80d 141 return wxListBox::GetStringSelection();
1e6feb95
VZ
142}
143
a340b80d 144void wxComboListBox::SetStringValue(const wxString& value)
1e6feb95 145{
615a96e3
JS
146 if ( !value.empty() )
147 {
f6fc052a
JS
148 if (FindString(value) != wxNOT_FOUND)
149 wxListBox::SetStringSelection(value);
150 }
a340b80d
VZ
151 else
152 wxListBox::SetSelection(-1);
1e6feb95
VZ
153}
154
a340b80d 155void wxComboListBox::OnPopup()
1e6feb95
VZ
156{
157}
158
159bool wxComboListBox::PerformAction(const wxControlAction& action,
160 long numArg,
161 const wxString& strArg)
162
163{
164 if ( action == wxACTION_LISTBOX_FIND )
165 {
166 // we don't let the listbox handle this as instead of just using the
167 // single key presses, as usual, we use the text ctrl value as prefix
a57d600f 168 // and this is done by wxComboCtrl itself
a290fa5a 169 return true;
1e6feb95
VZ
170 }
171
172 return wxListBox::PerformAction(action, numArg, strArg);
173}
174
55f095d4
VZ
175void wxComboListBox::OnLeftUp(wxMouseEvent& event)
176{
177 // we should dismiss the combo now
a340b80d
VZ
178 // first update the combo and close the listbox
179 Dismiss();
180 m_combo->SetValue(wxListBox::GetStringSelection());
55f095d4 181
a340b80d
VZ
182 // next let the user code have the event
183 wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED,m_combo->GetId());
184 evt.SetInt(wxListBox::GetSelection());
185 evt.SetEventObject(m_combo);
3b7fa206 186 m_combo->ProcessWindowEvent(evt);
55f095d4 187
a340b80d 188 event.Skip();
e2ca829e
JS
189}
190
a340b80d
VZ
191wxSize wxComboListBox::GetAdjustedSize(int minWidth,
192 int WXUNUSED(prefHeight),
193 int maxHeight)
1e6feb95 194{
a340b80d
VZ
195 wxSize bestSize = wxListBox::GetBestSize();
196 return wxSize(wxMax(bestSize.x,minWidth),
197 wxMin(bestSize.y,maxHeight));
1e6feb95
VZ
198}
199
200// ----------------------------------------------------------------------------
201// wxComboBox
202// ----------------------------------------------------------------------------
203
204void wxComboBox::Init()
205{
d3b9f782 206 m_lbox = NULL;
1e6feb95
VZ
207}
208
584ad2a3
MB
209wxComboBox::wxComboBox(wxWindow *parent,
210 wxWindowID id,
211 const wxString& value,
212 const wxPoint& pos,
213 const wxSize& size,
214 const wxArrayString& choices,
215 long style,
216 const wxValidator& validator,
217 const wxString& name)
218{
219 Init();
220
221 Create(parent, id, value, pos, size, choices, style, validator, name);
222}
223
224bool wxComboBox::Create(wxWindow *parent,
225 wxWindowID id,
226 const wxString& value,
227 const wxPoint& pos,
228 const wxSize& size,
229 const wxArrayString& choices,
230 long style,
231 const wxValidator& validator,
232 const wxString& name)
233{
234 wxCArrayString chs(choices);
235
236 return Create(parent, id, value, pos, size, chs.GetCount(),
237 chs.GetStrings(), style, validator, name);
238}
239
1e6feb95
VZ
240bool wxComboBox::Create(wxWindow *parent,
241 wxWindowID id,
242 const wxString& value,
243 const wxPoint& pos,
244 const wxSize& size,
245 int n,
ba1e9d6c 246 const wxString choices[],
1e6feb95
VZ
247 long style,
248 const wxValidator& validator,
249 const wxString& name)
250{
a57d600f 251 if ( !wxComboCtrl::Create(parent, id, value, pos, size, style,
1e6feb95
VZ
252 validator, name) )
253 {
a290fa5a 254 return false;
1e6feb95
VZ
255 }
256
e051d008 257 wxComboListBox *combolbox = new wxComboListBox();
a340b80d
VZ
258 SetPopupControl(combolbox);
259
1e6feb95
VZ
260 m_lbox = combolbox;
261 m_lbox->Set(n, choices);
262
a290fa5a 263 return true;
1e6feb95
VZ
264}
265
266wxComboBox::~wxComboBox()
267{
268}
269
270// ----------------------------------------------------------------------------
271// wxComboBox methods forwarded to wxTextCtrl
272// ----------------------------------------------------------------------------
273
3373e900 274wxString wxComboBox::DoGetValue() const
1e6feb95 275{
59060c07 276 return GetTextCtrl() ? GetTextCtrl()->GetValue() : m_valueString;
1e6feb95
VZ
277}
278
279void wxComboBox::SetValue(const wxString& value)
280{
0aae39ef
VZ
281 if ( GetTextCtrl() )
282 GetTextCtrl()->SetValue(value);
59060c07
VZ
283 else
284 m_valueString = value;
1e6feb95
VZ
285}
286
0ec1179b
VZ
287void wxComboBox::WriteText(const wxString& value)
288{
289 if ( GetTextCtrl() ) GetTextCtrl()->WriteText(value);
290}
291
1e6feb95
VZ
292void wxComboBox::Copy()
293{
a340b80d 294 if ( GetTextCtrl() ) GetTextCtrl()->Copy();
1e6feb95
VZ
295}
296
297void wxComboBox::Cut()
298{
a340b80d 299 if ( GetTextCtrl() ) GetTextCtrl()->Cut();
1e6feb95
VZ
300}
301
302void wxComboBox::Paste()
303{
a340b80d 304 if ( GetTextCtrl() ) GetTextCtrl()->Paste();
1e6feb95
VZ
305}
306
307void wxComboBox::SetInsertionPoint(long pos)
308{
a340b80d 309 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPoint(pos);
1e6feb95
VZ
310}
311
312void wxComboBox::SetInsertionPointEnd()
313{
a340b80d 314 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPointEnd();
1e6feb95
VZ
315}
316
317long wxComboBox::GetInsertionPoint() const
318{
a340b80d
VZ
319 if ( GetTextCtrl() )
320 return GetTextCtrl()->GetInsertionPoint();
321 return -1;
1e6feb95
VZ
322}
323
7d8268a1 324wxTextPos wxComboBox::GetLastPosition() const
1e6feb95 325{
a340b80d
VZ
326 if ( GetTextCtrl() )
327 return GetTextCtrl()->GetLastPosition();
328 return -1;
1e6feb95
VZ
329}
330
331void wxComboBox::Replace(long from, long to, const wxString& value)
332{
a340b80d 333 if ( GetTextCtrl() ) GetTextCtrl()->Replace(from, to, value);
1e6feb95
VZ
334}
335
336void wxComboBox::Remove(long from, long to)
337{
a340b80d 338 if ( GetTextCtrl() ) GetTextCtrl()->Remove(from, to);
1e6feb95
VZ
339}
340
341void wxComboBox::SetSelection(long from, long to)
342{
a340b80d 343 if ( GetTextCtrl() ) GetTextCtrl()->SetSelection(from, to);
1e6feb95
VZ
344}
345
0ec1179b
VZ
346void wxComboBox::GetSelection(long *from, long *to) const
347{
348 if ( GetTextCtrl() ) GetTextCtrl()->GetSelection(from, to);
349}
350
1e6feb95
VZ
351void wxComboBox::SetEditable(bool editable)
352{
a340b80d 353 if ( GetTextCtrl() ) GetTextCtrl()->SetEditable(editable);
1e6feb95
VZ
354}
355
356// ----------------------------------------------------------------------------
357// wxComboBox methods forwarded to wxListBox
358// ----------------------------------------------------------------------------
359
a236aa20 360void wxComboBox::DoClear()
1e6feb95
VZ
361{
362 GetLBox()->Clear();
59060c07 363 SetValue(wxEmptyString);
1e6feb95
VZ
364}
365
a236aa20 366void wxComboBox::DoDeleteOneItem(unsigned int n)
1e6feb95 367{
9a83f860 368 wxCHECK_RET( IsValid(n), wxT("invalid index in wxComboBox::Delete") );
48aa18c0 369
aa61d352 370 if (GetSelection() == (int)n)
59060c07 371 SetValue(wxEmptyString);
48aa18c0 372
1e6feb95
VZ
373 GetLBox()->Delete(n);
374}
375
aa61d352 376unsigned int wxComboBox::GetCount() const
1e6feb95
VZ
377{
378 return GetLBox()->GetCount();
379}
380
aa61d352 381wxString wxComboBox::GetString(unsigned int n) const
1e6feb95 382{
9a83f860 383 wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("invalid index in wxComboBox::GetString") );
48aa18c0 384
1e6feb95
VZ
385 return GetLBox()->GetString(n);
386}
387
aa61d352 388void wxComboBox::SetString(unsigned int n, const wxString& s)
1e6feb95 389{
9a83f860 390 wxCHECK_RET( IsValid(n), wxT("invalid index in wxComboBox::SetString") );
48aa18c0 391
1e6feb95
VZ
392 GetLBox()->SetString(n, s);
393}
394
11e62fe6 395int wxComboBox::FindString(const wxString& s, bool bCase) const
1e6feb95 396{
11e62fe6 397 return GetLBox()->FindString(s, bCase);
1e6feb95
VZ
398}
399
c6179a84 400void wxComboBox::SetSelection(int n)
1e6feb95 401{
9a83f860 402 wxCHECK_RET( (n == wxNOT_FOUND || IsValid(n)), wxT("invalid index in wxComboBox::Select") );
1e6feb95
VZ
403
404 GetLBox()->SetSelection(n);
365271b5 405
75a788ee
VZ
406 wxString str;
407 if ( n != wxNOT_FOUND )
408 str = GetLBox()->GetString(n);
409
410 SetText(str);
1e6feb95
VZ
411}
412
413int wxComboBox::GetSelection() const
414{
4c51a665 415#if 1 // FIXME:: What is the correct behaviour?
1e6feb95 416 // if the current value isn't one of the listbox strings, return -1
48aa18c0 417 return GetLBox()->GetSelection();
150e31d2
JS
418#else
419 // Why oh why is this done this way?
420 // It is not because the value displayed in the text can be found
48aa18c0 421 // in the list that it is the item that is selected!
a340b80d 422 return FindString(if ( GetTextCtrl() ) GetTextCtrl()->GetValue());
48aa18c0 423#endif
1e6feb95
VZ
424}
425
0ec1179b
VZ
426wxString wxComboBox::GetStringSelection() const
427{
428 return GetLBox()->GetStringSelection();
429}
430
b152d8c5
VZ
431wxClientDataType wxComboBox::GetClientDataType() const
432{
433 return GetLBox()->GetClientDataType();
434}
435
131b1fba
VZ
436void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType)
437{
438 GetLBox()->SetClientDataType(clientDataItemsType);
439}
440
a236aa20
VZ
441int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
442 unsigned int pos,
443 void **clientData, wxClientDataType type)
1e6feb95 444{
a236aa20 445 return GetLBox()->DoInsertItems(items, pos, clientData, type);
243dbf1a
VZ
446}
447
aa61d352 448void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
1e6feb95 449{
131b1fba 450 GetLBox()->DoSetItemClientData(n, clientData);
1e6feb95
VZ
451}
452
aa61d352 453void *wxComboBox::DoGetItemClientData(unsigned int n) const
1e6feb95 454{
131b1fba 455 return GetLBox()->DoGetItemClientData(n);
1e6feb95
VZ
456}
457
150e31d2
JS
458bool wxComboBox::IsEditable() const
459{
a340b80d 460 return GetTextCtrl() != NULL && (!HasFlag(wxCB_READONLY) || GetTextCtrl()->IsEditable() );
150e31d2
JS
461}
462
463void wxComboBox::Undo()
464{
465 if (IsEditable())
a340b80d 466 if ( GetTextCtrl() ) GetTextCtrl()->Undo();
150e31d2
JS
467}
468
469void wxComboBox::Redo()
470{
471 if (IsEditable())
a340b80d 472 if ( GetTextCtrl() ) GetTextCtrl()->Redo();
150e31d2
JS
473}
474
475void wxComboBox::SelectAll()
476{
a340b80d 477 if ( GetTextCtrl() ) GetTextCtrl()->SelectAll();
150e31d2
JS
478}
479
480bool wxComboBox::CanCopy() const
481{
a340b80d
VZ
482 if (GetTextCtrl() != NULL)
483 return GetTextCtrl()->CanCopy();
150e31d2
JS
484 else
485 return false;
486}
487
488bool wxComboBox::CanCut() const
489{
a340b80d
VZ
490 if (GetTextCtrl() != NULL)
491 return GetTextCtrl()->CanCut();
150e31d2
JS
492 else
493 return false;
494}
495
496bool wxComboBox::CanPaste() const
497{
498 if (IsEditable())
a340b80d 499 return GetTextCtrl()->CanPaste();
150e31d2
JS
500 else
501 return false;
502}
503
504bool wxComboBox::CanUndo() const
505{
506 if (IsEditable())
a340b80d 507 return GetTextCtrl()->CanUndo();
150e31d2
JS
508 else
509 return false;
510}
511
512bool wxComboBox::CanRedo() const
513{
514 if (IsEditable())
a340b80d 515 return GetTextCtrl()->CanRedo();
150e31d2
JS
516 else
517 return false;
518}
519
520
1e6feb95
VZ
521// ----------------------------------------------------------------------------
522// wxStdComboBoxInputHandler
523// ----------------------------------------------------------------------------
524
525wxStdComboBoxInputHandler::wxStdComboBoxInputHandler(wxInputHandler *inphand)
526 : wxStdInputHandler(inphand)
527{
528}
529
23645bfa 530bool wxStdComboBoxInputHandler::HandleKey(wxInputConsumer *consumer,
1e6feb95
VZ
531 const wxKeyEvent& event,
532 bool pressed)
533{
534 if ( pressed )
535 {
536 wxControlAction action;
537 switch ( event.GetKeyCode() )
538 {
539 case WXK_DOWN:
540 action = wxACTION_COMBOBOX_POPUP;
541 break;
542
543 case WXK_ESCAPE:
544 action = wxACTION_COMBOBOX_DISMISS;
545 break;
546 }
547
a290fa5a 548 if ( !action.IsEmpty() )
1e6feb95 549 {
23645bfa 550 consumer->PerformAction(action);
1e6feb95 551
a290fa5a 552 return true;
1e6feb95
VZ
553 }
554 }
555
23645bfa 556 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1e6feb95
VZ
557}
558
9467bdb7
VZ
559/* static */
560wxInputHandler *wxComboBox::GetStdInputHandler(wxInputHandler *handlerDef)
561{
562 static wxStdComboBoxInputHandler s_handler(handlerDef);
563
564 return &s_handler;
565}
a340b80d 566
1e6feb95 567#endif // wxUSE_COMBOBOX