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(const WXUINT param
, const 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 if ( !GetEventHandler()->ProcessEvent(event
) )
123 #if WXWIN_COMPATIBILITY
124 wxWindow
*parent
= (wxWindow
*)GetParent();
126 parent
->GetEventHandler()->OnDefaultAction(this);
135 wxListBox::wxListBox(void)
141 bool wxListBox::Create(wxWindow
*parent
, const wxWindowID id
,
144 const int n
, const wxString choices
[],
146 const wxValidator
& validator
,
147 const wxString
& name
)
154 SetValidator(validator
);
156 if (parent
) parent
->AddChild(this);
158 wxSystemSettings settings
;
159 SetBackgroundColour(settings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
160 SetForegroundColour(parent
->GetDefaultForegroundColour());
162 m_windowId
= ( id
== -1 ) ? (int)NewControlId() : id
;
168 m_windowStyle
= style
;
170 DWORD wstyle
= WS_VSCROLL
| WS_TABSTOP
| LBS_NOTIFY
| LBS_HASSTRINGS
;
171 if (m_windowStyle
& wxLB_MULTIPLE
)
172 wstyle
|= LBS_MULTIPLESEL
;
173 else if (m_windowStyle
& wxLB_EXTENDED
)
174 wstyle
|= LBS_EXTENDEDSEL
;
176 if (m_windowStyle
& wxLB_ALWAYS_SB
)
177 wstyle
|= LBS_DISABLENOSCROLL
;
178 if (m_windowStyle
& wxLB_HSCROLL
)
179 wstyle
|= WS_HSCROLL
;
180 if (m_windowStyle
& wxLB_SORT
)
184 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
185 // we don't support LBS_OWNERDRAWVARIABLE yet
186 wstyle
|= LBS_OWNERDRAWFIXED
;
189 // Without this style, you get unexpected heights, so e.g. constraint layout
190 // doesn't work properly
191 wstyle
|= LBS_NOINTEGRALHEIGHT
;
194 WXDWORD exStyle
= Determine3DEffects(WS_EX_CLIENTEDGE
, &want3D
) ;
196 // Even with extended styles, need to combine with WS_BORDER
197 // for them to look right.
198 if ( want3D
|| (m_windowStyle
& wxSIMPLE_BORDER
)
199 || (m_windowStyle
& wxRAISED_BORDER
)
200 || (m_windowStyle
& wxSUNKEN_BORDER
)
201 || (m_windowStyle
& wxDOUBLE_BORDER
) ) {
205 HWND wx_list
= CreateWindowEx(exStyle
, "LISTBOX", NULL
,
208 (HWND
)parent
->GetHWND(), (HMENU
)m_windowId
,
209 wxGetInstance(), NULL
);
213 Ctl3dSubclassCtl(wx_list
);
219 for (ui
= 0; ui
< (uint
)n
; ui
++) {
220 SendMessage(wx_list
, LB_ADDSTRING
, 0, (LPARAM
)(const char *)choices
[ui
]);
224 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
225 for (ui
= 0; ui
< (uint
)n
; ui
++) {
226 // create new item which will process WM_{DRAW|MEASURE}ITEM messages
227 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
228 pNewItem
->SetName(choices
[ui
]);
229 m_aItems
.Add(pNewItem
);
230 ListBox_SetItemData(wx_list
, ui
, pNewItem
);
235 if ((m_windowStyle
& wxLB_MULTIPLE
) == 0)
236 SendMessage(wx_list
, LB_SETCURSEL
, 0, 0);
238 ShowWindow(wx_list
, SW_SHOW
);
240 m_hWnd
= (WXHWND
)wx_list
;
242 // Subclass again for purposes of dialog editing mode
243 SubclassWin((WXHWND
)wx_list
);
245 SetFont(* parent
->GetFont());
247 SetSize(x
, y
, width
, height
);
252 wxListBox::~wxListBox(void)
255 uint uiCount
= m_aItems
.Count();
256 while ( uiCount
-- != 0 ) {
257 delete m_aItems
[uiCount
];
262 void wxListBox::SetupColours(void)
264 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
265 SetForegroundColour(GetParent()->GetDefaultForegroundColour());
268 void wxListBox::SetFirstItem(const int N
)
270 SendMessage(hwnd
,LB_SETTOPINDEX
,(WPARAM
)N
,(LPARAM
)0) ;
273 void wxListBox::SetFirstItem(const wxString
& s
)
275 int N
= FindString(s
) ;
281 void wxListBox::Delete(const int N
)
283 SendMessage(hwnd
, LB_DELETESTRING
, N
, 0);
285 SetHorizontalExtent("");
288 void wxListBox::Append(const wxString
& item
)
290 int index
= ListBox_AddString(hwnd
, item
);
294 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
295 wxOwnerDrawn
*pNewItem
= CreateItem(-1); // dummy argument
296 pNewItem
->SetName(item
);
297 m_aItems
.Add(pNewItem
);
298 ListBox_SetItemData(hwnd
, index
, pNewItem
);
302 SetHorizontalExtent(item
);
305 void wxListBox::Append(const wxString
& item
, char *Client_data
)
307 int index
= ListBox_AddString(hwnd
, item
);
311 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
312 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
313 // in OnMeasure/OnDraw.
314 wxFAIL_MSG("Can't use client data with owner-drawn listboxes");
318 ListBox_SetItemData(hwnd
, index
, Client_data
);
320 SetHorizontalExtent(item
);
323 void wxListBox::Set(const int n
, const wxString
*choices
, char** clientData
)
325 ShowWindow(hwnd
, SW_HIDE
);
326 ListBox_ResetContent(hwnd
);
328 for (i
= 0; i
< n
; i
++)
330 ListBox_AddString(hwnd
, choices
[i
]);
332 ListBox_SetItemData(hwnd
, i
, clientData
[i
]);
337 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
338 // first delete old items
339 uint ui
= m_aItems
.Count();
340 while ( ui
-- != 0 ) {
345 // then create new ones
346 for (ui
= 0; ui
< (uint
)n
; ui
++) {
347 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
348 pNewItem
->SetName(choices
[ui
]);
349 m_aItems
.Add(pNewItem
);
350 ListBox_SetItemData(hwnd
, ui
, pNewItem
);
352 wxASSERT_MSG(clientData
[ui
] == NULL
,
353 "Can't use client data with owner-drawn listboxes");
358 SetHorizontalExtent("");
359 ShowWindow(hwnd
, SW_SHOW
);
362 int wxListBox::FindString(const wxString
& s
) const
364 int pos
= ListBox_FindStringExact(hwnd
, (WPARAM
)-1, s
);
371 void wxListBox::Clear(void)
373 ListBox_ResetContent(hwnd
);
376 ListBox_GetHorizontalExtent(hwnd
);
379 void wxListBox::SetSelection(const int N
, const bool select
)
381 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
382 SendMessage(hwnd
, LB_SETSEL
, select
, N
);
388 SendMessage(hwnd
, LB_SETCURSEL
, N1
, 0);
392 bool wxListBox::Selected(const int N
) const
394 return SendMessage(hwnd
, LB_GETSEL
, N
, 0) == 0 ? FALSE
: TRUE
;
397 void wxListBox::Deselect(const int N
)
399 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
400 SendMessage(hwnd
, LB_SETSEL
, FALSE
, N
);
403 char *wxListBox::GetClientData(const int N
) const
405 return (char *)SendMessage(hwnd
, LB_GETITEMDATA
, N
, 0);
408 void wxListBox::SetClientData(const int N
, char *Client_data
)
410 if ( ListBox_SetItemData(hwnd
, N
, Client_data
) == LB_ERR
)
411 wxLogDebug("LB_SETITEMDATA failed");
414 // Return number of selections and an array of selected integers
415 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
419 if ((m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
))
421 int no_sel
= ListBox_GetSelCount(hwnd
);
423 int *selections
= new int[no_sel
];
424 if ( ListBox_GetSelItems(hwnd
, no_sel
, selections
) == LB_ERR
) {
425 wxFAIL_MSG("This listbox can't have single-selection style!");
428 aSelections
.Alloc(no_sel
);
429 for ( int n
= 0; n
< no_sel
; n
++ )
430 aSelections
.Add(selections
[n
]);
432 delete [] selections
;
437 else // single-selection listbox
439 aSelections
.Add(ListBox_GetCurSel(hwnd
));
445 // Get single selection, for single choice list items
446 int wxListBox::GetSelection() const
448 wxCHECK_MSG( !(m_windowStyle
& wxLB_MULTIPLE
) &&
449 !(m_windowStyle
& wxLB_EXTENDED
),
451 "GetSelection() can't be used with multiple-selection "
452 "listboxes, use GetSelections() instead." );
454 return ListBox_GetCurSel(hwnd
);
457 // Find string for position
458 wxString
wxListBox::GetString(const int N
) const
460 if (N
< 0 || N
> m_noItems
)
463 int len
= (int)SendMessage(hwnd
, LB_GETTEXT
, N
, (LONG
)wxBuffer
);
465 return wxString(wxBuffer
);
468 void wxListBox::SetSize(const int x
, const int y
, const int width
, const int height
, const int sizeFlags
)
470 int currentX
, currentY
;
471 GetPosition(¤tX
, ¤tY
);
478 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
480 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
483 // If we're prepared to use the existing size, then...
484 if (width
== -1 && height
== -1 && ((sizeFlags
& wxSIZE_AUTO
) != wxSIZE_AUTO
))
489 int cx
; // button font dimensions
492 wxGetCharSize(GetHWND(), &cx
, &cy
,GetFont());
494 float control_width
, control_height
, control_x
, control_y
;
496 // Deal with default size (using -1 values)
498 w1
= DEFAULT_ITEM_WIDTH
;
501 h1
= DEFAULT_ITEM_HEIGHT
;
503 control_x
= (float)x1
;
504 control_y
= (float)y1
;
505 control_width
= (float)w1
;
506 control_height
= (float)h1
;
508 // Calculations may have made size too small
509 if (control_height
<= 0)
510 control_height
= (float)DEFAULT_ITEM_HEIGHT
;
512 if (control_width
<= 0)
513 control_width
= (float)DEFAULT_ITEM_WIDTH
;
515 // wxDebugMsg("About to set the listbox height to %d", (int)control_height);
516 MoveWindow(hwnd
, (int)control_x
, (int)control_y
,
517 (int)control_width
, (int)control_height
, TRUE
);
520 #if WXWIN_COMPATIBILITY
521 GetEventHandler()->OldOnSize(width, height);
523 wxSizeEvent event(wxSize(width, height), m_windowId);
524 event.eventObject = this;
525 GetEventHandler()->ProcessEvent(event);
531 // Windows-specific code to set the horizontal extent of
532 // the listbox, if necessary. If s is non-NULL, it's
533 // used to calculate the horizontal extent.
534 // Otherwise, all strings are used.
535 void wxListBox::SetHorizontalExtent(const wxString
& s
)
537 // Only necessary if we want a horizontal scrollbar
538 if (!(m_windowStyle
& wxHSCROLL
))
540 TEXTMETRIC lpTextMetric
;
544 int existingExtent
= (int)SendMessage(hwnd
, LB_GETHORIZONTALEXTENT
, 0, 0L);
545 HDC dc
= GetWindowDC(hwnd
);
547 if (GetFont() && GetFont()->GetResourceHandle())
548 oldFont
= ::SelectObject(dc
, (HFONT
) GetFont()->GetResourceHandle());
550 GetTextMetrics(dc
, &lpTextMetric
);
552 ::GetTextExtentPoint(dc
, (LPSTR
) (const char *)s
, s
.Length(), &extentXY
);
553 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
556 ::SelectObject(dc
, oldFont
);
559 if (extentX
> existingExtent
)
560 SendMessage(hwnd
, LB_SETHORIZONTALEXTENT
, LOWORD(extentX
), 0L);
565 int largestExtent
= 0;
566 HDC dc
= GetWindowDC(hwnd
);
568 if (GetFont() && GetFont()->GetResourceHandle())
569 oldFont
= ::SelectObject(dc
, (HFONT
) GetFont()->GetResourceHandle());
571 GetTextMetrics(dc
, &lpTextMetric
);
573 for (i
= 0; i
< m_noItems
; i
++)
575 int len
= (int)SendMessage(hwnd
, LB_GETTEXT
, i
, (LONG
)wxBuffer
);
578 ::GetTextExtentPoint(dc
, (LPSTR
)wxBuffer
, len
, &extentXY
);
579 int extentX
= (int)(extentXY
.cx
+ lpTextMetric
.tmAveCharWidth
);
580 if (extentX
> largestExtent
)
581 largestExtent
= extentX
;
584 ::SelectObject(dc
, oldFont
);
587 SendMessage(hwnd
, LB_SETHORIZONTALEXTENT
, LOWORD(largestExtent
), 0L);
592 wxListBox::InsertItems(const int nItems
, const wxString items
[], const int pos
)
595 for (i
= 0; i
< nItems
; i
++)
596 ListBox_InsertString(hwnd
, i
+ pos
, items
[i
]);
600 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
601 for ( i
= 0; i
< nItems
; i
++ ) {
602 wxOwnerDrawn
*pNewItem
= CreateItem((uint
)(pos
+ i
));
603 pNewItem
->SetName(items
[i
]);
604 m_aItems
.Insert(pNewItem
, (uint
)(pos
+ i
));
605 ListBox_SetItemData(hwnd
, i
, pNewItem
);
610 SetHorizontalExtent("");
613 void wxListBox::SetString(const int N
, const wxString
& s
)
615 int sel
= GetSelection();
617 char *oldData
= (char *)wxListBox::GetClientData(N
);
619 SendMessage(hwnd
, LB_DELETESTRING
, N
, 0);
622 if (N
== (m_noItems
- 1))
625 SendMessage(hwnd
, LB_INSERTSTRING
, newN
, (LPARAM
) (const char *)s
);
627 wxListBox::SetClientData(N
, oldData
);
629 // Selection may have changed
634 if ( m_windowStyle
& wxLB_OWNERDRAW
)
635 // update item's text
636 m_aItems
[N
]->SetName(s
);
637 #endif //USE_OWNER_DRAWN
640 int wxListBox::Number (void) const
645 // For single selection items only
646 wxString
wxListBox::GetStringSelection (void) const
648 int sel
= GetSelection ();
650 return this->GetString (sel
);
655 bool wxListBox::SetStringSelection (const wxString
& s
, const bool flag
)
657 int sel
= FindString (s
);
660 SetSelection (sel
, flag
);
667 // Is this the right thing? Won't setselection generate a command
668 // event too? No! It'll just generate a setselection event.
669 // But we still can't have this being called whenever a real command
670 // is generated, because it sets the selection, which will already
671 // have been done! (Unless we have an optional argument for calling
672 // by the actual window system, or a separate function, ProcessCommand)
673 void wxListBox::Command (wxCommandEvent
& event
)
675 if (event
.m_extraLong
)
676 SetSelection (event
.m_commandInt
);
679 Deselect (event
.m_commandInt
);
682 ProcessCommand (event
);
685 WXHBRUSH
wxListBox::OnCtlColor(const WXHDC pDC
, const WXHWND pWnd
, const WXUINT nCtlColor
,
686 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
691 HBRUSH hbrush
= Ctl3dCtlColorEx(message
, wParam
, lParam
);
692 return (WXHBRUSH
) hbrush
;
696 if (GetParent()->GetTransparentBackground())
697 SetBkMode((HDC
) pDC
, TRANSPARENT
);
699 SetBkMode((HDC
) pDC
, OPAQUE
);
701 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
702 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
704 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
706 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
707 // has a zero usage count.
708 backgroundBrush
->RealizeResource();
709 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
712 long wxListBox::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
723 case WM_QUERYDRAGICON:
727 case WM_RBUTTONDBLCLK:
730 case WM_MBUTTONDBLCLK:
733 // case WM_LBUTTONDBLCLK:
739 case WM_INITMENUPOPUP:
744 case WM_CHAR: // Always an ASCII character
749 case WM_CTLCOLORLISTBOX:
750 case WM_CTLCOLORMSGBOX:
751 case WM_CTLCOLORSCROLLBAR:
752 case WM_CTLCOLORSTATIC:
753 case WM_CTLCOLOREDIT:
754 case WM_SYSCOLORCHANGE:
758 case WM_QUERYENDSESSION:
760 case WM_GETMINMAXINFO:
762 return MSWDefWindowProc(nMsg, wParam, lParam );
765 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
773 // space beneath/above each row in pixels
774 // "standard" checklistbox use 1 here, some might prefer 2. 0 is ugly.
775 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
777 // the height is the same for all items
778 // ## should be changed for LBS_OWNERDRAWVARIABLE style listboxes
779 // NB: can't forward this to wxListBoxItem because LB_SETITEMDATA
780 // message is not yet sent when we get here!
781 bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT
*item
)
783 // only owner-drawn control should receive this message
784 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
786 MEASUREITEMSTRUCT
*pStruct
= (MEASUREITEMSTRUCT
*)item
;
789 dc
.SetHDC((WXHDC
)CreateIC("DISPLAY", NULL
, NULL
, 0));
790 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_ANSI_VAR_FONT
));
792 pStruct
->itemHeight
= dc
.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE
;
793 pStruct
->itemWidth
= dc
.GetCharWidth();
798 // forward the message to the appropriate item
799 bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
801 // only owner-drawn control should receive this message
802 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
804 DRAWITEMSTRUCT
*pStruct
= (DRAWITEMSTRUCT
*)item
;
805 wxListBoxItem
*pItem
= (wxListBoxItem
*)SendMessage(hwnd
, LB_GETITEMDATA
,
808 wxCHECK( (int)pItem
!= LB_ERR
, FALSE
);
811 dc
.SetHDC((WXHDC
)pStruct
->hDC
, FALSE
);
812 wxRect
rect(pStruct
->rcItem
.left
, pStruct
->rcItem
.top
,
813 pStruct
->rcItem
.right
- pStruct
->rcItem
.left
,
814 pStruct
->rcItem
.bottom
- pStruct
->rcItem
.top
);
816 return pItem
->OnDrawItem(dc
, rect
,
817 (wxOwnerDrawn::wxODAction
)pStruct
->itemAction
,
818 (wxOwnerDrawn::wxODStatus
)pStruct
->itemState
);