Warning fixes found under hardest mode of OpenWatcom. Seems clean in Borland, MinGW...
[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 wxChar* buffer = result.GetWriteBuf(len + 1);
517 ListBox_GetText(GetHwnd(), N, buffer);
518 result.UngetWriteBuf();
519
520 return result;
521 }
522
523 void
524 wxListBox::DoInsertItems(const wxArrayString& items, int pos)
525 {
526 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
527 wxT("invalid index in wxListBox::InsertItems") );
528
529 InvalidateBestSize();
530
531 int nItems = items.GetCount();
532 for ( int i = 0; i < nItems; i++ )
533 {
534 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
535
536 #if wxUSE_OWNER_DRAWN
537 if ( m_windowStyle & wxLB_OWNERDRAW )
538 {
539 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
540 pNewItem->SetName(items[i]);
541 pNewItem->SetFont(GetFont());
542 m_aItems.Insert(pNewItem, idx);
543
544 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
545 }
546 #else
547 wxUnusedVar(idx);
548 #endif // wxUSE_OWNER_DRAWN
549 }
550
551 m_noItems += nItems;
552
553 SetHorizontalExtent();
554 }
555
556 void wxListBox::SetString(int N, const wxString& s)
557 {
558 wxCHECK_RET( N >= 0 && N < m_noItems,
559 wxT("invalid index in wxListBox::SetString") );
560
561 // remember the state of the item
562 bool wasSelected = IsSelected(N);
563
564 void *oldData = NULL;
565 wxClientData *oldObjData = NULL;
566 if ( m_clientDataItemsType == wxClientData_Void )
567 oldData = GetClientData(N);
568 else if ( m_clientDataItemsType == wxClientData_Object )
569 oldObjData = GetClientObject(N);
570
571 // delete and recreate it
572 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
573
574 int newN = N;
575 if ( N == m_noItems - 1 )
576 newN = -1;
577
578 ListBox_InsertString(GetHwnd(), newN, s);
579
580 // restore the client data
581 if ( oldData )
582 SetClientData(N, oldData);
583 else if ( oldObjData )
584 SetClientObject(N, oldObjData);
585
586 #if wxUSE_OWNER_DRAWN
587 if ( m_windowStyle & wxLB_OWNERDRAW )
588 {
589 // update item's text
590 m_aItems[N]->SetName(s);
591
592 // reassign the item's data
593 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
594 }
595 #endif //USE_OWNER_DRAWN
596
597 // we may have lost the selection
598 if ( wasSelected )
599 Select(N);
600 }
601
602 int wxListBox::GetCount() const
603 {
604 return m_noItems;
605 }
606
607 // ----------------------------------------------------------------------------
608 // helpers
609 // ----------------------------------------------------------------------------
610
611 // Windows-specific code to set the horizontal extent of the listbox, if
612 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
613 // Otherwise, all strings are used.
614 void wxListBox::SetHorizontalExtent(const wxString& s)
615 {
616 // Only necessary if we want a horizontal scrollbar
617 if (!(m_windowStyle & wxHSCROLL))
618 return;
619 TEXTMETRIC lpTextMetric;
620
621 if ( !s.IsEmpty() )
622 {
623 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
624 HDC dc = GetWindowDC(GetHwnd());
625 HFONT oldFont = 0;
626 if (GetFont().Ok() && GetFont().GetResourceHandle())
627 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
628
629 GetTextMetrics(dc, &lpTextMetric);
630 SIZE extentXY;
631 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
632 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
633
634 if (oldFont)
635 ::SelectObject(dc, oldFont);
636
637 ReleaseDC(GetHwnd(), dc);
638 if (extentX > existingExtent)
639 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
640 }
641 else
642 {
643 int largestExtent = 0;
644 HDC dc = GetWindowDC(GetHwnd());
645 HFONT oldFont = 0;
646 if (GetFont().Ok() && GetFont().GetResourceHandle())
647 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
648
649 GetTextMetrics(dc, &lpTextMetric);
650
651 // FIXME: buffer overflow!!
652 wxChar buf[1024];
653 for (int i = 0; i < m_noItems; i++)
654 {
655 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf);
656 buf[len] = 0;
657 SIZE extentXY;
658 ::GetTextExtentPoint(dc, buf, len, &extentXY);
659 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
660 if (extentX > largestExtent)
661 largestExtent = extentX;
662 }
663 if (oldFont)
664 ::SelectObject(dc, oldFont);
665
666 ReleaseDC(GetHwnd(), dc);
667 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
668 }
669 }
670
671 wxSize wxListBox::DoGetBestSize() const
672 {
673 // find the widest string
674 int wLine;
675 int wListbox = 0;
676 for ( int i = 0; i < m_noItems; i++ )
677 {
678 wxString str(GetString(i));
679 GetTextExtent(str, &wLine, NULL);
680 if ( wLine > wListbox )
681 wListbox = wLine;
682 }
683
684 // give it some reasonable default value if there are no strings in the
685 // list
686 if ( wListbox == 0 )
687 wListbox = 100;
688
689 // the listbox should be slightly larger than the widest string
690 int cx, cy;
691 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
692
693 wListbox += 3*cx;
694
695 // don't make the listbox too tall (limit height to 10 items) but don't
696 // make it too small neither
697 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
698 wxMin(wxMax(m_noItems, 3), 10);
699
700 return wxSize(wListbox, hListbox);
701 }
702
703 // ----------------------------------------------------------------------------
704 // callbacks
705 // ----------------------------------------------------------------------------
706
707 bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
708 {
709 wxEventType evtType;
710 if ( param == LBN_SELCHANGE )
711 {
712 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
713 }
714 else if ( param == LBN_DBLCLK )
715 {
716 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
717 }
718 else
719 {
720 // some event we're not interested in
721 return false;
722 }
723
724 wxCommandEvent event(evtType, m_windowId);
725 event.SetEventObject( this );
726
727 // retrieve the affected item
728 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
729 if ( n != LB_ERR )
730 {
731 if ( HasClientObjectData() )
732 event.SetClientObject( GetClientObject(n) );
733 else if ( HasClientUntypedData() )
734 event.SetClientData( GetClientData(n) );
735
736 event.SetString( GetString(n) );
737 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : true );
738 }
739
740 event.m_commandInt = n;
741
742 return GetEventHandler()->ProcessEvent(event);
743 }
744
745 // ----------------------------------------------------------------------------
746 // wxCheckListBox support
747 // ----------------------------------------------------------------------------
748
749 #if wxUSE_OWNER_DRAWN
750
751 // drawing
752 // -------
753
754 // space beneath/above each row in pixels
755 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
756 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
757
758 // the height is the same for all items
759 // TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
760
761 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
762 // message is not yet sent when we get here!
763 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
764 {
765 // only owner-drawn control should receive this message
766 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
767
768 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
769
770 #ifdef __WXWINCE__
771 HDC hdc = GetDC(NULL);
772 #else
773 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
774 #endif
775
776 wxDC dc;
777 dc.SetHDC((WXHDC)hdc);
778 dc.SetFont(GetFont());
779
780 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
781 pStruct->itemWidth = dc.GetCharWidth();
782
783 dc.SetHDC(0);
784
785 DeleteDC(hdc);
786
787 return true;
788 }
789
790 // forward the message to the appropriate item
791 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
792 {
793 // only owner-drawn control should receive this message
794 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
795
796 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
797 UINT itemID = pStruct->itemID;
798
799 // the item may be -1 for an empty listbox
800 if ( itemID == (UINT)-1 )
801 return false;
802
803 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
804
805 wxCHECK( data && (data != LB_ERR), false );
806
807 wxListBoxItem *pItem = (wxListBoxItem *)data;
808
809 wxDCTemp dc((WXHDC)pStruct->hDC);
810 wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top);
811 wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom);
812 wxRect rect(pt1, pt2);
813
814 return pItem->OnDrawItem(dc, rect,
815 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
816 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
817 }
818
819 #endif // wxUSE_OWNER_DRAWN
820
821 #endif // wxUSE_LISTBOX