]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
Added missing arg
[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
2bda0e17
KB
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
dd3c394a 16 #pragma hdrstop
2bda0e17
KB
17#endif
18
1e6feb95 19#if wxUSE_LISTBOX
0c589ad0 20
2a673eb1
WS
21#include "wx/listbox.h"
22
2bda0e17 23#ifndef WX_PRECOMP
ad9835c9 24 #include "wx/dynarray.h"
ad9835c9
WS
25 #include "wx/settings.h"
26 #include "wx/brush.h"
27 #include "wx/font.h"
28 #include "wx/dc.h"
29 #include "wx/utils.h"
e4db172a 30 #include "wx/log.h"
cdccdfab 31 #include "wx/window.h"
2bda0e17
KB
32#endif
33
1e6feb95
VZ
34#include "wx/msw/private.h"
35
5ea105e0
RR
36#include <windowsx.h>
37
0c589ad0
BM
38#if wxUSE_OWNER_DRAWN
39 #include "wx/ownerdrw.h"
40#endif
2bda0e17 41
6a89f9ee 42#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
43WX_DEFINE_FLAGS( wxListBoxStyle )
44
3ff066a4 45wxBEGIN_FLAGS( wxListBoxStyle )
bc9fb572
JS
46 // new style border flags, we put them first to
47 // use them for streaming out
3ff066a4
SC
48 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
49 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
50 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
51 wxFLAGS_MEMBER(wxBORDER_RAISED)
52 wxFLAGS_MEMBER(wxBORDER_STATIC)
53 wxFLAGS_MEMBER(wxBORDER_NONE)
7ec5921d 54
bc9fb572 55 // old style border flags
3ff066a4
SC
56 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
57 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
58 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
59 wxFLAGS_MEMBER(wxRAISED_BORDER)
60 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 61 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
62
63 // standard window styles
3ff066a4
SC
64 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
65 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
66 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
67 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 68 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
69 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
70 wxFLAGS_MEMBER(wxVSCROLL)
71 wxFLAGS_MEMBER(wxHSCROLL)
72
73 wxFLAGS_MEMBER(wxLB_SINGLE)
74 wxFLAGS_MEMBER(wxLB_MULTIPLE)
75 wxFLAGS_MEMBER(wxLB_EXTENDED)
76 wxFLAGS_MEMBER(wxLB_HSCROLL)
77 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
78 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
79 wxFLAGS_MEMBER(wxLB_SORT)
80
81wxEND_FLAGS( wxListBoxStyle )
bc9fb572 82
6a89f9ee
SC
83IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h")
84
3ff066a4 85wxBEGIN_PROPERTIES_TABLE(wxListBox)
665b71b1
WS
86 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent )
87 wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent )
c5ca409b 88
af498247 89 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4 90 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
665b71b1 91 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247 92 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 93wxEND_PROPERTIES_TABLE()
6a89f9ee 94
3ff066a4
SC
95wxBEGIN_HANDLERS_TABLE(wxListBox)
96wxEND_HANDLERS_TABLE()
6a89f9ee 97
7ec5921d 98wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
6a89f9ee 99#else
9c9c3d7a 100IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
6a89f9ee 101#endif
2bda0e17 102
066f1b7a
SC
103/*
104TODO PROPERTIES
665b71b1
WS
105 selection
106 content
107 item
066f1b7a
SC
108*/
109
2bda0e17
KB
110// ============================================================================
111// list box item declaration and implementation
112// ============================================================================
113
47d67540 114#if wxUSE_OWNER_DRAWN
2bda0e17
KB
115
116class wxListBoxItem : public wxOwnerDrawn
117{
118public:
2b5f62a0 119 wxListBoxItem(const wxString& str = wxEmptyString);
2bda0e17
KB
120};
121
598ddd96 122wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, false)
2bda0e17 123{
dd3c394a
VZ
124 // no bitmaps/checkmarks
125 SetMarginWidth(0);
2bda0e17
KB
126}
127
2b5f62a0 128wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
2bda0e17 129{
dd3c394a 130 return new wxListBoxItem();
2bda0e17
KB
131}
132
133#endif //USE_OWNER_DRAWN
134
135// ============================================================================
136// list box control implementation
137// ============================================================================
138
2ee3ee1b
VZ
139// ----------------------------------------------------------------------------
140// creation
141// ----------------------------------------------------------------------------
dd3c394a 142
2ee3ee1b 143// Listbox item
bfc6fde4 144wxListBox::wxListBox()
2bda0e17 145{
dd3c394a
VZ
146 m_noItems = 0;
147 m_selected = 0;
2bda0e17
KB
148}
149
dd3c394a
VZ
150bool wxListBox::Create(wxWindow *parent,
151 wxWindowID id,
2bda0e17
KB
152 const wxPoint& pos,
153 const wxSize& size,
debe6624
JS
154 int n, const wxString choices[],
155 long style,
56a44c64 156 const wxValidator& validator,
2bda0e17
KB
157 const wxString& name)
158{
dd3c394a 159 m_noItems = 0;
dd3c394a 160 m_selected = 0;
2bda0e17 161
7a69cd96
VZ
162 // initialize base class fields
163 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
164 return false;
2bda0e17 165
7a69cd96
VZ
166 // create the native control
167 if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) )
168 {
169 // control creation failed
170 return false;
2bda0e17 171 }
1c089c47 172
7a69cd96
VZ
173 // initialize the contents
174 for ( int i = 0; i < n; i++ )
175 {
176 Append(choices[i]);
2bda0e17 177 }
2bda0e17 178
7ec5921d 179 // now we can compute our best size correctly, so do it if necessary
170acdc9 180 SetInitialSize(size);
7ec5921d 181
7a69cd96 182 return true;
2bda0e17
KB
183}
184
584ad2a3
MB
185bool wxListBox::Create(wxWindow *parent,
186 wxWindowID id,
187 const wxPoint& pos,
188 const wxSize& size,
189 const wxArrayString& choices,
190 long style,
76efbc2a 191 const wxValidator& validator,
584ad2a3
MB
192 const wxString& name)
193{
194 wxCArrayString chs(choices);
195 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
196 style, validator, name);
197}
198
bfc6fde4 199wxListBox::~wxListBox()
2bda0e17 200{
baccb514 201 Free();
2bda0e17
KB
202}
203
7a69cd96 204WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
2bda0e17 205{
7a69cd96
VZ
206 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
207
6079ab87
VZ
208 // always show the vertical scrollbar if necessary -- otherwise it is
209 // impossible to use the control with the mouse
210 msStyle |= WS_VSCROLL;
211
7a69cd96
VZ
212 // we always want to get the notifications
213 msStyle |= LBS_NOTIFY;
214
215 // without this style, you get unexpected heights, so e.g. constraint
216 // layout doesn't work properly
217 msStyle |= LBS_NOINTEGRALHEIGHT;
218
219 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
220 _T("only one of listbox selection modes can be specified") );
221
222 if ( style & wxLB_MULTIPLE )
223 msStyle |= LBS_MULTIPLESEL;
224 else if ( style & wxLB_EXTENDED )
225 msStyle |= LBS_EXTENDEDSEL;
226
227 if ( m_windowStyle & wxLB_ALWAYS_SB )
228 msStyle |= LBS_DISABLENOSCROLL;
229 if ( m_windowStyle & wxLB_HSCROLL )
230 msStyle |= WS_HSCROLL;
231 if ( m_windowStyle & wxLB_SORT )
232 msStyle |= LBS_SORT;
233
234#if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
235 if ( m_windowStyle & wxLB_OWNERDRAW )
236 {
237 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
238 // the strings in the listbox for simplicity even though we could have
239 // avoided it in this case
240 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
241 }
242#endif // wxUSE_OWNER_DRAWN
243
244 return msStyle;
2bda0e17
KB
245}
246
2ee3ee1b
VZ
247// ----------------------------------------------------------------------------
248// implementation of wxListBoxBase methods
249// ----------------------------------------------------------------------------
250
251void wxListBox::DoSetFirstItem(int N)
2bda0e17 252{
8228b893 253 wxCHECK_RET( IsValid(N),
223d09f6 254 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 255
2ee3ee1b 256 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
257}
258
aa61d352 259void wxListBox::Delete(unsigned int n)
2bda0e17 260{
aa61d352 261 wxCHECK_RET( IsValid(n),
223d09f6 262 wxT("invalid index in wxListBox::Delete") );
dd3c394a 263
07cf98cb
VZ
264 // for owner drawn objects, the data is used for storing wxOwnerDrawn
265 // pointers and we shouldn't touch it
266#if !wxUSE_OWNER_DRAWN
267 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
268#endif // !wxUSE_OWNER_DRAWN
269 if ( HasClientObjectData() )
270 {
aa61d352 271 delete GetClientObject(n);
07cf98cb 272 }
6c8a980f 273
aa61d352 274 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
dd3c394a
VZ
275 m_noItems--;
276
2b5f62a0 277 SetHorizontalExtent(wxEmptyString);
2bda0e17
KB
278}
279
2ee3ee1b 280int wxListBox::DoAppend(const wxString& item)
2bda0e17 281{
a23fd0e1 282 int index = ListBox_AddString(GetHwnd(), item);
2ee3ee1b 283 m_noItems++;
2bda0e17 284
47d67540 285#if wxUSE_OWNER_DRAWN
2bda0e17 286 if ( m_windowStyle & wxLB_OWNERDRAW ) {
2b5f62a0 287 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
dd3c394a 288 pNewItem->SetName(item);
fd7ab28c 289 m_aItems.Insert(pNewItem, index);
a23fd0e1 290 ListBox_SetItemData(GetHwnd(), index, pNewItem);
60c65519 291 pNewItem->SetFont(GetFont());
2bda0e17 292 }
fd7ab28c 293#endif // wxUSE_OWNER_DRAWN
2bda0e17 294
dd3c394a 295 SetHorizontalExtent(item);
dd3c394a 296
2ee3ee1b 297 return index;
2bda0e17
KB
298}
299
2ee3ee1b 300void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
2bda0e17 301{
f6bcfd97
BP
302 // avoid flicker - but don't need to do this for a hidden listbox
303 bool hideAndShow = IsShown();
304 if ( hideAndShow )
305 {
306 ShowWindow(GetHwnd(), SW_HIDE);
307 }
2ee3ee1b 308
a23fd0e1 309 ListBox_ResetContent(GetHwnd());
2ee3ee1b
VZ
310
311 m_noItems = choices.GetCount();
aa61d352 312 unsigned int i;
2ee3ee1b 313 for (i = 0; i < m_noItems; i++)
dd3c394a 314 {
a23fd0e1 315 ListBox_AddString(GetHwnd(), choices[i]);
dd3c394a 316 if ( clientData )
2ee3ee1b 317 {
2b5f62a0 318 SetClientData(i, clientData[i]);
2ee3ee1b 319 }
dd3c394a 320 }
2bda0e17 321
47d67540 322#if wxUSE_OWNER_DRAWN
2bda0e17 323 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a 324 // first delete old items
fd7ab28c 325 WX_CLEAR_ARRAY(m_aItems);
dd3c394a
VZ
326
327 // then create new ones
aa61d352 328 for ( unsigned int ui = 0; ui < m_noItems; ui++ ) {
2b5f62a0 329 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
dd3c394a
VZ
330 pNewItem->SetName(choices[ui]);
331 m_aItems.Add(pNewItem);
a23fd0e1 332 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
dd3c394a 333 }
2bda0e17 334 }
2ee3ee1b
VZ
335#endif // wxUSE_OWNER_DRAWN
336
337 SetHorizontalExtent();
2bda0e17 338
f6bcfd97
BP
339 if ( hideAndShow )
340 {
341 // show the listbox back if we hid it
342 ShowWindow(GetHwnd(), SW_SHOW);
343 }
2bda0e17
KB
344}
345
11e62fe6 346int wxListBox::FindString(const wxString& s, bool bCase) const
2bda0e17 347{
11e62fe6
WS
348 // back to base class search for not native search type
349 if (bCase)
350 return wxItemContainerImmutable::FindString( s, bCase );
351
4a10ea8b 352 int pos = ListBox_FindStringExact(GetHwnd(), -1, s);
dd3c394a 353 if (pos == LB_ERR)
2ee3ee1b 354 return wxNOT_FOUND;
dd3c394a
VZ
355 else
356 return pos;
2bda0e17
KB
357}
358
bfc6fde4 359void wxListBox::Clear()
2bda0e17 360{
baccb514 361 Free();
8ee9d618
VZ
362
363 ListBox_ResetContent(GetHwnd());
364
365 m_noItems = 0;
366 SetHorizontalExtent();
367}
368
369void wxListBox::Free()
2b273975 370{
dd3c394a 371#if wxUSE_OWNER_DRAWN
185fa6bf
VZ
372 if ( m_windowStyle & wxLB_OWNERDRAW )
373 {
fd7ab28c 374 WX_CLEAR_ARRAY(m_aItems);
185fa6bf
VZ
375 }
376 else
377#endif // wxUSE_OWNER_DRAWN
6c8a980f
VZ
378 if ( HasClientObjectData() )
379 {
aa61d352 380 for ( unsigned int n = 0; n < m_noItems; n++ )
6c8a980f
VZ
381 {
382 delete GetClientObject(n);
383 }
384 }
2bda0e17 385}
baccb514 386
c6179a84 387void wxListBox::DoSetSelection(int N, bool select)
2bda0e17 388{
8228b893 389 wxCHECK_RET( N == wxNOT_FOUND || IsValid(N),
223d09f6 390 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 391
2ee3ee1b
VZ
392 if ( HasMultipleSelection() )
393 {
a23fd0e1 394 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 395 }
dd3c394a
VZ
396 else
397 {
2ee3ee1b 398 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 399 }
2bda0e17
KB
400}
401
2ee3ee1b 402bool wxListBox::IsSelected(int N) const
2bda0e17 403{
8228b893 404 wxCHECK_MSG( IsValid(N), false,
223d09f6 405 wxT("invalid index in wxListBox::Selected") );
dd3c394a 406
598ddd96 407 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true;
2bda0e17
KB
408}
409
aa61d352 410wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
2bda0e17 411{
6c8a980f 412 return (wxClientData *)DoGetItemClientData(n);
2bda0e17
KB
413}
414
aa61d352 415void *wxListBox::DoGetItemClientData(unsigned int n) const
2bda0e17 416{
8228b893 417 wxCHECK_MSG( IsValid(n), NULL,
223d09f6 418 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 419
2ee3ee1b 420 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
421}
422
aa61d352 423void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
2bda0e17 424{
6c8a980f 425 DoSetItemClientData(n, clientData);
2ee3ee1b
VZ
426}
427
aa61d352 428void wxListBox::DoSetItemClientData(unsigned int n, void *clientData)
2ee3ee1b 429{
8228b893 430 wxCHECK_RET( IsValid(n),
223d09f6 431 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 432
2ee3ee1b
VZ
433#if wxUSE_OWNER_DRAWN
434 if ( m_windowStyle & wxLB_OWNERDRAW )
435 {
436 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
437 // in OnMeasure/OnDraw.
438 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
439 }
440#endif // wxUSE_OWNER_DRAWN
441
442 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
223d09f6 443 wxLogDebug(wxT("LB_SETITEMDATA failed"));
2bda0e17
KB
444}
445
446// Return number of selections and an array of selected integers
14483330 447int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 448{
dd3c394a 449 aSelections.Empty();
14483330 450
2ee3ee1b 451 if ( HasMultipleSelection() )
dd3c394a 452 {
d90879fa
VZ
453 int countSel = ListBox_GetSelCount(GetHwnd());
454 if ( countSel == LB_ERR )
455 {
456 wxLogDebug(_T("ListBox_GetSelCount failed"));
457 }
458 else if ( countSel != 0 )
459 {
460 int *selections = new int[countSel];
14483330 461
d90879fa
VZ
462 if ( ListBox_GetSelItems(GetHwnd(),
463 countSel, selections) == LB_ERR )
464 {
465 wxLogDebug(wxT("ListBox_GetSelItems failed"));
466 countSel = -1;
467 }
468 else
469 {
470 aSelections.Alloc(countSel);
471 for ( int n = 0; n < countSel; n++ )
472 aSelections.Add(selections[n]);
473 }
14483330 474
dd3c394a
VZ
475 delete [] selections;
476 }
14483330 477
d90879fa 478 return countSel;
dd3c394a
VZ
479 }
480 else // single-selection listbox
481 {
f6bcfd97
BP
482 if (ListBox_GetCurSel(GetHwnd()) > -1)
483 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 484
f6bcfd97 485 return aSelections.Count();
dd3c394a 486 }
2bda0e17
KB
487}
488
489// Get single selection, for single choice list items
14483330 490int wxListBox::GetSelection() const
2bda0e17 491{
2ee3ee1b 492 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 493 -1,
f6bcfd97 494 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
14483330 495
a23fd0e1 496 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
497}
498
499// Find string for position
aa61d352 500wxString wxListBox::GetString(unsigned int n) const
2bda0e17 501{
aa61d352 502 wxCHECK_MSG( IsValid(n), wxEmptyString,
66cf41cb 503 wxT("invalid index in wxListBox::GetString") );
dd3c394a 504
aa61d352 505 int len = ListBox_GetTextLen(GetHwnd(), n);
dd3c394a
VZ
506
507 // +1 for terminating NUL
508 wxString result;
aa61d352 509 ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1));
dd3c394a
VZ
510
511 return result;
2bda0e17
KB
512}
513
2ee3ee1b 514void
aa61d352 515wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
2ee3ee1b 516{
8228b893 517 wxCHECK_RET( IsValidInsert(pos),
2ee3ee1b
VZ
518 wxT("invalid index in wxListBox::InsertItems") );
519
aa61d352
VZ
520 unsigned int nItems = items.GetCount();
521 for ( unsigned int i = 0; i < nItems; i++ )
fd7ab28c
VZ
522 {
523 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
524
525#if wxUSE_OWNER_DRAWN
9085d634
RD
526 if ( m_windowStyle & wxLB_OWNERDRAW )
527 {
2b5f62a0 528 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
9085d634
RD
529 pNewItem->SetName(items[i]);
530 pNewItem->SetFont(GetFont());
531 m_aItems.Insert(pNewItem, idx);
fd7ab28c 532
9085d634
RD
533 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
534 }
665b71b1
WS
535#else
536 wxUnusedVar(idx);
fd7ab28c
VZ
537#endif // wxUSE_OWNER_DRAWN
538 }
539
2ee3ee1b
VZ
540 m_noItems += nItems;
541
542 SetHorizontalExtent();
543}
544
c00fed0e
VZ
545int wxListBox::DoListHitTest(const wxPoint& point) const
546{
8228b893 547 LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT,
c00fed0e
VZ
548 0L, MAKELONG(point.x, point.y));
549
550 // non zero high-order word means that this item is outside of the client
551 // area, IOW the point is outside of the listbox
552 return HIWORD(lRes) ? wxNOT_FOUND : lRes;
553}
554
aa61d352 555void wxListBox::SetString(unsigned int n, const wxString& s)
2ee3ee1b 556{
aa61d352 557 wxCHECK_RET( IsValid(n),
2ee3ee1b
VZ
558 wxT("invalid index in wxListBox::SetString") );
559
560 // remember the state of the item
aa61d352 561 bool wasSelected = IsSelected(n);
2ee3ee1b
VZ
562
563 void *oldData = NULL;
564 wxClientData *oldObjData = NULL;
1e6feb95 565 if ( m_clientDataItemsType == wxClientData_Void )
aa61d352 566 oldData = GetClientData(n);
1e6feb95 567 else if ( m_clientDataItemsType == wxClientData_Object )
aa61d352 568 oldObjData = GetClientObject(n);
2ee3ee1b
VZ
569
570 // delete and recreate it
aa61d352 571 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
2ee3ee1b 572
aa61d352
VZ
573 int newN = n;
574 if ( n == (m_noItems - 1) )
2ee3ee1b
VZ
575 newN = -1;
576
577 ListBox_InsertString(GetHwnd(), newN, s);
578
579 // restore the client data
580 if ( oldData )
aa61d352 581 SetClientData(n, oldData);
2ee3ee1b 582 else if ( oldObjData )
aa61d352 583 SetClientObject(n, oldObjData);
2ee3ee1b 584
2ee3ee1b
VZ
585#if wxUSE_OWNER_DRAWN
586 if ( m_windowStyle & wxLB_OWNERDRAW )
c294f450 587 {
2ee3ee1b 588 // update item's text
aa61d352 589 m_aItems[n]->SetName(s);
fd7ab28c 590
c294f450 591 // reassign the item's data
aa61d352 592 ListBox_SetItemData(GetHwnd(), n, m_aItems[n]);
c294f450 593 }
2ee3ee1b 594#endif //USE_OWNER_DRAWN
781bdbb4
JS
595
596 // we may have lost the selection
597 if ( wasSelected )
aa61d352 598 Select(n);
31582e4e 599
fca418ae 600 SetHorizontalExtent();
2ee3ee1b
VZ
601}
602
aa61d352 603unsigned int wxListBox::GetCount() const
2ee3ee1b
VZ
604{
605 return m_noItems;
606}
607
608// ----------------------------------------------------------------------------
fca418ae 609// size-related stuff
2ee3ee1b
VZ
610// ----------------------------------------------------------------------------
611
2bda0e17
KB
612void wxListBox::SetHorizontalExtent(const wxString& s)
613{
fca418ae
VZ
614 // in any case, our best size could have changed
615 InvalidateBestSize();
616
617 // the rest is only necessary if we want a horizontal scrollbar
618 if ( !HasFlag(wxHSCROLL) )
dd3c394a 619 return;
dd3c394a 620
dd3c394a 621
fca418ae
VZ
622 WindowHDC dc(GetHwnd());
623 SelectInHDC selFont(dc, GetHfontOf(GetFont()));
dd3c394a 624
fca418ae
VZ
625 TEXTMETRIC lpTextMetric;
626 ::GetTextMetrics(dc, &lpTextMetric);
dd3c394a 627
fca418ae
VZ
628 int largestExtent = 0;
629 SIZE extentXY;
3e3be693 630
fca418ae
VZ
631 if ( s.empty() )
632 {
633 // set extent to the max length of all strings
634 for ( unsigned int i = 0; i < m_noItems; i++ )
dd3c394a 635 {
fca418ae 636 const wxString str = GetString(i);
767b35a5 637 ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
fca418ae 638
dd3c394a 639 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
fca418ae 640 if ( extentX > largestExtent )
dd3c394a
VZ
641 largestExtent = extentX;
642 }
fca418ae
VZ
643 }
644 else // just increase the extent to the length of this string
645 {
646 int existingExtent = (int)SendMessage(GetHwnd(),
647 LB_GETHORIZONTALEXTENT, 0, 0L);
dd3c394a 648
fca418ae
VZ
649 ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY);
650
651 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
652 if ( extentX > existingExtent )
653 largestExtent = extentX;
2bda0e17 654 }
fca418ae
VZ
655
656 if ( largestExtent )
657 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
658 //else: it shouldn't change
2bda0e17
KB
659}
660
f68586e5 661wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
662{
663 // find the widest string
664 int wLine;
665 int wListbox = 0;
aa61d352 666 for (unsigned int i = 0; i < m_noItems; i++)
b908d224
VZ
667 {
668 wxString str(GetString(i));
669 GetTextExtent(str, &wLine, NULL);
670 if ( wLine > wListbox )
671 wListbox = wLine;
672 }
673
674 // give it some reasonable default value if there are no strings in the
675 // list
676 if ( wListbox == 0 )
677 wListbox = 100;
678
679 // the listbox should be slightly larger than the widest string
680 int cx, cy;
7a5e53ab 681 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
b908d224
VZ
682
683 wListbox += 3*cx;
684
908f91bc
RD
685 // Add room for the scrollbar
686 wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
687
5b6d6b70
VZ
688 // don't make the listbox too tall (limit height to 10 items) but don't
689 // make it too small neither
690 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
691 wxMin(wxMax(m_noItems, 3), 10);
b908d224 692
31582e4e
RD
693 wxSize best(wListbox, hListbox);
694 CacheBestSize(best);
695 return best;
b908d224
VZ
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
aa61d352 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
7d09b97f
VZ
771 {
772 wxDCTemp dc((WXHDC)hdc);
773 dc.SetFont(GetFont());
2bda0e17 774
7d09b97f
VZ
775 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
776 pStruct->itemWidth = dc.GetCharWidth();
777 }
07cf98cb 778
7d09b97f
VZ
779#ifdef __WXWINCE__
780 ReleaseDC(NULL, hdc);
781#else
07cf98cb 782 DeleteDC(hdc);
7d09b97f 783#endif
07cf98cb 784
598ddd96 785 return true;
2bda0e17
KB
786}
787
788// forward the message to the appropriate item
789bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
790{
dd3c394a 791 // only owner-drawn control should receive this message
598ddd96 792 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
dd3c394a
VZ
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 )
598ddd96 799 return false;
dd3c394a 800
a23fd0e1 801 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
2bda0e17 802
598ddd96 803 wxCHECK( data && (data != LB_ERR), false );
2bda0e17 804
dd3c394a 805 wxListBoxItem *pItem = (wxListBoxItem *)data;
2bda0e17 806
7561aacd 807 wxDCTemp dc((WXHDC)pStruct->hDC);
42841dfc
WS
808 wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top);
809 wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom);
810 wxRect rect(pt1, pt2);
2bda0e17 811
dd3c394a 812 return pItem->OnDrawItem(dc, rect,
7561aacd
VZ
813 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
814 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
815}
816
1e6feb95
VZ
817#endif // wxUSE_OWNER_DRAWN
818
819#endif // wxUSE_LISTBOX