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