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