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