]> git.saurik.com Git - wxWidgets.git/blob - src/msw/choice.cpp
wxCocoa: Added preliminary Blit support
[wxWidgets.git] / src / msw / choice.cpp
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 #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 int wxChoice::DoInsert(const wxString& item, int pos)
119 {
120 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
121 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
122
123 int n = (int)SendMessage(GetHwnd(), CB_INSERTSTRING, pos, (LONG)item.c_str());
124 if ( n == CB_ERR )
125 {
126 wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)"));
127 }
128
129 return n;
130 }
131
132 void wxChoice::Delete(int n)
133 {
134 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
135
136 if ( HasClientObjectData() )
137 {
138 delete GetClientObject(n);
139 }
140
141 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
142 }
143
144 void wxChoice::Clear()
145 {
146 Free();
147
148 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
149 }
150
151 void wxChoice::Free()
152 {
153 if ( HasClientObjectData() )
154 {
155 size_t count = GetCount();
156 for ( size_t n = 0; n < count; n++ )
157 {
158 delete GetClientObject(n);
159 }
160 }
161 }
162
163 // ----------------------------------------------------------------------------
164 // selection
165 // ----------------------------------------------------------------------------
166
167 int wxChoice::GetSelection() const
168 {
169 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
170 }
171
172 void wxChoice::SetSelection(int n)
173 {
174 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
175 }
176
177 // ----------------------------------------------------------------------------
178 // string list functions
179 // ----------------------------------------------------------------------------
180
181 int wxChoice::GetCount() const
182 {
183 return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
184 }
185
186 int wxChoice::FindString(const wxString& s) const
187 {
188 #if defined(__WATCOMC__) && defined(__WIN386__)
189 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
190 // wxChoice::Do it the long way instead.
191 int count = GetCount();
192 for ( int i = 0; i < count; i++ )
193 {
194 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
195 if ( GetString(i).IsSameAs(s, FALSE) )
196 return i;
197 }
198
199 return wxNOT_FOUND;
200 #else // !Watcom
201 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
202 (WPARAM)-1, (LPARAM)s.c_str());
203
204 return pos == LB_ERR ? wxNOT_FOUND : pos;
205 #endif // Watcom/!Watcom
206 }
207
208 void wxChoice::SetString(int n, const wxString& s)
209 {
210 wxCHECK_RET( n >= 0 && n < GetCount(),
211 wxT("invalid item index in wxChoice::SetString") );
212
213 // we have to delete and add back the string as there is no way to change a
214 // string in place
215
216 // we need to preserve the client data
217 void *data;
218 if ( m_clientDataItemsType != wxClientData_None )
219 {
220 data = DoGetItemClientData(n);
221 }
222 else // no client data
223 {
224 data = NULL;
225 }
226
227 ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
228 ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.c_str() );
229
230 if ( data )
231 {
232 DoSetItemClientData(n, data);
233 }
234 //else: it's already NULL by default
235 }
236
237 wxString wxChoice::GetString(int n) const
238 {
239 int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
240
241 wxString str;
242 if ( len != CB_ERR && len > 0 )
243 {
244 if ( ::SendMessage
245 (
246 GetHwnd(),
247 CB_GETLBTEXT,
248 n,
249 (LPARAM)(wxChar *)wxStringBuffer(str, len)
250 ) == CB_ERR )
251 {
252 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
253 }
254 }
255
256 return str;
257 }
258
259 // ----------------------------------------------------------------------------
260 // client data
261 // ----------------------------------------------------------------------------
262
263 void wxChoice::DoSetItemClientData( int n, void* clientData )
264 {
265 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA,
266 n, (LPARAM)clientData) == CB_ERR )
267 {
268 wxLogLastError(wxT("CB_SETITEMDATA"));
269 }
270 }
271
272 void* wxChoice::DoGetItemClientData( int n ) const
273 {
274 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
275 if ( rc == CB_ERR )
276 {
277 wxLogLastError(wxT("CB_GETITEMDATA"));
278
279 // unfortunately, there is no way to return an error code to the user
280 rc = (LPARAM) NULL;
281 }
282
283 return (void *)rc;
284 }
285
286 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
287 {
288 DoSetItemClientData(n, clientData);
289 }
290
291 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
292 {
293 return (wxClientData *)DoGetItemClientData(n);
294 }
295
296 // ----------------------------------------------------------------------------
297 // wxMSW specific helpers
298 // ----------------------------------------------------------------------------
299
300 void wxChoice::DoMoveWindow(int x, int y, int width, int height)
301 {
302 // here is why this is necessary: if the width is negative, the combobox
303 // window proc makes the window of the size width*height instead of
304 // interpreting height in the usual manner (meaning the height of the drop
305 // down list - usually the height specified in the call to MoveWindow()
306 // will not change the height of combo box per se)
307 //
308 // this behaviour is not documented anywhere, but this is just how it is
309 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
310 // the check, constraints/sizers using combos may break the height
311 // constraint will have not at all the same value as expected
312 if ( width < 0 )
313 return;
314
315 wxControl::DoMoveWindow(x, y, width, height);
316 }
317
318 void wxChoice::DoSetSize(int x, int y,
319 int width, int WXUNUSED(height),
320 int sizeFlags)
321 {
322 // Ignore height parameter because height doesn't mean 'initially
323 // displayed' height, it refers to the drop-down menu as well. The
324 // wxWindows interpretation is different; also, getting the size returns
325 // the _displayed_ size (NOT the drop down menu size) so
326 // setting-getting-setting size would not work.
327
328 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
329 }
330
331 wxSize wxChoice::DoGetBestSize() const
332 {
333 // find the widest string
334 int wLine;
335 int wChoice = 0;
336 int nItems = GetCount();
337 for ( int i = 0; i < nItems; i++ )
338 {
339 wxString str(GetString(i));
340 GetTextExtent(str, &wLine, NULL);
341 if ( wLine > wChoice )
342 wChoice = wLine;
343 }
344
345 // give it some reasonable default value if there are no strings in the
346 // list
347 if ( wChoice == 0 )
348 wChoice = 100;
349
350 // the combobox should be larger than the widest string
351 int cx, cy;
352 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
353
354 wChoice += 5*cx;
355
356 // Choice drop-down list depends on number of items (limited to 10)
357 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
358 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
359
360 return wxSize(wChoice, hChoice);
361 }
362
363 long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
364 {
365 if ( nMsg == WM_LBUTTONUP )
366 {
367 int x = (int)LOWORD(lParam);
368 int y = (int)HIWORD(lParam);
369
370 // Ok, this is truly weird, but if a panel with a wxChoice loses the
371 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
372 // y = 65535. Filter out this nonsense.
373 //
374 // VZ: I'd like to know how to reproduce this please...
375 if ( x == 65535 && y == 65535 )
376 return 0;
377 }
378
379 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
380 }
381
382 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
383 {
384 if ( param != CBN_SELCHANGE)
385 {
386 // "selection changed" is the only event we're after
387 return FALSE;
388 }
389
390 int n = GetSelection();
391 if (n > -1)
392 {
393 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
394 event.SetInt(n);
395 event.SetEventObject(this);
396 event.SetString(GetStringSelection());
397 if ( HasClientObjectData() )
398 event.SetClientObject( GetClientObject(n) );
399 else if ( HasClientUntypedData() )
400 event.SetClientData( GetClientData(n) );
401 ProcessCommand(event);
402 }
403
404 return TRUE;
405 }
406
407 WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
408 WXUINT WXUNUSED(message),
409 WXWPARAM WXUNUSED(wParam),
410 WXLPARAM WXUNUSED(lParam)
411 )
412 {
413 HDC hdc = (HDC)pDC;
414 wxColour colBack = GetBackgroundColour();
415
416 if (!IsEnabled())
417 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
418
419 ::SetBkColor(hdc, wxColourToRGB(colBack));
420 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
421
422 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
423
424 return (WXHBRUSH)brush->GetResourceHandle();
425 }
426
427 #endif // wxUSE_CHOICE