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