]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listbox.cpp
implement HitTest(pos) variant and not HitTest(x, y) one as this is what Windows...
[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 #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
51 WX_DEFINE_FLAGS( wxListBoxStyle )
52
53 wxBEGIN_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
89 wxEND_FLAGS( wxListBoxStyle )
90
91 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h")
92
93 wxBEGIN_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 , EMPTY_MACROVALUE, 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, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
100 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
101 wxEND_PROPERTIES_TABLE()
102
103 wxBEGIN_HANDLERS_TABLE(wxListBox)
104 wxEND_HANDLERS_TABLE()
105
106 wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
107 #else
108 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
109 #endif
110
111 /*
112 TODO 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
124 class wxListBoxItem : public wxOwnerDrawn
125 {
126 public:
127 wxListBoxItem(const wxString& str = wxEmptyString);
128 };
129
130 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
131 {
132 // no bitmaps/checkmarks
133 SetMarginWidth(0);
134 }
135
136 wxOwnerDrawn *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
152 wxListBox::wxListBox()
153 {
154 m_noItems = 0;
155 m_selected = 0;
156 }
157
158 bool 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& 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 // now we can compute our best size correctly, so do it if necessary
188 SetBestSize(size);
189
190 return true;
191 }
192
193 bool wxListBox::Create(wxWindow *parent,
194 wxWindowID id,
195 const wxPoint& pos,
196 const wxSize& size,
197 const wxArrayString& choices,
198 long style,
199 const wxValidator& validator,
200 const wxString& name)
201 {
202 wxCArrayString chs(choices);
203 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
204 style, validator, name);
205 }
206
207 wxListBox::~wxListBox()
208 {
209 Free();
210 }
211
212 WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
213 {
214 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
215
216 // always show the vertical scrollbar if necessary -- otherwise it is
217 // impossible to use the control with the mouse
218 msStyle |= WS_VSCROLL;
219
220 // we always want to get the notifications
221 msStyle |= LBS_NOTIFY;
222
223 // without this style, you get unexpected heights, so e.g. constraint
224 // layout doesn't work properly
225 msStyle |= LBS_NOINTEGRALHEIGHT;
226
227 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
228 _T("only one of listbox selection modes can be specified") );
229
230 if ( style & wxLB_MULTIPLE )
231 msStyle |= LBS_MULTIPLESEL;
232 else if ( style & wxLB_EXTENDED )
233 msStyle |= LBS_EXTENDEDSEL;
234
235 if ( m_windowStyle & wxLB_ALWAYS_SB )
236 msStyle |= LBS_DISABLENOSCROLL;
237 if ( m_windowStyle & wxLB_HSCROLL )
238 msStyle |= WS_HSCROLL;
239 if ( m_windowStyle & wxLB_SORT )
240 msStyle |= LBS_SORT;
241
242 #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
243 if ( m_windowStyle & wxLB_OWNERDRAW )
244 {
245 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
246 // the strings in the listbox for simplicity even though we could have
247 // avoided it in this case
248 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
249 }
250 #endif // wxUSE_OWNER_DRAWN
251
252 return msStyle;
253 }
254
255 // ----------------------------------------------------------------------------
256 // implementation of wxListBoxBase methods
257 // ----------------------------------------------------------------------------
258
259 void wxListBox::DoSetFirstItem(int N)
260 {
261 wxCHECK_RET( N >= 0 && N < m_noItems,
262 wxT("invalid index in wxListBox::SetFirstItem") );
263
264 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
265 }
266
267 void wxListBox::Delete(int N)
268 {
269 wxCHECK_RET( N >= 0 && N < m_noItems,
270 wxT("invalid index in wxListBox::Delete") );
271
272 // for owner drawn objects, the data is used for storing wxOwnerDrawn
273 // pointers and we shouldn't touch it
274 #if !wxUSE_OWNER_DRAWN
275 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
276 #endif // !wxUSE_OWNER_DRAWN
277 if ( HasClientObjectData() )
278 {
279 delete GetClientObject(N);
280 }
281
282 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
283 m_noItems--;
284
285 SetHorizontalExtent(wxEmptyString);
286 }
287
288 int wxListBox::DoAppend(const wxString& item)
289 {
290 InvalidateBestSize();
291
292 int index = ListBox_AddString(GetHwnd(), item);
293 m_noItems++;
294
295 #if wxUSE_OWNER_DRAWN
296 if ( m_windowStyle & wxLB_OWNERDRAW ) {
297 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
298 pNewItem->SetName(item);
299 m_aItems.Insert(pNewItem, index);
300 ListBox_SetItemData(GetHwnd(), index, pNewItem);
301 pNewItem->SetFont(GetFont());
302 }
303 #endif // wxUSE_OWNER_DRAWN
304
305 SetHorizontalExtent(item);
306
307 return index;
308 }
309
310 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
311 {
312 // avoid flicker - but don't need to do this for a hidden listbox
313 bool hideAndShow = IsShown();
314 if ( hideAndShow )
315 {
316 ShowWindow(GetHwnd(), SW_HIDE);
317 }
318
319 ListBox_ResetContent(GetHwnd());
320
321 m_noItems = choices.GetCount();
322 int i;
323 for (i = 0; i < m_noItems; i++)
324 {
325 ListBox_AddString(GetHwnd(), choices[i]);
326 if ( clientData )
327 {
328 SetClientData(i, clientData[i]);
329 }
330 }
331
332 #if wxUSE_OWNER_DRAWN
333 if ( m_windowStyle & wxLB_OWNERDRAW ) {
334 // first delete old items
335 WX_CLEAR_ARRAY(m_aItems);
336
337 // then create new ones
338 for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) {
339 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
340 pNewItem->SetName(choices[ui]);
341 m_aItems.Add(pNewItem);
342 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
343 }
344 }
345 #endif // wxUSE_OWNER_DRAWN
346
347 SetHorizontalExtent();
348
349 if ( hideAndShow )
350 {
351 // show the listbox back if we hid it
352 ShowWindow(GetHwnd(), SW_SHOW);
353 }
354 }
355
356 int wxListBox::FindString(const wxString& s) const
357 {
358 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
359 if (pos == LB_ERR)
360 return wxNOT_FOUND;
361 else
362 return pos;
363 }
364
365 void wxListBox::Clear()
366 {
367 Free();
368
369 ListBox_ResetContent(GetHwnd());
370
371 m_noItems = 0;
372 SetHorizontalExtent();
373 }
374
375 void wxListBox::Free()
376 {
377 #if wxUSE_OWNER_DRAWN
378 if ( m_windowStyle & wxLB_OWNERDRAW )
379 {
380 WX_CLEAR_ARRAY(m_aItems);
381 }
382 else
383 #endif // wxUSE_OWNER_DRAWN
384 if ( HasClientObjectData() )
385 {
386 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
387 {
388 delete GetClientObject(n);
389 }
390 }
391 }
392
393 void wxListBox::SetSelection(int N, bool select)
394 {
395 wxCHECK_RET( N == wxNOT_FOUND ||
396 (N >= 0 && N < m_noItems),
397 wxT("invalid index in wxListBox::SetSelection") );
398
399 if ( HasMultipleSelection() )
400 {
401 SendMessage(GetHwnd(), LB_SETSEL, select, N);
402 }
403 else
404 {
405 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
406 }
407 }
408
409 bool wxListBox::IsSelected(int N) const
410 {
411 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
412 wxT("invalid index in wxListBox::Selected") );
413
414 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
415 }
416
417 wxClientData* wxListBox::DoGetItemClientObject(int n) const
418 {
419 return (wxClientData *)DoGetItemClientData(n);
420 }
421
422 void *wxListBox::DoGetItemClientData(int n) const
423 {
424 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
425 wxT("invalid index in wxListBox::GetClientData") );
426
427 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
428 }
429
430 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
431 {
432 DoSetItemClientData(n, clientData);
433 }
434
435 void wxListBox::DoSetItemClientData(int n, void *clientData)
436 {
437 wxCHECK_RET( n >= 0 && n < m_noItems,
438 wxT("invalid index in wxListBox::SetClientData") );
439
440 #if wxUSE_OWNER_DRAWN
441 if ( m_windowStyle & wxLB_OWNERDRAW )
442 {
443 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
444 // in OnMeasure/OnDraw.
445 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
446 }
447 #endif // wxUSE_OWNER_DRAWN
448
449 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
450 wxLogDebug(wxT("LB_SETITEMDATA failed"));
451 }
452
453 // Return number of selections and an array of selected integers
454 int wxListBox::GetSelections(wxArrayInt& aSelections) const
455 {
456 aSelections.Empty();
457
458 if ( HasMultipleSelection() )
459 {
460 int countSel = ListBox_GetSelCount(GetHwnd());
461 if ( countSel == LB_ERR )
462 {
463 wxLogDebug(_T("ListBox_GetSelCount failed"));
464 }
465 else if ( countSel != 0 )
466 {
467 int *selections = new int[countSel];
468
469 if ( ListBox_GetSelItems(GetHwnd(),
470 countSel, selections) == LB_ERR )
471 {
472 wxLogDebug(wxT("ListBox_GetSelItems failed"));
473 countSel = -1;
474 }
475 else
476 {
477 aSelections.Alloc(countSel);
478 for ( int n = 0; n < countSel; n++ )
479 aSelections.Add(selections[n]);
480 }
481
482 delete [] selections;
483 }
484
485 return countSel;
486 }
487 else // single-selection listbox
488 {
489 if (ListBox_GetCurSel(GetHwnd()) > -1)
490 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
491
492 return aSelections.Count();
493 }
494 }
495
496 // Get single selection, for single choice list items
497 int wxListBox::GetSelection() const
498 {
499 wxCHECK_MSG( !HasMultipleSelection(),
500 -1,
501 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
502
503 return ListBox_GetCurSel(GetHwnd());
504 }
505
506 // Find string for position
507 wxString wxListBox::GetString(int N) const
508 {
509 wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString,
510 wxT("invalid index in wxListBox::GetClientData") );
511
512 int len = ListBox_GetTextLen(GetHwnd(), N);
513
514 // +1 for terminating NUL
515 wxString result;
516 ListBox_GetText(GetHwnd(), N, wxStringBuffer(result, len + 1));
517
518 return result;
519 }
520
521 void
522 wxListBox::DoInsertItems(const wxArrayString& items, int pos)
523 {
524 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
525 wxT("invalid index in wxListBox::InsertItems") );
526
527 InvalidateBestSize();
528
529 int nItems = items.GetCount();
530 for ( int i = 0; i < nItems; i++ )
531 {
532 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
533
534 #if wxUSE_OWNER_DRAWN
535 if ( m_windowStyle & wxLB_OWNERDRAW )
536 {
537 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
538 pNewItem->SetName(items[i]);
539 pNewItem->SetFont(GetFont());
540 m_aItems.Insert(pNewItem, idx);
541
542 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
543 }
544 #else
545 wxUnusedVar(idx);
546 #endif // wxUSE_OWNER_DRAWN
547 }
548
549 m_noItems += nItems;
550
551 SetHorizontalExtent();
552 }
553
554 void wxListBox::SetString(int N, const wxString& s)
555 {
556 wxCHECK_RET( N >= 0 && N < m_noItems,
557 wxT("invalid index in wxListBox::SetString") );
558
559 // remember the state of the item
560 bool wasSelected = IsSelected(N);
561
562 void *oldData = NULL;
563 wxClientData *oldObjData = NULL;
564 if ( m_clientDataItemsType == wxClientData_Void )
565 oldData = GetClientData(N);
566 else if ( m_clientDataItemsType == wxClientData_Object )
567 oldObjData = GetClientObject(N);
568
569 // delete and recreate it
570 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
571
572 int newN = N;
573 if ( N == m_noItems - 1 )
574 newN = -1;
575
576 ListBox_InsertString(GetHwnd(), newN, s);
577
578 // restore the client data
579 if ( oldData )
580 SetClientData(N, oldData);
581 else if ( oldObjData )
582 SetClientObject(N, oldObjData);
583
584 #if wxUSE_OWNER_DRAWN
585 if ( m_windowStyle & wxLB_OWNERDRAW )
586 {
587 // update item's text
588 m_aItems[N]->SetName(s);
589
590 // reassign the item's data
591 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
592 }
593 #endif //USE_OWNER_DRAWN
594
595 // we may have lost the selection
596 if ( wasSelected )
597 Select(N);
598 }
599
600 int wxListBox::GetCount() const
601 {
602 return m_noItems;
603 }
604
605 // ----------------------------------------------------------------------------
606 // helpers
607 // ----------------------------------------------------------------------------
608
609 // Windows-specific code to set the horizontal extent of the listbox, if
610 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
611 // Otherwise, all strings are used.
612 void wxListBox::SetHorizontalExtent(const wxString& s)
613 {
614 // Only necessary if we want a horizontal scrollbar
615 if (!(m_windowStyle & wxHSCROLL))
616 return;
617 TEXTMETRIC lpTextMetric;
618
619 if ( !s.IsEmpty() )
620 {
621 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
622 HDC dc = GetWindowDC(GetHwnd());
623 HFONT oldFont = 0;
624 if (GetFont().Ok() && GetFont().GetResourceHandle())
625 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
626
627 GetTextMetrics(dc, &lpTextMetric);
628 SIZE extentXY;
629 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
630 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
631
632 if (oldFont)
633 ::SelectObject(dc, oldFont);
634
635 ReleaseDC(GetHwnd(), dc);
636 if (extentX > existingExtent)
637 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
638 }
639 else
640 {
641 int largestExtent = 0;
642 HDC dc = GetWindowDC(GetHwnd());
643 HFONT oldFont = 0;
644 if (GetFont().Ok() && GetFont().GetResourceHandle())
645 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
646
647 GetTextMetrics(dc, &lpTextMetric);
648
649 // FIXME: buffer overflow!!
650 wxChar buf[1024];
651 for (int i = 0; i < m_noItems; i++)
652 {
653 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf);
654 buf[len] = 0;
655 SIZE extentXY;
656 ::GetTextExtentPoint(dc, buf, len, &extentXY);
657 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
658 if (extentX > largestExtent)
659 largestExtent = extentX;
660 }
661 if (oldFont)
662 ::SelectObject(dc, oldFont);
663
664 ReleaseDC(GetHwnd(), dc);
665 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
666 }
667 }
668
669 wxSize wxListBox::DoGetBestSize() const
670 {
671 // find the widest string
672 int wLine;
673 int wListbox = 0;
674 for ( int i = 0; i < m_noItems; i++ )
675 {
676 wxString str(GetString(i));
677 GetTextExtent(str, &wLine, NULL);
678 if ( wLine > wListbox )
679 wListbox = wLine;
680 }
681
682 // give it some reasonable default value if there are no strings in the
683 // list
684 if ( wListbox == 0 )
685 wListbox = 100;
686
687 // the listbox should be slightly larger than the widest string
688 int cx, cy;
689 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
690
691 wListbox += 3*cx;
692
693 // don't make the listbox too tall (limit height to 10 items) but don't
694 // make it too small neither
695 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
696 wxMin(wxMax(m_noItems, 3), 10);
697
698 return wxSize(wListbox, hListbox);
699 }
700
701 // ----------------------------------------------------------------------------
702 // callbacks
703 // ----------------------------------------------------------------------------
704
705 bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
706 {
707 wxEventType evtType;
708 if ( param == LBN_SELCHANGE )
709 {
710 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
711 }
712 else if ( param == LBN_DBLCLK )
713 {
714 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
715 }
716 else
717 {
718 // some event we're not interested in
719 return FALSE;
720 }
721
722 wxCommandEvent event(evtType, m_windowId);
723 event.SetEventObject( this );
724
725 // retrieve the affected item
726 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
727 if ( n != LB_ERR )
728 {
729 if ( HasClientObjectData() )
730 event.SetClientObject( GetClientObject(n) );
731 else if ( HasClientUntypedData() )
732 event.SetClientData( GetClientData(n) );
733
734 event.SetString( GetString(n) );
735 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : TRUE );
736 }
737
738 event.m_commandInt = n;
739
740 return GetEventHandler()->ProcessEvent(event);
741 }
742
743 // ----------------------------------------------------------------------------
744 // wxCheckListBox support
745 // ----------------------------------------------------------------------------
746
747 #if wxUSE_OWNER_DRAWN
748
749 // drawing
750 // -------
751
752 // space beneath/above each row in pixels
753 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
754 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
755
756 // the height is the same for all items
757 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
758
759 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
760 // message is not yet sent when we get here!
761 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
762 {
763 // only owner-drawn control should receive this message
764 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
765
766 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
767
768 #ifdef __WXWINCE__
769 HDC hdc = GetDC(NULL);
770 #else
771 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
772 #endif
773
774 wxDC dc;
775 dc.SetHDC((WXHDC)hdc);
776 dc.SetFont(GetFont());
777
778 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
779 pStruct->itemWidth = dc.GetCharWidth();
780
781 dc.SetHDC(0);
782
783 DeleteDC(hdc);
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 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
809 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
810
811 return pItem->OnDrawItem(dc, rect,
812 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
813 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
814 }
815
816 #endif // wxUSE_OWNER_DRAWN
817
818 #endif // wxUSE_LISTBOX