]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/listbox.cpp
expose find window method
[wxWidgets.git] / src / msw / listbox.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/listbox.cpp
3// Purpose: wxListBox
4// Author: Julian Smart
5// Modified by: Vadim Zeitlin (owner drawn stuff)
6// Created:
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "listbox.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#if wxUSE_LISTBOX
24
25#ifndef WX_PRECOMP
26#include "wx/listbox.h"
27#include "wx/settings.h"
28#include "wx/brush.h"
29#include "wx/font.h"
30#include "wx/dc.h"
31#include "wx/utils.h"
32#endif
33
34#include "wx/window.h"
35#include "wx/msw/private.h"
36
37#include <windowsx.h>
38
39#include "wx/dynarray.h"
40#include "wx/log.h"
41
42#if wxUSE_OWNER_DRAWN
43 #include "wx/ownerdrw.h"
44#endif
45
46#ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
48#endif
49
50#if wxUSE_EXTENDED_RTTI
51WX_DEFINE_FLAGS( wxListBoxStyle )
52
53wxBEGIN_FLAGS( wxListBoxStyle )
54 // new style border flags, we put them first to
55 // use them for streaming out
56 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
57 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
58 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
59 wxFLAGS_MEMBER(wxBORDER_RAISED)
60 wxFLAGS_MEMBER(wxBORDER_STATIC)
61 wxFLAGS_MEMBER(wxBORDER_NONE)
62
63 // old style border flags
64 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
65 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
66 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
67 wxFLAGS_MEMBER(wxRAISED_BORDER)
68 wxFLAGS_MEMBER(wxSTATIC_BORDER)
69 wxFLAGS_MEMBER(wxBORDER)
70
71 // standard window styles
72 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
73 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
74 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
75 wxFLAGS_MEMBER(wxWANTS_CHARS)
76 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
77 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
78 wxFLAGS_MEMBER(wxVSCROLL)
79 wxFLAGS_MEMBER(wxHSCROLL)
80
81 wxFLAGS_MEMBER(wxLB_SINGLE)
82 wxFLAGS_MEMBER(wxLB_MULTIPLE)
83 wxFLAGS_MEMBER(wxLB_EXTENDED)
84 wxFLAGS_MEMBER(wxLB_HSCROLL)
85 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
86 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
87 wxFLAGS_MEMBER(wxLB_SORT)
88
89wxEND_FLAGS( wxListBoxStyle )
90
91IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h")
92
93wxBEGIN_PROPERTIES_TABLE(wxListBox)
94 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent )
95 wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent )
96
97 wxPROPERTY( Font , wxFont , SetFont , GetFont , , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
98 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
99 wxPROPERTY( Selection ,int, SetSelection, GetSelection,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
100 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
101wxEND_PROPERTIES_TABLE()
102
103wxBEGIN_HANDLERS_TABLE(wxListBox)
104wxEND_HANDLERS_TABLE()
105
106wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
107#else
108IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
109#endif
110
111/*
112TODO PROPERTIES
113 selection
114 content
115 item
116*/
117
118// ============================================================================
119// list box item declaration and implementation
120// ============================================================================
121
122#if wxUSE_OWNER_DRAWN
123
124class wxListBoxItem : public wxOwnerDrawn
125{
126public:
127 wxListBoxItem(const wxString& str = wxEmptyString);
128};
129
130wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
131{
132 // no bitmaps/checkmarks
133 SetMarginWidth(0);
134}
135
136wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
137{
138 return new wxListBoxItem();
139}
140
141#endif //USE_OWNER_DRAWN
142
143// ============================================================================
144// list box control implementation
145// ============================================================================
146
147// ----------------------------------------------------------------------------
148// creation
149// ----------------------------------------------------------------------------
150
151// Listbox item
152wxListBox::wxListBox()
153{
154 m_noItems = 0;
155 m_selected = 0;
156}
157
158bool wxListBox::Create(wxWindow *parent,
159 wxWindowID id,
160 const wxPoint& pos,
161 const wxSize& size,
162 int n, const wxString choices[],
163 long style,
164 const wxValidator& wxVALIDATOR_PARAM(validator),
165 const wxString& name)
166{
167 m_noItems = 0;
168 m_selected = 0;
169
170 // initialize base class fields
171 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
172 return false;
173
174 // create the native control
175 if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) )
176 {
177 // control creation failed
178 return false;
179 }
180
181 // initialize the contents
182 for ( int i = 0; i < n; i++ )
183 {
184 Append(choices[i]);
185 }
186
187 return true;
188}
189
190bool wxListBox::Create(wxWindow *parent,
191 wxWindowID id,
192 const wxPoint& pos,
193 const wxSize& size,
194 const wxArrayString& choices,
195 long style,
196 const wxValidator& validator,
197 const wxString& name)
198{
199 wxCArrayString chs(choices);
200 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
201 style, validator, name);
202}
203
204wxListBox::~wxListBox()
205{
206 Free();
207}
208
209WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
210{
211 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
212
213 // always show the vertical scrollbar if necessary -- otherwise it is
214 // impossible to use the control with the mouse
215 msStyle |= WS_VSCROLL;
216
217 // we always want to get the notifications
218 msStyle |= LBS_NOTIFY;
219
220 // without this style, you get unexpected heights, so e.g. constraint
221 // layout doesn't work properly
222 msStyle |= LBS_NOINTEGRALHEIGHT;
223
224 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
225 _T("only one of listbox selection modes can be specified") );
226
227 if ( style & wxLB_MULTIPLE )
228 msStyle |= LBS_MULTIPLESEL;
229 else if ( style & wxLB_EXTENDED )
230 msStyle |= LBS_EXTENDEDSEL;
231
232 if ( m_windowStyle & wxLB_ALWAYS_SB )
233 msStyle |= LBS_DISABLENOSCROLL;
234 if ( m_windowStyle & wxLB_HSCROLL )
235 msStyle |= WS_HSCROLL;
236 if ( m_windowStyle & wxLB_SORT )
237 msStyle |= LBS_SORT;
238
239#if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
240 if ( m_windowStyle & wxLB_OWNERDRAW )
241 {
242 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
243 // the strings in the listbox for simplicity even though we could have
244 // avoided it in this case
245 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
246 }
247#endif // wxUSE_OWNER_DRAWN
248
249 return msStyle;
250}
251
252// ----------------------------------------------------------------------------
253// implementation of wxListBoxBase methods
254// ----------------------------------------------------------------------------
255
256void wxListBox::DoSetFirstItem(int N)
257{
258 wxCHECK_RET( N >= 0 && N < m_noItems,
259 wxT("invalid index in wxListBox::SetFirstItem") );
260
261 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
262}
263
264void wxListBox::Delete(int N)
265{
266 wxCHECK_RET( N >= 0 && N < m_noItems,
267 wxT("invalid index in wxListBox::Delete") );
268
269 // for owner drawn objects, the data is used for storing wxOwnerDrawn
270 // pointers and we shouldn't touch it
271#if !wxUSE_OWNER_DRAWN
272 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
273#endif // !wxUSE_OWNER_DRAWN
274 if ( HasClientObjectData() )
275 {
276 delete GetClientObject(N);
277 }
278
279 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
280 m_noItems--;
281
282 SetHorizontalExtent(wxEmptyString);
283}
284
285int wxListBox::DoAppend(const wxString& item)
286{
287 int index = ListBox_AddString(GetHwnd(), item);
288 m_noItems++;
289
290#if wxUSE_OWNER_DRAWN
291 if ( m_windowStyle & wxLB_OWNERDRAW ) {
292 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
293 pNewItem->SetName(item);
294 m_aItems.Insert(pNewItem, index);
295 ListBox_SetItemData(GetHwnd(), index, pNewItem);
296 pNewItem->SetFont(GetFont());
297 }
298#endif // wxUSE_OWNER_DRAWN
299
300 SetHorizontalExtent(item);
301
302 return index;
303}
304
305void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
306{
307 // avoid flicker - but don't need to do this for a hidden listbox
308 bool hideAndShow = IsShown();
309 if ( hideAndShow )
310 {
311 ShowWindow(GetHwnd(), SW_HIDE);
312 }
313
314 ListBox_ResetContent(GetHwnd());
315
316 m_noItems = choices.GetCount();
317 int i;
318 for (i = 0; i < m_noItems; i++)
319 {
320 ListBox_AddString(GetHwnd(), choices[i]);
321 if ( clientData )
322 {
323 SetClientData(i, clientData[i]);
324 }
325 }
326
327#if wxUSE_OWNER_DRAWN
328 if ( m_windowStyle & wxLB_OWNERDRAW ) {
329 // first delete old items
330 WX_CLEAR_ARRAY(m_aItems);
331
332 // then create new ones
333 for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) {
334 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
335 pNewItem->SetName(choices[ui]);
336 m_aItems.Add(pNewItem);
337 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
338 }
339 }
340#endif // wxUSE_OWNER_DRAWN
341
342 SetHorizontalExtent();
343
344 if ( hideAndShow )
345 {
346 // show the listbox back if we hid it
347 ShowWindow(GetHwnd(), SW_SHOW);
348 }
349}
350
351int wxListBox::FindString(const wxString& s) const
352{
353 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
354 if (pos == LB_ERR)
355 return wxNOT_FOUND;
356 else
357 return pos;
358}
359
360void wxListBox::Clear()
361{
362 Free();
363
364 ListBox_ResetContent(GetHwnd());
365
366 m_noItems = 0;
367 SetHorizontalExtent();
368}
369
370void wxListBox::Free()
371{
372#if wxUSE_OWNER_DRAWN
373 if ( m_windowStyle & wxLB_OWNERDRAW )
374 {
375 WX_CLEAR_ARRAY(m_aItems);
376 }
377 else
378#endif // wxUSE_OWNER_DRAWN
379 if ( HasClientObjectData() )
380 {
381 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
382 {
383 delete GetClientObject(n);
384 }
385 }
386}
387
388void wxListBox::SetSelection(int N, bool select)
389{
390 wxCHECK_RET( N == wxNOT_FOUND ||
391 (N >= 0 && N < m_noItems),
392 wxT("invalid index in wxListBox::SetSelection") );
393
394 if ( HasMultipleSelection() )
395 {
396 SendMessage(GetHwnd(), LB_SETSEL, select, N);
397 }
398 else
399 {
400 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
401 }
402}
403
404bool wxListBox::IsSelected(int N) const
405{
406 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
407 wxT("invalid index in wxListBox::Selected") );
408
409 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
410}
411
412wxClientData* wxListBox::DoGetItemClientObject(int n) const
413{
414 return (wxClientData *)DoGetItemClientData(n);
415}
416
417void *wxListBox::DoGetItemClientData(int n) const
418{
419 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
420 wxT("invalid index in wxListBox::GetClientData") );
421
422 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
423}
424
425void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
426{
427 DoSetItemClientData(n, clientData);
428}
429
430void wxListBox::DoSetItemClientData(int n, void *clientData)
431{
432 wxCHECK_RET( n >= 0 && n < m_noItems,
433 wxT("invalid index in wxListBox::SetClientData") );
434
435#if wxUSE_OWNER_DRAWN
436 if ( m_windowStyle & wxLB_OWNERDRAW )
437 {
438 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
439 // in OnMeasure/OnDraw.
440 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
441 }
442#endif // wxUSE_OWNER_DRAWN
443
444 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
445 wxLogDebug(wxT("LB_SETITEMDATA failed"));
446}
447
448// Return number of selections and an array of selected integers
449int wxListBox::GetSelections(wxArrayInt& aSelections) const
450{
451 aSelections.Empty();
452
453 if ( HasMultipleSelection() )
454 {
455 int countSel = ListBox_GetSelCount(GetHwnd());
456 if ( countSel == LB_ERR )
457 {
458 wxLogDebug(_T("ListBox_GetSelCount failed"));
459 }
460 else if ( countSel != 0 )
461 {
462 int *selections = new int[countSel];
463
464 if ( ListBox_GetSelItems(GetHwnd(),
465 countSel, selections) == LB_ERR )
466 {
467 wxLogDebug(wxT("ListBox_GetSelItems failed"));
468 countSel = -1;
469 }
470 else
471 {
472 aSelections.Alloc(countSel);
473 for ( int n = 0; n < countSel; n++ )
474 aSelections.Add(selections[n]);
475 }
476
477 delete [] selections;
478 }
479
480 return countSel;
481 }
482 else // single-selection listbox
483 {
484 if (ListBox_GetCurSel(GetHwnd()) > -1)
485 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
486
487 return aSelections.Count();
488 }
489}
490
491// Get single selection, for single choice list items
492int wxListBox::GetSelection() const
493{
494 wxCHECK_MSG( !HasMultipleSelection(),
495 -1,
496 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
497
498 return ListBox_GetCurSel(GetHwnd());
499}
500
501// Find string for position
502wxString wxListBox::GetString(int N) const
503{
504 wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString,
505 wxT("invalid index in wxListBox::GetClientData") );
506
507 int len = ListBox_GetTextLen(GetHwnd(), N);
508
509 // +1 for terminating NUL
510 wxString result;
511 ListBox_GetText(GetHwnd(), N, wxStringBuffer(result, len + 1));
512
513 return result;
514}
515
516void
517wxListBox::DoInsertItems(const wxArrayString& items, int pos)
518{
519 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
520 wxT("invalid index in wxListBox::InsertItems") );
521
522 int nItems = items.GetCount();
523 for ( int i = 0; i < nItems; i++ )
524 {
525 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
526
527#if wxUSE_OWNER_DRAWN
528 if ( m_windowStyle & wxLB_OWNERDRAW )
529 {
530 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
531 pNewItem->SetName(items[i]);
532 pNewItem->SetFont(GetFont());
533 m_aItems.Insert(pNewItem, idx);
534
535 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
536 }
537#endif // wxUSE_OWNER_DRAWN
538 }
539
540 m_noItems += nItems;
541
542 SetHorizontalExtent();
543}
544
545void wxListBox::SetString(int N, const wxString& s)
546{
547 wxCHECK_RET( N >= 0 && N < m_noItems,
548 wxT("invalid index in wxListBox::SetString") );
549
550 // remember the state of the item
551 bool wasSelected = IsSelected(N);
552
553 void *oldData = NULL;
554 wxClientData *oldObjData = NULL;
555 if ( m_clientDataItemsType == wxClientData_Void )
556 oldData = GetClientData(N);
557 else if ( m_clientDataItemsType == wxClientData_Object )
558 oldObjData = GetClientObject(N);
559
560 // delete and recreate it
561 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
562
563 int newN = N;
564 if ( N == m_noItems - 1 )
565 newN = -1;
566
567 ListBox_InsertString(GetHwnd(), newN, s);
568
569 // restore the client data
570 if ( oldData )
571 SetClientData(N, oldData);
572 else if ( oldObjData )
573 SetClientObject(N, oldObjData);
574
575 // we may have lost the selection
576 if ( wasSelected )
577 Select(N);
578
579#if wxUSE_OWNER_DRAWN
580 if ( m_windowStyle & wxLB_OWNERDRAW )
581 {
582 // update item's text
583 m_aItems[N]->SetName(s);
584
585 // reassign the item's data
586 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
587 }
588#endif //USE_OWNER_DRAWN
589}
590
591int wxListBox::GetCount() const
592{
593 return m_noItems;
594}
595
596// ----------------------------------------------------------------------------
597// helpers
598// ----------------------------------------------------------------------------
599
600// Windows-specific code to set the horizontal extent of the listbox, if
601// necessary. If s is non-NULL, it's used to calculate the horizontal extent.
602// Otherwise, all strings are used.
603void wxListBox::SetHorizontalExtent(const wxString& s)
604{
605 // Only necessary if we want a horizontal scrollbar
606 if (!(m_windowStyle & wxHSCROLL))
607 return;
608 TEXTMETRIC lpTextMetric;
609
610 if ( !s.IsEmpty() )
611 {
612 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
613 HDC dc = GetWindowDC(GetHwnd());
614 HFONT oldFont = 0;
615 if (GetFont().Ok() && GetFont().GetResourceHandle())
616 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
617
618 GetTextMetrics(dc, &lpTextMetric);
619 SIZE extentXY;
620 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
621 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
622
623 if (oldFont)
624 ::SelectObject(dc, oldFont);
625
626 ReleaseDC(GetHwnd(), dc);
627 if (extentX > existingExtent)
628 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
629 }
630 else
631 {
632 int largestExtent = 0;
633 HDC dc = GetWindowDC(GetHwnd());
634 HFONT oldFont = 0;
635 if (GetFont().Ok() && GetFont().GetResourceHandle())
636 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
637
638 GetTextMetrics(dc, &lpTextMetric);
639
640 // FIXME: buffer overflow!!
641 wxChar buf[1024];
642 for (int i = 0; i < m_noItems; i++)
643 {
644 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf);
645 buf[len] = 0;
646 SIZE extentXY;
647 ::GetTextExtentPoint(dc, buf, len, &extentXY);
648 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
649 if (extentX > largestExtent)
650 largestExtent = extentX;
651 }
652 if (oldFont)
653 ::SelectObject(dc, oldFont);
654
655 ReleaseDC(GetHwnd(), dc);
656 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
657 }
658}
659
660wxSize wxListBox::DoGetBestSize() const
661{
662 // find the widest string
663 int wLine;
664 int wListbox = 0;
665 for ( int i = 0; i < m_noItems; i++ )
666 {
667 wxString str(GetString(i));
668 GetTextExtent(str, &wLine, NULL);
669 if ( wLine > wListbox )
670 wListbox = wLine;
671 }
672
673 // give it some reasonable default value if there are no strings in the
674 // list
675 if ( wListbox == 0 )
676 wListbox = 100;
677
678 // the listbox should be slightly larger than the widest string
679 int cx, cy;
680 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
681
682 wListbox += 3*cx;
683
684 // don't make the listbox too tall (limit height to 10 items) but don't
685 // make it too small neither
686 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
687 wxMin(wxMax(m_noItems, 3), 10);
688
689 return wxSize(wListbox, hListbox);
690}
691
692// ----------------------------------------------------------------------------
693// callbacks
694// ----------------------------------------------------------------------------
695
696bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
697{
698 wxEventType evtType;
699 if ( param == LBN_SELCHANGE )
700 {
701 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
702 }
703 else if ( param == LBN_DBLCLK )
704 {
705 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
706 }
707 else
708 {
709 // some event we're not interested in
710 return FALSE;
711 }
712
713 wxCommandEvent event(evtType, m_windowId);
714 event.SetEventObject( this );
715
716 // retrieve the affected item
717 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
718 if ( n != LB_ERR )
719 {
720 if ( HasClientObjectData() )
721 event.SetClientObject( GetClientObject(n) );
722 else if ( HasClientUntypedData() )
723 event.SetClientData( GetClientData(n) );
724
725 event.SetString( GetString(n) );
726 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : TRUE );
727 }
728
729 event.m_commandInt = n;
730
731 return GetEventHandler()->ProcessEvent(event);
732}
733
734// ----------------------------------------------------------------------------
735// wxCheckListBox support
736// ----------------------------------------------------------------------------
737
738#if wxUSE_OWNER_DRAWN
739
740// drawing
741// -------
742
743// space beneath/above each row in pixels
744// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
745#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
746
747// the height is the same for all items
748// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
749
750// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
751// message is not yet sent when we get here!
752bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
753{
754 // only owner-drawn control should receive this message
755 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
756
757 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
758
759#ifdef __WXWINCE__
760 HDC hdc = GetDC(NULL);
761#else
762 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
763#endif
764
765 wxDC dc;
766 dc.SetHDC((WXHDC)hdc);
767 dc.SetFont(wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT));
768
769 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
770 pStruct->itemWidth = dc.GetCharWidth();
771
772 dc.SetHDC(0);
773
774 DeleteDC(hdc);
775
776 return TRUE;
777}
778
779// forward the message to the appropriate item
780bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
781{
782 // only owner-drawn control should receive this message
783 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
784
785 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
786 UINT itemID = pStruct->itemID;
787
788 // the item may be -1 for an empty listbox
789 if ( itemID == (UINT)-1 )
790 return FALSE;
791
792 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
793
794 wxCHECK( data && (data != LB_ERR), FALSE );
795
796 wxListBoxItem *pItem = (wxListBoxItem *)data;
797
798 wxDCTemp dc((WXHDC)pStruct->hDC);
799 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
800 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
801
802 return pItem->OnDrawItem(dc, rect,
803 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
804 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
805}
806
807#endif // wxUSE_OWNER_DRAWN
808
809#endif // wxUSE_LISTBOX