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