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