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