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