]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/combobox.cpp
fixed loop deleting the old filters in SetWildcard() after last commit
[wxWidgets.git] / src / mac / classic / combobox.cpp
CommitLineData
2646f485
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose: wxComboBox class
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "combobox.h"
14#endif
15
16#include "wx/combobox.h"
17#include "wx/button.h"
18#include "wx/menu.h"
19#include "wx/mac/uma.h"
20
21#if !USE_SHARED_LIBRARY
22IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
23#endif
24
25// composite combobox implementation by Dan "Bud" Keith bud@otsys.com
26
27
28static int nextPopUpMenuId = 1000 ;
150e31d2 29MenuHandle NewUniqueMenu()
2646f485
SC
30{
31 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
32 nextPopUpMenuId++ ;
33 return handle ;
34}
35
36
37// ----------------------------------------------------------------------------
38// constants
39// ----------------------------------------------------------------------------
40
41// the margin between the text control and the choice
42static const wxCoord MARGIN = 2;
43static const int POPUPWIDTH = 18;
44static const int POPUPHEIGHT = 23;
45
46
47// ----------------------------------------------------------------------------
48// wxComboBoxText: text control forwards events to combobox
49// ----------------------------------------------------------------------------
50
51class wxComboBoxText : public wxTextCtrl
52{
53public:
54 wxComboBoxText( wxComboBox * cb )
55 : wxTextCtrl( cb , 1 )
56 {
57 m_cb = cb;
58 }
59
60protected:
61 void OnChar( wxKeyEvent& event )
62 {
63 if ( event.GetKeyCode() == WXK_RETURN )
64 {
65 wxString value = GetValue();
66
67 if ( m_cb->GetCount() == 0 )
68 {
69 // make Enter generate "selected" event if there is only one item
70 // in the combobox - without it, it's impossible to select it at
71 // all!
72 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
73 event.SetInt( 0 );
74 event.SetString( value );
75 event.SetEventObject( m_cb );
76 m_cb->GetEventHandler()->ProcessEvent( event );
77 }
78 else
79 {
80 // add the item to the list if it's not there yet
81 if ( m_cb->FindString(value) == wxNOT_FOUND )
82 {
83 m_cb->Append(value);
84 m_cb->SetStringSelection(value);
85
86 // and generate the selected event for it
87 wxCommandEvent event( wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
88 event.SetInt( m_cb->GetCount() - 1 );
89 event.SetString( value );
90 event.SetEventObject( m_cb );
91 m_cb->GetEventHandler()->ProcessEvent( event );
92 }
93
94 // This will invoke the dialog default action, such
95 // as the clicking the default button.
96
97 wxWindow *parent = GetParent();
98 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
99 parent = parent->GetParent() ;
100 }
101 if ( parent && parent->GetDefaultItem() )
102 {
103 wxButton *def = wxDynamicCast(parent->GetDefaultItem(),
104 wxButton);
105 if ( def && def->IsEnabled() )
106 {
107 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
108 event.SetEventObject(def);
109 def->Command(event);
110 return ;
111 }
112 }
113
114 return;
115 }
116 }
150e31d2 117
2646f485
SC
118 event.Skip();
119 }
120
121private:
122 wxComboBox *m_cb;
123
124 DECLARE_EVENT_TABLE()
125};
126
127BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
128 EVT_CHAR( wxComboBoxText::OnChar)
129END_EVENT_TABLE()
130
131class wxComboBoxChoice : public wxChoice
132{
133public:
134 wxComboBoxChoice(wxComboBox *cb, int style)
135 : wxChoice( cb , 1 )
136 {
137 m_cb = cb;
138 }
139
140protected:
141 void OnChoice( wxCommandEvent& e )
142 {
143 wxString s = e.GetString();
144
145 m_cb->DelegateChoice( s );
146 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
147 event2.SetInt(m_cb->GetSelection());
148 event2.SetEventObject(m_cb);
149 event2.SetString(m_cb->GetStringSelection());
150 m_cb->ProcessCommand(event2);
151 }
152
153private:
154 wxComboBox *m_cb;
155
156 DECLARE_EVENT_TABLE()
157};
158
159BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
160 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
161END_EVENT_TABLE()
162
163wxComboBox::~wxComboBox()
164{
165 // delete client objects
166 FreeData();
167
168 // delete the controls now, don't leave them alive even though they would
169 // still be eventually deleted by our parent - but it will be too late, the
170 // user code expects them to be gone now
171 if (m_text != NULL) {
172 delete m_text;
173 m_text = NULL;
174 }
175 if (m_choice != NULL) {
176 delete m_choice;
177 m_choice = NULL;
178 }
179}
180
181
182// ----------------------------------------------------------------------------
183// geometry
184// ----------------------------------------------------------------------------
185
186wxSize wxComboBox::DoGetBestSize() const
187{
188 wxSize size = m_choice->GetBestSize();
150e31d2 189
2646f485
SC
190 if ( m_text != NULL )
191 {
192 wxSize sizeText = m_text->GetBestSize();
150e31d2 193
2646f485
SC
194 size.x = POPUPWIDTH + sizeText.x + MARGIN;
195 }
196
197 return size;
198}
199
200void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
201 height = POPUPHEIGHT;
150e31d2 202
2646f485
SC
203 wxControl::DoMoveWindow(x, y, width, height);
204
205 if ( m_text == NULL )
206 {
207 m_choice->SetSize(0, 0 , width, -1);
208 }
209 else
210 {
211 wxCoord wText = width - POPUPWIDTH - MARGIN;
212 m_text->SetSize(0, 0, wText, height);
213 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
150e31d2 214 }
2646f485
SC
215}
216
217
218
219// ----------------------------------------------------------------------------
220// operations forwarded to the subcontrols
221// ----------------------------------------------------------------------------
222
223bool wxComboBox::Enable(bool enable)
224{
225 if ( !wxControl::Enable(enable) )
7d8268a1 226 return false;
2646f485 227
7d8268a1 228 return true;
2646f485
SC
229}
230
231bool wxComboBox::Show(bool show)
232{
233 if ( !wxControl::Show(show) )
7d8268a1 234 return false;
2646f485 235
7d8268a1 236 return true;
2646f485
SC
237}
238
239void wxComboBox::SetFocus()
240{
241 if ( m_text != NULL) {
242 m_text->SetFocus();
243 }
244}
245
246
247void wxComboBox::DelegateTextChanged( const wxString& value )
248{
249 SetStringSelection( value );
250}
251
252
253void wxComboBox::DelegateChoice( const wxString& value )
254{
255 SetStringSelection( value );
256}
257
258
259bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
260 const wxString& value,
261 const wxPoint& pos,
262 const wxSize& size,
263 const wxArrayString& choices,
264 long style,
265 const wxValidator& validator,
266 const wxString& name)
267{
268 wxCArrayString chs( choices );
269
270 return Create( parent, id, value, pos, size, chs.GetCount(),
271 chs.GetStrings(), style, validator, name );
272}
273
274
275bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
276 const wxString& value,
277 const wxPoint& pos,
278 const wxSize& size,
279 int n, const wxString choices[],
280 long style,
281 const wxValidator& validator,
282 const wxString& name)
283{
284 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
285 wxDefaultValidator, name) )
286 {
7d8268a1 287 return false;
2646f485
SC
288 }
289
290 m_choice = new wxComboBoxChoice(this, style );
291
292 wxSize csize = size;
293 if ( style & wxCB_READONLY )
294 {
295 m_text = NULL;
296 }
297 else
298 {
299 m_text = new wxComboBoxText(this);
300 if ( size.y == -1 ) {
301 csize.y = m_text->GetSize().y ;
302 }
303 }
150e31d2 304
2646f485 305 DoSetSize(pos.x, pos.y, csize.x, csize.y);
150e31d2 306
2646f485
SC
307 for ( int i = 0 ; i < n ; i++ )
308 {
309 m_choice->DoAppend( choices[ i ] );
310 }
311
7d8268a1 312 return true;
2646f485
SC
313}
314
315wxString wxComboBox::GetValue() const
316{
317 wxString result;
150e31d2 318
2646f485
SC
319 if ( m_text == NULL )
320 {
321 result = m_choice->GetString( m_choice->GetSelection() );
322 }
323 else
324 {
325 result = m_text->GetValue();
326 }
327
328 return result;
329}
330
331void wxComboBox::SetValue(const wxString& value)
332{
333 int s = FindString (value);
334 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
335 {
336 m_choice->Append(value) ;
337 }
338 SetStringSelection( value ) ;
339}
340
341// Clipboard operations
342void wxComboBox::Copy()
343{
344 if ( m_text != NULL )
345 {
346 m_text->Copy();
347 }
348}
349
350void wxComboBox::Cut()
351{
352 if ( m_text != NULL )
353 {
354 m_text->Cut();
355 }
356}
357
358void wxComboBox::Paste()
359{
360 if ( m_text != NULL )
361 {
362 m_text->Paste();
363 }
364}
365
366void wxComboBox::SetEditable(bool editable)
367{
368 if ( ( m_text == NULL ) && editable )
369 {
370 m_text = new wxComboBoxText( this );
371 }
372 else if ( ( m_text != NULL ) && !editable )
373 {
374 delete m_text;
375 m_text = NULL;
376 }
377
378 int currentX, currentY;
379 GetPosition( &currentX, &currentY );
150e31d2 380
2646f485
SC
381 int currentW, currentH;
382 GetSize( &currentW, &currentH );
383
384 DoMoveWindow( currentX, currentY, currentW, currentH );
385}
386
387void wxComboBox::SetInsertionPoint(long pos)
388{
389 // TODO
390}
391
392void wxComboBox::SetInsertionPointEnd()
393{
394 // TODO
395}
396
397long wxComboBox::GetInsertionPoint() const
398{
399 // TODO
400 return 0;
401}
402
7d8268a1 403wxTextPos wxComboBox::GetLastPosition() const
2646f485
SC
404{
405 // TODO
406 return 0;
407}
408
409void wxComboBox::Replace(long from, long to, const wxString& value)
410{
411 // TODO
412}
413
414void wxComboBox::Remove(long from, long to)
415{
416 // TODO
417}
418
419void wxComboBox::SetSelection(long from, long to)
420{
421 // TODO
422}
423
150e31d2 424int wxComboBox::DoAppend(const wxString& item)
2646f485
SC
425{
426 return m_choice->DoAppend( item ) ;
427}
428
150e31d2 429int wxComboBox::DoInsert(const wxString& item, int pos)
2646f485
SC
430{
431 return m_choice->DoInsert( item , pos ) ;
432}
433
150e31d2 434void wxComboBox::DoSetItemClientData(int n, void* clientData)
2646f485
SC
435{
436 return m_choice->DoSetItemClientData( n , clientData ) ;
437}
438
439void* wxComboBox::DoGetItemClientData(int n) const
440{
441 return m_choice->DoGetItemClientData( n ) ;
442}
443
444void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
445{
446 return m_choice->DoSetItemClientObject( n , clientData ) ;
447}
448
150e31d2 449wxClientData* wxComboBox::DoGetItemClientObject(int n) const
2646f485
SC
450{
451 return m_choice->DoGetItemClientObject( n ) ;
452}
453
454void wxComboBox::FreeData()
455{
456 if ( HasClientObjectData() )
457 {
458 size_t count = GetCount();
459 for ( size_t n = 0; n < count; n++ )
460 {
461 SetClientObject( n, NULL );
462 }
463 }
464}
465
466void wxComboBox::Delete(int n)
467{
468 // force client object deletion
469 if( HasClientObjectData() )
470 SetClientObject( n, NULL );
471 m_choice->Delete( n );
472}
473
474void wxComboBox::Clear()
475{
476 FreeData();
477 m_choice->Clear();
478}
479
480int wxComboBox::GetSelection() const
481{
482 return m_choice->GetSelection();
483}
484
485void wxComboBox::SetSelection(int n)
486{
487 m_choice->SetSelection( n );
150e31d2 488
2646f485
SC
489 if ( m_text != NULL )
490 {
491 m_text->SetValue( GetString( n ) );
492 }
493}
494
495int wxComboBox::FindString(const wxString& s) const
496{
497 return m_choice->FindString( s );
498}
499
500wxString wxComboBox::GetString(int n) const
501{
502 return m_choice->GetString( n );
503}
504
505wxString wxComboBox::GetStringSelection() const
506{
507 int sel = GetSelection ();
508 if (sel > -1)
509 return wxString(this->GetString (sel));
510 else
511 return wxEmptyString;
512}
513
150e31d2 514void wxComboBox::SetString(int n, const wxString& s)
2646f485
SC
515{
516 m_choice->SetString( n , s ) ;
517}
518
150e31d2
JS
519bool wxComboBox::IsEditable() const
520{
7d8268a1 521 return m_text != NULL && !HasFlag(wxCB_READONLY);
150e31d2
JS
522}
523
524void wxComboBox::Undo()
525{
526 if (m_text != NULL)
527 m_text->Undo();
528}
529
530void wxComboBox::Redo()
531{
532 if (m_text != NULL)
533 m_text->Redo();
534}
535
536void wxComboBox::SelectAll()
537{
538 if (m_text != NULL)
539 m_text->SelectAll();
540}
541
542bool wxComboBox::CanCopy() const
543{
544 if (m_text != NULL)
545 return m_text->CanCopy();
546 else
547 return false;
548}
549
550bool wxComboBox::CanCut() const
551{
552 if (m_text != NULL)
553 return m_text->CanCut();
554 else
555 return false;
556}
557
558bool wxComboBox::CanPaste() const
559{
560 if (m_text != NULL)
561 return m_text->CanPaste();
562 else
563 return false;
564}
565
566bool wxComboBox::CanUndo() const
567{
568 if (m_text != NULL)
569 return m_text->CanUndo();
570 else
571 return false;
572}
573
574bool wxComboBox::CanRedo() const
575{
576 if (m_text != NULL)
577 return m_text->CanRedo();
578 else
579 return false;
580}
2646f485 581
150e31d2 582void wxComboBox::MacHandleControlClick( WXWidget WXUNUSED(control) , wxInt16 WXUNUSED(controlpart) , bool WXUNUSED(mouseStillDown))
2646f485
SC
583{
584 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
585 event.SetInt(GetSelection());
586 event.SetEventObject(this);
587 event.SetString(GetStringSelection());
588 ProcessCommand(event);
589}
590