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