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