]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
fixing infinite recursion
[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 34#include "wx/msw/private.h"
74052fe8 35#include "wx/msw/dc.h"
1e6feb95 36
5ea105e0
RR
37#include <windowsx.h>
38
0c589ad0
BM
39#if wxUSE_OWNER_DRAWN
40 #include "wx/ownerdrw.h"
41#endif
2bda0e17 42
6a89f9ee 43#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
44WX_DEFINE_FLAGS( wxListBoxStyle )
45
3ff066a4 46wxBEGIN_FLAGS( wxListBoxStyle )
bc9fb572
JS
47 // new style border flags, we put them first to
48 // use them for streaming out
3ff066a4
SC
49 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
50 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
51 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
52 wxFLAGS_MEMBER(wxBORDER_RAISED)
53 wxFLAGS_MEMBER(wxBORDER_STATIC)
54 wxFLAGS_MEMBER(wxBORDER_NONE)
7ec5921d 55
bc9fb572 56 // old style border flags
3ff066a4
SC
57 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
58 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
59 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
60 wxFLAGS_MEMBER(wxRAISED_BORDER)
61 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 62 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
63
64 // standard window styles
3ff066a4
SC
65 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
66 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
67 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
68 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 69 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
70 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
71 wxFLAGS_MEMBER(wxVSCROLL)
72 wxFLAGS_MEMBER(wxHSCROLL)
73
74 wxFLAGS_MEMBER(wxLB_SINGLE)
75 wxFLAGS_MEMBER(wxLB_MULTIPLE)
76 wxFLAGS_MEMBER(wxLB_EXTENDED)
77 wxFLAGS_MEMBER(wxLB_HSCROLL)
78 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
79 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
58dcd1ae 80 wxFLAGS_MEMBER(wxLB_NO_SB)
3ff066a4
SC
81 wxFLAGS_MEMBER(wxLB_SORT)
82
83wxEND_FLAGS( wxListBoxStyle )
bc9fb572 84
0ad2a19e 85IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControlWithItems,"wx/listbox.h")
6a89f9ee 86
3ff066a4 87wxBEGIN_PROPERTIES_TABLE(wxListBox)
665b71b1
WS
88 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent )
89 wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent )
c5ca409b 90
af498247 91 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4 92 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
665b71b1 93 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247 94 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 95wxEND_PROPERTIES_TABLE()
6a89f9ee 96
3ff066a4
SC
97wxBEGIN_HANDLERS_TABLE(wxListBox)
98wxEND_HANDLERS_TABLE()
6a89f9ee 99
7ec5921d 100wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
6a89f9ee 101#else
0ad2a19e 102IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
6a89f9ee 103#endif
2bda0e17 104
066f1b7a
SC
105/*
106TODO PROPERTIES
665b71b1
WS
107 selection
108 content
109 item
066f1b7a
SC
110*/
111
2bda0e17
KB
112// ============================================================================
113// list box item declaration and implementation
114// ============================================================================
115
47d67540 116#if wxUSE_OWNER_DRAWN
2bda0e17
KB
117
118class wxListBoxItem : public wxOwnerDrawn
119{
120public:
98fbab9e
VZ
121 wxListBoxItem(wxListBox *parent)
122 { m_parent = parent; }
2bda0e17 123
98fbab9e
VZ
124 wxListBox *GetParent() const
125 { return m_parent; }
126
127 int GetIndex() const
128 { return m_parent->GetItemIndex(const_cast<wxListBoxItem*>(this)); }
129
130 wxString GetName() const
131 { return m_parent->GetString(GetIndex()); }
132
133private:
134 wxListBox *m_parent;
135};
2bda0e17 136
2b5f62a0 137wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
2bda0e17 138{
98fbab9e 139 return new wxListBoxItem(this);
2bda0e17
KB
140}
141
142#endif //USE_OWNER_DRAWN
143
144// ============================================================================
145// list box control implementation
146// ============================================================================
147
2ee3ee1b
VZ
148// ----------------------------------------------------------------------------
149// creation
150// ----------------------------------------------------------------------------
dd3c394a 151
2ee3ee1b 152// Listbox item
bfc6fde4 153wxListBox::wxListBox()
2bda0e17 154{
dd3c394a 155 m_noItems = 0;
1ddb283a 156 m_updateHorizontalExtent = false;
2bda0e17
KB
157}
158
dd3c394a
VZ
159bool wxListBox::Create(wxWindow *parent,
160 wxWindowID id,
2bda0e17
KB
161 const wxPoint& pos,
162 const wxSize& size,
debe6624
JS
163 int n, const wxString choices[],
164 long style,
56a44c64 165 const wxValidator& validator,
2bda0e17
KB
166 const wxString& name)
167{
dd3c394a 168 m_noItems = 0;
1ddb283a 169 m_updateHorizontalExtent = false;
2bda0e17 170
7a69cd96
VZ
171 // initialize base class fields
172 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
173 return false;
2bda0e17 174
7a69cd96 175 // create the native control
9a83f860 176 if ( !MSWCreateControl(wxT("LISTBOX"), wxEmptyString, pos, size) )
7a69cd96
VZ
177 {
178 // control creation failed
179 return false;
2bda0e17 180 }
1c089c47 181
7a69cd96
VZ
182 // initialize the contents
183 for ( int i = 0; i < n; i++ )
184 {
185 Append(choices[i]);
2bda0e17 186 }
2bda0e17 187
f022dc23 188 // now we can compute our best size correctly, so do it again
170acdc9 189 SetInitialSize(size);
7ec5921d 190
7a69cd96 191 return true;
2bda0e17
KB
192}
193
584ad2a3
MB
194bool wxListBox::Create(wxWindow *parent,
195 wxWindowID id,
196 const wxPoint& pos,
197 const wxSize& size,
198 const wxArrayString& choices,
199 long style,
76efbc2a 200 const wxValidator& validator,
584ad2a3
MB
201 const wxString& name)
202{
203 wxCArrayString chs(choices);
204 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
205 style, validator, name);
206}
207
bfc6fde4 208wxListBox::~wxListBox()
2bda0e17 209{
ad998d78 210 Clear();
2bda0e17
KB
211}
212
7a69cd96 213WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
2bda0e17 214{
7a69cd96
VZ
215 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
216
217 // we always want to get the notifications
218 msStyle |= LBS_NOTIFY;
219
220 // without this style, you get unexpected heights, so e.g. constraint
221 // layout doesn't work properly
222 msStyle |= LBS_NOINTEGRALHEIGHT;
223
224 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
9a83f860 225 wxT("only one of listbox selection modes can be specified") );
7a69cd96
VZ
226
227 if ( style & wxLB_MULTIPLE )
228 msStyle |= LBS_MULTIPLESEL;
229 else if ( style & wxLB_EXTENDED )
230 msStyle |= LBS_EXTENDEDSEL;
231
58dcd1ae 232 wxASSERT_MSG( !(style & wxLB_ALWAYS_SB) || !(style & wxLB_NO_SB),
9a83f860 233 wxT( "Conflicting styles wxLB_ALWAYS_SB and wxLB_NO_SB." ) );
58dcd1ae
VZ
234
235 if ( !(style & wxLB_NO_SB) )
236 {
237 msStyle |= WS_VSCROLL;
238 if ( style & wxLB_ALWAYS_SB )
239 msStyle |= LBS_DISABLENOSCROLL;
240 }
241
7a69cd96
VZ
242 if ( m_windowStyle & wxLB_HSCROLL )
243 msStyle |= WS_HSCROLL;
244 if ( m_windowStyle & wxLB_SORT )
245 msStyle |= LBS_SORT;
246
247#if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
248 if ( m_windowStyle & wxLB_OWNERDRAW )
249 {
250 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
251 // the strings in the listbox for simplicity even though we could have
252 // avoided it in this case
253 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
254 }
255#endif // wxUSE_OWNER_DRAWN
256
257 return msStyle;
2bda0e17
KB
258}
259
1ddb283a
RR
260void wxListBox::OnInternalIdle()
261{
262 wxWindow::OnInternalIdle();
03647350 263
1ddb283a
RR
264 if (m_updateHorizontalExtent)
265 {
266 SetHorizontalExtent(wxEmptyString);
267 m_updateHorizontalExtent = false;
268 }
269}
270
316bba0c
VZ
271void wxListBox::MSWOnItemsChanged()
272{
273 // we need to do two things when items change: update their max horizontal
274 // extent so that horizontal scrollbar could be shown or hidden as
275 // appropriate and also invlaidate the best size
276 //
277 // updating the max extent is slow (it's an O(N) operation) and so we defer
278 // it until the idle time but the best size should be invalidated
279 // immediately doing it in idle time is too late -- layout using incorrect
280 // old best size will have been already done by then
281
282 m_updateHorizontalExtent = true;
283
284 InvalidateBestSize();
285}
286
2ee3ee1b
VZ
287// ----------------------------------------------------------------------------
288// implementation of wxListBoxBase methods
289// ----------------------------------------------------------------------------
290
291void wxListBox::DoSetFirstItem(int N)
2bda0e17 292{
8228b893 293 wxCHECK_RET( IsValid(N),
223d09f6 294 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 295
2ee3ee1b 296 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
297}
298
a236aa20 299void wxListBox::DoDeleteOneItem(unsigned int n)
2bda0e17 300{
aa61d352 301 wxCHECK_RET( IsValid(n),
223d09f6 302 wxT("invalid index in wxListBox::Delete") );
dd3c394a 303
98fbab9e 304#if wxUSE_OWNER_DRAWN
e1009295
VZ
305 if ( HasFlag(wxLB_OWNERDRAW) )
306 {
307 delete m_aItems[n];
308 m_aItems.RemoveAt(n);
309 }
98fbab9e
VZ
310#endif // wxUSE_OWNER_DRAWN
311
aa61d352 312 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
dd3c394a
VZ
313 m_noItems--;
314
316bba0c 315 MSWOnItemsChanged();
84c5c49b
RR
316
317 UpdateOldSelections();
2bda0e17
KB
318}
319
11e62fe6 320int wxListBox::FindString(const wxString& s, bool bCase) const
2bda0e17 321{
11e62fe6
WS
322 // back to base class search for not native search type
323 if (bCase)
324 return wxItemContainerImmutable::FindString( s, bCase );
325
e0a050e3 326 int pos = ListBox_FindStringExact(GetHwnd(), -1, s.wx_str());
dd3c394a 327 if (pos == LB_ERR)
2ee3ee1b 328 return wxNOT_FOUND;
dd3c394a
VZ
329 else
330 return pos;
2bda0e17
KB
331}
332
a236aa20 333void wxListBox::DoClear()
2bda0e17 334{
98fbab9e 335#if wxUSE_OWNER_DRAWN
e1009295 336 if ( HasFlag(wxLB_OWNERDRAW) )
98fbab9e
VZ
337 {
338 WX_CLEAR_ARRAY(m_aItems);
339 }
340#endif // wxUSE_OWNER_DRAWN
8ee9d618
VZ
341
342 ListBox_ResetContent(GetHwnd());
343
344 m_noItems = 0;
316bba0c 345 MSWOnItemsChanged();
84c5c49b
RR
346
347 UpdateOldSelections();
8ee9d618
VZ
348}
349
c6179a84 350void wxListBox::DoSetSelection(int N, bool select)
2bda0e17 351{
8228b893 352 wxCHECK_RET( N == wxNOT_FOUND || IsValid(N),
223d09f6 353 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 354
2ee3ee1b
VZ
355 if ( HasMultipleSelection() )
356 {
a23fd0e1 357 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 358 }
dd3c394a
VZ
359 else
360 {
2ee3ee1b 361 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 362 }
84c5c49b
RR
363
364 UpdateOldSelections();
2bda0e17
KB
365}
366
2ee3ee1b 367bool wxListBox::IsSelected(int N) const
2bda0e17 368{
8228b893 369 wxCHECK_MSG( IsValid(N), false,
223d09f6 370 wxT("invalid index in wxListBox::Selected") );
dd3c394a 371
598ddd96 372 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true;
2bda0e17
KB
373}
374
aa61d352 375void *wxListBox::DoGetItemClientData(unsigned int n) const
2bda0e17 376{
8228b893 377 wxCHECK_MSG( IsValid(n), NULL,
223d09f6 378 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 379
2ee3ee1b 380 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
381}
382
aa61d352 383void wxListBox::DoSetItemClientData(unsigned int n, void *clientData)
2ee3ee1b 384{
8228b893 385 wxCHECK_RET( IsValid(n),
223d09f6 386 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 387
2ee3ee1b 388 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
43b2d5e7 389 {
223d09f6 390 wxLogDebug(wxT("LB_SETITEMDATA failed"));
43b2d5e7 391 }
2bda0e17
KB
392}
393
394// Return number of selections and an array of selected integers
14483330 395int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 396{
dd3c394a 397 aSelections.Empty();
14483330 398
2ee3ee1b 399 if ( HasMultipleSelection() )
dd3c394a 400 {
d90879fa
VZ
401 int countSel = ListBox_GetSelCount(GetHwnd());
402 if ( countSel == LB_ERR )
403 {
9a83f860 404 wxLogDebug(wxT("ListBox_GetSelCount failed"));
d90879fa
VZ
405 }
406 else if ( countSel != 0 )
407 {
408 int *selections = new int[countSel];
14483330 409
d90879fa
VZ
410 if ( ListBox_GetSelItems(GetHwnd(),
411 countSel, selections) == LB_ERR )
412 {
413 wxLogDebug(wxT("ListBox_GetSelItems failed"));
414 countSel = -1;
415 }
416 else
417 {
418 aSelections.Alloc(countSel);
419 for ( int n = 0; n < countSel; n++ )
420 aSelections.Add(selections[n]);
421 }
14483330 422
dd3c394a
VZ
423 delete [] selections;
424 }
14483330 425
d90879fa 426 return countSel;
dd3c394a
VZ
427 }
428 else // single-selection listbox
429 {
f6bcfd97
BP
430 if (ListBox_GetCurSel(GetHwnd()) > -1)
431 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 432
f6bcfd97 433 return aSelections.Count();
dd3c394a 434 }
2bda0e17
KB
435}
436
437// Get single selection, for single choice list items
14483330 438int wxListBox::GetSelection() const
2bda0e17 439{
2ee3ee1b 440 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 441 -1,
f6bcfd97 442 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
14483330 443
a23fd0e1 444 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
445}
446
447// Find string for position
aa61d352 448wxString wxListBox::GetString(unsigned int n) const
2bda0e17 449{
aa61d352 450 wxCHECK_MSG( IsValid(n), wxEmptyString,
66cf41cb 451 wxT("invalid index in wxListBox::GetString") );
dd3c394a 452
aa61d352 453 int len = ListBox_GetTextLen(GetHwnd(), n);
dd3c394a
VZ
454
455 // +1 for terminating NUL
456 wxString result;
aa61d352 457 ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1));
dd3c394a
VZ
458
459 return result;
2bda0e17
KB
460}
461
a236aa20
VZ
462int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items,
463 unsigned int pos,
464 void **clientData,
465 wxClientDataType type)
2ee3ee1b 466{
a236aa20
VZ
467 MSWAllocStorage(items, LB_INITSTORAGE);
468
469 const bool append = pos == GetCount();
470
471 // we must use CB_ADDSTRING when appending as only it works correctly for
472 // the sorted controls
473 const unsigned msg = append ? LB_ADDSTRING : LB_INSERTSTRING;
2ee3ee1b 474
a236aa20
VZ
475 if ( append )
476 pos = 0;
477
478 int n = wxNOT_FOUND;
479
480 const unsigned int numItems = items.GetCount();
481 for ( unsigned int i = 0; i < numItems; i++ )
fd7ab28c 482 {
a236aa20
VZ
483 n = MSWInsertOrAppendItem(pos, items[i], msg);
484 if ( n == wxNOT_FOUND )
485 return n;
486
487 if ( !append )
488 pos++;
489
490 ++m_noItems;
fd7ab28c
VZ
491
492#if wxUSE_OWNER_DRAWN
a236aa20 493 if ( HasFlag(wxLB_OWNERDRAW) )
9085d634 494 {
a236aa20 495 wxOwnerDrawn *pNewItem = CreateLboxItem(n);
9085d634 496 pNewItem->SetFont(GetFont());
a236aa20 497 m_aItems.Insert(pNewItem, n);
9085d634 498 }
fd7ab28c 499#endif // wxUSE_OWNER_DRAWN
a236aa20 500 AssignNewItemClientData(n, clientData, i, type);
fd7ab28c
VZ
501 }
502
316bba0c 503 MSWOnItemsChanged();
a236aa20 504
84c5c49b
RR
505 UpdateOldSelections();
506
a236aa20 507 return n;
2ee3ee1b
VZ
508}
509
85ee88cd 510int wxListBox::DoHitTestList(const wxPoint& point) const
c00fed0e 511{
44e7bad0
VZ
512 LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT,
513 0, MAKELPARAM(point.x, point.y));
c00fed0e
VZ
514
515 // non zero high-order word means that this item is outside of the client
516 // area, IOW the point is outside of the listbox
44e7bad0 517 return HIWORD(lRes) ? wxNOT_FOUND : LOWORD(lRes);
c00fed0e
VZ
518}
519
aa61d352 520void wxListBox::SetString(unsigned int n, const wxString& s)
2ee3ee1b 521{
aa61d352 522 wxCHECK_RET( IsValid(n),
2ee3ee1b
VZ
523 wxT("invalid index in wxListBox::SetString") );
524
525 // remember the state of the item
aa61d352 526 bool wasSelected = IsSelected(n);
2ee3ee1b
VZ
527
528 void *oldData = NULL;
529 wxClientData *oldObjData = NULL;
b4a11fe8 530 if ( HasClientUntypedData() )
aa61d352 531 oldData = GetClientData(n);
b4a11fe8 532 else if ( HasClientObjectData() )
aa61d352 533 oldObjData = GetClientObject(n);
2ee3ee1b
VZ
534
535 // delete and recreate it
aa61d352 536 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
2ee3ee1b 537
aa61d352
VZ
538 int newN = n;
539 if ( n == (m_noItems - 1) )
2ee3ee1b
VZ
540 newN = -1;
541
e0a050e3 542 ListBox_InsertString(GetHwnd(), newN, s.wx_str());
2ee3ee1b
VZ
543
544 // restore the client data
545 if ( oldData )
aa61d352 546 SetClientData(n, oldData);
2ee3ee1b 547 else if ( oldObjData )
aa61d352 548 SetClientObject(n, oldObjData);
2ee3ee1b 549
781bdbb4
JS
550 // we may have lost the selection
551 if ( wasSelected )
aa61d352 552 Select(n);
31582e4e 553
316bba0c 554 MSWOnItemsChanged();
2ee3ee1b
VZ
555}
556
aa61d352 557unsigned int wxListBox::GetCount() const
2ee3ee1b
VZ
558{
559 return m_noItems;
560}
561
562// ----------------------------------------------------------------------------
fca418ae 563// size-related stuff
2ee3ee1b
VZ
564// ----------------------------------------------------------------------------
565
2bda0e17
KB
566void wxListBox::SetHorizontalExtent(const wxString& s)
567{
fca418ae
VZ
568 // the rest is only necessary if we want a horizontal scrollbar
569 if ( !HasFlag(wxHSCROLL) )
dd3c394a 570 return;
dd3c394a 571
dd3c394a 572
fca418ae
VZ
573 WindowHDC dc(GetHwnd());
574 SelectInHDC selFont(dc, GetHfontOf(GetFont()));
dd3c394a 575
fca418ae
VZ
576 TEXTMETRIC lpTextMetric;
577 ::GetTextMetrics(dc, &lpTextMetric);
dd3c394a 578
fca418ae
VZ
579 int largestExtent = 0;
580 SIZE extentXY;
3e3be693 581
fca418ae
VZ
582 if ( s.empty() )
583 {
584 // set extent to the max length of all strings
585 for ( unsigned int i = 0; i < m_noItems; i++ )
dd3c394a 586 {
fca418ae 587 const wxString str = GetString(i);
767b35a5 588 ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
fca418ae 589
dd3c394a 590 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
fca418ae 591 if ( extentX > largestExtent )
dd3c394a
VZ
592 largestExtent = extentX;
593 }
fca418ae
VZ
594 }
595 else // just increase the extent to the length of this string
596 {
597 int existingExtent = (int)SendMessage(GetHwnd(),
598 LB_GETHORIZONTALEXTENT, 0, 0L);
dd3c394a 599
fca418ae
VZ
600 ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY);
601
602 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
603 if ( extentX > existingExtent )
604 largestExtent = extentX;
2bda0e17 605 }
fca418ae
VZ
606
607 if ( largestExtent )
608 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
609 //else: it shouldn't change
2bda0e17
KB
610}
611
743b4266 612wxSize wxListBox::DoGetBestClientSize() const
b908d224
VZ
613{
614 // find the widest string
615 int wLine;
616 int wListbox = 0;
aa61d352 617 for (unsigned int i = 0; i < m_noItems; i++)
b908d224
VZ
618 {
619 wxString str(GetString(i));
620 GetTextExtent(str, &wLine, NULL);
621 if ( wLine > wListbox )
622 wListbox = wLine;
623 }
624
625 // give it some reasonable default value if there are no strings in the
626 // list
627 if ( wListbox == 0 )
628 wListbox = 100;
629
630 // the listbox should be slightly larger than the widest string
743b4266 631 wListbox += 3*GetCharWidth();
b908d224 632
743b4266 633 // add room for the scrollbar
908f91bc
RD
634 wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
635
5b6d6b70
VZ
636 // don't make the listbox too tall (limit height to 10 items) but don't
637 // make it too small neither
743b4266 638 int hListbox = SendMessage(GetHwnd(), LB_GETITEMHEIGHT, 0, 0)*
5b6d6b70 639 wxMin(wxMax(m_noItems, 3), 10);
b908d224 640
743b4266 641 return wxSize(wListbox, hListbox);
b908d224
VZ
642}
643
2ee3ee1b
VZ
644// ----------------------------------------------------------------------------
645// callbacks
646// ----------------------------------------------------------------------------
647
648bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 649{
84c5c49b
RR
650 if ((param == LBN_SELCHANGE) && HasMultipleSelection())
651 {
652 CalcAndSendEvent();
653 return true;
654 }
655
bada28f0 656 wxEventType evtType;
44e7bad0 657 int n;
8ee9d618 658 if ( param == LBN_SELCHANGE )
2bda0e17 659 {
bada28f0 660 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
44e7bad0
VZ
661 n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
662
663 // NB: conveniently enough, LB_ERR is the same as wxNOT_FOUND
2bda0e17 664 }
8ee9d618 665 else if ( param == LBN_DBLCLK )
2ee3ee1b 666 {
bada28f0 667 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
44e7bad0 668 n = HitTest(ScreenToClient(wxGetMousePosition()));
bada28f0
VZ
669 }
670 else
671 {
672 // some event we're not interested in
598ddd96 673 return false;
bada28f0
VZ
674 }
675
9c9c3d7a 676 // retrieve the affected item
44e7bad0 677 if ( n == wxNOT_FOUND )
84c5c49b 678 return false;
bada28f0 679
44e7bad0
VZ
680 wxCommandEvent event(evtType, m_windowId);
681 event.SetEventObject(this);
682
683 if ( HasClientObjectData() )
684 event.SetClientObject( GetClientObject(n) );
685 else if ( HasClientUntypedData() )
686 event.SetClientData( GetClientData(n) );
687
688 event.SetString(GetString(n));
687706f5 689 event.SetInt(n);
2ee3ee1b 690
937013e0 691 return HandleWindowEvent(event);
2bda0e17
KB
692}
693
2ee3ee1b 694// ----------------------------------------------------------------------------
e1009295 695// owner-drawn list boxes support
2ee3ee1b 696// ----------------------------------------------------------------------------
2bda0e17 697
47d67540 698#if wxUSE_OWNER_DRAWN
2bda0e17 699
85ee88cd
VZ
700// misc overloaded methods
701// -----------------------
702
85ee88cd
VZ
703bool wxListBox::SetFont(const wxFont &font)
704{
e1009295
VZ
705 if ( HasFlag(wxLB_OWNERDRAW) )
706 {
707 const unsigned count = m_aItems.GetCount();
708 for ( unsigned i = 0; i < count; i++ )
709 m_aItems[i]->SetFont(font);
710 }
85ee88cd
VZ
711
712 wxListBoxBase::SetFont(font);
713
714 return true;
715}
716
717bool wxListBox::GetItemRect(size_t n, wxRect& rect) const
718{
719 wxCHECK_MSG( IsValid(n), false,
720 wxT("invalid index in wxListBox::GetItemRect") );
721
722 RECT rc;
723
724 if ( ListBox_GetItemRect(GetHwnd(), n, &rc) != LB_ERR )
725 {
726 rect = wxRectFromRECT(rc);
727 return true;
728 }
729 else
730 {
731 // couldn't retrieve rect: for example, item isn't visible
732 return false;
733 }
734}
735
736bool wxListBox::RefreshItem(size_t n)
737{
738 wxRect rect;
739 if ( !GetItemRect(n, rect) )
740 return false;
741
742 RECT rc;
743 wxCopyRectToRECT(rect, rc);
744
745 return ::InvalidateRect((HWND)GetHWND(), &rc, FALSE) == TRUE;
746}
747
748
2bda0e17
KB
749// drawing
750// -------
751
85ee88cd
VZ
752namespace
753{
754 // space beneath/above each row in pixels
755 static const int LISTBOX_EXTRA_SPACE = 1;
756
757} // anonymous namespace
2bda0e17
KB
758
759// the height is the same for all items
dd3c394a
VZ
760// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
761
762// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
763// message is not yet sent when we get here!
2bda0e17
KB
764bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
765{
dd3c394a 766 // only owner-drawn control should receive this message
e1009295 767 wxCHECK( HasFlag(wxLB_OWNERDRAW), false );
2bda0e17 768
dd3c394a 769 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 770
4676948b
JS
771#ifdef __WXWINCE__
772 HDC hdc = GetDC(NULL);
773#else
07cf98cb 774 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
4676948b 775#endif
07cf98cb 776
7d09b97f
VZ
777 {
778 wxDCTemp dc((WXHDC)hdc);
779 dc.SetFont(GetFont());
2bda0e17 780
85ee88cd 781 pStruct->itemHeight = dc.GetCharHeight() + 2 * LISTBOX_EXTRA_SPACE;
7d09b97f
VZ
782 pStruct->itemWidth = dc.GetCharWidth();
783 }
07cf98cb 784
7d09b97f
VZ
785#ifdef __WXWINCE__
786 ReleaseDC(NULL, hdc);
787#else
07cf98cb 788 DeleteDC(hdc);
7d09b97f 789#endif
07cf98cb 790
598ddd96 791 return true;
2bda0e17
KB
792}
793
794// forward the message to the appropriate item
795bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
796{
dd3c394a 797 // only owner-drawn control should receive this message
e1009295 798 wxCHECK( HasFlag(wxLB_OWNERDRAW), false );
dd3c394a
VZ
799
800 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
f6bcfd97
BP
801
802 // the item may be -1 for an empty listbox
e4de7a77 803 if ( pStruct->itemID == (UINT)-1 )
598ddd96 804 return false;
dd3c394a 805
e4de7a77 806 wxListBoxItem *pItem = (wxListBoxItem *)m_aItems[pStruct->itemID];
2bda0e17 807
7561aacd 808 wxDCTemp dc((WXHDC)pStruct->hDC);
2bda0e17 809
6efda5fd 810 return pItem->OnDrawItem(dc, wxRectFromRECT(pStruct->rcItem),
7561aacd 811 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
98fbab9e 812 (wxOwnerDrawn::wxODStatus)(pStruct->itemState | wxOwnerDrawn::wxODHidePrefix));
2bda0e17
KB
813}
814
1e6feb95
VZ
815#endif // wxUSE_OWNER_DRAWN
816
817#endif // wxUSE_LISTBOX