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