]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
fixing translation
[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
2bda0e17 21#ifndef WX_PRECOMP
ad9835c9
WS
22 #include "wx/dynarray.h"
23 #include "wx/listbox.h"
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"
2bda0e17
KB
30#endif
31
1e6feb95
VZ
32#include "wx/window.h"
33#include "wx/msw/private.h"
34
5ea105e0
RR
35#include <windowsx.h>
36
0c589ad0
BM
37#if wxUSE_OWNER_DRAWN
38 #include "wx/ownerdrw.h"
39#endif
2bda0e17 40
6a89f9ee 41#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
42WX_DEFINE_FLAGS( wxListBoxStyle )
43
3ff066a4 44wxBEGIN_FLAGS( wxListBoxStyle )
bc9fb572
JS
45 // new style border flags, we put them first to
46 // use them for streaming out
3ff066a4
SC
47 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
48 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
49 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
50 wxFLAGS_MEMBER(wxBORDER_RAISED)
51 wxFLAGS_MEMBER(wxBORDER_STATIC)
52 wxFLAGS_MEMBER(wxBORDER_NONE)
7ec5921d 53
bc9fb572 54 // old style border flags
3ff066a4
SC
55 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
56 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
57 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
58 wxFLAGS_MEMBER(wxRAISED_BORDER)
59 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 60 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
61
62 // standard window styles
3ff066a4
SC
63 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
64 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
65 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
66 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 67 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
68 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
69 wxFLAGS_MEMBER(wxVSCROLL)
70 wxFLAGS_MEMBER(wxHSCROLL)
71
72 wxFLAGS_MEMBER(wxLB_SINGLE)
73 wxFLAGS_MEMBER(wxLB_MULTIPLE)
74 wxFLAGS_MEMBER(wxLB_EXTENDED)
75 wxFLAGS_MEMBER(wxLB_HSCROLL)
76 wxFLAGS_MEMBER(wxLB_ALWAYS_SB)
77 wxFLAGS_MEMBER(wxLB_NEEDED_SB)
78 wxFLAGS_MEMBER(wxLB_SORT)
79
80wxEND_FLAGS( wxListBoxStyle )
bc9fb572 81
6a89f9ee
SC
82IMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox, wxControl,"wx/listbox.h")
83
3ff066a4 84wxBEGIN_PROPERTIES_TABLE(wxListBox)
665b71b1
WS
85 wxEVENT_PROPERTY( Select , wxEVT_COMMAND_LISTBOX_SELECTED , wxCommandEvent )
86 wxEVENT_PROPERTY( DoubleClick , wxEVT_COMMAND_LISTBOX_DOUBLECLICKED , wxCommandEvent )
c5ca409b 87
af498247 88 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4 89 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
665b71b1 90 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247 91 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 92wxEND_PROPERTIES_TABLE()
6a89f9ee 93
3ff066a4
SC
94wxBEGIN_HANDLERS_TABLE(wxListBox)
95wxEND_HANDLERS_TABLE()
6a89f9ee 96
7ec5921d 97wxCONSTRUCTOR_4( wxListBox , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size )
6a89f9ee 98#else
9c9c3d7a 99IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
6a89f9ee 100#endif
2bda0e17 101
066f1b7a
SC
102/*
103TODO PROPERTIES
665b71b1
WS
104 selection
105 content
106 item
066f1b7a
SC
107*/
108
2bda0e17
KB
109// ============================================================================
110// list box item declaration and implementation
111// ============================================================================
112
47d67540 113#if wxUSE_OWNER_DRAWN
2bda0e17
KB
114
115class wxListBoxItem : public wxOwnerDrawn
116{
117public:
2b5f62a0 118 wxListBoxItem(const wxString& str = wxEmptyString);
2bda0e17
KB
119};
120
598ddd96 121wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, false)
2bda0e17 122{
dd3c394a
VZ
123 // no bitmaps/checkmarks
124 SetMarginWidth(0);
2bda0e17
KB
125}
126
2b5f62a0 127wxOwnerDrawn *wxListBox::CreateLboxItem(size_t WXUNUSED(n))
2bda0e17 128{
dd3c394a 129 return new wxListBoxItem();
2bda0e17
KB
130}
131
132#endif //USE_OWNER_DRAWN
133
134// ============================================================================
135// list box control implementation
136// ============================================================================
137
2ee3ee1b
VZ
138// ----------------------------------------------------------------------------
139// creation
140// ----------------------------------------------------------------------------
dd3c394a 141
2ee3ee1b 142// Listbox item
bfc6fde4 143wxListBox::wxListBox()
2bda0e17 144{
dd3c394a
VZ
145 m_noItems = 0;
146 m_selected = 0;
2bda0e17
KB
147}
148
dd3c394a
VZ
149bool wxListBox::Create(wxWindow *parent,
150 wxWindowID id,
2bda0e17
KB
151 const wxPoint& pos,
152 const wxSize& size,
debe6624
JS
153 int n, const wxString choices[],
154 long style,
56a44c64 155 const wxValidator& validator,
2bda0e17
KB
156 const wxString& name)
157{
dd3c394a 158 m_noItems = 0;
dd3c394a 159 m_selected = 0;
2bda0e17 160
7a69cd96
VZ
161 // initialize base class fields
162 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
163 return false;
2bda0e17 164
7a69cd96
VZ
165 // create the native control
166 if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) )
167 {
168 // control creation failed
169 return false;
2bda0e17 170 }
1c089c47 171
7a69cd96
VZ
172 // initialize the contents
173 for ( int i = 0; i < n; i++ )
174 {
175 Append(choices[i]);
2bda0e17 176 }
2bda0e17 177
7ec5921d
VZ
178 // now we can compute our best size correctly, so do it if necessary
179 SetBestSize(size);
180
7a69cd96 181 return true;
2bda0e17
KB
182}
183
584ad2a3
MB
184bool wxListBox::Create(wxWindow *parent,
185 wxWindowID id,
186 const wxPoint& pos,
187 const wxSize& size,
188 const wxArrayString& choices,
189 long style,
76efbc2a 190 const wxValidator& validator,
584ad2a3
MB
191 const wxString& name)
192{
193 wxCArrayString chs(choices);
194 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
195 style, validator, name);
196}
197
bfc6fde4 198wxListBox::~wxListBox()
2bda0e17 199{
baccb514 200 Free();
2bda0e17
KB
201}
202
7a69cd96 203WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
2bda0e17 204{
7a69cd96
VZ
205 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
206
6079ab87
VZ
207 // always show the vertical scrollbar if necessary -- otherwise it is
208 // impossible to use the control with the mouse
209 msStyle |= WS_VSCROLL;
210
7a69cd96
VZ
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),
219 _T("only one of listbox selection modes can be specified") );
220
221 if ( style & wxLB_MULTIPLE )
222 msStyle |= LBS_MULTIPLESEL;
223 else if ( style & wxLB_EXTENDED )
224 msStyle |= LBS_EXTENDEDSEL;
225
226 if ( m_windowStyle & wxLB_ALWAYS_SB )
227 msStyle |= LBS_DISABLENOSCROLL;
228 if ( m_windowStyle & wxLB_HSCROLL )
229 msStyle |= WS_HSCROLL;
230 if ( m_windowStyle & wxLB_SORT )
231 msStyle |= LBS_SORT;
232
233#if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
234 if ( m_windowStyle & wxLB_OWNERDRAW )
235 {
236 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
237 // the strings in the listbox for simplicity even though we could have
238 // avoided it in this case
239 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
240 }
241#endif // wxUSE_OWNER_DRAWN
242
243 return msStyle;
2bda0e17
KB
244}
245
2ee3ee1b
VZ
246// ----------------------------------------------------------------------------
247// implementation of wxListBoxBase methods
248// ----------------------------------------------------------------------------
249
250void wxListBox::DoSetFirstItem(int N)
2bda0e17 251{
8228b893 252 wxCHECK_RET( IsValid(N),
223d09f6 253 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 254
2ee3ee1b 255 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
256}
257
aa61d352 258void wxListBox::Delete(unsigned int n)
2bda0e17 259{
aa61d352 260 wxCHECK_RET( IsValid(n),
223d09f6 261 wxT("invalid index in wxListBox::Delete") );
dd3c394a 262
07cf98cb
VZ
263 // for owner drawn objects, the data is used for storing wxOwnerDrawn
264 // pointers and we shouldn't touch it
265#if !wxUSE_OWNER_DRAWN
266 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
267#endif // !wxUSE_OWNER_DRAWN
268 if ( HasClientObjectData() )
269 {
aa61d352 270 delete GetClientObject(n);
07cf98cb 271 }
6c8a980f 272
aa61d352 273 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
dd3c394a
VZ
274 m_noItems--;
275
2b5f62a0 276 SetHorizontalExtent(wxEmptyString);
31582e4e
RD
277
278 InvalidateBestSize();
2bda0e17
KB
279}
280
2ee3ee1b 281int wxListBox::DoAppend(const wxString& item)
2bda0e17 282{
a23fd0e1 283 int index = ListBox_AddString(GetHwnd(), item);
2ee3ee1b 284 m_noItems++;
2bda0e17 285
47d67540 286#if wxUSE_OWNER_DRAWN
2bda0e17 287 if ( m_windowStyle & wxLB_OWNERDRAW ) {
2b5f62a0 288 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
dd3c394a 289 pNewItem->SetName(item);
fd7ab28c 290 m_aItems.Insert(pNewItem, index);
a23fd0e1 291 ListBox_SetItemData(GetHwnd(), index, pNewItem);
60c65519 292 pNewItem->SetFont(GetFont());
2bda0e17 293 }
fd7ab28c 294#endif // wxUSE_OWNER_DRAWN
2bda0e17 295
dd3c394a 296 SetHorizontalExtent(item);
dd3c394a 297
31582e4e 298 InvalidateBestSize();
2ee3ee1b 299 return index;
2bda0e17
KB
300}
301
2ee3ee1b 302void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
2bda0e17 303{
f6bcfd97
BP
304 // avoid flicker - but don't need to do this for a hidden listbox
305 bool hideAndShow = IsShown();
306 if ( hideAndShow )
307 {
308 ShowWindow(GetHwnd(), SW_HIDE);
309 }
2ee3ee1b 310
a23fd0e1 311 ListBox_ResetContent(GetHwnd());
2ee3ee1b
VZ
312
313 m_noItems = choices.GetCount();
aa61d352 314 unsigned int i;
2ee3ee1b 315 for (i = 0; i < m_noItems; i++)
dd3c394a 316 {
a23fd0e1 317 ListBox_AddString(GetHwnd(), choices[i]);
dd3c394a 318 if ( clientData )
2ee3ee1b 319 {
2b5f62a0 320 SetClientData(i, clientData[i]);
2ee3ee1b 321 }
dd3c394a 322 }
2bda0e17 323
47d67540 324#if wxUSE_OWNER_DRAWN
2bda0e17 325 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a 326 // first delete old items
fd7ab28c 327 WX_CLEAR_ARRAY(m_aItems);
dd3c394a
VZ
328
329 // then create new ones
aa61d352 330 for ( unsigned int ui = 0; ui < m_noItems; ui++ ) {
2b5f62a0 331 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
dd3c394a
VZ
332 pNewItem->SetName(choices[ui]);
333 m_aItems.Add(pNewItem);
a23fd0e1 334 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
dd3c394a 335 }
2bda0e17 336 }
2ee3ee1b
VZ
337#endif // wxUSE_OWNER_DRAWN
338
339 SetHorizontalExtent();
2bda0e17 340
f6bcfd97
BP
341 if ( hideAndShow )
342 {
343 // show the listbox back if we hid it
344 ShowWindow(GetHwnd(), SW_SHOW);
345 }
31582e4e
RD
346
347 InvalidateBestSize();
2bda0e17
KB
348}
349
11e62fe6 350int wxListBox::FindString(const wxString& s, bool bCase) const
2bda0e17 351{
11e62fe6
WS
352 // back to base class search for not native search type
353 if (bCase)
354 return wxItemContainerImmutable::FindString( s, bCase );
355
4a10ea8b 356 int pos = ListBox_FindStringExact(GetHwnd(), -1, s);
dd3c394a 357 if (pos == LB_ERR)
2ee3ee1b 358 return wxNOT_FOUND;
dd3c394a
VZ
359 else
360 return pos;
2bda0e17
KB
361}
362
bfc6fde4 363void wxListBox::Clear()
2bda0e17 364{
baccb514 365 Free();
8ee9d618
VZ
366
367 ListBox_ResetContent(GetHwnd());
368
369 m_noItems = 0;
370 SetHorizontalExtent();
31582e4e
RD
371
372 InvalidateBestSize();
8ee9d618
VZ
373}
374
375void wxListBox::Free()
2b273975 376{
dd3c394a 377#if wxUSE_OWNER_DRAWN
185fa6bf
VZ
378 if ( m_windowStyle & wxLB_OWNERDRAW )
379 {
fd7ab28c 380 WX_CLEAR_ARRAY(m_aItems);
185fa6bf
VZ
381 }
382 else
383#endif // wxUSE_OWNER_DRAWN
6c8a980f
VZ
384 if ( HasClientObjectData() )
385 {
aa61d352 386 for ( unsigned int n = 0; n < m_noItems; n++ )
6c8a980f
VZ
387 {
388 delete GetClientObject(n);
389 }
390 }
2bda0e17 391}
baccb514 392
c6179a84 393void wxListBox::DoSetSelection(int N, bool select)
2bda0e17 394{
8228b893 395 wxCHECK_RET( N == wxNOT_FOUND || IsValid(N),
223d09f6 396 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 397
2ee3ee1b
VZ
398 if ( HasMultipleSelection() )
399 {
a23fd0e1 400 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 401 }
dd3c394a
VZ
402 else
403 {
2ee3ee1b 404 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 405 }
2bda0e17
KB
406}
407
2ee3ee1b 408bool wxListBox::IsSelected(int N) const
2bda0e17 409{
8228b893 410 wxCHECK_MSG( IsValid(N), false,
223d09f6 411 wxT("invalid index in wxListBox::Selected") );
dd3c394a 412
598ddd96 413 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? false : true;
2bda0e17
KB
414}
415
aa61d352 416wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
2bda0e17 417{
6c8a980f 418 return (wxClientData *)DoGetItemClientData(n);
2bda0e17
KB
419}
420
aa61d352 421void *wxListBox::DoGetItemClientData(unsigned int n) const
2bda0e17 422{
8228b893 423 wxCHECK_MSG( IsValid(n), NULL,
223d09f6 424 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 425
2ee3ee1b 426 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
427}
428
aa61d352 429void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
2bda0e17 430{
6c8a980f 431 DoSetItemClientData(n, clientData);
2ee3ee1b
VZ
432}
433
aa61d352 434void wxListBox::DoSetItemClientData(unsigned int n, void *clientData)
2ee3ee1b 435{
8228b893 436 wxCHECK_RET( IsValid(n),
223d09f6 437 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 438
2ee3ee1b
VZ
439#if wxUSE_OWNER_DRAWN
440 if ( m_windowStyle & wxLB_OWNERDRAW )
441 {
442 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
443 // in OnMeasure/OnDraw.
444 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
445 }
446#endif // wxUSE_OWNER_DRAWN
447
448 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
223d09f6 449 wxLogDebug(wxT("LB_SETITEMDATA failed"));
2bda0e17
KB
450}
451
452// Return number of selections and an array of selected integers
14483330 453int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 454{
dd3c394a 455 aSelections.Empty();
14483330 456
2ee3ee1b 457 if ( HasMultipleSelection() )
dd3c394a 458 {
d90879fa
VZ
459 int countSel = ListBox_GetSelCount(GetHwnd());
460 if ( countSel == LB_ERR )
461 {
462 wxLogDebug(_T("ListBox_GetSelCount failed"));
463 }
464 else if ( countSel != 0 )
465 {
466 int *selections = new int[countSel];
14483330 467
d90879fa
VZ
468 if ( ListBox_GetSelItems(GetHwnd(),
469 countSel, selections) == LB_ERR )
470 {
471 wxLogDebug(wxT("ListBox_GetSelItems failed"));
472 countSel = -1;
473 }
474 else
475 {
476 aSelections.Alloc(countSel);
477 for ( int n = 0; n < countSel; n++ )
478 aSelections.Add(selections[n]);
479 }
14483330 480
dd3c394a
VZ
481 delete [] selections;
482 }
14483330 483
d90879fa 484 return countSel;
dd3c394a
VZ
485 }
486 else // single-selection listbox
487 {
f6bcfd97
BP
488 if (ListBox_GetCurSel(GetHwnd()) > -1)
489 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 490
f6bcfd97 491 return aSelections.Count();
dd3c394a 492 }
2bda0e17
KB
493}
494
495// Get single selection, for single choice list items
14483330 496int wxListBox::GetSelection() const
2bda0e17 497{
2ee3ee1b 498 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 499 -1,
f6bcfd97 500 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
14483330 501
a23fd0e1 502 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
503}
504
505// Find string for position
aa61d352 506wxString wxListBox::GetString(unsigned int n) const
2bda0e17 507{
aa61d352 508 wxCHECK_MSG( IsValid(n), wxEmptyString,
66cf41cb 509 wxT("invalid index in wxListBox::GetString") );
dd3c394a 510
aa61d352 511 int len = ListBox_GetTextLen(GetHwnd(), n);
dd3c394a
VZ
512
513 // +1 for terminating NUL
514 wxString result;
aa61d352 515 ListBox_GetText(GetHwnd(), n, (wxChar*)wxStringBuffer(result, len + 1));
dd3c394a
VZ
516
517 return result;
2bda0e17
KB
518}
519
2ee3ee1b 520void
aa61d352 521wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
2ee3ee1b 522{
8228b893 523 wxCHECK_RET( IsValidInsert(pos),
2ee3ee1b
VZ
524 wxT("invalid index in wxListBox::InsertItems") );
525
aa61d352
VZ
526 unsigned int nItems = items.GetCount();
527 for ( unsigned int i = 0; i < nItems; i++ )
fd7ab28c
VZ
528 {
529 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
530
531#if wxUSE_OWNER_DRAWN
9085d634
RD
532 if ( m_windowStyle & wxLB_OWNERDRAW )
533 {
2b5f62a0 534 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
9085d634
RD
535 pNewItem->SetName(items[i]);
536 pNewItem->SetFont(GetFont());
537 m_aItems.Insert(pNewItem, idx);
fd7ab28c 538
9085d634
RD
539 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
540 }
665b71b1
WS
541#else
542 wxUnusedVar(idx);
fd7ab28c
VZ
543#endif // wxUSE_OWNER_DRAWN
544 }
545
2ee3ee1b
VZ
546 m_noItems += nItems;
547
548 SetHorizontalExtent();
31582e4e
RD
549
550 InvalidateBestSize();
2ee3ee1b
VZ
551}
552
c00fed0e
VZ
553int wxListBox::DoListHitTest(const wxPoint& point) const
554{
8228b893 555 LRESULT lRes = ::SendMessage(GetHwnd(), LB_ITEMFROMPOINT,
c00fed0e
VZ
556 0L, MAKELONG(point.x, point.y));
557
558 // non zero high-order word means that this item is outside of the client
559 // area, IOW the point is outside of the listbox
560 return HIWORD(lRes) ? wxNOT_FOUND : lRes;
561}
562
aa61d352 563void wxListBox::SetString(unsigned int n, const wxString& s)
2ee3ee1b 564{
aa61d352 565 wxCHECK_RET( IsValid(n),
2ee3ee1b
VZ
566 wxT("invalid index in wxListBox::SetString") );
567
568 // remember the state of the item
aa61d352 569 bool wasSelected = IsSelected(n);
2ee3ee1b
VZ
570
571 void *oldData = NULL;
572 wxClientData *oldObjData = NULL;
1e6feb95 573 if ( m_clientDataItemsType == wxClientData_Void )
aa61d352 574 oldData = GetClientData(n);
1e6feb95 575 else if ( m_clientDataItemsType == wxClientData_Object )
aa61d352 576 oldObjData = GetClientObject(n);
2ee3ee1b
VZ
577
578 // delete and recreate it
aa61d352 579 SendMessage(GetHwnd(), LB_DELETESTRING, n, 0);
2ee3ee1b 580
aa61d352
VZ
581 int newN = n;
582 if ( n == (m_noItems - 1) )
2ee3ee1b
VZ
583 newN = -1;
584
585 ListBox_InsertString(GetHwnd(), newN, s);
586
587 // restore the client data
588 if ( oldData )
aa61d352 589 SetClientData(n, oldData);
2ee3ee1b 590 else if ( oldObjData )
aa61d352 591 SetClientObject(n, oldObjData);
2ee3ee1b 592
2ee3ee1b
VZ
593#if wxUSE_OWNER_DRAWN
594 if ( m_windowStyle & wxLB_OWNERDRAW )
c294f450 595 {
2ee3ee1b 596 // update item's text
aa61d352 597 m_aItems[n]->SetName(s);
fd7ab28c 598
c294f450 599 // reassign the item's data
aa61d352 600 ListBox_SetItemData(GetHwnd(), n, m_aItems[n]);
c294f450 601 }
2ee3ee1b 602#endif //USE_OWNER_DRAWN
781bdbb4
JS
603
604 // we may have lost the selection
605 if ( wasSelected )
aa61d352 606 Select(n);
31582e4e
RD
607
608 InvalidateBestSize();
2ee3ee1b
VZ
609}
610
aa61d352 611unsigned int wxListBox::GetCount() const
2ee3ee1b
VZ
612{
613 return m_noItems;
614}
615
616// ----------------------------------------------------------------------------
617// helpers
618// ----------------------------------------------------------------------------
619
4438caf4
VZ
620// Windows-specific code to set the horizontal extent of the listbox, if
621// necessary. If s is non-NULL, it's used to calculate the horizontal extent.
2bda0e17
KB
622// Otherwise, all strings are used.
623void wxListBox::SetHorizontalExtent(const wxString& s)
624{
dd3c394a
VZ
625 // Only necessary if we want a horizontal scrollbar
626 if (!(m_windowStyle & wxHSCROLL))
627 return;
628 TEXTMETRIC lpTextMetric;
629
c34924f7 630 if ( !s.empty() )
2bda0e17 631 {
a23fd0e1
VZ
632 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
633 HDC dc = GetWindowDC(GetHwnd());
dd3c394a 634 HFONT oldFont = 0;
6305fabe 635 if (GetFont().Ok() && GetFont().GetResourceHandle() != 0)
dd3c394a
VZ
636 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
637
638 GetTextMetrics(dc, &lpTextMetric);
639 SIZE extentXY;
8228b893 640 ::GetTextExtentPoint32(dc, (LPTSTR) (const wxChar *)s, s.length(), &extentXY);
dd3c394a
VZ
641 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
642
643 if (oldFont)
644 ::SelectObject(dc, oldFont);
645
a23fd0e1 646 ReleaseDC(GetHwnd(), dc);
dd3c394a 647 if (extentX > existingExtent)
a23fd0e1 648 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
dd3c394a
VZ
649 }
650 else
651 {
652 int largestExtent = 0;
a23fd0e1 653 HDC dc = GetWindowDC(GetHwnd());
dd3c394a 654 HFONT oldFont = 0;
6305fabe 655 if (GetFont().Ok() && GetFont().GetResourceHandle() != 0)
dd3c394a
VZ
656 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
657
658 GetTextMetrics(dc, &lpTextMetric);
3e3be693 659
aa61d352 660 for (unsigned int i = 0; i < m_noItems; i++)
dd3c394a 661 {
50c6bf91 662 wxString str = GetString(i);
dd3c394a 663 SIZE extentXY;
767b35a5 664 ::GetTextExtentPoint32(dc, str.c_str(), str.length(), &extentXY);
dd3c394a
VZ
665 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
666 if (extentX > largestExtent)
667 largestExtent = extentX;
668 }
669 if (oldFont)
670 ::SelectObject(dc, oldFont);
671
a23fd0e1
VZ
672 ReleaseDC(GetHwnd(), dc);
673 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
2bda0e17 674 }
2bda0e17
KB
675}
676
f68586e5 677wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
678{
679 // find the widest string
680 int wLine;
681 int wListbox = 0;
aa61d352 682 for (unsigned int i = 0; i < m_noItems; i++)
b908d224
VZ
683 {
684 wxString str(GetString(i));
685 GetTextExtent(str, &wLine, NULL);
686 if ( wLine > wListbox )
687 wListbox = wLine;
688 }
689
690 // give it some reasonable default value if there are no strings in the
691 // list
692 if ( wListbox == 0 )
693 wListbox = 100;
694
695 // the listbox should be slightly larger than the widest string
696 int cx, cy;
7a5e53ab 697 wxGetCharSize(GetHWND(), &cx, &cy, GetFont());
b908d224
VZ
698
699 wListbox += 3*cx;
700
908f91bc
RD
701 // Add room for the scrollbar
702 wListbox += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
703
5b6d6b70
VZ
704 // don't make the listbox too tall (limit height to 10 items) but don't
705 // make it too small neither
706 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
707 wxMin(wxMax(m_noItems, 3), 10);
b908d224 708
31582e4e
RD
709 wxSize best(wListbox, hListbox);
710 CacheBestSize(best);
711 return best;
b908d224
VZ
712}
713
2ee3ee1b
VZ
714// ----------------------------------------------------------------------------
715// callbacks
716// ----------------------------------------------------------------------------
717
718bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 719{
bada28f0 720 wxEventType evtType;
8ee9d618 721 if ( param == LBN_SELCHANGE )
2bda0e17 722 {
bada28f0 723 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
2bda0e17 724 }
8ee9d618 725 else if ( param == LBN_DBLCLK )
2ee3ee1b 726 {
bada28f0
VZ
727 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
728 }
729 else
730 {
731 // some event we're not interested in
598ddd96 732 return false;
bada28f0
VZ
733 }
734
735 wxCommandEvent event(evtType, m_windowId);
736 event.SetEventObject( this );
8ee9d618 737
9c9c3d7a
VZ
738 // retrieve the affected item
739 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
740 if ( n != LB_ERR )
bada28f0 741 {
bada28f0
VZ
742 if ( HasClientObjectData() )
743 event.SetClientObject( GetClientObject(n) );
744 else if ( HasClientUntypedData() )
745 event.SetClientData( GetClientData(n) );
9c9c3d7a 746
aa61d352 747 event.SetString(GetString(n));
598ddd96 748 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : true );
bada28f0
VZ
749 }
750
687706f5 751 event.SetInt(n);
2ee3ee1b 752
bada28f0 753 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
754}
755
2ee3ee1b
VZ
756// ----------------------------------------------------------------------------
757// wxCheckListBox support
758// ----------------------------------------------------------------------------
2bda0e17 759
47d67540 760#if wxUSE_OWNER_DRAWN
2bda0e17
KB
761
762// drawing
763// -------
764
765// space beneath/above each row in pixels
766// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
767#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
768
769// the height is the same for all items
dd3c394a
VZ
770// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
771
772// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
773// message is not yet sent when we get here!
2bda0e17
KB
774bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
775{
dd3c394a 776 // only owner-drawn control should receive this message
598ddd96 777 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
2bda0e17 778
dd3c394a 779 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 780
4676948b
JS
781#ifdef __WXWINCE__
782 HDC hdc = GetDC(NULL);
783#else
07cf98cb 784 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
4676948b 785#endif
07cf98cb 786
7d09b97f
VZ
787 {
788 wxDCTemp dc((WXHDC)hdc);
789 dc.SetFont(GetFont());
2bda0e17 790
7d09b97f
VZ
791 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
792 pStruct->itemWidth = dc.GetCharWidth();
793 }
07cf98cb 794
7d09b97f
VZ
795#ifdef __WXWINCE__
796 ReleaseDC(NULL, hdc);
797#else
07cf98cb 798 DeleteDC(hdc);
7d09b97f 799#endif
07cf98cb 800
598ddd96 801 return true;
2bda0e17
KB
802}
803
804// forward the message to the appropriate item
805bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
806{
dd3c394a 807 // only owner-drawn control should receive this message
598ddd96 808 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), false );
dd3c394a
VZ
809
810 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
f6bcfd97
BP
811 UINT itemID = pStruct->itemID;
812
813 // the item may be -1 for an empty listbox
814 if ( itemID == (UINT)-1 )
598ddd96 815 return false;
dd3c394a 816
a23fd0e1 817 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
2bda0e17 818
598ddd96 819 wxCHECK( data && (data != LB_ERR), false );
2bda0e17 820
dd3c394a 821 wxListBoxItem *pItem = (wxListBoxItem *)data;
2bda0e17 822
7561aacd 823 wxDCTemp dc((WXHDC)pStruct->hDC);
42841dfc
WS
824 wxPoint pt1(pStruct->rcItem.left, pStruct->rcItem.top);
825 wxPoint pt2(pStruct->rcItem.right, pStruct->rcItem.bottom);
826 wxRect rect(pt1, pt2);
2bda0e17 827
dd3c394a 828 return pItem->OnDrawItem(dc, rect,
7561aacd
VZ
829 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
830 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
831}
832
1e6feb95
VZ
833#endif // wxUSE_OWNER_DRAWN
834
835#endif // wxUSE_LISTBOX