]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/choice.cpp
Copy about.htm
[wxWidgets.git] / src / mac / carbon / choice.cpp
CommitLineData
2ac013b1 1/////////////////////////////////////////////////////////////////////////////
e9576ca5
SC
2// Name: choice.cpp
3// Purpose: wxChoice
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 10/////////////////////////////////////////////////////////////////////////////
e40298d5 11
a8e9860d 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "choice.h"
14#endif
e40298d5 15
a8e9860d 16#include "wx/wxprec.h"
312ebad4
WS
17
18#if wxUSE_CHOICE
19
e9576ca5 20#include "wx/choice.h"
03e11df5 21#include "wx/menu.h"
519cb848 22#include "wx/mac/uma.h"
e40298d5 23
2f1ae414 24#if !USE_SHARED_LIBRARY
e9576ca5 25IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
2f1ae414 26#endif
e40298d5 27
892e461e 28extern MenuHandle NewUniqueMenu() ;
e40298d5 29
5b781a67
SC
30wxChoice::~wxChoice()
31{
d76240bf
MB
32 if ( HasClientObjectData() )
33 {
34 size_t i, max = GetCount();
35
36 for ( i = 0; i < max; ++i )
37 delete GetClientObject(i);
38 }
39
40 // DeleteMenu( m_macPopUpMenuId ) ;
e40298d5 41 // DisposeMenu( m_macPopUpMenuHandle ) ;
5b781a67 42}
e40298d5 43
584ad2a3
MB
44bool wxChoice::Create(wxWindow *parent, wxWindowID id,
45 const wxPoint& pos,
46 const wxSize& size,
47 const wxArrayString& choices,
48 long style,
49 const wxValidator& validator,
50 const wxString& name)
51{
52 wxCArrayString chs(choices);
53
54 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
55 style, validator, name);
56}
57
e9576ca5
SC
58bool wxChoice::Create(wxWindow *parent, wxWindowID id,
59 const wxPoint& pos,
60 const wxSize& size,
e40298d5
JS
61 int n, const wxString choices[],
62 long style,
e9576ca5
SC
63 const wxValidator& validator,
64 const wxString& name)
65{
312ebad4
WS
66 m_macIsUserPane = false ;
67
b45ed7a2
VZ
68 if ( !wxChoiceBase::Create(parent, id, pos, size, style, validator, name) )
69 return false;
70
facd6764 71 Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ;
4c37f124 72
b905d6cc 73 m_peer = new wxMacControl(this) ;
312ebad4 74 verify_noerr ( CreatePopupButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") ,
5ca0d812 75 -12345 , false /* no variable width */ , 0 , 0 , 0 , m_peer->GetControlRefAddr() ) );
312ebad4 76
c69279ef 77
e40298d5 78 m_macPopUpMenuHandle = NewUniqueMenu() ;
21fd5529
SC
79 m_peer->SetData<MenuHandle>( kControlNoPart , kControlPopupButtonMenuHandleTag , (MenuHandle) m_macPopUpMenuHandle ) ;
80 m_peer->SetValueAndRange( n > 0 ? 1 : 0 , 0 , 0 ) ;
facd6764 81 MacPostControlCreate(pos,size) ;
34585919 82
7b6986c3 83#if !wxUSE_STL
34585919
SC
84 if ( style & wxCB_SORT )
85 {
86 m_strings = wxArrayString(1) ; // autosort
87 }
7b6986c3 88#endif
34585919 89
b668a735
SC
90 for ( int i = 0; i < n; i++ )
91 {
92 Append(choices[i]);
93 }
d3b5db4b 94 SetBestSize(size); // Needed because it is a wxControlWithItems
312ebad4 95 return true;
e9576ca5 96}
e40298d5 97
b668a735
SC
98// ----------------------------------------------------------------------------
99// adding/deleting items to/from the list
100// ----------------------------------------------------------------------------
2597135a 101int wxChoice::DoAppend(const wxString& item)
e9576ca5 102{
aacd1442 103#if wxUSE_STL
9c707f80
MB
104 wxArrayString::iterator insertPoint;
105 size_t index;
106
107 if (GetWindowStyle() & wxCB_SORT)
108 {
109 insertPoint = std::lower_bound( m_strings.begin(), m_strings.end(), item );
110 index = insertPoint - m_strings.begin();
111 }
112 else
113 {
114 insertPoint = m_strings.end();
115 index = m_strings.size();
116 }
117
118 m_strings.insert( insertPoint, item );
aacd1442 119#else
34585919 120 size_t index = m_strings.Add( item ) ;
aacd1442 121#endif
34585919
SC
122 m_datas.Insert( NULL , index ) ;
123 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() , index );
e40298d5 124 DoSetItemClientData( index , NULL ) ;
21fd5529 125 m_peer->SetMaximum( GetCount() ) ;
e40298d5 126 return index ;
2597135a 127}
e40298d5 128
243dbf1a
VZ
129int wxChoice::DoInsert(const wxString& item, int pos)
130{
131 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
132 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
133
134 if (pos == GetCount())
135 return DoAppend(item);
136
34585919 137 UMAInsertMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() , pos );
243dbf1a
VZ
138 m_strings.Insert( item, pos ) ;
139 m_datas.Insert( NULL, pos ) ;
140 DoSetItemClientData( pos , NULL ) ;
21fd5529 141 m_peer->SetMaximum( GetCount() ) ;
243dbf1a
VZ
142 return pos ;
143}
144
b668a735 145void wxChoice::Delete(int n)
2597135a 146{
b668a735 147 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
b668a735 148 if ( HasClientObjectData() )
2597135a 149 {
b668a735 150 delete GetClientObject(n);
2597135a 151 }
76a5e5d2 152 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ;
5fe38474 153 m_strings.RemoveAt( n ) ;
3ef585df 154 m_datas.RemoveAt( n ) ;
21fd5529 155 m_peer->SetMaximum( GetCount() ) ;
e9576ca5 156}
e40298d5 157
e9576ca5
SC
158void wxChoice::Clear()
159{
4b651a46 160 FreeData();
b668a735 161 for ( int i = 0 ; i < GetCount() ; i++ )
519cb848 162 {
e40298d5 163 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
4b651a46 164 }
b668a735
SC
165 m_strings.Empty() ;
166 m_datas.Empty() ;
21fd5529 167 m_peer->SetMaximum( 0 ) ;
e9576ca5 168}
e40298d5 169
4b651a46 170void wxChoice::FreeData()
b668a735
SC
171{
172 if ( HasClientObjectData() )
173 {
174 size_t count = GetCount();
175 for ( size_t n = 0; n < count; n++ )
176 {
177 delete GetClientObject(n);
178 }
179 }
180}
e40298d5 181
b668a735
SC
182// ----------------------------------------------------------------------------
183// selection
184// ----------------------------------------------------------------------------
e9576ca5
SC
185int wxChoice::GetSelection() const
186{
21fd5529 187 return m_peer->GetValue() -1 ;
e9576ca5 188}
e40298d5 189
b668a735 190void wxChoice::SetSelection(int n)
519cb848 191{
21fd5529 192 m_peer->SetValue( n + 1 ) ;
519cb848 193}
e40298d5 194
b668a735
SC
195// ----------------------------------------------------------------------------
196// string list functions
197// ----------------------------------------------------------------------------
e40298d5 198
b668a735 199int wxChoice::GetCount() const
e9576ca5 200{
b668a735 201 return m_strings.GetCount() ;
e9576ca5 202}
e40298d5 203
e9576ca5
SC
204int wxChoice::FindString(const wxString& s) const
205{
34585919 206 return m_strings.Index( s , true , false) ;
b668a735 207}
e40298d5 208
b668a735
SC
209void wxChoice::SetString(int n, const wxString& s)
210{
34585919
SC
211 m_strings[n] = s ;
212 // apple menu pos is 1-based
213 UMASetMenuItemText( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1 , s , wxFont::GetDefaultEncoding() ) ;
e9576ca5
SC
214}
215
216wxString wxChoice::GetString(int n) const
217{
17a6a662
VZ
218 wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), _T(""),
219 _T("wxChoice::GetString(): invalid index") );
220
e40298d5 221 return m_strings[n] ;
e9576ca5 222}
17a6a662 223
b668a735
SC
224// ----------------------------------------------------------------------------
225// client data
226// ----------------------------------------------------------------------------
b668a735
SC
227void wxChoice::DoSetItemClientData( int n, void* clientData )
228{
d81cc883 229 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
427ff662 230 wxT("invalid index in wxChoice::SetClientData") );
c69279ef 231
d81cc883 232 m_datas[n] = (char*) clientData ;
b668a735 233}
e40298d5 234
d81cc883 235void *wxChoice::DoGetItemClientData(int n) const
b668a735 236{
3ba18664
RD
237 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
238 wxT("invalid index in wxChoice::GetClientData") );
d81cc883 239 return (void *)m_datas[n];
b668a735 240}
e40298d5 241
b668a735
SC
242void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
243{
244 DoSetItemClientData(n, clientData);
245}
e40298d5 246
b668a735 247wxClientData* wxChoice::DoGetItemClientObject( int n ) const
e9576ca5 248{
b668a735 249 return (wxClientData *)DoGetItemClientData(n);
e9576ca5 250}
e40298d5 251
312ebad4 252wxInt32 wxChoice::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
b668a735
SC
253{
254 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
f2d990ad
SC
255 int n = GetSelection();
256 // actually n should be made sure by the os to be a valid selection, but ...
257 if ( n > -1 )
258 {
259 event.SetInt( n );
260 event.SetString(GetStringSelection());
261 event.SetEventObject(this);
f2d990ad
SC
262 if ( HasClientObjectData() )
263 event.SetClientObject( GetClientObject(n) );
264 else if ( HasClientUntypedData() )
265 event.SetClientData( GetClientData(n) );
f2d990ad
SC
266 ProcessCommand(event);
267 }
4c37f124 268 return noErr ;
9453cf2b 269}
e40298d5 270
f2d990ad 271wxSize wxChoice::DoGetBestSize() const
b668a735 272{
637988a4 273 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
f2d990ad
SC
274 int lbHeight = 20;
275 int wLine;
276#if TARGET_CARBON
277 long metric ;
c69279ef
RD
278 GetThemeMetric(kThemeMetricPopupButtonHeight , &metric );
279 lbHeight = metric ;
f2d990ad 280#endif
e40298d5 281 {
facd6764 282 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
fcb35beb 283 if ( m_font.Ok() )
e40298d5 284 {
facd6764
SC
285 ::TextFont( m_font.MacGetFontNum() ) ;
286 ::TextSize( m_font.MacGetFontSize() ) ;
287 ::TextFace( m_font.MacGetFontStyle() ) ;
e40298d5
JS
288 }
289 else
290 {
291 ::TextFont( kFontIDMonaco ) ;
292 ::TextSize( 9 );
293 ::TextFace( 0 ) ;
294 }
295 // Find the widest line
296 for(int i = 0; i < GetCount(); i++) {
297 wxString str(GetString(i));
2c1a3312
SC
298 #if wxUSE_UNICODE
299 Point bounds={0,0} ;
300 SInt16 baseline ;
a9412f8f 301 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
2c1a3312
SC
302 kThemeCurrentPortFont,
303 kThemeStateActive,
304 false,
305 &bounds,
306 &baseline );
307 wLine = bounds.h ;
308 #else
939fba6c 309 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
2c1a3312 310 #endif
e40298d5
JS
311 lbWidth = wxMax(lbWidth, wLine);
312 }
313 // Add room for the popup arrow
314 lbWidth += 2 * lbHeight ;
315 // And just a bit more
e40298d5
JS
316 int cx = ::TextWidth( "X" , 0 , 1 ) ;
317 lbWidth += cx ;
c69279ef 318
e40298d5 319 }
f2d990ad 320 return wxSize(lbWidth, lbHeight);
d76240bf 321}
312ebad4
WS
322
323#endif // wxUSE_CHOICE