1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/listbox.cpp
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #include "wx/window.h"
16 #include "wx/os2/private.h"
19 #include "wx/listbox.h"
20 #include "wx/settings.h"
24 #include "wx/dcscreen.h"
26 #include "wx/scrolwin.h"
32 #include "wx/dynarray.h"
38 #include "wx/ownerdrw.h"
41 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
43 // ============================================================================
44 // list box item declaration and implementation
45 // ============================================================================
49 class wxListBoxItem
: public wxOwnerDrawn
52 wxListBoxItem(const wxString
& rsStr
= wxEmptyString
);
55 wxListBoxItem::wxListBoxItem(
63 // No bitmaps/checkmarks
66 } // end of wxListBoxItem::wxListBoxItem
68 wxOwnerDrawn
* wxListBox::CreateItem( size_t WXUNUSED(n
) )
70 return new wxListBoxItem();
71 } // end of wxListBox::CreateItem
73 #endif //USE_OWNER_DRAWN
75 // ============================================================================
76 // list box control implementation
77 // ============================================================================
80 wxListBox::wxListBox()
84 } // end of wxListBox::wxListBox
86 bool wxListBox::Create(
91 , const wxArrayString
& asChoices
93 , const wxValidator
& rValidator
94 , const wxString
& rsName
97 wxCArrayString
chs(asChoices
);
99 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
100 lStyle
, rValidator
, rsName
);
103 bool wxListBox::Create(
106 , const wxPoint
& rPos
107 , const wxSize
& rSize
109 , const wxString asChoices
[]
111 , const wxValidator
& rValidator
112 , const wxString
& rsName
121 SetValidator(rValidator
);
125 pParent
->AddChild(this);
127 wxSystemSettings vSettings
;
129 SetBackgroundColour(vSettings
.GetColour(wxSYS_COLOUR_WINDOW
));
130 SetForegroundColour(pParent
->GetForegroundColour());
132 m_windowId
= (vId
== -1) ? (int)NewControlId() : vId
;
136 int nWidth
= rSize
.x
;
137 int nHeight
= rSize
.y
;
139 m_windowStyle
= lStyle
;
143 if (m_windowStyle
& wxCLIP_SIBLINGS
)
144 lStyle
|= WS_CLIPSIBLINGS
;
145 if (m_windowStyle
& wxLB_MULTIPLE
)
146 lStyle
|= LS_MULTIPLESEL
;
147 else if (m_windowStyle
& wxLB_EXTENDED
)
148 lStyle
|= LS_EXTENDEDSEL
;
149 if (m_windowStyle
& wxLB_HSCROLL
)
150 lStyle
|= LS_HORZSCROLL
;
151 if (m_windowStyle
& wxLB_OWNERDRAW
)
152 lStyle
|= LS_OWNERDRAW
;
155 // Without this style, you get unexpected heights, so e.g. constraint layout
156 // doesn't work properly
158 lStyle
|= LS_NOADJUSTPOS
;
160 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
) // Parent
161 ,WC_LISTBOX
// Default Listbox class
162 ,"LISTBOX" // Control's name
163 ,lStyle
// Initial Style
164 ,0, 0, 0, 0 // Position and size
165 ,GetWinHwnd(pParent
) // Owner
167 ,(HMENU
)m_windowId
// Id
168 ,NULL
// Control Data
169 ,NULL
// Presentation Parameters
177 // Subclass again for purposes of dialog editing mode
183 for (lUi
= 0; lUi
< (LONG
)n
; lUi
++)
185 Append(asChoices
[lUi
]);
187 wxFont
* pTextFont
= new wxFont( 10
195 // Set standard wxWidgets colors for Listbox items and highlighting
199 vColour
.Set(wxString(wxT("WHITE")));
201 LONG lColor
= (LONG
)vColour
.GetPixel();
203 ::WinSetPresParam( m_hWnd
204 ,PP_HILITEFOREGROUNDCOLOR
208 vColour
.Set(wxString(wxT("NAVY")));
209 lColor
= (LONG
)vColour
.GetPixel();
210 ::WinSetPresParam( m_hWnd
211 ,PP_HILITEBACKGROUNDCOLOR
223 } // end of wxListBox::Create
225 wxListBox::~wxListBox()
227 #if wxUSE_OWNER_DRAWN
228 size_t lUiCount
= m_aItems
.Count();
230 while (lUiCount
-- != 0)
232 delete m_aItems
[lUiCount
];
234 #endif // wxUSE_OWNER_DRAWN
235 } // end of wxListBox::~wxListBox
237 void wxListBox::SetupColours()
239 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
240 SetForegroundColour(GetParent()->GetForegroundColour());
241 } // end of wxListBox::SetupColours
243 // ----------------------------------------------------------------------------
244 // implementation of wxListBoxBase methods
245 // ----------------------------------------------------------------------------
247 void wxListBox::DoSetFirstItem(
251 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
252 wxT("invalid index in wxListBox::SetFirstItem") );
254 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX
, MPFROMLONG(N
), (MPARAM
)0);
255 } // end of wxListBox::DoSetFirstItem
257 void wxListBox::Delete(
261 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
262 wxT("invalid index in wxListBox::Delete") );
264 #if wxUSE_OWNER_DRAWN
266 m_aItems
.RemoveAt(N
);
267 #else // !wxUSE_OWNER_DRAWN
268 if (HasClientObjectData())
270 delete GetClientObject(N
);
272 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
274 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)N
, (MPARAM
)0);
276 } // end of wxListBox::DoSetFirstItem
278 int wxListBox::DoAppend(
279 const wxString
& rsItem
285 if (m_windowStyle
& wxLB_SORT
)
286 lIndexType
= LIT_SORTASCENDING
;
288 lIndexType
= LIT_END
;
289 lIndex
= (long)::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)lIndexType
, (MPARAM
)rsItem
.c_str());
292 #if wxUSE_OWNER_DRAWN
293 if (m_windowStyle
& wxLB_OWNERDRAW
)
295 wxOwnerDrawn
* pNewItem
= CreateItem(lIndex
); // dummy argument
299 pNewItem
->SetName(rsItem
);
300 m_aItems
.Insert(pNewItem
, lIndex
);
301 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)lIndex
, MPFROMP(pNewItem
));
302 pNewItem
->SetFont(GetFont());
306 } // end of wxListBox::DoAppend
308 void wxListBox::DoSetItems(
309 const wxArrayString
& raChoices
310 , void** ppClientData
313 BOOL bHideAndShow
= IsShown();
319 ::WinShowWindow(GetHwnd(), FALSE
);
321 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
322 m_nNumItems
= raChoices
.GetCount();
323 for (i
= 0; i
< m_nNumItems
; i
++)
326 if (m_windowStyle
& wxLB_SORT
)
327 lIndexType
= LIT_SORTASCENDING
;
329 lIndexType
= LIT_END
;
330 ::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)lIndexType
, (MPARAM
)raChoices
[i
].c_str());
334 #if wxUSE_OWNER_DRAWN
335 wxASSERT_MSG(ppClientData
[i
] == NULL
,
336 wxT("Can't use client data with owner-drawn listboxes"));
337 #else // !wxUSE_OWNER_DRAWN
338 ::WinSendMsg(WinUtil_GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(lCount
), MPFROMP(ppClientData
[i
]));
339 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
343 #if wxUSE_OWNER_DRAWN
344 if ( m_windowStyle
& wxLB_OWNERDRAW
)
347 // First delete old items
349 WX_CLEAR_ARRAY(m_aItems
);
352 // Then create new ones
354 for (size_t ui
= 0; ui
< (size_t)m_nNumItems
; ui
++)
356 wxOwnerDrawn
* pNewItem
= CreateItem(ui
);
358 pNewItem
->SetName(raChoices
[ui
]);
359 m_aItems
.Add(pNewItem
);
360 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(ui
), MPFROMP(pNewItem
));
363 #endif // wxUSE_OWNER_DRAWN
364 ::WinShowWindow(GetHwnd(), TRUE
);
365 } // end of wxListBox::DoSetItems
367 void wxListBox::Clear()
369 #if wxUSE_OWNER_DRAWN
370 size_t lUiCount
= m_aItems
.Count();
372 while (lUiCount
-- != 0)
374 delete m_aItems
[lUiCount
];
378 #else // !wxUSE_OWNER_DRAWN
379 if (HasClientObjectData())
381 for (size_t n
= 0; n
< (size_t)m_lNumItems
; n
++)
383 delete GetClientObject(n
);
386 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
387 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
390 } // end of wxListBox::Clear
392 void wxListBox::DoSetSelection(
397 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
398 wxT("invalid index in wxListBox::SetSelection") );
399 ::WinSendMsg( GetHwnd()
404 if(m_windowStyle
& wxLB_OWNERDRAW
)
406 } // end of wxListBox::SetSelection
408 bool wxListBox::IsSelected(
412 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, false,
413 wxT("invalid index in wxListBox::Selected") );
417 if (GetWindowStyleFlag() & wxLB_EXTENDED
)
420 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
422 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)(N
- 1), (MPARAM
)0));
426 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
428 return (lItem
== (LONG
)N
&& lItem
!= LIT_NONE
);
429 } // end of wxListBox::IsSelected
431 wxClientData
* wxListBox::DoGetItemClientObject(
435 return (wxClientData
*)DoGetItemClientData(n
);
438 void* wxListBox::DoGetItemClientData(
442 wxCHECK_MSG( n
>= 0 && n
< m_nNumItems
, NULL
,
443 wxT("invalid index in wxListBox::GetClientData") );
445 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, MPFROMLONG(n
), (MPARAM
)0));
446 } // end of wxListBox::DoGetItemClientData
448 void wxListBox::DoSetItemClientObject(
450 , wxClientData
* pClientData
453 DoSetItemClientData( n
456 } // end of wxListBox::DoSetItemClientObject
458 void wxListBox::DoSetItemClientData(
463 wxCHECK_RET( n
>= 0 && n
< m_nNumItems
,
464 wxT("invalid index in wxListBox::SetClientData") );
466 #if wxUSE_OWNER_DRAWN
467 if ( m_windowStyle
& wxLB_OWNERDRAW
)
470 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
471 // in OnMeasure/OnDraw.
473 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
475 #endif // wxUSE_OWNER_DRAWN
477 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(n
), MPFROMP(pClientData
));
478 } // end of wxListBox::DoSetItemClientData
480 bool wxListBox::HasMultipleSelection() const
482 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
483 } // end of wxListBox::HasMultipleSelection
485 int wxListBox::GetSelections( wxArrayInt
& raSelections
) const
491 raSelections
.Empty();
492 if (HasMultipleSelection())
494 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
500 if (lItem
!= LIT_NONE
)
503 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
512 raSelections
.Alloc(nCount
);
513 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
520 raSelections
.Add((int)lItem
);
521 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
528 raSelections
.Add((int)lItem
);
533 else // single-selection listbox
535 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
541 raSelections
.Add((int)lItem
);
545 } // end of wxListBox::GetSelections
547 int wxListBox::GetSelection() const
549 wxCHECK_MSG( !HasMultipleSelection(),
551 wxT("GetSelection() can't be used with multiple-selection "
552 "listboxes, use GetSelections() instead.") );
554 return(LONGFROMMR(::WinSendMsg( GetHwnd()
560 } // end of wxListBox::GetSelection
562 wxString
wxListBox::GetString(
570 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, wxEmptyString
,
571 wxT("invalid index in wxListBox::GetClientData") );
573 lLen
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)N
, (MPARAM
)0));
574 zBuf
= new wxChar
[lLen
+ 1];
575 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)N
, (SHORT
)lLen
), (MPARAM
)zBuf
);
580 } // end of wxListBox::GetString
582 void wxListBox::DoInsertItems(
583 const wxArrayString
& asItems
587 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
588 wxT("invalid index in wxListBox::InsertItems") );
590 int nItems
= asItems
.GetCount();
592 for (int i
= 0; i
< nItems
; i
++)
594 int nIndex
= (int)::WinSendMsg( GetHwnd()
596 ,MPFROMLONG((LONG
)(i
+ nPos
))
597 ,(MPARAM
)asItems
[i
].c_str()
600 wxOwnerDrawn
* pNewItem
= CreateItem(nIndex
);
602 pNewItem
->SetName(asItems
[i
]);
603 pNewItem
->SetFont(GetFont());
604 m_aItems
.Insert(pNewItem
, nIndex
);
605 ::WinSendMsg( GetHwnd()
607 ,(MPARAM
)((LONG
)nIndex
)
610 m_nNumItems
+= nItems
;
612 } // end of wxListBox::DoInsertItems
614 void wxListBox::SetString(
616 , const wxString
& rsString
619 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
620 wxT("invalid index in wxListBox::SetString") );
623 // Remember the state of the item
625 bool bWasSelected
= IsSelected(N
);
626 void* pOldData
= NULL
;
627 wxClientData
* pOldObjData
= NULL
;
629 if (m_clientDataItemsType
== wxClientData_Void
)
630 pOldData
= GetClientData(N
);
631 else if (m_clientDataItemsType
== wxClientData_Object
)
632 pOldObjData
= GetClientObject(N
);
635 // Delete and recreate it
637 ::WinSendMsg( GetHwnd()
645 if (N
== m_nNumItems
- 1)
648 ::WinSendMsg( GetHwnd()
651 ,(MPARAM
)rsString
.c_str()
655 // Restore the client data
661 else if (pOldObjData
)
667 // We may have lost the selection
672 #if wxUSE_OWNER_DRAWN
673 if (m_windowStyle
& wxLB_OWNERDRAW
)
675 // Update item's text
677 m_aItems
[N
]->SetName(rsString
);
678 #endif //USE_OWNER_DRAWN
679 } // end of wxListBox::SetString
681 int wxListBox::GetCount() const
686 // ----------------------------------------------------------------------------
688 // ----------------------------------------------------------------------------
690 wxSize
wxListBox::DoGetBestSize() const
693 // Find the widest string
699 wxFont vFont
= (wxFont
)GetFont();
701 for (int i
= 0; i
< m_nNumItems
; i
++)
703 wxString
vStr(GetString(i
));
709 if (nLine
> nListbox
)
714 // Give it some reasonable default value if there are no strings in the
721 // The listbox should be slightly larger than the widest string
723 wxGetCharSize( GetHWND()
730 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * (wxMax(m_nNumItems
, 7));
732 return wxSize( nListbox
735 } // end of wxListBox::DoGetBestSize
737 // ----------------------------------------------------------------------------
739 // ----------------------------------------------------------------------------
741 bool wxListBox::OS2Command(
743 , WXWORD
WXUNUSED(wId
))
745 wxEventType eEvtType
;
747 if (uParam
== LN_SELECT
)
749 eEvtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
751 else if (uParam
== LN_ENTER
)
753 eEvtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
758 // Some event we're not interested in
762 wxCommandEvent
vEvent( eEvtType
766 vEvent
.SetEventObject(this);
768 wxArrayInt aSelections
;
770 int nCount
= GetSelections(aSelections
);
775 if (HasClientObjectData())
776 vEvent
.SetClientObject(GetClientObject(n
));
777 else if ( HasClientUntypedData() )
778 vEvent
.SetClientData(GetClientData(n
));
779 vEvent
.SetString(GetString(n
));
786 return GetEventHandler()->ProcessEvent(vEvent
);
787 } // end of wxListBox::OS2Command
789 // ----------------------------------------------------------------------------
790 // wxCheckListBox support
791 // ----------------------------------------------------------------------------
793 #if wxUSE_OWNER_DRAWN
799 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
801 long wxListBox::OS2OnMeasure(
802 WXMEASUREITEMSTRUCT
* pItem
806 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
808 POWNERITEM pMeasureStruct
= (POWNERITEM
)pItem
;
812 // Only owner-drawn control should receive this message
814 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
816 vDc
.SetFont(GetFont());
825 pMeasureStruct
->rclItem
.xRight
= (USHORT
)vWidth
;
826 pMeasureStruct
->rclItem
.xLeft
= 0;
827 pMeasureStruct
->rclItem
.yTop
= 0;
828 pMeasureStruct
->rclItem
.yBottom
= 0;
830 vHeight
= (wxCoord
)(vDc
.GetCharHeight() * 2.5);
831 pMeasureStruct
->rclItem
.yTop
= (USHORT
)vHeight
;
833 return long(MRFROM2SHORT((USHORT
)vHeight
, (USHORT
)vWidth
));
834 } // end of wxListBox::OS2OnMeasure
836 bool wxListBox::OS2OnDraw (
837 WXDRAWITEMSTRUCT
* pItem
840 POWNERITEM pDrawStruct
= (POWNERITEM
)pItem
;
841 LONG lItemID
= pDrawStruct
->idItem
;
846 // Only owner-drawn control should receive this message
848 wxCHECK(((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), false);
852 // The item may be -1 for an empty listbox
857 wxListBoxItem
* pData
= (wxListBoxItem
*)PVOIDFROMMR( ::WinSendMsg( GetHwnd()
859 ,MPFROMLONG(pDrawStruct
->idItem
)
864 wxCHECK(pData
, false );
867 wxPoint
pt1( pDrawStruct
->rclItem
.xLeft
, pDrawStruct
->rclItem
.yTop
);
868 wxPoint
pt2( pDrawStruct
->rclItem
.xRight
, pDrawStruct
->rclItem
.yBottom
);
869 wxRect
vRect( pt1
, pt2
);
871 vDc
.SetHPS(pDrawStruct
->hps
);
873 if (pDrawStruct
->fsAttribute
== pDrawStruct
->fsAttributeOld
)
876 // Entire Item needs to be redrawn (either it has reappeared from
877 // behind another window or is being displayed for the first time
879 eAction
= wxOwnerDrawn::wxODDrawAll
;
881 if (pDrawStruct
->fsAttribute
& MIA_HILITED
)
884 // If it is currently selected we let the system handle it
886 eStatus
|= wxOwnerDrawn::wxODSelected
;
888 if (pDrawStruct
->fsAttribute
& MIA_CHECKED
)
891 // If it is currently checked we draw our own
893 eStatus
|= wxOwnerDrawn::wxODChecked
;
894 pDrawStruct
->fsAttributeOld
= pDrawStruct
->fsAttribute
&= ~MIA_CHECKED
;
896 if (pDrawStruct
->fsAttribute
& MIA_DISABLED
)
899 // If it is currently disabled we let the system handle it
901 eStatus
|= wxOwnerDrawn::wxODDisabled
;
904 // Don't really care about framed (indicationg focus) or NoDismiss
909 if (pDrawStruct
->fsAttribute
& MIA_HILITED
)
911 eAction
= wxOwnerDrawn::wxODDrawAll
;
912 eStatus
|= wxOwnerDrawn::wxODSelected
;
914 // Keep the system from trying to highlight with its bogus colors
916 pDrawStruct
->fsAttributeOld
= pDrawStruct
->fsAttribute
&= ~MIA_HILITED
;
918 else if (!(pDrawStruct
->fsAttribute
& MIA_HILITED
))
920 eAction
= wxOwnerDrawn::wxODDrawAll
;
923 // Keep the system from trying to highlight with its bogus colors
925 pDrawStruct
->fsAttribute
= pDrawStruct
->fsAttributeOld
&= ~MIA_HILITED
;
930 // For now we don't care about anything else
931 // just ignore the entire message!
936 return pData
->OnDrawItem( vDc
938 ,(wxOwnerDrawn::wxODAction
)eAction
939 ,(wxOwnerDrawn::wxODStatus
)eStatus
941 } // end of wxListBox::OS2OnDraw
943 #endif // ndef for wxUSE_OWNER_DRAWN
945 #endif // ndef for wxUSE_LISTBOX