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