]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
Hopefully fixed library names generated by wx-config for OS/2's PM port.
[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
6a89f9ee 46#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
47WX_DEFINE_FLAGS( wxListBoxStyle )
48
3ff066a4 49wxBEGIN_FLAGS( wxListBoxStyle )
bc9fb572
JS
50 // new style border flags, we put them first to
51 // use them for streaming out
3ff066a4
SC
52 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
53 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
54 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
55 wxFLAGS_MEMBER(wxBORDER_RAISED)
56 wxFLAGS_MEMBER(wxBORDER_STATIC)
57 wxFLAGS_MEMBER(wxBORDER_NONE)
7ec5921d 58
bc9fb572 59 // old style border flags
3ff066a4
SC
60 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
61 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
62 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
63 wxFLAGS_MEMBER(wxRAISED_BORDER)
64 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 65 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
66
67 // standard window styles
3ff066a4
SC
68 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
69 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
70 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
71 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 72 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
73 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
74 wxFLAGS_MEMBER(wxVSCROLL)
75 wxFLAGS_MEMBER(wxHSCROLL)
76
77 wxFLAGS_MEMBER(wxLB_SINGLE)
78 wxFLAGS_MEMBER(wxLB_MULTIPLE)
79 wxFLAGS_MEMBER(wxLB_EXTENDED)
80 wxFLAGS_MEMBER(wxLB_HSCROLL)
81 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
82 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
83 wxFLAGS_MEMBER(wxLB_SORT)
84
85wxEND_FLAGS( wxListBoxStyle )
bc9fb572 86
6a89f9ee
SC
87IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h")
88
3ff066a4 89wxBEGIN_PROPERTIES_TABLE(wxListBox)
665b71b1
WS
90 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent )
91 wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent )
c5ca409b 92
af498247 93 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4 94 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
665b71b1 95 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247 96 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 97wxEND_PROPERTIES_TABLE()
6a89f9ee 98
3ff066a4
SC
99wxBEGIN_HANDLERS_TABLE(wxListBox)
100wxEND_HANDLERS_TABLE()
6a89f9ee 101
7ec5921d 102wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
6a89f9ee 103#else
9c9c3d7a 104IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
6a89f9ee 105#endif
2bda0e17 106
066f1b7a
SC
107/*
108TODO PROPERTIES
665b71b1
WS
109 selection
110 content
111 item
066f1b7a
SC
112*/
113
2bda0e17
KB
114// ============================================================================
115// list box item declaration and implementation
116// ============================================================================
117
47d67540 118#if wxUSE_OWNER_DRAWN
2bda0e17
KB
119
120class wxListBoxItem : public wxOwnerDrawn
121{
122public:
2b5f62a0 123 wxListBoxItem(const wxString& str = wxEmptyString);
2bda0e17
KB
124};
125
598ddd96 126wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, false)
2bda0e17 127{
dd3c394a
VZ
128 // no bitmaps/checkmarks
129 SetMarginWidth(0);
2bda0e17
KB
130}
131
2b5f62a0 132wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
2bda0e17 133{
dd3c394a 134 return new wxListBoxItem();
2bda0e17
KB
135}
136
137#endif //USE_OWNER_DRAWN
138
139// ============================================================================
140// list box control implementation
141// ============================================================================
142
2ee3ee1b
VZ
143// ----------------------------------------------------------------------------
144// creation
145// ----------------------------------------------------------------------------
dd3c394a 146
2ee3ee1b 147// Listbox item
bfc6fde4 148wxListBox::wxListBox()
2bda0e17 149{
dd3c394a
VZ
150 m_noItems = 0;
151 m_selected = 0;
2bda0e17
KB
152}
153
dd3c394a
VZ
154bool wxListBox::Create(wxWindow *parent,
155 wxWindowID id,
2bda0e17
KB
156 const wxPoint& pos,
157 const wxSize& size,
debe6624
JS
158 int n, const wxString choices[],
159 long style,
56a44c64 160 const wxValidator& validator,
2bda0e17
KB
161 const wxString& name)
162{
dd3c394a 163 m_noItems = 0;
dd3c394a 164 m_selected = 0;
2bda0e17 165
7a69cd96
VZ
166 // initialize base class fields
167 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
168 return false;
2bda0e17 169
7a69cd96
VZ
170 // create the native control
171 if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) )
172 {
173 // control creation failed
174 return false;
2bda0e17 175 }
1c089c47 176
a374cdc4
JS
177 // Necessary to prevent scroll problems within a notebook with Windows
178 // effects enabled
179 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
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);
31582e4e
RD
286
287 InvalidateBestSize();
2bda0e17
KB
288}
289
2ee3ee1b 290int wxListBox::DoAppend(const wxString& item)
2bda0e17 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
31582e4e 307 InvalidateBestSize();
2ee3ee1b 308 return index;
2bda0e17
KB
309}
310
2ee3ee1b 311void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
2bda0e17 312{
f6bcfd97
BP
313 // avoid flicker - but don't need to do this for a hidden listbox
314 bool hideAndShow = IsShown();
315 if ( hideAndShow )
316 {
317 ShowWindow(GetHwnd(), SW_HIDE);
318 }
2ee3ee1b 319
a23fd0e1 320 ListBox_ResetContent(GetHwnd());
2ee3ee1b
VZ
321
322 m_noItems = choices.GetCount();
dd3c394a 323 int i;
2ee3ee1b 324 for (i = 0; i < m_noItems; i++)
dd3c394a 325 {
a23fd0e1 326 ListBox_AddString(GetHwnd(), choices[i]);
dd3c394a 327 if ( clientData )
2ee3ee1b 328 {
2b5f62a0 329 SetClientData(i, clientData[i]);
2ee3ee1b 330 }
dd3c394a 331 }
2bda0e17 332
47d67540 333#if wxUSE_OWNER_DRAWN
2bda0e17 334 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a 335 // first delete old items
fd7ab28c 336 WX_CLEAR_ARRAY(m_aItems);
dd3c394a
VZ
337
338 // then create new ones
fd7ab28c 339 for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) {
2b5f62a0 340 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
dd3c394a
VZ
341 pNewItem->SetName(choices[ui]);
342 m_aItems.Add(pNewItem);
a23fd0e1 343 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
dd3c394a 344 }
2bda0e17 345 }
2ee3ee1b
VZ
346#endif // wxUSE_OWNER_DRAWN
347
348 SetHorizontalExtent();
2bda0e17 349
f6bcfd97
BP
350 if ( hideAndShow )
351 {
352 // show the listbox back if we hid it
353 ShowWindow(GetHwnd(), SW_SHOW);
354 }
31582e4e
RD
355
356 InvalidateBestSize();
2bda0e17
KB
357}
358
359int wxListBox::FindString(const wxString& s) const
360{
a23fd0e1 361 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
dd3c394a 362 if (pos == LB_ERR)
2ee3ee1b 363 return wxNOT_FOUND;
dd3c394a
VZ
364 else
365 return pos;
2bda0e17
KB
366}
367
bfc6fde4 368void wxListBox::Clear()
2bda0e17 369{
baccb514 370 Free();
8ee9d618
VZ
371
372 ListBox_ResetContent(GetHwnd());
373
374 m_noItems = 0;
375 SetHorizontalExtent();
31582e4e
RD
376
377 InvalidateBestSize();
8ee9d618
VZ
378}
379
380void wxListBox::Free()
2b273975 381{
dd3c394a 382#if wxUSE_OWNER_DRAWN
185fa6bf
VZ
383 if ( m_windowStyle & wxLB_OWNERDRAW )
384 {
fd7ab28c 385 WX_CLEAR_ARRAY(m_aItems);
185fa6bf
VZ
386 }
387 else
388#endif // wxUSE_OWNER_DRAWN
6c8a980f
VZ
389 if ( HasClientObjectData() )
390 {
391 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
392 {
393 delete GetClientObject(n);
394 }
395 }
2bda0e17 396}
baccb514 397
c6179a84 398void wxListBox::DoSetSelection(int N, bool select)
2bda0e17 399{
7ec5921d 400 wxCHECK_RET( N == wxNOT_FOUND ||
5955710c 401 (N >= 0 && N < m_noItems),
223d09f6 402 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 403
2ee3ee1b
VZ
404 if ( HasMultipleSelection() )
405 {
a23fd0e1 406 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 407 }
dd3c394a
VZ
408 else
409 {
2ee3ee1b 410 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 411 }
2bda0e17
KB
412}
413
2ee3ee1b 414bool wxListBox::IsSelected(int N) const
2bda0e17 415{
598ddd96 416 wxCHECK_MSG( N >= 0 && N < m_noItems, false,
223d09f6 417 wxT("invalid index in wxListBox::Selected") );
dd3c394a 418
598ddd96 419 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true;
2bda0e17
KB
420}
421
6c8a980f 422wxClientData* wxListBox::DoGetItemClientObject(int n) const
2bda0e17 423{
6c8a980f 424 return (wxClientData *)DoGetItemClientData(n);
2bda0e17
KB
425}
426
6c8a980f 427void *wxListBox::DoGetItemClientData(int n) const
2bda0e17 428{
2ee3ee1b 429 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
223d09f6 430 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 431
2ee3ee1b 432 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
433}
434
6c8a980f 435void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
2bda0e17 436{
6c8a980f 437 DoSetItemClientData(n, clientData);
2ee3ee1b
VZ
438}
439
6c8a980f 440void wxListBox::DoSetItemClientData(int n, void *clientData)
2ee3ee1b
VZ
441{
442 wxCHECK_RET( n >= 0 && n < m_noItems,
223d09f6 443 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 444
2ee3ee1b
VZ
445#if wxUSE_OWNER_DRAWN
446 if ( m_windowStyle & wxLB_OWNERDRAW )
447 {
448 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
449 // in OnMeasure/OnDraw.
450 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
451 }
452#endif // wxUSE_OWNER_DRAWN
453
454 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
223d09f6 455 wxLogDebug(wxT("LB_SETITEMDATA failed"));
2bda0e17
KB
456}
457
458// Return number of selections and an array of selected integers
14483330 459int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 460{
dd3c394a 461 aSelections.Empty();
14483330 462
2ee3ee1b 463 if ( HasMultipleSelection() )
dd3c394a 464 {
d90879fa
VZ
465 int countSel = ListBox_GetSelCount(GetHwnd());
466 if ( countSel == LB_ERR )
467 {
468 wxLogDebug(_T("ListBox_GetSelCount failed"));
469 }
470 else if ( countSel != 0 )
471 {
472 int *selections = new int[countSel];
14483330 473
d90879fa
VZ
474 if ( ListBox_GetSelItems(GetHwnd(),
475 countSel, selections) == LB_ERR )
476 {
477 wxLogDebug(wxT("ListBox_GetSelItems failed"));
478 countSel = -1;
479 }
480 else
481 {
482 aSelections.Alloc(countSel);
483 for ( int n = 0; n < countSel; n++ )
484 aSelections.Add(selections[n]);
485 }
14483330 486
dd3c394a
VZ
487 delete [] selections;
488 }
14483330 489
d90879fa 490 return countSel;
dd3c394a
VZ
491 }
492 else // single-selection listbox
493 {
f6bcfd97
BP
494 if (ListBox_GetCurSel(GetHwnd()) > -1)
495 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 496
f6bcfd97 497 return aSelections.Count();
dd3c394a 498 }
2bda0e17
KB
499}
500
501// Get single selection, for single choice list items
14483330 502int wxListBox::GetSelection() const
2bda0e17 503{
2ee3ee1b 504 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 505 -1,
f6bcfd97 506 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
14483330 507
a23fd0e1 508 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
509}
510
511// Find string for position
debe6624 512wxString wxListBox::GetString(int N) const
2bda0e17 513{
2b5f62a0 514 wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString,
66cf41cb 515 wxT("invalid index in wxListBox::GetString") );
dd3c394a 516
a23fd0e1 517 int len = ListBox_GetTextLen(GetHwnd(), N);
dd3c394a
VZ
518
519 // +1 for terminating NUL
520 wxString result;
c34924f7 521 ListBox_GetText(GetHwnd(), N, (wxChar*)wxStringBuffer(result, len + 1));
dd3c394a
VZ
522
523 return result;
2bda0e17
KB
524}
525
2ee3ee1b
VZ
526void
527wxListBox::DoInsertItems(const wxArrayString& items, int pos)
528{
529 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
530 wxT("invalid index in wxListBox::InsertItems") );
531
532 int nItems = items.GetCount();
533 for ( int i = 0; i < nItems; i++ )
fd7ab28c
VZ
534 {
535 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
536
537#if wxUSE_OWNER_DRAWN
9085d634
RD
538 if ( m_windowStyle & wxLB_OWNERDRAW )
539 {
2b5f62a0 540 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
9085d634
RD
541 pNewItem->SetName(items[i]);
542 pNewItem->SetFont(GetFont());
543 m_aItems.Insert(pNewItem, idx);
fd7ab28c 544
9085d634
RD
545 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
546 }
665b71b1
WS
547#else
548 wxUnusedVar(idx);
fd7ab28c
VZ
549#endif // wxUSE_OWNER_DRAWN
550 }
551
2ee3ee1b
VZ
552 m_noItems += nItems;
553
554 SetHorizontalExtent();
31582e4e
RD
555
556 InvalidateBestSize();
2ee3ee1b
VZ
557}
558
559void wxListBox::SetString(int N, const wxString& s)
560{
561 wxCHECK_RET( N >= 0 && N < m_noItems,
562 wxT("invalid index in wxListBox::SetString") );
563
564 // remember the state of the item
565 bool wasSelected = IsSelected(N);
566
567 void *oldData = NULL;
568 wxClientData *oldObjData = NULL;
1e6feb95 569 if ( m_clientDataItemsType == wxClientData_Void )
2ee3ee1b 570 oldData = GetClientData(N);
1e6feb95 571 else if ( m_clientDataItemsType == wxClientData_Object )
2ee3ee1b
VZ
572 oldObjData = GetClientObject(N);
573
574 // delete and recreate it
575 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
576
577 int newN = N;
578 if ( N == m_noItems - 1 )
579 newN = -1;
580
581 ListBox_InsertString(GetHwnd(), newN, s);
582
583 // restore the client data
584 if ( oldData )
585 SetClientData(N, oldData);
586 else if ( oldObjData )
587 SetClientObject(N, oldObjData);
588
2ee3ee1b
VZ
589#if wxUSE_OWNER_DRAWN
590 if ( m_windowStyle & wxLB_OWNERDRAW )
c294f450 591 {
2ee3ee1b
VZ
592 // update item's text
593 m_aItems[N]->SetName(s);
fd7ab28c 594
c294f450
RD
595 // reassign the item's data
596 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
597 }
2ee3ee1b 598#endif //USE_OWNER_DRAWN
781bdbb4
JS
599
600 // we may have lost the selection
601 if ( wasSelected )
602 Select(N);
31582e4e
RD
603
604 InvalidateBestSize();
2ee3ee1b
VZ
605}
606
607int wxListBox::GetCount() const
608{
609 return m_noItems;
610}
611
612// ----------------------------------------------------------------------------
613// helpers
614// ----------------------------------------------------------------------------
615
4438caf4
VZ
616// Windows-specific code to set the horizontal extent of the listbox, if
617// necessary. If s is non-NULL, it's used to calculate the horizontal extent.
2bda0e17
KB
618// Otherwise, all strings are used.
619void wxListBox::SetHorizontalExtent(const wxString& s)
620{
dd3c394a
VZ
621 // Only necessary if we want a horizontal scrollbar
622 if (!(m_windowStyle & wxHSCROLL))
623 return;
624 TEXTMETRIC lpTextMetric;
625
c34924f7 626 if ( !s.empty() )
2bda0e17 627 {
a23fd0e1
VZ
628 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
629 HDC dc = GetWindowDC(GetHwnd());
dd3c394a 630 HFONT oldFont = 0;
6305fabe 631 if (GetFont().Ok() && GetFont().GetResourceHandle() != 0)
dd3c394a
VZ
632 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
633
634 GetTextMetrics(dc, &lpTextMetric);
635 SIZE extentXY;
767b35a5 636 ::GetTextExtentPoint32(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
dd3c394a
VZ
637 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
638
639 if (oldFont)
640 ::SelectObject(dc, oldFont);
641
a23fd0e1 642 ReleaseDC(GetHwnd(), dc);
dd3c394a 643 if (extentX > existingExtent)
a23fd0e1 644 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
dd3c394a
VZ
645 }
646 else
647 {
648 int largestExtent = 0;
a23fd0e1 649 HDC dc = GetWindowDC(GetHwnd());
dd3c394a 650 HFONT oldFont = 0;
6305fabe 651 if (GetFont().Ok() && GetFont().GetResourceHandle() != 0)
dd3c394a
VZ
652 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
653
654 GetTextMetrics(dc, &lpTextMetric);
3e3be693 655
3e3be693 656 for (int i = 0; i < m_noItems; i++)
dd3c394a 657 {
50c6bf91 658 wxString str = GetString(i);
dd3c394a 659 SIZE extentXY;
767b35a5 660 ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
dd3c394a
VZ
661 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
662 if (extentX > largestExtent)
663 largestExtent = extentX;
664 }
665 if (oldFont)
666 ::SelectObject(dc, oldFont);
667
a23fd0e1
VZ
668 ReleaseDC(GetHwnd(), dc);
669 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
2bda0e17 670 }
2bda0e17
KB
671}
672
f68586e5 673wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
674{
675 // find the widest string
676 int wLine;
677 int wListbox = 0;
678 for ( int i = 0; i < m_noItems; i++ )
679 {
680 wxString str(GetString(i));
681 GetTextExtent(str, &wLine, NULL);
682 if ( wLine > wListbox )
683 wListbox = wLine;
684 }
685
686 // give it some reasonable default value if there are no strings in the
687 // list
688 if ( wListbox == 0 )
689 wListbox = 100;
690
691 // the listbox should be slightly larger than the widest string
692 int cx, cy;
7a5e53ab 693 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
b908d224
VZ
694
695 wListbox += 3*cx;
696
908f91bc
RD
697 // Add room for the scrollbar
698 wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
699
5b6d6b70
VZ
700 // don't make the listbox too tall (limit height to 10 items) but don't
701 // make it too small neither
702 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
703 wxMin(wxMax(m_noItems, 3), 10);
b908d224 704
31582e4e
RD
705 wxSize best(wListbox, hListbox);
706 CacheBestSize(best);
707 return best;
b908d224
VZ
708}
709
2ee3ee1b
VZ
710// ----------------------------------------------------------------------------
711// callbacks
712// ----------------------------------------------------------------------------
713
714bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 715{
bada28f0 716 wxEventType evtType;
8ee9d618 717 if ( param == LBN_SELCHANGE )
2bda0e17 718 {
bada28f0 719 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
2bda0e17 720 }
8ee9d618 721 else if ( param == LBN_DBLCLK )
2ee3ee1b 722 {
bada28f0
VZ
723 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
724 }
725 else
726 {
727 // some event we're not interested in
598ddd96 728 return false;
bada28f0
VZ
729 }
730
731 wxCommandEvent event(evtType, m_windowId);
732 event.SetEventObject( this );
8ee9d618 733
9c9c3d7a
VZ
734 // retrieve the affected item
735 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
736 if ( n != LB_ERR )
bada28f0 737 {
bada28f0
VZ
738 if ( HasClientObjectData() )
739 event.SetClientObject( GetClientObject(n) );
740 else if ( HasClientUntypedData() )
741 event.SetClientData( GetClientData(n) );
9c9c3d7a 742
bada28f0 743 event.SetString( GetString(n) );
598ddd96 744 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : true );
bada28f0
VZ
745 }
746
687706f5 747 event.SetInt(n);
2ee3ee1b 748
bada28f0 749 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
750}
751
2ee3ee1b
VZ
752// ----------------------------------------------------------------------------
753// wxCheckListBox support
754// ----------------------------------------------------------------------------
2bda0e17 755
47d67540 756#if wxUSE_OWNER_DRAWN
2bda0e17
KB
757
758// drawing
759// -------
760
761// space beneath/above each row in pixels
762// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
763#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
764
765// the height is the same for all items
dd3c394a
VZ
766// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
767
768// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
769// message is not yet sent when we get here!
2bda0e17
KB
770bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
771{
dd3c394a 772 // only owner-drawn control should receive this message
598ddd96 773 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
2bda0e17 774
dd3c394a 775 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 776
4676948b
JS
777#ifdef __WXWINCE__
778 HDC hdc = GetDC(NULL);
779#else
07cf98cb 780 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
4676948b 781#endif
07cf98cb 782
dd3c394a 783 wxDC dc;
07cf98cb 784 dc.SetHDC((WXHDC)hdc);
e0176dd9 785 dc.SetFont(GetFont());
2bda0e17 786
dd3c394a
VZ
787 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
788 pStruct->itemWidth = dc.GetCharWidth();
2bda0e17 789
07cf98cb
VZ
790 dc.SetHDC(0);
791
792 DeleteDC(hdc);
793
598ddd96 794 return true;
2bda0e17
KB
795}
796
797// forward the message to the appropriate item
798bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
799{
dd3c394a 800 // only owner-drawn control should receive this message
598ddd96 801 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
dd3c394a
VZ
802
803 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
f6bcfd97
BP
804 UINT itemID = pStruct->itemID;
805
806 // the item may be -1 for an empty listbox
807 if ( itemID == (UINT)-1 )
598ddd96 808 return false;
dd3c394a 809
a23fd0e1 810 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
2bda0e17 811
598ddd96 812 wxCHECK( data && (data != LB_ERR), false );
2bda0e17 813
dd3c394a 814 wxListBoxItem *pItem = (wxListBoxItem *)data;
2bda0e17 815
7561aacd 816 wxDCTemp dc((WXHDC)pStruct->hDC);
42841dfc
WS
817 wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top);
818 wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom);
819 wxRect rect(pt1, pt2);
2bda0e17 820
dd3c394a 821 return pItem->OnDrawItem(dc, rect,
7561aacd
VZ
822 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
823 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
824}
825
1e6feb95
VZ
826#endif // wxUSE_OWNER_DRAWN
827
828#endif // wxUSE_LISTBOX