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