]> git.saurik.com Git - wxWidgets.git/blame - src/msw/listbox.cpp
typo fixed
[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
c86f1403 103wxOwnerDrawn *wxListBox::CreateItem(size_t 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
VZ
158 DWORD wstyle = WS_VISIBLE | WS_VSCROLL | WS_TABSTOP |
159 LBS_NOTIFY | LBS_HASSTRINGS;
dd3c394a
VZ
160 if (m_windowStyle & wxLB_MULTIPLE)
161 wstyle |= LBS_MULTIPLESEL;
162 else if (m_windowStyle & wxLB_EXTENDED)
163 wstyle |= LBS_EXTENDEDSEL;
2bda0e17 164
dd3c394a 165 if (m_windowStyle & wxLB_ALWAYS_SB)
2ee3ee1b 166 wstyle |= LBS_DISABLENOSCROLL;
dd3c394a
VZ
167 if (m_windowStyle & wxLB_HSCROLL)
168 wstyle |= WS_HSCROLL;
169 if (m_windowStyle & wxLB_SORT)
170 wstyle |= LBS_SORT;
2bda0e17 171
47d67540 172#if wxUSE_OWNER_DRAWN
2bda0e17 173 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a
VZ
174 // we don't support LBS_OWNERDRAWVARIABLE yet
175 wstyle |= LBS_OWNERDRAWFIXED;
2bda0e17 176 }
2bda0e17
KB
177#endif
178
dd3c394a
VZ
179 // Without this style, you get unexpected heights, so e.g. constraint layout
180 // doesn't work properly
181 wstyle |= LBS_NOINTEGRALHEIGHT;
182
183 bool want3D;
2ee3ee1b 184 WXDWORD exStyle = Determine3DEffects(WS_EX_CLIENTEDGE, &want3D);
2bda0e17 185
2ee3ee1b
VZ
186 // Even with extended styles, need to combine with WS_BORDER for them to
187 // look right.
dd3c394a
VZ
188 if ( want3D || wxStyleHasBorder(m_windowStyle) )
189 {
190 wstyle |= WS_BORDER;
191 }
2bda0e17 192
223d09f6 193 m_hWnd = (WXHWND)::CreateWindowEx(exStyle, wxT("LISTBOX"), NULL,
dd3c394a
VZ
194 wstyle | WS_CHILD,
195 0, 0, 0, 0,
196 (HWND)parent->GetHWND(), (HMENU)m_windowId,
197 wxGetInstance(), NULL);
1c089c47 198
223d09f6 199 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create listbox") );
1c089c47 200
1f112209 201#if wxUSE_CTL3D
dd3c394a
VZ
202 if (want3D)
203 {
a23fd0e1 204 Ctl3dSubclassCtl(GetHwnd());
dd3c394a
VZ
205 m_useCtl3D = TRUE;
206 }
2bda0e17
KB
207#endif
208
dd3c394a
VZ
209 // Subclass again to catch messages
210 SubclassWin(m_hWnd);
2bda0e17 211
dd3c394a
VZ
212 size_t ui;
213 for (ui = 0; ui < (size_t)n; ui++) {
214 Append(choices[ui]);
2bda0e17 215 }
2bda0e17 216
dd3c394a 217 if ( (m_windowStyle & wxLB_MULTIPLE) == 0 )
a23fd0e1 218 SendMessage(GetHwnd(), LB_SETCURSEL, 0, 0);
2bda0e17 219
dd3c394a 220 SetFont(parent->GetFont());
2bda0e17 221
dd3c394a 222 SetSize(x, y, width, height);
2bda0e17 223
dd3c394a 224 Show(TRUE);
1c089c47 225
dd3c394a 226 return TRUE;
2bda0e17
KB
227}
228
bfc6fde4 229wxListBox::~wxListBox()
2bda0e17 230{
baccb514 231 Free();
2bda0e17
KB
232}
233
bfc6fde4 234void wxListBox::SetupColours()
2bda0e17 235{
dd3c394a
VZ
236 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
237 SetForegroundColour(GetParent()->GetForegroundColour());
2bda0e17
KB
238}
239
2ee3ee1b
VZ
240// ----------------------------------------------------------------------------
241// implementation of wxListBoxBase methods
242// ----------------------------------------------------------------------------
243
244void wxListBox::DoSetFirstItem(int N)
2bda0e17 245{
dd3c394a 246 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 247 wxT("invalid index in wxListBox::SetFirstItem") );
dd3c394a 248
2ee3ee1b 249 SendMessage(GetHwnd(), LB_SETTOPINDEX, (WPARAM)N, (LPARAM)0);
2bda0e17
KB
250}
251
debe6624 252void wxListBox::Delete(int N)
2bda0e17 253{
dd3c394a 254 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 255 wxT("invalid index in wxListBox::Delete") );
dd3c394a 256
07cf98cb
VZ
257 // for owner drawn objects, the data is used for storing wxOwnerDrawn
258 // pointers and we shouldn't touch it
259#if !wxUSE_OWNER_DRAWN
260 if ( !(m_windowStyle & wxLB_OWNERDRAW) )
261#endif // !wxUSE_OWNER_DRAWN
262 if ( HasClientObjectData() )
263 {
264 delete GetClientObject(N);
265 }
6c8a980f 266
a23fd0e1 267 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
dd3c394a
VZ
268 m_noItems--;
269
270 SetHorizontalExtent("");
2bda0e17
KB
271}
272
2ee3ee1b 273int wxListBox::DoAppend(const wxString& item)
2bda0e17 274{
a23fd0e1 275 int index = ListBox_AddString(GetHwnd(), item);
2ee3ee1b 276 m_noItems++;
2bda0e17 277
47d67540 278#if wxUSE_OWNER_DRAWN
2bda0e17 279 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a
VZ
280 wxOwnerDrawn *pNewItem = CreateItem(index); // dummy argument
281 pNewItem->SetName(item);
282 m_aItems.Add(pNewItem);
a23fd0e1 283 ListBox_SetItemData(GetHwnd(), index, pNewItem);
60c65519 284 pNewItem->SetFont(GetFont());
2bda0e17 285 }
1c089c47 286#endif
2bda0e17 287
dd3c394a 288 SetHorizontalExtent(item);
dd3c394a 289
2ee3ee1b 290 return index;
2bda0e17
KB
291}
292
2ee3ee1b 293void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
2bda0e17 294{
a23fd0e1 295 ShowWindow(GetHwnd(), SW_HIDE);
2ee3ee1b 296
a23fd0e1 297 ListBox_ResetContent(GetHwnd());
2ee3ee1b
VZ
298
299 m_noItems = choices.GetCount();
dd3c394a 300 int i;
2ee3ee1b 301 for (i = 0; i < m_noItems; i++)
dd3c394a 302 {
a23fd0e1 303 ListBox_AddString(GetHwnd(), choices[i]);
dd3c394a 304 if ( clientData )
2ee3ee1b
VZ
305 {
306#if wxUSE_OWNER_DRAWN
d9a012ad 307 wxASSERT_MSG(clientData[i] == NULL,
2ee3ee1b
VZ
308 wxT("Can't use client data with owner-drawn listboxes"));
309#else // !wxUSE_OWNER_DRAWN
a23fd0e1 310 ListBox_SetItemData(GetHwnd(), i, clientData[i]);
2ee3ee1b
VZ
311#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
312 }
dd3c394a 313 }
2bda0e17 314
47d67540 315#if wxUSE_OWNER_DRAWN
2bda0e17 316 if ( m_windowStyle & wxLB_OWNERDRAW ) {
dd3c394a
VZ
317 // first delete old items
318 size_t ui = m_aItems.Count();
319 while ( ui-- != 0 ) {
320 delete m_aItems[ui];
321 }
322 m_aItems.Empty();
323
324 // then create new ones
2ee3ee1b 325 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
dd3c394a
VZ
326 wxOwnerDrawn *pNewItem = CreateItem(ui);
327 pNewItem->SetName(choices[ui]);
328 m_aItems.Add(pNewItem);
a23fd0e1 329 ListBox_SetItemData(GetHwnd(), ui, pNewItem);
dd3c394a 330 }
2bda0e17 331 }
2ee3ee1b
VZ
332#endif // wxUSE_OWNER_DRAWN
333
334 SetHorizontalExtent();
2bda0e17 335
a23fd0e1 336 ShowWindow(GetHwnd(), SW_SHOW);
2bda0e17
KB
337}
338
339int wxListBox::FindString(const wxString& s) const
340{
a23fd0e1 341 int pos = ListBox_FindStringExact(GetHwnd(), (WPARAM)-1, s);
dd3c394a 342 if (pos == LB_ERR)
2ee3ee1b 343 return wxNOT_FOUND;
dd3c394a
VZ
344 else
345 return pos;
2bda0e17
KB
346}
347
bfc6fde4 348void wxListBox::Clear()
2bda0e17 349{
baccb514 350 Free();
8ee9d618
VZ
351
352 ListBox_ResetContent(GetHwnd());
353
354 m_noItems = 0;
355 SetHorizontalExtent();
356}
357
358void wxListBox::Free()
2b273975 359{
dd3c394a 360#if wxUSE_OWNER_DRAWN
185fa6bf
VZ
361 if ( m_windowStyle & wxLB_OWNERDRAW )
362 {
363 size_t uiCount = m_aItems.Count();
364 while ( uiCount-- != 0 ) {
365 delete m_aItems[uiCount];
366 }
dd3c394a 367
185fa6bf
VZ
368 m_aItems.Clear();
369 }
370 else
371#endif // wxUSE_OWNER_DRAWN
6c8a980f
VZ
372 if ( HasClientObjectData() )
373 {
374 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
375 {
376 delete GetClientObject(n);
377 }
378 }
2bda0e17 379}
baccb514 380
debe6624 381void wxListBox::SetSelection(int N, bool select)
2bda0e17 382{
dd3c394a 383 wxCHECK_RET( N >= 0 && N < m_noItems,
223d09f6 384 wxT("invalid index in wxListBox::SetSelection") );
dd3c394a 385
2ee3ee1b
VZ
386 if ( HasMultipleSelection() )
387 {
a23fd0e1 388 SendMessage(GetHwnd(), LB_SETSEL, select, N);
2ee3ee1b 389 }
dd3c394a
VZ
390 else
391 {
2ee3ee1b 392 SendMessage(GetHwnd(), LB_SETCURSEL, select ? N : -1, 0);
dd3c394a 393 }
2bda0e17
KB
394}
395
2ee3ee1b 396bool wxListBox::IsSelected(int N) const
2bda0e17 397{
dd3c394a 398 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
223d09f6 399 wxT("invalid index in wxListBox::Selected") );
dd3c394a 400
a23fd0e1 401 return SendMessage(GetHwnd(), LB_GETSEL, N, 0) == 0 ? FALSE : TRUE;
2bda0e17
KB
402}
403
6c8a980f 404wxClientData* wxListBox::DoGetItemClientObject(int n) const
2bda0e17 405{
6c8a980f 406 return (wxClientData *)DoGetItemClientData(n);
2bda0e17
KB
407}
408
6c8a980f 409void *wxListBox::DoGetItemClientData(int n) const
2bda0e17 410{
2ee3ee1b 411 wxCHECK_MSG( n >= 0 && n < m_noItems, NULL,
223d09f6 412 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 413
2ee3ee1b 414 return (void *)SendMessage(GetHwnd(), LB_GETITEMDATA, n, 0);
2bda0e17
KB
415}
416
6c8a980f 417void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
2bda0e17 418{
6c8a980f 419 DoSetItemClientData(n, clientData);
2ee3ee1b
VZ
420}
421
6c8a980f 422void wxListBox::DoSetItemClientData(int n, void *clientData)
2ee3ee1b
VZ
423{
424 wxCHECK_RET( n >= 0 && n < m_noItems,
223d09f6 425 wxT("invalid index in wxListBox::SetClientData") );
dd3c394a 426
2ee3ee1b
VZ
427#if wxUSE_OWNER_DRAWN
428 if ( m_windowStyle & wxLB_OWNERDRAW )
429 {
430 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
431 // in OnMeasure/OnDraw.
432 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
433 }
434#endif // wxUSE_OWNER_DRAWN
435
436 if ( ListBox_SetItemData(GetHwnd(), n, clientData) == LB_ERR )
223d09f6 437 wxLogDebug(wxT("LB_SETITEMDATA failed"));
2bda0e17
KB
438}
439
2ee3ee1b
VZ
440bool wxListBox::HasMultipleSelection() const
441{
442 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
443}
444
2bda0e17 445// Return number of selections and an array of selected integers
14483330 446int wxListBox::GetSelections(wxArrayInt& aSelections) const
2bda0e17 447{
dd3c394a 448 aSelections.Empty();
14483330 449
2ee3ee1b 450 if ( HasMultipleSelection() )
dd3c394a 451 {
a23fd0e1 452 int no_sel = ListBox_GetSelCount(GetHwnd());
dd3c394a
VZ
453 if (no_sel != 0) {
454 int *selections = new int[no_sel];
2ee3ee1b
VZ
455 int rc = ListBox_GetSelItems(GetHwnd(), no_sel, selections);
456
457 wxCHECK_MSG(rc != LB_ERR, -1, wxT("ListBox_GetSelItems failed"));
14483330 458
dd3c394a
VZ
459 aSelections.Alloc(no_sel);
460 for ( int n = 0; n < no_sel; n++ )
461 aSelections.Add(selections[n]);
14483330 462
dd3c394a
VZ
463 delete [] selections;
464 }
14483330 465
dd3c394a
VZ
466 return no_sel;
467 }
468 else // single-selection listbox
469 {
a23fd0e1 470 aSelections.Add(ListBox_GetCurSel(GetHwnd()));
14483330 471
dd3c394a
VZ
472 return 1;
473 }
2bda0e17
KB
474}
475
476// Get single selection, for single choice list items
14483330 477int wxListBox::GetSelection() const
2bda0e17 478{
2ee3ee1b 479 wxCHECK_MSG( !HasMultipleSelection(),
dd3c394a 480 -1,
223d09f6 481 wxT("GetSelection() can't be used with multiple-selection "
837e5743 482 "listboxes, use GetSelections() instead.") );
14483330 483
a23fd0e1 484 return ListBox_GetCurSel(GetHwnd());
2bda0e17
KB
485}
486
487// Find string for position
debe6624 488wxString wxListBox::GetString(int N) const
2bda0e17 489{
dd3c394a 490 wxCHECK_MSG( N >= 0 && N < m_noItems, "",
223d09f6 491 wxT("invalid index in wxListBox::GetClientData") );
dd3c394a 492
a23fd0e1 493 int len = ListBox_GetTextLen(GetHwnd(), N);
dd3c394a
VZ
494
495 // +1 for terminating NUL
496 wxString result;
a23fd0e1 497 ListBox_GetText(GetHwnd(), N, result.GetWriteBuf(len + 1));
dd3c394a
VZ
498 result.UngetWriteBuf();
499
500 return result;
2bda0e17
KB
501}
502
2ee3ee1b
VZ
503void
504wxListBox::DoInsertItems(const wxArrayString& items, int pos)
505{
506 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
507 wxT("invalid index in wxListBox::InsertItems") );
508
509 int nItems = items.GetCount();
510 for ( int i = 0; i < nItems; i++ )
511 ListBox_InsertString(GetHwnd(), i + pos, items[i]);
512 m_noItems += nItems;
513
514 SetHorizontalExtent();
515}
516
517void wxListBox::SetString(int N, const wxString& s)
518{
519 wxCHECK_RET( N >= 0 && N < m_noItems,
520 wxT("invalid index in wxListBox::SetString") );
521
522 // remember the state of the item
523 bool wasSelected = IsSelected(N);
524
525 void *oldData = NULL;
526 wxClientData *oldObjData = NULL;
527 if ( m_clientDataItemsType == ClientData_Void )
528 oldData = GetClientData(N);
529 else if ( m_clientDataItemsType == ClientData_Object )
530 oldObjData = GetClientObject(N);
531
532 // delete and recreate it
533 SendMessage(GetHwnd(), LB_DELETESTRING, N, 0);
534
535 int newN = N;
536 if ( N == m_noItems - 1 )
537 newN = -1;
538
539 ListBox_InsertString(GetHwnd(), newN, s);
540
541 // restore the client data
542 if ( oldData )
543 SetClientData(N, oldData);
544 else if ( oldObjData )
545 SetClientObject(N, oldObjData);
546
547 // we may have lost the selection
548 if ( wasSelected )
549 Select(N);
550
551#if wxUSE_OWNER_DRAWN
552 if ( m_windowStyle & wxLB_OWNERDRAW )
553 // update item's text
554 m_aItems[N]->SetName(s);
555#endif //USE_OWNER_DRAWN
556}
557
558int wxListBox::GetCount() const
559{
560 return m_noItems;
561}
562
563// ----------------------------------------------------------------------------
564// helpers
565// ----------------------------------------------------------------------------
566
4438caf4
VZ
567// Windows-specific code to set the horizontal extent of the listbox, if
568// necessary. If s is non-NULL, it's used to calculate the horizontal extent.
2bda0e17
KB
569// Otherwise, all strings are used.
570void wxListBox::SetHorizontalExtent(const wxString& s)
571{
dd3c394a
VZ
572 // Only necessary if we want a horizontal scrollbar
573 if (!(m_windowStyle & wxHSCROLL))
574 return;
575 TEXTMETRIC lpTextMetric;
576
2ee3ee1b 577 if ( !s.IsEmpty() )
2bda0e17 578 {
a23fd0e1
VZ
579 int existingExtent = (int)SendMessage(GetHwnd(), LB_GETHORIZONTALEXTENT, 0, 0L);
580 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
581 HFONT oldFont = 0;
582 if (GetFont().Ok() && GetFont().GetResourceHandle())
583 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
584
585 GetTextMetrics(dc, &lpTextMetric);
586 SIZE extentXY;
837e5743 587 ::GetTextExtentPoint(dc, (LPTSTR) (const wxChar *)s, s.Length(), &extentXY);
dd3c394a
VZ
588 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
589
590 if (oldFont)
591 ::SelectObject(dc, oldFont);
592
a23fd0e1 593 ReleaseDC(GetHwnd(), dc);
dd3c394a 594 if (extentX > existingExtent)
a23fd0e1 595 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(extentX), 0L);
dd3c394a
VZ
596 }
597 else
598 {
599 int largestExtent = 0;
a23fd0e1 600 HDC dc = GetWindowDC(GetHwnd());
dd3c394a
VZ
601 HFONT oldFont = 0;
602 if (GetFont().Ok() && GetFont().GetResourceHandle())
603 oldFont = (HFONT) ::SelectObject(dc, (HFONT) GetFont().GetResourceHandle());
604
605 GetTextMetrics(dc, &lpTextMetric);
606 int i;
607 for (i = 0; i < m_noItems; i++)
608 {
a23fd0e1 609 int len = (int)SendMessage(GetHwnd(), LB_GETTEXT, i, (LONG)wxBuffer);
dd3c394a
VZ
610 wxBuffer[len] = 0;
611 SIZE extentXY;
837e5743 612 ::GetTextExtentPoint(dc, (LPTSTR)wxBuffer, len, &extentXY);
dd3c394a
VZ
613 int extentX = (int)(extentXY.cx + lpTextMetric.tmAveCharWidth);
614 if (extentX > largestExtent)
615 largestExtent = extentX;
616 }
617 if (oldFont)
618 ::SelectObject(dc, oldFont);
619
a23fd0e1
VZ
620 ReleaseDC(GetHwnd(), dc);
621 SendMessage(GetHwnd(), LB_SETHORIZONTALEXTENT, LOWORD(largestExtent), 0L);
2bda0e17 622 }
2bda0e17
KB
623}
624
f68586e5 625wxSize wxListBox::DoGetBestSize() const
b908d224
VZ
626{
627 // find the widest string
628 int wLine;
629 int wListbox = 0;
630 for ( int i = 0; i < m_noItems; i++ )
631 {
632 wxString str(GetString(i));
633 GetTextExtent(str, &wLine, NULL);
634 if ( wLine > wListbox )
635 wListbox = wLine;
636 }
637
638 // give it some reasonable default value if there are no strings in the
639 // list
640 if ( wListbox == 0 )
641 wListbox = 100;
642
643 // the listbox should be slightly larger than the widest string
644 int cx, cy;
645 wxGetCharSize(GetHWND(), &cx, &cy, &GetFont());
646
647 wListbox += 3*cx;
648
649 int hListbox = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)*(wxMax(m_noItems, 7));
650
651 return wxSize(wListbox, hListbox);
652}
653
2ee3ee1b
VZ
654// ----------------------------------------------------------------------------
655// callbacks
656// ----------------------------------------------------------------------------
657
658bool wxListBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
2bda0e17 659{
8ee9d618 660 if ( param == LBN_SELCHANGE )
2bda0e17 661 {
2ee3ee1b 662 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
8ee9d618
VZ
663 event.SetEventObject( this );
664
2ee3ee1b 665 wxArrayInt aSelections;
8ee9d618 666 int n, count = GetSelections(aSelections);
2ee3ee1b
VZ
667 if ( count > 0 )
668 {
8ee9d618
VZ
669 n = aSelections[0];
670 if ( HasClientObjectData() )
671 event.SetClientObject( GetClientObject(n) );
672 else if ( HasClientUntypedData() )
673 event.SetClientData( GetClientData(n) );
674 event.SetString( GetString(n) );
2ee3ee1b
VZ
675 }
676 else
677 {
8ee9d618 678 n = -1;
2ee3ee1b
VZ
679 }
680
8ee9d618
VZ
681 event.m_commandInt = n;
682
683 return GetEventHandler()->ProcessEvent(event);
2bda0e17 684 }
8ee9d618 685 else if ( param == LBN_DBLCLK )
2ee3ee1b
VZ
686 {
687 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
688 event.SetEventObject( this );
8ee9d618
VZ
689
690 return GetEventHandler()->ProcessEvent(event);
2ee3ee1b 691 }
8ee9d618 692 //else:
2ee3ee1b
VZ
693
694 return FALSE;
2bda0e17
KB
695}
696
2ee3ee1b
VZ
697// ----------------------------------------------------------------------------
698// wxCheckListBox support
699// ----------------------------------------------------------------------------
2bda0e17 700
47d67540 701#if wxUSE_OWNER_DRAWN
2bda0e17
KB
702
703// drawing
704// -------
705
706// space beneath/above each row in pixels
707// "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
708#define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
709
710// the height is the same for all items
dd3c394a
VZ
711// TODO should be changed for LBS_OWNERDRAWVARIABLE style listboxes
712
713// NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
714// message is not yet sent when we get here!
2bda0e17
KB
715bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
716{
dd3c394a
VZ
717 // only owner-drawn control should receive this message
718 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
2bda0e17 719
dd3c394a 720 MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
2bda0e17 721
07cf98cb
VZ
722 HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0);
723
dd3c394a 724 wxDC dc;
07cf98cb 725 dc.SetHDC((WXHDC)hdc);
dd3c394a 726 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT));
2bda0e17 727
dd3c394a
VZ
728 pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE;
729 pStruct->itemWidth = dc.GetCharWidth();
2bda0e17 730
07cf98cb
VZ
731 dc.SetHDC(0);
732
733 DeleteDC(hdc);
734
dd3c394a 735 return TRUE;
2bda0e17
KB
736}
737
738// forward the message to the appropriate item
739bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
740{
dd3c394a
VZ
741 // only owner-drawn control should receive this message
742 wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
743
744 DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
745
a23fd0e1 746 long data = ListBox_GetItemData(GetHwnd(), pStruct->itemID);
2bda0e17 747
dd3c394a 748 wxCHECK( data && (data != LB_ERR), FALSE );
2bda0e17 749
dd3c394a 750 wxListBoxItem *pItem = (wxListBoxItem *)data;
2bda0e17 751
dd3c394a
VZ
752 wxDC dc;
753 dc.SetHDC((WXHDC)pStruct->hDC, FALSE);
754 wxRect rect(wxPoint(pStruct->rcItem.left, pStruct->rcItem.top),
755 wxPoint(pStruct->rcItem.right, pStruct->rcItem.bottom));
2bda0e17 756
dd3c394a
VZ
757 return pItem->OnDrawItem(dc, rect,
758 (wxOwnerDrawn::wxODAction)pStruct->itemAction,
759 (wxOwnerDrawn::wxODStatus)pStruct->itemState);
2bda0e17
KB
760}
761
762#endif
dd3c394a 763 // wxUSE_OWNER_DRAWN