]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
Avoid core dumps when SetImageList is used.
[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
6a89f9ee 50#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
51WX_DEFINE_FLAGS( wxListBoxStyle )
52
3ff066a4 53wxBEGIN_FLAGS( wxListBoxStyle )
bc9fb572
JS
54 // new style border flags, we put them first to
55 // use them for streaming out
3ff066a4
SC
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)
bc9fb572
JS
62
63 // old style border flags
3ff066a4
SC
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)
cb0afb26 69 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
70
71 // standard window styles
3ff066a4
SC
72 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
73 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
74 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
75 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 76 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
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
89wxEND_FLAGS( wxListBoxStyle )
bc9fb572 90
6a89f9ee
SC
91IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h")
92
3ff066a4
SC
93wxBEGIN_PROPERTIES_TABLE(wxListBox)
94 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent )
95 wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent )
c5ca409b 96
3ff066a4
SC
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
101wxEND_PROPERTIES_TABLE()
6a89f9ee 102
3ff066a4
SC
103wxBEGIN_HANDLERS_TABLE(wxListBox)
104wxEND_HANDLERS_TABLE()
6a89f9ee 105
3ff066a4 106wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
6a89f9ee 107#else
9c9c3d7a 108IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
6a89f9ee 109#endif
2bda0e17 110
066f1b7a
SC
111/*
112TODO PROPERTIES
113 selection
114 content
115 item
116*/
117
2bda0e17
KB
118// ============================================================================
119// list box item declaration and implementation
120// ============================================================================
121
47d67540 122#if wxUSE_OWNER_DRAWN
2bda0e17
KB
123
124class wxListBoxItem : public wxOwnerDrawn
125{
126public:
2b5f62a0 127 wxListBoxItem(const wxString& str = wxEmptyString);
2bda0e17
KB
128};
129
dd3c394a 130wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
2bda0e17 131{
dd3c394a
VZ
132 // no bitmaps/checkmarks
133 SetMarginWidth(0);
2bda0e17
KB
134}
135
2b5f62a0 136wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
2bda0e17 137{
dd3c394a 138 return new wxListBoxItem();
2bda0e17
KB
139}
140
141#endif //USE_OWNER_DRAWN
142
143// ============================================================================
144// list box control implementation
145// ============================================================================
146
2ee3ee1b
VZ
147// ----------------------------------------------------------------------------
148// creation
149// ----------------------------------------------------------------------------
dd3c394a 150
2ee3ee1b 151// Listbox item
bfc6fde4 152wxListBox::wxListBox()
2bda0e17 153{
dd3c394a
VZ
154 m_noItems = 0;
155 m_selected = 0;
2bda0e17
KB
156}
157
dd3c394a
VZ
158bool wxListBox::Create(wxWindow *parent,
159 wxWindowID id,
2bda0e17
KB
160 const wxPoint& pos,
161 const wxSize& size,
debe6624
JS
162 int n, const wxString choices[],
163 long style,
ac8d0c11 164 const wxValidator& wxVALIDATOR_PARAM(validator),
2bda0e17
KB
165 const wxString& name)
166{
dd3c394a 167 m_noItems = 0;
dd3c394a 168 m_selected = 0;
2bda0e17 169
7a69cd96
VZ
170 // initialize base class fields
171 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
172 return false;
2bda0e17 173
7a69cd96
VZ
174 // create the native control
175 if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) )
176 {
177 // control creation failed
178 return false;
2bda0e17 179 }
1c089c47 180
7a69cd96
VZ
181 // initialize the contents
182 for ( int i = 0; i < n; i++ )
183 {
184 Append(choices[i]);
2bda0e17 185 }
2bda0e17 186
7a69cd96 187 return true;
2bda0e17
KB
188}
189
584ad2a3
MB
190bool wxListBox::Create(wxWindow *parent,
191 wxWindowID id,
192 const wxPoint& pos,
193 const wxSize& size,
194 const wxArrayString& choices,
195 long style,
76efbc2a 196 const wxValidator& validator,
584ad2a3
MB
197 const wxString& name)
198{
199 wxCArrayString chs(choices);
200 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
201 style, validator, name);
202}
203
bfc6fde4 204wxListBox::~wxListBox()
2bda0e17 205{
baccb514 206 Free();
2bda0e17
KB
207}
208
7a69cd96 209WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
2bda0e17 210{
7a69cd96
VZ
211 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
212
213 // we always want to get the notifications
214 msStyle |= LBS_NOTIFY;
215
216 // without this style, you get unexpected heights, so e.g. constraint
217 // layout doesn't work properly
218 msStyle |= LBS_NOINTEGRALHEIGHT;
219
220 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
221 _T("only one of listbox selection modes can be specified") );
222
223 if ( style & wxLB_MULTIPLE )
224 msStyle |= LBS_MULTIPLESEL;
225 else if ( style & wxLB_EXTENDED )
226 msStyle |= LBS_EXTENDEDSEL;
227
228 if ( m_windowStyle & wxLB_ALWAYS_SB )
229 msStyle |= LBS_DISABLENOSCROLL;
230 if ( m_windowStyle & wxLB_HSCROLL )
231 msStyle |= WS_HSCROLL;
232 if ( m_windowStyle & wxLB_SORT )
233 msStyle |= LBS_SORT;
234
235#if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
236 if ( m_windowStyle & wxLB_OWNERDRAW )
237 {
238 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
239 // the strings in the listbox for simplicity even though we could have
240 // avoided it in this case
241 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
242 }
243#endif // wxUSE_OWNER_DRAWN
244
245 return msStyle;
2bda0e17
KB
246}
247
2ee3ee1b
VZ
248// ----------------------------------------------------------------------------
249// implementation of wxListBoxBase methods
250// ----------------------------------------------------------------------------
251
252void wxListBox::DoSetFirstItem(int N)
2bda0e17 253{
dd3c394a 254 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 255 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 256
2ee3ee1b 257 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
258}
259
debe6624 260void wxListBox::Delete(int N)
2bda0e17 261{
dd3c394a 262 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 263 wxT("invalid index in wxListBox::Delete") );
dd3c394a 264
07cf98cb
VZ
265 // for owner drawn objects, the data is used for storing wxOwnerDrawn
266 // pointers and we shouldn't touch it
267#if !wxUSE_OWNER_DRAWN
268 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
269#endif // !wxUSE_OWNER_DRAWN
270 if ( HasClientObjectData() )
271 {
272 delete GetClientObject(N);
273 }
6c8a980f 274
a23fd0e1 275 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
dd3c394a
VZ
276 m_noItems--;
277
2b5f62a0 278 SetHorizontalExtent(wxEmptyString);
2bda0e17
KB
279}
280
2ee3ee1b 281int wxListBox::DoAppend(const wxString& item)
2bda0e17 282{
a23fd0e1 283 int index = ListBox_AddString(GetHwnd(), item);
2ee3ee1b 284 m_noItems++;
2bda0e17 285
47d67540 286#if wxUSE_OWNER_DRAWN
2bda0e17 287 if ( m_windowStyle & wxLB_OWNERDRAW ) {
2b5f62a0 288 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
dd3c394a 289 pNewItem->SetName(item);
fd7ab28c 290 m_aItems.Insert(pNewItem, index);
a23fd0e1 291 ListBox_SetItemData(GetHwnd(), index, pNewItem);
60c65519 292 pNewItem->SetFont(GetFont());
2bda0e17 293 }
fd7ab28c 294#endif // wxUSE_OWNER_DRAWN
2bda0e17 295
dd3c394a 296 SetHorizontalExtent(item);
dd3c394a 297
2ee3ee1b 298 return index;
2bda0e17
KB
299}
300
2ee3ee1b 301void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
2bda0e17 302{
f6bcfd97
BP
303 // avoid flicker - but don't need to do this for a hidden listbox
304 bool hideAndShow = IsShown();
305 if ( hideAndShow )
306 {
307 ShowWindow(GetHwnd(), SW_HIDE);
308 }
2ee3ee1b 309
a23fd0e1 310 ListBox_ResetContent(GetHwnd());
2ee3ee1b
VZ
311
312 m_noItems = choices.GetCount();
dd3c394a 313 int i;
2ee3ee1b 314 for (i = 0; i < m_noItems; i++)
dd3c394a 315 {
a23fd0e1 316 ListBox_AddString(GetHwnd(), choices[i]);
dd3c394a 317 if ( clientData )
2ee3ee1b 318 {
2b5f62a0 319 SetClientData(i, clientData[i]);
2ee3ee1b 320 }
dd3c394a 321 }
2bda0e17 322
47d67540 323#if wxUSE_OWNER_DRAWN
2bda0e17 324 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a 325 // first delete old items
fd7ab28c 326 WX_CLEAR_ARRAY(m_aItems);
dd3c394a
VZ
327
328 // then create new ones
fd7ab28c 329 for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) {
2b5f62a0 330 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
dd3c394a
VZ
331 pNewItem->SetName(choices[ui]);
332 m_aItems.Add(pNewItem);
a23fd0e1 333 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
dd3c394a 334 }
2bda0e17 335 }
2ee3ee1b
VZ
336#endif // wxUSE_OWNER_DRAWN
337
338 SetHorizontalExtent();
2bda0e17 339
f6bcfd97
BP
340 if ( hideAndShow )
341 {
342 // show the listbox back if we hid it
343 ShowWindow(GetHwnd(), SW_SHOW);
344 }
2bda0e17
KB
345}
346
347int wxListBox::FindString(const wxString& s) const
348{
a23fd0e1 349 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
dd3c394a 350 if (pos == LB_ERR)
2ee3ee1b 351 return wxNOT_FOUND;
dd3c394a
VZ
352 else
353 return pos;
2bda0e17
KB
354}
355
bfc6fde4 356void wxListBox::Clear()
2bda0e17 357{
baccb514 358 Free();
8ee9d618
VZ
359
360 ListBox_ResetContent(GetHwnd());
361
362 m_noItems = 0;
363 SetHorizontalExtent();
364}
365
366void wxListBox::Free()
2b273975 367{
dd3c394a 368#if wxUSE_OWNER_DRAWN
185fa6bf
VZ
369 if ( m_windowStyle & wxLB_OWNERDRAW )
370 {
fd7ab28c 371 WX_CLEAR_ARRAY(m_aItems);
185fa6bf
VZ
372 }
373 else
374#endif // wxUSE_OWNER_DRAWN
6c8a980f
VZ
375 if ( HasClientObjectData() )
376 {
377 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
378 {
379 delete GetClientObject(n);
380 }
381 }
2bda0e17 382}
baccb514 383
debe6624 384void wxListBox::SetSelection(int N, bool select)
2bda0e17 385{
5955710c
JS
386 wxCHECK_RET( N == wxNOT_FOUND ||
387 (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;
de564874 507 ListBox_GetText(GetHwnd(), N, wxStringBuffer(result, len + 1));
dd3c394a
VZ
508
509 return result;
2bda0e17
KB
510}
511
2ee3ee1b
VZ
512void
513wxListBox::DoInsertItems(const wxArrayString& items, int pos)
514{
515 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
516 wxT("invalid index in wxListBox::InsertItems") );
517
518 int nItems = items.GetCount();
519 for ( int i = 0; i < nItems; i++ )
fd7ab28c
VZ
520 {
521 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
522
523#if wxUSE_OWNER_DRAWN
9085d634
RD
524 if ( m_windowStyle & wxLB_OWNERDRAW )
525 {
2b5f62a0 526 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
9085d634
RD
527 pNewItem->SetName(items[i]);
528 pNewItem->SetFont(GetFont());
529 m_aItems.Insert(pNewItem, idx);
fd7ab28c 530
9085d634
RD
531 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
532 }
fd7ab28c
VZ
533#endif // wxUSE_OWNER_DRAWN
534 }
535
2ee3ee1b
VZ
536 m_noItems += nItems;
537
538 SetHorizontalExtent();
539}
540
541void wxListBox::SetString(int N, const wxString& s)
542{
543 wxCHECK_RET( N >= 0 && N < m_noItems,
544 wxT("invalid index in wxListBox::SetString") );
545
546 // remember the state of the item
547 bool wasSelected = IsSelected(N);
548
549 void *oldData = NULL;
550 wxClientData *oldObjData = NULL;
1e6feb95 551 if ( m_clientDataItemsType == wxClientData_Void )
2ee3ee1b 552 oldData = GetClientData(N);
1e6feb95 553 else if ( m_clientDataItemsType == wxClientData_Object )
2ee3ee1b
VZ
554 oldObjData = GetClientObject(N);
555
556 // delete and recreate it
557 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
558
559 int newN = N;
560 if ( N == m_noItems - 1 )
561 newN = -1;
562
563 ListBox_InsertString(GetHwnd(), newN, s);
564
565 // restore the client data
566 if ( oldData )
567 SetClientData(N, oldData);
568 else if ( oldObjData )
569 SetClientObject(N, oldObjData);
570
571 // we may have lost the selection
572 if ( wasSelected )
573 Select(N);
574
575#if wxUSE_OWNER_DRAWN
576 if ( m_windowStyle & wxLB_OWNERDRAW )
c294f450 577 {
2ee3ee1b
VZ
578 // update item's text
579 m_aItems[N]->SetName(s);
fd7ab28c 580
c294f450
RD
581 // reassign the item's data
582 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
583 }
2ee3ee1b
VZ
584#endif //USE_OWNER_DRAWN
585}
586
587int wxListBox::GetCount() const
588{
589 return m_noItems;
590}
591
592// ----------------------------------------------------------------------------
593// helpers
594// ----------------------------------------------------------------------------
595
4438caf4
VZ
596// Windows-specific code to set the horizontal extent of the listbox, if
597// necessary. If s is non-NULL, it's used to calculate the horizontal extent.
2bda0e17
KB
598// Otherwise, all strings are used.
599void wxListBox::SetHorizontalExtent(const wxString& s)
600{
dd3c394a
VZ
601 // Only necessary if we want a horizontal scrollbar
602 if (!(m_windowStyle & wxHSCROLL))
603 return;
604 TEXTMETRIC lpTextMetric;
605
2ee3ee1b 606 if ( !s.IsEmpty() )
2bda0e17 607 {
a23fd0e1
VZ
608 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
609 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
610 HFONT oldFont = 0;
611 if (GetFont().Ok() && GetFont().GetResourceHandle())
612 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
613
614 GetTextMetrics(dc, &lpTextMetric);
615 SIZE extentXY;
837e5743 616 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
dd3c394a
VZ
617 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
618
619 if (oldFont)
620 ::SelectObject(dc, oldFont);
621
a23fd0e1 622 ReleaseDC(GetHwnd(), dc);
dd3c394a 623 if (extentX > existingExtent)
a23fd0e1 624 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
dd3c394a
VZ
625 }
626 else
627 {
628 int largestExtent = 0;
a23fd0e1 629 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
630 HFONT oldFont = 0;
631 if (GetFont().Ok() && GetFont().GetResourceHandle())
632 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
633
634 GetTextMetrics(dc, &lpTextMetric);
3e3be693
VZ
635
636 // FIXME: buffer overflow!!
637 wxChar buf[1024];
638 for (int i = 0; i < m_noItems; i++)
dd3c394a 639 {
3e3be693
VZ
640 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf);
641 buf[len] = 0;
dd3c394a 642 SIZE extentXY;
3e3be693 643 ::GetTextExtentPoint(dc, buf, len, &extentXY);
dd3c394a
VZ
644 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
645 if (extentX > largestExtent)
646 largestExtent = extentX;
647 }
648 if (oldFont)
649 ::SelectObject(dc, oldFont);
650
a23fd0e1
VZ
651 ReleaseDC(GetHwnd(), dc);
652 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
2bda0e17 653 }
2bda0e17
KB
654}
655
f68586e5 656wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
657{
658 // find the widest string
659 int wLine;
660 int wListbox = 0;
661 for ( int i = 0; i < m_noItems; i++ )
662 {
663 wxString str(GetString(i));
664 GetTextExtent(str, &wLine, NULL);
665 if ( wLine > wListbox )
666 wListbox = wLine;
667 }
668
669 // give it some reasonable default value if there are no strings in the
670 // list
671 if ( wListbox == 0 )
672 wListbox = 100;
673
674 // the listbox should be slightly larger than the widest string
675 int cx, cy;
676 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
677
678 wListbox += 3*cx;
679
5b6d6b70
VZ
680 // don't make the listbox too tall (limit height to 10 items) but don't
681 // make it too small neither
682 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
683 wxMin(wxMax(m_noItems, 3), 10);
b908d224
VZ
684
685 return wxSize(wListbox, hListbox);
686}
687
2ee3ee1b
VZ
688// ----------------------------------------------------------------------------
689// callbacks
690// ----------------------------------------------------------------------------
691
692bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 693{
bada28f0 694 wxEventType evtType;
8ee9d618 695 if ( param == LBN_SELCHANGE )
2bda0e17 696 {
bada28f0 697 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
2bda0e17 698 }
8ee9d618 699 else if ( param == LBN_DBLCLK )
2ee3ee1b 700 {
bada28f0
VZ
701 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
702 }
703 else
704 {
705 // some event we're not interested in
706 return FALSE;
707 }
708
709 wxCommandEvent event(evtType, m_windowId);
710 event.SetEventObject( this );
8ee9d618 711
9c9c3d7a
VZ
712 // retrieve the affected item
713 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
714 if ( n != LB_ERR )
bada28f0 715 {
bada28f0
VZ
716 if ( HasClientObjectData() )
717 event.SetClientObject( GetClientObject(n) );
718 else if ( HasClientUntypedData() )
719 event.SetClientData( GetClientData(n) );
9c9c3d7a 720
bada28f0 721 event.SetString( GetString(n) );
9c9c3d7a 722 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : TRUE );
bada28f0
VZ
723 }
724
725 event.m_commandInt = n;
2ee3ee1b 726
bada28f0 727 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
728}
729
2ee3ee1b
VZ
730// ----------------------------------------------------------------------------
731// wxCheckListBox support
732// ----------------------------------------------------------------------------
2bda0e17 733
47d67540 734#if wxUSE_OWNER_DRAWN
2bda0e17
KB
735
736// drawing
737// -------
738
739// space beneath/above each row in pixels
740// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
741#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
742
743// the height is the same for all items
dd3c394a
VZ
744// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
745
746// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
747// message is not yet sent when we get here!
2bda0e17
KB
748bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
749{
dd3c394a
VZ
750 // only owner-drawn control should receive this message
751 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
2bda0e17 752
dd3c394a 753 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 754
4676948b
JS
755#ifdef __WXWINCE__
756 HDC hdc = GetDC(NULL);
757#else
07cf98cb 758 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
4676948b 759#endif
07cf98cb 760
dd3c394a 761 wxDC dc;
07cf98cb 762 dc.SetHDC((WXHDC)hdc);
a756f210 763 dc.SetFont(wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT));
2bda0e17 764
dd3c394a
VZ
765 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
766 pStruct->itemWidth = dc.GetCharWidth();
2bda0e17 767
07cf98cb
VZ
768 dc.SetHDC(0);
769
770 DeleteDC(hdc);
771
dd3c394a 772 return TRUE;
2bda0e17
KB
773}
774
775// forward the message to the appropriate item
776bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
777{
dd3c394a
VZ
778 // only owner-drawn control should receive this message
779 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
780
781 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
f6bcfd97
BP
782 UINT itemID = pStruct->itemID;
783
784 // the item may be -1 for an empty listbox
785 if ( itemID == (UINT)-1 )
786 return FALSE;
dd3c394a 787
a23fd0e1 788 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
2bda0e17 789
dd3c394a 790 wxCHECK( data && (data != LB_ERR), FALSE );
2bda0e17 791
dd3c394a 792 wxListBoxItem *pItem = (wxListBoxItem *)data;
2bda0e17 793
7561aacd 794 wxDCTemp dc((WXHDC)pStruct->hDC);
dd3c394a 795 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
f6bcfd97 796 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
2bda0e17 797
dd3c394a 798 return pItem->OnDrawItem(dc, rect,
7561aacd
VZ
799 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
800 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
801}
802
1e6feb95
VZ
803#endif // wxUSE_OWNER_DRAWN
804
805#endif // wxUSE_LISTBOX