]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/combobox.cpp
adding metafile and clipboard support
[wxWidgets.git] / src / mac / carbon / combobox.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: combobox.cpp
3// Purpose: wxComboBox class
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
3d1a4878 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "combobox.h"
14#endif
15
3d1a4878
SC
16#include "wx/wxprec.h"
17
e9576ca5 18#include "wx/combobox.h"
b5a8b32f 19#include "wx/button.h"
03e11df5 20#include "wx/menu.h"
519cb848 21#include "wx/mac/uma.h"
e9576ca5 22
2f1ae414 23#if !USE_SHARED_LIBRARY
e9576ca5 24IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
2f1ae414 25#endif
e9576ca5 26
12f31626
SC
27// composite combobox implementation by Dan "Bud" Keith bud@otsys.com
28
519cb848 29
0e03d1fa 30static int nextPopUpMenuId = 1000 ;
150e31d2 31MenuHandle NewUniqueMenu()
892e461e
SC
32{
33 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
34 nextPopUpMenuId++ ;
35 return handle ;
36}
519cb848 37
12f31626
SC
38
39// ----------------------------------------------------------------------------
40// constants
41// ----------------------------------------------------------------------------
42
43// the margin between the text control and the choice
6097743a 44#if TARGET_API_MAC_OSX
f26ca7f8
KO
45// margin should be bigger on OS X due to blue highlight
46// around text control.
b5267123 47static const wxCoord MARGIN = 4;
788e118f
SC
48// this is the border a focus rect on OSX is needing
49static const int TEXTFOCUSBORDER = 3 ;
6097743a 50#else
f26ca7f8 51static const wxCoord MARGIN = 2;
788e118f 52static const int TEXTFOCUSBORDER = 0 ;
6097743a 53#endif
12f31626
SC
54static const int POPUPHEIGHT = 23;
55
56
57// ----------------------------------------------------------------------------
58// wxComboBoxText: text control forwards events to combobox
59// ----------------------------------------------------------------------------
60
61class wxComboBoxText : public wxTextCtrl
62{
63public:
64 wxComboBoxText( wxComboBox * cb )
327788ac 65 : wxTextCtrl( cb , 1 )
12f31626
SC
66 {
67 m_cb = cb;
68 }
69
70protected:
8095ef23 71 void OnChar( wxKeyEvent& event )
12f31626 72 {
7d8268a1
WS
73 // Allows processing the tab key to go to the next control
74 if (event.GetKeyCode() == WXK_TAB)
75 {
76 wxNavigationKeyEvent NavEvent;
77 NavEvent.SetEventObject(this);
78 NavEvent.SetDirection(true);
79 NavEvent.SetWindowChange(false);
80
81 // Get the parent of the combo and have it process the navigation?
82 if (m_cb->GetParent()->GetEventHandler()->ProcessEvent(NavEvent))
645b5bd6 83 return;
7d8268a1 84 }
eb22f2a6 85 if ( event.GetKeyCode() == WXK_RETURN )
8095ef23 86 {
645b5bd6
JS
87 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_cb->GetId());
88 event.SetString( GetValue() );
89 event.SetInt( m_cb->GetSelection() );
90 event.SetEventObject( m_cb );
8095ef23 91
645b5bd6
JS
92 // This will invoke the dialog default action, such
93 // as the clicking the default button.
8095ef23 94
645b5bd6
JS
95 if (!m_cb->GetEventHandler()->ProcessEvent( event ))
96 {
8095ef23
SC
97 wxWindow *parent = GetParent();
98 while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) {
e40298d5 99 parent = parent->GetParent() ;
8095ef23
SC
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);
8095ef23
SC
110 }
111 }
112
113 return;
114 }
115 }
150e31d2 116
12f31626
SC
117 event.Skip();
118 }
119
645b5bd6
JS
120 // Use the KeyUp as a naive approximation for TEXT_UPDATED, even though it is somewhat delayed
121 // but this is less complicated than dealing with idle-ness, and is much better than nothing
122 void OnKeyUp( wxKeyEvent& event )
123 {
9da00e91 124 if ( event.GetKeyCode() != WXK_RETURN && event.GetKeyCode() != WXK_TAB )
645b5bd6
JS
125 {
126 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, m_cb->GetId());
127 event.SetString( GetValue() );
128 event.SetEventObject( m_cb );
150e31d2 129 m_cb->GetEventHandler()->ProcessEvent(event);
645b5bd6
JS
130 }
131 }
12f31626
SC
132private:
133 wxComboBox *m_cb;
134
135 DECLARE_EVENT_TABLE()
136};
137
138BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
8095ef23 139 EVT_CHAR( wxComboBoxText::OnChar)
645b5bd6 140 EVT_KEY_UP( wxComboBoxText::OnKeyUp)
12f31626
SC
141END_EVENT_TABLE()
142
143class wxComboBoxChoice : public wxChoice
144{
145public:
146 wxComboBoxChoice(wxComboBox *cb, int style)
327788ac 147 : wxChoice( cb , 1 )
12f31626
SC
148 {
149 m_cb = cb;
150 }
ff6871ef
SC
151 int GetPopupWidth() const
152 {
153 switch ( GetWindowVariant() )
154 {
155 case wxWINDOW_VARIANT_NORMAL :
156 case wxWINDOW_VARIANT_LARGE :
157 return 24 ;
158 default :
159 return 21 ;
160 }
161 }
12f31626
SC
162
163protected:
164 void OnChoice( wxCommandEvent& e )
165 {
166 wxString s = e.GetString();
167
168 m_cb->DelegateChoice( s );
8095ef23
SC
169 wxCommandEvent event2(wxEVT_COMMAND_COMBOBOX_SELECTED, m_cb->GetId() );
170 event2.SetInt(m_cb->GetSelection());
171 event2.SetEventObject(m_cb);
172 event2.SetString(m_cb->GetStringSelection());
173 m_cb->ProcessCommand(event2);
645b5bd6
JS
174
175 // For consistency with MSW and GTK, also send a text updated event
176 // After all, the text is updated when a selection is made
177 wxCommandEvent TextEvent( wxEVT_COMMAND_TEXT_UPDATED, m_cb->GetId() );
178 TextEvent.SetString( m_cb->GetStringSelection() );
179 TextEvent.SetEventObject( m_cb );
180 m_cb->ProcessCommand( TextEvent );
12f31626 181 }
6097743a
SC
182 virtual wxSize DoGetBestSize() const
183 {
184 wxSize sz = wxChoice::DoGetBestSize() ;
d0770e4a 185 if (! m_cb->HasFlag(wxCB_READONLY) )
ff6871ef 186 sz.x = GetPopupWidth() ;
6097743a 187 return sz ;
150e31d2 188 }
12f31626
SC
189
190private:
191 wxComboBox *m_cb;
192
193 DECLARE_EVENT_TABLE()
194};
195
196BEGIN_EVENT_TABLE(wxComboBoxChoice, wxChoice)
197 EVT_CHOICE(-1, wxComboBoxChoice::OnChoice)
198END_EVENT_TABLE()
199
12f31626
SC
200wxComboBox::~wxComboBox()
201{
e94e2e95
MB
202 // delete client objects
203 FreeData();
204
205 // delete the controls now, don't leave them alive even though they would
12f31626
SC
206 // still be eventually deleted by our parent - but it will be too late, the
207 // user code expects them to be gone now
f5bb2251
GD
208 if (m_text != NULL) {
209 delete m_text;
210 m_text = NULL;
211 }
212 if (m_choice != NULL) {
213 delete m_choice;
214 m_choice = NULL;
215 }
12f31626
SC
216}
217
218
219// ----------------------------------------------------------------------------
220// geometry
221// ----------------------------------------------------------------------------
222
223wxSize wxComboBox::DoGetBestSize() const
224{
88db1d64 225 if (!m_choice && !m_text)
1deb64c0 226 return GetSize();
12f31626 227 wxSize size = m_choice->GetBestSize();
150e31d2 228
d99937cd 229 if ( m_text != NULL )
12f31626
SC
230 {
231 wxSize sizeText = m_text->GetBestSize();
f26ca7f8
KO
232 if (sizeText.y > size.y)
233 size.y = sizeText.y;
ff6871ef 234 size.x = m_choice->GetPopupWidth() + sizeText.x + MARGIN;
788e118f
SC
235 size.x += TEXTFOCUSBORDER ;
236 size.y += 2 * TEXTFOCUSBORDER ;
12f31626 237 }
ff6871ef
SC
238 else
239 {
240 // clipping is too tight
241 size.y += 1 ;
242 }
12f31626
SC
243 return size;
244}
245
150e31d2 246void wxComboBox::DoMoveWindow(int x, int y, int width, int height)
ff6871ef 247{
788e118f 248 wxControl::DoMoveWindow(x, y, width , height );
150e31d2 249
d99937cd 250 if ( m_text == NULL )
12f31626 251 {
facd6764
SC
252 // we might not be fully constructed yet, therefore watch out...
253 if ( m_choice )
254 m_choice->SetSize(0, 0 , width, -1);
12f31626
SC
255 }
256 else
257 {
ff6871ef 258 wxCoord wText = width - m_choice->GetPopupWidth() - MARGIN;
788e118f 259 m_text->SetSize(TEXTFOCUSBORDER, TEXTFOCUSBORDER, wText, -1 );
ff6871ef
SC
260 // put it at an inset of 1 to have outer area shadows drawn as well
261 m_choice->SetSize(TEXTFOCUSBORDER + wText + MARGIN - 1 , TEXTFOCUSBORDER, m_choice->GetPopupWidth() , -1);
150e31d2 262 }
12f31626
SC
263}
264
265
266
267// ----------------------------------------------------------------------------
268// operations forwarded to the subcontrols
269// ----------------------------------------------------------------------------
270
271bool wxComboBox::Enable(bool enable)
272{
273 if ( !wxControl::Enable(enable) )
7d8268a1 274 return false;
12f31626 275
7d8268a1 276 return true;
12f31626
SC
277}
278
279bool wxComboBox::Show(bool show)
280{
281 if ( !wxControl::Show(show) )
7d8268a1 282 return false;
12f31626 283
7d8268a1 284 return true;
12f31626
SC
285}
286
d99937cd
GD
287void wxComboBox::SetFocus()
288{
289 if ( m_text != NULL) {
290 m_text->SetFocus();
291 }
292}
465605e0 293
12f31626 294
d99937cd
GD
295void wxComboBox::DelegateTextChanged( const wxString& value )
296{
8095ef23 297 SetStringSelection( value );
12f31626
SC
298}
299
300
301void wxComboBox::DelegateChoice( const wxString& value )
302{
303 SetStringSelection( value );
304}
305
306
584ad2a3
MB
307bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
308 const wxString& value,
309 const wxPoint& pos,
310 const wxSize& size,
311 const wxArrayString& choices,
312 long style,
313 const wxValidator& validator,
314 const wxString& name)
315{
316 wxCArrayString chs( choices );
317
318 return Create( parent, id, value, pos, size, chs.GetCount(),
319 chs.GetStrings(), style, validator, name );
320}
321
322
e9576ca5
SC
323bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
324 const wxString& value,
325 const wxPoint& pos,
326 const wxSize& size,
465605e0
RR
327 int n, const wxString choices[],
328 long style,
e9576ca5
SC
329 const wxValidator& validator,
330 const wxString& name)
331{
327788ac 332 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
12f31626
SC
333 wxDefaultValidator, name) )
334 {
7d8268a1 335 return false;
12f31626
SC
336 }
337
327788ac 338 m_choice = new wxComboBoxChoice(this, style );
12f31626
SC
339 wxSize csize = size;
340 if ( style & wxCB_READONLY )
341 {
d99937cd 342 m_text = NULL;
12f31626
SC
343 }
344 else
345 {
346 m_text = new wxComboBoxText(this);
150e31d2 347 if ( size.y == -1 )
788e118f
SC
348 {
349 csize.y = m_text->GetSize().y ;
350 csize.y += 2 * TEXTFOCUSBORDER ;
12f31626
SC
351 }
352 }
150e31d2 353
12f31626 354 DoSetSize(pos.x, pos.y, csize.x, csize.y);
150e31d2 355
12f31626
SC
356 for ( int i = 0 ; i < n ; i++ )
357 {
465605e0 358 m_choice->DoAppend( choices[ i ] );
12f31626
SC
359 }
360
88db1d64 361 SetBestSize(size); // Needed because it is a wxControlWithItems
d3b5db4b 362
7d8268a1 363 return true;
e9576ca5
SC
364}
365
366wxString wxComboBox::GetValue() const
367{
12f31626 368 wxString result;
150e31d2 369
d99937cd 370 if ( m_text == NULL )
12f31626
SC
371 {
372 result = m_choice->GetString( m_choice->GetSelection() );
373 }
374 else
375 {
376 result = m_text->GetValue();
377 }
378
379 return result;
e9576ca5
SC
380}
381
46cc7c4e 382int wxComboBox::GetCount() const
150e31d2
JS
383{
384 return m_choice->GetCount() ;
46cc7c4e
SC
385}
386
e9576ca5
SC
387void wxComboBox::SetValue(const wxString& value)
388{
30a936ee
SC
389 if ( HasFlag(wxCB_READONLY) )
390 SetStringSelection( value ) ;
391 else
392 m_text->SetValue( value );
e9576ca5
SC
393}
394
395// Clipboard operations
396void wxComboBox::Copy()
397{
d99937cd 398 if ( m_text != NULL )
12f31626
SC
399 {
400 m_text->Copy();
401 }
e9576ca5
SC
402}
403
404void wxComboBox::Cut()
405{
d99937cd 406 if ( m_text != NULL )
12f31626
SC
407 {
408 m_text->Cut();
409 }
e9576ca5
SC
410}
411
412void wxComboBox::Paste()
413{
d99937cd 414 if ( m_text != NULL )
12f31626
SC
415 {
416 m_text->Paste();
417 }
e9576ca5
SC
418}
419
420void wxComboBox::SetEditable(bool editable)
421{
d99937cd 422 if ( ( m_text == NULL ) && editable )
12f31626
SC
423 {
424 m_text = new wxComboBoxText( this );
425 }
d99937cd 426 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
427 {
428 delete m_text;
d99937cd 429 m_text = NULL;
12f31626
SC
430 }
431
432 int currentX, currentY;
433 GetPosition( &currentX, &currentY );
150e31d2 434
12f31626
SC
435 int currentW, currentH;
436 GetSize( &currentW, &currentH );
437
438 DoMoveWindow( currentX, currentY, currentW, currentH );
e9576ca5
SC
439}
440
441void wxComboBox::SetInsertionPoint(long pos)
442{
443 // TODO
444}
445
446void wxComboBox::SetInsertionPointEnd()
447{
448 // TODO
449}
450
451long wxComboBox::GetInsertionPoint() const
452{
453 // TODO
454 return 0;
455}
456
7d8268a1 457wxTextPos wxComboBox::GetLastPosition() const
e9576ca5
SC
458{
459 // TODO
460 return 0;
461}
462
463void wxComboBox::Replace(long from, long to, const wxString& value)
464{
465 // TODO
466}
467
468void wxComboBox::Remove(long from, long to)
469{
470 // TODO
471}
472
473void wxComboBox::SetSelection(long from, long to)
474{
475 // TODO
476}
477
150e31d2 478int wxComboBox::DoAppend(const wxString& item)
e9576ca5 479{
e71a0aa9
SC
480 return m_choice->DoAppend( item ) ;
481}
482
150e31d2 483int wxComboBox::DoInsert(const wxString& item, int pos)
e71a0aa9
SC
484{
485 return m_choice->DoInsert( item , pos ) ;
486}
487
150e31d2 488void wxComboBox::DoSetItemClientData(int n, void* clientData)
e71a0aa9 489{
f148f2ba 490 return m_choice->DoSetItemClientData( n , clientData ) ;
e71a0aa9
SC
491}
492
493void* wxComboBox::DoGetItemClientData(int n) const
494{
f148f2ba 495 return m_choice->DoGetItemClientData( n ) ;
e71a0aa9 496}
22a70443 497
e71a0aa9
SC
498void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
499{
f148f2ba 500 return m_choice->DoSetItemClientObject( n , clientData ) ;
e71a0aa9
SC
501}
502
150e31d2 503wxClientData* wxComboBox::DoGetItemClientObject(int n) const
e71a0aa9 504{
f148f2ba
MB
505 return m_choice->DoGetItemClientObject( n ) ;
506}
507
508void wxComboBox::FreeData()
509{
510 if ( HasClientObjectData() )
511 {
512 size_t count = GetCount();
513 for ( size_t n = 0; n < count; n++ )
514 {
515 SetClientObject( n, NULL );
516 }
517 }
e9576ca5
SC
518}
519
520void wxComboBox::Delete(int n)
521{
f148f2ba
MB
522 // force client object deletion
523 if( HasClientObjectData() )
524 SetClientObject( n, NULL );
12f31626 525 m_choice->Delete( n );
e9576ca5
SC
526}
527
528void wxComboBox::Clear()
529{
f148f2ba 530 FreeData();
12f31626 531 m_choice->Clear();
e9576ca5
SC
532}
533
534int wxComboBox::GetSelection() const
535{
12f31626 536 return m_choice->GetSelection();
e9576ca5
SC
537}
538
539void wxComboBox::SetSelection(int n)
540{
12f31626 541 m_choice->SetSelection( n );
150e31d2 542
d99937cd 543 if ( m_text != NULL )
12f31626
SC
544 {
545 m_text->SetValue( GetString( n ) );
546 }
e9576ca5
SC
547}
548
549int wxComboBox::FindString(const wxString& s) const
550{
12f31626 551 return m_choice->FindString( s );
e9576ca5
SC
552}
553
554wxString wxComboBox::GetString(int n) const
555{
12f31626 556 return m_choice->GetString( n );
e9576ca5
SC
557}
558
559wxString wxComboBox::GetStringSelection() const
560{
519cb848
SC
561 int sel = GetSelection ();
562 if (sel > -1)
563 return wxString(this->GetString (sel));
564 else
427ff662 565 return wxEmptyString;
e9576ca5
SC
566}
567
568bool wxComboBox::SetStringSelection(const wxString& sel)
569{
519cb848
SC
570 int s = FindString (sel);
571 if (s > -1)
572 {
573 SetSelection (s);
7d8268a1 574 return true;
519cb848
SC
575 }
576 else
7d8268a1 577 return false;
519cb848
SC
578}
579
150e31d2 580void wxComboBox::SetString(int n, const wxString& s)
e71a0aa9
SC
581{
582 m_choice->SetString( n , s ) ;
583}
584
150e31d2
JS
585bool wxComboBox::IsEditable() const
586{
7d8268a1 587 return m_text != NULL && !HasFlag(wxCB_READONLY);
150e31d2
JS
588}
589
590void wxComboBox::Undo()
591{
592 if (m_text != NULL)
593 m_text->Undo();
594}
595
596void wxComboBox::Redo()
597{
598 if (m_text != NULL)
599 m_text->Redo();
600}
601
602void wxComboBox::SelectAll()
603{
604 if (m_text != NULL)
605 m_text->SelectAll();
606}
607
608bool wxComboBox::CanCopy() const
609{
610 if (m_text != NULL)
611 return m_text->CanCopy();
612 else
613 return false;
614}
615
616bool wxComboBox::CanCut() const
617{
618 if (m_text != NULL)
619 return m_text->CanCut();
620 else
621 return false;
622}
623
624bool wxComboBox::CanPaste() const
625{
626 if (m_text != NULL)
627 return m_text->CanPaste();
628 else
629 return false;
630}
631
632bool wxComboBox::CanUndo() const
633{
634 if (m_text != NULL)
635 return m_text->CanUndo();
636 else
637 return false;
638}
639
640bool wxComboBox::CanRedo() const
641{
642 if (m_text != NULL)
643 return m_text->CanRedo();
644 else
645 return false;
646}
e71a0aa9 647
150e31d2 648wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
519cb848 649{
645b5bd6 650 /* For consistency with other platforms, clicking in the text area does not constitute a selection
519cb848 651 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 652 event.SetInt(GetSelection());
519cb848 653 event.SetEventObject(this);
0a67a93b 654 event.SetString(GetStringSelection());
12fce8fb
RN
655 ProcessCommand(event);*/
656 return noErr ;
e9576ca5 657}
519cb848 658