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 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
49 // ============================================================================
50 // list box item declaration and implementation
51 // ============================================================================
55 class wxListBoxItem
: public wxOwnerDrawn
58 wxListBoxItem(const wxString
& str
= "");
61 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
63 // no bitmaps/checkmarks
67 wxOwnerDrawn
*wxListBox::CreateItem(uint n
)
69 return new wxListBoxItem();
72 #endif //USE_OWNER_DRAWN
74 // ============================================================================
75 // list box control implementation
76 // ============================================================================
78 // this macro is dangerous but still better than tons of (HWND)GetHWND()
79 #define hwnd (HWND)GetHWND()
81 bool wxListBox::MSWCommand(const WXUINT param
, const WXWORD
WXUNUSED(id
))
84 if (param == LBN_SELCANCEL)
86 event.extraLong = FALSE;
89 if (param
== LBN_SELCHANGE
)
91 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
93 int count
= GetSelections(&liste
) ;
96 event
.m_commandInt
= liste
[0] ;
97 event
.m_clientData
= GetClientData(event
.m_commandInt
);
98 wxString
str(GetString(event
.m_commandInt
));
100 event
.m_commandString
= copystring((char *)(const char *)str
);
104 event
.m_commandInt
= -1 ;
105 event
.m_commandString
= copystring("") ;
108 event
.SetEventObject( this );
109 ProcessCommand(event
);
110 if (event
.m_commandString
)
111 delete[] event
.m_commandString
;
114 else if (param
== LBN_DBLCLK
)
116 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
117 event
.SetEventObject( this );
118 if ( !GetEventHandler()->ProcessEvent(event
) )
120 #if WXWIN_COMPATIBILITY
121 wxWindow
*parent
= (wxWindow
*)GetParent();
123 parent
->GetEventHandler()->OnDefaultAction(this);
132 wxListBox::wxListBox(void)
139 bool wxListBox::Create(wxWindow
*parent
, const wxWindowID id
,
142 const int n
, const wxString choices
[],
144 const wxValidator
& validator
,
145 const wxString
& name
)
153 SetValidator(validator
);
155 if (parent
) parent
->AddChild(this);
157 wxSystemSettings settings
;
158 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
159 SetForegroundColour(parent
->GetDefaultForegroundColour());
161 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
167 m_windowStyle
= style
;
169 DWORD wstyle
= WS_VSCROLL
| WS_TABSTOP
| LBS_NOTIFY
| LBS_HASSTRINGS
;
170 if (m_windowStyle
& wxLB_MULTIPLE
)
171 wstyle
|= LBS_MULTIPLESEL
;
172 else if (m_windowStyle
& wxLB_EXTENDED
)
173 wstyle
|= LBS_EXTENDEDSEL
;
175 if (m_windowStyle
& wxLB_ALWAYS_SB
)
176 wstyle
|= LBS_DISABLENOSCROLL
;
177 if (m_windowStyle
& wxLB_HSCROLL
)
178 wstyle
|= WS_HSCROLL
;
179 if (m_windowStyle
& wxLB_SORT
)
183 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
184 // we don't support LBS_OWNERDRAWVARIABLE yet
185 wstyle
|= LBS_OWNERDRAWFIXED
;
188 // Without this style, you get unexpected heights, so e.g. constraint layout
189 // doesn't work properly
190 wstyle
|= LBS_NOINTEGRALHEIGHT
;
193 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
195 // Even with extended styles, need to combine with WS_BORDER
196 // for them to look right.
197 if ( want3D
|| (m_windowStyle
& wxSIMPLE_BORDER
)
198 || (m_windowStyle
& wxRAISED_BORDER
)
199 || (m_windowStyle
& wxSUNKEN_BORDER
)
200 || (m_windowStyle
& wxDOUBLE_BORDER
) ) {
204 HWND wx_list
= CreateWindowEx(exStyle
, "LISTBOX", NULL
,
207 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
208 wxGetInstance(), NULL
);
212 Ctl3dSubclassCtl(wx_list
);
218 for (ui
= 0; ui
< (uint
)n
; ui
++) {
219 SendMessage(wx_list
, LB_ADDSTRING
, 0, (LPARAM
)(const char *)choices
[ui
]);
223 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
224 for (ui
= 0; ui
< (uint
)n
; ui
++) {
225 // create new item which will process WM_{DRAW|MEASURE}ITEM messages
226 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
227 pNewItem
->SetName(choices
[ui
]);
228 m_aItems
.Add(pNewItem
);
229 ListBox_SetItemData(wx_list
, ui
, pNewItem
);
234 if ((m_windowStyle
& wxLB_MULTIPLE
) == 0)
235 SendMessage(wx_list
, LB_SETCURSEL
, 0, 0);
237 ShowWindow(wx_list
, SW_SHOW
);
239 m_hWnd
= (WXHWND
)wx_list
;
241 // Subclass again for purposes of dialog editing mode
242 SubclassWin((WXHWND
)wx_list
);
244 SetFont(* parent
->GetFont());
246 SetSize(x
, y
, width
, height
);
251 wxListBox::~wxListBox(void)
254 uint uiCount
= m_aItems
.Count();
255 while ( uiCount
-- != 0 ) {
256 delete m_aItems
[uiCount
];
260 DELETEA(m_selections
);
263 void wxListBox::SetupColours(void)
265 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
266 SetForegroundColour(GetParent()->GetDefaultForegroundColour());
269 void wxListBox::SetFirstItem(const int N
)
271 SendMessage(hwnd
,LB_SETTOPINDEX
,(WPARAM
)N
,(LPARAM
)0) ;
274 void wxListBox::SetFirstItem(const wxString
& s
)
276 int N
= FindString(s
) ;
282 void wxListBox::Delete(const int N
)
284 SendMessage(hwnd
, LB_DELETESTRING
, N
, 0);
286 SetHorizontalExtent("");
289 void wxListBox::Append(const wxString
& item
)
291 int index
= ListBox_AddString(hwnd
, item
);
295 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
296 wxOwnerDrawn
*pNewItem
= CreateItem(-1); // dummy argument
297 pNewItem
->SetName(item
);
298 m_aItems
.Add(pNewItem
);
299 ListBox_SetItemData(hwnd
, index
, pNewItem
);
303 SetHorizontalExtent(item
);
306 void wxListBox::Append(const wxString
& item
, char *Client_data
)
308 int index
= ListBox_AddString(hwnd
, item
);
312 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
313 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
314 // in OnMeasure/OnDraw.
315 wxFAIL_MSG("Can't use client data with owner-drawn listboxes");
319 ListBox_SetItemData(hwnd
, index
, Client_data
);
321 SetHorizontalExtent(item
);
324 void wxListBox::Set(const int n
, const wxString
*choices
, char** clientData
)
326 ShowWindow(hwnd
, SW_HIDE
);
327 ListBox_ResetContent(hwnd
);
329 for (i
= 0; i
< n
; i
++)
331 ListBox_AddString(hwnd
, choices
[i
]);
333 ListBox_SetItemData(hwnd
, i
, clientData
[i
]);
338 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
339 // first delete old items
340 uint ui
= m_aItems
.Count();
341 while ( ui
-- != 0 ) {
346 // then create new ones
347 for (ui
= 0; ui
< (uint
)n
; ui
++) {
348 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
349 pNewItem
->SetName(choices
[ui
]);
350 m_aItems
.Add(pNewItem
);
351 ListBox_SetItemData(hwnd
, ui
, pNewItem
);
353 wxASSERT_MSG(clientData
[ui
] == NULL
,
354 "Can't use client data with owner-drawn listboxes");
359 SetHorizontalExtent("");
360 ShowWindow(hwnd
, SW_SHOW
);
363 int wxListBox::FindString(const wxString
& s
) const
365 int pos
= ListBox_FindStringExact(hwnd
, (WPARAM
)-1, s
);
372 void wxListBox::Clear(void)
374 ListBox_ResetContent(hwnd
);
377 ListBox_GetHorizontalExtent(hwnd
);
380 void wxListBox::SetSelection(const int N
, const bool select
)
382 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
383 SendMessage(hwnd
, LB_SETSEL
, select
, N
);
389 SendMessage(hwnd
, LB_SETCURSEL
, N1
, 0);
393 bool wxListBox::Selected(const int N
) const
395 return SendMessage(hwnd
, LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
398 void wxListBox::Deselect(const int N
)
400 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
401 SendMessage(hwnd
, LB_SETSEL
, FALSE
, N
);
404 char *wxListBox::GetClientData(const int N
) const
406 return (char *)SendMessage(hwnd
, LB_GETITEMDATA
, N
, 0);
409 void wxListBox::SetClientData(const int N
, char *Client_data
)
411 (void)SendMessage(hwnd
, LB_SETITEMDATA
, N
, (LONG
)Client_data
);
413 if (result == LB_ERR)
420 // Return number of selections and an array of selected integers
421 // Use selections field to store data, which will be cleaned up
422 // by destructor if necessary.
423 int wxListBox::GetSelections(int **list_selections
) const
425 wxListBox
*nonConst
= (wxListBox
*)this; // const is a white lie!
426 if (nonConst
->m_selections
)
427 { delete[] nonConst
->m_selections
; nonConst
->m_selections
= NULL
; };
428 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
430 int no_sel
= (int)SendMessage(hwnd
, LB_GETSELCOUNT
, 0, 0);
433 nonConst
->m_selections
= new int[no_sel
];
434 SendMessage(hwnd
, LB_GETSELITEMS
, no_sel
, (LONG
)m_selections
);
435 *list_selections
= m_selections
;
440 int sel
= (int)SendMessage(hwnd
, LB_GETCURSEL
, 0, 0);
443 nonConst
->m_selections
= new int[1];
444 nonConst
->m_selections
[0] = sel
;
445 *list_selections
= m_selections
;
450 // Get single selection, for single choice list items
451 int wxListBox::GetSelection(void) const
453 wxListBox
*nonConst
= (wxListBox
*)this; // const is a white lie!
454 if (nonConst
->m_selections
)
455 { delete[] nonConst
->m_selections
; nonConst
->m_selections
= NULL
; };
456 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
460 int sel
= (int)SendMessage(hwnd
, LB_GETCURSEL
, 0, 0);
470 // Find string for position
471 wxString
wxListBox::GetString(const int N
) const
473 if (N
< 0 || N
> m_noItems
)
476 int len
= (int)SendMessage(hwnd
, LB_GETTEXT
, N
, (LONG
)wxBuffer
);
478 return wxString(wxBuffer
);
481 void wxListBox::SetSize(const int x
, const int y
, const int width
, const int height
, const int sizeFlags
)
483 int currentX
, currentY
;
484 GetPosition(¤tX
, ¤tY
);
491 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
493 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
496 // If we're prepared to use the existing size, then...
497 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
502 int cx
; // button font dimensions
505 wxGetCharSize(GetHWND(), &cx
, &cy
,GetFont());
507 float control_width
, control_height
, control_x
, control_y
;
509 // Deal with default size (using -1 values)
511 w1
= DEFAULT_ITEM_WIDTH
;
514 h1
= DEFAULT_ITEM_HEIGHT
;
516 control_x
= (float)x1
;
517 control_y
= (float)y1
;
518 control_width
= (float)w1
;
519 control_height
= (float)h1
;
521 // Calculations may have made size too small
522 if (control_height
<= 0)
523 control_height
= (float)DEFAULT_ITEM_HEIGHT
;
525 if (control_width
<= 0)
526 control_width
= (float)DEFAULT_ITEM_WIDTH
;
528 // wxDebugMsg("About to set the listbox height to %d", (int)control_height);
529 MoveWindow(hwnd
, (int)control_x
, (int)control_y
,
530 (int)control_width
, (int)control_height
, TRUE
);
533 #if WXWIN_COMPATIBILITY
534 GetEventHandler()->OldOnSize(width, height);
536 wxSizeEvent event(wxSize(width, height), m_windowId);
537 event.eventObject = this;
538 GetEventHandler()->ProcessEvent(event);
544 // Windows-specific code to set the horizontal extent of
545 // the listbox, if necessary. If s is non-NULL, it's
546 // used to calculate the horizontal extent.
547 // Otherwise, all strings are used.
548 void wxListBox::SetHorizontalExtent(const wxString
& s
)
550 // Only necessary if we want a horizontal scrollbar
551 if (!(m_windowStyle
& wxHSCROLL
))
553 TEXTMETRIC lpTextMetric
;
557 int existingExtent
= (int)SendMessage(hwnd
, LB_GETHORIZONTALEXTENT
, 0, 0L);
558 HDC dc
= GetWindowDC(hwnd
);
560 if (GetFont() && GetFont()->GetResourceHandle())
561 oldFont
= ::SelectObject(dc
, (HFONT
) GetFont()->GetResourceHandle());
563 GetTextMetrics(dc
, &lpTextMetric
);
565 ::GetTextExtentPoint(dc
, (LPSTR
) (const char *)s
, s
.Length(), &extentXY
);
566 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
569 ::SelectObject(dc
, oldFont
);
572 if (extentX
> existingExtent
)
573 SendMessage(hwnd
, LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
578 int largestExtent
= 0;
579 HDC dc
= GetWindowDC(hwnd
);
581 if (GetFont() && GetFont()->GetResourceHandle())
582 oldFont
= ::SelectObject(dc
, (HFONT
) GetFont()->GetResourceHandle());
584 GetTextMetrics(dc
, &lpTextMetric
);
586 for (i
= 0; i
< m_noItems
; i
++)
588 int len
= (int)SendMessage(hwnd
, LB_GETTEXT
, i
, (LONG
)wxBuffer
);
591 ::GetTextExtentPoint(dc
, (LPSTR
)wxBuffer
, len
, &extentXY
);
592 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
593 if (extentX
> largestExtent
)
594 largestExtent
= extentX
;
597 ::SelectObject(dc
, oldFont
);
600 SendMessage(hwnd
, LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
605 wxListBox::InsertItems(const int nItems
, const wxString items
[], const int pos
)
608 for (i
= 0; i
< nItems
; i
++)
609 ListBox_InsertString(hwnd
, i
+ pos
, items
[i
]);
613 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
614 for ( i
= 0; i
< nItems
; i
++ ) {
615 wxOwnerDrawn
*pNewItem
= CreateItem((uint
)(pos
+ i
));
616 pNewItem
->SetName(items
[i
]);
617 m_aItems
.Insert(pNewItem
, (uint
)(pos
+ i
));
618 ListBox_SetItemData(hwnd
, i
, pNewItem
);
623 SetHorizontalExtent("");
626 void wxListBox::SetString(const int N
, const wxString
& s
)
628 int sel
= GetSelection();
630 char *oldData
= (char *)wxListBox::GetClientData(N
);
632 SendMessage(hwnd
, LB_DELETESTRING
, N
, 0);
635 if (N
== (m_noItems
- 1))
638 SendMessage(hwnd
, LB_INSERTSTRING
, newN
, (LPARAM
) (const char *)s
);
640 wxListBox::SetClientData(N
, oldData
);
642 // Selection may have changed
647 if ( m_windowStyle
& wxLB_OWNERDRAW
)
648 // update item's text
649 m_aItems
[N
]->SetName(s
);
650 #endif //USE_OWNER_DRAWN
653 int wxListBox::Number (void) const
658 // For single selection items only
659 wxString
wxListBox::GetStringSelection (void) const
661 int sel
= GetSelection ();
663 return this->GetString (sel
);
668 bool wxListBox::SetStringSelection (const wxString
& s
, const bool flag
)
670 int sel
= FindString (s
);
673 SetSelection (sel
, flag
);
680 // Is this the right thing? Won't setselection generate a command
681 // event too? No! It'll just generate a setselection event.
682 // But we still can't have this being called whenever a real command
683 // is generated, because it sets the selection, which will already
684 // have been done! (Unless we have an optional argument for calling
685 // by the actual window system, or a separate function, ProcessCommand)
686 void wxListBox::Command (wxCommandEvent
& event
)
688 if (event
.m_extraLong
)
689 SetSelection (event
.m_commandInt
);
692 Deselect (event
.m_commandInt
);
695 ProcessCommand (event
);
698 WXHBRUSH
wxListBox::OnCtlColor(const WXHDC pDC
, const WXHWND pWnd
, const WXUINT nCtlColor
,
699 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
704 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
705 return (WXHBRUSH
) hbrush
;
709 if (GetParent()->GetTransparentBackground())
710 SetBkMode((HDC
) pDC
, TRANSPARENT
);
712 SetBkMode((HDC
) pDC
, OPAQUE
);
714 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
715 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
717 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
719 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
720 // has a zero usage count.
721 backgroundBrush
->RealizeResource();
722 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
725 long wxListBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
736 case WM_QUERYDRAGICON:
740 case WM_RBUTTONDBLCLK:
743 case WM_MBUTTONDBLCLK:
746 // case WM_LBUTTONDBLCLK:
752 case WM_INITMENUPOPUP:
757 case WM_CHAR: // Always an ASCII character
762 case WM_CTLCOLORLISTBOX:
763 case WM_CTLCOLORMSGBOX:
764 case WM_CTLCOLORSCROLLBAR:
765 case WM_CTLCOLORSTATIC:
766 case WM_CTLCOLOREDIT:
767 case WM_SYSCOLORCHANGE:
771 case WM_QUERYENDSESSION:
773 case WM_GETMINMAXINFO:
775 return MSWDefWindowProc(nMsg, wParam, lParam );
778 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
786 // space beneath/above each row in pixels
787 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
788 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
790 // the height is the same for all items
791 // ## should be changed for LBS_OWNERDRAWVARIABLE style listboxes
792 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
793 // message is not yet sent when we get here!
794 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
796 // only owner-drawn control should receive this message
797 wxCHECK_RET( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
799 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
802 dc
.SetHDC((WXHDC
)CreateIC("DISPLAY", NULL
, NULL
, 0));
803 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
805 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
806 pStruct
->itemWidth
= dc
.GetCharWidth();
811 // forward the message to the appropriate item
812 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
814 // only owner-drawn control should receive this message
815 wxCHECK_RET( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
817 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
818 wxListBoxItem
*pItem
= (wxListBoxItem
*)SendMessage(hwnd
, LB_GETITEMDATA
,
821 wxCHECK_RET( (int)pItem
!= LB_ERR
, FALSE
);
824 dc
.SetHDC((WXHDC
)pStruct
->hDC
, FALSE
);
825 wxRect
rect(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
,
826 pStruct
->rcItem
.right
- pStruct
->rcItem
.left
,
827 pStruct
->rcItem
.bottom
- pStruct
->rcItem
.top
);
829 return pItem
->OnDrawItem(dc
, rect
,
830 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
831 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);