]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: choice.cpp | |
3 | // Purpose: wxChoice | |
4 | // Author: Julian Smart | |
5 | // Modified by: Vadim Zeitlin to derive from wxChoiceBase | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
21 | #pragma implementation "choice.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #if wxUSE_CHOICE | |
32 | ||
33 | #ifndef WX_PRECOMP | |
34 | #include "wx/choice.h" | |
35 | #include "wx/utils.h" | |
36 | #include "wx/log.h" | |
37 | #include "wx/brush.h" | |
38 | #include "wx/settings.h" | |
39 | #endif | |
40 | ||
41 | #include "wx/msw/private.h" | |
42 | ||
43 | #if wxUSE_EXTENDED_RTTI | |
44 | WX_DEFINE_FLAGS( wxChoiceStyle ) | |
45 | ||
46 | WX_BEGIN_FLAGS( wxChoiceStyle ) | |
47 | // new style border flags, we put them first to | |
48 | // use them for streaming out | |
49 | WX_FLAGS_MEMBER(wxBORDER_SIMPLE) | |
50 | WX_FLAGS_MEMBER(wxBORDER_SUNKEN) | |
51 | WX_FLAGS_MEMBER(wxBORDER_DOUBLE) | |
52 | WX_FLAGS_MEMBER(wxBORDER_RAISED) | |
53 | WX_FLAGS_MEMBER(wxBORDER_STATIC) | |
54 | WX_FLAGS_MEMBER(wxBORDER_NONE) | |
55 | ||
56 | // old style border flags | |
57 | WX_FLAGS_MEMBER(wxSIMPLE_BORDER) | |
58 | WX_FLAGS_MEMBER(wxSUNKEN_BORDER) | |
59 | WX_FLAGS_MEMBER(wxDOUBLE_BORDER) | |
60 | WX_FLAGS_MEMBER(wxRAISED_BORDER) | |
61 | WX_FLAGS_MEMBER(wxSTATIC_BORDER) | |
62 | WX_FLAGS_MEMBER(wxNO_BORDER) | |
63 | ||
64 | // standard window styles | |
65 | WX_FLAGS_MEMBER(wxTAB_TRAVERSAL) | |
66 | WX_FLAGS_MEMBER(wxCLIP_CHILDREN) | |
67 | WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
68 | WX_FLAGS_MEMBER(wxWANTS_CHARS) | |
69 | WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE) | |
70 | WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB ) | |
71 | WX_FLAGS_MEMBER(wxVSCROLL) | |
72 | WX_FLAGS_MEMBER(wxHSCROLL) | |
73 | ||
74 | WX_END_FLAGS( wxChoiceStyle ) | |
75 | ||
76 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxChoice, wxControl,"wx/choice.h") | |
77 | ||
78 | WX_BEGIN_PROPERTIES_TABLE(wxChoice) | |
79 | // TODO DELEGATES | |
80 | WX_PROPERTY( Font , wxFont , SetFont , GetFont , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
81 | WX_PROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
82 | WX_PROPERTY( Selection ,int, SetSelection, GetSelection, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) | |
83 | WX_PROPERTY_FLAGS( WindowStyle , wxChoiceStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style | |
84 | WX_END_PROPERTIES_TABLE() | |
85 | ||
86 | WX_BEGIN_HANDLERS_TABLE(wxChoice) | |
87 | WX_END_HANDLERS_TABLE() | |
88 | ||
89 | WX_CONSTRUCTOR_4( wxChoice , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size ) | |
90 | #else | |
91 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) | |
92 | #endif | |
93 | /* | |
94 | TODO PROPERTIES | |
95 | selection (long) | |
96 | content (list) | |
97 | item | |
98 | */ | |
99 | ||
100 | // ============================================================================ | |
101 | // implementation | |
102 | // ============================================================================ | |
103 | ||
104 | // ---------------------------------------------------------------------------- | |
105 | // creation | |
106 | // ---------------------------------------------------------------------------- | |
107 | ||
108 | bool wxChoice::Create(wxWindow *parent, | |
109 | wxWindowID id, | |
110 | const wxPoint& pos, | |
111 | const wxSize& size, | |
112 | int n, const wxString choices[], | |
113 | long style, | |
114 | const wxValidator& validator, | |
115 | const wxString& name) | |
116 | { | |
117 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) | |
118 | return FALSE; | |
119 | ||
120 | long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL /* | WS_CLIPSIBLINGS */; | |
121 | if ( style & wxCB_SORT ) | |
122 | msStyle |= CBS_SORT; | |
123 | ||
124 | if ( style & wxCLIP_SIBLINGS ) | |
125 | msStyle |= WS_CLIPSIBLINGS; | |
126 | ||
127 | ||
128 | // Experience shows that wxChoice vs. wxComboBox distinction confuses | |
129 | // quite a few people - try to help them | |
130 | wxASSERT_MSG( !(style & wxCB_DROPDOWN) && | |
131 | !(style & wxCB_READONLY) && | |
132 | !(style & wxCB_SIMPLE), | |
133 | _T("this style flag is ignored by wxChoice, you ") | |
134 | _T("probably want to use a wxComboBox") ); | |
135 | ||
136 | if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle) ) | |
137 | return FALSE; | |
138 | ||
139 | // A choice/combobox normally has a white background (or other, depending | |
140 | // on global settings) rather than inheriting the parent's background colour. | |
141 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
142 | ||
143 | for ( int i = 0; i < n; i++ ) | |
144 | { | |
145 | Append(choices[i]); | |
146 | } | |
147 | ||
148 | SetSize(pos.x, pos.y, size.x, size.y); | |
149 | ||
150 | return TRUE; | |
151 | } | |
152 | ||
153 | wxChoice::~wxChoice() | |
154 | { | |
155 | Free(); | |
156 | } | |
157 | ||
158 | // ---------------------------------------------------------------------------- | |
159 | // adding/deleting items to/from the list | |
160 | // ---------------------------------------------------------------------------- | |
161 | ||
162 | int wxChoice::DoAppend(const wxString& item) | |
163 | { | |
164 | int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str()); | |
165 | if ( n == CB_ERR ) | |
166 | { | |
167 | wxLogLastError(wxT("SendMessage(CB_ADDSTRING)")); | |
168 | } | |
169 | ||
170 | return n; | |
171 | } | |
172 | ||
173 | int wxChoice::DoInsert(const wxString& item, int pos) | |
174 | { | |
175 | wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); | |
176 | wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); | |
177 | ||
178 | int n = (int)SendMessage(GetHwnd(), CB_INSERTSTRING, pos, (LONG)item.c_str()); | |
179 | if ( n == CB_ERR ) | |
180 | { | |
181 | wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)")); | |
182 | } | |
183 | ||
184 | return n; | |
185 | } | |
186 | ||
187 | void wxChoice::Delete(int n) | |
188 | { | |
189 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); | |
190 | ||
191 | if ( HasClientObjectData() ) | |
192 | { | |
193 | delete GetClientObject(n); | |
194 | } | |
195 | ||
196 | SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); | |
197 | } | |
198 | ||
199 | void wxChoice::Clear() | |
200 | { | |
201 | Free(); | |
202 | ||
203 | SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0); | |
204 | } | |
205 | ||
206 | void wxChoice::Free() | |
207 | { | |
208 | if ( HasClientObjectData() ) | |
209 | { | |
210 | size_t count = GetCount(); | |
211 | for ( size_t n = 0; n < count; n++ ) | |
212 | { | |
213 | delete GetClientObject(n); | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
218 | // ---------------------------------------------------------------------------- | |
219 | // selection | |
220 | // ---------------------------------------------------------------------------- | |
221 | ||
222 | int wxChoice::GetSelection() const | |
223 | { | |
224 | return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0); | |
225 | } | |
226 | ||
227 | void wxChoice::SetSelection(int n) | |
228 | { | |
229 | SendMessage(GetHwnd(), CB_SETCURSEL, n, 0); | |
230 | } | |
231 | ||
232 | // ---------------------------------------------------------------------------- | |
233 | // string list functions | |
234 | // ---------------------------------------------------------------------------- | |
235 | ||
236 | int wxChoice::GetCount() const | |
237 | { | |
238 | return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0); | |
239 | } | |
240 | ||
241 | int wxChoice::FindString(const wxString& s) const | |
242 | { | |
243 | #if defined(__WATCOMC__) && defined(__WIN386__) | |
244 | // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message. | |
245 | // wxChoice::Do it the long way instead. | |
246 | int count = GetCount(); | |
247 | for ( int i = 0; i < count; i++ ) | |
248 | { | |
249 | // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too | |
250 | if ( GetString(i).IsSameAs(s, FALSE) ) | |
251 | return i; | |
252 | } | |
253 | ||
254 | return wxNOT_FOUND; | |
255 | #else // !Watcom | |
256 | int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT, | |
257 | (WPARAM)-1, (LPARAM)s.c_str()); | |
258 | ||
259 | return pos == LB_ERR ? wxNOT_FOUND : pos; | |
260 | #endif // Watcom/!Watcom | |
261 | } | |
262 | ||
263 | void wxChoice::SetString(int n, const wxString& s) | |
264 | { | |
265 | wxCHECK_RET( n >= 0 && n < GetCount(), | |
266 | wxT("invalid item index in wxChoice::SetString") ); | |
267 | ||
268 | // we have to delete and add back the string as there is no way to change a | |
269 | // string in place | |
270 | ||
271 | // we need to preserve the client data | |
272 | void *data; | |
273 | if ( m_clientDataItemsType != wxClientData_None ) | |
274 | { | |
275 | data = DoGetItemClientData(n); | |
276 | } | |
277 | else // no client data | |
278 | { | |
279 | data = NULL; | |
280 | } | |
281 | ||
282 | ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0); | |
283 | ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.c_str() ); | |
284 | ||
285 | if ( data ) | |
286 | { | |
287 | DoSetItemClientData(n, data); | |
288 | } | |
289 | //else: it's already NULL by default | |
290 | } | |
291 | ||
292 | wxString wxChoice::GetString(int n) const | |
293 | { | |
294 | int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0); | |
295 | ||
296 | wxString str; | |
297 | if ( len != CB_ERR && len > 0 ) | |
298 | { | |
299 | if ( ::SendMessage | |
300 | ( | |
301 | GetHwnd(), | |
302 | CB_GETLBTEXT, | |
303 | n, | |
304 | (LPARAM)(wxChar *)wxStringBuffer(str, len) | |
305 | ) == CB_ERR ) | |
306 | { | |
307 | wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)")); | |
308 | } | |
309 | } | |
310 | ||
311 | return str; | |
312 | } | |
313 | ||
314 | // ---------------------------------------------------------------------------- | |
315 | // client data | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
318 | void wxChoice::DoSetItemClientData( int n, void* clientData ) | |
319 | { | |
320 | if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA, | |
321 | n, (LPARAM)clientData) == CB_ERR ) | |
322 | { | |
323 | wxLogLastError(wxT("CB_SETITEMDATA")); | |
324 | } | |
325 | } | |
326 | ||
327 | void* wxChoice::DoGetItemClientData( int n ) const | |
328 | { | |
329 | LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0); | |
330 | if ( rc == CB_ERR ) | |
331 | { | |
332 | wxLogLastError(wxT("CB_GETITEMDATA")); | |
333 | ||
334 | // unfortunately, there is no way to return an error code to the user | |
335 | rc = (LPARAM) NULL; | |
336 | } | |
337 | ||
338 | return (void *)rc; | |
339 | } | |
340 | ||
341 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) | |
342 | { | |
343 | DoSetItemClientData(n, clientData); | |
344 | } | |
345 | ||
346 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const | |
347 | { | |
348 | return (wxClientData *)DoGetItemClientData(n); | |
349 | } | |
350 | ||
351 | // ---------------------------------------------------------------------------- | |
352 | // wxMSW specific helpers | |
353 | // ---------------------------------------------------------------------------- | |
354 | ||
355 | void wxChoice::DoMoveWindow(int x, int y, int width, int height) | |
356 | { | |
357 | // here is why this is necessary: if the width is negative, the combobox | |
358 | // window proc makes the window of the size width*height instead of | |
359 | // interpreting height in the usual manner (meaning the height of the drop | |
360 | // down list - usually the height specified in the call to MoveWindow() | |
361 | // will not change the height of combo box per se) | |
362 | // | |
363 | // this behaviour is not documented anywhere, but this is just how it is | |
364 | // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without | |
365 | // the check, constraints/sizers using combos may break the height | |
366 | // constraint will have not at all the same value as expected | |
367 | if ( width < 0 ) | |
368 | return; | |
369 | ||
370 | wxControl::DoMoveWindow(x, y, width, height); | |
371 | } | |
372 | ||
373 | void wxChoice::DoSetSize(int x, int y, | |
374 | int width, int WXUNUSED(height), | |
375 | int sizeFlags) | |
376 | { | |
377 | // Ignore height parameter because height doesn't mean 'initially | |
378 | // displayed' height, it refers to the drop-down menu as well. The | |
379 | // wxWindows interpretation is different; also, getting the size returns | |
380 | // the _displayed_ size (NOT the drop down menu size) so | |
381 | // setting-getting-setting size would not work. | |
382 | ||
383 | wxControl::DoSetSize(x, y, width, -1, sizeFlags); | |
384 | } | |
385 | ||
386 | wxSize wxChoice::DoGetBestSize() const | |
387 | { | |
388 | // find the widest string | |
389 | int wLine; | |
390 | int wChoice = 0; | |
391 | int nItems = GetCount(); | |
392 | for ( int i = 0; i < nItems; i++ ) | |
393 | { | |
394 | wxString str(GetString(i)); | |
395 | GetTextExtent(str, &wLine, NULL); | |
396 | if ( wLine > wChoice ) | |
397 | wChoice = wLine; | |
398 | } | |
399 | ||
400 | // give it some reasonable default value if there are no strings in the | |
401 | // list | |
402 | if ( wChoice == 0 ) | |
403 | wChoice = 100; | |
404 | ||
405 | // the combobox should be larger than the widest string | |
406 | int cx, cy; | |
407 | wxGetCharSize(GetHWND(), &cx, &cy, &GetFont()); | |
408 | ||
409 | wChoice += 5*cx; | |
410 | ||
411 | // Choice drop-down list depends on number of items (limited to 10) | |
412 | size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1; | |
413 | int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings; | |
414 | ||
415 | return wxSize(wChoice, hChoice); | |
416 | } | |
417 | ||
418 | long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
419 | { | |
420 | if ( nMsg == WM_LBUTTONUP ) | |
421 | { | |
422 | int x = (int)LOWORD(lParam); | |
423 | int y = (int)HIWORD(lParam); | |
424 | ||
425 | // Ok, this is truly weird, but if a panel with a wxChoice loses the | |
426 | // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and | |
427 | // y = 65535. Filter out this nonsense. | |
428 | // | |
429 | // VZ: I'd like to know how to reproduce this please... | |
430 | if ( x == 65535 && y == 65535 ) | |
431 | return 0; | |
432 | } | |
433 | ||
434 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); | |
435 | } | |
436 | ||
437 | bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id)) | |
438 | { | |
439 | if ( param != CBN_SELCHANGE) | |
440 | { | |
441 | // "selection changed" is the only event we're after | |
442 | return FALSE; | |
443 | } | |
444 | ||
445 | int n = GetSelection(); | |
446 | if (n > -1) | |
447 | { | |
448 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId); | |
449 | event.SetInt(n); | |
450 | event.SetEventObject(this); | |
451 | event.SetString(GetStringSelection()); | |
452 | if ( HasClientObjectData() ) | |
453 | event.SetClientObject( GetClientObject(n) ); | |
454 | else if ( HasClientUntypedData() ) | |
455 | event.SetClientData( GetClientData(n) ); | |
456 | ProcessCommand(event); | |
457 | } | |
458 | ||
459 | return TRUE; | |
460 | } | |
461 | ||
462 | WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor), | |
463 | WXUINT WXUNUSED(message), | |
464 | WXWPARAM WXUNUSED(wParam), | |
465 | WXLPARAM WXUNUSED(lParam) | |
466 | ) | |
467 | { | |
468 | HDC hdc = (HDC)pDC; | |
469 | wxColour colBack = GetBackgroundColour(); | |
470 | ||
471 | if (!IsEnabled()) | |
472 | colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
473 | ||
474 | ::SetBkColor(hdc, wxColourToRGB(colBack)); | |
475 | ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour())); | |
476 | ||
477 | wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID); | |
478 | ||
479 | return (WXHBRUSH)brush->GetResourceHandle(); | |
480 | } | |
481 | ||
482 | #endif // wxUSE_CHOICE |