]>
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 |
e40298d5 | 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 SC |
68 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; |
69 | m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , "\p" , true , 0 , -12345 , 0 , | |
c69279ef RD |
70 | kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ; |
71 | ||
e40298d5 | 72 | m_macPopUpMenuHandle = NewUniqueMenu() ; |
facd6764 SC |
73 | SetControlData( (ControlRef) m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ; |
74 | SetControl32BitMinimum( (ControlRef) m_macControl , 0 ) ; | |
75 | SetControl32BitMaximum( (ControlRef) m_macControl , 0) ; | |
e40298d5 | 76 | if ( n > 0 ) |
facd6764 SC |
77 | SetControl32BitValue( (ControlRef) m_macControl , 1 ) ; |
78 | MacPostControlCreate(pos,size) ; | |
258e4e91 | 79 | // TODO wxCB_SORT |
b668a735 SC |
80 | for ( int i = 0; i < n; i++ ) |
81 | { | |
82 | Append(choices[i]); | |
83 | } | |
e40298d5 | 84 | return TRUE; |
e9576ca5 | 85 | } |
e40298d5 | 86 | |
b668a735 SC |
87 | // ---------------------------------------------------------------------------- |
88 | // adding/deleting items to/from the list | |
89 | // ---------------------------------------------------------------------------- | |
2597135a | 90 | int wxChoice::DoAppend(const wxString& item) |
e9576ca5 | 91 | { |
a9412f8f | 92 | UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() ); |
e40298d5 JS |
93 | m_strings.Add( item ) ; |
94 | m_datas.Add( NULL ) ; | |
95 | int index = m_strings.GetCount() - 1 ; | |
96 | DoSetItemClientData( index , NULL ) ; | |
facd6764 | 97 | SetControl32BitMaximum( (ControlRef) m_macControl , GetCount()) ; |
e40298d5 | 98 | return index ; |
2597135a | 99 | } |
e40298d5 | 100 | |
243dbf1a VZ |
101 | int wxChoice::DoInsert(const wxString& item, int pos) |
102 | { | |
103 | wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); | |
104 | wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
105 | ||
106 | if (pos == GetCount()) | |
107 | return DoAppend(item); | |
108 | ||
a9412f8f | 109 | UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item, m_font.GetEncoding() ); |
243dbf1a VZ |
110 | m_strings.Insert( item, pos ) ; |
111 | m_datas.Insert( NULL, pos ) ; | |
112 | DoSetItemClientData( pos , NULL ) ; | |
facd6764 | 113 | SetControl32BitMaximum( (ControlRef) m_macControl , pos) ; |
243dbf1a VZ |
114 | return pos ; |
115 | } | |
116 | ||
b668a735 | 117 | void wxChoice::Delete(int n) |
2597135a | 118 | { |
b668a735 | 119 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); |
b668a735 | 120 | if ( HasClientObjectData() ) |
2597135a | 121 | { |
b668a735 | 122 | delete GetClientObject(n); |
2597135a | 123 | } |
76a5e5d2 | 124 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ; |
5fe38474 | 125 | m_strings.RemoveAt( n ) ; |
3ef585df | 126 | m_datas.RemoveAt( n ) ; |
facd6764 | 127 | SetControl32BitMaximum( (ControlRef) m_macControl , GetCount()) ; |
e9576ca5 | 128 | } |
e40298d5 | 129 | |
e9576ca5 SC |
130 | void wxChoice::Clear() |
131 | { | |
4b651a46 | 132 | FreeData(); |
b668a735 | 133 | for ( int i = 0 ; i < GetCount() ; i++ ) |
519cb848 | 134 | { |
e40298d5 | 135 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ; |
4b651a46 | 136 | } |
b668a735 SC |
137 | m_strings.Empty() ; |
138 | m_datas.Empty() ; | |
facd6764 | 139 | SetControl32BitMaximum( (ControlRef) m_macControl , 0 ) ; |
e9576ca5 | 140 | } |
e40298d5 | 141 | |
4b651a46 | 142 | void wxChoice::FreeData() |
b668a735 SC |
143 | { |
144 | if ( HasClientObjectData() ) | |
145 | { | |
146 | size_t count = GetCount(); | |
147 | for ( size_t n = 0; n < count; n++ ) | |
148 | { | |
149 | delete GetClientObject(n); | |
150 | } | |
151 | } | |
152 | } | |
e40298d5 | 153 | |
b668a735 SC |
154 | // ---------------------------------------------------------------------------- |
155 | // selection | |
156 | // ---------------------------------------------------------------------------- | |
e9576ca5 SC |
157 | int wxChoice::GetSelection() const |
158 | { | |
facd6764 | 159 | return GetControl32BitValue( (ControlRef) m_macControl ) -1 ; |
e9576ca5 | 160 | } |
e40298d5 | 161 | |
b668a735 | 162 | void wxChoice::SetSelection(int n) |
519cb848 | 163 | { |
facd6764 | 164 | SetControl32BitValue( (ControlRef) m_macControl , n + 1 ) ; |
519cb848 | 165 | } |
e40298d5 | 166 | |
b668a735 SC |
167 | // ---------------------------------------------------------------------------- |
168 | // string list functions | |
169 | // ---------------------------------------------------------------------------- | |
e40298d5 | 170 | |
b668a735 | 171 | int wxChoice::GetCount() const |
e9576ca5 | 172 | { |
b668a735 | 173 | return m_strings.GetCount() ; |
e9576ca5 | 174 | } |
e40298d5 | 175 | |
e9576ca5 SC |
176 | int wxChoice::FindString(const wxString& s) const |
177 | { | |
b668a735 | 178 | for( int i = 0 ; i < GetCount() ; i++ ) |
519cb848 | 179 | { |
e40298d5 | 180 | if ( GetString( i ).IsSameAs(s, FALSE) ) |
c69279ef | 181 | return i ; |
519cb848 | 182 | } |
b668a735 SC |
183 | return wxNOT_FOUND ; |
184 | } | |
e40298d5 | 185 | |
b668a735 SC |
186 | void wxChoice::SetString(int n, const wxString& s) |
187 | { | |
4b651a46 | 188 | wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented")); |
b668a735 SC |
189 | #if 0 // should do this, but no Insert() so far |
190 | Delete(n); | |
191 | Insert(n + 1, s); | |
192 | #endif | |
e9576ca5 SC |
193 | } |
194 | ||
195 | wxString wxChoice::GetString(int n) const | |
196 | { | |
17a6a662 VZ |
197 | wxCHECK_MSG( n >= 0 && (size_t)n < m_strings.GetCount(), _T(""), |
198 | _T("wxChoice::GetString(): invalid index") ); | |
199 | ||
e40298d5 | 200 | return m_strings[n] ; |
e9576ca5 | 201 | } |
17a6a662 | 202 | |
b668a735 SC |
203 | // ---------------------------------------------------------------------------- |
204 | // client data | |
205 | // ---------------------------------------------------------------------------- | |
b668a735 SC |
206 | void wxChoice::DoSetItemClientData( int n, void* clientData ) |
207 | { | |
d81cc883 | 208 | wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(), |
427ff662 | 209 | wxT("invalid index in wxChoice::SetClientData") ); |
c69279ef | 210 | |
d81cc883 | 211 | m_datas[n] = (char*) clientData ; |
b668a735 | 212 | } |
e40298d5 | 213 | |
d81cc883 | 214 | void *wxChoice::DoGetItemClientData(int n) const |
b668a735 | 215 | { |
3ba18664 RD |
216 | wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL, |
217 | wxT("invalid index in wxChoice::GetClientData") ); | |
d81cc883 | 218 | return (void *)m_datas[n]; |
b668a735 | 219 | } |
e40298d5 | 220 | |
b668a735 SC |
221 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) |
222 | { | |
223 | DoSetItemClientData(n, clientData); | |
224 | } | |
e40298d5 | 225 | |
b668a735 | 226 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const |
e9576ca5 | 227 | { |
b668a735 | 228 | return (wxClientData *)DoGetItemClientData(n); |
e9576ca5 | 229 | } |
e40298d5 | 230 | |
4b26b60f | 231 | void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown)) |
b668a735 SC |
232 | { |
233 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); | |
f2d990ad SC |
234 | int n = GetSelection(); |
235 | // actually n should be made sure by the os to be a valid selection, but ... | |
236 | if ( n > -1 ) | |
237 | { | |
238 | event.SetInt( n ); | |
239 | event.SetString(GetStringSelection()); | |
240 | event.SetEventObject(this); | |
f2d990ad SC |
241 | if ( HasClientObjectData() ) |
242 | event.SetClientObject( GetClientObject(n) ); | |
243 | else if ( HasClientUntypedData() ) | |
244 | event.SetClientData( GetClientData(n) ); | |
f2d990ad SC |
245 | ProcessCommand(event); |
246 | } | |
9453cf2b | 247 | } |
e40298d5 | 248 | |
f2d990ad | 249 | wxSize wxChoice::DoGetBestSize() const |
b668a735 | 250 | { |
637988a4 | 251 | int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults |
f2d990ad SC |
252 | int lbHeight = 20; |
253 | int wLine; | |
254 | #if TARGET_CARBON | |
255 | long metric ; | |
c69279ef RD |
256 | GetThemeMetric(kThemeMetricPopupButtonHeight , &metric ); |
257 | lbHeight = metric ; | |
f2d990ad | 258 | #endif |
e40298d5 | 259 | { |
facd6764 | 260 | wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ; |
fcb35beb | 261 | if ( m_font.Ok() ) |
e40298d5 | 262 | { |
facd6764 SC |
263 | ::TextFont( m_font.MacGetFontNum() ) ; |
264 | ::TextSize( m_font.MacGetFontSize() ) ; | |
265 | ::TextFace( m_font.MacGetFontStyle() ) ; | |
e40298d5 JS |
266 | } |
267 | else | |
268 | { | |
269 | ::TextFont( kFontIDMonaco ) ; | |
270 | ::TextSize( 9 ); | |
271 | ::TextFace( 0 ) ; | |
272 | } | |
273 | // Find the widest line | |
274 | for(int i = 0; i < GetCount(); i++) { | |
275 | wxString str(GetString(i)); | |
2c1a3312 SC |
276 | #if wxUSE_UNICODE |
277 | Point bounds={0,0} ; | |
278 | SInt16 baseline ; | |
a9412f8f | 279 | ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) , |
2c1a3312 SC |
280 | kThemeCurrentPortFont, |
281 | kThemeStateActive, | |
282 | false, | |
283 | &bounds, | |
284 | &baseline ); | |
285 | wLine = bounds.h ; | |
286 | #else | |
939fba6c | 287 | wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ; |
2c1a3312 | 288 | #endif |
e40298d5 JS |
289 | lbWidth = wxMax(lbWidth, wLine); |
290 | } | |
291 | // Add room for the popup arrow | |
292 | lbWidth += 2 * lbHeight ; | |
293 | // And just a bit more | |
e40298d5 JS |
294 | int cx = ::TextWidth( "X" , 0 , 1 ) ; |
295 | lbWidth += cx ; | |
c69279ef | 296 | |
e40298d5 | 297 | } |
f2d990ad | 298 | return wxSize(lbWidth, lbHeight); |
d76240bf | 299 | } |