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