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