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