]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/choice.cpp
Applied [ 1059554 ] patch for [1028659] fixes a couple of bugs with menus
[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
21fd5529 73 m_peer = new wxMacControl() ;
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) ;
258e4e91 82 // TODO wxCB_SORT
b668a735
SC
83 for ( int i = 0; i < n; i++ )
84 {
85 Append(choices[i]);
86 }
d3b5db4b 87 SetBestSize(size); // Needed because it is a wxControlWithItems
312ebad4 88 return true;
e9576ca5 89}
e40298d5 90
b668a735
SC
91// ----------------------------------------------------------------------------
92// adding/deleting items to/from the list
93// ----------------------------------------------------------------------------
2597135a 94int wxChoice::DoAppend(const wxString& item)
e9576ca5 95{
a9412f8f 96 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() );
e40298d5
JS
97 m_strings.Add( item ) ;
98 m_datas.Add( NULL ) ;
99 int index = m_strings.GetCount() - 1 ;
100 DoSetItemClientData( index , NULL ) ;
21fd5529 101 m_peer->SetMaximum( GetCount() ) ;
e40298d5 102 return index ;
2597135a 103}
e40298d5 104
243dbf1a
VZ
105int wxChoice::DoInsert(const wxString& item, int pos)
106{
107 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
108 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
109
110 if (pos == GetCount())
111 return DoAppend(item);
112
a9412f8f 113 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() );
243dbf1a
VZ
114 m_strings.Insert( item, pos ) ;
115 m_datas.Insert( NULL, pos ) ;
116 DoSetItemClientData( pos , NULL ) ;
21fd5529 117 m_peer->SetMaximum( GetCount() ) ;
243dbf1a
VZ
118 return pos ;
119}
120
b668a735 121void wxChoice::Delete(int n)
2597135a 122{
b668a735 123 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
b668a735 124 if ( HasClientObjectData() )
2597135a 125 {
b668a735 126 delete GetClientObject(n);
2597135a 127 }
76a5e5d2 128 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ;
5fe38474 129 m_strings.RemoveAt( n ) ;
3ef585df 130 m_datas.RemoveAt( n ) ;
21fd5529 131 m_peer->SetMaximum( GetCount() ) ;
e9576ca5 132}
e40298d5 133
e9576ca5
SC
134void wxChoice::Clear()
135{
4b651a46 136 FreeData();
b668a735 137 for ( int i = 0 ; i < GetCount() ; i++ )
519cb848 138 {
e40298d5 139 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
4b651a46 140 }
b668a735
SC
141 m_strings.Empty() ;
142 m_datas.Empty() ;
21fd5529 143 m_peer->SetMaximum( 0 ) ;
e9576ca5 144}
e40298d5 145
4b651a46 146void wxChoice::FreeData()
b668a735
SC
147{
148 if ( HasClientObjectData() )
149 {
150 size_t count = GetCount();
151 for ( size_t n = 0; n < count; n++ )
152 {
153 delete GetClientObject(n);
154 }
155 }
156}
e40298d5 157
b668a735
SC
158// ----------------------------------------------------------------------------
159// selection
160// ----------------------------------------------------------------------------
e9576ca5
SC
161int wxChoice::GetSelection() const
162{
21fd5529 163 return m_peer->GetValue() -1 ;
e9576ca5 164}
e40298d5 165
b668a735 166void wxChoice::SetSelection(int n)
519cb848 167{
21fd5529 168 m_peer->SetValue( n + 1 ) ;
519cb848 169}
e40298d5 170
b668a735
SC
171// ----------------------------------------------------------------------------
172// string list functions
173// ----------------------------------------------------------------------------
e40298d5 174
b668a735 175int wxChoice::GetCount() const
e9576ca5 176{
b668a735 177 return m_strings.GetCount() ;
e9576ca5 178}
e40298d5 179
e9576ca5
SC
180int wxChoice::FindString(const wxString& s) const
181{
b668a735 182 for( int i = 0 ; i < GetCount() ; i++ )
519cb848 183 {
312ebad4 184 if ( GetString( i ).IsSameAs(s, false) )
c69279ef 185 return i ;
519cb848 186 }
b668a735
SC
187 return wxNOT_FOUND ;
188}
e40298d5 189
b668a735
SC
190void wxChoice::SetString(int n, const wxString& s)
191{
4b651a46 192 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
b668a735
SC
193#if 0 // should do this, but no Insert() so far
194 Delete(n);
195 Insert(n + 1, s);
196#endif
e9576ca5
SC
197}
198
199wxString wxChoice::GetString(int n) const
200{
17a6a662
VZ
201 wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), _T(""),
202 _T("wxChoice::GetString(): invalid index") );
203
e40298d5 204 return m_strings[n] ;
e9576ca5 205}
17a6a662 206
b668a735
SC
207// ----------------------------------------------------------------------------
208// client data
209// ----------------------------------------------------------------------------
b668a735
SC
210void wxChoice::DoSetItemClientData( int n, void* clientData )
211{
d81cc883 212 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
427ff662 213 wxT("invalid index in wxChoice::SetClientData") );
c69279ef 214
d81cc883 215 m_datas[n] = (char*) clientData ;
b668a735 216}
e40298d5 217
d81cc883 218void *wxChoice::DoGetItemClientData(int n) const
b668a735 219{
3ba18664
RD
220 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
221 wxT("invalid index in wxChoice::GetClientData") );
d81cc883 222 return (void *)m_datas[n];
b668a735 223}
e40298d5 224
b668a735
SC
225void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
226{
227 DoSetItemClientData(n, clientData);
228}
e40298d5 229
b668a735 230wxClientData* wxChoice::DoGetItemClientObject( int n ) const
e9576ca5 231{
b668a735 232 return (wxClientData *)DoGetItemClientData(n);
e9576ca5 233}
e40298d5 234
312ebad4 235wxInt32 wxChoice::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
b668a735
SC
236{
237 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
f2d990ad
SC
238 int n = GetSelection();
239 // actually n should be made sure by the os to be a valid selection, but ...
240 if ( n > -1 )
241 {
242 event.SetInt( n );
243 event.SetString(GetStringSelection());
244 event.SetEventObject(this);
f2d990ad
SC
245 if ( HasClientObjectData() )
246 event.SetClientObject( GetClientObject(n) );
247 else if ( HasClientUntypedData() )
248 event.SetClientData( GetClientData(n) );
f2d990ad
SC
249 ProcessCommand(event);
250 }
4c37f124 251 return noErr ;
9453cf2b 252}
e40298d5 253
f2d990ad 254wxSize wxChoice::DoGetBestSize() const
b668a735 255{
637988a4 256 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
f2d990ad
SC
257 int lbHeight = 20;
258 int wLine;
259#if TARGET_CARBON
260 long metric ;
c69279ef
RD
261 GetThemeMetric(kThemeMetricPopupButtonHeight , &metric );
262 lbHeight = metric ;
f2d990ad 263#endif
e40298d5 264 {
facd6764 265 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ;
fcb35beb 266 if ( m_font.Ok() )
e40298d5 267 {
facd6764
SC
268 ::TextFont( m_font.MacGetFontNum() ) ;
269 ::TextSize( m_font.MacGetFontSize() ) ;
270 ::TextFace( m_font.MacGetFontStyle() ) ;
e40298d5
JS
271 }
272 else
273 {
274 ::TextFont( kFontIDMonaco ) ;
275 ::TextSize( 9 );
276 ::TextFace( 0 ) ;
277 }
278 // Find the widest line
279 for(int i = 0; i < GetCount(); i++) {
280 wxString str(GetString(i));
2c1a3312
SC
281 #if wxUSE_UNICODE
282 Point bounds={0,0} ;
283 SInt16 baseline ;
a9412f8f 284 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
2c1a3312
SC
285 kThemeCurrentPortFont,
286 kThemeStateActive,
287 false,
288 &bounds,
289 &baseline );
290 wLine = bounds.h ;
291 #else
939fba6c 292 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
2c1a3312 293 #endif
e40298d5
JS
294 lbWidth = wxMax(lbWidth, wLine);
295 }
296 // Add room for the popup arrow
297 lbWidth += 2 * lbHeight ;
298 // And just a bit more
e40298d5
JS
299 int cx = ::TextWidth( "X" , 0 , 1 ) ;
300 lbWidth += cx ;
c69279ef 301
e40298d5 302 }
f2d990ad 303 return wxSize(lbWidth, lbHeight);
d76240bf 304}
312ebad4
WS
305
306#endif // wxUSE_CHOICE