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