]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/combobox.cpp
Fixed DDE memory leaks.
[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
465605e0 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "combobox.h"
14#endif
15
16#include "wx/combobox.h"
b5a8b32f 17#include "wx/button.h"
03e11df5 18#include "wx/menu.h"
519cb848 19#include "wx/mac/uma.h"
e9576ca5 20
2f1ae414 21#if !USE_SHARED_LIBRARY
e9576ca5 22IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
2f1ae414 23#endif
e9576ca5 24
12f31626
SC
25// composite combobox implementation by Dan "Bud" Keith bud@otsys.com
26
519cb848 27
0e03d1fa 28static int nextPopUpMenuId = 1000 ;
892e461e
SC
29MenuHandle NewUniqueMenu()
30{
31 MenuHandle handle = NewMenu( nextPopUpMenuId , "\pMenu" ) ;
32 nextPopUpMenuId++ ;
33 return handle ;
34}
519cb848 35
12f31626
SC
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 )
327788ac 55 : wxTextCtrl( cb , 1 )
12f31626
SC
56 {
57 m_cb = cb;
58 }
59
60protected:
8095ef23 61 void OnChar( wxKeyEvent& event )
12f31626 62 {
eb22f2a6 63 if ( event.GetKeyCode() == WXK_RETURN )
8095ef23
SC
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 ) {
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);
110 return ;
111 }
112 }
113
114 return;
115 }
116 }
22a70443 117
12f31626
SC
118 event.Skip();
119 }
120
121private:
122 wxComboBox *m_cb;
123
124 DECLARE_EVENT_TABLE()
125};
126
127BEGIN_EVENT_TABLE(wxComboBoxText, wxTextCtrl)
8095ef23 128 EVT_CHAR( wxComboBoxText::OnChar)
12f31626
SC
129END_EVENT_TABLE()
130
131class wxComboBoxChoice : public wxChoice
132{
133public:
134 wxComboBoxChoice(wxComboBox *cb, int style)
327788ac 135 : wxChoice( cb , 1 )
12f31626
SC
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 );
8095ef23
SC
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);
12f31626
SC
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
12f31626
SC
163wxComboBox::~wxComboBox()
164{
e94e2e95
MB
165 // delete client objects
166 FreeData();
167
168 // delete the controls now, don't leave them alive even though they would
12f31626
SC
169 // still be eventually deleted by our parent - but it will be too late, the
170 // user code expects them to be gone now
f5bb2251
GD
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 }
12f31626
SC
179}
180
181
182// ----------------------------------------------------------------------------
183// geometry
184// ----------------------------------------------------------------------------
185
186wxSize wxComboBox::DoGetBestSize() const
187{
188 wxSize size = m_choice->GetBestSize();
189
d99937cd 190 if ( m_text != NULL )
12f31626
SC
191 {
192 wxSize sizeText = m_text->GetBestSize();
193
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;
202
203 wxControl::DoMoveWindow(x, y, width, height);
204
d99937cd 205 if ( m_text == NULL )
12f31626 206 {
327788ac 207 m_choice->SetSize(0, 0 , width, -1);
12f31626
SC
208 }
209 else
210 {
7854746e 211 wxCoord wText = width - POPUPWIDTH - MARGIN;
327788ac
SC
212 m_text->SetSize(0, 0, wText, height);
213 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, -1);
12f31626
SC
214 }
215}
216
217
218
219// ----------------------------------------------------------------------------
220// operations forwarded to the subcontrols
221// ----------------------------------------------------------------------------
222
223bool wxComboBox::Enable(bool enable)
224{
225 if ( !wxControl::Enable(enable) )
226 return FALSE;
227
12f31626
SC
228 return TRUE;
229}
230
231bool wxComboBox::Show(bool show)
232{
233 if ( !wxControl::Show(show) )
234 return FALSE;
235
12f31626
SC
236 return TRUE;
237}
238
d99937cd
GD
239void wxComboBox::SetFocus()
240{
241 if ( m_text != NULL) {
242 m_text->SetFocus();
243 }
244}
465605e0 245
12f31626 246
d99937cd
GD
247void wxComboBox::DelegateTextChanged( const wxString& value )
248{
8095ef23 249 SetStringSelection( value );
12f31626
SC
250}
251
252
253void wxComboBox::DelegateChoice( const wxString& value )
254{
255 SetStringSelection( value );
256}
257
258
e9576ca5
SC
259bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
260 const wxString& value,
261 const wxPoint& pos,
262 const wxSize& size,
465605e0
RR
263 int n, const wxString choices[],
264 long style,
e9576ca5
SC
265 const wxValidator& validator,
266 const wxString& name)
267{
327788ac 268 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
12f31626
SC
269 wxDefaultValidator, name) )
270 {
271 return FALSE;
272 }
273
327788ac 274 m_choice = new wxComboBoxChoice(this, style );
12f31626
SC
275
276 wxSize csize = size;
277 if ( style & wxCB_READONLY )
278 {
d99937cd 279 m_text = NULL;
12f31626
SC
280 }
281 else
282 {
283 m_text = new wxComboBoxText(this);
284 if ( size.y == -1 ) {
285 csize.y = m_text->GetSize().y ;
286 }
287 }
288
289 DoSetSize(pos.x, pos.y, csize.x, csize.y);
327788ac 290
12f31626
SC
291 for ( int i = 0 ; i < n ; i++ )
292 {
465605e0 293 m_choice->DoAppend( choices[ i ] );
12f31626
SC
294 }
295
12f31626 296 return TRUE;
e9576ca5
SC
297}
298
299wxString wxComboBox::GetValue() const
300{
12f31626
SC
301 wxString result;
302
d99937cd 303 if ( m_text == NULL )
12f31626
SC
304 {
305 result = m_choice->GetString( m_choice->GetSelection() );
306 }
307 else
308 {
309 result = m_text->GetValue();
310 }
311
312 return result;
e9576ca5
SC
313}
314
315void wxComboBox::SetValue(const wxString& value)
316{
d60de516
SC
317 int s = FindString (value);
318 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
319 {
e40298d5 320 m_choice->Append(value) ;
d60de516 321 }
e40298d5 322 SetStringSelection( value ) ;
e9576ca5
SC
323}
324
325// Clipboard operations
326void wxComboBox::Copy()
327{
d99937cd 328 if ( m_text != NULL )
12f31626
SC
329 {
330 m_text->Copy();
331 }
e9576ca5
SC
332}
333
334void wxComboBox::Cut()
335{
d99937cd 336 if ( m_text != NULL )
12f31626
SC
337 {
338 m_text->Cut();
339 }
e9576ca5
SC
340}
341
342void wxComboBox::Paste()
343{
d99937cd 344 if ( m_text != NULL )
12f31626
SC
345 {
346 m_text->Paste();
347 }
e9576ca5
SC
348}
349
350void wxComboBox::SetEditable(bool editable)
351{
d99937cd 352 if ( ( m_text == NULL ) && editable )
12f31626
SC
353 {
354 m_text = new wxComboBoxText( this );
355 }
d99937cd 356 else if ( ( m_text != NULL ) && !editable )
12f31626
SC
357 {
358 delete m_text;
d99937cd 359 m_text = NULL;
12f31626
SC
360 }
361
362 int currentX, currentY;
363 GetPosition( &currentX, &currentY );
364
365 int currentW, currentH;
366 GetSize( &currentW, &currentH );
367
368 DoMoveWindow( currentX, currentY, currentW, currentH );
e9576ca5
SC
369}
370
371void wxComboBox::SetInsertionPoint(long pos)
372{
373 // TODO
374}
375
376void wxComboBox::SetInsertionPointEnd()
377{
378 // TODO
379}
380
381long wxComboBox::GetInsertionPoint() const
382{
383 // TODO
384 return 0;
385}
386
387long wxComboBox::GetLastPosition() const
388{
389 // TODO
390 return 0;
391}
392
393void wxComboBox::Replace(long from, long to, const wxString& value)
394{
395 // TODO
396}
397
398void wxComboBox::Remove(long from, long to)
399{
400 // TODO
401}
402
403void wxComboBox::SetSelection(long from, long to)
404{
405 // TODO
406}
407
e71a0aa9 408int wxComboBox::DoAppend(const wxString& item)
e9576ca5 409{
e71a0aa9
SC
410 return m_choice->DoAppend( item ) ;
411}
412
413int wxComboBox::DoInsert(const wxString& item, int pos)
414{
415 return m_choice->DoInsert( item , pos ) ;
416}
417
418void wxComboBox::DoSetItemClientData(int n, void* clientData)
419{
f148f2ba 420 return m_choice->DoSetItemClientData( n , clientData ) ;
e71a0aa9
SC
421}
422
423void* wxComboBox::DoGetItemClientData(int n) const
424{
f148f2ba 425 return m_choice->DoGetItemClientData( n ) ;
e71a0aa9 426}
22a70443 427
e71a0aa9
SC
428void wxComboBox::DoSetItemClientObject(int n, wxClientData* clientData)
429{
f148f2ba 430 return m_choice->DoSetItemClientObject( n , clientData ) ;
e71a0aa9
SC
431}
432
433wxClientData* wxComboBox::DoGetItemClientObject(int n) const
434{
f148f2ba
MB
435 return m_choice->DoGetItemClientObject( n ) ;
436}
437
438void wxComboBox::FreeData()
439{
440 if ( HasClientObjectData() )
441 {
442 size_t count = GetCount();
443 for ( size_t n = 0; n < count; n++ )
444 {
445 SetClientObject( n, NULL );
446 }
447 }
e9576ca5
SC
448}
449
450void wxComboBox::Delete(int n)
451{
f148f2ba
MB
452 // force client object deletion
453 if( HasClientObjectData() )
454 SetClientObject( n, NULL );
12f31626 455 m_choice->Delete( n );
e9576ca5
SC
456}
457
458void wxComboBox::Clear()
459{
f148f2ba 460 FreeData();
12f31626 461 m_choice->Clear();
e9576ca5
SC
462}
463
464int wxComboBox::GetSelection() const
465{
12f31626 466 return m_choice->GetSelection();
e9576ca5
SC
467}
468
469void wxComboBox::SetSelection(int n)
470{
12f31626
SC
471 m_choice->SetSelection( n );
472
d99937cd 473 if ( m_text != NULL )
12f31626
SC
474 {
475 m_text->SetValue( GetString( n ) );
476 }
e9576ca5
SC
477}
478
479int wxComboBox::FindString(const wxString& s) const
480{
12f31626 481 return m_choice->FindString( s );
e9576ca5
SC
482}
483
484wxString wxComboBox::GetString(int n) const
485{
12f31626 486 return m_choice->GetString( n );
e9576ca5
SC
487}
488
489wxString wxComboBox::GetStringSelection() const
490{
519cb848
SC
491 int sel = GetSelection ();
492 if (sel > -1)
493 return wxString(this->GetString (sel));
494 else
427ff662 495 return wxEmptyString;
e9576ca5
SC
496}
497
498bool wxComboBox::SetStringSelection(const wxString& sel)
499{
519cb848
SC
500 int s = FindString (sel);
501 if (s > -1)
502 {
503 SetSelection (s);
504 return TRUE;
505 }
506 else
507 return FALSE;
508}
509
e71a0aa9
SC
510void wxComboBox::SetString(int n, const wxString& s)
511{
512 m_choice->SetString( n , s ) ;
513}
514
515
516void wxComboBox::MacHandleControlClick( WXWidget WXUNUSED(control) , wxInt16 WXUNUSED(controlpart) , bool WXUNUSED(mouseStillDown))
519cb848
SC
517{
518 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
465605e0 519 event.SetInt(GetSelection());
519cb848 520 event.SetEventObject(this);
0a67a93b 521 event.SetString(GetStringSelection());
519cb848 522 ProcessCommand(event);
e9576ca5 523}
519cb848 524