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"
28 #include "wx/msw/private.h"
34 #include <wx/msw/gnuwin32/extra.h>
42 #include "wx/ownerdrw.h"
45 #include "wx/dynarray.h"
48 #if !USE_SHARED_LIBRARY
49 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
52 // ============================================================================
53 // list box item declaration and implementation
54 // ============================================================================
58 class wxListBoxItem
: public wxOwnerDrawn
61 wxListBoxItem(const wxString
& str
= "");
64 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
66 // no bitmaps/checkmarks
70 wxOwnerDrawn
*wxListBox::CreateItem(uint n
)
72 return new wxListBoxItem();
75 #endif //USE_OWNER_DRAWN
77 // ============================================================================
78 // list box control implementation
79 // ============================================================================
81 // this macro is dangerous but still better than tons of (HWND)GetHWND()
82 #define hwnd (HWND)GetHWND()
84 bool wxListBox::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
87 if (param == LBN_SELCANCEL)
89 event.extraLong = FALSE;
92 if (param
== LBN_SELCHANGE
)
94 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
95 wxArrayInt aSelections
;
96 int count
= GetSelections(aSelections
);
99 event
.m_commandInt
= aSelections
[0] ;
100 event
.m_clientData
= GetClientData(event
.m_commandInt
);
101 wxString
str(GetString(event
.m_commandInt
));
103 event
.m_commandString
= copystring((char *)(const char *)str
);
107 event
.m_commandInt
= -1 ;
108 event
.m_commandString
= copystring("") ;
111 event
.SetEventObject( this );
112 ProcessCommand(event
);
113 if (event
.m_commandString
)
114 delete[] event
.m_commandString
;
117 else if (param
== LBN_DBLCLK
)
119 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
120 event
.SetEventObject( this );
121 GetEventHandler()->ProcessEvent(event
) ;
125 #if WXWIN_COMPATIBILITY
126 wxWindow *parent = (wxWindow *)GetParent();
128 parent->GetEventHandler()->OnDefaultAction(this);
138 wxListBox::wxListBox(void)
144 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
147 int n
, const wxString choices
[],
149 const wxValidator
& validator
,
150 const wxString
& name
)
157 SetValidator(validator
);
159 if (parent
) parent
->AddChild(this);
161 wxSystemSettings settings
;
162 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
163 SetForegroundColour(parent
->GetDefaultForegroundColour());
165 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
171 m_windowStyle
= style
;
173 DWORD wstyle
= WS_VSCROLL
| WS_TABSTOP
| LBS_NOTIFY
| LBS_HASSTRINGS
;
174 if (m_windowStyle
& wxLB_MULTIPLE
)
175 wstyle
|= LBS_MULTIPLESEL
;
176 else if (m_windowStyle
& wxLB_EXTENDED
)
177 wstyle
|= LBS_EXTENDEDSEL
;
179 if (m_windowStyle
& wxLB_ALWAYS_SB
)
180 wstyle
|= LBS_DISABLENOSCROLL
;
181 if (m_windowStyle
& wxLB_HSCROLL
)
182 wstyle
|= WS_HSCROLL
;
183 if (m_windowStyle
& wxLB_SORT
)
187 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
188 // we don't support LBS_OWNERDRAWVARIABLE yet
189 wstyle
|= LBS_OWNERDRAWFIXED
;
192 // Without this style, you get unexpected heights, so e.g. constraint layout
193 // doesn't work properly
194 wstyle
|= LBS_NOINTEGRALHEIGHT
;
197 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
199 // Even with extended styles, need to combine with WS_BORDER
200 // for them to look right.
201 if ( want3D
|| (m_windowStyle
& wxSIMPLE_BORDER
)
202 || (m_windowStyle
& wxRAISED_BORDER
)
203 || (m_windowStyle
& wxSUNKEN_BORDER
)
204 || (m_windowStyle
& wxDOUBLE_BORDER
) ) {
208 HWND wx_list
= CreateWindowEx(exStyle
, "LISTBOX", NULL
,
211 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
212 wxGetInstance(), NULL
);
214 m_hWnd
= (WXHWND
)wx_list
;
219 Ctl3dSubclassCtl(wx_list
);
224 // Subclass again to catch messages
225 SubclassWin((WXHWND
)wx_list
);
228 for (ui
= 0; ui
< (uint
)n
; ui
++) {
229 SendMessage(wx_list
, LB_ADDSTRING
, 0, (LPARAM
)(const char *)choices
[ui
]);
233 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
234 for (ui
= 0; ui
< (uint
)n
; ui
++) {
235 // create new item which will process WM_{DRAW|MEASURE}ITEM messages
236 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
237 pNewItem
->SetName(choices
[ui
]);
238 m_aItems
.Add(pNewItem
);
239 ListBox_SetItemData(wx_list
, ui
, pNewItem
);
244 if ((m_windowStyle
& wxLB_MULTIPLE
) == 0)
245 SendMessage(wx_list
, LB_SETCURSEL
, 0, 0);
247 SetFont(* parent
->GetFont());
249 SetSize(x
, y
, width
, height
);
251 ShowWindow(wx_list
, SW_SHOW
);
256 wxListBox::~wxListBox(void)
259 uint uiCount
= m_aItems
.Count();
260 while ( uiCount
-- != 0 ) {
261 delete m_aItems
[uiCount
];
266 void wxListBox::SetupColours(void)
268 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
269 SetForegroundColour(GetParent()->GetDefaultForegroundColour());
272 void wxListBox::SetFirstItem(int N
)
274 SendMessage(hwnd
,LB_SETTOPINDEX
,(WPARAM
)N
,(LPARAM
)0) ;
277 void wxListBox::SetFirstItem(const wxString
& s
)
279 int N
= FindString(s
) ;
285 void wxListBox::Delete(int N
)
287 SendMessage(hwnd
, LB_DELETESTRING
, N
, 0);
289 SetHorizontalExtent("");
292 void wxListBox::Append(const wxString
& item
)
294 int index
= ListBox_AddString(hwnd
, item
);
298 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
299 wxOwnerDrawn
*pNewItem
= CreateItem(-1); // dummy argument
300 pNewItem
->SetName(item
);
301 m_aItems
.Add(pNewItem
);
302 ListBox_SetItemData(hwnd
, index
, pNewItem
);
306 SetHorizontalExtent(item
);
309 void wxListBox::Append(const wxString
& item
, char *Client_data
)
311 int index
= ListBox_AddString(hwnd
, item
);
315 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
316 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
317 // in OnMeasure/OnDraw.
318 wxFAIL_MSG("Can't use client data with owner-drawn listboxes");
322 ListBox_SetItemData(hwnd
, index
, Client_data
);
324 SetHorizontalExtent(item
);
327 void wxListBox::Set(int n
, const wxString
*choices
, char** clientData
)
329 ShowWindow(hwnd
, SW_HIDE
);
330 ListBox_ResetContent(hwnd
);
332 for (i
= 0; i
< n
; i
++)
334 ListBox_AddString(hwnd
, choices
[i
]);
336 ListBox_SetItemData(hwnd
, i
, clientData
[i
]);
341 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
342 // first delete old items
343 uint ui
= m_aItems
.Count();
344 while ( ui
-- != 0 ) {
349 // then create new ones
350 for (ui
= 0; ui
< (uint
)n
; ui
++) {
351 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
352 pNewItem
->SetName(choices
[ui
]);
353 m_aItems
.Add(pNewItem
);
354 ListBox_SetItemData(hwnd
, ui
, pNewItem
);
356 wxASSERT_MSG(clientData
[ui
] == NULL
,
357 "Can't use client data with owner-drawn listboxes");
362 SetHorizontalExtent("");
363 ShowWindow(hwnd
, SW_SHOW
);
366 int wxListBox::FindString(const wxString
& s
) const
368 int pos
= ListBox_FindStringExact(hwnd
, (WPARAM
)-1, s
);
375 void wxListBox::Clear(void)
377 ListBox_ResetContent(hwnd
);
380 ListBox_GetHorizontalExtent(hwnd
);
383 void wxListBox::SetSelection(int N
, bool select
)
385 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
386 SendMessage(hwnd
, LB_SETSEL
, select
, N
);
392 SendMessage(hwnd
, LB_SETCURSEL
, N1
, 0);
396 bool wxListBox::Selected(int N
) const
398 return SendMessage(hwnd
, LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
401 void wxListBox::Deselect(int N
)
403 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
404 SendMessage(hwnd
, LB_SETSEL
, FALSE
, N
);
407 char *wxListBox::GetClientData(int N
) const
409 return (char *)SendMessage(hwnd
, LB_GETITEMDATA
, N
, 0);
412 void wxListBox::SetClientData(int N
, char *Client_data
)
414 if ( ListBox_SetItemData(hwnd
, N
, Client_data
) == LB_ERR
)
415 wxLogDebug("LB_SETITEMDATA failed");
418 // Return number of selections and an array of selected integers
419 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
423 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
425 int no_sel
= ListBox_GetSelCount(hwnd
);
427 int *selections
= new int[no_sel
];
428 if ( ListBox_GetSelItems(hwnd
, no_sel
, selections
) == LB_ERR
) {
429 wxFAIL_MSG("This listbox can't have single-selection style!");
432 aSelections
.Alloc(no_sel
);
433 for ( int n
= 0; n
< no_sel
; n
++ )
434 aSelections
.Add(selections
[n
]);
436 delete [] selections
;
441 else // single-selection listbox
443 aSelections
.Add(ListBox_GetCurSel(hwnd
));
449 // Get single selection, for single choice list items
450 int wxListBox::GetSelection() const
452 wxCHECK_MSG( !(m_windowStyle
& wxLB_MULTIPLE
) &&
453 !(m_windowStyle
& wxLB_EXTENDED
),
455 "GetSelection() can't be used with multiple-selection "
456 "listboxes, use GetSelections() instead." );
458 return ListBox_GetCurSel(hwnd
);
461 // Find string for position
462 wxString
wxListBox::GetString(int N
) const
464 if (N
< 0 || N
> m_noItems
)
467 int len
= (int)SendMessage(hwnd
, LB_GETTEXT
, N
, (LONG
)wxBuffer
);
469 return wxString(wxBuffer
);
472 void wxListBox::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
474 int currentX
, currentY
;
475 GetPosition(¤tX
, ¤tY
);
482 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
484 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
487 // If we're prepared to use the existing size, then...
488 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
493 int cx
; // button font dimensions
496 wxGetCharSize(GetHWND(), &cx
, &cy
,GetFont());
498 float control_width
, control_height
, control_x
, control_y
;
500 // Deal with default size (using -1 values)
502 w1
= DEFAULT_ITEM_WIDTH
;
505 h1
= DEFAULT_ITEM_HEIGHT
;
507 control_x
= (float)x1
;
508 control_y
= (float)y1
;
509 control_width
= (float)w1
;
510 control_height
= (float)h1
;
512 // Calculations may have made size too small
513 if (control_height
<= 0)
514 control_height
= (float)DEFAULT_ITEM_HEIGHT
;
516 if (control_width
<= 0)
517 control_width
= (float)DEFAULT_ITEM_WIDTH
;
519 // wxDebugMsg("About to set the listbox height to %d", (int)control_height);
520 MoveWindow(hwnd
, (int)control_x
, (int)control_y
,
521 (int)control_width
, (int)control_height
, TRUE
);
525 // Windows-specific code to set the horizontal extent of
526 // the listbox, if necessary. If s is non-NULL, it's
527 // used to calculate the horizontal extent.
528 // Otherwise, all strings are used.
529 void wxListBox::SetHorizontalExtent(const wxString
& s
)
531 // Only necessary if we want a horizontal scrollbar
532 if (!(m_windowStyle
& wxHSCROLL
))
534 TEXTMETRIC lpTextMetric
;
538 int existingExtent
= (int)SendMessage(hwnd
, LB_GETHORIZONTALEXTENT
, 0, 0L);
539 HDC dc
= GetWindowDC(hwnd
);
541 if (GetFont() && GetFont()->GetResourceHandle())
542 oldFont
= ::SelectObject(dc
, (HFONT
) GetFont()->GetResourceHandle());
544 GetTextMetrics(dc
, &lpTextMetric
);
546 ::GetTextExtentPoint(dc
, (LPSTR
) (const char *)s
, s
.Length(), &extentXY
);
547 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
550 ::SelectObject(dc
, oldFont
);
553 if (extentX
> existingExtent
)
554 SendMessage(hwnd
, LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
559 int largestExtent
= 0;
560 HDC dc
= GetWindowDC(hwnd
);
562 if (GetFont() && GetFont()->GetResourceHandle())
563 oldFont
= ::SelectObject(dc
, (HFONT
) GetFont()->GetResourceHandle());
565 GetTextMetrics(dc
, &lpTextMetric
);
567 for (i
= 0; i
< m_noItems
; i
++)
569 int len
= (int)SendMessage(hwnd
, LB_GETTEXT
, i
, (LONG
)wxBuffer
);
572 ::GetTextExtentPoint(dc
, (LPSTR
)wxBuffer
, len
, &extentXY
);
573 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
574 if (extentX
> largestExtent
)
575 largestExtent
= extentX
;
578 ::SelectObject(dc
, oldFont
);
581 SendMessage(hwnd
, LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
586 wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
589 for (i
= 0; i
< nItems
; i
++)
590 ListBox_InsertString(hwnd
, i
+ pos
, items
[i
]);
594 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
595 for ( i
= 0; i
< nItems
; i
++ ) {
596 wxOwnerDrawn
*pNewItem
= CreateItem((uint
)(pos
+ i
));
597 pNewItem
->SetName(items
[i
]);
598 m_aItems
.Insert(pNewItem
, (uint
)(pos
+ i
));
599 ListBox_SetItemData(hwnd
, i
, pNewItem
);
604 SetHorizontalExtent("");
607 void wxListBox::SetString(int N
, const wxString
& s
)
609 int sel
= GetSelection();
611 char *oldData
= (char *)wxListBox::GetClientData(N
);
613 SendMessage(hwnd
, LB_DELETESTRING
, N
, 0);
616 if (N
== (m_noItems
- 1))
619 SendMessage(hwnd
, LB_INSERTSTRING
, newN
, (LPARAM
) (const char *)s
);
621 wxListBox::SetClientData(N
, oldData
);
623 // Selection may have changed
628 if ( m_windowStyle
& wxLB_OWNERDRAW
)
629 // update item's text
630 m_aItems
[N
]->SetName(s
);
631 #endif //USE_OWNER_DRAWN
634 int wxListBox::Number (void) const
639 // For single selection items only
640 wxString
wxListBox::GetStringSelection (void) const
642 int sel
= GetSelection ();
644 return this->GetString (sel
);
649 bool wxListBox::SetStringSelection (const wxString
& s
, bool flag
)
651 int sel
= FindString (s
);
654 SetSelection (sel
, flag
);
661 // Is this the right thing? Won't setselection generate a command
662 // event too? No! It'll just generate a setselection event.
663 // But we still can't have this being called whenever a real command
664 // is generated, because it sets the selection, which will already
665 // have been done! (Unless we have an optional argument for calling
666 // by the actual window system, or a separate function, ProcessCommand)
667 void wxListBox::Command (wxCommandEvent
& event
)
669 if (event
.m_extraLong
)
670 SetSelection (event
.m_commandInt
);
673 Deselect (event
.m_commandInt
);
676 ProcessCommand (event
);
679 WXHBRUSH
wxListBox::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
680 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
685 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
686 return (WXHBRUSH
) hbrush
;
690 if (GetParent()->GetTransparentBackground())
691 SetBkMode((HDC
) pDC
, TRANSPARENT
);
693 SetBkMode((HDC
) pDC
, OPAQUE
);
695 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
696 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
698 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
700 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
701 // has a zero usage count.
702 backgroundBrush
->RealizeResource();
703 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
706 long wxListBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
717 case WM_QUERYDRAGICON
:
721 case WM_RBUTTONDBLCLK
:
724 case WM_MBUTTONDBLCLK
:
727 case WM_LBUTTONDBLCLK
:
733 case WM_INITMENUPOPUP
:
738 case WM_CHAR
: // Always an ASCII character
743 case WM_CTLCOLORLISTBOX
:
744 case WM_CTLCOLORMSGBOX
:
745 case WM_CTLCOLORSCROLLBAR
:
746 case WM_CTLCOLORSTATIC
:
747 case WM_CTLCOLOREDIT
:
748 case WM_SYSCOLORCHANGE
:
752 case WM_QUERYENDSESSION
:
754 case WM_GETMINMAXINFO
:
756 return MSWDefWindowProc(nMsg
, wParam
, lParam
);
760 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
768 // space beneath/above each row in pixels
769 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
770 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
772 // the height is the same for all items
773 // ## should be changed for LBS_OWNERDRAWVARIABLE style listboxes
774 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
775 // message is not yet sent when we get here!
776 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
778 // only owner-drawn control should receive this message
779 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
781 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
784 dc
.SetHDC((WXHDC
)CreateIC("DISPLAY", NULL
, NULL
, 0));
785 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
787 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
788 pStruct
->itemWidth
= dc
.GetCharWidth();
793 // forward the message to the appropriate item
794 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
796 // only owner-drawn control should receive this message
797 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
799 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
800 wxListBoxItem
*pItem
= (wxListBoxItem
*)SendMessage(hwnd
, LB_GETITEMDATA
,
803 wxCHECK( (int)pItem
!= LB_ERR
, FALSE
);
806 dc
.SetHDC((WXHDC
)pStruct
->hDC
, FALSE
);
807 wxRect
rect(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
,
808 pStruct
->rcItem
.right
- pStruct
->rcItem
.left
,
809 pStruct
->rcItem
.bottom
- pStruct
->rcItem
.top
);
811 return pItem
->OnDrawItem(dc
, rect
,
812 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
813 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);