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