]>
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 ; | |
51 | ||
52 | MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ; | |
53 | m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , -12345 , 0 , | |
54 | kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ; | |
55 | ||
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 | |
b668a735 | 84 | void wxChoice::Delete(int n) |
2597135a | 85 | { |
b668a735 | 86 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); |
b668a735 | 87 | if ( HasClientObjectData() ) |
2597135a | 88 | { |
b668a735 | 89 | delete GetClientObject(n); |
2597135a | 90 | } |
76a5e5d2 | 91 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ; |
2f1ae414 | 92 | m_strings.Remove( n ) ; |
3ef585df | 93 | m_datas.RemoveAt( n ) ; |
e40298d5 | 94 | SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ; |
e9576ca5 | 95 | } |
e40298d5 | 96 | |
e9576ca5 SC |
97 | void wxChoice::Clear() |
98 | { | |
4b651a46 | 99 | FreeData(); |
b668a735 | 100 | for ( int i = 0 ; i < GetCount() ; i++ ) |
519cb848 | 101 | { |
e40298d5 | 102 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ; |
4b651a46 | 103 | } |
b668a735 SC |
104 | m_strings.Empty() ; |
105 | m_datas.Empty() ; | |
467e3168 | 106 | SetControl32BitMaximum( (ControlHandle) m_macControl , 0 ) ; |
e9576ca5 | 107 | } |
e40298d5 | 108 | |
4b651a46 | 109 | void wxChoice::FreeData() |
b668a735 SC |
110 | { |
111 | if ( HasClientObjectData() ) | |
112 | { | |
113 | size_t count = GetCount(); | |
114 | for ( size_t n = 0; n < count; n++ ) | |
115 | { | |
116 | delete GetClientObject(n); | |
117 | } | |
118 | } | |
119 | } | |
e40298d5 | 120 | |
b668a735 SC |
121 | // ---------------------------------------------------------------------------- |
122 | // selection | |
123 | // ---------------------------------------------------------------------------- | |
e9576ca5 SC |
124 | int wxChoice::GetSelection() const |
125 | { | |
467e3168 | 126 | return GetControl32BitValue( (ControlHandle) m_macControl ) -1 ; |
e9576ca5 | 127 | } |
e40298d5 | 128 | |
b668a735 | 129 | void wxChoice::SetSelection(int n) |
519cb848 | 130 | { |
467e3168 | 131 | SetControl32BitValue( (ControlHandle) m_macControl , n + 1 ) ; |
519cb848 | 132 | } |
e40298d5 | 133 | |
b668a735 SC |
134 | // ---------------------------------------------------------------------------- |
135 | // string list functions | |
136 | // ---------------------------------------------------------------------------- | |
e40298d5 | 137 | |
b668a735 | 138 | int wxChoice::GetCount() const |
e9576ca5 | 139 | { |
b668a735 | 140 | return m_strings.GetCount() ; |
e9576ca5 | 141 | } |
e40298d5 | 142 | |
e9576ca5 SC |
143 | int wxChoice::FindString(const wxString& s) const |
144 | { | |
b668a735 | 145 | for( int i = 0 ; i < GetCount() ; i++ ) |
519cb848 | 146 | { |
e40298d5 JS |
147 | if ( GetString( i ).IsSameAs(s, FALSE) ) |
148 | return i ; | |
519cb848 | 149 | } |
b668a735 SC |
150 | return wxNOT_FOUND ; |
151 | } | |
e40298d5 | 152 | |
b668a735 SC |
153 | void wxChoice::SetString(int n, const wxString& s) |
154 | { | |
4b651a46 | 155 | wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented")); |
b668a735 SC |
156 | #if 0 // should do this, but no Insert() so far |
157 | Delete(n); | |
158 | Insert(n + 1, s); | |
159 | #endif | |
e9576ca5 SC |
160 | } |
161 | ||
162 | wxString wxChoice::GetString(int n) const | |
163 | { | |
e40298d5 | 164 | return m_strings[n] ; |
e9576ca5 | 165 | } |
b668a735 SC |
166 | // ---------------------------------------------------------------------------- |
167 | // client data | |
168 | // ---------------------------------------------------------------------------- | |
b668a735 SC |
169 | void wxChoice::DoSetItemClientData( int n, void* clientData ) |
170 | { | |
d81cc883 | 171 | wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(), |
b668a735 | 172 | "invalid index in wxChoice::SetClientData" ); |
e40298d5 | 173 | |
d81cc883 | 174 | m_datas[n] = (char*) clientData ; |
b668a735 | 175 | } |
e40298d5 | 176 | |
d81cc883 | 177 | void *wxChoice::DoGetItemClientData(int n) const |
b668a735 | 178 | { |
d81cc883 | 179 | wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL, |
b668a735 | 180 | "invalid index in wxChoice::GetClientData" ); |
d81cc883 | 181 | return (void *)m_datas[n]; |
b668a735 | 182 | } |
e40298d5 | 183 | |
b668a735 SC |
184 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) |
185 | { | |
186 | DoSetItemClientData(n, clientData); | |
187 | } | |
e40298d5 | 188 | |
b668a735 | 189 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const |
e9576ca5 | 190 | { |
b668a735 | 191 | return (wxClientData *)DoGetItemClientData(n); |
e9576ca5 | 192 | } |
e40298d5 | 193 | |
76a5e5d2 | 194 | void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart ) |
b668a735 SC |
195 | { |
196 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); | |
f2d990ad SC |
197 | int n = GetSelection(); |
198 | // actually n should be made sure by the os to be a valid selection, but ... | |
199 | if ( n > -1 ) | |
200 | { | |
201 | event.SetInt( n ); | |
202 | event.SetString(GetStringSelection()); | |
203 | event.SetEventObject(this); | |
f2d990ad SC |
204 | if ( HasClientObjectData() ) |
205 | event.SetClientObject( GetClientObject(n) ); | |
206 | else if ( HasClientUntypedData() ) | |
207 | event.SetClientData( GetClientData(n) ); | |
f2d990ad SC |
208 | ProcessCommand(event); |
209 | } | |
9453cf2b | 210 | } |
e40298d5 | 211 | |
f2d990ad | 212 | wxSize wxChoice::DoGetBestSize() const |
b668a735 | 213 | { |
637988a4 | 214 | int lbWidth = GetCount() > 0 ? 20 : 100; // some defaults |
f2d990ad SC |
215 | int lbHeight = 20; |
216 | int wLine; | |
217 | #if TARGET_CARBON | |
218 | long metric ; | |
219 | GetThemeMetric(kThemeMetricPopupButtonHeight , &metric ); | |
220 | lbHeight = metric ; | |
221 | #endif | |
e40298d5 JS |
222 | { |
223 | wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ; | |
224 | Rect drawRect ; | |
225 | wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ; | |
226 | if ( font ) | |
227 | { | |
228 | ::TextFont( font->m_macFontNum ) ; | |
229 | ::TextSize( short(font->m_macFontSize) ) ; | |
230 | ::TextFace( font->m_macFontStyle ) ; | |
231 | } | |
232 | else | |
233 | { | |
234 | ::TextFont( kFontIDMonaco ) ; | |
235 | ::TextSize( 9 ); | |
236 | ::TextFace( 0 ) ; | |
237 | } | |
238 | // Find the widest line | |
239 | for(int i = 0; i < GetCount(); i++) { | |
240 | wxString str(GetString(i)); | |
241 | wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ; | |
242 | lbWidth = wxMax(lbWidth, wLine); | |
243 | } | |
244 | // Add room for the popup arrow | |
245 | lbWidth += 2 * lbHeight ; | |
246 | // And just a bit more | |
247 | int cy = 12 ; | |
248 | int cx = ::TextWidth( "X" , 0 , 1 ) ; | |
249 | lbWidth += cx ; | |
250 | ||
251 | } | |
f2d990ad | 252 | return wxSize(lbWidth, lbHeight); |
d76240bf | 253 | } |