1 ///////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin (owner drawn stuff)
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/listbox.h"
25 #include "wx/settings.h"
31 #include "wx/msw/private.h"
38 #include <wx/msw/gnuwin32/extra.h>
47 #include "wx/ownerdrw.h"
50 #include "wx/dynarray.h"
53 #if !USE_SHARED_LIBRARY
54 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
57 // ============================================================================
58 // list box item declaration and implementation
59 // ============================================================================
63 class wxListBoxItem
: public wxOwnerDrawn
66 wxListBoxItem(const wxString
& str
= "");
69 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
71 // no bitmaps/checkmarks
75 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
77 return new wxListBoxItem();
80 #endif //USE_OWNER_DRAWN
82 // ============================================================================
83 // list box control implementation
84 // ============================================================================
86 // this macro is dangerous but still better than tons of (HWND)GetHWND()
87 #define hwnd (HWND)GetHWND()
89 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
92 if (param == LBN_SELCANCEL)
94 event.extraLong = FALSE;
97 if (param
== LBN_SELCHANGE
)
99 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
100 wxArrayInt aSelections
;
101 int count
= GetSelections(aSelections
);
104 event
.m_commandInt
= aSelections
[0] ;
105 event
.m_clientData
= GetClientData(event
.m_commandInt
);
106 wxString
str(GetString(event
.m_commandInt
));
108 event
.m_commandString
= copystring((char *)(const char *)str
);
112 event
.m_commandInt
= -1 ;
113 event
.m_commandString
= copystring("") ;
116 event
.SetEventObject( this );
117 ProcessCommand(event
);
118 if (event
.m_commandString
)
119 delete[] event
.m_commandString
;
122 else if (param
== LBN_DBLCLK
)
124 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
125 event
.SetEventObject( this );
126 GetEventHandler()->ProcessEvent(event
) ;
130 #if WXWIN_COMPATIBILITY
131 wxWindow *parent = (wxWindow *)GetParent();
133 parent->GetEventHandler()->OnDefaultAction(this);
143 wxListBox::wxListBox(void)
149 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
152 int n
, const wxString choices
[],
154 const wxValidator
& validator
,
155 const wxString
& name
)
162 SetValidator(validator
);
164 if (parent
) parent
->AddChild(this);
166 wxSystemSettings settings
;
167 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
168 SetForegroundColour(parent
->GetForegroundColour());
170 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
176 m_windowStyle
= style
;
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
;
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
)
191 #if wxUSE_OWNER_DRAWN
192 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
193 // we don't support LBS_OWNERDRAWVARIABLE yet
194 wstyle
|= LBS_OWNERDRAWFIXED
;
197 // Without this style, you get unexpected heights, so e.g. constraint layout
198 // doesn't work properly
199 wstyle
|= LBS_NOINTEGRALHEIGHT
;
202 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
204 // Even with extended styles, need to combine with WS_BORDER
205 // for them to look right.
206 if ( want3D
|| wxStyleHasBorder(m_windowStyle
) )
211 m_hWnd
= (WXHWND
)::CreateWindowEx(exStyle
, "LISTBOX", NULL
,
214 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
215 wxGetInstance(), NULL
);
217 wxCHECK_MSG( m_hWnd
, FALSE
, "Failed to create listbox" );
222 Ctl3dSubclassCtl(hwnd
);
227 // Subclass again to catch messages
231 for (ui
= 0; ui
< (size_t)n
; ui
++) {
235 /* Not needed -- done in Append
236 #if wxUSE_OWNER_DRAWN
237 if ( m_windowStyle & wxLB_OWNERDRAW ) {
238 for (ui = 0; ui < (size_t)n; ui++) {
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);
243 ListBox_SetItemData(hwnd, ui, pNewItem);
249 if ( (m_windowStyle
& wxLB_MULTIPLE
) == 0 )
250 SendMessage(hwnd
, LB_SETCURSEL
, 0, 0);
252 SetFont(parent
->GetFont());
254 SetSize(x
, y
, width
, height
);
261 wxListBox::~wxListBox(void)
263 #if wxUSE_OWNER_DRAWN
264 size_t uiCount
= m_aItems
.Count();
265 while ( uiCount
-- != 0 ) {
266 delete m_aItems
[uiCount
];
271 void wxListBox::SetupColours(void)
273 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
274 SetForegroundColour(GetParent()->GetForegroundColour());
277 void wxListBox::SetFirstItem(int N
)
279 SendMessage(hwnd
,LB_SETTOPINDEX
,(WPARAM
)N
,(LPARAM
)0) ;
282 void wxListBox::SetFirstItem(const wxString
& s
)
284 int N
= FindString(s
) ;
290 void wxListBox::Delete(int N
)
292 SendMessage(hwnd
, LB_DELETESTRING
, N
, 0);
294 SetHorizontalExtent("");
297 void wxListBox::Append(const wxString
& item
)
299 int index
= ListBox_AddString(hwnd
, item
);
302 #if wxUSE_OWNER_DRAWN
303 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
304 wxOwnerDrawn
*pNewItem
= CreateItem(index
); // dummy argument
305 pNewItem
->SetName(item
);
306 m_aItems
.Add(pNewItem
);
307 ListBox_SetItemData(hwnd
, index
, pNewItem
);
311 SetHorizontalExtent(item
);
314 void wxListBox::Append(const wxString
& item
, char *Client_data
)
316 int index
= ListBox_AddString(hwnd
, item
);
319 #if wxUSE_OWNER_DRAWN
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");
327 ListBox_SetItemData(hwnd
, index
, Client_data
);
329 SetHorizontalExtent(item
);
332 void wxListBox::Set(int n
, const wxString
*choices
, char** clientData
)
334 ShowWindow(hwnd
, SW_HIDE
);
335 ListBox_ResetContent(hwnd
);
337 for (i
= 0; i
< n
; i
++)
339 ListBox_AddString(hwnd
, choices
[i
]);
341 ListBox_SetItemData(hwnd
, i
, clientData
[i
]);
345 #if wxUSE_OWNER_DRAWN
346 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
347 // first delete old items
348 size_t ui
= m_aItems
.Count();
349 while ( ui
-- != 0 ) {
354 // then create new ones
355 for (ui
= 0; ui
< (size_t)n
; ui
++) {
356 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
357 pNewItem
->SetName(choices
[ui
]);
358 m_aItems
.Add(pNewItem
);
359 ListBox_SetItemData(hwnd
, ui
, pNewItem
);
361 wxASSERT_MSG(clientData
[ui
] == NULL
,
362 "Can't use client data with owner-drawn listboxes");
367 SetHorizontalExtent("");
368 ShowWindow(hwnd
, SW_SHOW
);
371 int wxListBox::FindString(const wxString
& s
) const
373 int pos
= ListBox_FindStringExact(hwnd
, (WPARAM
)-1, s
);
380 void wxListBox::Clear(void)
382 ListBox_ResetContent(hwnd
);
385 ListBox_GetHorizontalExtent(hwnd
);
388 void wxListBox::SetSelection(int N
, bool select
)
390 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
391 SendMessage(hwnd
, LB_SETSEL
, select
, N
);
397 SendMessage(hwnd
, LB_SETCURSEL
, N1
, 0);
401 bool wxListBox::Selected(int N
) const
403 return SendMessage(hwnd
, LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
406 void wxListBox::Deselect(int N
)
408 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
409 SendMessage(hwnd
, LB_SETSEL
, FALSE
, N
);
412 char *wxListBox::GetClientData(int N
) const
414 return (char *)SendMessage(hwnd
, LB_GETITEMDATA
, N
, 0);
417 void wxListBox::SetClientData(int N
, char *Client_data
)
419 if ( ListBox_SetItemData(hwnd
, N
, Client_data
) == LB_ERR
)
420 wxLogDebug("LB_SETITEMDATA failed");
423 // Return number of selections and an array of selected integers
424 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
428 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
430 int no_sel
= ListBox_GetSelCount(hwnd
);
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!");
437 aSelections
.Alloc(no_sel
);
438 for ( int n
= 0; n
< no_sel
; n
++ )
439 aSelections
.Add(selections
[n
]);
441 delete [] selections
;
446 else // single-selection listbox
448 aSelections
.Add(ListBox_GetCurSel(hwnd
));
454 // Get single selection, for single choice list items
455 int wxListBox::GetSelection() const
457 wxCHECK_MSG( !(m_windowStyle
& wxLB_MULTIPLE
) &&
458 !(m_windowStyle
& wxLB_EXTENDED
),
460 "GetSelection() can't be used with multiple-selection "
461 "listboxes, use GetSelections() instead." );
463 return ListBox_GetCurSel(hwnd
);
466 // Find string for position
467 wxString
wxListBox::GetString(int N
) const
469 if (N
< 0 || N
> m_noItems
)
472 int len
= (int)SendMessage(hwnd
, LB_GETTEXT
, N
, (LONG
)wxBuffer
);
474 return wxString(wxBuffer
);
477 void wxListBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
479 int currentX
, currentY
;
480 GetPosition(¤tX
, ¤tY
);
487 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
489 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
492 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
494 // If we're prepared to use the existing size, then...
495 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
500 int cx
; // button font dimensions
503 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
505 float control_width
, control_height
, control_x
, control_y
;
507 // Deal with default size (using -1 values)
509 w1
= DEFAULT_ITEM_WIDTH
;
512 h1
= DEFAULT_ITEM_HEIGHT
;
514 control_x
= (float)x1
;
515 control_y
= (float)y1
;
516 control_width
= (float)w1
;
517 control_height
= (float)h1
;
519 // Calculations may have made size too small
520 if (control_height
<= 0)
521 control_height
= (float)DEFAULT_ITEM_HEIGHT
;
523 if (control_width
<= 0)
524 control_width
= (float)DEFAULT_ITEM_WIDTH
;
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
);
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.
536 void wxListBox::SetHorizontalExtent(const wxString
& s
)
538 // Only necessary if we want a horizontal scrollbar
539 if (!(m_windowStyle
& wxHSCROLL
))
541 TEXTMETRIC lpTextMetric
;
545 int existingExtent
= (int)SendMessage(hwnd
, LB_GETHORIZONTALEXTENT
, 0, 0L);
546 HDC dc
= GetWindowDC(hwnd
);
548 if (GetFont().Ok() && GetFont().GetResourceHandle())
549 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
551 GetTextMetrics(dc
, &lpTextMetric
);
553 ::GetTextExtentPoint(dc
, (LPSTR
) (const char *)s
, s
.Length(), &extentXY
);
554 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
557 ::SelectObject(dc
, oldFont
);
560 if (extentX
> existingExtent
)
561 SendMessage(hwnd
, LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
566 int largestExtent
= 0;
567 HDC dc
= GetWindowDC(hwnd
);
569 if (GetFont().Ok() && GetFont().GetResourceHandle())
570 oldFont
= (HFONT
) ::SelectObject(dc
, (HFONT
) GetFont().GetResourceHandle());
572 GetTextMetrics(dc
, &lpTextMetric
);
574 for (i
= 0; i
< m_noItems
; i
++)
576 int len
= (int)SendMessage(hwnd
, LB_GETTEXT
, i
, (LONG
)wxBuffer
);
579 ::GetTextExtentPoint(dc
, (LPSTR
)wxBuffer
, len
, &extentXY
);
580 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
581 if (extentX
> largestExtent
)
582 largestExtent
= extentX
;
585 ::SelectObject(dc
, oldFont
);
588 SendMessage(hwnd
, LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
593 wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
596 for (i
= 0; i
< nItems
; i
++)
597 ListBox_InsertString(hwnd
, i
+ pos
, items
[i
]);
600 #if wxUSE_OWNER_DRAWN
601 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
602 for ( i
= 0; i
< nItems
; i
++ ) {
603 wxOwnerDrawn
*pNewItem
= CreateItem((size_t)(pos
+ i
));
604 pNewItem
->SetName(items
[i
]);
605 m_aItems
.Insert(pNewItem
, (size_t)(pos
+ i
));
606 ListBox_SetItemData(hwnd
, i
, pNewItem
);
611 SetHorizontalExtent("");
614 void wxListBox::SetString(int N
, const wxString
& s
)
617 if (!(m_windowStyle
& wxLB_MULTIPLE
) && !(m_windowStyle
& wxLB_EXTENDED
))
618 sel
= GetSelection();
620 char *oldData
= (char *)wxListBox::GetClientData(N
);
622 SendMessage(hwnd
, LB_DELETESTRING
, N
, 0);
625 if (N
== (m_noItems
- 1))
628 SendMessage(hwnd
, LB_INSERTSTRING
, newN
, (LPARAM
) (const char *)s
);
630 wxListBox::SetClientData(N
, oldData
);
632 // Selection may have changed
636 #if wxUSE_OWNER_DRAWN
637 if ( m_windowStyle
& wxLB_OWNERDRAW
)
638 // update item's text
639 m_aItems
[N
]->SetName(s
);
640 #endif //USE_OWNER_DRAWN
643 int wxListBox::Number (void) const
648 // For single selection items only
649 wxString
wxListBox::GetStringSelection (void) const
651 int sel
= GetSelection ();
653 return this->GetString (sel
);
658 bool wxListBox::SetStringSelection (const wxString
& s
, bool flag
)
660 int sel
= FindString (s
);
663 SetSelection (sel
, flag
);
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)
676 void wxListBox::Command (wxCommandEvent
& event
)
678 if (event
.m_extraLong
)
679 SetSelection (event
.m_commandInt
);
682 Deselect (event
.m_commandInt
);
685 ProcessCommand (event
);
688 WXHBRUSH
wxListBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
689 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
694 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
695 return (WXHBRUSH
) hbrush
;
699 if (GetParent()->GetTransparentBackground())
700 SetBkMode((HDC
) pDC
, TRANSPARENT
);
702 SetBkMode((HDC
) pDC
, OPAQUE
);
704 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
705 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
707 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
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();
715 long wxListBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
726 case WM_QUERYDRAGICON
:
730 case WM_RBUTTONDBLCLK
:
733 case WM_MBUTTONDBLCLK
:
736 case WM_LBUTTONDBLCLK
:
742 case WM_INITMENUPOPUP
:
747 case WM_CHAR
: // Always an ASCII character
752 case WM_CTLCOLORLISTBOX
:
753 case WM_CTLCOLORMSGBOX
:
754 case WM_CTLCOLORSCROLLBAR
:
755 case WM_CTLCOLORSTATIC
:
756 case WM_CTLCOLOREDIT
:
757 case WM_SYSCOLORCHANGE
:
761 case WM_QUERYENDSESSION
:
763 case WM_GETMINMAXINFO
:
765 return MSWDefWindowProc(nMsg
, wParam
, lParam
);
769 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
772 #if wxUSE_OWNER_DRAWN
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)
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!
785 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
787 // only owner-drawn control should receive this message
788 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
790 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
793 dc
.SetHDC((WXHDC
)CreateIC("DISPLAY", NULL
, NULL
, 0));
794 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
796 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
797 pStruct
->itemWidth
= dc
.GetCharWidth();
802 // forward the message to the appropriate item
803 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
805 // only owner-drawn control should receive this message
806 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
808 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
809 wxListBoxItem
*pItem
= (wxListBoxItem
*)SendMessage(hwnd
, LB_GETITEMDATA
,
812 wxCHECK( (int)pItem
!= LB_ERR
, FALSE
);
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
);
820 return pItem
->OnDrawItem(dc
, rect
,
821 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
822 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);