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