]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: choice.cpp | |
3 | // Purpose: wxChoice | |
4 | // Author: Julian Smart | |
8d99be5f | 5 | // Modified by: Vadim Zeitlin to derive from wxChoiceBase |
2bda0e17 KB |
6 | // Created: 04/01/98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
c085e333 | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
8d99be5f VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
8d99be5f | 21 | #pragma implementation "choice.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
8d99be5f | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
31 | #ifndef WX_PRECOMP | |
8d99be5f VZ |
32 | #include "wx/choice.h" |
33 | #include "wx/utils.h" | |
34 | #include "wx/log.h" | |
c7401637 | 35 | #include "wx/settings.h" |
2bda0e17 KB |
36 | #endif |
37 | ||
38 | #include "wx/msw/private.h" | |
39 | ||
40 | #if !USE_SHARED_LIBRARY | |
8d99be5f | 41 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) |
2bda0e17 KB |
42 | #endif |
43 | ||
8d99be5f VZ |
44 | // ============================================================================ |
45 | // implementation | |
46 | // ============================================================================ | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
49 | // creation | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | bool wxChoice::Create(wxWindow *parent, | |
53 | wxWindowID id, | |
54 | const wxPoint& pos, | |
55 | const wxSize& size, | |
56 | int n, const wxString choices[], | |
57 | long style, | |
58 | const wxValidator& validator, | |
59 | const wxString& name) | |
2bda0e17 | 60 | { |
8d99be5f VZ |
61 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
62 | return FALSE; | |
29006414 | 63 | |
d50c81d3 | 64 | long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL; |
8d99be5f VZ |
65 | if ( style & wxCB_SORT ) |
66 | msStyle |= CBS_SORT; | |
2bda0e17 | 67 | |
8d99be5f VZ |
68 | // the experience shows that wxChoice vs. wxComboBox distinction confuses |
69 | // quite a few people - try to help them | |
70 | wxASSERT_MSG( !(style & wxCB_DROPDOWN) && | |
71 | !(style & wxCB_READONLY) && | |
72 | !(style & wxCB_SIMPLE), | |
223d09f6 | 73 | wxT("this style flag is ignored by wxChoice, you " |
8d99be5f | 74 | "probably want to use a wxComboBox") ); |
2bda0e17 | 75 | |
223d09f6 | 76 | if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle) ) |
8d99be5f | 77 | return FALSE; |
2bda0e17 | 78 | |
c92d798f JS |
79 | // A choice/combobox normally has a white background (or other, depending |
80 | // on global settings) rather than inheriting the parent's background colour. | |
81 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW)); | |
82 | ||
8d99be5f VZ |
83 | for ( int i = 0; i < n; i++ ) |
84 | { | |
85 | Append(choices[i]); | |
86 | } | |
2bda0e17 | 87 | |
8d99be5f | 88 | SetSize(pos.x, pos.y, size.x, size.y); |
2bda0e17 | 89 | |
8d99be5f | 90 | return TRUE; |
2bda0e17 KB |
91 | } |
92 | ||
8ee9d618 VZ |
93 | wxChoice::~wxChoice() |
94 | { | |
95 | Free(); | |
96 | } | |
97 | ||
8d99be5f VZ |
98 | // ---------------------------------------------------------------------------- |
99 | // adding/deleting items to/from the list | |
100 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 101 | |
def6fb9b | 102 | int wxChoice::DoAppend(const wxString& item) |
8d99be5f | 103 | { |
def6fb9b VZ |
104 | int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str()); |
105 | if ( n == CB_ERR ) | |
106 | { | |
107 | wxLogLastError("SendMessage(CB_ADDSTRING)"); | |
108 | } | |
109 | ||
110 | return n; | |
2bda0e17 KB |
111 | } |
112 | ||
debe6624 | 113 | void wxChoice::Delete(int n) |
2bda0e17 | 114 | { |
223d09f6 | 115 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); |
8d99be5f | 116 | |
6c8a980f VZ |
117 | if ( HasClientObjectData() ) |
118 | { | |
119 | delete GetClientObject(n); | |
120 | } | |
121 | ||
8d99be5f | 122 | SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); |
2bda0e17 KB |
123 | } |
124 | ||
8d99be5f | 125 | void wxChoice::Clear() |
8ee9d618 | 126 | { |
92d2389e | 127 | Free(); |
8ee9d618 VZ |
128 | |
129 | SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0); | |
130 | } | |
131 | ||
132 | void wxChoice::Free() | |
2bda0e17 | 133 | { |
6c8a980f VZ |
134 | if ( HasClientObjectData() ) |
135 | { | |
136 | size_t count = GetCount(); | |
137 | for ( size_t n = 0; n < count; n++ ) | |
138 | { | |
139 | delete GetClientObject(n); | |
140 | } | |
141 | } | |
2bda0e17 KB |
142 | } |
143 | ||
8d99be5f VZ |
144 | // ---------------------------------------------------------------------------- |
145 | // selection | |
146 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 147 | |
8d99be5f | 148 | int wxChoice::GetSelection() const |
2bda0e17 | 149 | { |
8d99be5f | 150 | return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0); |
2bda0e17 KB |
151 | } |
152 | ||
debe6624 | 153 | void wxChoice::SetSelection(int n) |
2bda0e17 | 154 | { |
8d99be5f VZ |
155 | SendMessage(GetHwnd(), CB_SETCURSEL, n, 0); |
156 | } | |
157 | ||
158 | // ---------------------------------------------------------------------------- | |
159 | // string list functions | |
160 | // ---------------------------------------------------------------------------- | |
161 | ||
162 | int wxChoice::GetCount() const | |
163 | { | |
164 | return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0); | |
2bda0e17 KB |
165 | } |
166 | ||
167 | int wxChoice::FindString(const wxString& s) const | |
168 | { | |
169 | #if defined(__WATCOMC__) && defined(__WIN386__) | |
8d99be5f VZ |
170 | // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message. |
171 | // wxChoice::Do it the long way instead. | |
172 | int count = GetCount(); | |
173 | for ( int i = 0; i < count; i++ ) | |
174 | { | |
175 | // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too | |
176 | if ( GetString(i).IsSameAs(s, FALSE) ) | |
177 | return i; | |
178 | } | |
179 | ||
180 | return wxNOT_FOUND; | |
181 | #else // !Watcom | |
182 | int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, | |
183 | (WPARAM)-1, (LPARAM)s.c_str()); | |
184 | ||
185 | return pos == LB_ERR ? wxNOT_FOUND : pos; | |
186 | #endif // Watcom/!Watcom | |
2bda0e17 KB |
187 | } |
188 | ||
6c8a980f VZ |
189 | void wxChoice::SetString(int n, const wxString& s) |
190 | { | |
191 | wxFAIL_MSG(wxT("not implemented")); | |
192 | ||
193 | #if 0 // should do this, but no Insert() so far | |
194 | Delete(n); | |
195 | Insert(n + 1, s); | |
196 | #endif | |
197 | } | |
198 | ||
debe6624 | 199 | wxString wxChoice::GetString(int n) const |
2bda0e17 | 200 | { |
4438caf4 | 201 | size_t len = (size_t)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0); |
6c8a980f | 202 | wxString str; |
21d72d17 RD |
203 | if (len) { |
204 | if ( ::SendMessage(GetHwnd(), CB_GETLBTEXT, n, | |
205 | (LPARAM)str.GetWriteBuf(len)) == CB_ERR ) { | |
206 | wxLogLastError("SendMessage(CB_GETLBTEXT)"); | |
207 | } | |
208 | str.UngetWriteBuf(); | |
4438caf4 | 209 | } |
2bda0e17 | 210 | |
4438caf4 VZ |
211 | return str; |
212 | } | |
2bda0e17 | 213 | |
8d99be5f VZ |
214 | // ---------------------------------------------------------------------------- |
215 | // client data | |
216 | // ---------------------------------------------------------------------------- | |
217 | ||
6c8a980f | 218 | void wxChoice::DoSetItemClientData( int n, void* clientData ) |
8d99be5f VZ |
219 | { |
220 | if ( SendMessage(GetHwnd(), CB_SETITEMDATA, n, (LPARAM)clientData) == CB_ERR ) | |
221 | { | |
223d09f6 | 222 | wxLogLastError(wxT("CB_SETITEMDATA")); |
8d99be5f VZ |
223 | } |
224 | } | |
225 | ||
6c8a980f | 226 | void* wxChoice::DoGetItemClientData( int n ) const |
8d99be5f VZ |
227 | { |
228 | LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0); | |
229 | if ( rc == CB_ERR ) | |
230 | { | |
223d09f6 | 231 | wxLogLastError(wxT("CB_GETITEMDATA")); |
8d99be5f VZ |
232 | |
233 | // unfortunately, there is no way to return an error code to the user | |
8ee9d618 | 234 | rc = (LPARAM) NULL; |
8d99be5f VZ |
235 | } |
236 | ||
237 | return (void *)rc; | |
238 | } | |
239 | ||
6c8a980f | 240 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) |
8d99be5f | 241 | { |
6c8a980f | 242 | DoSetItemClientData(n, clientData); |
8d99be5f VZ |
243 | } |
244 | ||
6c8a980f | 245 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const |
8d99be5f | 246 | { |
6c8a980f | 247 | return (wxClientData *)DoGetItemClientData(n); |
8d99be5f VZ |
248 | } |
249 | ||
250 | // ---------------------------------------------------------------------------- | |
251 | // wxMSW specific helpers | |
252 | // ---------------------------------------------------------------------------- | |
253 | ||
4438caf4 VZ |
254 | void wxChoice::DoSetSize(int x, int y, |
255 | int width, int height, | |
256 | int sizeFlags) | |
257 | { | |
258 | // Ignore height parameter because height doesn't mean 'initially | |
259 | // displayed' height, it refers to the drop-down menu as well. The | |
260 | // wxWindows interpretation is different; also, getting the size returns | |
261 | // the _displayed_ size (NOT the drop down menu size) so | |
262 | // setting-getting-setting size would not work. | |
263 | wxControl::DoSetSize(x, y, width, -1, sizeFlags); | |
264 | } | |
2bda0e17 | 265 | |
4438caf4 VZ |
266 | wxSize wxChoice::DoGetBestSize() |
267 | { | |
268 | // find the widest string | |
269 | int wLine; | |
270 | int wChoice = 0; | |
8d99be5f VZ |
271 | int nItems = GetCount(); |
272 | for ( int i = 0; i < nItems; i++ ) | |
2bda0e17 | 273 | { |
2bda0e17 | 274 | wxString str(GetString(i)); |
4438caf4 VZ |
275 | GetTextExtent(str, &wLine, NULL); |
276 | if ( wLine > wChoice ) | |
277 | wChoice = wLine; | |
2bda0e17 | 278 | } |
fd3f686c | 279 | |
4438caf4 VZ |
280 | // give it some reasonable default value if there are no strings in the |
281 | // list | |
282 | if ( wChoice == 0 ) | |
283 | wChoice = 100; | |
2bda0e17 | 284 | |
4438caf4 VZ |
285 | // the combobox should be larger than the widest string |
286 | int cx, cy; | |
287 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
2bda0e17 | 288 | |
4438caf4 | 289 | wChoice += 5*cx; |
2bda0e17 | 290 | |
4438caf4 | 291 | // Choice drop-down list depends on number of items (limited to 10) |
8d99be5f | 292 | size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1; |
4438caf4 | 293 | int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings; |
2bda0e17 | 294 | |
4438caf4 | 295 | return wxSize(wChoice, hChoice); |
2bda0e17 KB |
296 | } |
297 | ||
2bda0e17 KB |
298 | long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
299 | { | |
8d99be5f | 300 | if ( nMsg == WM_LBUTTONUP ) |
2bda0e17 | 301 | { |
2bda0e17 KB |
302 | int x = (int)LOWORD(lParam); |
303 | int y = (int)HIWORD(lParam); | |
304 | ||
8d99be5f VZ |
305 | // Ok, this is truly weird, but if a panel with a wxChoice loses the |
306 | // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and | |
307 | // y = 65535. Filter out this nonsense. | |
308 | // | |
309 | // VZ: I'd like to know how to reproduce this please... | |
310 | if ( x == 65535 && y == 65535 ) | |
311 | return 0; | |
2bda0e17 KB |
312 | } |
313 | ||
8d99be5f | 314 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); |
2bda0e17 KB |
315 | } |
316 | ||
8d99be5f | 317 | bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) |
2bda0e17 | 318 | { |
8d99be5f | 319 | if ( param != CBN_SELCHANGE) |
2bda0e17 | 320 | { |
8d99be5f VZ |
321 | // "selection changed" is the only event we're after |
322 | return FALSE; | |
2bda0e17 | 323 | } |
2bda0e17 | 324 | |
2b273975 | 325 | int n = GetSelection(); |
8c1c5302 JS |
326 | if (n > -1) |
327 | { | |
328 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); | |
329 | event.SetInt(n); | |
330 | event.SetEventObject(this); | |
331 | event.SetString(GetStringSelection()); | |
332 | if ( HasClientObjectData() ) | |
333 | event.SetClientObject( GetClientObject(n) ); | |
334 | else if ( HasClientUntypedData() ) | |
335 | event.SetClientData( GetClientData(n) ); | |
336 | ProcessCommand(event); | |
337 | } | |
2bda0e17 | 338 | |
8d99be5f VZ |
339 | return TRUE; |
340 | } | |
2bda0e17 | 341 |