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