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