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