]> git.saurik.com Git - wxWidgets.git/blob - src/msw/listbox.cpp
fix wxExecute() return code checks and removed not working code to open URLs in new...
[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/listbox.h"
23 #include "wx/settings.h"
24 #include "wx/brush.h"
25 #include "wx/font.h"
26 #include "wx/dc.h"
27 #include "wx/utils.h"
28 #endif
29
30 #include "wx/window.h"
31 #include "wx/msw/private.h"
32
33 #include <windowsx.h>
34
35 #include "wx/dynarray.h"
36 #include "wx/log.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( N >= 0 && N < m_noItems,
254 wxT("invalid index in wxListBox::SetFirstItem") );
255
256 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
257 }
258
259 void wxListBox::Delete(int N)
260 {
261 wxCHECK_RET( N >= 0 && N < m_noItems,
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 InvalidateBestSize();
280 }
281
282 int wxListBox::DoAppend(const wxString& item)
283 {
284 int index = ListBox_AddString(GetHwnd(), item);
285 m_noItems++;
286
287 #if wxUSE_OWNER_DRAWN
288 if ( m_windowStyle & wxLB_OWNERDRAW ) {
289 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
290 pNewItem->SetName(item);
291 m_aItems.Insert(pNewItem, index);
292 ListBox_SetItemData(GetHwnd(), index, pNewItem);
293 pNewItem->SetFont(GetFont());
294 }
295 #endif // wxUSE_OWNER_DRAWN
296
297 SetHorizontalExtent(item);
298
299 InvalidateBestSize();
300 return index;
301 }
302
303 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
304 {
305 // avoid flicker - but don't need to do this for a hidden listbox
306 bool hideAndShow = IsShown();
307 if ( hideAndShow )
308 {
309 ShowWindow(GetHwnd(), SW_HIDE);
310 }
311
312 ListBox_ResetContent(GetHwnd());
313
314 m_noItems = choices.GetCount();
315 int i;
316 for (i = 0; i < m_noItems; i++)
317 {
318 ListBox_AddString(GetHwnd(), choices[i]);
319 if ( clientData )
320 {
321 SetClientData(i, clientData[i]);
322 }
323 }
324
325 #if wxUSE_OWNER_DRAWN
326 if ( m_windowStyle & wxLB_OWNERDRAW ) {
327 // first delete old items
328 WX_CLEAR_ARRAY(m_aItems);
329
330 // then create new ones
331 for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) {
332 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
333 pNewItem->SetName(choices[ui]);
334 m_aItems.Add(pNewItem);
335 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
336 }
337 }
338 #endif // wxUSE_OWNER_DRAWN
339
340 SetHorizontalExtent();
341
342 if ( hideAndShow )
343 {
344 // show the listbox back if we hid it
345 ShowWindow(GetHwnd(), SW_SHOW);
346 }
347
348 InvalidateBestSize();
349 }
350
351 int wxListBox::FindString(const wxString& s) const
352 {
353 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
354 if (pos == LB_ERR)
355 return wxNOT_FOUND;
356 else
357 return pos;
358 }
359
360 void wxListBox::Clear()
361 {
362 Free();
363
364 ListBox_ResetContent(GetHwnd());
365
366 m_noItems = 0;
367 SetHorizontalExtent();
368
369 InvalidateBestSize();
370 }
371
372 void wxListBox::Free()
373 {
374 #if wxUSE_OWNER_DRAWN
375 if ( m_windowStyle & wxLB_OWNERDRAW )
376 {
377 WX_CLEAR_ARRAY(m_aItems);
378 }
379 else
380 #endif // wxUSE_OWNER_DRAWN
381 if ( HasClientObjectData() )
382 {
383 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
384 {
385 delete GetClientObject(n);
386 }
387 }
388 }
389
390 void wxListBox::DoSetSelection(int N, bool select)
391 {
392 wxCHECK_RET( N == wxNOT_FOUND ||
393 (N >= 0 && N < m_noItems),
394 wxT("invalid index in wxListBox::SetSelection") );
395
396 if ( HasMultipleSelection() )
397 {
398 SendMessage(GetHwnd(), LB_SETSEL, select, N);
399 }
400 else
401 {
402 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
403 }
404 }
405
406 bool wxListBox::IsSelected(int N) const
407 {
408 wxCHECK_MSG( N >= 0 && N < m_noItems, false,
409 wxT("invalid index in wxListBox::Selected") );
410
411 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true;
412 }
413
414 wxClientData* wxListBox::DoGetItemClientObject(int n) const
415 {
416 return (wxClientData *)DoGetItemClientData(n);
417 }
418
419 void *wxListBox::DoGetItemClientData(int n) const
420 {
421 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
422 wxT("invalid index in wxListBox::GetClientData") );
423
424 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
425 }
426
427 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
428 {
429 DoSetItemClientData(n, clientData);
430 }
431
432 void wxListBox::DoSetItemClientData(int n, void *clientData)
433 {
434 wxCHECK_RET( n >= 0 && n < m_noItems,
435 wxT("invalid index in wxListBox::SetClientData") );
436
437 #if wxUSE_OWNER_DRAWN
438 if ( m_windowStyle & wxLB_OWNERDRAW )
439 {
440 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
441 // in OnMeasure/OnDraw.
442 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
443 }
444 #endif // wxUSE_OWNER_DRAWN
445
446 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
447 wxLogDebug(wxT("LB_SETITEMDATA failed"));
448 }
449
450 // Return number of selections and an array of selected integers
451 int wxListBox::GetSelections(wxArrayInt& aSelections) const
452 {
453 aSelections.Empty();
454
455 if ( HasMultipleSelection() )
456 {
457 int countSel = ListBox_GetSelCount(GetHwnd());
458 if ( countSel == LB_ERR )
459 {
460 wxLogDebug(_T("ListBox_GetSelCount failed"));
461 }
462 else if ( countSel != 0 )
463 {
464 int *selections = new int[countSel];
465
466 if ( ListBox_GetSelItems(GetHwnd(),
467 countSel, selections) == LB_ERR )
468 {
469 wxLogDebug(wxT("ListBox_GetSelItems failed"));
470 countSel = -1;
471 }
472 else
473 {
474 aSelections.Alloc(countSel);
475 for ( int n = 0; n < countSel; n++ )
476 aSelections.Add(selections[n]);
477 }
478
479 delete [] selections;
480 }
481
482 return countSel;
483 }
484 else // single-selection listbox
485 {
486 if (ListBox_GetCurSel(GetHwnd()) > -1)
487 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
488
489 return aSelections.Count();
490 }
491 }
492
493 // Get single selection, for single choice list items
494 int wxListBox::GetSelection() const
495 {
496 wxCHECK_MSG( !HasMultipleSelection(),
497 -1,
498 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
499
500 return ListBox_GetCurSel(GetHwnd());
501 }
502
503 // Find string for position
504 wxString wxListBox::GetString(int N) const
505 {
506 wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString,
507 wxT("invalid index in wxListBox::GetString") );
508
509 int len = ListBox_GetTextLen(GetHwnd(), N);
510
511 // +1 for terminating NUL
512 wxString result;
513 ListBox_GetText(GetHwnd(), N, (wxChar*)wxStringBuffer(result, len + 1));
514
515 return result;
516 }
517
518 void
519 wxListBox::DoInsertItems(const wxArrayString& items, int pos)
520 {
521 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
522 wxT("invalid index in wxListBox::InsertItems") );
523
524 int nItems = items.GetCount();
525 for ( int i = 0; i < nItems; i++ )
526 {
527 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
528
529 #if wxUSE_OWNER_DRAWN
530 if ( m_windowStyle & wxLB_OWNERDRAW )
531 {
532 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
533 pNewItem->SetName(items[i]);
534 pNewItem->SetFont(GetFont());
535 m_aItems.Insert(pNewItem, idx);
536
537 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
538 }
539 #else
540 wxUnusedVar(idx);
541 #endif // wxUSE_OWNER_DRAWN
542 }
543
544 m_noItems += nItems;
545
546 SetHorizontalExtent();
547
548 InvalidateBestSize();
549 }
550
551 void wxListBox::SetString(int N, const wxString& s)
552 {
553 wxCHECK_RET( N >= 0 && N < m_noItems,
554 wxT("invalid index in wxListBox::SetString") );
555
556 // remember the state of the item
557 bool wasSelected = IsSelected(N);
558
559 void *oldData = NULL;
560 wxClientData *oldObjData = NULL;
561 if ( m_clientDataItemsType == wxClientData_Void )
562 oldData = GetClientData(N);
563 else if ( m_clientDataItemsType == wxClientData_Object )
564 oldObjData = GetClientObject(N);
565
566 // delete and recreate it
567 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
568
569 int newN = N;
570 if ( N == m_noItems - 1 )
571 newN = -1;
572
573 ListBox_InsertString(GetHwnd(), newN, s);
574
575 // restore the client data
576 if ( oldData )
577 SetClientData(N, oldData);
578 else if ( oldObjData )
579 SetClientObject(N, oldObjData);
580
581 #if wxUSE_OWNER_DRAWN
582 if ( m_windowStyle & wxLB_OWNERDRAW )
583 {
584 // update item's text
585 m_aItems[N]->SetName(s);
586
587 // reassign the item's data
588 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
589 }
590 #endif //USE_OWNER_DRAWN
591
592 // we may have lost the selection
593 if ( wasSelected )
594 Select(N);
595
596 InvalidateBestSize();
597 }
598
599 int wxListBox::GetCount() const
600 {
601 return m_noItems;
602 }
603
604 // ----------------------------------------------------------------------------
605 // helpers
606 // ----------------------------------------------------------------------------
607
608 // Windows-specific code to set the horizontal extent of the listbox, if
609 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
610 // Otherwise, all strings are used.
611 void wxListBox::SetHorizontalExtent(const wxString& s)
612 {
613 // Only necessary if we want a horizontal scrollbar
614 if (!(m_windowStyle & wxHSCROLL))
615 return;
616 TEXTMETRIC lpTextMetric;
617
618 if ( !s.empty() )
619 {
620 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
621 HDC dc = GetWindowDC(GetHwnd());
622 HFONT oldFont = 0;
623 if (GetFont().Ok() && GetFont().GetResourceHandle() != 0)
624 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
625
626 GetTextMetrics(dc, &lpTextMetric);
627 SIZE extentXY;
628 ::GetTextExtentPoint32(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
629 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
630
631 if (oldFont)
632 ::SelectObject(dc, oldFont);
633
634 ReleaseDC(GetHwnd(), dc);
635 if (extentX > existingExtent)
636 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
637 }
638 else
639 {
640 int largestExtent = 0;
641 HDC dc = GetWindowDC(GetHwnd());
642 HFONT oldFont = 0;
643 if (GetFont().Ok() && GetFont().GetResourceHandle() != 0)
644 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
645
646 GetTextMetrics(dc, &lpTextMetric);
647
648 for (int i = 0; i < m_noItems; i++)
649 {
650 wxString str = GetString(i);
651 SIZE extentXY;
652 ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
653 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
654 if (extentX > largestExtent)
655 largestExtent = extentX;
656 }
657 if (oldFont)
658 ::SelectObject(dc, oldFont);
659
660 ReleaseDC(GetHwnd(), dc);
661 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
662 }
663 }
664
665 wxSize wxListBox::DoGetBestSize() const
666 {
667 // find the widest string
668 int wLine;
669 int wListbox = 0;
670 for ( int i = 0; i < m_noItems; i++ )
671 {
672 wxString str(GetString(i));
673 GetTextExtent(str, &wLine, NULL);
674 if ( wLine > wListbox )
675 wListbox = wLine;
676 }
677
678 // give it some reasonable default value if there are no strings in the
679 // list
680 if ( wListbox == 0 )
681 wListbox = 100;
682
683 // the listbox should be slightly larger than the widest string
684 int cx, cy;
685 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
686
687 wListbox += 3*cx;
688
689 // Add room for the scrollbar
690 wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
691
692 // don't make the listbox too tall (limit height to 10 items) but don't
693 // make it too small neither
694 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
695 wxMin(wxMax(m_noItems, 3), 10);
696
697 wxSize best(wListbox, hListbox);
698 CacheBestSize(best);
699 return best;
700 }
701
702 // ----------------------------------------------------------------------------
703 // callbacks
704 // ----------------------------------------------------------------------------
705
706 bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
707 {
708 wxEventType evtType;
709 if ( param == LBN_SELCHANGE )
710 {
711 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
712 }
713 else if ( param == LBN_DBLCLK )
714 {
715 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
716 }
717 else
718 {
719 // some event we're not interested in
720 return false;
721 }
722
723 wxCommandEvent event(evtType, m_windowId);
724 event.SetEventObject( this );
725
726 // retrieve the affected item
727 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
728 if ( n != LB_ERR )
729 {
730 if ( HasClientObjectData() )
731 event.SetClientObject( GetClientObject(n) );
732 else if ( HasClientUntypedData() )
733 event.SetClientData( GetClientData(n) );
734
735 event.SetString( GetString(n) );
736 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : true );
737 }
738
739 event.SetInt(n);
740
741 return GetEventHandler()->ProcessEvent(event);
742 }
743
744 // ----------------------------------------------------------------------------
745 // wxCheckListBox support
746 // ----------------------------------------------------------------------------
747
748 #if wxUSE_OWNER_DRAWN
749
750 // drawing
751 // -------
752
753 // space beneath/above each row in pixels
754 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
755 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
756
757 // the height is the same for all items
758 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
759
760 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
761 // message is not yet sent when we get here!
762 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
763 {
764 // only owner-drawn control should receive this message
765 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
766
767 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
768
769 #ifdef __WXWINCE__
770 HDC hdc = GetDC(NULL);
771 #else
772 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
773 #endif
774
775 wxDC dc;
776 dc.SetHDC((WXHDC)hdc);
777 dc.SetFont(GetFont());
778
779 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
780 pStruct->itemWidth = dc.GetCharWidth();
781
782 dc.SetHDC(0);
783
784 DeleteDC(hdc);
785
786 return true;
787 }
788
789 // forward the message to the appropriate item
790 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
791 {
792 // only owner-drawn control should receive this message
793 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
794
795 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
796 UINT itemID = pStruct->itemID;
797
798 // the item may be -1 for an empty listbox
799 if ( itemID == (UINT)-1 )
800 return false;
801
802 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
803
804 wxCHECK( data && (data != LB_ERR), false );
805
806 wxListBoxItem *pItem = (wxListBoxItem *)data;
807
808 wxDCTemp dc((WXHDC)pStruct->hDC);
809 wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top);
810 wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom);
811 wxRect rect(pt1, pt2);
812
813 return pItem->OnDrawItem(dc, rect,
814 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
815 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
816 }
817
818 #endif // wxUSE_OWNER_DRAWN
819
820 #endif // wxUSE_LISTBOX