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