]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
Add EVT_RIBBONGALLERY_CLICKED event.
[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
096d5447 152void wxListBox::Init()
2bda0e17 153{
dd3c394a 154 m_noItems = 0;
1ddb283a 155 m_updateHorizontalExtent = false;
f2eb4ad2 156 m_selectedByKeyboard = 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{
7a69cd96
VZ
168 // initialize base class fields
169 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
170 return false;
2bda0e17 171
7a69cd96 172 // create the native control
9a83f860 173 if ( !MSWCreateControl(wxT("LISTBOX"), wxEmptyString, pos, size) )
7a69cd96
VZ
174 {
175 // control creation failed
176 return false;
2bda0e17 177 }
1c089c47 178
7a69cd96
VZ
179 // initialize the contents
180 for ( int i = 0; i < n; i++ )
181 {
182 Append(choices[i]);
2bda0e17 183 }
2bda0e17 184
f022dc23 185 // now we can compute our best size correctly, so do it again
170acdc9 186 SetInitialSize(size);
7ec5921d 187
7a69cd96 188 return true;
2bda0e17
KB
189}
190
584ad2a3
MB
191bool wxListBox::Create(wxWindow *parent,
192 wxWindowID id,
193 const wxPoint& pos,
194 const wxSize& size,
195 const wxArrayString& choices,
196 long style,
76efbc2a 197 const wxValidator& validator,
584ad2a3
MB
198 const wxString& name)
199{
200 wxCArrayString chs(choices);
201 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
202 style, validator, name);
203}
204
bfc6fde4 205wxListBox::~wxListBox()
2bda0e17 206{
ad998d78 207 Clear();
2bda0e17
KB
208}
209
7a69cd96 210WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
2bda0e17 211{
7a69cd96
VZ
212 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
213
214 // we always want to get the notifications
215 msStyle |= LBS_NOTIFY;
216
217 // without this style, you get unexpected heights, so e.g. constraint
218 // layout doesn't work properly
219 msStyle |= LBS_NOINTEGRALHEIGHT;
220
221 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
9a83f860 222 wxT("only one of listbox selection modes can be specified") );
7a69cd96
VZ
223
224 if ( style & wxLB_MULTIPLE )
225 msStyle |= LBS_MULTIPLESEL;
226 else if ( style & wxLB_EXTENDED )
227 msStyle |= LBS_EXTENDEDSEL;
228
58dcd1ae 229 wxASSERT_MSG( !(style & wxLB_ALWAYS_SB) || !(style & wxLB_NO_SB),
9a83f860 230 wxT( "Conflicting styles wxLB_ALWAYS_SB and wxLB_NO_SB." ) );
58dcd1ae
VZ
231
232 if ( !(style & wxLB_NO_SB) )
233 {
234 msStyle |= WS_VSCROLL;
235 if ( style & wxLB_ALWAYS_SB )
236 msStyle |= LBS_DISABLENOSCROLL;
237 }
238
7a69cd96
VZ
239 if ( m_windowStyle & wxLB_HSCROLL )
240 msStyle |= WS_HSCROLL;
241 if ( m_windowStyle & wxLB_SORT )
242 msStyle |= LBS_SORT;
243
244#if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
245 if ( m_windowStyle & wxLB_OWNERDRAW )
246 {
247 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
248 // the strings in the listbox for simplicity even though we could have
249 // avoided it in this case
250 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
251 }
252#endif // wxUSE_OWNER_DRAWN
253
254 return msStyle;
2bda0e17
KB
255}
256
1ddb283a
RR
257void wxListBox::OnInternalIdle()
258{
259 wxWindow::OnInternalIdle();
03647350 260
1ddb283a
RR
261 if (m_updateHorizontalExtent)
262 {
263 SetHorizontalExtent(wxEmptyString);
264 m_updateHorizontalExtent = false;
265 }
266}
267
316bba0c
VZ
268void wxListBox::MSWOnItemsChanged()
269{
270 // we need to do two things when items change: update their max horizontal
271 // extent so that horizontal scrollbar could be shown or hidden as
272 // appropriate and also invlaidate the best size
273 //
274 // updating the max extent is slow (it's an O(N) operation) and so we defer
275 // it until the idle time but the best size should be invalidated
276 // immediately doing it in idle time is too late -- layout using incorrect
277 // old best size will have been already done by then
278
279 m_updateHorizontalExtent = true;
280
281 InvalidateBestSize();
282}
283
2ee3ee1b
VZ
284// ----------------------------------------------------------------------------
285// implementation of wxListBoxBase methods
286// ----------------------------------------------------------------------------
287
288void wxListBox::DoSetFirstItem(int N)
2bda0e17 289{
8228b893 290 wxCHECK_RET( IsValid(N),
223d09f6 291 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 292
2ee3ee1b 293 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
294}
295
a236aa20 296void wxListBox::DoDeleteOneItem(unsigned int n)
2bda0e17 297{
aa61d352 298 wxCHECK_RET( IsValid(n),
223d09f6 299 wxT("invalid index in wxListBox::Delete") );
dd3c394a 300
98fbab9e 301#if wxUSE_OWNER_DRAWN
e1009295
VZ
302 if ( HasFlag(wxLB_OWNERDRAW) )
303 {
304 delete m_aItems[n];
305 m_aItems.RemoveAt(n);
306 }
98fbab9e
VZ
307#endif // wxUSE_OWNER_DRAWN
308
aa61d352 309 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
dd3c394a
VZ
310 m_noItems--;
311
316bba0c 312 MSWOnItemsChanged();
84c5c49b
RR
313
314 UpdateOldSelections();
2bda0e17
KB
315}
316
11e62fe6 317int wxListBox::FindString(const wxString& s, bool bCase) const
2bda0e17 318{
11e62fe6
WS
319 // back to base class search for not native search type
320 if (bCase)
321 return wxItemContainerImmutable::FindString( s, bCase );
322
e0a050e3 323 int pos = ListBox_FindStringExact(GetHwnd(), -1, s.wx_str());
dd3c394a 324 if (pos == LB_ERR)
2ee3ee1b 325 return wxNOT_FOUND;
dd3c394a
VZ
326 else
327 return pos;
2bda0e17
KB
328}
329
a236aa20 330void wxListBox::DoClear()
2bda0e17 331{
98fbab9e 332#if wxUSE_OWNER_DRAWN
e1009295 333 if ( HasFlag(wxLB_OWNERDRAW) )
98fbab9e
VZ
334 {
335 WX_CLEAR_ARRAY(m_aItems);
336 }
337#endif // wxUSE_OWNER_DRAWN
8ee9d618
VZ
338
339 ListBox_ResetContent(GetHwnd());
340
341 m_noItems = 0;
316bba0c 342 MSWOnItemsChanged();
84c5c49b
RR
343
344 UpdateOldSelections();
8ee9d618
VZ
345}
346
c6179a84 347void wxListBox::DoSetSelection(int N, bool select)
2bda0e17 348{
8228b893 349 wxCHECK_RET( N == wxNOT_FOUND || IsValid(N),
223d09f6 350 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 351
2ee3ee1b
VZ
352 if ( HasMultipleSelection() )
353 {
a23fd0e1 354 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 355 }
dd3c394a
VZ
356 else
357 {
2ee3ee1b 358 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 359 }
84c5c49b
RR
360
361 UpdateOldSelections();
2bda0e17
KB
362}
363
2ee3ee1b 364bool wxListBox::IsSelected(int N) const
2bda0e17 365{
8228b893 366 wxCHECK_MSG( IsValid(N), false,
223d09f6 367 wxT("invalid index in wxListBox::Selected") );
dd3c394a 368
598ddd96 369 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true;
2bda0e17
KB
370}
371
aa61d352 372void *wxListBox::DoGetItemClientData(unsigned int n) const
2bda0e17 373{
8228b893 374 wxCHECK_MSG( IsValid(n), NULL,
223d09f6 375 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 376
2ee3ee1b 377 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
378}
379
aa61d352 380void wxListBox::DoSetItemClientData(unsigned int n, void *clientData)
2ee3ee1b 381{
8228b893 382 wxCHECK_RET( IsValid(n),
223d09f6 383 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 384
2ee3ee1b 385 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
43b2d5e7 386 {
223d09f6 387 wxLogDebug(wxT("LB_SETITEMDATA failed"));
43b2d5e7 388 }
2bda0e17
KB
389}
390
391// Return number of selections and an array of selected integers
14483330 392int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 393{
dd3c394a 394 aSelections.Empty();
14483330 395
2ee3ee1b 396 if ( HasMultipleSelection() )
dd3c394a 397 {
d90879fa
VZ
398 int countSel = ListBox_GetSelCount(GetHwnd());
399 if ( countSel == LB_ERR )
400 {
9a83f860 401 wxLogDebug(wxT("ListBox_GetSelCount failed"));
d90879fa
VZ
402 }
403 else if ( countSel != 0 )
404 {
405 int *selections = new int[countSel];
14483330 406
d90879fa
VZ
407 if ( ListBox_GetSelItems(GetHwnd(),
408 countSel, selections) == LB_ERR )
409 {
410 wxLogDebug(wxT("ListBox_GetSelItems failed"));
411 countSel = -1;
412 }
413 else
414 {
415 aSelections.Alloc(countSel);
416 for ( int n = 0; n < countSel; n++ )
417 aSelections.Add(selections[n]);
418 }
14483330 419
dd3c394a
VZ
420 delete [] selections;
421 }
14483330 422
d90879fa 423 return countSel;
dd3c394a
VZ
424 }
425 else // single-selection listbox
426 {
f6bcfd97
BP
427 if (ListBox_GetCurSel(GetHwnd()) > -1)
428 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 429
f6bcfd97 430 return aSelections.Count();
dd3c394a 431 }
2bda0e17
KB
432}
433
434// Get single selection, for single choice list items
14483330 435int wxListBox::GetSelection() const
2bda0e17 436{
2ee3ee1b 437 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 438 -1,
f6bcfd97 439 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
14483330 440
a23fd0e1 441 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
442}
443
444// Find string for position
aa61d352 445wxString wxListBox::GetString(unsigned int n) const
2bda0e17 446{
aa61d352 447 wxCHECK_MSG( IsValid(n), wxEmptyString,
66cf41cb 448 wxT("invalid index in wxListBox::GetString") );
dd3c394a 449
aa61d352 450 int len = ListBox_GetTextLen(GetHwnd(), n);
dd3c394a
VZ
451
452 // +1 for terminating NUL
453 wxString result;
aa61d352 454 ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1));
dd3c394a
VZ
455
456 return result;
2bda0e17
KB
457}
458
a236aa20
VZ
459int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items,
460 unsigned int pos,
461 void **clientData,
462 wxClientDataType type)
2ee3ee1b 463{
a236aa20
VZ
464 MSWAllocStorage(items, LB_INITSTORAGE);
465
466 const bool append = pos == GetCount();
467
468 // we must use CB_ADDSTRING when appending as only it works correctly for
469 // the sorted controls
470 const unsigned msg = append ? LB_ADDSTRING : LB_INSERTSTRING;
2ee3ee1b 471
a236aa20
VZ
472 if ( append )
473 pos = 0;
474
475 int n = wxNOT_FOUND;
476
477 const unsigned int numItems = items.GetCount();
478 for ( unsigned int i = 0; i < numItems; i++ )
fd7ab28c 479 {
a236aa20
VZ
480 n = MSWInsertOrAppendItem(pos, items[i], msg);
481 if ( n == wxNOT_FOUND )
482 return n;
483
484 if ( !append )
485 pos++;
486
487 ++m_noItems;
fd7ab28c
VZ
488
489#if wxUSE_OWNER_DRAWN
a236aa20 490 if ( HasFlag(wxLB_OWNERDRAW) )
9085d634 491 {
a236aa20 492 wxOwnerDrawn *pNewItem = CreateLboxItem(n);
9085d634 493 pNewItem->SetFont(GetFont());
a236aa20 494 m_aItems.Insert(pNewItem, n);
9085d634 495 }
fd7ab28c 496#endif // wxUSE_OWNER_DRAWN
a236aa20 497 AssignNewItemClientData(n, clientData, i, type);
fd7ab28c
VZ
498 }
499
316bba0c 500 MSWOnItemsChanged();
a236aa20 501
84c5c49b
RR
502 UpdateOldSelections();
503
a236aa20 504 return n;
2ee3ee1b
VZ
505}
506
85ee88cd 507int wxListBox::DoHitTestList(const wxPoint& point) const
c00fed0e 508{
44e7bad0
VZ
509 LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT,
510 0, MAKELPARAM(point.x, point.y));
c00fed0e
VZ
511
512 // non zero high-order word means that this item is outside of the client
513 // area, IOW the point is outside of the listbox
44e7bad0 514 return HIWORD(lRes) ? wxNOT_FOUND : LOWORD(lRes);
c00fed0e
VZ
515}
516
aa61d352 517void wxListBox::SetString(unsigned int n, const wxString& s)
2ee3ee1b 518{
aa61d352 519 wxCHECK_RET( IsValid(n),
2ee3ee1b
VZ
520 wxT("invalid index in wxListBox::SetString") );
521
522 // remember the state of the item
aa61d352 523 bool wasSelected = IsSelected(n);
2ee3ee1b
VZ
524
525 void *oldData = NULL;
526 wxClientData *oldObjData = NULL;
b4a11fe8 527 if ( HasClientUntypedData() )
aa61d352 528 oldData = GetClientData(n);
b4a11fe8 529 else if ( HasClientObjectData() )
aa61d352 530 oldObjData = GetClientObject(n);
2ee3ee1b
VZ
531
532 // delete and recreate it
aa61d352 533 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
2ee3ee1b 534
aa61d352
VZ
535 int newN = n;
536 if ( n == (m_noItems - 1) )
2ee3ee1b
VZ
537 newN = -1;
538
e0a050e3 539 ListBox_InsertString(GetHwnd(), newN, s.wx_str());
2ee3ee1b
VZ
540
541 // restore the client data
542 if ( oldData )
aa61d352 543 SetClientData(n, oldData);
2ee3ee1b 544 else if ( oldObjData )
aa61d352 545 SetClientObject(n, oldObjData);
2ee3ee1b 546
781bdbb4
JS
547 // we may have lost the selection
548 if ( wasSelected )
aa61d352 549 Select(n);
31582e4e 550
316bba0c 551 MSWOnItemsChanged();
2ee3ee1b
VZ
552}
553
aa61d352 554unsigned int wxListBox::GetCount() const
2ee3ee1b
VZ
555{
556 return m_noItems;
557}
558
559// ----------------------------------------------------------------------------
fca418ae 560// size-related stuff
2ee3ee1b
VZ
561// ----------------------------------------------------------------------------
562
2bda0e17
KB
563void wxListBox::SetHorizontalExtent(const wxString& s)
564{
fca418ae
VZ
565 // the rest is only necessary if we want a horizontal scrollbar
566 if ( !HasFlag(wxHSCROLL) )
dd3c394a 567 return;
dd3c394a 568
dd3c394a 569
fca418ae
VZ
570 WindowHDC dc(GetHwnd());
571 SelectInHDC selFont(dc, GetHfontOf(GetFont()));
dd3c394a 572
fca418ae
VZ
573 TEXTMETRIC lpTextMetric;
574 ::GetTextMetrics(dc, &lpTextMetric);
dd3c394a 575
fca418ae
VZ
576 int largestExtent = 0;
577 SIZE extentXY;
3e3be693 578
fca418ae
VZ
579 if ( s.empty() )
580 {
581 // set extent to the max length of all strings
582 for ( unsigned int i = 0; i < m_noItems; i++ )
dd3c394a 583 {
fca418ae 584 const wxString str = GetString(i);
767b35a5 585 ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
fca418ae 586
dd3c394a 587 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
fca418ae 588 if ( extentX > largestExtent )
dd3c394a
VZ
589 largestExtent = extentX;
590 }
fca418ae
VZ
591 }
592 else // just increase the extent to the length of this string
593 {
594 int existingExtent = (int)SendMessage(GetHwnd(),
595 LB_GETHORIZONTALEXTENT, 0, 0L);
dd3c394a 596
fca418ae
VZ
597 ::GetTextExtentPoint32(dc, s.c_str(), s.length(), &extentXY);
598
599 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
600 if ( extentX > existingExtent )
601 largestExtent = extentX;
2bda0e17 602 }
fca418ae
VZ
603
604 if ( largestExtent )
605 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
606 //else: it shouldn't change
2bda0e17
KB
607}
608
743b4266 609wxSize wxListBox::DoGetBestClientSize() const
b908d224
VZ
610{
611 // find the widest string
612 int wLine;
613 int wListbox = 0;
aa61d352 614 for (unsigned int i = 0; i < m_noItems; i++)
b908d224
VZ
615 {
616 wxString str(GetString(i));
617 GetTextExtent(str, &wLine, NULL);
618 if ( wLine > wListbox )
619 wListbox = wLine;
620 }
621
622 // give it some reasonable default value if there are no strings in the
623 // list
624 if ( wListbox == 0 )
625 wListbox = 100;
626
627 // the listbox should be slightly larger than the widest string
743b4266 628 wListbox += 3*GetCharWidth();
b908d224 629
743b4266 630 // add room for the scrollbar
908f91bc
RD
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
743b4266 635 int hListbox = SendMessage(GetHwnd(), LB_GETITEMHEIGHT, 0, 0)*
5b6d6b70 636 wxMin(wxMax(m_noItems, 3), 10);
b908d224 637
743b4266 638 return wxSize(wListbox, hListbox);
b908d224
VZ
639}
640
2ee3ee1b
VZ
641// ----------------------------------------------------------------------------
642// callbacks
643// ----------------------------------------------------------------------------
644
645bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 646{
bada28f0 647 wxEventType evtType;
f2eb4ad2 648 int n = wxNOT_FOUND;
8ee9d618 649 if ( param == LBN_SELCHANGE )
2bda0e17 650 {
53f60d4a
VZ
651 if ( HasMultipleSelection() )
652 return CalcAndSendEvent();
653
bada28f0 654 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
44e7bad0 655
f2eb4ad2
VZ
656 if ( m_selectedByKeyboard )
657 {
658 // We shouldn't use the mouse position to find the item as mouse
659 // can be anywhere, ask the listbox itself. Notice that this can't
660 // be used when the item is selected using the mouse however as
661 // LB_GETCARETINDEX will always return a valid item, even if the
662 // mouse is clicked below all the items, which is why we find the
663 // item ourselves below in this case.
664 n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
665 }
f186bd4d 666 //else: n will be determined below from the mouse position
2bda0e17 667 }
8ee9d618 668 else if ( param == LBN_DBLCLK )
2ee3ee1b 669 {
bada28f0
VZ
670 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
671 }
672 else
673 {
674 // some event we're not interested in
598ddd96 675 return false;
bada28f0
VZ
676 }
677
f186bd4d
VZ
678 // Find the item position if it was a mouse-generated selection event or a
679 // double click event (which is always generated using the mouse)
680 if ( n == wxNOT_FOUND )
681 {
682 const DWORD pos = ::GetMessagePos();
683 const wxPoint pt(GET_X_LPARAM(pos), GET_Y_LPARAM(pos));
684 n = HitTest(ScreenToClient(wxPoint(pt)));
685 }
686
687 // We get events even when mouse is clicked outside of any valid item from
688 // Windows, just ignore them.
c07b0669
VZ
689 if ( n == wxNOT_FOUND )
690 return false;
691
692 // As we don't use m_oldSelections in single selection mode, we store the
693 // last item that we notified the user about in it in this case because we
694 // need to remember it to be able to filter out the dummy LBN_SELCHANGE
695 // messages that we get when the user clicks on an already selected item.
696 if ( param == LBN_SELCHANGE )
697 {
698 if ( !m_oldSelections.empty() && *m_oldSelections.begin() == n )
699 {
700 // Same item as the last time.
701 return false;
702 }
703
704 m_oldSelections.clear();
705 m_oldSelections.push_back(n);
706 }
707
708 // Do generate an event otherwise.
709 return SendEvent(evtType, n, true /* selection */);
2bda0e17
KB
710}
711
f2eb4ad2
VZ
712WXLRESULT
713wxListBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
714{
715 // Remember whether there was a keyboard or mouse event before
716 // LBN_SELCHANGE: this allows us to correctly determine the item affected
717 // by it in MSWCommand() above in any case.
718 if ( WM_KEYFIRST <= nMsg && nMsg <= WM_KEYLAST )
719 m_selectedByKeyboard = true;
720 else if ( WM_MOUSEFIRST <= nMsg && nMsg <= WM_MOUSELAST )
721 m_selectedByKeyboard = false;
722
723 return wxListBoxBase::MSWWindowProc(nMsg, wParam, lParam);
724}
725
2ee3ee1b 726// ----------------------------------------------------------------------------
e1009295 727// owner-drawn list boxes support
2ee3ee1b 728// ----------------------------------------------------------------------------
2bda0e17 729
47d67540 730#if wxUSE_OWNER_DRAWN
2bda0e17 731
85ee88cd
VZ
732// misc overloaded methods
733// -----------------------
734
85ee88cd
VZ
735bool wxListBox::SetFont(const wxFont &font)
736{
e1009295
VZ
737 if ( HasFlag(wxLB_OWNERDRAW) )
738 {
739 const unsigned count = m_aItems.GetCount();
740 for ( unsigned i = 0; i < count; i++ )
741 m_aItems[i]->SetFont(font);
742 }
85ee88cd
VZ
743
744 wxListBoxBase::SetFont(font);
745
746 return true;
747}
748
749bool wxListBox::GetItemRect(size_t n, wxRect& rect) const
750{
751 wxCHECK_MSG( IsValid(n), false,
752 wxT("invalid index in wxListBox::GetItemRect") );
753
754 RECT rc;
755
756 if ( ListBox_GetItemRect(GetHwnd(), n, &rc) != LB_ERR )
757 {
758 rect = wxRectFromRECT(rc);
759 return true;
760 }
761 else
762 {
763 // couldn't retrieve rect: for example, item isn't visible
764 return false;
765 }
766}
767
768bool wxListBox::RefreshItem(size_t n)
769{
770 wxRect rect;
771 if ( !GetItemRect(n, rect) )
772 return false;
773
774 RECT rc;
775 wxCopyRectToRECT(rect, rc);
776
777 return ::InvalidateRect((HWND)GetHWND(), &rc, FALSE) == TRUE;
778}
779
780
2bda0e17
KB
781// drawing
782// -------
783
85ee88cd
VZ
784namespace
785{
786 // space beneath/above each row in pixels
787 static const int LISTBOX_EXTRA_SPACE = 1;
788
789} // anonymous namespace
2bda0e17
KB
790
791// the height is the same for all items
dd3c394a
VZ
792// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
793
794// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
795// message is not yet sent when we get here!
2bda0e17
KB
796bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
797{
dd3c394a 798 // only owner-drawn control should receive this message
e1009295 799 wxCHECK( HasFlag(wxLB_OWNERDRAW), false );
2bda0e17 800
dd3c394a 801 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 802
4676948b
JS
803#ifdef __WXWINCE__
804 HDC hdc = GetDC(NULL);
805#else
07cf98cb 806 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
4676948b 807#endif
07cf98cb 808
7d09b97f
VZ
809 {
810 wxDCTemp dc((WXHDC)hdc);
811 dc.SetFont(GetFont());
2bda0e17 812
85ee88cd 813 pStruct->itemHeight = dc.GetCharHeight() + 2 * LISTBOX_EXTRA_SPACE;
7d09b97f
VZ
814 pStruct->itemWidth = dc.GetCharWidth();
815 }
07cf98cb 816
7d09b97f
VZ
817#ifdef __WXWINCE__
818 ReleaseDC(NULL, hdc);
819#else
07cf98cb 820 DeleteDC(hdc);
7d09b97f 821#endif
07cf98cb 822
598ddd96 823 return true;
2bda0e17
KB
824}
825
826// forward the message to the appropriate item
827bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
828{
dd3c394a 829 // only owner-drawn control should receive this message
e1009295 830 wxCHECK( HasFlag(wxLB_OWNERDRAW), false );
dd3c394a
VZ
831
832 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
f6bcfd97
BP
833
834 // the item may be -1 for an empty listbox
e4de7a77 835 if ( pStruct->itemID == (UINT)-1 )
598ddd96 836 return false;
dd3c394a 837
e4de7a77 838 wxListBoxItem *pItem = (wxListBoxItem *)m_aItems[pStruct->itemID];
2bda0e17 839
7561aacd 840 wxDCTemp dc((WXHDC)pStruct->hDC);
2bda0e17 841
6efda5fd 842 return pItem->OnDrawItem(dc, wxRectFromRECT(pStruct->rcItem),
7561aacd 843 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
98fbab9e 844 (wxOwnerDrawn::wxODStatus)(pStruct->itemState | wxOwnerDrawn::wxODHidePrefix));
2bda0e17
KB
845}
846
1e6feb95
VZ
847#endif // wxUSE_OWNER_DRAWN
848
849#endif // wxUSE_LISTBOX