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