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