]> git.saurik.com Git - wxWidgets.git/blame - src/mac/classic/choice.cpp
Include wx/log.h according to precompiled headers of wx/wx.h (with other minor cleaning).
[wxWidgets.git] / src / mac / classic / choice.cpp
CommitLineData
2646f485 1/////////////////////////////////////////////////////////////////////////////
11e62fe6 2// Name: src/mac/classic/choice.cpp
2646f485
SC
3// Purpose: wxChoice
4// Author: Stefan Csomor
5// Modified by:
6// Created: 1998-01-01
7// RCS-ID: $Id$
8// Copyright: (c) Stefan Csomor
8228b893 9// Licence: wxWindows licence
2646f485
SC
10/////////////////////////////////////////////////////////////////////////////
11
8228b893 12#include "wx/wxprec.h"
312ebad4
WS
13
14#if wxUSE_CHOICE
15
2646f485
SC
16#include "wx/choice.h"
17#include "wx/menu.h"
18#include "wx/mac/uma.h"
19
2646f485 20IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
2646f485
SC
21
22extern MenuHandle NewUniqueMenu() ;
23
24wxChoice::~wxChoice()
25{
26 if ( HasClientObjectData() )
27 {
aa61d352 28 unsigned int i, max = GetCount();
2646f485
SC
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 ) ;
6bc3b8e9 67 m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , -12345 , 0 ,
2646f485
SC
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 }
312ebad4 82 return true;
2646f485
SC
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
aa61d352 99int wxChoice::DoInsert(const wxString& item, unsigned int pos)
2646f485
SC
100{
101 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
8228b893 102 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
2646f485
SC
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
aa61d352 115void wxChoice::Delete(unsigned int n)
2646f485 116{
8228b893 117 wxCHECK_RET( IsValid(n), wxT("invalid item index in wxChoice::Delete") );
2646f485
SC
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();
aa61d352 131 for ( unsigned int i = 0 ; i < GetCount() ; i++ )
2646f485
SC
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 {
aa61d352
VZ
144 unsigned int count = GetCount();
145 for ( unsigned int n = 0; n < count; n++ )
2646f485
SC
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
aa61d352 169unsigned int wxChoice::GetCount() const
2646f485
SC
170{
171 return m_strings.GetCount() ;
172}
173
aa61d352 174void wxChoice::SetString(unsigned int n, const wxString& s)
2646f485
SC
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
aa61d352 183wxString wxChoice::GetString(unsigned int n) const
2646f485 184{
8228b893 185 wxCHECK_MSG( IsValid(n), wxEmptyString,
2646f485
SC
186 _T("wxChoice::GetString(): invalid index") );
187
188 return m_strings[n] ;
189}
190
191// ----------------------------------------------------------------------------
192// client data
193// ----------------------------------------------------------------------------
aa61d352 194void wxChoice::DoSetItemClientData(unsigned int n, void* clientData)
2646f485 195{
aa61d352 196 wxCHECK_RET( n >= 0 && (unsigned int)n < m_datas.GetCount(),
2646f485
SC
197 wxT("invalid index in wxChoice::SetClientData") );
198
199 m_datas[n] = (char*) clientData ;
200}
201
aa61d352 202void *wxChoice::DoGetItemClientData(unsigned int n) const
2646f485 203{
aa61d352 204 wxCHECK_MSG( n >= 0 && (unsigned int)n < m_datas.GetCount(), NULL,
2646f485
SC
205 wxT("invalid index in wxChoice::GetClientData") );
206 return (void *)m_datas[n];
207}
208
aa61d352 209void wxChoice::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
2646f485
SC
210{
211 DoSetItemClientData(n, clientData);
212}
213
aa61d352 214wxClientData* wxChoice::DoGetItemClientObject(unsigned int n) const
2646f485
SC
215{
216 return (wxClientData *)DoGetItemClientData(n);
217}
218
312ebad4 219void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown))
2646f485
SC
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
aa61d352 262 for(unsigned int i = 0; i < GetCount(); i++) {
2646f485
SC
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
8228b893 275 wLine = ::TextWidth( str.c_str() , 0 , str.length() ) ;
2646f485
SC
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}
312ebad4
WS
288
289#endif // wxUSE_CHOICE