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