]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
Since CE cannot do the logical/device origin calculations
[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
af498247 97 wxPROPERTY( Font , wxFont , SetFont , GetFont , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
3ff066a4 98 wxPROPERTY_COLLECTION( Choices , wxArrayString , wxString , AppendString , GetStrings, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
af498247
VZ
99 wxPROPERTY( Selection ,int, SetSelection, GetSelection, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
100 wxPROPERTY_FLAGS( WindowStyle , wxListBoxStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 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 167 m_noItems = 0;
dd3c394a 168 m_selected = 0;
2bda0e17 169
7a69cd96
VZ
170 // initialize base class fields
171 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
172 return false;
2bda0e17 173
7a69cd96
VZ
174 // create the native control
175 if ( !MSWCreateControl(_T("LISTBOX"), wxEmptyString, pos, size) )
176 {
177 // control creation failed
178 return false;
2bda0e17 179 }
1c089c47 180
7a69cd96
VZ
181 // initialize the contents
182 for ( int i = 0; i < n; i++ )
183 {
184 Append(choices[i]);
2bda0e17 185 }
2bda0e17 186
7a69cd96 187 return true;
2bda0e17
KB
188}
189
584ad2a3
MB
190bool wxListBox::Create(wxWindow *parent,
191 wxWindowID id,
192 const wxPoint& pos,
193 const wxSize& size,
194 const wxArrayString& choices,
195 long style,
76efbc2a 196 const wxValidator& validator,
584ad2a3
MB
197 const wxString& name)
198{
199 wxCArrayString chs(choices);
200 return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
201 style, validator, name);
202}
203
bfc6fde4 204wxListBox::~wxListBox()
2bda0e17 205{
baccb514 206 Free();
2bda0e17
KB
207}
208
7a69cd96 209WXDWORD wxListBox::MSWGetStyle(long style, WXDWORD *exstyle) const
2bda0e17 210{
7a69cd96
VZ
211 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
212
6079ab87
VZ
213 // always show the vertical scrollbar if necessary -- otherwise it is
214 // impossible to use the control with the mouse
215 msStyle |= WS_VSCROLL;
216
7a69cd96
VZ
217 // we always want to get the notifications
218 msStyle |= LBS_NOTIFY;
219
220 // without this style, you get unexpected heights, so e.g. constraint
221 // layout doesn't work properly
222 msStyle |= LBS_NOINTEGRALHEIGHT;
223
224 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
225 _T("only one of listbox selection modes can be specified") );
226
227 if ( style & wxLB_MULTIPLE )
228 msStyle |= LBS_MULTIPLESEL;
229 else if ( style & wxLB_EXTENDED )
230 msStyle |= LBS_EXTENDEDSEL;
231
232 if ( m_windowStyle & wxLB_ALWAYS_SB )
233 msStyle |= LBS_DISABLENOSCROLL;
234 if ( m_windowStyle & wxLB_HSCROLL )
235 msStyle |= WS_HSCROLL;
236 if ( m_windowStyle & wxLB_SORT )
237 msStyle |= LBS_SORT;
238
239#if wxUSE_OWNER_DRAWN && !defined(__WXWINCE__)
240 if ( m_windowStyle & wxLB_OWNERDRAW )
241 {
242 // we don't support LBS_OWNERDRAWVARIABLE yet and we also always put
243 // the strings in the listbox for simplicity even though we could have
244 // avoided it in this case
245 msStyle |= LBS_OWNERDRAWFIXED | LBS_HASSTRINGS;
246 }
247#endif // wxUSE_OWNER_DRAWN
248
249 return msStyle;
2bda0e17
KB
250}
251
2ee3ee1b
VZ
252// ----------------------------------------------------------------------------
253// implementation of wxListBoxBase methods
254// ----------------------------------------------------------------------------
255
256void wxListBox::DoSetFirstItem(int N)
2bda0e17 257{
dd3c394a 258 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 259 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 260
2ee3ee1b 261 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
262}
263
debe6624 264void wxListBox::Delete(int N)
2bda0e17 265{
dd3c394a 266 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 267 wxT("invalid index in wxListBox::Delete") );
dd3c394a 268
07cf98cb
VZ
269 // for owner drawn objects, the data is used for storing wxOwnerDrawn
270 // pointers and we shouldn't touch it
271#if !wxUSE_OWNER_DRAWN
272 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
273#endif // !wxUSE_OWNER_DRAWN
274 if ( HasClientObjectData() )
275 {
276 delete GetClientObject(N);
277 }
6c8a980f 278
a23fd0e1 279 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
dd3c394a
VZ
280 m_noItems--;
281
2b5f62a0 282 SetHorizontalExtent(wxEmptyString);
2bda0e17
KB
283}
284
2ee3ee1b 285int wxListBox::DoAppend(const wxString& item)
2bda0e17 286{
a23fd0e1 287 int index = ListBox_AddString(GetHwnd(), item);
2ee3ee1b 288 m_noItems++;
2bda0e17 289
47d67540 290#if wxUSE_OWNER_DRAWN
2bda0e17 291 if ( m_windowStyle & wxLB_OWNERDRAW ) {
2b5f62a0 292 wxOwnerDrawn *pNewItem = CreateLboxItem(index); // dummy argument
dd3c394a 293 pNewItem->SetName(item);
fd7ab28c 294 m_aItems.Insert(pNewItem, index);
a23fd0e1 295 ListBox_SetItemData(GetHwnd(), index, pNewItem);
60c65519 296 pNewItem->SetFont(GetFont());
2bda0e17 297 }
fd7ab28c 298#endif // wxUSE_OWNER_DRAWN
2bda0e17 299
dd3c394a 300 SetHorizontalExtent(item);
dd3c394a 301
2ee3ee1b 302 return index;
2bda0e17
KB
303}
304
2ee3ee1b 305void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
2bda0e17 306{
f6bcfd97
BP
307 // avoid flicker - but don't need to do this for a hidden listbox
308 bool hideAndShow = IsShown();
309 if ( hideAndShow )
310 {
311 ShowWindow(GetHwnd(), SW_HIDE);
312 }
2ee3ee1b 313
a23fd0e1 314 ListBox_ResetContent(GetHwnd());
2ee3ee1b
VZ
315
316 m_noItems = choices.GetCount();
dd3c394a 317 int i;
2ee3ee1b 318 for (i = 0; i < m_noItems; i++)
dd3c394a 319 {
a23fd0e1 320 ListBox_AddString(GetHwnd(), choices[i]);
dd3c394a 321 if ( clientData )
2ee3ee1b 322 {
2b5f62a0 323 SetClientData(i, clientData[i]);
2ee3ee1b 324 }
dd3c394a 325 }
2bda0e17 326
47d67540 327#if wxUSE_OWNER_DRAWN
2bda0e17 328 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a 329 // first delete old items
fd7ab28c 330 WX_CLEAR_ARRAY(m_aItems);
dd3c394a
VZ
331
332 // then create new ones
fd7ab28c 333 for ( size_t ui = 0; ui < (size_t)m_noItems; ui++ ) {
2b5f62a0 334 wxOwnerDrawn *pNewItem = CreateLboxItem(ui);
dd3c394a
VZ
335 pNewItem->SetName(choices[ui]);
336 m_aItems.Add(pNewItem);
a23fd0e1 337 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
dd3c394a 338 }
2bda0e17 339 }
2ee3ee1b
VZ
340#endif // wxUSE_OWNER_DRAWN
341
342 SetHorizontalExtent();
2bda0e17 343
f6bcfd97
BP
344 if ( hideAndShow )
345 {
346 // show the listbox back if we hid it
347 ShowWindow(GetHwnd(), SW_SHOW);
348 }
2bda0e17
KB
349}
350
351int wxListBox::FindString(const wxString& s) const
352{
a23fd0e1 353 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
dd3c394a 354 if (pos == LB_ERR)
2ee3ee1b 355 return wxNOT_FOUND;
dd3c394a
VZ
356 else
357 return pos;
2bda0e17
KB
358}
359
bfc6fde4 360void wxListBox::Clear()
2bda0e17 361{
baccb514 362 Free();
8ee9d618
VZ
363
364 ListBox_ResetContent(GetHwnd());
365
366 m_noItems = 0;
367 SetHorizontalExtent();
368}
369
370void wxListBox::Free()
2b273975 371{
dd3c394a 372#if wxUSE_OWNER_DRAWN
185fa6bf
VZ
373 if ( m_windowStyle & wxLB_OWNERDRAW )
374 {
fd7ab28c 375 WX_CLEAR_ARRAY(m_aItems);
185fa6bf
VZ
376 }
377 else
378#endif // wxUSE_OWNER_DRAWN
6c8a980f
VZ
379 if ( HasClientObjectData() )
380 {
381 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
382 {
383 delete GetClientObject(n);
384 }
385 }
2bda0e17 386}
baccb514 387
debe6624 388void wxListBox::SetSelection(int N, bool select)
2bda0e17 389{
5955710c
JS
390 wxCHECK_RET( N == wxNOT_FOUND ||
391 (N >= 0 && N < m_noItems),
223d09f6 392 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 393
2ee3ee1b
VZ
394 if ( HasMultipleSelection() )
395 {
a23fd0e1 396 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 397 }
dd3c394a
VZ
398 else
399 {
2ee3ee1b 400 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 401 }
2bda0e17
KB
402}
403
2ee3ee1b 404bool wxListBox::IsSelected(int N) const
2bda0e17 405{
dd3c394a 406 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
223d09f6 407 wxT("invalid index in wxListBox::Selected") );
dd3c394a 408
a23fd0e1 409 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
2bda0e17
KB
410}
411
6c8a980f 412wxClientData* wxListBox::DoGetItemClientObject(int n) const
2bda0e17 413{
6c8a980f 414 return (wxClientData *)DoGetItemClientData(n);
2bda0e17
KB
415}
416
6c8a980f 417void *wxListBox::DoGetItemClientData(int n) const
2bda0e17 418{
2ee3ee1b 419 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
223d09f6 420 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 421
2ee3ee1b 422 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
423}
424
6c8a980f 425void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
2bda0e17 426{
6c8a980f 427 DoSetItemClientData(n, clientData);
2ee3ee1b
VZ
428}
429
6c8a980f 430void wxListBox::DoSetItemClientData(int n, void *clientData)
2ee3ee1b
VZ
431{
432 wxCHECK_RET( n >= 0 && n < m_noItems,
223d09f6 433 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 434
2ee3ee1b
VZ
435#if wxUSE_OWNER_DRAWN
436 if ( m_windowStyle & wxLB_OWNERDRAW )
437 {
438 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
439 // in OnMeasure/OnDraw.
440 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
441 }
442#endif // wxUSE_OWNER_DRAWN
443
444 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
223d09f6 445 wxLogDebug(wxT("LB_SETITEMDATA failed"));
2bda0e17
KB
446}
447
448// Return number of selections and an array of selected integers
14483330 449int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 450{
dd3c394a 451 aSelections.Empty();
14483330 452
2ee3ee1b 453 if ( HasMultipleSelection() )
dd3c394a 454 {
d90879fa
VZ
455 int countSel = ListBox_GetSelCount(GetHwnd());
456 if ( countSel == LB_ERR )
457 {
458 wxLogDebug(_T("ListBox_GetSelCount failed"));
459 }
460 else if ( countSel != 0 )
461 {
462 int *selections = new int[countSel];
14483330 463
d90879fa
VZ
464 if ( ListBox_GetSelItems(GetHwnd(),
465 countSel, selections) == LB_ERR )
466 {
467 wxLogDebug(wxT("ListBox_GetSelItems failed"));
468 countSel = -1;
469 }
470 else
471 {
472 aSelections.Alloc(countSel);
473 for ( int n = 0; n < countSel; n++ )
474 aSelections.Add(selections[n]);
475 }
14483330 476
dd3c394a
VZ
477 delete [] selections;
478 }
14483330 479
d90879fa 480 return countSel;
dd3c394a
VZ
481 }
482 else // single-selection listbox
483 {
f6bcfd97
BP
484 if (ListBox_GetCurSel(GetHwnd()) > -1)
485 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 486
f6bcfd97 487 return aSelections.Count();
dd3c394a 488 }
2bda0e17
KB
489}
490
491// Get single selection, for single choice list items
14483330 492int wxListBox::GetSelection() const
2bda0e17 493{
2ee3ee1b 494 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 495 -1,
f6bcfd97 496 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
14483330 497
a23fd0e1 498 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
499}
500
501// Find string for position
debe6624 502wxString wxListBox::GetString(int N) const
2bda0e17 503{
2b5f62a0 504 wxCHECK_MSG( N >= 0 && N < m_noItems, wxEmptyString,
223d09f6 505 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 506
a23fd0e1 507 int len = ListBox_GetTextLen(GetHwnd(), N);
dd3c394a
VZ
508
509 // +1 for terminating NUL
510 wxString result;
de564874 511 ListBox_GetText(GetHwnd(), N, wxStringBuffer(result, len + 1));
dd3c394a
VZ
512
513 return result;
2bda0e17
KB
514}
515
2ee3ee1b
VZ
516void
517wxListBox::DoInsertItems(const wxArrayString& items, int pos)
518{
519 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
520 wxT("invalid index in wxListBox::InsertItems") );
521
522 int nItems = items.GetCount();
523 for ( int i = 0; i < nItems; i++ )
fd7ab28c
VZ
524 {
525 int idx = ListBox_InsertString(GetHwnd(), i + pos, items[i]);
526
527#if wxUSE_OWNER_DRAWN
9085d634
RD
528 if ( m_windowStyle & wxLB_OWNERDRAW )
529 {
2b5f62a0 530 wxOwnerDrawn *pNewItem = CreateLboxItem(idx);
9085d634
RD
531 pNewItem->SetName(items[i]);
532 pNewItem->SetFont(GetFont());
533 m_aItems.Insert(pNewItem, idx);
fd7ab28c 534
9085d634
RD
535 ListBox_SetItemData(GetHwnd(), idx, pNewItem);
536 }
fd7ab28c
VZ
537#endif // wxUSE_OWNER_DRAWN
538 }
539
2ee3ee1b
VZ
540 m_noItems += nItems;
541
542 SetHorizontalExtent();
543}
544
545void wxListBox::SetString(int N, const wxString& s)
546{
547 wxCHECK_RET( N >= 0 && N < m_noItems,
548 wxT("invalid index in wxListBox::SetString") );
549
550 // remember the state of the item
551 bool wasSelected = IsSelected(N);
552
553 void *oldData = NULL;
554 wxClientData *oldObjData = NULL;
1e6feb95 555 if ( m_clientDataItemsType == wxClientData_Void )
2ee3ee1b 556 oldData = GetClientData(N);
1e6feb95 557 else if ( m_clientDataItemsType == wxClientData_Object )
2ee3ee1b
VZ
558 oldObjData = GetClientObject(N);
559
560 // delete and recreate it
561 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
562
563 int newN = N;
564 if ( N == m_noItems - 1 )
565 newN = -1;
566
567 ListBox_InsertString(GetHwnd(), newN, s);
568
569 // restore the client data
570 if ( oldData )
571 SetClientData(N, oldData);
572 else if ( oldObjData )
573 SetClientObject(N, oldObjData);
574
2ee3ee1b
VZ
575#if wxUSE_OWNER_DRAWN
576 if ( m_windowStyle & wxLB_OWNERDRAW )
c294f450 577 {
2ee3ee1b
VZ
578 // update item's text
579 m_aItems[N]->SetName(s);
fd7ab28c 580
c294f450
RD
581 // reassign the item's data
582 ListBox_SetItemData(GetHwnd(), N, m_aItems[N]);
583 }
2ee3ee1b 584#endif //USE_OWNER_DRAWN
781bdbb4
JS
585
586 // we may have lost the selection
587 if ( wasSelected )
588 Select(N);
2ee3ee1b
VZ
589}
590
591int wxListBox::GetCount() const
592{
593 return m_noItems;
594}
595
596// ----------------------------------------------------------------------------
597// helpers
598// ----------------------------------------------------------------------------
599
4438caf4
VZ
600// Windows-specific code to set the horizontal extent of the listbox, if
601// necessary. If s is non-NULL, it's used to calculate the horizontal extent.
2bda0e17
KB
602// Otherwise, all strings are used.
603void wxListBox::SetHorizontalExtent(const wxString& s)
604{
dd3c394a
VZ
605 // Only necessary if we want a horizontal scrollbar
606 if (!(m_windowStyle & wxHSCROLL))
607 return;
608 TEXTMETRIC lpTextMetric;
609
2ee3ee1b 610 if ( !s.IsEmpty() )
2bda0e17 611 {
a23fd0e1
VZ
612 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
613 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
614 HFONT oldFont = 0;
615 if (GetFont().Ok() && GetFont().GetResourceHandle())
616 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
617
618 GetTextMetrics(dc, &lpTextMetric);
619 SIZE extentXY;
837e5743 620 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
dd3c394a
VZ
621 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
622
623 if (oldFont)
624 ::SelectObject(dc, oldFont);
625
a23fd0e1 626 ReleaseDC(GetHwnd(), dc);
dd3c394a 627 if (extentX > existingExtent)
a23fd0e1 628 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
dd3c394a
VZ
629 }
630 else
631 {
632 int largestExtent = 0;
a23fd0e1 633 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
634 HFONT oldFont = 0;
635 if (GetFont().Ok() && GetFont().GetResourceHandle())
636 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
637
638 GetTextMetrics(dc, &lpTextMetric);
3e3be693
VZ
639
640 // FIXME: buffer overflow!!
641 wxChar buf[1024];
642 for (int i = 0; i < m_noItems; i++)
dd3c394a 643 {
3e3be693
VZ
644 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LPARAM)buf);
645 buf[len] = 0;
dd3c394a 646 SIZE extentXY;
3e3be693 647 ::GetTextExtentPoint(dc, buf, len, &extentXY);
dd3c394a
VZ
648 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
649 if (extentX > largestExtent)
650 largestExtent = extentX;
651 }
652 if (oldFont)
653 ::SelectObject(dc, oldFont);
654
a23fd0e1
VZ
655 ReleaseDC(GetHwnd(), dc);
656 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
2bda0e17 657 }
2bda0e17
KB
658}
659
f68586e5 660wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
661{
662 // find the widest string
663 int wLine;
664 int wListbox = 0;
665 for ( int i = 0; i < m_noItems; i++ )
666 {
667 wxString str(GetString(i));
668 GetTextExtent(str, &wLine, NULL);
669 if ( wLine > wListbox )
670 wListbox = wLine;
671 }
672
673 // give it some reasonable default value if there are no strings in the
674 // list
675 if ( wListbox == 0 )
676 wListbox = 100;
677
678 // the listbox should be slightly larger than the widest string
679 int cx, cy;
680 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
681
682 wListbox += 3*cx;
683
5b6d6b70
VZ
684 // don't make the listbox too tall (limit height to 10 items) but don't
685 // make it too small neither
686 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*
687 wxMin(wxMax(m_noItems, 3), 10);
b908d224
VZ
688
689 return wxSize(wListbox, hListbox);
690}
691
2ee3ee1b
VZ
692// ----------------------------------------------------------------------------
693// callbacks
694// ----------------------------------------------------------------------------
695
696bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 697{
bada28f0 698 wxEventType evtType;
8ee9d618 699 if ( param == LBN_SELCHANGE )
2bda0e17 700 {
bada28f0 701 evtType = wxEVT_COMMAND_LISTBOX_SELECTED;
2bda0e17 702 }
8ee9d618 703 else if ( param == LBN_DBLCLK )
2ee3ee1b 704 {
bada28f0
VZ
705 evtType = wxEVT_COMMAND_LISTBOX_DOUBLECLICKED;
706 }
707 else
708 {
709 // some event we're not interested in
710 return FALSE;
711 }
712
713 wxCommandEvent event(evtType, m_windowId);
714 event.SetEventObject( this );
8ee9d618 715
9c9c3d7a
VZ
716 // retrieve the affected item
717 int n = SendMessage(GetHwnd(), LB_GETCARETINDEX, 0, 0);
718 if ( n != LB_ERR )
bada28f0 719 {
bada28f0
VZ
720 if ( HasClientObjectData() )
721 event.SetClientObject( GetClientObject(n) );
722 else if ( HasClientUntypedData() )
723 event.SetClientData( GetClientData(n) );
9c9c3d7a 724
bada28f0 725 event.SetString( GetString(n) );
9c9c3d7a 726 event.SetExtraLong( HasMultipleSelection() ? IsSelected(n) : TRUE );
bada28f0
VZ
727 }
728
729 event.m_commandInt = n;
2ee3ee1b 730
bada28f0 731 return GetEventHandler()->ProcessEvent(event);
2bda0e17
KB
732}
733
2ee3ee1b
VZ
734// ----------------------------------------------------------------------------
735// wxCheckListBox support
736// ----------------------------------------------------------------------------
2bda0e17 737
47d67540 738#if wxUSE_OWNER_DRAWN
2bda0e17
KB
739
740// drawing
741// -------
742
743// space beneath/above each row in pixels
744// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
745#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
746
747// the height is the same for all items
dd3c394a
VZ
748// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
749
750// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
751// message is not yet sent when we get here!
2bda0e17
KB
752bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
753{
dd3c394a
VZ
754 // only owner-drawn control should receive this message
755 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
2bda0e17 756
dd3c394a 757 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 758
4676948b
JS
759#ifdef __WXWINCE__
760 HDC hdc = GetDC(NULL);
761#else
07cf98cb 762 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
4676948b 763#endif
07cf98cb 764
dd3c394a 765 wxDC dc;
07cf98cb 766 dc.SetHDC((WXHDC)hdc);
a756f210 767 dc.SetFont(wxSystemSettings::GetFont(wxSYS_ANSI_VAR_FONT));
2bda0e17 768
dd3c394a
VZ
769 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
770 pStruct->itemWidth = dc.GetCharWidth();
2bda0e17 771
07cf98cb
VZ
772 dc.SetHDC(0);
773
774 DeleteDC(hdc);
775
dd3c394a 776 return TRUE;
2bda0e17
KB
777}
778
779// forward the message to the appropriate item
780bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
781{
dd3c394a
VZ
782 // only owner-drawn control should receive this message
783 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
784
785 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
f6bcfd97
BP
786 UINT itemID = pStruct->itemID;
787
788 // the item may be -1 for an empty listbox
789 if ( itemID == (UINT)-1 )
790 return FALSE;
dd3c394a 791
a23fd0e1 792 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
2bda0e17 793
dd3c394a 794 wxCHECK( data && (data != LB_ERR), FALSE );
2bda0e17 795
dd3c394a 796 wxListBoxItem *pItem = (wxListBoxItem *)data;
2bda0e17 797
7561aacd 798 wxDCTemp dc((WXHDC)pStruct->hDC);
dd3c394a 799 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
f6bcfd97 800 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
2bda0e17 801
dd3c394a 802 return pItem->OnDrawItem(dc, rect,
7561aacd
VZ
803 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
804 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
805}
806
1e6feb95
VZ
807#endif // wxUSE_OWNER_DRAWN
808
809#endif // wxUSE_LISTBOX