]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/combobox.cpp
[ 1579793 ] numerous bug fixes when mask is used
[wxWidgets.git] / src / mac / classic / combobox.cpp
CommitLineData
2646f485 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/mac/classic/combobox.cpp
2646f485
SC
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
f1e01716
WS
12#include "wx/wxprec.h"
13
2646f485 14#include "wx/combobox.h"
f1e01716
WS
15
16#ifndef WX_PRECOMP
17 #include "wx/button.h"
3b3dc801 18 #include "wx/menu.h"
f1e01716
WS
19#endif
20
2646f485
SC
21#include "wx/mac/uma.h"
22
2646f485 23IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
2646f485
SC
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
6c20e8f8
VZ
97 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
98 if ( tlw && tlw->GetDefaultItem() )
2646f485 99 {
6c20e8f8 100 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
2646f485
SC
101 if ( def && def->IsEnabled() )
102 {
103 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
104 event.SetEventObject(def);
105 def->Command(event);
106 return ;
107 }
108 }
109
110 return;
111 }
112 }
150e31d2 113
2646f485
SC
114 event.Skip();
115 }
116
117private:
118 wxComboBox *m_cb;
119
120 DECLARE_EVENT_TABLE()
121};
122
123BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
124 EVT_CHAR( wxComboBoxText::OnChar)
125END_EVENT_TABLE()
126
127class wxComboBoxChoice : public wxChoice
128{
129public:
130 wxComboBoxChoice(wxComboBox *cb, int style)
131 : wxChoice( cb , 1 )
132 {
133 m_cb = cb;
134 }
135
136protected:
137 void OnChoice( wxCommandEvent& e )
138 {
aa61d352 139 wxString s = e.GetString();
2646f485
SC
140
141 m_cb->DelegateChoice( s );
142 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
143 event2.SetInt(m_cb->GetSelection());
144 event2.SetEventObject(m_cb);
145 event2.SetString(m_cb->GetStringSelection());
146 m_cb->ProcessCommand(event2);
147 }
148
149private:
150 wxComboBox *m_cb;
151
152 DECLARE_EVENT_TABLE()
153};
154
155BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
156 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
157END_EVENT_TABLE()
158
159wxComboBox::~wxComboBox()
160{
161 // delete client objects
162 FreeData();
163
164 // delete the controls now, don't leave them alive even though they would
165 // still be eventually deleted by our parent - but it will be too late, the
166 // user code expects them to be gone now
167 if (m_text != NULL) {
168 delete m_text;
169 m_text = NULL;
170 }
171 if (m_choice != NULL) {
172 delete m_choice;
173 m_choice = NULL;
174 }
175}
176
177
178// ----------------------------------------------------------------------------
179// geometry
180// ----------------------------------------------------------------------------
181
182wxSize wxComboBox::DoGetBestSize() const
183{
184 wxSize size = m_choice->GetBestSize();
150e31d2 185
2646f485
SC
186 if ( m_text != NULL )
187 {
188 wxSize sizeText = m_text->GetBestSize();
150e31d2 189
2646f485
SC
190 size.x = POPUPWIDTH + sizeText.x + MARGIN;
191 }
192
193 return size;
194}
195
196void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
197 height = POPUPHEIGHT;
150e31d2 198
2646f485
SC
199 wxControl::DoMoveWindow(x, y, width, height);
200
201 if ( m_text == NULL )
202 {
203 m_choice->SetSize(0, 0 , width, -1);
204 }
205 else
206 {
207 wxCoord wText = width - POPUPWIDTH - MARGIN;
208 m_text->SetSize(0, 0, wText, height);
209 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
150e31d2 210 }
2646f485
SC
211}
212
213
214
215// ----------------------------------------------------------------------------
216// operations forwarded to the subcontrols
217// ----------------------------------------------------------------------------
218
219bool wxComboBox::Enable(bool enable)
220{
221 if ( !wxControl::Enable(enable) )
7d8268a1 222 return false;
2646f485 223
7d8268a1 224 return true;
2646f485
SC
225}
226
227bool wxComboBox::Show(bool show)
228{
229 if ( !wxControl::Show(show) )
7d8268a1 230 return false;
2646f485 231
7d8268a1 232 return true;
2646f485
SC
233}
234
235void wxComboBox::SetFocus()
236{
237 if ( m_text != NULL) {
238 m_text->SetFocus();
239 }
240}
241
242
243void wxComboBox::DelegateTextChanged( const wxString& value )
244{
245 SetStringSelection( value );
246}
247
248
249void wxComboBox::DelegateChoice( const wxString& value )
250{
251 SetStringSelection( value );
252}
253
254
255bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
256 const wxString& value,
257 const wxPoint& pos,
258 const wxSize& size,
259 const wxArrayString& choices,
260 long style,
261 const wxValidator& validator,
262 const wxString& name)
263{
264 wxCArrayString chs( choices );
265
266 return Create( parent, id, value, pos, size, chs.GetCount(),
267 chs.GetStrings(), style, validator, name );
268}
269
270
271bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
272 const wxString& value,
273 const wxPoint& pos,
274 const wxSize& size,
275 int n, const wxString choices[],
276 long style,
277 const wxValidator& validator,
278 const wxString& name)
279{
280 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
281 wxDefaultValidator, name) )
282 {
7d8268a1 283 return false;
2646f485
SC
284 }
285
286 m_choice = new wxComboBoxChoice(this, style );
287
288 wxSize csize = size;
289 if ( style & wxCB_READONLY )
290 {
291 m_text = NULL;
292 }
293 else
294 {
295 m_text = new wxComboBoxText(this);
296 if ( size.y == -1 ) {
297 csize.y = m_text->GetSize().y ;
298 }
299 }
150e31d2 300
2646f485 301 DoSetSize(pos.x, pos.y, csize.x, csize.y);
150e31d2 302
2646f485
SC
303 for ( int i = 0 ; i < n ; i++ )
304 {
305 m_choice->DoAppend( choices[ i ] );
306 }
307
7d8268a1 308 return true;
2646f485
SC
309}
310
311wxString wxComboBox::GetValue() const
312{
aa61d352 313 wxString result;
150e31d2 314
2646f485
SC
315 if ( m_text == NULL )
316 {
317 result = m_choice->GetString( m_choice->GetSelection() );
318 }
319 else
320 {
321 result = m_text->GetValue();
322 }
323
324 return result;
325}
326
327void wxComboBox::SetValue(const wxString& value)
328{
329 int s = FindString (value);
330 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
331 {
332 m_choice->Append(value) ;
333 }
334 SetStringSelection( value ) ;
335}
336
337// Clipboard operations
338void wxComboBox::Copy()
339{
340 if ( m_text != NULL )
341 {
342 m_text->Copy();
343 }
344}
345
346void wxComboBox::Cut()
347{
348 if ( m_text != NULL )
349 {
350 m_text->Cut();
351 }
352}
353
354void wxComboBox::Paste()
355{
356 if ( m_text != NULL )
357 {
358 m_text->Paste();
359 }
360}
361
362void wxComboBox::SetEditable(bool editable)
363{
364 if ( ( m_text == NULL ) && editable )
365 {
366 m_text = new wxComboBoxText( this );
367 }
368 else if ( ( m_text != NULL ) && !editable )
369 {
370 delete m_text;
371 m_text = NULL;
372 }
373
374 int currentX, currentY;
375 GetPosition( &currentX, &currentY );
150e31d2 376
2646f485
SC
377 int currentW, currentH;
378 GetSize( &currentW, &currentH );
379
380 DoMoveWindow( currentX, currentY, currentW, currentH );
381}
382
383void wxComboBox::SetInsertionPoint(long pos)
384{
385 // TODO
386}
387
388void wxComboBox::SetInsertionPointEnd()
389{
390 // TODO
391}
392
393long wxComboBox::GetInsertionPoint() const
394{
395 // TODO
396 return 0;
397}
398
7d8268a1 399wxTextPos wxComboBox::GetLastPosition() const
2646f485
SC
400{
401 // TODO
402 return 0;
403}
404
405void wxComboBox::Replace(long from, long to, const wxString& value)
406{
407 // TODO
408}
409
410void wxComboBox::Remove(long from, long to)
411{
412 // TODO
413}
414
415void wxComboBox::SetSelection(long from, long to)
416{
417 // TODO
418}
419
150e31d2 420int wxComboBox::DoAppend(const wxString& item)
2646f485
SC
421{
422 return m_choice->DoAppend( item ) ;
423}
424
aa61d352 425int wxComboBox::DoInsert(const wxString& item, unsigned int pos)
2646f485
SC
426{
427 return m_choice->DoInsert( item , pos ) ;
428}
429
aa61d352 430void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
2646f485
SC
431{
432 return m_choice->DoSetItemClientData( n , clientData ) ;
433}
434
aa61d352 435void* wxComboBox::DoGetItemClientData(unsigned int n) const
2646f485
SC
436{
437 return m_choice->DoGetItemClientData( n ) ;
438}
439
aa61d352 440void wxComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
2646f485 441{
aa61d352 442 return m_choice->DoSetItemClientObject(n , clientData);
2646f485
SC
443}
444
aa61d352 445wxClientData* wxComboBox::DoGetItemClientObject(unsigned int n) const
2646f485
SC
446{
447 return m_choice->DoGetItemClientObject( n ) ;
448}
449
450void wxComboBox::FreeData()
451{
452 if ( HasClientObjectData() )
453 {
aa61d352
VZ
454 unsigned int count = GetCount();
455 for ( unsigned int n = 0; n < count; n++ )
2646f485
SC
456 {
457 SetClientObject( n, NULL );
458 }
459 }
460}
461
aa61d352 462void wxComboBox::Delete(unsigned int n)
2646f485
SC
463{
464 // force client object deletion
465 if( HasClientObjectData() )
466 SetClientObject( n, NULL );
467 m_choice->Delete( n );
468}
469
470void wxComboBox::Clear()
471{
472 FreeData();
473 m_choice->Clear();
474}
475
476int wxComboBox::GetSelection() const
477{
478 return m_choice->GetSelection();
479}
480
481void wxComboBox::SetSelection(int n)
482{
483 m_choice->SetSelection( n );
150e31d2 484
2646f485
SC
485 if ( m_text != NULL )
486 {
aa61d352 487 m_text->SetValue(GetString(n));
2646f485
SC
488 }
489}
490
11e62fe6 491int wxComboBox::FindString(const wxString& s, bool bCase ) const
2646f485 492{
11e62fe6 493 return m_choice->FindString( s , bCase );
2646f485
SC
494}
495
aa61d352 496wxString wxComboBox::GetString(unsigned int n) const
2646f485
SC
497{
498 return m_choice->GetString( n );
499}
500
501wxString wxComboBox::GetStringSelection() const
502{
503 int sel = GetSelection ();
aa61d352
VZ
504
505 if (sel != wxNOT_FOUND)
506 return wxString(this->GetString((unsigned int)sel));
2646f485
SC
507 else
508 return wxEmptyString;
509}
510
aa61d352 511void wxComboBox::SetString(unsigned int n, const wxString& s)
2646f485 512{
aa61d352 513 m_choice->SetString( n , s );
2646f485
SC
514}
515
150e31d2
JS
516bool wxComboBox::IsEditable() const
517{
7d8268a1 518 return m_text != NULL && !HasFlag(wxCB_READONLY);
150e31d2
JS
519}
520
521void wxComboBox::Undo()
522{
523 if (m_text != NULL)
524 m_text->Undo();
525}
526
527void wxComboBox::Redo()
528{
529 if (m_text != NULL)
530 m_text->Redo();
531}
532
533void wxComboBox::SelectAll()
534{
535 if (m_text != NULL)
536 m_text->SelectAll();
537}
538
539bool wxComboBox::CanCopy() const
540{
541 if (m_text != NULL)
542 return m_text->CanCopy();
543 else
544 return false;
545}
546
547bool wxComboBox::CanCut() const
548{
549 if (m_text != NULL)
550 return m_text->CanCut();
551 else
552 return false;
553}
554
555bool wxComboBox::CanPaste() const
556{
557 if (m_text != NULL)
558 return m_text->CanPaste();
559 else
560 return false;
561}
562
563bool wxComboBox::CanUndo() const
564{
565 if (m_text != NULL)
566 return m_text->CanUndo();
567 else
568 return false;
569}
570
571bool wxComboBox::CanRedo() const
572{
573 if (m_text != NULL)
574 return m_text->CanRedo();
575 else
576 return false;
577}
2646f485 578
150e31d2 579void wxComboBox::MacHandleControlClick( WXWidget WXUNUSED(control) , wxInt16 WXUNUSED(controlpart) , bool WXUNUSED(mouseStillDown))
2646f485
SC
580{
581 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
582 event.SetInt(GetSelection());
583 event.SetEventObject(this);
584 event.SetString(GetStringSelection());
585 ProcessCommand(event);
586}