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