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