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