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