]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
fixed wxImage->wxBitmap conversion for images with alpha channel
[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
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,
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{
dd3c394a 411 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
223d09f6 412 wxT("invalid index in wxListBox::Selected") );
dd3c394a 413
a23fd0e1 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;
de564874 516 ListBox_GetText(GetHwnd(), N, 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
2ee3ee1b 619 if ( !s.IsEmpty() )
2bda0e17 620 {
a23fd0e1
VZ
621 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
622 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
623 HFONT oldFont = 0;
624 if (GetFont().Ok() && GetFont().GetResourceHandle())
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
VZ
643 HFONT oldFont = 0;
644 if (GetFont().Ok() && GetFont().GetResourceHandle())
645 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
646
647 GetTextMetrics(dc, &lpTextMetric);
3e3be693
VZ
648
649 // FIXME: buffer overflow!!
650 wxChar buf[1024];
651 for (int i = 0; i < m_noItems; i++)
dd3c394a 652 {
3e3be693
VZ
653 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf);
654 buf[len] = 0;
dd3c394a 655 SIZE extentXY;
3e3be693 656 ::GetTextExtentPoint(dc, buf, len, &extentXY);
dd3c394a
VZ
657 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
658 if (extentX > largestExtent)
659 largestExtent = extentX;
660 }
661 if (oldFont)
662 ::SelectObject(dc, oldFont);
663
a23fd0e1
VZ
664 ReleaseDC(GetHwnd(), dc);
665 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
2bda0e17 666 }
2bda0e17
KB
667}
668
f68586e5 669wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
670{
671 // find the widest string
672 int wLine;
673 int wListbox = 0;
674 for ( int i = 0; i < m_noItems; i++ )
675 {
676 wxString str(GetString(i));
677 GetTextExtent(str, &wLine, NULL);
678 if ( wLine > wListbox )
679 wListbox = wLine;
680 }
681
682 // give it some reasonable default value if there are no strings in the
683 // list
684 if ( wListbox == 0 )
685 wListbox = 100;
686
687 // the listbox should be slightly larger than the widest string
688 int cx, cy;
7a5e53ab 689 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
b908d224
VZ
690
691 wListbox += 3*cx;
692
5b6d6b70
VZ
693 // don't make the listbox too tall (limit height to 10 items) but don't
694 // make it too small neither
695 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
696 wxMin(wxMax(m_noItems, 3), 10);
b908d224
VZ
697
698 return wxSize(wListbox, hListbox);
699}
700
2ee3ee1b
VZ
701// ----------------------------------------------------------------------------
702// callbacks
703// ----------------------------------------------------------------------------
704
705bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 706{
bada28f0 707 wxEventType evtType;
8ee9d618 708 if ( param == LBN_SELCHANGE )
2bda0e17 709 {
bada28f0 710 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
2bda0e17 711 }
8ee9d618 712 else if ( param == LBN_DBLCLK )
2ee3ee1b 713 {
bada28f0
VZ
714 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
715 }
716 else
717 {
718 // some event we're not interested in
719 return FALSE;
720 }
721
722 wxCommandEvent event(evtType, m_windowId);
723 event.SetEventObject( this );
8ee9d618 724
9c9c3d7a
VZ
725 // retrieve the affected item
726 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
727 if ( n != LB_ERR )
bada28f0 728 {
bada28f0
VZ
729 if ( HasClientObjectData() )
730 event.SetClientObject( GetClientObject(n) );
731 else if ( HasClientUntypedData() )
732 event.SetClientData( GetClientData(n) );
9c9c3d7a 733
bada28f0 734 event.SetString( GetString(n) );
9c9c3d7a 735 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : TRUE );
bada28f0
VZ
736 }
737
738 event.m_commandInt = n;
2ee3ee1b 739
bada28f0 740 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
741}
742
2ee3ee1b
VZ
743// ----------------------------------------------------------------------------
744// wxCheckListBox support
745// ----------------------------------------------------------------------------
2bda0e17 746
47d67540 747#if wxUSE_OWNER_DRAWN
2bda0e17
KB
748
749// drawing
750// -------
751
752// space beneath/above each row in pixels
753// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
754#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
755
756// the height is the same for all items
dd3c394a
VZ
757// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
758
759// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
760// message is not yet sent when we get here!
2bda0e17
KB
761bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
762{
dd3c394a
VZ
763 // only owner-drawn control should receive this message
764 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
2bda0e17 765
dd3c394a 766 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 767
4676948b
JS
768#ifdef __WXWINCE__
769 HDC hdc = GetDC(NULL);
770#else
07cf98cb 771 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
4676948b 772#endif
07cf98cb 773
dd3c394a 774 wxDC dc;
07cf98cb 775 dc.SetHDC((WXHDC)hdc);
e0176dd9 776 dc.SetFont(GetFont());
2bda0e17 777
dd3c394a
VZ
778 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
779 pStruct->itemWidth = dc.GetCharWidth();
2bda0e17 780
07cf98cb
VZ
781 dc.SetHDC(0);
782
783 DeleteDC(hdc);
784
dd3c394a 785 return TRUE;
2bda0e17
KB
786}
787
788// forward the message to the appropriate item
789bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
790{
dd3c394a
VZ
791 // only owner-drawn control should receive this message
792 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
793
794 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
f6bcfd97
BP
795 UINT itemID = pStruct->itemID;
796
797 // the item may be -1 for an empty listbox
798 if ( itemID == (UINT)-1 )
799 return FALSE;
dd3c394a 800
a23fd0e1 801 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
2bda0e17 802
dd3c394a 803 wxCHECK( data && (data != LB_ERR), FALSE );
2bda0e17 804
dd3c394a 805 wxListBoxItem *pItem = (wxListBoxItem *)data;
2bda0e17 806
7561aacd 807 wxDCTemp dc((WXHDC)pStruct->hDC);
dd3c394a 808 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
f6bcfd97 809 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
2bda0e17 810
dd3c394a 811 return pItem->OnDrawItem(dc, rect,
7561aacd
VZ
812 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
813 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
814}
815
1e6feb95
VZ
816#endif // wxUSE_OWNER_DRAWN
817
818#endif // wxUSE_LISTBOX