]> git.saurik.com Git - wxWidgets.git/blob - src/msw/choice.cpp
Moved wxToolBarSimple to deprecated library
[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 IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
44
45 /*
46 TODO PROPERTIES
47 selection (long)
48 content (list)
49 item
50 */
51
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)
68 {
69 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
70 return FALSE;
71
72 long msStyle = WS_CHILD | CBS_DROPDOWNLIST | WS_TABSTOP | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL /* | WS_CLIPSIBLINGS */;
73 if ( style & wxCB_SORT )
74 msStyle |= CBS_SORT;
75
76 if ( style & wxCLIP_SIBLINGS )
77 msStyle |= WS_CLIPSIBLINGS;
78
79
80 // Experience shows that wxChoice vs. wxComboBox distinction confuses
81 // quite a few people - try to help them
82 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
83 !(style & wxCB_READONLY) &&
84 !(style & wxCB_SIMPLE),
85 _T("this style flag is ignored by wxChoice, you ")
86 _T("probably want to use a wxComboBox") );
87
88 if ( !MSWCreateControl(wxT("COMBOBOX"), msStyle) )
89 return FALSE;
90
91 // A choice/combobox normally has a white background (or other, depending
92 // on global settings) rather than inheriting the parent's background colour.
93 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
94
95 for ( int i = 0; i < n; i++ )
96 {
97 Append(choices[i]);
98 }
99
100 SetSize(pos.x, pos.y, size.x, size.y);
101
102 return TRUE;
103 }
104
105 wxChoice::~wxChoice()
106 {
107 Free();
108 }
109
110 // ----------------------------------------------------------------------------
111 // adding/deleting items to/from the list
112 // ----------------------------------------------------------------------------
113
114 int wxChoice::DoAppend(const wxString& item)
115 {
116 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LONG)item.c_str());
117 if ( n == CB_ERR )
118 {
119 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
120 }
121
122 return n;
123 }
124
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
139 void wxChoice::Delete(int n)
140 {
141 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
142
143 if ( HasClientObjectData() )
144 {
145 delete GetClientObject(n);
146 }
147
148 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
149 }
150
151 void wxChoice::Clear()
152 {
153 Free();
154
155 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
156 }
157
158 void wxChoice::Free()
159 {
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 }
168 }
169
170 // ----------------------------------------------------------------------------
171 // selection
172 // ----------------------------------------------------------------------------
173
174 int wxChoice::GetSelection() const
175 {
176 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
177 }
178
179 void wxChoice::SetSelection(int n)
180 {
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);
191 }
192
193 int wxChoice::FindString(const wxString& s) const
194 {
195 #if defined(__WATCOMC__) && defined(__WIN386__)
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
213 }
214
215 void wxChoice::SetString(int n, const wxString& s)
216 {
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
242 }
243
244 wxString wxChoice::GetString(int n) const
245 {
246 int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
247
248 wxString str;
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 {
259 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
260 }
261 }
262
263 return str;
264 }
265
266 // ----------------------------------------------------------------------------
267 // client data
268 // ----------------------------------------------------------------------------
269
270 void wxChoice::DoSetItemClientData( int n, void* clientData )
271 {
272 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA,
273 n, (LPARAM)clientData) == CB_ERR )
274 {
275 wxLogLastError(wxT("CB_SETITEMDATA"));
276 }
277 }
278
279 void* wxChoice::DoGetItemClientData( int n ) const
280 {
281 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
282 if ( rc == CB_ERR )
283 {
284 wxLogLastError(wxT("CB_GETITEMDATA"));
285
286 // unfortunately, there is no way to return an error code to the user
287 rc = (LPARAM) NULL;
288 }
289
290 return (void *)rc;
291 }
292
293 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
294 {
295 DoSetItemClientData(n, clientData);
296 }
297
298 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
299 {
300 return (wxClientData *)DoGetItemClientData(n);
301 }
302
303 // ----------------------------------------------------------------------------
304 // wxMSW specific helpers
305 // ----------------------------------------------------------------------------
306
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
325 void wxChoice::DoSetSize(int x, int y,
326 int width, int WXUNUSED(height),
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.
334
335 wxControl::DoSetSize(x, y, width, -1, sizeFlags);
336 }
337
338 wxSize wxChoice::DoGetBestSize() const
339 {
340 // find the widest string
341 int wLine;
342 int wChoice = 0;
343 int nItems = GetCount();
344 for ( int i = 0; i < nItems; i++ )
345 {
346 wxString str(GetString(i));
347 GetTextExtent(str, &wLine, NULL);
348 if ( wLine > wChoice )
349 wChoice = wLine;
350 }
351
352 // give it some reasonable default value if there are no strings in the
353 // list
354 if ( wChoice == 0 )
355 wChoice = 100;
356
357 // the combobox should be larger than the widest string
358 int cx, cy;
359 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
360
361 wChoice += 5*cx;
362
363 // Choice drop-down list depends on number of items (limited to 10)
364 size_t nStrings = nItems == 0 ? 10 : wxMin(10, nItems) + 1;
365 int hChoice = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*nStrings;
366
367 return wxSize(wChoice, hChoice);
368 }
369
370 long wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
371 {
372 if ( nMsg == WM_LBUTTONUP )
373 {
374 int x = (int)LOWORD(lParam);
375 int y = (int)HIWORD(lParam);
376
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;
384 }
385
386 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
387 }
388
389 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
390 {
391 if ( param != CBN_SELCHANGE)
392 {
393 // "selection changed" is the only event we're after
394 return FALSE;
395 }
396
397 int n = GetSelection();
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 }
410
411 return TRUE;
412 }
413
414 WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
415 WXUINT WXUNUSED(message),
416 WXWPARAM WXUNUSED(wParam),
417 WXLPARAM WXUNUSED(lParam)
418 )
419 {
420 HDC hdc = (HDC)pDC;
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