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