Added some window style metadata
[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 WX_BEGIN_FLAGS( wxListBoxStyle )
54 // new style border flags, we put them first to
55 // use them for streaming out
56 WX_FLAGS_MEMBER(wxBORDER_SIMPLE)
57 WX_FLAGS_MEMBER(wxBORDER_SUNKEN)
58 WX_FLAGS_MEMBER(wxBORDER_DOUBLE)
59 WX_FLAGS_MEMBER(wxBORDER_RAISED)
60 WX_FLAGS_MEMBER(wxBORDER_STATIC)
61 WX_FLAGS_MEMBER(wxBORDER_NONE)
62
63 // old style border flags
64 WX_FLAGS_MEMBER(wxSIMPLE_BORDER)
65 WX_FLAGS_MEMBER(wxSUNKEN_BORDER)
66 WX_FLAGS_MEMBER(wxDOUBLE_BORDER)
67 WX_FLAGS_MEMBER(wxRAISED_BORDER)
68 WX_FLAGS_MEMBER(wxSTATIC_BORDER)
69 WX_FLAGS_MEMBER(wxNO_BORDER)
70
71 // standard window styles
72 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL)
73 WX_FLAGS_MEMBER(wxCLIP_CHILDREN)
74 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW)
75 WX_FLAGS_MEMBER(wxWANTS_CHARS)
76 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE)
77 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB )
78 WX_FLAGS_MEMBER(wxVSCROLL)
79 WX_FLAGS_MEMBER(wxHSCROLL)
80
81 WX_FLAGS_MEMBER(wxLB_SINGLE)
82 WX_FLAGS_MEMBER(wxLB_MULTIPLE)
83 WX_FLAGS_MEMBER(wxLB_EXTENDED)
84 WX_FLAGS_MEMBER(wxLB_HSCROLL)
85 WX_FLAGS_MEMBER(wxLB_ALWAYS_SB)
86 WX_FLAGS_MEMBER(wxLB_NEEDED_SB)
87 WX_FLAGS_MEMBER(wxLB_SORT)
88
89 WX_END_FLAGS( wxListBoxStyle )
90
91 IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h")
92
93 WX_BEGIN_PROPERTIES_TABLE(wxListBox)
94 // TODO DELEGATES
95 WX_PROPERTY( Font , wxFont , SetFont , GetFont , , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
96 WX_PROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
97 WX_PROPERTY( Selection ,int, SetSelection, GetSelection,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
98 WX_PROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
99 WX_END_PROPERTIES_TABLE()
100
101 WX_BEGIN_HANDLERS_TABLE(wxListBox)
102 WX_END_HANDLERS_TABLE()
103
104 WX_CONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
105 #else
106 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
107 #endif
108
109 /*
110 TODO PROPERTIES
111 selection
112 content
113 item
114 */
115
116 // ============================================================================
117 // list box item declaration and implementation
118 // ============================================================================
119
120 #if wxUSE_OWNER_DRAWN
121
122 class wxListBoxItem : public wxOwnerDrawn
123 {
124 public:
125 wxListBoxItem(const wxString& str = wxEmptyString);
126 };
127
128 wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
129 {
130 // no bitmaps/checkmarks
131 SetMarginWidth(0);
132 }
133
134 wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
135 {
136 return new wxListBoxItem();
137 }
138
139 #endif //USE_OWNER_DRAWN
140
141 // ============================================================================
142 // list box control implementation
143 // ============================================================================
144
145 // ----------------------------------------------------------------------------
146 // creation
147 // ----------------------------------------------------------------------------
148
149 // Listbox item
150 wxListBox::wxListBox()
151 {
152 m_noItems = 0;
153 m_selected = 0;
154 }
155
156 bool wxListBox::Create(wxWindow *parent,
157 wxWindowID id,
158 const wxPoint& pos,
159 const wxSize& size,
160 int n, const wxString choices[],
161 long style,
162 const wxValidator& validator,
163 const wxString& name)
164 {
165 m_noItems = 0;
166 m_hWnd = 0;
167 m_selected = 0;
168
169 SetName(name);
170 #if wxUSE_VALIDATORS
171 SetValidator(validator);
172 #endif // wxUSE_VALIDATORS
173
174 if (parent)
175 parent->AddChild(this);
176
177 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
178 SetForegroundColour(parent->GetForegroundColour());
179
180 m_windowId = ( id == -1 ) ? (int)NewControlId() : id;
181
182 int x = pos.x;
183 int y = pos.y;
184 int width = size.x;
185 int height = size.y;
186 m_windowStyle = style;
187
188 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_TABSTOP |
189 LBS_NOTIFY | LBS_HASSTRINGS ;
190
191 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
192 _T("only one of listbox selection modes can be specified") );
193
194 if ( (m_windowStyle & wxBORDER_MASK) == wxBORDER_DEFAULT )
195 m_windowStyle |= wxBORDER_SUNKEN;
196
197 if ( m_windowStyle & wxCLIP_SIBLINGS )
198 wstyle |= WS_CLIPSIBLINGS;
199
200 if (m_windowStyle & wxLB_MULTIPLE)
201 wstyle |= LBS_MULTIPLESEL;
202 else if (m_windowStyle & wxLB_EXTENDED)
203 wstyle |= LBS_EXTENDEDSEL;
204
205 if (m_windowStyle & wxLB_ALWAYS_SB)
206 wstyle |= LBS_DISABLENOSCROLL;
207 if (m_windowStyle & wxLB_HSCROLL)
208 wstyle |= WS_HSCROLL;
209 if (m_windowStyle & wxLB_SORT)
210 wstyle |= LBS_SORT;
211
212 #if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
213 if ( m_windowStyle & wxLB_OWNERDRAW ) {
214 // we don't support LBS_OWNERDRAWVARIABLE yet
215 wstyle |= LBS_OWNERDRAWFIXED;
216 }
217 #endif
218
219 // Without this style, you get unexpected heights, so e.g. constraint layout
220 // doesn't work properly
221 wstyle |= LBS_NOINTEGRALHEIGHT;
222
223 WXDWORD exStyle = 0;
224 (void) MSWGetStyle(m_windowStyle, & exStyle) ;
225
226 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL,
227 wstyle ,
228 0, 0, 0, 0,
229 (HWND)parent->GetHWND(), (HMENU)m_windowId,
230 wxGetInstance(), NULL);
231
232 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") );
233
234 // Subclass again to catch messages
235 SubclassWin(m_hWnd);
236
237 size_t ui;
238 for (ui = 0; ui < (size_t)n; ui++) {
239 Append(choices[ui]);
240 }
241
242 if ( (m_windowStyle & wxLB_MULTIPLE) == 0 )
243 SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0);
244
245 SetFont(parent->GetFont());
246
247 SetSize(x, y, width, height);
248
249 return TRUE;
250 }
251
252 wxListBox::~wxListBox()
253 {
254 Free();
255 }
256
257 void wxListBox::SetupColours()
258 {
259 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
260 SetForegroundColour(GetParent()->GetForegroundColour());
261 }
262
263 // ----------------------------------------------------------------------------
264 // implementation of wxListBoxBase methods
265 // ----------------------------------------------------------------------------
266
267 void wxListBox::DoSetFirstItem(int N)
268 {
269 wxCHECK_RET( N >= 0 && N < m_noItems,
270 wxT("invalid index in wxListBox::SetFirstItem") );
271
272 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
273 }
274
275 void wxListBox::Delete(int N)
276 {
277 wxCHECK_RET( N >= 0 && N < m_noItems,
278 wxT("invalid index in wxListBox::Delete") );
279
280 // for owner drawn objects, the data is used for storing wxOwnerDrawn
281 // pointers and we shouldn't touch it
282 #if !wxUSE_OWNER_DRAWN
283 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
284 #endif // !wxUSE_OWNER_DRAWN
285 if ( HasClientObjectData() )
286 {
287 delete GetClientObject(N);
288 }
289
290 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
291 m_noItems--;
292
293 SetHorizontalExtent(wxEmptyString);
294 }
295
296 int wxListBox::DoAppend(const wxString& item)
297 {
298 int index = ListBox_AddString(GetHwnd(), item);
299 m_noItems++;
300
301 #if wxUSE_OWNER_DRAWN
302 if ( m_windowStyle & wxLB_OWNERDRAW ) {
303 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
304 pNewItem->SetName(item);
305 m_aItems.Insert(pNewItem, index);
306 ListBox_SetItemData(GetHwnd(), index, pNewItem);
307 pNewItem->SetFont(GetFont());
308 }
309 #endif // wxUSE_OWNER_DRAWN
310
311 SetHorizontalExtent(item);
312
313 return index;
314 }
315
316 void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
317 {
318 // avoid flicker - but don't need to do this for a hidden listbox
319 bool hideAndShow = IsShown();
320 if ( hideAndShow )
321 {
322 ShowWindow(GetHwnd(), SW_HIDE);
323 }
324
325 ListBox_ResetContent(GetHwnd());
326
327 m_noItems = choices.GetCount();
328 int i;
329 for (i = 0; i < m_noItems; i++)
330 {
331 ListBox_AddString(GetHwnd(), choices[i]);
332 if ( clientData )
333 {
334 SetClientData(i, clientData[i]);
335 }
336 }
337
338 #if wxUSE_OWNER_DRAWN
339 if ( m_windowStyle & wxLB_OWNERDRAW ) {
340 // first delete old items
341 WX_CLEAR_ARRAY(m_aItems);
342
343 // then create new ones
344 for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) {
345 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
346 pNewItem->SetName(choices[ui]);
347 m_aItems.Add(pNewItem);
348 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
349 }
350 }
351 #endif // wxUSE_OWNER_DRAWN
352
353 SetHorizontalExtent();
354
355 if ( hideAndShow )
356 {
357 // show the listbox back if we hid it
358 ShowWindow(GetHwnd(), SW_SHOW);
359 }
360 }
361
362 int wxListBox::FindString(const wxString& s) const
363 {
364 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
365 if (pos == LB_ERR)
366 return wxNOT_FOUND;
367 else
368 return pos;
369 }
370
371 void wxListBox::Clear()
372 {
373 Free();
374
375 ListBox_ResetContent(GetHwnd());
376
377 m_noItems = 0;
378 SetHorizontalExtent();
379 }
380
381 void wxListBox::Free()
382 {
383 #if wxUSE_OWNER_DRAWN
384 if ( m_windowStyle & wxLB_OWNERDRAW )
385 {
386 WX_CLEAR_ARRAY(m_aItems);
387 }
388 else
389 #endif // wxUSE_OWNER_DRAWN
390 if ( HasClientObjectData() )
391 {
392 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
393 {
394 delete GetClientObject(n);
395 }
396 }
397 }
398
399 void wxListBox::SetSelection(int N, bool select)
400 {
401 wxCHECK_RET( N >= 0 && N < m_noItems,
402 wxT("invalid index in wxListBox::SetSelection") );
403
404 if ( HasMultipleSelection() )
405 {
406 SendMessage(GetHwnd(), LB_SETSEL, select, N);
407 }
408 else
409 {
410 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
411 }
412 }
413
414 bool wxListBox::IsSelected(int N) const
415 {
416 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
417 wxT("invalid index in wxListBox::Selected") );
418
419 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
420 }
421
422 wxClientData* wxListBox::DoGetItemClientObject(int n) const
423 {
424 return (wxClientData *)DoGetItemClientData(n);
425 }
426
427 void *wxListBox::DoGetItemClientData(int n) const
428 {
429 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
430 wxT("invalid index in wxListBox::GetClientData") );
431
432 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
433 }
434
435 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
436 {
437 DoSetItemClientData(n, clientData);
438 }
439
440 void wxListBox::DoSetItemClientData(int n, void *clientData)
441 {
442 wxCHECK_RET( n >= 0 && n < m_noItems,
443 wxT("invalid index in wxListBox::SetClientData") );
444
445 #if wxUSE_OWNER_DRAWN
446 if ( m_windowStyle & wxLB_OWNERDRAW )
447 {
448 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
449 // in OnMeasure/OnDraw.
450 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
451 }
452 #endif // wxUSE_OWNER_DRAWN
453
454 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
455 wxLogDebug(wxT("LB_SETITEMDATA failed"));
456 }
457
458 // Return number of selections and an array of selected integers
459 int wxListBox::GetSelections(wxArrayInt& aSelections) const
460 {
461 aSelections.Empty();
462
463 if ( HasMultipleSelection() )
464 {
465 int countSel = ListBox_GetSelCount(GetHwnd());
466 if ( countSel == LB_ERR )
467 {
468 wxLogDebug(_T("ListBox_GetSelCount failed"));
469 }
470 else if ( countSel != 0 )
471 {
472 int *selections = new int[countSel];
473
474 if ( ListBox_GetSelItems(GetHwnd(),
475 countSel, selections) == LB_ERR )
476 {
477 wxLogDebug(wxT("ListBox_GetSelItems failed"));
478 countSel = -1;
479 }
480 else
481 {
482 aSelections.Alloc(countSel);
483 for ( int n = 0; n < countSel; n++ )
484 aSelections.Add(selections[n]);
485 }
486
487 delete [] selections;
488 }
489
490 return countSel;
491 }
492 else // single-selection listbox
493 {
494 if (ListBox_GetCurSel(GetHwnd()) > -1)
495 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
496
497 return aSelections.Count();
498 }
499 }
500
501 // Get single selection, for single choice list items
502 int wxListBox::GetSelection() const
503 {
504 wxCHECK_MSG( !HasMultipleSelection(),
505 -1,
506 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
507
508 return ListBox_GetCurSel(GetHwnd());
509 }
510
511 // Find string for position
512 wxString wxListBox::GetString(int N) const
513 {
514 wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString,
515 wxT("invalid index in wxListBox::GetClientData") );
516
517 int len = ListBox_GetTextLen(GetHwnd(), N);
518
519 // +1 for terminating NUL
520 wxString result;
521 ListBox_GetText(GetHwnd(), N, wxStringBuffer(result, len + 1));
522
523 return result;
524 }
525
526 void
527 wxListBox::DoInsertItems(const wxArrayString& items, int pos)
528 {
529 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
530 wxT("invalid index in wxListBox::InsertItems") );
531
532 int nItems = items.GetCount();
533 for ( int i = 0; i < nItems; i++ )
534 {
535 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
536
537 #if wxUSE_OWNER_DRAWN
538 if ( m_windowStyle & wxLB_OWNERDRAW )
539 {
540 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
541 pNewItem->SetName(items[i]);
542 pNewItem->SetFont(GetFont());
543 m_aItems.Insert(pNewItem, idx);
544
545 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
546 }
547 #endif // wxUSE_OWNER_DRAWN
548 }
549
550 m_noItems += nItems;
551
552 SetHorizontalExtent();
553 }
554
555 void wxListBox::SetString(int N, const wxString& s)
556 {
557 wxCHECK_RET( N >= 0 && N < m_noItems,
558 wxT("invalid index in wxListBox::SetString") );
559
560 // remember the state of the item
561 bool wasSelected = IsSelected(N);
562
563 void *oldData = NULL;
564 wxClientData *oldObjData = NULL;
565 if ( m_clientDataItemsType == wxClientData_Void )
566 oldData = GetClientData(N);
567 else if ( m_clientDataItemsType == wxClientData_Object )
568 oldObjData = GetClientObject(N);
569
570 // delete and recreate it
571 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
572
573 int newN = N;
574 if ( N == m_noItems - 1 )
575 newN = -1;
576
577 ListBox_InsertString(GetHwnd(), newN, s);
578
579 // restore the client data
580 if ( oldData )
581 SetClientData(N, oldData);
582 else if ( oldObjData )
583 SetClientObject(N, oldObjData);
584
585 // we may have lost the selection
586 if ( wasSelected )
587 Select(N);
588
589 #if wxUSE_OWNER_DRAWN
590 if ( m_windowStyle & wxLB_OWNERDRAW )
591 {
592 // update item's text
593 m_aItems[N]->SetName(s);
594
595 // reassign the item's data
596 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
597 }
598 #endif //USE_OWNER_DRAWN
599 }
600
601 int wxListBox::GetCount() const
602 {
603 return m_noItems;
604 }
605
606 // ----------------------------------------------------------------------------
607 // helpers
608 // ----------------------------------------------------------------------------
609
610 // Windows-specific code to set the horizontal extent of the listbox, if
611 // necessary. If s is non-NULL, it's used to calculate the horizontal extent.
612 // Otherwise, all strings are used.
613 void wxListBox::SetHorizontalExtent(const wxString& s)
614 {
615 // Only necessary if we want a horizontal scrollbar
616 if (!(m_windowStyle & wxHSCROLL))
617 return;
618 TEXTMETRIC lpTextMetric;
619
620 if ( !s.IsEmpty() )
621 {
622 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
623 HDC dc = GetWindowDC(GetHwnd());
624 HFONT oldFont = 0;
625 if (GetFont().Ok() && GetFont().GetResourceHandle())
626 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
627
628 GetTextMetrics(dc, &lpTextMetric);
629 SIZE extentXY;
630 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
631 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
632
633 if (oldFont)
634 ::SelectObject(dc, oldFont);
635
636 ReleaseDC(GetHwnd(), dc);
637 if (extentX > existingExtent)
638 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
639 }
640 else
641 {
642 int largestExtent = 0;
643 HDC dc = GetWindowDC(GetHwnd());
644 HFONT oldFont = 0;
645 if (GetFont().Ok() && GetFont().GetResourceHandle())
646 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
647
648 GetTextMetrics(dc, &lpTextMetric);
649
650 // FIXME: buffer overflow!!
651 wxChar buf[1024];
652 for (int i = 0; i < m_noItems; i++)
653 {
654 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf);
655 buf[len] = 0;
656 SIZE extentXY;
657 ::GetTextExtentPoint(dc, buf, len, &extentXY);
658 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
659 if (extentX > largestExtent)
660 largestExtent = extentX;
661 }
662 if (oldFont)
663 ::SelectObject(dc, oldFont);
664
665 ReleaseDC(GetHwnd(), dc);
666 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
667 }
668 }
669
670 wxSize wxListBox::DoGetBestSize() const
671 {
672 // find the widest string
673 int wLine;
674 int wListbox = 0;
675 for ( int i = 0; i < m_noItems; i++ )
676 {
677 wxString str(GetString(i));
678 GetTextExtent(str, &wLine, NULL);
679 if ( wLine > wListbox )
680 wListbox = wLine;
681 }
682
683 // give it some reasonable default value if there are no strings in the
684 // list
685 if ( wListbox == 0 )
686 wListbox = 100;
687
688 // the listbox should be slightly larger than the widest string
689 int cx, cy;
690 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
691
692 wListbox += 3*cx;
693
694 // don't make the listbox too tall (limit height to 10 items) but don't
695 // make it too small neither
696 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
697 wxMin(wxMax(m_noItems, 3), 10);
698
699 return wxSize(wListbox, hListbox);
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.m_commandInt = 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(wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT));
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 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
810 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
811
812 return pItem->OnDrawItem(dc, rect,
813 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
814 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
815 }
816
817 #endif // wxUSE_OWNER_DRAWN
818
819 #endif // wxUSE_LISTBOX