]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/combobox.cpp
Add SetBackgroundImage
[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
eba2f031
RD
362 SetStringSelection(value);
363
7d8268a1 364 return true;
e9576ca5
SC
365}
366
367wxString wxComboBox::GetValue() const
368{
12f31626 369 wxString result;
150e31d2 370
d99937cd 371 if ( m_text == NULL )
12f31626
SC
372 {
373 result = m_choice->GetString( m_choice->GetSelection() );
374 }
375 else
376 {
377 result = m_text->GetValue();
378 }
379
380 return result;
e9576ca5
SC
381}
382
46cc7c4e 383int wxComboBox::GetCount() const
150e31d2
JS
384{
385 return m_choice->GetCount() ;
46cc7c4e
SC
386}
387
e9576ca5
SC
388void wxComboBox::SetValue(const wxString& value)
389{
30a936ee
SC
390 if ( HasFlag(wxCB_READONLY) )
391 SetStringSelection( value ) ;
392 else
393 m_text->SetValue( value );
e9576ca5
SC
394}
395
396// Clipboard operations
397void wxComboBox::Copy()
398{
d99937cd 399 if ( m_text != NULL )
12f31626
SC
400 {
401 m_text->Copy();
402 }
e9576ca5
SC
403}
404
405void wxComboBox::Cut()
406{
d99937cd 407 if ( m_text != NULL )
12f31626
SC
408 {
409 m_text->Cut();
410 }
e9576ca5
SC
411}
412
413void wxComboBox::Paste()
414{
d99937cd 415 if ( m_text != NULL )
12f31626
SC
416 {
417 m_text->Paste();
418 }
e9576ca5
SC
419}
420
421void wxComboBox::SetEditable(bool editable)
422{
d99937cd 423 if ( ( m_text == NULL ) && editable )
12f31626
SC
424 {
425 m_text = new wxComboBoxText( this );
426 }
d99937cd 427 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
428 {
429 delete m_text;
d99937cd 430 m_text = NULL;
12f31626
SC
431 }
432
433 int currentX, currentY;
434 GetPosition( &currentX, &currentY );
150e31d2 435
12f31626
SC
436 int currentW, currentH;
437 GetSize( &currentW, &currentH );
438
439 DoMoveWindow( currentX, currentY, currentW, currentH );
e9576ca5
SC
440}
441
442void wxComboBox::SetInsertionPoint(long pos)
443{
444 // TODO
445}
446
447void wxComboBox::SetInsertionPointEnd()
448{
449 // TODO
450}
451
452long wxComboBox::GetInsertionPoint() const
453{
454 // TODO
455 return 0;
456}
457
7d8268a1 458wxTextPos wxComboBox::GetLastPosition() const
e9576ca5
SC
459{
460 // TODO
461 return 0;
462}
463
464void wxComboBox::Replace(long from, long to, const wxString& value)
465{
466 // TODO
467}
468
469void wxComboBox::Remove(long from, long to)
470{
471 // TODO
472}
473
474void wxComboBox::SetSelection(long from, long to)
475{
476 // TODO
477}
478
150e31d2 479int wxComboBox::DoAppend(const wxString& item)
e9576ca5 480{
e71a0aa9
SC
481 return m_choice->DoAppend( item ) ;
482}
483
150e31d2 484int wxComboBox::DoInsert(const wxString& item, int pos)
e71a0aa9
SC
485{
486 return m_choice->DoInsert( item , pos ) ;
487}
488
150e31d2 489void wxComboBox::DoSetItemClientData(int n, void* clientData)
e71a0aa9 490{
f148f2ba 491 return m_choice->DoSetItemClientData( n , clientData ) ;
e71a0aa9
SC
492}
493
494void* wxComboBox::DoGetItemClientData(int n) const
495{
f148f2ba 496 return m_choice->DoGetItemClientData( n ) ;
e71a0aa9 497}
22a70443 498
e71a0aa9
SC
499void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
500{
f148f2ba 501 return m_choice->DoSetItemClientObject( n , clientData ) ;
e71a0aa9
SC
502}
503
150e31d2 504wxClientData* wxComboBox::DoGetItemClientObject(int n) const
e71a0aa9 505{
f148f2ba
MB
506 return m_choice->DoGetItemClientObject( n ) ;
507}
508
509void wxComboBox::FreeData()
510{
511 if ( HasClientObjectData() )
512 {
513 size_t count = GetCount();
514 for ( size_t n = 0; n < count; n++ )
515 {
516 SetClientObject( n, NULL );
517 }
518 }
e9576ca5
SC
519}
520
521void wxComboBox::Delete(int n)
522{
f148f2ba
MB
523 // force client object deletion
524 if( HasClientObjectData() )
525 SetClientObject( n, NULL );
12f31626 526 m_choice->Delete( n );
e9576ca5
SC
527}
528
529void wxComboBox::Clear()
530{
f148f2ba 531 FreeData();
12f31626 532 m_choice->Clear();
e9576ca5
SC
533}
534
535int wxComboBox::GetSelection() const
536{
12f31626 537 return m_choice->GetSelection();
e9576ca5
SC
538}
539
540void wxComboBox::SetSelection(int n)
541{
12f31626 542 m_choice->SetSelection( n );
150e31d2 543
d99937cd 544 if ( m_text != NULL )
12f31626
SC
545 {
546 m_text->SetValue( GetString( n ) );
547 }
e9576ca5
SC
548}
549
550int wxComboBox::FindString(const wxString& s) const
551{
12f31626 552 return m_choice->FindString( s );
e9576ca5
SC
553}
554
555wxString wxComboBox::GetString(int n) const
556{
12f31626 557 return m_choice->GetString( n );
e9576ca5
SC
558}
559
560wxString wxComboBox::GetStringSelection() const
561{
519cb848
SC
562 int sel = GetSelection ();
563 if (sel > -1)
564 return wxString(this->GetString (sel));
565 else
427ff662 566 return wxEmptyString;
e9576ca5
SC
567}
568
150e31d2 569void wxComboBox::SetString(int n, const wxString& s)
e71a0aa9
SC
570{
571 m_choice->SetString( n , s ) ;
572}
573
150e31d2
JS
574bool wxComboBox::IsEditable() const
575{
7d8268a1 576 return m_text != NULL && !HasFlag(wxCB_READONLY);
150e31d2
JS
577}
578
579void wxComboBox::Undo()
580{
581 if (m_text != NULL)
582 m_text->Undo();
583}
584
585void wxComboBox::Redo()
586{
587 if (m_text != NULL)
588 m_text->Redo();
589}
590
591void wxComboBox::SelectAll()
592{
593 if (m_text != NULL)
594 m_text->SelectAll();
595}
596
597bool wxComboBox::CanCopy() const
598{
599 if (m_text != NULL)
600 return m_text->CanCopy();
601 else
602 return false;
603}
604
605bool wxComboBox::CanCut() const
606{
607 if (m_text != NULL)
608 return m_text->CanCut();
609 else
610 return false;
611}
612
613bool wxComboBox::CanPaste() const
614{
615 if (m_text != NULL)
616 return m_text->CanPaste();
617 else
618 return false;
619}
620
621bool wxComboBox::CanUndo() const
622{
623 if (m_text != NULL)
624 return m_text->CanUndo();
625 else
626 return false;
627}
628
629bool wxComboBox::CanRedo() const
630{
631 if (m_text != NULL)
632 return m_text->CanRedo();
633 else
634 return false;
635}
e71a0aa9 636
150e31d2 637wxInt32 wxComboBox::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
519cb848 638{
645b5bd6 639 /* For consistency with other platforms, clicking in the text area does not constitute a selection
519cb848 640 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 641 event.SetInt(GetSelection());
519cb848 642 event.SetEventObject(this);
0a67a93b 643 event.SetString(GetStringSelection());
12fce8fb
RN
644 ProcessCommand(event);*/
645 return noErr ;
e9576ca5 646}
519cb848 647