]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
two fixes from Justin Bradford
[wxWidgets.git] / src / msw / listbox.cpp
CommitLineData
2bda0e17 1///////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/msw/listbox.cpp
2bda0e17
KB
3// Purpose: wxListBox
4// Author: Julian Smart
5// Modified by: Vadim Zeitlin (owner drawn stuff)
dd3c394a 6// Created:
2bda0e17
KB
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
6c9a19aa 9// Licence: wxWindows licence
2bda0e17
KB
10///////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
dd3c394a 13 #pragma implementation "listbox.h"
2bda0e17
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
dd3c394a 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
1e6feb95 23#if wxUSE_LISTBOX
0c589ad0 24
2bda0e17 25#ifndef WX_PRECOMP
0c589ad0
BM
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"
2662e49e 31#include "wx/utils.h"
2bda0e17
KB
32#endif
33
1e6feb95
VZ
34#include "wx/window.h"
35#include "wx/msw/private.h"
36
5ea105e0
RR
37#include <windowsx.h>
38
0c589ad0
BM
39#include "wx/dynarray.h"
40#include "wx/log.h"
2bda0e17 41
0c589ad0
BM
42#if wxUSE_OWNER_DRAWN
43 #include "wx/ownerdrw.h"
44#endif
2bda0e17 45
b39dbf34
JS
46#ifdef __GNUWIN32_OLD__
47 #include "wx/msw/gnuwin32/extra.h"
57c208c5 48#endif
2bda0e17 49
9c9c3d7a 50IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
2bda0e17 51
066f1b7a
SC
52/*
53TODO PROPERTIES
54 selection
55 content
56 item
57*/
58
2bda0e17
KB
59// ============================================================================
60// list box item declaration and implementation
61// ============================================================================
62
47d67540 63#if wxUSE_OWNER_DRAWN
2bda0e17
KB
64
65class wxListBoxItem : public wxOwnerDrawn
66{
67public:
2b5f62a0 68 wxListBoxItem(const wxString& str = wxEmptyString);
2bda0e17
KB
69};
70
dd3c394a 71wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
2bda0e17 72{
dd3c394a
VZ
73 // no bitmaps/checkmarks
74 SetMarginWidth(0);
2bda0e17
KB
75}
76
2b5f62a0 77wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
2bda0e17 78{
dd3c394a 79 return new wxListBoxItem();
2bda0e17
KB
80}
81
82#endif //USE_OWNER_DRAWN
83
84// ============================================================================
85// list box control implementation
86// ============================================================================
87
2ee3ee1b
VZ
88// ----------------------------------------------------------------------------
89// creation
90// ----------------------------------------------------------------------------
dd3c394a 91
2ee3ee1b 92// Listbox item
bfc6fde4 93wxListBox::wxListBox()
2bda0e17 94{
dd3c394a
VZ
95 m_noItems = 0;
96 m_selected = 0;
2bda0e17
KB
97}
98
dd3c394a
VZ
99bool wxListBox::Create(wxWindow *parent,
100 wxWindowID id,
2bda0e17
KB
101 const wxPoint& pos,
102 const wxSize& size,
debe6624
JS
103 int n, const wxString choices[],
104 long style,
2bda0e17
KB
105 const wxValidator& validator,
106 const wxString& name)
107{
dd3c394a
VZ
108 m_noItems = 0;
109 m_hWnd = 0;
110 m_selected = 0;
2bda0e17 111
dd3c394a 112 SetName(name);
11b6a93b 113#if wxUSE_VALIDATORS
dd3c394a 114 SetValidator(validator);
11b6a93b 115#endif // wxUSE_VALIDATORS
2bda0e17 116
dd3c394a
VZ
117 if (parent)
118 parent->AddChild(this);
2bda0e17 119
7516ed26 120 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
dd3c394a 121 SetForegroundColour(parent->GetForegroundColour());
2bda0e17 122
dd3c394a 123 m_windowId = ( id == -1 ) ? (int)NewControlId() : id;
2bda0e17 124
dd3c394a
VZ
125 int x = pos.x;
126 int y = pos.y;
127 int width = size.x;
128 int height = size.y;
129 m_windowStyle = style;
2bda0e17 130
fe3d9123
JS
131 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_TABSTOP |
132 LBS_NOTIFY | LBS_HASSTRINGS ;
54081dc5
VZ
133
134 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
135 _T("only one of listbox selection modes can be specified") );
c76b1a30
JS
136
137 if ( (m_windowStyle & wxBORDER_MASK) == wxBORDER_DEFAULT )
138 m_windowStyle |= wxBORDER_SUNKEN;
139
b0766406
JS
140 if ( m_windowStyle & wxCLIP_SIBLINGS )
141 wstyle |= WS_CLIPSIBLINGS;
54081dc5 142
dd3c394a
VZ
143 if (m_windowStyle & wxLB_MULTIPLE)
144 wstyle |= LBS_MULTIPLESEL;
145 else if (m_windowStyle & wxLB_EXTENDED)
146 wstyle |= LBS_EXTENDEDSEL;
2bda0e17 147
dd3c394a 148 if (m_windowStyle & wxLB_ALWAYS_SB)
2ee3ee1b 149 wstyle |= LBS_DISABLENOSCROLL;
dd3c394a
VZ
150 if (m_windowStyle & wxLB_HSCROLL)
151 wstyle |= WS_HSCROLL;
152 if (m_windowStyle & wxLB_SORT)
153 wstyle |= LBS_SORT;
2bda0e17 154
4676948b 155#if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
2bda0e17 156 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a
VZ
157 // we don't support LBS_OWNERDRAWVARIABLE yet
158 wstyle |= LBS_OWNERDRAWFIXED;
2bda0e17 159 }
2bda0e17
KB
160#endif
161
dd3c394a
VZ
162 // Without this style, you get unexpected heights, so e.g. constraint layout
163 // doesn't work properly
164 wstyle |= LBS_NOINTEGRALHEIGHT;
165
fe3d9123 166 WXDWORD exStyle = 0;
22a746d7 167 (void) MSWGetStyle(m_windowStyle, & exStyle) ;
2bda0e17 168
223d09f6 169 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL,
fe3d9123 170 wstyle ,
dd3c394a
VZ
171 0, 0, 0, 0,
172 (HWND)parent->GetHWND(), (HMENU)m_windowId,
173 wxGetInstance(), NULL);
1c089c47 174
223d09f6 175 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") );
1c089c47 176
dd3c394a
VZ
177 // Subclass again to catch messages
178 SubclassWin(m_hWnd);
2bda0e17 179
dd3c394a
VZ
180 size_t ui;
181 for (ui = 0; ui < (size_t)n; ui++) {
182 Append(choices[ui]);
2bda0e17 183 }
2bda0e17 184
dd3c394a 185 if ( (m_windowStyle & wxLB_MULTIPLE) == 0 )
a23fd0e1 186 SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0);
2bda0e17 187
dd3c394a 188 SetFont(parent->GetFont());
2bda0e17 189
dd3c394a 190 SetSize(x, y, width, height);
2bda0e17 191
dd3c394a 192 return TRUE;
2bda0e17
KB
193}
194
bfc6fde4 195wxListBox::~wxListBox()
2bda0e17 196{
baccb514 197 Free();
2bda0e17
KB
198}
199
bfc6fde4 200void wxListBox::SetupColours()
2bda0e17 201{
a756f210 202 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
dd3c394a 203 SetForegroundColour(GetParent()->GetForegroundColour());
2bda0e17
KB
204}
205
2ee3ee1b
VZ
206// ----------------------------------------------------------------------------
207// implementation of wxListBoxBase methods
208// ----------------------------------------------------------------------------
209
210void wxListBox::DoSetFirstItem(int N)
2bda0e17 211{
dd3c394a 212 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 213 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 214
2ee3ee1b 215 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
216}
217
debe6624 218void wxListBox::Delete(int N)
2bda0e17 219{
dd3c394a 220 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 221 wxT("invalid index in wxListBox::Delete") );
dd3c394a 222
07cf98cb
VZ
223 // for owner drawn objects, the data is used for storing wxOwnerDrawn
224 // pointers and we shouldn't touch it
225#if !wxUSE_OWNER_DRAWN
226 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
227#endif // !wxUSE_OWNER_DRAWN
228 if ( HasClientObjectData() )
229 {
230 delete GetClientObject(N);
231 }
6c8a980f 232
a23fd0e1 233 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
dd3c394a
VZ
234 m_noItems--;
235
2b5f62a0 236 SetHorizontalExtent(wxEmptyString);
2bda0e17
KB
237}
238
2ee3ee1b 239int wxListBox::DoAppend(const wxString& item)
2bda0e17 240{
a23fd0e1 241 int index = ListBox_AddString(GetHwnd(), item);
2ee3ee1b 242 m_noItems++;
2bda0e17 243
47d67540 244#if wxUSE_OWNER_DRAWN
2bda0e17 245 if ( m_windowStyle & wxLB_OWNERDRAW ) {
2b5f62a0 246 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
dd3c394a 247 pNewItem->SetName(item);
fd7ab28c 248 m_aItems.Insert(pNewItem, index);
a23fd0e1 249 ListBox_SetItemData(GetHwnd(), index, pNewItem);
60c65519 250 pNewItem->SetFont(GetFont());
2bda0e17 251 }
fd7ab28c 252#endif // wxUSE_OWNER_DRAWN
2bda0e17 253
dd3c394a 254 SetHorizontalExtent(item);
dd3c394a 255
2ee3ee1b 256 return index;
2bda0e17
KB
257}
258
2ee3ee1b 259void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
2bda0e17 260{
f6bcfd97
BP
261 // avoid flicker - but don't need to do this for a hidden listbox
262 bool hideAndShow = IsShown();
263 if ( hideAndShow )
264 {
265 ShowWindow(GetHwnd(), SW_HIDE);
266 }
2ee3ee1b 267
a23fd0e1 268 ListBox_ResetContent(GetHwnd());
2ee3ee1b
VZ
269
270 m_noItems = choices.GetCount();
dd3c394a 271 int i;
2ee3ee1b 272 for (i = 0; i < m_noItems; i++)
dd3c394a 273 {
a23fd0e1 274 ListBox_AddString(GetHwnd(), choices[i]);
dd3c394a 275 if ( clientData )
2ee3ee1b 276 {
2b5f62a0 277 SetClientData(i, clientData[i]);
2ee3ee1b 278 }
dd3c394a 279 }
2bda0e17 280
47d67540 281#if wxUSE_OWNER_DRAWN
2bda0e17 282 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a 283 // first delete old items
fd7ab28c 284 WX_CLEAR_ARRAY(m_aItems);
dd3c394a
VZ
285
286 // then create new ones
fd7ab28c 287 for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) {
2b5f62a0 288 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
dd3c394a
VZ
289 pNewItem->SetName(choices[ui]);
290 m_aItems.Add(pNewItem);
a23fd0e1 291 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
dd3c394a 292 }
2bda0e17 293 }
2ee3ee1b
VZ
294#endif // wxUSE_OWNER_DRAWN
295
296 SetHorizontalExtent();
2bda0e17 297
f6bcfd97
BP
298 if ( hideAndShow )
299 {
300 // show the listbox back if we hid it
301 ShowWindow(GetHwnd(), SW_SHOW);
302 }
2bda0e17
KB
303}
304
305int wxListBox::FindString(const wxString& s) const
306{
a23fd0e1 307 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
dd3c394a 308 if (pos == LB_ERR)
2ee3ee1b 309 return wxNOT_FOUND;
dd3c394a
VZ
310 else
311 return pos;
2bda0e17
KB
312}
313
bfc6fde4 314void wxListBox::Clear()
2bda0e17 315{
baccb514 316 Free();
8ee9d618
VZ
317
318 ListBox_ResetContent(GetHwnd());
319
320 m_noItems = 0;
321 SetHorizontalExtent();
322}
323
324void wxListBox::Free()
2b273975 325{
dd3c394a 326#if wxUSE_OWNER_DRAWN
185fa6bf
VZ
327 if ( m_windowStyle & wxLB_OWNERDRAW )
328 {
fd7ab28c 329 WX_CLEAR_ARRAY(m_aItems);
185fa6bf
VZ
330 }
331 else
332#endif // wxUSE_OWNER_DRAWN
6c8a980f
VZ
333 if ( HasClientObjectData() )
334 {
335 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
336 {
337 delete GetClientObject(n);
338 }
339 }
2bda0e17 340}
baccb514 341
debe6624 342void wxListBox::SetSelection(int N, bool select)
2bda0e17 343{
dd3c394a 344 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 345 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 346
2ee3ee1b
VZ
347 if ( HasMultipleSelection() )
348 {
a23fd0e1 349 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 350 }
dd3c394a
VZ
351 else
352 {
2ee3ee1b 353 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 354 }
2bda0e17
KB
355}
356
2ee3ee1b 357bool wxListBox::IsSelected(int N) const
2bda0e17 358{
dd3c394a 359 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
223d09f6 360 wxT("invalid index in wxListBox::Selected") );
dd3c394a 361
a23fd0e1 362 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
2bda0e17
KB
363}
364
6c8a980f 365wxClientData* wxListBox::DoGetItemClientObject(int n) const
2bda0e17 366{
6c8a980f 367 return (wxClientData *)DoGetItemClientData(n);
2bda0e17
KB
368}
369
6c8a980f 370void *wxListBox::DoGetItemClientData(int n) const
2bda0e17 371{
2ee3ee1b 372 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
223d09f6 373 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 374
2ee3ee1b 375 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
376}
377
6c8a980f 378void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
2bda0e17 379{
6c8a980f 380 DoSetItemClientData(n, clientData);
2ee3ee1b
VZ
381}
382
6c8a980f 383void wxListBox::DoSetItemClientData(int n, void *clientData)
2ee3ee1b
VZ
384{
385 wxCHECK_RET( n >= 0 && n < m_noItems,
223d09f6 386 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 387
2ee3ee1b
VZ
388#if wxUSE_OWNER_DRAWN
389 if ( m_windowStyle & wxLB_OWNERDRAW )
390 {
391 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
392 // in OnMeasure/OnDraw.
393 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
394 }
395#endif // wxUSE_OWNER_DRAWN
396
397 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
223d09f6 398 wxLogDebug(wxT("LB_SETITEMDATA failed"));
2bda0e17
KB
399}
400
401// Return number of selections and an array of selected integers
14483330 402int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 403{
dd3c394a 404 aSelections.Empty();
14483330 405
2ee3ee1b 406 if ( HasMultipleSelection() )
dd3c394a 407 {
d90879fa
VZ
408 int countSel = ListBox_GetSelCount(GetHwnd());
409 if ( countSel == LB_ERR )
410 {
411 wxLogDebug(_T("ListBox_GetSelCount failed"));
412 }
413 else if ( countSel != 0 )
414 {
415 int *selections = new int[countSel];
14483330 416
d90879fa
VZ
417 if ( ListBox_GetSelItems(GetHwnd(),
418 countSel, selections) == LB_ERR )
419 {
420 wxLogDebug(wxT("ListBox_GetSelItems failed"));
421 countSel = -1;
422 }
423 else
424 {
425 aSelections.Alloc(countSel);
426 for ( int n = 0; n < countSel; n++ )
427 aSelections.Add(selections[n]);
428 }
14483330 429
dd3c394a
VZ
430 delete [] selections;
431 }
14483330 432
d90879fa 433 return countSel;
dd3c394a
VZ
434 }
435 else // single-selection listbox
436 {
f6bcfd97
BP
437 if (ListBox_GetCurSel(GetHwnd()) > -1)
438 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 439
f6bcfd97 440 return aSelections.Count();
dd3c394a 441 }
2bda0e17
KB
442}
443
444// Get single selection, for single choice list items
14483330 445int wxListBox::GetSelection() const
2bda0e17 446{
2ee3ee1b 447 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 448 -1,
f6bcfd97 449 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
14483330 450
a23fd0e1 451 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
452}
453
454// Find string for position
debe6624 455wxString wxListBox::GetString(int N) const
2bda0e17 456{
2b5f62a0 457 wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString,
223d09f6 458 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 459
a23fd0e1 460 int len = ListBox_GetTextLen(GetHwnd(), N);
dd3c394a
VZ
461
462 // +1 for terminating NUL
463 wxString result;
de564874 464 ListBox_GetText(GetHwnd(), N, wxStringBuffer(result, len + 1));
dd3c394a
VZ
465
466 return result;
2bda0e17
KB
467}
468
2ee3ee1b
VZ
469void
470wxListBox::DoInsertItems(const wxArrayString& items, int pos)
471{
472 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
473 wxT("invalid index in wxListBox::InsertItems") );
474
475 int nItems = items.GetCount();
476 for ( int i = 0; i < nItems; i++ )
fd7ab28c
VZ
477 {
478 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
479
480#if wxUSE_OWNER_DRAWN
9085d634
RD
481 if ( m_windowStyle & wxLB_OWNERDRAW )
482 {
2b5f62a0 483 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
9085d634
RD
484 pNewItem->SetName(items[i]);
485 pNewItem->SetFont(GetFont());
486 m_aItems.Insert(pNewItem, idx);
fd7ab28c 487
9085d634
RD
488 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
489 }
fd7ab28c
VZ
490#endif // wxUSE_OWNER_DRAWN
491 }
492
2ee3ee1b
VZ
493 m_noItems += nItems;
494
495 SetHorizontalExtent();
496}
497
498void wxListBox::SetString(int N, const wxString& s)
499{
500 wxCHECK_RET( N >= 0 && N < m_noItems,
501 wxT("invalid index in wxListBox::SetString") );
502
503 // remember the state of the item
504 bool wasSelected = IsSelected(N);
505
506 void *oldData = NULL;
507 wxClientData *oldObjData = NULL;
1e6feb95 508 if ( m_clientDataItemsType == wxClientData_Void )
2ee3ee1b 509 oldData = GetClientData(N);
1e6feb95 510 else if ( m_clientDataItemsType == wxClientData_Object )
2ee3ee1b
VZ
511 oldObjData = GetClientObject(N);
512
513 // delete and recreate it
514 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
515
516 int newN = N;
517 if ( N == m_noItems - 1 )
518 newN = -1;
519
520 ListBox_InsertString(GetHwnd(), newN, s);
521
522 // restore the client data
523 if ( oldData )
524 SetClientData(N, oldData);
525 else if ( oldObjData )
526 SetClientObject(N, oldObjData);
527
528 // we may have lost the selection
529 if ( wasSelected )
530 Select(N);
531
532#if wxUSE_OWNER_DRAWN
533 if ( m_windowStyle & wxLB_OWNERDRAW )
c294f450 534 {
2ee3ee1b
VZ
535 // update item's text
536 m_aItems[N]->SetName(s);
fd7ab28c 537
c294f450
RD
538 // reassign the item's data
539 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
540 }
2ee3ee1b
VZ
541#endif //USE_OWNER_DRAWN
542}
543
544int wxListBox::GetCount() const
545{
546 return m_noItems;
547}
548
549// ----------------------------------------------------------------------------
550// helpers
551// ----------------------------------------------------------------------------
552
4438caf4
VZ
553// Windows-specific code to set the horizontal extent of the listbox, if
554// necessary. If s is non-NULL, it's used to calculate the horizontal extent.
2bda0e17
KB
555// Otherwise, all strings are used.
556void wxListBox::SetHorizontalExtent(const wxString& s)
557{
dd3c394a
VZ
558 // Only necessary if we want a horizontal scrollbar
559 if (!(m_windowStyle & wxHSCROLL))
560 return;
561 TEXTMETRIC lpTextMetric;
562
2ee3ee1b 563 if ( !s.IsEmpty() )
2bda0e17 564 {
a23fd0e1
VZ
565 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
566 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
567 HFONT oldFont = 0;
568 if (GetFont().Ok() && GetFont().GetResourceHandle())
569 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
570
571 GetTextMetrics(dc, &lpTextMetric);
572 SIZE extentXY;
837e5743 573 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
dd3c394a
VZ
574 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
575
576 if (oldFont)
577 ::SelectObject(dc, oldFont);
578
a23fd0e1 579 ReleaseDC(GetHwnd(), dc);
dd3c394a 580 if (extentX > existingExtent)
a23fd0e1 581 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
dd3c394a
VZ
582 }
583 else
584 {
585 int largestExtent = 0;
a23fd0e1 586 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
587 HFONT oldFont = 0;
588 if (GetFont().Ok() && GetFont().GetResourceHandle())
589 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
590
591 GetTextMetrics(dc, &lpTextMetric);
3e3be693
VZ
592
593 // FIXME: buffer overflow!!
594 wxChar buf[1024];
595 for (int i = 0; i < m_noItems; i++)
dd3c394a 596 {
3e3be693
VZ
597 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf);
598 buf[len] = 0;
dd3c394a 599 SIZE extentXY;
3e3be693 600 ::GetTextExtentPoint(dc, buf, len, &extentXY);
dd3c394a
VZ
601 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
602 if (extentX > largestExtent)
603 largestExtent = extentX;
604 }
605 if (oldFont)
606 ::SelectObject(dc, oldFont);
607
a23fd0e1
VZ
608 ReleaseDC(GetHwnd(), dc);
609 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
2bda0e17 610 }
2bda0e17
KB
611}
612
f68586e5 613wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
614{
615 // find the widest string
616 int wLine;
617 int wListbox = 0;
618 for ( int i = 0; i < m_noItems; i++ )
619 {
620 wxString str(GetString(i));
621 GetTextExtent(str, &wLine, NULL);
622 if ( wLine > wListbox )
623 wListbox = wLine;
624 }
625
626 // give it some reasonable default value if there are no strings in the
627 // list
628 if ( wListbox == 0 )
629 wListbox = 100;
630
631 // the listbox should be slightly larger than the widest string
632 int cx, cy;
633 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
634
635 wListbox += 3*cx;
636
5b6d6b70
VZ
637 // don't make the listbox too tall (limit height to 10 items) but don't
638 // make it too small neither
639 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
640 wxMin(wxMax(m_noItems, 3), 10);
b908d224
VZ
641
642 return wxSize(wListbox, hListbox);
643}
644
2ee3ee1b
VZ
645// ----------------------------------------------------------------------------
646// callbacks
647// ----------------------------------------------------------------------------
648
649bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 650{
bada28f0 651 wxEventType evtType;
8ee9d618 652 if ( param == LBN_SELCHANGE )
2bda0e17 653 {
bada28f0 654 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
2bda0e17 655 }
8ee9d618 656 else if ( param == LBN_DBLCLK )
2ee3ee1b 657 {
bada28f0
VZ
658 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
659 }
660 else
661 {
662 // some event we're not interested in
663 return FALSE;
664 }
665
666 wxCommandEvent event(evtType, m_windowId);
667 event.SetEventObject( this );
8ee9d618 668
9c9c3d7a
VZ
669 // retrieve the affected item
670 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
671 if ( n != LB_ERR )
bada28f0 672 {
bada28f0
VZ
673 if ( HasClientObjectData() )
674 event.SetClientObject( GetClientObject(n) );
675 else if ( HasClientUntypedData() )
676 event.SetClientData( GetClientData(n) );
9c9c3d7a 677
bada28f0 678 event.SetString( GetString(n) );
9c9c3d7a 679 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : TRUE );
bada28f0
VZ
680 }
681
682 event.m_commandInt = n;
2ee3ee1b 683
bada28f0 684 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
685}
686
2ee3ee1b
VZ
687// ----------------------------------------------------------------------------
688// wxCheckListBox support
689// ----------------------------------------------------------------------------
2bda0e17 690
47d67540 691#if wxUSE_OWNER_DRAWN
2bda0e17
KB
692
693// drawing
694// -------
695
696// space beneath/above each row in pixels
697// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
698#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
699
700// the height is the same for all items
dd3c394a
VZ
701// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
702
703// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
704// message is not yet sent when we get here!
2bda0e17
KB
705bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
706{
dd3c394a
VZ
707 // only owner-drawn control should receive this message
708 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
2bda0e17 709
dd3c394a 710 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 711
4676948b
JS
712#ifdef __WXWINCE__
713 HDC hdc = GetDC(NULL);
714#else
07cf98cb 715 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
4676948b 716#endif
07cf98cb 717
dd3c394a 718 wxDC dc;
07cf98cb 719 dc.SetHDC((WXHDC)hdc);
a756f210 720 dc.SetFont(wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT));
2bda0e17 721
dd3c394a
VZ
722 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
723 pStruct->itemWidth = dc.GetCharWidth();
2bda0e17 724
07cf98cb
VZ
725 dc.SetHDC(0);
726
727 DeleteDC(hdc);
728
dd3c394a 729 return TRUE;
2bda0e17
KB
730}
731
732// forward the message to the appropriate item
733bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
734{
dd3c394a
VZ
735 // only owner-drawn control should receive this message
736 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
737
738 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
f6bcfd97
BP
739 UINT itemID = pStruct->itemID;
740
741 // the item may be -1 for an empty listbox
742 if ( itemID == (UINT)-1 )
743 return FALSE;
dd3c394a 744
a23fd0e1 745 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
2bda0e17 746
dd3c394a 747 wxCHECK( data && (data != LB_ERR), FALSE );
2bda0e17 748
dd3c394a 749 wxListBoxItem *pItem = (wxListBoxItem *)data;
2bda0e17 750
7561aacd 751 wxDCTemp dc((WXHDC)pStruct->hDC);
dd3c394a 752 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
f6bcfd97 753 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
2bda0e17 754
dd3c394a 755 return pItem->OnDrawItem(dc, rect,
7561aacd
VZ
756 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
757 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
758}
759
1e6feb95
VZ
760#endif // wxUSE_OWNER_DRAWN
761
762#endif // wxUSE_LISTBOX