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 OS/2 system colours for Listbox items and highlighting
199 vColour
= wxSystemSettingsNative::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
201 LONG lColor
= (LONG
)vColour
.GetPixel();
203 ::WinSetPresParam( m_hWnd
204 ,PP_HILITEFOREGROUNDCOLOR
208 vColour
= wxSystemSettingsNative::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
209 lColor
= (LONG
)vColour
.GetPixel();
210 ::WinSetPresParam( m_hWnd
211 ,PP_HILITEBACKGROUNDCOLOR
225 } // end of wxListBox::Create
227 wxListBox::~wxListBox()
229 #if wxUSE_OWNER_DRAWN
230 size_t lUiCount
= m_aItems
.Count();
232 while (lUiCount
-- != 0)
234 delete m_aItems
[lUiCount
];
236 #endif // wxUSE_OWNER_DRAWN
237 } // end of wxListBox::~wxListBox
239 void wxListBox::SetupColours()
241 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
242 SetForegroundColour(GetParent()->GetForegroundColour());
243 } // end of wxListBox::SetupColours
245 // ----------------------------------------------------------------------------
246 // implementation of wxListBoxBase methods
247 // ----------------------------------------------------------------------------
249 void wxListBox::DoSetFirstItem(
253 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
254 wxT("invalid index in wxListBox::SetFirstItem") );
256 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX
, MPFROMLONG(N
), (MPARAM
)0);
257 } // end of wxListBox::DoSetFirstItem
259 void wxListBox::Delete(
263 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
264 wxT("invalid index in wxListBox::Delete") );
266 #if wxUSE_OWNER_DRAWN
268 m_aItems
.RemoveAt(N
);
269 #else // !wxUSE_OWNER_DRAWN
270 if (HasClientObjectData())
272 delete GetClientObject(N
);
274 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
276 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)N
, (MPARAM
)0);
278 } // end of wxListBox::DoSetFirstItem
280 int wxListBox::DoAppend(
281 const wxString
& rsItem
287 if (m_windowStyle
& wxLB_SORT
)
288 lIndexType
= LIT_SORTASCENDING
;
290 lIndexType
= LIT_END
;
291 lIndex
= (long)::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)lIndexType
, (MPARAM
)rsItem
.c_str());
294 #if wxUSE_OWNER_DRAWN
295 if (m_windowStyle
& wxLB_OWNERDRAW
)
297 wxOwnerDrawn
* pNewItem
= CreateItem(lIndex
); // dummy argument
301 pNewItem
->SetName(rsItem
);
302 m_aItems
.Insert(pNewItem
, lIndex
);
303 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)lIndex
, MPFROMP(pNewItem
));
304 pNewItem
->SetFont(GetFont());
308 } // end of wxListBox::DoAppend
310 void wxListBox::DoSetItems(
311 const wxArrayString
& raChoices
312 , void** ppClientData
315 BOOL bHideAndShow
= IsShown();
321 ::WinShowWindow(GetHwnd(), FALSE
);
323 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
324 m_nNumItems
= raChoices
.GetCount();
325 for (i
= 0; i
< m_nNumItems
; i
++)
328 if (m_windowStyle
& wxLB_SORT
)
329 lIndexType
= LIT_SORTASCENDING
;
331 lIndexType
= LIT_END
;
332 ::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)lIndexType
, (MPARAM
)raChoices
[i
].c_str());
336 #if wxUSE_OWNER_DRAWN
337 wxASSERT_MSG(ppClientData
[i
] == NULL
,
338 wxT("Can't use client data with owner-drawn listboxes"));
339 #else // !wxUSE_OWNER_DRAWN
340 ::WinSendMsg(WinUtil_GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(lCount
), MPFROMP(ppClientData
[i
]));
341 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
345 #if wxUSE_OWNER_DRAWN
346 if ( m_windowStyle
& wxLB_OWNERDRAW
)
349 // First delete old items
351 WX_CLEAR_ARRAY(m_aItems
);
354 // Then create new ones
356 for (size_t ui
= 0; ui
< (size_t)m_nNumItems
; ui
++)
358 wxOwnerDrawn
* pNewItem
= CreateItem(ui
);
360 pNewItem
->SetName(raChoices
[ui
]);
361 m_aItems
.Add(pNewItem
);
362 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(ui
), MPFROMP(pNewItem
));
365 #endif // wxUSE_OWNER_DRAWN
366 ::WinShowWindow(GetHwnd(), TRUE
);
367 } // end of wxListBox::DoSetItems
369 void wxListBox::Clear()
371 #if wxUSE_OWNER_DRAWN
372 size_t lUiCount
= m_aItems
.Count();
374 while (lUiCount
-- != 0)
376 delete m_aItems
[lUiCount
];
380 #else // !wxUSE_OWNER_DRAWN
381 if (HasClientObjectData())
383 for (size_t n
= 0; n
< (size_t)m_lNumItems
; n
++)
385 delete GetClientObject(n
);
388 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
389 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
392 } // end of wxListBox::Clear
394 void wxListBox::DoSetSelection(
399 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
400 wxT("invalid index in wxListBox::SetSelection") );
401 ::WinSendMsg( GetHwnd()
406 if(m_windowStyle
& wxLB_OWNERDRAW
)
408 } // end of wxListBox::SetSelection
410 bool wxListBox::IsSelected(
414 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, false,
415 wxT("invalid index in wxListBox::Selected") );
419 if (GetWindowStyleFlag() & wxLB_EXTENDED
)
422 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
424 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)(N
- 1), (MPARAM
)0));
428 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
430 return (lItem
== (LONG
)N
&& lItem
!= LIT_NONE
);
431 } // end of wxListBox::IsSelected
433 wxClientData
* wxListBox::DoGetItemClientObject(
437 return (wxClientData
*)DoGetItemClientData(n
);
440 void* wxListBox::DoGetItemClientData(
444 wxCHECK_MSG( n
>= 0 && n
< m_nNumItems
, NULL
,
445 wxT("invalid index in wxListBox::GetClientData") );
447 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, MPFROMLONG(n
), (MPARAM
)0));
448 } // end of wxListBox::DoGetItemClientData
450 void wxListBox::DoSetItemClientObject(
452 , wxClientData
* pClientData
455 DoSetItemClientData( n
458 } // end of wxListBox::DoSetItemClientObject
460 void wxListBox::DoSetItemClientData(
465 wxCHECK_RET( n
>= 0 && n
< m_nNumItems
,
466 wxT("invalid index in wxListBox::SetClientData") );
468 #if wxUSE_OWNER_DRAWN
469 if ( m_windowStyle
& wxLB_OWNERDRAW
)
472 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
473 // in OnMeasure/OnDraw.
475 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
477 #endif // wxUSE_OWNER_DRAWN
479 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(n
), MPFROMP(pClientData
));
480 } // end of wxListBox::DoSetItemClientData
482 bool wxListBox::HasMultipleSelection() const
484 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
485 } // end of wxListBox::HasMultipleSelection
487 int wxListBox::GetSelections( wxArrayInt
& raSelections
) const
493 raSelections
.Empty();
494 if (HasMultipleSelection())
496 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
502 if (lItem
!= LIT_NONE
)
505 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
514 raSelections
.Alloc(nCount
);
515 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
522 raSelections
.Add((int)lItem
);
523 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
530 raSelections
.Add((int)lItem
);
535 else // single-selection listbox
537 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
543 raSelections
.Add((int)lItem
);
547 } // end of wxListBox::GetSelections
549 int wxListBox::GetSelection() const
551 wxCHECK_MSG( !HasMultipleSelection(),
553 wxT("GetSelection() can't be used with multiple-selection "
554 "listboxes, use GetSelections() instead.") );
556 return(LONGFROMMR(::WinSendMsg( GetHwnd()
562 } // end of wxListBox::GetSelection
564 wxString
wxListBox::GetString(
572 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, wxEmptyString
,
573 wxT("invalid index in wxListBox::GetClientData") );
575 lLen
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)N
, (MPARAM
)0));
576 zBuf
= new wxChar
[lLen
+ 1];
577 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)N
, (SHORT
)lLen
), (MPARAM
)zBuf
);
582 } // end of wxListBox::GetString
584 void wxListBox::DoInsertItems(
585 const wxArrayString
& asItems
589 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
590 wxT("invalid index in wxListBox::InsertItems") );
592 int nItems
= asItems
.GetCount();
594 for (int i
= 0; i
< nItems
; i
++)
596 int nIndex
= (int)::WinSendMsg( GetHwnd()
598 ,MPFROMLONG((LONG
)(i
+ nPos
))
599 ,(MPARAM
)asItems
[i
].c_str()
602 wxOwnerDrawn
* pNewItem
= CreateItem(nIndex
);
604 pNewItem
->SetName(asItems
[i
]);
605 pNewItem
->SetFont(GetFont());
606 m_aItems
.Insert(pNewItem
, nIndex
);
607 ::WinSendMsg( GetHwnd()
609 ,(MPARAM
)((LONG
)nIndex
)
612 m_nNumItems
+= nItems
;
614 } // end of wxListBox::DoInsertItems
616 void wxListBox::SetString(
618 , const wxString
& rsString
621 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
622 wxT("invalid index in wxListBox::SetString") );
625 // Remember the state of the item
627 bool bWasSelected
= IsSelected(N
);
628 void* pOldData
= NULL
;
629 wxClientData
* pOldObjData
= NULL
;
631 if (m_clientDataItemsType
== wxClientData_Void
)
632 pOldData
= GetClientData(N
);
633 else if (m_clientDataItemsType
== wxClientData_Object
)
634 pOldObjData
= GetClientObject(N
);
637 // Delete and recreate it
639 ::WinSendMsg( GetHwnd()
647 if (N
== m_nNumItems
- 1)
650 ::WinSendMsg( GetHwnd()
653 ,(MPARAM
)rsString
.c_str()
657 // Restore the client data
663 else if (pOldObjData
)
669 // We may have lost the selection
674 #if wxUSE_OWNER_DRAWN
675 if (m_windowStyle
& wxLB_OWNERDRAW
)
677 // Update item's text
679 m_aItems
[N
]->SetName(rsString
);
680 #endif //USE_OWNER_DRAWN
681 } // end of wxListBox::SetString
683 int wxListBox::GetCount() const
688 // ----------------------------------------------------------------------------
690 // ----------------------------------------------------------------------------
692 wxSize
wxListBox::DoGetBestSize() const
695 // Find the widest string
701 wxFont vFont
= (wxFont
)GetFont();
703 for (int i
= 0; i
< m_nNumItems
; i
++)
705 wxString
vStr(GetString(i
));
711 if (nLine
> nListbox
)
716 // Give it some reasonable default value if there are no strings in the
723 // The listbox should be slightly larger than the widest string
725 wxGetCharSize( GetHWND()
732 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * (wxMax(m_nNumItems
, 7));
734 return wxSize( nListbox
737 } // end of wxListBox::DoGetBestSize
739 // ----------------------------------------------------------------------------
741 // ----------------------------------------------------------------------------
743 bool wxListBox::OS2Command(
745 , WXWORD
WXUNUSED(wId
))
747 wxEventType eEvtType
;
749 if (uParam
== LN_SELECT
)
751 eEvtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
753 else if (uParam
== LN_ENTER
)
755 eEvtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
760 // Some event we're not interested in
764 wxCommandEvent
vEvent( eEvtType
768 vEvent
.SetEventObject(this);
770 wxArrayInt aSelections
;
772 int nCount
= GetSelections(aSelections
);
777 if (HasClientObjectData())
778 vEvent
.SetClientObject(GetClientObject(n
));
779 else if ( HasClientUntypedData() )
780 vEvent
.SetClientData(GetClientData(n
));
781 vEvent
.SetString(GetString(n
));
788 return GetEventHandler()->ProcessEvent(vEvent
);
789 } // end of wxListBox::OS2Command
791 // ----------------------------------------------------------------------------
792 // wxCheckListBox support
793 // ----------------------------------------------------------------------------
795 #if wxUSE_OWNER_DRAWN
801 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
803 long wxListBox::OS2OnMeasure(
804 WXMEASUREITEMSTRUCT
* pItem
808 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
810 POWNERITEM pMeasureStruct
= (POWNERITEM
)pItem
;
814 // Only owner-drawn control should receive this message
816 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
818 vDc
.SetFont(GetFont());
827 pMeasureStruct
->rclItem
.xRight
= (USHORT
)vWidth
;
828 pMeasureStruct
->rclItem
.xLeft
= 0;
829 pMeasureStruct
->rclItem
.yTop
= 0;
830 pMeasureStruct
->rclItem
.yBottom
= 0;
832 vHeight
= (wxCoord
)(vDc
.GetCharHeight() * 2.5);
833 pMeasureStruct
->rclItem
.yTop
= (USHORT
)vHeight
;
835 return long(MRFROM2SHORT((USHORT
)vHeight
, (USHORT
)vWidth
));
836 } // end of wxListBox::OS2OnMeasure
838 bool wxListBox::OS2OnDraw (
839 WXDRAWITEMSTRUCT
* pItem
842 POWNERITEM pDrawStruct
= (POWNERITEM
)pItem
;
843 LONG lItemID
= pDrawStruct
->idItem
;
848 // Only owner-drawn control should receive this message
850 wxCHECK(((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), false);
854 // The item may be -1 for an empty listbox
859 wxListBoxItem
* pData
= (wxListBoxItem
*)PVOIDFROMMR( ::WinSendMsg( GetHwnd()
861 ,MPFROMLONG(pDrawStruct
->idItem
)
866 wxCHECK(pData
, false );
869 wxPoint
pt1( pDrawStruct
->rclItem
.xLeft
, pDrawStruct
->rclItem
.yTop
);
870 wxPoint
pt2( pDrawStruct
->rclItem
.xRight
, pDrawStruct
->rclItem
.yBottom
);
871 wxRect
vRect( pt1
, pt2
);
873 vDc
.SetHPS(pDrawStruct
->hps
);
875 if (pDrawStruct
->fsAttribute
== pDrawStruct
->fsAttributeOld
)
878 // Entire Item needs to be redrawn (either it has reappeared from
879 // behind another window or is being displayed for the first time
881 eAction
= wxOwnerDrawn::wxODDrawAll
;
883 if (pDrawStruct
->fsAttribute
& MIA_HILITED
)
886 // If it is currently selected we let the system handle it
888 eStatus
|= wxOwnerDrawn::wxODSelected
;
890 if (pDrawStruct
->fsAttribute
& MIA_CHECKED
)
893 // If it is currently checked we draw our own
895 eStatus
|= wxOwnerDrawn::wxODChecked
;
896 pDrawStruct
->fsAttributeOld
= pDrawStruct
->fsAttribute
&= ~MIA_CHECKED
;
898 if (pDrawStruct
->fsAttribute
& MIA_DISABLED
)
901 // If it is currently disabled we let the system handle it
903 eStatus
|= wxOwnerDrawn::wxODDisabled
;
906 // Don't really care about framed (indicationg focus) or NoDismiss
911 if (pDrawStruct
->fsAttribute
& MIA_HILITED
)
913 eAction
= wxOwnerDrawn::wxODDrawAll
;
914 eStatus
|= wxOwnerDrawn::wxODSelected
;
916 // Keep the system from trying to highlight with its bogus colors
918 pDrawStruct
->fsAttributeOld
= pDrawStruct
->fsAttribute
&= ~MIA_HILITED
;
920 else if (!(pDrawStruct
->fsAttribute
& MIA_HILITED
))
922 eAction
= wxOwnerDrawn::wxODDrawAll
;
925 // Keep the system from trying to highlight with its bogus colors
927 pDrawStruct
->fsAttribute
= pDrawStruct
->fsAttributeOld
&= ~MIA_HILITED
;
932 // For now we don't care about anything else
933 // just ignore the entire message!
938 return pData
->OnDrawItem( vDc
940 ,(wxOwnerDrawn::wxODAction
)eAction
941 ,(wxOwnerDrawn::wxODStatus
)eStatus
943 } // end of wxListBox::OS2OnDraw
945 #endif // ndef for wxUSE_OWNER_DRAWN
947 #endif // ndef for wxUSE_LISTBOX