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