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