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