]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/choice.cpp
Second try to add "support" for deriving from
[wxWidgets.git] / src / mac / classic / choice.cpp
CommitLineData
2646f485
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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
65571936 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "choice.h"
14#endif
15
16#include "wx/defs.h"
312ebad4
WS
17
18#if wxUSE_CHOICE
19
2646f485
SC
20#include "wx/choice.h"
21#include "wx/menu.h"
22#include "wx/mac/uma.h"
23
24#if !USE_SHARED_LIBRARY
25IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
26#endif
27
28extern MenuHandle NewUniqueMenu() ;
29
30wxChoice::~wxChoice()
31{
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 ) ;
41 // DisposeMenu( m_macPopUpMenuHandle ) ;
42}
43
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
58bool wxChoice::Create(wxWindow *parent, wxWindowID id,
59 const wxPoint& pos,
60 const wxSize& size,
61 int n, const wxString choices[],
62 long style,
63 const wxValidator& validator,
64 const wxString& name)
65{
66 if ( !wxChoiceBase::Create(parent, id, pos, size, style, validator, name) )
67 return false;
68
69 Rect bounds ;
70 Str255 title ;
71
72 MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ;
6bc3b8e9 73 m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , -12345 , 0 ,
2646f485
SC
74 kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ;
75
76 m_macPopUpMenuHandle = NewUniqueMenu() ;
77 SetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ;
78 SetControl32BitMinimum( (ControlHandle) m_macControl , 0 ) ;
79 SetControl32BitMaximum( (ControlHandle) m_macControl , 0) ;
80 if ( n > 0 )
81 SetControl32BitValue( (ControlHandle) m_macControl , 1 ) ;
82 MacPostControlCreate() ;
83 // TODO wxCB_SORT
84 for ( int i = 0; i < n; i++ )
85 {
86 Append(choices[i]);
87 }
312ebad4 88 return true;
2646f485
SC
89}
90
91// ----------------------------------------------------------------------------
92// adding/deleting items to/from the list
93// ----------------------------------------------------------------------------
94int wxChoice::DoAppend(const wxString& item)
95{
96 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() );
97 m_strings.Add( item ) ;
98 m_datas.Add( NULL ) ;
99 int index = m_strings.GetCount() - 1 ;
100 DoSetItemClientData( index , NULL ) ;
101 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
102 return index ;
103}
104
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
113 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() );
114 m_strings.Insert( item, pos ) ;
115 m_datas.Insert( NULL, pos ) ;
116 DoSetItemClientData( pos , NULL ) ;
117 SetControl32BitMaximum( (ControlHandle) m_macControl , pos) ;
118 return pos ;
119}
120
121void wxChoice::Delete(int n)
122{
123 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
124 if ( HasClientObjectData() )
125 {
126 delete GetClientObject(n);
127 }
128 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ;
129 m_strings.RemoveAt( n ) ;
130 m_datas.RemoveAt( n ) ;
131 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
132}
133
134void wxChoice::Clear()
135{
136 FreeData();
137 for ( int i = 0 ; i < GetCount() ; i++ )
138 {
139 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
140 }
141 m_strings.Empty() ;
142 m_datas.Empty() ;
143 SetControl32BitMaximum( (ControlHandle) m_macControl , 0 ) ;
144}
145
146void wxChoice::FreeData()
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}
157
158// ----------------------------------------------------------------------------
159// selection
160// ----------------------------------------------------------------------------
161int wxChoice::GetSelection() const
162{
163 return GetControl32BitValue( (ControlHandle) m_macControl ) -1 ;
164}
165
166void wxChoice::SetSelection(int n)
167{
168 SetControl32BitValue( (ControlHandle) m_macControl , n + 1 ) ;
169}
170
171// ----------------------------------------------------------------------------
172// string list functions
173// ----------------------------------------------------------------------------
174
175int wxChoice::GetCount() const
176{
177 return m_strings.GetCount() ;
178}
179
180int wxChoice::FindString(const wxString& s) const
181{
182 for( int i = 0 ; i < GetCount() ; i++ )
183 {
312ebad4 184 if ( GetString( i ).IsSameAs(s, false) )
2646f485
SC
185 return i ;
186 }
187 return wxNOT_FOUND ;
188}
189
190void wxChoice::SetString(int n, const wxString& s)
191{
192 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
193#if 0 // should do this, but no Insert() so far
194 Delete(n);
195 Insert(n + 1, s);
196#endif
197}
198
199wxString wxChoice::GetString(int n) const
200{
201 wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), _T(""),
202 _T("wxChoice::GetString(): invalid index") );
203
204 return m_strings[n] ;
205}
206
207// ----------------------------------------------------------------------------
208// client data
209// ----------------------------------------------------------------------------
210void wxChoice::DoSetItemClientData( int n, void* clientData )
211{
212 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
213 wxT("invalid index in wxChoice::SetClientData") );
214
215 m_datas[n] = (char*) clientData ;
216}
217
218void *wxChoice::DoGetItemClientData(int n) const
219{
220 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
221 wxT("invalid index in wxChoice::GetClientData") );
222 return (void *)m_datas[n];
223}
224
225void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
226{
227 DoSetItemClientData(n, clientData);
228}
229
230wxClientData* wxChoice::DoGetItemClientObject( int n ) const
231{
232 return (wxClientData *)DoGetItemClientData(n);
233}
234
312ebad4 235void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
2646f485
SC
236{
237 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
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);
245 if ( HasClientObjectData() )
246 event.SetClientObject( GetClientObject(n) );
247 else if ( HasClientUntypedData() )
248 event.SetClientData( GetClientData(n) );
249 ProcessCommand(event);
250 }
251}
252
253wxSize wxChoice::DoGetBestSize() const
254{
255 int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults
256 int lbHeight = 20;
257 int wLine;
258#if TARGET_CARBON
259 long metric ;
260 GetThemeMetric(kThemeMetricPopupButtonHeight , &metric );
261 lbHeight = metric ;
262#endif
263 {
264 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ;
265 if ( m_font.Ok() )
266 {
267 ::TextFont( m_font.GetMacFontNum() ) ;
268 ::TextSize( m_font.GetMacFontSize() ) ;
269 ::TextFace( m_font.GetMacFontStyle() ) ;
270 }
271 else
272 {
273 ::TextFont( kFontIDMonaco ) ;
274 ::TextSize( 9 );
275 ::TextFace( 0 ) ;
276 }
277 // Find the widest line
278 for(int i = 0; i < GetCount(); i++) {
279 wxString str(GetString(i));
280 #if wxUSE_UNICODE
281 Point bounds={0,0} ;
282 SInt16 baseline ;
283 ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) ,
284 kThemeCurrentPortFont,
285 kThemeStateActive,
286 false,
287 &bounds,
288 &baseline );
289 wLine = bounds.h ;
290 #else
291 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
292 #endif
293 lbWidth = wxMax(lbWidth, wLine);
294 }
295 // Add room for the popup arrow
296 lbWidth += 2 * lbHeight ;
297 // And just a bit more
298 int cx = ::TextWidth( "X" , 0 , 1 ) ;
299 lbWidth += cx ;
300
301 }
302 return wxSize(lbWidth, lbHeight);
303}
312ebad4
WS
304
305#endif // wxUSE_CHOICE