]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
fixed wxComboBox sizing problem again
[wxWidgets.git] / src / msw / listbox.cpp
CommitLineData
2bda0e17
KB
1///////////////////////////////////////////////////////////////////////////////
2// Name: listbox.cpp
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
0c589ad0
BM
23#include "wx/window.h"
24#include "wx/msw/private.h"
25
2bda0e17 26#ifndef WX_PRECOMP
0c589ad0
BM
27#include "wx/listbox.h"
28#include "wx/settings.h"
29#include "wx/brush.h"
30#include "wx/font.h"
31#include "wx/dc.h"
2662e49e 32#include "wx/utils.h"
2bda0e17
KB
33#endif
34
5ea105e0
RR
35#include <windowsx.h>
36
37#ifdef __WXWINE__
38 #if defined(GetWindowStyle)
39 #undef GetWindowStyle
40 #endif
41#endif
42
0c589ad0
BM
43#include "wx/dynarray.h"
44#include "wx/log.h"
2bda0e17 45
0c589ad0
BM
46#if wxUSE_OWNER_DRAWN
47 #include "wx/ownerdrw.h"
48#endif
2bda0e17 49
57c208c5 50#ifndef __TWIN32__
c42404a5
VZ
51 #ifdef __GNUWIN32_OLD__
52 #include "wx/msw/gnuwin32/extra.h"
dd3c394a 53 #endif
57c208c5 54#endif
2bda0e17 55
5ea105e0
RR
56#ifdef __WXWINE__
57 #ifndef ListBox_SetItemData
58 #define ListBox_SetItemData(hwndCtl, index, data) \
59 ((int)(DWORD)SendMessage((hwndCtl), LB_SETITEMDATA, (WPARAM)(int)(index), (LPARAM)(data)))
60 #endif
61 #ifndef ListBox_GetHorizontalExtent
62 #define ListBox_GetHorizontalExtent(hwndCtl) \
63 ((int)(DWORD)SendMessage((hwndCtl), LB_GETHORIZONTALEXTENT, 0L, 0L))
64 #endif
65 #ifndef ListBox_GetSelCount
66 #define ListBox_GetSelCount(hwndCtl) \
67 ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELCOUNT, 0L, 0L))
68 #endif
69 #ifndef ListBox_GetSelItems
70 #define ListBox_GetSelItems(hwndCtl, cItems, lpItems) \
71 ((int)(DWORD)SendMessage((hwndCtl), LB_GETSELITEMS, (WPARAM)(int)(cItems), (LPARAM)(int *)(lpItems)))
72 #endif
73 #ifndef ListBox_GetTextLen
74 #define ListBox_GetTextLen(hwndCtl, index) \
75 ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXTLEN, (WPARAM)(int)(index), 0L))
76 #endif
77 #ifndef ListBox_GetText
78 #define ListBox_GetText(hwndCtl, index, lpszBuffer) \
79 ((int)(DWORD)SendMessage((hwndCtl), LB_GETTEXT, (WPARAM)(int)(index), (LPARAM)(LPCTSTR)(lpszBuffer)))
80 #endif
81#endif
14483330 82
dd3c394a 83 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
2bda0e17
KB
84
85// ============================================================================
86// list box item declaration and implementation
87// ============================================================================
88
47d67540 89#if wxUSE_OWNER_DRAWN
2bda0e17
KB
90
91class wxListBoxItem : public wxOwnerDrawn
92{
93public:
dd3c394a 94 wxListBoxItem(const wxString& str = "");
2bda0e17
KB
95};
96
dd3c394a 97wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
2bda0e17 98{
dd3c394a
VZ
99 // no bitmaps/checkmarks
100 SetMarginWidth(0);
2bda0e17
KB
101}
102
33ac7e6f 103wxOwnerDrawn *wxListBox::CreateItem(size_t WXUNUSED(n))
2bda0e17 104{
dd3c394a 105 return new wxListBoxItem();
2bda0e17
KB
106}
107
108#endif //USE_OWNER_DRAWN
109
110// ============================================================================
111// list box control implementation
112// ============================================================================
113
2ee3ee1b
VZ
114// ----------------------------------------------------------------------------
115// creation
116// ----------------------------------------------------------------------------
dd3c394a 117
2ee3ee1b 118// Listbox item
bfc6fde4 119wxListBox::wxListBox()
2bda0e17 120{
dd3c394a
VZ
121 m_noItems = 0;
122 m_selected = 0;
2bda0e17
KB
123}
124
dd3c394a
VZ
125bool wxListBox::Create(wxWindow *parent,
126 wxWindowID id,
2bda0e17
KB
127 const wxPoint& pos,
128 const wxSize& size,
debe6624
JS
129 int n, const wxString choices[],
130 long style,
2bda0e17
KB
131 const wxValidator& validator,
132 const wxString& name)
133{
dd3c394a
VZ
134 m_noItems = 0;
135 m_hWnd = 0;
136 m_selected = 0;
2bda0e17 137
dd3c394a 138 SetName(name);
11b6a93b 139#if wxUSE_VALIDATORS
dd3c394a 140 SetValidator(validator);
11b6a93b 141#endif // wxUSE_VALIDATORS
2bda0e17 142
dd3c394a
VZ
143 if (parent)
144 parent->AddChild(this);
2bda0e17 145
dd3c394a
VZ
146 wxSystemSettings settings;
147 SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_WINDOW));
148 SetForegroundColour(parent->GetForegroundColour());
2bda0e17 149
dd3c394a 150 m_windowId = ( id == -1 ) ? (int)NewControlId() : id;
2bda0e17 151
dd3c394a
VZ
152 int x = pos.x;
153 int y = pos.y;
154 int width = size.x;
155 int height = size.y;
156 m_windowStyle = style;
2bda0e17 157
42e69d6b 158 DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP |
f6bcfd97 159 LBS_NOTIFY | LBS_HASSTRINGS /* | WS_CLIPSIBLINGS */;
54081dc5
VZ
160
161 wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED),
162 _T("only one of listbox selection modes can be specified") );
b0766406
JS
163 if ( m_windowStyle & wxCLIP_SIBLINGS )
164 wstyle |= WS_CLIPSIBLINGS;
54081dc5 165
dd3c394a
VZ
166 if (m_windowStyle & wxLB_MULTIPLE)
167 wstyle |= LBS_MULTIPLESEL;
168 else if (m_windowStyle & wxLB_EXTENDED)
169 wstyle |= LBS_EXTENDEDSEL;
2bda0e17 170
dd3c394a 171 if (m_windowStyle & wxLB_ALWAYS_SB)
2ee3ee1b 172 wstyle |= LBS_DISABLENOSCROLL;
dd3c394a
VZ
173 if (m_windowStyle & wxLB_HSCROLL)
174 wstyle |= WS_HSCROLL;
175 if (m_windowStyle & wxLB_SORT)
176 wstyle |= LBS_SORT;
2bda0e17 177
47d67540 178#if wxUSE_OWNER_DRAWN
2bda0e17 179 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a
VZ
180 // we don't support LBS_OWNERDRAWVARIABLE yet
181 wstyle |= LBS_OWNERDRAWFIXED;
2bda0e17 182 }
2bda0e17
KB
183#endif
184
dd3c394a
VZ
185 // Without this style, you get unexpected heights, so e.g. constraint layout
186 // doesn't work properly
187 wstyle |= LBS_NOINTEGRALHEIGHT;
188
189 bool want3D;
2ee3ee1b 190 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
2bda0e17 191
2ee3ee1b
VZ
192 // Even with extended styles, need to combine with WS_BORDER for them to
193 // look right.
dd3c394a
VZ
194 if ( want3D || wxStyleHasBorder(m_windowStyle) )
195 {
196 wstyle |= WS_BORDER;
197 }
2bda0e17 198
223d09f6 199 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL,
dd3c394a
VZ
200 wstyle | WS_CHILD,
201 0, 0, 0, 0,
202 (HWND)parent->GetHWND(), (HMENU)m_windowId,
203 wxGetInstance(), NULL);
1c089c47 204
223d09f6 205 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") );
1c089c47 206
1f112209 207#if wxUSE_CTL3D
dd3c394a
VZ
208 if (want3D)
209 {
a23fd0e1 210 Ctl3dSubclassCtl(GetHwnd());
dd3c394a
VZ
211 m_useCtl3D = TRUE;
212 }
2bda0e17
KB
213#endif
214
dd3c394a
VZ
215 // Subclass again to catch messages
216 SubclassWin(m_hWnd);
2bda0e17 217
dd3c394a
VZ
218 size_t ui;
219 for (ui = 0; ui < (size_t)n; ui++) {
220 Append(choices[ui]);
2bda0e17 221 }
2bda0e17 222
dd3c394a 223 if ( (m_windowStyle & wxLB_MULTIPLE) == 0 )
a23fd0e1 224 SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0);
2bda0e17 225
dd3c394a 226 SetFont(parent->GetFont());
2bda0e17 227
dd3c394a 228 SetSize(x, y, width, height);
2bda0e17 229
dd3c394a 230 return TRUE;
2bda0e17
KB
231}
232
bfc6fde4 233wxListBox::~wxListBox()
2bda0e17 234{
baccb514 235 Free();
2bda0e17
KB
236}
237
bfc6fde4 238void wxListBox::SetupColours()
2bda0e17 239{
dd3c394a
VZ
240 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
241 SetForegroundColour(GetParent()->GetForegroundColour());
2bda0e17
KB
242}
243
2ee3ee1b
VZ
244// ----------------------------------------------------------------------------
245// implementation of wxListBoxBase methods
246// ----------------------------------------------------------------------------
247
248void wxListBox::DoSetFirstItem(int N)
2bda0e17 249{
dd3c394a 250 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 251 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 252
2ee3ee1b 253 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
254}
255
debe6624 256void wxListBox::Delete(int N)
2bda0e17 257{
dd3c394a 258 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 259 wxT("invalid index in wxListBox::Delete") );
dd3c394a 260
07cf98cb
VZ
261 // for owner drawn objects, the data is used for storing wxOwnerDrawn
262 // pointers and we shouldn't touch it
263#if !wxUSE_OWNER_DRAWN
264 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
265#endif // !wxUSE_OWNER_DRAWN
266 if ( HasClientObjectData() )
267 {
268 delete GetClientObject(N);
269 }
6c8a980f 270
a23fd0e1 271 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
dd3c394a
VZ
272 m_noItems--;
273
274 SetHorizontalExtent("");
2bda0e17
KB
275}
276
2ee3ee1b 277int wxListBox::DoAppend(const wxString& item)
2bda0e17 278{
a23fd0e1 279 int index = ListBox_AddString(GetHwnd(), item);
2ee3ee1b 280 m_noItems++;
2bda0e17 281
47d67540 282#if wxUSE_OWNER_DRAWN
2bda0e17 283 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a
VZ
284 wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument
285 pNewItem->SetName(item);
286 m_aItems.Add(pNewItem);
a23fd0e1 287 ListBox_SetItemData(GetHwnd(), index, pNewItem);
60c65519 288 pNewItem->SetFont(GetFont());
2bda0e17 289 }
1c089c47 290#endif
2bda0e17 291
dd3c394a 292 SetHorizontalExtent(item);
dd3c394a 293
2ee3ee1b 294 return index;
2bda0e17
KB
295}
296
2ee3ee1b 297void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
2bda0e17 298{
f6bcfd97
BP
299 // avoid flicker - but don't need to do this for a hidden listbox
300 bool hideAndShow = IsShown();
301 if ( hideAndShow )
302 {
303 ShowWindow(GetHwnd(), SW_HIDE);
304 }
2ee3ee1b 305
a23fd0e1 306 ListBox_ResetContent(GetHwnd());
2ee3ee1b
VZ
307
308 m_noItems = choices.GetCount();
dd3c394a 309 int i;
2ee3ee1b 310 for (i = 0; i < m_noItems; i++)
dd3c394a 311 {
a23fd0e1 312 ListBox_AddString(GetHwnd(), choices[i]);
dd3c394a 313 if ( clientData )
2ee3ee1b
VZ
314 {
315#if wxUSE_OWNER_DRAWN
5615ab37
DW
316 if ( m_windowStyle & wxLB_OWNERDRAW )
317 {
318 wxASSERT_MSG(clientData[i] == NULL,
319 wxT("Can't use client data with owner-drawn listboxes"));
320 }
321 ListBox_SetItemData(GetHwnd(), i, clientData[i]);
2ee3ee1b 322#else // !wxUSE_OWNER_DRAWN
a23fd0e1 323 ListBox_SetItemData(GetHwnd(), i, clientData[i]);
2ee3ee1b
VZ
324#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
325 }
dd3c394a 326 }
2bda0e17 327
47d67540 328#if wxUSE_OWNER_DRAWN
2bda0e17 329 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a
VZ
330 // first delete old items
331 size_t ui = m_aItems.Count();
332 while ( ui-- != 0 ) {
333 delete m_aItems[ui];
334 }
335 m_aItems.Empty();
336
337 // then create new ones
2ee3ee1b 338 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
dd3c394a
VZ
339 wxOwnerDrawn *pNewItem = CreateItem(ui);
340 pNewItem->SetName(choices[ui]);
341 m_aItems.Add(pNewItem);
a23fd0e1 342 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
dd3c394a 343 }
2bda0e17 344 }
2ee3ee1b
VZ
345#endif // wxUSE_OWNER_DRAWN
346
347 SetHorizontalExtent();
2bda0e17 348
f6bcfd97
BP
349 if ( hideAndShow )
350 {
351 // show the listbox back if we hid it
352 ShowWindow(GetHwnd(), SW_SHOW);
353 }
2bda0e17
KB
354}
355
356int wxListBox::FindString(const wxString& s) const
357{
a23fd0e1 358 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
dd3c394a 359 if (pos == LB_ERR)
2ee3ee1b 360 return wxNOT_FOUND;
dd3c394a
VZ
361 else
362 return pos;
2bda0e17
KB
363}
364
bfc6fde4 365void wxListBox::Clear()
2bda0e17 366{
baccb514 367 Free();
8ee9d618
VZ
368
369 ListBox_ResetContent(GetHwnd());
370
371 m_noItems = 0;
372 SetHorizontalExtent();
373}
374
375void wxListBox::Free()
2b273975 376{
dd3c394a 377#if wxUSE_OWNER_DRAWN
185fa6bf
VZ
378 if ( m_windowStyle & wxLB_OWNERDRAW )
379 {
380 size_t uiCount = m_aItems.Count();
381 while ( uiCount-- != 0 ) {
382 delete m_aItems[uiCount];
383 }
dd3c394a 384
185fa6bf
VZ
385 m_aItems.Clear();
386 }
387 else
388#endif // wxUSE_OWNER_DRAWN
6c8a980f
VZ
389 if ( HasClientObjectData() )
390 {
391 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
392 {
393 delete GetClientObject(n);
394 }
395 }
2bda0e17 396}
baccb514 397
debe6624 398void wxListBox::SetSelection(int N, bool select)
2bda0e17 399{
dd3c394a 400 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 401 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 402
2ee3ee1b
VZ
403 if ( HasMultipleSelection() )
404 {
a23fd0e1 405 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 406 }
dd3c394a
VZ
407 else
408 {
2ee3ee1b 409 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 410 }
2bda0e17
KB
411}
412
2ee3ee1b 413bool wxListBox::IsSelected(int N) const
2bda0e17 414{
dd3c394a 415 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
223d09f6 416 wxT("invalid index in wxListBox::Selected") );
dd3c394a 417
a23fd0e1 418 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
2bda0e17
KB
419}
420
6c8a980f 421wxClientData* wxListBox::DoGetItemClientObject(int n) const
2bda0e17 422{
6c8a980f 423 return (wxClientData *)DoGetItemClientData(n);
2bda0e17
KB
424}
425
6c8a980f 426void *wxListBox::DoGetItemClientData(int n) const
2bda0e17 427{
2ee3ee1b 428 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
223d09f6 429 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 430
2ee3ee1b 431 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
432}
433
6c8a980f 434void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
2bda0e17 435{
6c8a980f 436 DoSetItemClientData(n, clientData);
2ee3ee1b
VZ
437}
438
6c8a980f 439void wxListBox::DoSetItemClientData(int n, void *clientData)
2ee3ee1b
VZ
440{
441 wxCHECK_RET( n >= 0 && n < m_noItems,
223d09f6 442 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 443
2ee3ee1b
VZ
444#if wxUSE_OWNER_DRAWN
445 if ( m_windowStyle & wxLB_OWNERDRAW )
446 {
447 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
448 // in OnMeasure/OnDraw.
449 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
450 }
451#endif // wxUSE_OWNER_DRAWN
452
453 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
223d09f6 454 wxLogDebug(wxT("LB_SETITEMDATA failed"));
2bda0e17
KB
455}
456
2ee3ee1b
VZ
457bool wxListBox::HasMultipleSelection() const
458{
459 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
460}
461
2bda0e17 462// Return number of selections and an array of selected integers
14483330 463int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 464{
dd3c394a 465 aSelections.Empty();
14483330 466
2ee3ee1b 467 if ( HasMultipleSelection() )
dd3c394a 468 {
a23fd0e1 469 int no_sel = ListBox_GetSelCount(GetHwnd());
dd3c394a
VZ
470 if (no_sel != 0) {
471 int *selections = new int[no_sel];
2ee3ee1b
VZ
472 int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections);
473
474 wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed"));
14483330 475
dd3c394a
VZ
476 aSelections.Alloc(no_sel);
477 for ( int n = 0; n < no_sel; n++ )
478 aSelections.Add(selections[n]);
14483330 479
dd3c394a
VZ
480 delete [] selections;
481 }
14483330 482
dd3c394a
VZ
483 return no_sel;
484 }
485 else // single-selection listbox
486 {
f6bcfd97
BP
487 if (ListBox_GetCurSel(GetHwnd()) > -1)
488 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 489
f6bcfd97 490 return aSelections.Count();
dd3c394a 491 }
2bda0e17
KB
492}
493
494// Get single selection, for single choice list items
14483330 495int wxListBox::GetSelection() const
2bda0e17 496{
2ee3ee1b 497 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 498 -1,
f6bcfd97 499 wxT("GetSelection() can't be used with multiple-selection listboxes, use GetSelections() instead.") );
14483330 500
a23fd0e1 501 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
502}
503
504// Find string for position
debe6624 505wxString wxListBox::GetString(int N) const
2bda0e17 506{
dd3c394a 507 wxCHECK_MSG( N >= 0 && N < m_noItems, "",
223d09f6 508 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 509
a23fd0e1 510 int len = ListBox_GetTextLen(GetHwnd(), N);
dd3c394a
VZ
511
512 // +1 for terminating NUL
513 wxString result;
a23fd0e1 514 ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1));
dd3c394a
VZ
515 result.UngetWriteBuf();
516
517 return result;
2bda0e17
KB
518}
519
2ee3ee1b
VZ
520void
521wxListBox::DoInsertItems(const wxArrayString& items, int pos)
522{
523 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
524 wxT("invalid index in wxListBox::InsertItems") );
525
526 int nItems = items.GetCount();
527 for ( int i = 0; i < nItems; i++ )
528 ListBox_InsertString(GetHwnd(), i + pos, items[i]);
529 m_noItems += nItems;
530
531 SetHorizontalExtent();
532}
533
534void wxListBox::SetString(int N, const wxString& s)
535{
536 wxCHECK_RET( N >= 0 && N < m_noItems,
537 wxT("invalid index in wxListBox::SetString") );
538
539 // remember the state of the item
540 bool wasSelected = IsSelected(N);
541
542 void *oldData = NULL;
543 wxClientData *oldObjData = NULL;
544 if ( m_clientDataItemsType == ClientData_Void )
545 oldData = GetClientData(N);
546 else if ( m_clientDataItemsType == ClientData_Object )
547 oldObjData = GetClientObject(N);
548
549 // delete and recreate it
550 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
551
552 int newN = N;
553 if ( N == m_noItems - 1 )
554 newN = -1;
555
556 ListBox_InsertString(GetHwnd(), newN, s);
557
558 // restore the client data
559 if ( oldData )
560 SetClientData(N, oldData);
561 else if ( oldObjData )
562 SetClientObject(N, oldObjData);
563
564 // we may have lost the selection
565 if ( wasSelected )
566 Select(N);
567
568#if wxUSE_OWNER_DRAWN
569 if ( m_windowStyle & wxLB_OWNERDRAW )
570 // update item's text
571 m_aItems[N]->SetName(s);
572#endif //USE_OWNER_DRAWN
573}
574
575int wxListBox::GetCount() const
576{
577 return m_noItems;
578}
579
580// ----------------------------------------------------------------------------
581// helpers
582// ----------------------------------------------------------------------------
583
4438caf4
VZ
584// Windows-specific code to set the horizontal extent of the listbox, if
585// necessary. If s is non-NULL, it's used to calculate the horizontal extent.
2bda0e17
KB
586// Otherwise, all strings are used.
587void wxListBox::SetHorizontalExtent(const wxString& s)
588{
dd3c394a
VZ
589 // Only necessary if we want a horizontal scrollbar
590 if (!(m_windowStyle & wxHSCROLL))
591 return;
592 TEXTMETRIC lpTextMetric;
593
2ee3ee1b 594 if ( !s.IsEmpty() )
2bda0e17 595 {
a23fd0e1
VZ
596 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
597 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
598 HFONT oldFont = 0;
599 if (GetFont().Ok() && GetFont().GetResourceHandle())
600 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
601
602 GetTextMetrics(dc, &lpTextMetric);
603 SIZE extentXY;
837e5743 604 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
dd3c394a
VZ
605 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
606
607 if (oldFont)
608 ::SelectObject(dc, oldFont);
609
a23fd0e1 610 ReleaseDC(GetHwnd(), dc);
dd3c394a 611 if (extentX > existingExtent)
a23fd0e1 612 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
dd3c394a
VZ
613 }
614 else
615 {
616 int largestExtent = 0;
a23fd0e1 617 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
618 HFONT oldFont = 0;
619 if (GetFont().Ok() && GetFont().GetResourceHandle())
620 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
621
622 GetTextMetrics(dc, &lpTextMetric);
623 int i;
624 for (i = 0; i < m_noItems; i++)
625 {
a23fd0e1 626 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer);
dd3c394a
VZ
627 wxBuffer[len] = 0;
628 SIZE extentXY;
837e5743 629 ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY);
dd3c394a
VZ
630 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
631 if (extentX > largestExtent)
632 largestExtent = extentX;
633 }
634 if (oldFont)
635 ::SelectObject(dc, oldFont);
636
a23fd0e1
VZ
637 ReleaseDC(GetHwnd(), dc);
638 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
2bda0e17 639 }
2bda0e17
KB
640}
641
f68586e5 642wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
643{
644 // find the widest string
645 int wLine;
646 int wListbox = 0;
647 for ( int i = 0; i < m_noItems; i++ )
648 {
649 wxString str(GetString(i));
650 GetTextExtent(str, &wLine, NULL);
651 if ( wLine > wListbox )
652 wListbox = wLine;
653 }
654
655 // give it some reasonable default value if there are no strings in the
656 // list
657 if ( wListbox == 0 )
658 wListbox = 100;
659
660 // the listbox should be slightly larger than the widest string
661 int cx, cy;
662 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
663
664 wListbox += 3*cx;
665
666 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMax(m_noItems, 7));
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
dd3c394a
VZ
776 wxDC dc;
777 dc.SetHDC((WXHDC)pStruct->hDC, FALSE);
778 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
f6bcfd97 779 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
2bda0e17 780
dd3c394a
VZ
781 return pItem->OnDrawItem(dc, rect,
782 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
783 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
784}
785
786#endif
dd3c394a 787 // wxUSE_OWNER_DRAWN