]> git.saurik.com Git - wxWidgets.git/blame - src/univ/combobox.cpp
compilation fix for wxUniv/GTK (closes #9849)
[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
a57d600f 108IMPLEMENT_DYNAMIC_CLASS2(wxComboBox, wxControl, wxComboCtrl)
1e6feb95
VZ
109
110// ============================================================================
111// implementation
112// ============================================================================
113
114// ----------------------------------------------------------------------------
a340b80d 115// wxComboListBox
1e6feb95
VZ
116// ----------------------------------------------------------------------------
117
e051d008 118wxComboListBox::wxComboListBox() : wxListBox(), wxComboPopup()
1e6feb95 119{
1e6feb95
VZ
120}
121
a340b80d 122bool wxComboListBox::Create(wxWindow* parent)
1e6feb95 123{
a340b80d
VZ
124 if ( !wxListBox::Create(parent, wxID_ANY,
125 wxDefaultPosition, wxDefaultSize,
126 0, NULL,
750040fa
WS
127 wxBORDER_SIMPLE |
128 ( m_combo->GetWindowStyle() & wxCB_SORT ? wxLB_SORT : 0 ) ) )
a290fa5a 129 return false;
1e6feb95 130
a340b80d
VZ
131 // we don't react to the mouse events outside the window at all
132 StopAutoScrolling();
1e6feb95 133
a290fa5a 134 return true;
1e6feb95
VZ
135}
136
a340b80d 137wxComboListBox::~wxComboListBox()
1e6feb95 138{
1e6feb95
VZ
139}
140
a340b80d 141wxString wxComboListBox::GetStringValue() const
1e6feb95 142{
a340b80d 143 return wxListBox::GetStringSelection();
1e6feb95
VZ
144}
145
a340b80d 146void wxComboListBox::SetStringValue(const wxString& value)
1e6feb95 147{
615a96e3
JS
148 if ( !value.empty() )
149 {
f6fc052a
JS
150 if (FindString(value) != wxNOT_FOUND)
151 wxListBox::SetStringSelection(value);
152 }
a340b80d
VZ
153 else
154 wxListBox::SetSelection(-1);
1e6feb95
VZ
155}
156
a340b80d 157void wxComboListBox::OnPopup()
1e6feb95
VZ
158{
159}
160
161bool wxComboListBox::PerformAction(const wxControlAction& action,
162 long numArg,
163 const wxString& strArg)
164
165{
166 if ( action == wxACTION_LISTBOX_FIND )
167 {
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
a57d600f 170 // and this is done by wxComboCtrl itself
a290fa5a 171 return true;
1e6feb95
VZ
172 }
173
174 return wxListBox::PerformAction(action, numArg, strArg);
175}
176
55f095d4
VZ
177void wxComboListBox::OnLeftUp(wxMouseEvent& event)
178{
179 // we should dismiss the combo now
a340b80d
VZ
180 // first update the combo and close the listbox
181 Dismiss();
182 m_combo->SetValue(wxListBox::GetStringSelection());
55f095d4 183
a340b80d
VZ
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);
55f095d4 189
a340b80d 190 event.Skip();
e2ca829e
JS
191}
192
a340b80d
VZ
193wxSize wxComboListBox::GetAdjustedSize(int minWidth,
194 int WXUNUSED(prefHeight),
195 int maxHeight)
1e6feb95 196{
a340b80d
VZ
197 wxSize bestSize = wxListBox::GetBestSize();
198 return wxSize(wxMax(bestSize.x,minWidth),
199 wxMin(bestSize.y,maxHeight));
1e6feb95
VZ
200}
201
202// ----------------------------------------------------------------------------
203// wxComboBox
204// ----------------------------------------------------------------------------
205
206void wxComboBox::Init()
207{
208 m_lbox = (wxListBox *)NULL;
209}
210
584ad2a3
MB
211wxComboBox::wxComboBox(wxWindow *parent,
212 wxWindowID id,
213 const wxString& value,
214 const wxPoint& pos,
215 const wxSize& size,
216 const wxArrayString& choices,
217 long style,
218 const wxValidator& validator,
219 const wxString& name)
220{
221 Init();
222
223 Create(parent, id, value, pos, size, choices, style, validator, name);
224}
225
226bool wxComboBox::Create(wxWindow *parent,
227 wxWindowID id,
228 const wxString& value,
229 const wxPoint& pos,
230 const wxSize& size,
231 const wxArrayString& choices,
232 long style,
233 const wxValidator& validator,
234 const wxString& name)
235{
236 wxCArrayString chs(choices);
237
238 return Create(parent, id, value, pos, size, chs.GetCount(),
239 chs.GetStrings(), style, validator, name);
240}
241
1e6feb95
VZ
242bool wxComboBox::Create(wxWindow *parent,
243 wxWindowID id,
244 const wxString& value,
245 const wxPoint& pos,
246 const wxSize& size,
247 int n,
ba1e9d6c 248 const wxString choices[],
1e6feb95
VZ
249 long style,
250 const wxValidator& validator,
251 const wxString& name)
252{
a57d600f 253 if ( !wxComboCtrl::Create(parent, id, value, pos, size, style,
1e6feb95
VZ
254 validator, name) )
255 {
a290fa5a 256 return false;
1e6feb95
VZ
257 }
258
e051d008 259 wxComboListBox *combolbox = new wxComboListBox();
a340b80d
VZ
260 SetPopupControl(combolbox);
261
1e6feb95
VZ
262 m_lbox = combolbox;
263 m_lbox->Set(n, choices);
264
a290fa5a 265 return true;
1e6feb95
VZ
266}
267
268wxComboBox::~wxComboBox()
269{
270}
271
272// ----------------------------------------------------------------------------
273// wxComboBox methods forwarded to wxTextCtrl
274// ----------------------------------------------------------------------------
275
276wxString wxComboBox::GetValue() const
277{
a57d600f 278 return wxComboCtrl::GetValue();
1e6feb95
VZ
279}
280
281void wxComboBox::SetValue(const wxString& value)
282{
a57d600f 283 wxComboCtrl::SetValue(value);
1e6feb95
VZ
284}
285
0ec1179b
VZ
286void wxComboBox::WriteText(const wxString& value)
287{
288 if ( GetTextCtrl() ) GetTextCtrl()->WriteText(value);
289}
290
1e6feb95
VZ
291void wxComboBox::Copy()
292{
a340b80d 293 if ( GetTextCtrl() ) GetTextCtrl()->Copy();
1e6feb95
VZ
294}
295
296void wxComboBox::Cut()
297{
a340b80d 298 if ( GetTextCtrl() ) GetTextCtrl()->Cut();
1e6feb95
VZ
299}
300
301void wxComboBox::Paste()
302{
a340b80d 303 if ( GetTextCtrl() ) GetTextCtrl()->Paste();
1e6feb95
VZ
304}
305
306void wxComboBox::SetInsertionPoint(long pos)
307{
a340b80d 308 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPoint(pos);
1e6feb95
VZ
309}
310
311void wxComboBox::SetInsertionPointEnd()
312{
a340b80d 313 if ( GetTextCtrl() ) GetTextCtrl()->SetInsertionPointEnd();
1e6feb95
VZ
314}
315
316long wxComboBox::GetInsertionPoint() const
317{
a340b80d
VZ
318 if ( GetTextCtrl() )
319 return GetTextCtrl()->GetInsertionPoint();
320 return -1;
1e6feb95
VZ
321}
322
7d8268a1 323wxTextPos wxComboBox::GetLastPosition() const
1e6feb95 324{
a340b80d
VZ
325 if ( GetTextCtrl() )
326 return GetTextCtrl()->GetLastPosition();
327 return -1;
1e6feb95
VZ
328}
329
330void wxComboBox::Replace(long from, long to, const wxString& value)
331{
a340b80d 332 if ( GetTextCtrl() ) GetTextCtrl()->Replace(from, to, value);
1e6feb95
VZ
333}
334
335void wxComboBox::Remove(long from, long to)
336{
a340b80d 337 if ( GetTextCtrl() ) GetTextCtrl()->Remove(from, to);
1e6feb95
VZ
338}
339
340void wxComboBox::SetSelection(long from, long to)
341{
a340b80d 342 if ( GetTextCtrl() ) GetTextCtrl()->SetSelection(from, to);
1e6feb95
VZ
343}
344
0ec1179b
VZ
345void wxComboBox::GetSelection(long *from, long *to) const
346{
347 if ( GetTextCtrl() ) GetTextCtrl()->GetSelection(from, to);
348}
349
1e6feb95
VZ
350void wxComboBox::SetEditable(bool editable)
351{
a340b80d 352 if ( GetTextCtrl() ) GetTextCtrl()->SetEditable(editable);
1e6feb95
VZ
353}
354
355// ----------------------------------------------------------------------------
356// wxComboBox methods forwarded to wxListBox
357// ----------------------------------------------------------------------------
358
a236aa20 359void wxComboBox::DoClear()
1e6feb95
VZ
360{
361 GetLBox()->Clear();
a340b80d 362 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString);
1e6feb95
VZ
363}
364
a236aa20 365void wxComboBox::DoDeleteOneItem(unsigned int n)
1e6feb95 366{
8228b893 367 wxCHECK_RET( IsValid(n), _T("invalid index in wxComboBox::Delete") );
48aa18c0 368
aa61d352 369 if (GetSelection() == (int)n)
a340b80d 370 if ( GetTextCtrl() ) GetTextCtrl()->SetValue(wxEmptyString);
48aa18c0 371
1e6feb95
VZ
372 GetLBox()->Delete(n);
373}
374
aa61d352 375unsigned int wxComboBox::GetCount() const
1e6feb95
VZ
376{
377 return GetLBox()->GetCount();
378}
379
aa61d352 380wxString wxComboBox::GetString(unsigned int n) const
1e6feb95 381{
8228b893 382 wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxComboBox::GetString") );
48aa18c0 383
1e6feb95
VZ
384 return GetLBox()->GetString(n);
385}
386
aa61d352 387void wxComboBox::SetString(unsigned int n, const wxString& s)
1e6feb95 388{
8228b893 389 wxCHECK_RET( IsValid(n), _T("invalid index in wxComboBox::SetString") );
48aa18c0 390
1e6feb95
VZ
391 GetLBox()->SetString(n, s);
392}
393
11e62fe6 394int wxComboBox::FindString(const wxString& s, bool bCase) const
1e6feb95 395{
11e62fe6 396 return GetLBox()->FindString(s, bCase);
1e6feb95
VZ
397}
398
c6179a84 399void wxComboBox::SetSelection(int n)
1e6feb95 400{
f6fc052a 401 wxCHECK_RET( (n == wxNOT_FOUND || IsValid(n)), _T("invalid index in wxComboBox::Select") );
1e6feb95
VZ
402
403 GetLBox()->SetSelection(n);
365271b5 404
75a788ee
VZ
405 wxString str;
406 if ( n != wxNOT_FOUND )
407 str = GetLBox()->GetString(n);
408
409 SetText(str);
1e6feb95
VZ
410}
411
412int wxComboBox::GetSelection() const
413{
48aa18c0 414#if 1 // FIXME:: What is the correct behavior?
1e6feb95 415 // if the current value isn't one of the listbox strings, return -1
48aa18c0 416 return GetLBox()->GetSelection();
150e31d2
JS
417#else
418 // Why oh why is this done this way?
419 // It is not because the value displayed in the text can be found
48aa18c0 420 // in the list that it is the item that is selected!
a340b80d 421 return FindString(if ( GetTextCtrl() ) GetTextCtrl()->GetValue());
48aa18c0 422#endif
1e6feb95
VZ
423}
424
0ec1179b
VZ
425wxString wxComboBox::GetStringSelection() const
426{
427 return GetLBox()->GetStringSelection();
428}
429
b152d8c5
VZ
430wxClientDataType wxComboBox::GetClientDataType() const
431{
432 return GetLBox()->GetClientDataType();
433}
434
131b1fba
VZ
435void wxComboBox::SetClientDataType(wxClientDataType clientDataItemsType)
436{
437 GetLBox()->SetClientDataType(clientDataItemsType);
438}
439
a236aa20
VZ
440int wxComboBox::DoInsertItems(const wxArrayStringsAdapter & items,
441 unsigned int pos,
442 void **clientData, wxClientDataType type)
1e6feb95 443{
a236aa20 444 return GetLBox()->DoInsertItems(items, pos, clientData, type);
243dbf1a
VZ
445}
446
aa61d352 447void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
1e6feb95 448{
131b1fba 449 GetLBox()->DoSetItemClientData(n, clientData);
1e6feb95
VZ
450}
451
aa61d352 452void *wxComboBox::DoGetItemClientData(unsigned int n) const
1e6feb95 453{
131b1fba 454 return GetLBox()->DoGetItemClientData(n);
1e6feb95
VZ
455}
456
150e31d2
JS
457bool wxComboBox::IsEditable() const
458{
a340b80d 459 return GetTextCtrl() != NULL && (!HasFlag(wxCB_READONLY) || GetTextCtrl()->IsEditable() );
150e31d2
JS
460}
461
462void wxComboBox::Undo()
463{
464 if (IsEditable())
a340b80d 465 if ( GetTextCtrl() ) GetTextCtrl()->Undo();
150e31d2
JS
466}
467
468void wxComboBox::Redo()
469{
470 if (IsEditable())
a340b80d 471 if ( GetTextCtrl() ) GetTextCtrl()->Redo();
150e31d2
JS
472}
473
474void wxComboBox::SelectAll()
475{
a340b80d 476 if ( GetTextCtrl() ) GetTextCtrl()->SelectAll();
150e31d2
JS
477}
478
479bool wxComboBox::CanCopy() const
480{
a340b80d
VZ
481 if (GetTextCtrl() != NULL)
482 return GetTextCtrl()->CanCopy();
150e31d2
JS
483 else
484 return false;
485}
486
487bool wxComboBox::CanCut() const
488{
a340b80d
VZ
489 if (GetTextCtrl() != NULL)
490 return GetTextCtrl()->CanCut();
150e31d2
JS
491 else
492 return false;
493}
494
495bool wxComboBox::CanPaste() const
496{
497 if (IsEditable())
a340b80d 498 return GetTextCtrl()->CanPaste();
150e31d2
JS
499 else
500 return false;
501}
502
503bool wxComboBox::CanUndo() const
504{
505 if (IsEditable())
a340b80d 506 return GetTextCtrl()->CanUndo();
150e31d2
JS
507 else
508 return false;
509}
510
511bool wxComboBox::CanRedo() const
512{
513 if (IsEditable())
a340b80d 514 return GetTextCtrl()->CanRedo();
150e31d2
JS
515 else
516 return false;
517}
518
519
1e6feb95
VZ
520// ----------------------------------------------------------------------------
521// wxStdComboBoxInputHandler
522// ----------------------------------------------------------------------------
523
524wxStdComboBoxInputHandler::wxStdComboBoxInputHandler(wxInputHandler *inphand)
525 : wxStdInputHandler(inphand)
526{
527}
528
23645bfa 529bool wxStdComboBoxInputHandler::HandleKey(wxInputConsumer *consumer,
1e6feb95
VZ
530 const wxKeyEvent& event,
531 bool pressed)
532{
533 if ( pressed )
534 {
535 wxControlAction action;
536 switch ( event.GetKeyCode() )
537 {
538 case WXK_DOWN:
539 action = wxACTION_COMBOBOX_POPUP;
540 break;
541
542 case WXK_ESCAPE:
543 action = wxACTION_COMBOBOX_DISMISS;
544 break;
545 }
546
a290fa5a 547 if ( !action.IsEmpty() )
1e6feb95 548 {
23645bfa 549 consumer->PerformAction(action);
1e6feb95 550
a290fa5a 551 return true;
1e6feb95
VZ
552 }
553 }
554
23645bfa 555 return wxStdInputHandler::HandleKey(consumer, event, pressed);
1e6feb95
VZ
556}
557
9467bdb7
VZ
558/* static */
559wxInputHandler *wxComboBox::GetStdInputHandler(wxInputHandler *handlerDef)
560{
561 static wxStdComboBoxInputHandler s_handler(handlerDef);
562
563 return &s_handler;
564}
a340b80d 565
1e6feb95 566#endif // wxUSE_COMBOBOX