]> git.saurik.com Git - wxWidgets.git/blame - src/osx/combobox_osx.cpp
Rebake from clean wx tree.
[wxWidgets.git] / src / osx / combobox_osx.cpp
CommitLineData
4ddfa282
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/combobox_osx.cpp
3// Purpose: wxComboBox class using HIView ComboBox
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id: combobox_osx.cpp 58318 2009-01-23 08:36:16Z RR $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_COMBOBOX
15
16#include "wx/combobox.h"
17
18#ifndef WX_PRECOMP
19#endif
20
21// work in progress
22
23IMPLEMENT_DYNAMIC_CLASS(wxComboBox, wxControl)
24
25wxComboBox::~wxComboBox()
26{
27}
28
29bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
30 const wxString& value,
31 const wxPoint& pos,
32 const wxSize& size,
33 const wxArrayString& choices,
34 long style,
35 const wxValidator& validator,
36 const wxString& name)
37{
38 wxCArrayString chs( choices );
39
40 return Create( parent, id, value, pos, size, chs.GetCount(),
41 chs.GetStrings(), style, validator, name );
42}
43
44bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
45 const wxString& value,
46 const wxPoint& pos,
47 const wxSize& size,
48 int n, const wxString choices[],
49 long style,
50 const wxValidator& validator,
51 const wxString& name)
52{
53 m_text = NULL;
54 m_choice = NULL;
55
56 m_macIsUserPane = false;
57
58 if ( !wxControl::Create( parent, id, pos, size, style, validator, name ) )
59 return false;
60
61 m_peer = wxWidgetImpl::CreateComboBox( this, parent, id, m_popUpMenu, pos, size, style, GetExtraStyle() );
62
63 MacPostControlCreate( pos, size );
64
65#if !wxUSE_STL
66 if ( style & wxCB_SORT )
67 // autosort
68 m_strings = wxArrayString( 1 );
69#endif
70
71 Append(n, choices);
72
73 // Set the first item as being selected
74 if (n > 0)
75 SetSelection( 0 );
76
77 // Needed because it is a wxControlWithItems
78 SetInitialSize( size );
79
80 return true;
81}
82
83// ----------------------------------------------------------------------------
84// geometry
85// ----------------------------------------------------------------------------
86
87wxSize wxComboBox::DoGetBestSize() const
88{
89#if USE_HICOMBOBOX
90 return wxControl::DoGetBestSize();
91#else
92 wxSize size = m_choice->GetBestSize();
93
94 if ( m_text != NULL )
95 {
96 wxSize sizeText = m_text->GetBestSize();
97
98 size.x = POPUPWIDTH + sizeText.x + MARGIN;
99 }
100
101 return size;
102#endif
103}
104
105void wxComboBox::DoMoveWindow(int x, int y, int width, int height) {
106#if USE_HICOMBOBOX
107 wxControl::DoMoveWindow(x, y, width, height);
108#else
109 height = POPUPHEIGHT;
110
111 wxControl::DoMoveWindow(x, y, width, height);
112
113 if ( m_text == NULL )
114 {
115 // we might not be fully constructed yet, therefore watch out...
116 if ( m_choice )
117 m_choice->SetSize(0, 0 , width, wxDefaultCoord);
118 }
119 else
120 {
121 wxCoord wText = width - POPUPWIDTH - MARGIN;
122 m_text->SetSize(0, 0, wText, height);
123 m_choice->SetSize(0 + wText + MARGIN, 0, POPUPWIDTH, wxDefaultCoord);
124 }
125#endif
126}
127
128
129
130// ----------------------------------------------------------------------------
131// operations forwarded to the subcontrols
132// ----------------------------------------------------------------------------
133
134bool wxComboBox::Enable(bool enable)
135{
136 if ( !wxControl::Enable(enable) )
137 return false;
138
139 return true;
140}
141
142bool wxComboBox::Show(bool show)
143{
144 if ( !wxControl::Show(show) )
145 return false;
146
147 return true;
148}
149
150void wxComboBox::SetFocus()
151{
152#if USE_HICOMBOBOX
153 wxControl::SetFocus();
154#else
155 if ( m_text != NULL) {
156 m_text->SetFocus();
157 }
158#endif
159}
160
161
162void wxComboBox::DelegateTextChanged( const wxString& value )
163{
164 SetStringSelection( value );
165}
166
167
168void wxComboBox::DelegateChoice( const wxString& value )
169{
170 SetStringSelection( value );
171}
172
173bool wxComboBox::Create(wxWindow *parent, wxWindowID id,
174 const wxString& value,
175 const wxPoint& pos,
176 const wxSize& size,
177 int n, const wxString choices[],
178 long style,
179 const wxValidator& validator,
180 const wxString& name)
181{
182 m_text = NULL;
183 m_choice = NULL;
184#if USE_HICOMBOBOX
185 m_macIsUserPane = false;
186#endif
187 if ( !wxControl::Create(parent, id, wxDefaultPosition, wxDefaultSize, style ,
188 wxDefaultValidator, name) )
189 {
190 return false;
191 }
192#if USE_HICOMBOBOX
193 Rect bounds = wxMacGetBoundsForControl( this , pos , size );
194 HIRect hiRect;
195
196 hiRect.origin.x = 20; //bounds.left;
197 hiRect.origin.y = 25; //bounds.top;
198 hiRect.size.width = 120;// bounds.right - bounds.left;
199 hiRect.size.height = 24;
200
201 //For some reason, this code causes the combo box not to be displayed at all.
202 //hiRect.origin.x = bounds.left;
203 //hiRect.origin.y = bounds.top;
204 //hiRect.size.width = bounds.right - bounds.left;
205 //hiRect.size.height = bounds.bottom - bounds.top;
206 //printf("left = %d, right = %d, top = %d, bottom = %d\n", bounds.left, bounds.right, bounds.top, bounds.bottom);
207 //printf("x = %d, y = %d, width = %d, height = %d\n", hibounds.origin.x, hibounds.origin.y, hibounds.size.width, hibounds.size.height);
208 m_peer = new wxMacControl(this);
209 verify_noerr( HIComboBoxCreate( &hiRect, CFSTR(""), NULL, NULL, kHIComboBoxStandardAttributes, m_peer->GetControlRefAddr() ) );
210
211
212 m_peer->SetMinimum( 0 );
213 m_peer->SetMaximum( 100);
214 if ( n > 0 )
215 m_peer->SetValue( 1 );
216
217 MacPostControlCreate(pos,size);
218
219 Append( choices[ i ] );
220
221 HIViewSetVisible( m_peer->GetControlRef(), true );
222 SetSelection(0);
223 EventHandlerRef comboEventHandler;
224 InstallControlEventHandler( m_peer->GetControlRef(), GetwxMacComboBoxEventHandlerUPP(),
225 GetEventTypeCount(eventList), eventList, this,
226 (EventHandlerRef *)&comboEventHandler);
227#else
228 m_choice = new wxComboBoxChoice(this, style );
229 m_choice->SetMinSize( wxSize( POPUPWIDTH , POPUPHEIGHT ) );
230
231 wxSize csize = size;
232 if ( style & wxCB_READONLY )
233 {
234 m_text = NULL;
235 }
236 else
237 {
238 m_text = new wxComboBoxText(this);
239 if ( size.y == wxDefaultCoord ) {
240 csize.y = m_text->GetSize().y;
241 }
242 }
243
244 DoSetSize(pos.x, pos.y, csize.x, csize.y);
245
246 m_choice->Append( n, choices );
247 SetInitialSize(csize); // Needed because it is a wxControlWithItems
248#endif
249
250 return true;
251}
252
253wxString wxComboBox::GetValue() const
254{
255#if USE_HICOMBOBOX
256 CFStringRef myString;
257 HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)GetSelection(), &myString );
258 return wxMacCFStringHolder( myString, GetFont().GetEncoding() ).AsString();
259#else
260 wxString result;
261
262 if ( m_text == NULL )
263 {
264 result = m_choice->GetString( m_choice->GetSelection() );
265 }
266 else
267 {
268 result = m_text->GetValue();
269 }
270
271 return result;
272#endif
273}
274
275void wxComboBox::SetValue(const wxString& value)
276{
277#if USE_HICOMBOBOX
278
279#else
280 int s = FindString (value);
281 if (s == wxNOT_FOUND && !HasFlag(wxCB_READONLY) )
282 {
283 m_choice->Append(value);
284 }
285 SetStringSelection( value );
286#endif
287}
288
289// Clipboard operations
290void wxComboBox::Copy()
291{
292 if ( m_text != NULL )
293 {
294 m_text->Copy();
295 }
296}
297
298void wxComboBox::Cut()
299{
300 if ( m_text != NULL )
301 {
302 m_text->Cut();
303 }
304}
305
306void wxComboBox::Paste()
307{
308 if ( m_text != NULL )
309 {
310 m_text->Paste();
311 }
312}
313
314void wxComboBox::SetEditable(bool editable)
315{
316 if ( ( m_text == NULL ) && editable )
317 {
318 m_text = new wxComboBoxText( this );
319 }
320 else if ( ( m_text != NULL ) && !editable )
321 {
322 delete m_text;
323 m_text = NULL;
324 }
325
326 int currentX, currentY;
327 GetPosition( &currentX, &currentY );
328
329 int currentW, currentH;
330 GetSize( &currentW, &currentH );
331
332 DoMoveWindow( currentX, currentY, currentW, currentH );
333}
334
335void wxComboBox::SetInsertionPoint(long pos)
336{
337 // TODO
338}
339
340void wxComboBox::SetInsertionPointEnd()
341{
342 // TODO
343}
344
345long wxComboBox::GetInsertionPoint() const
346{
347 // TODO
348 return 0;
349}
350
351wxTextPos wxComboBox::GetLastPosition() const
352{
353 // TODO
354 return 0;
355}
356
357void wxComboBox::Replace(long from, long to, const wxString& value)
358{
359 // TODO
360}
361
362void wxComboBox::Remove(long from, long to)
363{
364 // TODO
365}
366
367void wxComboBox::SetSelection(long from, long to)
368{
369 // TODO
370}
371
372int wxComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
373 unsigned int pos,
374 void **clientData, wxClientDataType type)
375{
376#if USE_HICOMBOBOX
377 const unsigned int count = items.GetCount();
378 for ( unsigned int i = 0; i < count; ++i, ++pos )
379 {
380 HIComboBoxInsertTextItemAtIndex(m_peer->GetControlRef(),
381 (CFIndex)pos,
382 wxMacCFStringHolder(items[i],
383 GetFont().GetEncoding()));
384 AssignNewItemClientData(pos, clientData, i, type);
385 }
386
387 //SetControl32BitMaximum( m_peer->GetControlRef(), GetCount() );
388
389 return pos - 1;
390#else
391 return m_choice->DoInsertItems( items, pos, clientData, type );
392#endif
393}
394
395void wxComboBox::DoSetItemClientData(unsigned int n, void* clientData)
396{
397#if USE_HICOMBOBOX
398 return; //TODO
399#else
400 return m_choice->DoSetItemClientData( n , clientData );
401#endif
402}
403
404void* wxComboBox::DoGetItemClientData(unsigned int n) const
405{
406#if USE_HICOMBOBOX
407 return NULL; //TODO
408#else
409 return m_choice->DoGetItemClientData( n );
410#endif
411}
412
413unsigned int wxComboBox::GetCount() const {
414#if USE_HICOMBOBOX
415 return (unsigned int) HIComboBoxGetItemCount( m_peer->GetControlRef() );
416#else
417 return m_choice->GetCount();
418#endif
419}
420
421void wxComboBox::DoDeleteOneItem(unsigned int n)
422{
423#if USE_HICOMBOBOX
424 HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex)n );
425#else
426 m_choice->Delete( n );
427#endif
428}
429
430void wxComboBox::DoClear()
431{
432#if USE_HICOMBOBOX
433 for ( CFIndex i = GetCount() - 1; i >= 0; ++ i )
434 verify_noerr( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), i ) );
435 m_peer->SetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag,CFSTR(""));
436#else
437 m_choice->Clear();
438#endif
439}
440
441int wxComboBox::GetSelection() const
442{
443#if USE_HICOMBOBOX
444 return FindString( GetStringSelection() );
445#else
446 return m_choice->GetSelection();
447#endif
448}
449
450void wxComboBox::SetSelection(int n)
451{
452#if USE_HICOMBOBOX
453 SetControl32BitValue( m_peer->GetControlRef() , n + 1 );
454#else
455 m_choice->SetSelection( n );
456
457 if ( m_text != NULL )
458 {
459 m_text->SetValue(GetString(n));
460 }
461#endif
462}
463
464int wxComboBox::FindString(const wxString& s, bool bCase) const
465{
466#if USE_HICOMBOBOX
467 for( unsigned int i = 0 ; i < GetCount() ; i++ )
468 {
469 if (GetString(i).IsSameAs(s, bCase) )
470 return i ;
471 }
472 return wxNOT_FOUND;
473#else
474 return m_choice->FindString( s, bCase );
475#endif
476}
477
478wxString wxComboBox::GetString(unsigned int n) const
479{
480#if USE_HICOMBOBOX
481 CFStringRef itemText;
482 HIComboBoxCopyTextItemAtIndex( m_peer->GetControlRef(), (CFIndex)n, &itemText );
483 return wxMacCFStringHolder(itemText).AsString();
484#else
485 return m_choice->GetString( n );
486#endif
487}
488
489wxString wxComboBox::GetStringSelection() const
490{
491#if USE_HICOMBOBOX
492 return wxMacCFStringHolder(m_peer->GetData<CFStringRef>(kHIComboBoxEditTextPart,kControlEditTextCFStringTag)).AsString();
493#else
494 int sel = GetSelection ();
495 if (sel != wxNOT_FOUND)
496 return wxString(this->GetString((unsigned int)sel));
497 else
498 return wxEmptyString;
499#endif
500}
501
502void wxComboBox::SetString(unsigned int n, const wxString& s)
503{
504#if USE_HICOMBOBOX
505 verify_noerr ( HIComboBoxInsertTextItemAtIndex( m_peer->GetControlRef(), (CFIndex) n,
506 wxMacCFStringHolder(s, GetFont().GetEncoding()) ) );
507 verify_noerr ( HIComboBoxRemoveItemAtIndex( m_peer->GetControlRef(), (CFIndex) n + 1 ) );
508#else
509 m_choice->SetString( n , s );
510#endif
511}
512
513bool wxComboBox::IsEditable() const
514{
515#if USE_HICOMBOBOX
516 // TODO
517 return !HasFlag(wxCB_READONLY);
518#else
519 return m_text != NULL && !HasFlag(wxCB_READONLY);
520#endif
521}
522
523void wxComboBox::Undo()
524{
525#if USE_HICOMBOBOX
526 // TODO
527#else
528 if (m_text != NULL)
529 m_text->Undo();
530#endif
531}
532
533void wxComboBox::Redo()
534{
535#if USE_HICOMBOBOX
536 // TODO
537#else
538 if (m_text != NULL)
539 m_text->Redo();
540#endif
541}
542
543void wxComboBox::SelectAll()
544{
545#if USE_HICOMBOBOX
546 // TODO
547#else
548 if (m_text != NULL)
549 m_text->SelectAll();
550#endif
551}
552
553bool wxComboBox::CanCopy() const
554{
555#if USE_HICOMBOBOX
556 // TODO
557 return false;
558#else
559 if (m_text != NULL)
560 return m_text->CanCopy();
561 else
562 return false;
563#endif
564}
565
566bool wxComboBox::CanCut() const
567{
568#if USE_HICOMBOBOX
569 // TODO
570 return false;
571#else
572 if (m_text != NULL)
573 return m_text->CanCut();
574 else
575 return false;
576#endif
577}
578
579bool wxComboBox::CanPaste() const
580{
581#if USE_HICOMBOBOX
582 // TODO
583 return false;
584#else
585 if (m_text != NULL)
586 return m_text->CanPaste();
587 else
588 return false;
589#endif
590}
591
592bool wxComboBox::CanUndo() const
593{
594#if USE_HICOMBOBOX
595 // TODO
596 return false;
597#else
598 if (m_text != NULL)
599 return m_text->CanUndo();
600 else
601 return false;
602#endif
603}
604
605bool wxComboBox::CanRedo() const
606{
607#if USE_HICOMBOBOX
608 // TODO
609 return false;
610#else
611 if (m_text != NULL)
612 return m_text->CanRedo();
613 else
614 return false;
615#endif
616}
617
618bool wxComboBox::OSXHandleClicked( double timestampsec )
619{
620 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, m_windowId );
621 event.SetInt(GetSelection());
622 event.SetEventObject(this);
623 event.SetString(GetStringSelection());
624 ProcessCommand(event);
625 return true;
626}
627
628#endif