]> git.saurik.com Git - wxWidgets.git/blame - src/mac/choice.cpp
added condition for DARWIN (thanks to Steve Hartwell)
[wxWidgets.git] / src / mac / 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
e40298d5 9// Licence: wxWindows licence
e9576ca5 10/////////////////////////////////////////////////////////////////////////////
e40298d5 11
e9576ca5
SC
12#ifdef __GNUG__
13#pragma implementation "choice.h"
14#endif
e40298d5 15
d8c736e5 16#include "wx/defs.h"
e9576ca5 17#include "wx/choice.h"
03e11df5 18#include "wx/menu.h"
519cb848 19#include "wx/mac/uma.h"
e40298d5 20
2f1ae414 21#if !USE_SHARED_LIBRARY
e9576ca5 22IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
2f1ae414 23#endif
e40298d5 24
892e461e 25extern MenuHandle NewUniqueMenu() ;
e40298d5 26
5b781a67
SC
27wxChoice::~wxChoice()
28{
d76240bf
MB
29 if ( HasClientObjectData() )
30 {
31 size_t i, max = GetCount();
32
33 for ( i = 0; i < max; ++i )
34 delete GetClientObject(i);
35 }
36
37 // DeleteMenu( m_macPopUpMenuId ) ;
e40298d5 38 // DisposeMenu( m_macPopUpMenuHandle ) ;
5b781a67 39}
e40298d5 40
584ad2a3
MB
41bool wxChoice::Create(wxWindow *parent, wxWindowID id,
42 const wxPoint& pos,
43 const wxSize& size,
44 const wxArrayString& choices,
45 long style,
46 const wxValidator& validator,
47 const wxString& name)
48{
49 wxCArrayString chs(choices);
50
51 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
52 style, validator, name);
53}
54
e9576ca5
SC
55bool wxChoice::Create(wxWindow *parent, wxWindowID id,
56 const wxPoint& pos,
57 const wxSize& size,
e40298d5
JS
58 int n, const wxString choices[],
59 long style,
e9576ca5
SC
60 const wxValidator& validator,
61 const wxString& name)
62{
b45ed7a2
VZ
63 if ( !wxChoiceBase::Create(parent, id, pos, size, style, validator, name) )
64 return false;
65
e40298d5
JS
66 Rect bounds ;
67 Str255 title ;
c69279ef 68
427ff662 69 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
c69279ef
RD
70 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , -12345 , 0 ,
71 kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ;
72
e40298d5
JS
73 m_macPopUpMenuHandle = NewUniqueMenu() ;
74 SetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ;
75 SetControl32BitMinimum( (ControlHandle) m_macControl , 0 ) ;
76 SetControl32BitMaximum( (ControlHandle) m_macControl , 0) ;
77 if ( n > 0 )
78 SetControl32BitValue( (ControlHandle) m_macControl , 1 ) ;
79 MacPostControlCreate() ;
258e4e91 80 // TODO wxCB_SORT
b668a735
SC
81 for ( int i = 0; i < n; i++ )
82 {
83 Append(choices[i]);
84 }
e40298d5 85 return TRUE;
e9576ca5 86}
e40298d5 87
b668a735
SC
88// ----------------------------------------------------------------------------
89// adding/deleting items to/from the list
90// ----------------------------------------------------------------------------
2597135a 91int wxChoice::DoAppend(const wxString& item)
e9576ca5 92{
a9412f8f 93 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() );
e40298d5
JS
94 m_strings.Add( item ) ;
95 m_datas.Add( NULL ) ;
96 int index = m_strings.GetCount() - 1 ;
97 DoSetItemClientData( index , NULL ) ;
98 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
99 return index ;
2597135a 100}
e40298d5 101
243dbf1a
VZ
102int wxChoice::DoInsert(const wxString& item, int pos)
103{
104 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
105 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
106
107 if (pos == GetCount())
108 return DoAppend(item);
109
a9412f8f 110 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() );
243dbf1a
VZ
111 m_strings.Insert( item, pos ) ;
112 m_datas.Insert( NULL, pos ) ;
113 DoSetItemClientData( pos , NULL ) ;
114 SetControl32BitMaximum( (ControlHandle) m_macControl , pos) ;
115 return pos ;
116}
117
b668a735 118void wxChoice::Delete(int n)
2597135a 119{
b668a735 120 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
b668a735 121 if ( HasClientObjectData() )
2597135a 122 {
b668a735 123 delete GetClientObject(n);
2597135a 124 }
76a5e5d2 125 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ;
5fe38474 126 m_strings.RemoveAt( n ) ;
3ef585df 127 m_datas.RemoveAt( n ) ;
e40298d5 128 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
e9576ca5 129}
e40298d5 130
e9576ca5
SC
131void wxChoice::Clear()
132{
4b651a46 133 FreeData();
b668a735 134 for ( int i = 0 ; i < GetCount() ; i++ )
519cb848 135 {
e40298d5 136 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
4b651a46 137 }
b668a735
SC
138 m_strings.Empty() ;
139 m_datas.Empty() ;
467e3168 140 SetControl32BitMaximum( (ControlHandle) m_macControl , 0 ) ;
e9576ca5 141}
e40298d5 142
4b651a46 143void wxChoice::FreeData()
b668a735
SC
144{
145 if ( HasClientObjectData() )
146 {
147 size_t count = GetCount();
148 for ( size_t n = 0; n < count; n++ )
149 {
150 delete GetClientObject(n);
151 }
152 }
153}
e40298d5 154
b668a735
SC
155// ----------------------------------------------------------------------------
156// selection
157// ----------------------------------------------------------------------------
e9576ca5
SC
158int wxChoice::GetSelection() const
159{
467e3168 160 return GetControl32BitValue( (ControlHandle) m_macControl ) -1 ;
e9576ca5 161}
e40298d5 162
b668a735 163void wxChoice::SetSelection(int n)
519cb848 164{
467e3168 165 SetControl32BitValue( (ControlHandle) m_macControl , n + 1 ) ;
519cb848 166}
e40298d5 167
b668a735
SC
168// ----------------------------------------------------------------------------
169// string list functions
170// ----------------------------------------------------------------------------
e40298d5 171
b668a735 172int wxChoice::GetCount() const
e9576ca5 173{
b668a735 174 return m_strings.GetCount() ;
e9576ca5 175}
e40298d5 176
e9576ca5
SC
177int wxChoice::FindString(const wxString& s) const
178{
b668a735 179 for( int i = 0 ; i < GetCount() ; i++ )
519cb848 180 {
e40298d5 181 if ( GetString( i ).IsSameAs(s, FALSE) )
c69279ef 182 return i ;
519cb848 183 }
b668a735
SC
184 return wxNOT_FOUND ;
185}
e40298d5 186
b668a735
SC
187void wxChoice::SetString(int n, const wxString& s)
188{
4b651a46 189 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
b668a735
SC
190#if 0 // should do this, but no Insert() so far
191 Delete(n);
192 Insert(n + 1, s);
193#endif
e9576ca5
SC
194}
195
196wxString wxChoice::GetString(int n) const
197{
17a6a662
VZ
198 wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), _T(""),
199 _T("wxChoice::GetString(): invalid index") );
200
e40298d5 201 return m_strings[n] ;
e9576ca5 202}
17a6a662 203
b668a735
SC
204// ----------------------------------------------------------------------------
205// client data
206// ----------------------------------------------------------------------------
b668a735
SC
207void wxChoice::DoSetItemClientData( int n, void* clientData )
208{
d81cc883 209 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
427ff662 210 wxT("invalid index in wxChoice::SetClientData") );
c69279ef 211
d81cc883 212 m_datas[n] = (char*) clientData ;
b668a735 213}
e40298d5 214
d81cc883 215void *wxChoice::DoGetItemClientData(int n) const
b668a735 216{
3ba18664
RD
217 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
218 wxT("invalid index in wxChoice::GetClientData") );
d81cc883 219 return (void *)m_datas[n];
b668a735 220}
e40298d5 221
b668a735
SC
222void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
223{
224 DoSetItemClientData(n, clientData);
225}
e40298d5 226
b668a735 227wxClientData* wxChoice::DoGetItemClientObject( int n ) const
e9576ca5 228{
b668a735 229 return (wxClientData *)DoGetItemClientData(n);
e9576ca5 230}
e40298d5 231
4b26b60f 232void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
b668a735
SC
233{
234 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
f2d990ad
SC
235 int n = GetSelection();
236 // actually n should be made sure by the os to be a valid selection, but ...
237 if ( n > -1 )
238 {
239 event.SetInt( n );
240 event.SetString(GetStringSelection());
241 event.SetEventObject(this);
f2d990ad
SC
242 if ( HasClientObjectData() )
243 event.SetClientObject( GetClientObject(n) );
244 else if ( HasClientUntypedData() )
245 event.SetClientData( GetClientData(n) );
f2d990ad
SC
246 ProcessCommand(event);
247 }
9453cf2b 248}
e40298d5 249
f2d990ad 250wxSize wxChoice::DoGetBestSize() const
b668a735 251{
637988a4 252 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
f2d990ad
SC
253 int lbHeight = 20;
254 int wLine;
255#if TARGET_CARBON
256 long metric ;
c69279ef
RD
257 GetThemeMetric(kThemeMetricPopupButtonHeight , &metric );
258 lbHeight = metric ;
f2d990ad 259#endif
e40298d5 260 {
c69279ef 261 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ;
fcb35beb 262 if ( m_font.Ok() )
e40298d5 263 {
fcb35beb
VZ
264 ::TextFont( m_font.GetMacFontNum() ) ;
265 ::TextSize( m_font.GetMacFontSize() ) ;
266 ::TextFace( m_font.GetMacFontStyle() ) ;
e40298d5
JS
267 }
268 else
269 {
270 ::TextFont( kFontIDMonaco ) ;
271 ::TextSize( 9 );
272 ::TextFace( 0 ) ;
273 }
274 // Find the widest line
275 for(int i = 0; i < GetCount(); i++) {
276 wxString str(GetString(i));
2c1a3312
SC
277 #if wxUSE_UNICODE
278 Point bounds={0,0} ;
279 SInt16 baseline ;
a9412f8f 280 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
2c1a3312
SC
281 kThemeCurrentPortFont,
282 kThemeStateActive,
283 false,
284 &bounds,
285 &baseline );
286 wLine = bounds.h ;
287 #else
939fba6c 288 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
2c1a3312 289 #endif
e40298d5
JS
290 lbWidth = wxMax(lbWidth, wLine);
291 }
292 // Add room for the popup arrow
293 lbWidth += 2 * lbHeight ;
294 // And just a bit more
e40298d5
JS
295 int cx = ::TextWidth( "X" , 0 , 1 ) ;
296 lbWidth += cx ;
c69279ef 297
e40298d5 298 }
f2d990ad 299 return wxSize(lbWidth, lbHeight);
d76240bf 300}