]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/choice.cpp
Blind fix
[wxWidgets.git] / src / msw / choice.cpp
... / ...
CommitLineData
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
44WX_DEFINE_FLAGS( wxChoiceStyle )
45
46wxBEGIN_FLAGS( wxChoiceStyle )
47 // new style border flags, we put them first to
48 // use them for streaming out
49 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
50 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
51 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
52 wxFLAGS_MEMBER(wxBORDER_RAISED)
53 wxFLAGS_MEMBER(wxBORDER_STATIC)
54 wxFLAGS_MEMBER(wxBORDER_NONE)
55
56 // old style border flags
57 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
58 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
59 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
60 wxFLAGS_MEMBER(wxRAISED_BORDER)
61 wxFLAGS_MEMBER(wxSTATIC_BORDER)
62 wxFLAGS_MEMBER(wxBORDER)
63
64 // standard window styles
65 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
66 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
67 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
68 wxFLAGS_MEMBER(wxWANTS_CHARS)
69 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
70 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
71 wxFLAGS_MEMBER(wxVSCROLL)
72 wxFLAGS_MEMBER(wxHSCROLL)
73
74wxEND_FLAGS( wxChoiceStyle )
75
76IMPLEMENT_DYNAMIC_CLASS_XTI(wxChoice, wxControl,"wx/choice.h")
77
78wxBEGIN_PROPERTIES_TABLE(wxChoice)
79 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_CHOICE_SELECTED , wxCommandEvent )
80
81 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
82 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
83 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
84 wxPROPERTY_FLAGS( WindowStyle , wxChoiceStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
85wxEND_PROPERTIES_TABLE()
86
87wxBEGIN_HANDLERS_TABLE(wxChoice)
88wxEND_HANDLERS_TABLE()
89
90wxCONSTRUCTOR_4( wxChoice , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
91#else
92IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
93#endif
94/*
95 TODO PROPERTIES
96 selection (long)
97 content (list)
98 item
99*/
100
101// ============================================================================
102// implementation
103// ============================================================================
104
105// ----------------------------------------------------------------------------
106// creation
107// ----------------------------------------------------------------------------
108
109bool wxChoice::Create(wxWindow *parent,
110 wxWindowID id,
111 const wxPoint& pos,
112 const wxSize& size,
113 int n, const wxString choices[],
114 long style,
115 const wxValidator& validator,
116 const wxString& name)
117{
118 // Experience shows that wxChoice vs. wxComboBox distinction confuses
119 // quite a few people - try to help them
120 wxASSERT_MSG( !(style & wxCB_DROPDOWN) &&
121 !(style & wxCB_READONLY) &&
122 !(style & wxCB_SIMPLE),
123 _T("this style flag is ignored by wxChoice, you ")
124 _T("probably want to use a wxComboBox") );
125
126 return CreateAndInit(parent, id, pos, size, n, choices, style,
127 validator, name);
128}
129
130bool wxChoice::CreateAndInit(wxWindow *parent,
131 wxWindowID id,
132 const wxPoint& pos,
133 const wxSize& size,
134 int n, const wxString choices[],
135 long style,
136 const wxValidator& validator,
137 const wxString& name)
138{
139 // initialize wxControl
140 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
141 return FALSE;
142
143 // now create the real HWND
144 if ( !MSWCreateControl(wxT("COMBOBOX"), _T(""), pos, size) )
145 return FALSE;
146
147
148 // choice/combobox normally has "white" (depends on colour scheme, of
149 // course) background rather than inheriting the parent's background
150 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
151
152 // initialize the controls contents
153 for ( int i = 0; i < n; i++ )
154 {
155 Append(choices[i]);
156 }
157
158 // and now we may finally size the control properly (if needed)
159 SetBestSize(size);
160
161 return TRUE;
162}
163
164bool wxChoice::Create(wxWindow *parent,
165 wxWindowID id,
166 const wxPoint& pos,
167 const wxSize& size,
168 const wxArrayString& choices,
169 long style,
170 const wxValidator& validator,
171 const wxString& name)
172{
173 wxCArrayString chs(choices);
174 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
175 style, validator, name);
176}
177
178bool wxChoice::MSWShouldPreProcessMessage(WXMSG *pMsg)
179{
180 MSG *msg = (MSG *) pMsg;
181
182 // don't preprocess "ESC" if combobox is dropped down
183 if ( msg->message == WM_KEYDOWN && msg->wParam == VK_ESCAPE )
184 {
185 if (::SendMessage(GetHwndOf(this), CB_GETDROPPEDSTATE, 0, 0))
186 {
187 return false;
188 }
189 }
190
191 return wxControl::MSWShouldPreProcessMessage(pMsg);
192}
193
194WXDWORD wxChoice::MSWGetStyle(long style, WXDWORD *exstyle) const
195{
196 // we never have an external border
197 WXDWORD msStyle = wxControl::MSWGetStyle
198 (
199 (style & ~wxBORDER_MASK) | wxBORDER_NONE, exstyle
200 );
201
202 // WS_CLIPSIBLINGS is useful with wxChoice and doesn't seem to result in
203 // any problems
204 msStyle |= WS_CLIPSIBLINGS;
205
206 // wxChoice-specific styles
207 msStyle |= CBS_DROPDOWNLIST | WS_HSCROLL | WS_VSCROLL;
208 if ( style & wxCB_SORT )
209 msStyle |= CBS_SORT;
210
211 return msStyle;
212}
213
214wxChoice::~wxChoice()
215{
216 Free();
217}
218
219// ----------------------------------------------------------------------------
220// adding/deleting items to/from the list
221// ----------------------------------------------------------------------------
222
223int wxChoice::DoAppend(const wxString& item)
224{
225 int n = (int)SendMessage(GetHwnd(), CB_ADDSTRING, 0, (LPARAM)item.c_str());
226 if ( n == CB_ERR )
227 {
228 wxLogLastError(wxT("SendMessage(CB_ADDSTRING)"));
229 }
230 else // ok
231 {
232 // we need to refresh our size in order to have enough space for the
233 // newly added items
234 UpdateVisibleHeight();
235 }
236
237 return n;
238}
239
240int wxChoice::DoInsert(const wxString& item, int pos)
241{
242 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
243 wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index"));
244
245 int n = (int)SendMessage(GetHwnd(), CB_INSERTSTRING, pos, (LPARAM)item.c_str());
246 if ( n == CB_ERR )
247 {
248 wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)"));
249 }
250 else // ok
251 {
252 UpdateVisibleHeight();
253 }
254
255 return n;
256}
257
258void wxChoice::Delete(int n)
259{
260 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
261
262 if ( HasClientObjectData() )
263 {
264 delete GetClientObject(n);
265 }
266
267 SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
268
269 UpdateVisibleHeight();
270}
271
272void wxChoice::Clear()
273{
274 Free();
275
276 SendMessage(GetHwnd(), CB_RESETCONTENT, 0, 0);
277
278 UpdateVisibleHeight();
279}
280
281void wxChoice::Free()
282{
283 if ( HasClientObjectData() )
284 {
285 size_t count = GetCount();
286 for ( size_t n = 0; n < count; n++ )
287 {
288 delete GetClientObject(n);
289 }
290 }
291}
292
293// ----------------------------------------------------------------------------
294// selection
295// ----------------------------------------------------------------------------
296
297int wxChoice::GetSelection() const
298{
299 return (int)SendMessage(GetHwnd(), CB_GETCURSEL, 0, 0);
300}
301
302void wxChoice::SetSelection(int n)
303{
304 SendMessage(GetHwnd(), CB_SETCURSEL, n, 0);
305}
306
307// ----------------------------------------------------------------------------
308// string list functions
309// ----------------------------------------------------------------------------
310
311int wxChoice::GetCount() const
312{
313 return (int)SendMessage(GetHwnd(), CB_GETCOUNT, 0, 0);
314}
315
316int wxChoice::FindString(const wxString& s) const
317{
318#if defined(__WATCOMC__) && defined(__WIN386__)
319 // For some reason, Watcom in WIN386 mode crashes in the CB_FINDSTRINGEXACT message.
320 // wxChoice::Do it the long way instead.
321 int count = GetCount();
322 for ( int i = 0; i < count; i++ )
323 {
324 // as CB_FINDSTRINGEXACT is case insensitive, be case insensitive too
325 if ( GetString(i).IsSameAs(s, FALSE) )
326 return i;
327 }
328
329 return wxNOT_FOUND;
330#else // !Watcom
331 int pos = (int)SendMessage(GetHwnd(), CB_FINDSTRINGEXACT,
332 (WPARAM)-1, (LPARAM)s.c_str());
333
334 return pos == LB_ERR ? wxNOT_FOUND : pos;
335#endif // Watcom/!Watcom
336}
337
338void wxChoice::SetString(int n, const wxString& s)
339{
340 wxCHECK_RET( n >= 0 && n < GetCount(),
341 wxT("invalid item index in wxChoice::SetString") );
342
343 // we have to delete and add back the string as there is no way to change a
344 // string in place
345
346 // we need to preserve the client data
347 void *data;
348 if ( m_clientDataItemsType != wxClientData_None )
349 {
350 data = DoGetItemClientData(n);
351 }
352 else // no client data
353 {
354 data = NULL;
355 }
356
357 ::SendMessage(GetHwnd(), CB_DELETESTRING, n, 0);
358 ::SendMessage(GetHwnd(), CB_INSERTSTRING, n, (LPARAM)s.c_str() );
359
360 if ( data )
361 {
362 DoSetItemClientData(n, data);
363 }
364 //else: it's already NULL by default
365}
366
367wxString wxChoice::GetString(int n) const
368{
369 int len = (int)::SendMessage(GetHwnd(), CB_GETLBTEXTLEN, n, 0);
370
371 wxString str;
372 if ( len != CB_ERR && len > 0 )
373 {
374 if ( ::SendMessage
375 (
376 GetHwnd(),
377 CB_GETLBTEXT,
378 n,
379 (LPARAM)(wxChar *)wxStringBuffer(str, len)
380 ) == CB_ERR )
381 {
382 wxLogLastError(wxT("SendMessage(CB_GETLBTEXT)"));
383 }
384 }
385
386 return str;
387}
388
389// ----------------------------------------------------------------------------
390// client data
391// ----------------------------------------------------------------------------
392
393void wxChoice::DoSetItemClientData( int n, void* clientData )
394{
395 if ( ::SendMessage(GetHwnd(), CB_SETITEMDATA,
396 n, (LPARAM)clientData) == CB_ERR )
397 {
398 wxLogLastError(wxT("CB_SETITEMDATA"));
399 }
400}
401
402void* wxChoice::DoGetItemClientData( int n ) const
403{
404 LPARAM rc = SendMessage(GetHwnd(), CB_GETITEMDATA, n, 0);
405 if ( rc == CB_ERR )
406 {
407 wxLogLastError(wxT("CB_GETITEMDATA"));
408
409 // unfortunately, there is no way to return an error code to the user
410 rc = (LPARAM) NULL;
411 }
412
413 return (void *)rc;
414}
415
416void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
417{
418 DoSetItemClientData(n, clientData);
419}
420
421wxClientData* wxChoice::DoGetItemClientObject( int n ) const
422{
423 return (wxClientData *)DoGetItemClientData(n);
424}
425
426// ----------------------------------------------------------------------------
427// wxMSW specific helpers
428// ----------------------------------------------------------------------------
429
430void wxChoice::UpdateVisibleHeight()
431{
432 // be careful to not change the width here
433 DoSetSize(-1, -1, -1, GetSize().y, wxSIZE_USE_EXISTING);
434}
435
436void wxChoice::DoMoveWindow(int x, int y, int width, int height)
437{
438 // here is why this is necessary: if the width is negative, the combobox
439 // window proc makes the window of the size width*height instead of
440 // interpreting height in the usual manner (meaning the height of the drop
441 // down list - usually the height specified in the call to MoveWindow()
442 // will not change the height of combo box per se)
443 //
444 // this behaviour is not documented anywhere, but this is just how it is
445 // here (NT 4.4) and, anyhow, the check shouldn't hurt - however without
446 // the check, constraints/sizers using combos may break the height
447 // constraint will have not at all the same value as expected
448 if ( width < 0 )
449 return;
450
451 wxControl::DoMoveWindow(x, y, width, height);
452}
453
454void wxChoice::DoGetSize(int *w, int *h) const
455{
456 // this is weird: sometimes, the height returned by Windows is clearly the
457 // total height of the control including the drop down list -- but only
458 // sometimes, and normally it isn't... I have no idea about what to do with
459 // this
460 wxControl::DoGetSize(w, h);
461}
462
463void wxChoice::DoSetSize(int x, int y,
464 int width, int height,
465 int sizeFlags)
466{
467 int heightOrig = height;
468
469 // the height which we must pass to Windows should be the total height of
470 // the control including the drop down list while the height given to us
471 // is, of course, just the height of the permanently visible part of it
472 if ( height != -1 )
473 {
474 // don't make the drop down list too tall, arbitrarily limit it to 40
475 // items max and also don't leave it empty
476 size_t nItems = GetCount();
477 if ( !nItems )
478 nItems = 9;
479 else if ( nItems > 39 )
480 nItems = 39;
481
482 // add space for the drop down list
483 const int hItem = SendMessage(GetHwnd(), CB_GETITEMHEIGHT, 0, 0);
484 height += hItem*(nItems + 1);
485 }
486
487 wxControl::DoSetSize(x, y, width, height, sizeFlags);
488
489 // if the height specified for the visible part of the control is
490 // different from the current one, we need to change it separately
491 // as it is not affected by normal WM_SETSIZE
492 if ( height != -1 )
493 {
494 const int delta = heightOrig - GetSize().y;
495 if ( delta )
496 {
497 int h = ::SendMessage(GetHwnd(), CB_GETITEMHEIGHT, (WPARAM)-1, 0);
498 SendMessage(GetHwnd(), CB_SETITEMHEIGHT, (WPARAM)-1, h + delta);
499 }
500 }
501}
502
503wxSize wxChoice::DoGetBestSize() const
504{
505 // find the widest string
506 int wChoice = 0;
507 const size_t nItems = GetCount();
508 for ( size_t i = 0; i < nItems; i++ )
509 {
510 int wLine;
511 GetTextExtent(GetString(i), &wLine, NULL);
512 if ( wLine > wChoice )
513 wChoice = wLine;
514 }
515
516 // give it some reasonable default value if there are no strings in the
517 // list
518 if ( wChoice == 0 )
519 wChoice = 100;
520
521 // the combobox should be slightly larger than the widest string
522 wChoice += 5*GetCharWidth();
523
524 return wxSize(wChoice, EDIT_HEIGHT_FROM_CHAR_HEIGHT(GetCharHeight()));
525}
526
527WXLRESULT wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
528{
529 if ( nMsg == WM_LBUTTONUP )
530 {
531 int x = (int)LOWORD(lParam);
532 int y = (int)HIWORD(lParam);
533
534 // Ok, this is truly weird, but if a panel with a wxChoice loses the
535 // focus, then you get a *fake* WM_LBUTTONUP message with x = 65535 and
536 // y = 65535. Filter out this nonsense.
537 //
538 // VZ: I'd like to know how to reproduce this please...
539 if ( x == 65535 && y == 65535 )
540 return 0;
541 }
542
543 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
544}
545
546bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
547{
548 if ( param != CBN_SELCHANGE)
549 {
550 // "selection changed" is the only event we're after
551 return FALSE;
552 }
553
554 int n = GetSelection();
555 if (n > -1)
556 {
557 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId);
558 event.SetInt(n);
559 event.SetEventObject(this);
560 event.SetString(GetStringSelection());
561 if ( HasClientObjectData() )
562 event.SetClientObject( GetClientObject(n) );
563 else if ( HasClientUntypedData() )
564 event.SetClientData( GetClientData(n) );
565 ProcessCommand(event);
566 }
567
568 return TRUE;
569}
570
571WXHBRUSH wxChoice::OnCtlColor(WXHDC pDC, WXHWND WXUNUSED(pWnd), WXUINT WXUNUSED(nCtlColor),
572 WXUINT WXUNUSED(message),
573 WXWPARAM WXUNUSED(wParam),
574 WXLPARAM WXUNUSED(lParam)
575 )
576{
577 HDC hdc = (HDC)pDC;
578 wxColour colBack = GetBackgroundColour();
579
580 if (!IsEnabled())
581 colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
582
583 ::SetBkColor(hdc, wxColourToRGB(colBack));
584 ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
585
586 wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
587
588 return (WXHBRUSH)brush->GetResourceHandle();
589}
590
591#endif // wxUSE_CHOICE