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"
17 #include "wx/listbox.h"
20 #include "wx/dynarray.h"
21 #include "wx/settings.h"
25 #include "wx/dcscreen.h"
27 #include "wx/scrolwin.h"
29 #include "wx/window.h"
32 #include "wx/os2/dcclient.h"
33 #include "wx/os2/private.h"
39 #include "wx/ownerdrw.h"
42 // ============================================================================
43 // list box item declaration and implementation
44 // ============================================================================
48 class wxListBoxItem
: public wxOwnerDrawn
51 wxListBoxItem(wxListBox
*parent
)
52 { m_parent
= parent
; }
54 wxListBox
*GetParent() const
58 { return m_parent
->GetItemIndex(const_cast<wxListBoxItem
*>(this)); }
60 wxString
GetName() const
61 { return m_parent
->GetString(GetIndex()); }
67 wxOwnerDrawn
* wxListBox::CreateItem( size_t WXUNUSED(n
) )
69 return new wxListBoxItem(this);
70 } // end of wxListBox::CreateItem
72 #endif //USE_OWNER_DRAWN
74 // ============================================================================
75 // list box control implementation
76 // ============================================================================
79 wxListBox::wxListBox()
83 } // end of wxListBox::wxListBox
85 bool wxListBox::Create(
90 , const wxArrayString
& asChoices
92 , const wxValidator
& rValidator
93 , const wxString
& rsName
96 wxCArrayString
chs(asChoices
);
98 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
99 lStyle
, rValidator
, rsName
);
102 bool wxListBox::Create( wxWindow
* pParent
,
107 const wxString asChoices
[],
109 const wxValidator
& rValidator
,
110 const wxString
& rsName
)
118 SetValidator(rValidator
);
122 pParent
->AddChild(this);
124 wxSystemSettings vSettings
;
126 SetBackgroundColour(vSettings
.GetColour(wxSYS_COLOUR_WINDOW
));
127 SetForegroundColour(pParent
->GetForegroundColour());
129 m_windowId
= (vId
== -1) ? (int)NewControlId() : vId
;
133 int nWidth
= rSize
.x
;
134 int nHeight
= rSize
.y
;
136 m_windowStyle
= lStyle
;
140 if (m_windowStyle
& wxCLIP_SIBLINGS
)
141 lStyle
|= WS_CLIPSIBLINGS
;
142 if (m_windowStyle
& wxLB_MULTIPLE
)
143 lStyle
|= LS_MULTIPLESEL
;
144 else if (m_windowStyle
& wxLB_EXTENDED
)
145 lStyle
|= LS_EXTENDEDSEL
;
146 if (m_windowStyle
& wxLB_HSCROLL
)
147 lStyle
|= LS_HORZSCROLL
;
148 if (m_windowStyle
& wxLB_OWNERDRAW
)
149 lStyle
|= LS_OWNERDRAW
;
152 // Without this style, you get unexpected heights, so e.g. constraint layout
153 // doesn't work properly
155 lStyle
|= LS_NOADJUSTPOS
;
157 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
) // Parent
158 ,WC_LISTBOX
// Default Listbox class
159 ,"LISTBOX" // Control's name
160 ,lStyle
// Initial Style
161 ,0, 0, 0, 0 // Position and size
162 ,GetWinHwnd(pParent
) // Owner
164 ,(HMENU
)m_windowId
// Id
165 ,NULL
// Control Data
166 ,NULL
// Presentation Parameters
174 // Subclass again for purposes of dialog editing mode
180 for (lUi
= 0; lUi
< (LONG
)n
; lUi
++)
182 Append(asChoices
[lUi
]);
184 wxFont
* pTextFont
= new wxFont( 10
192 // Set OS/2 system colours for Listbox items and highlighting
196 vColour
= wxSystemSettingsNative::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
198 LONG lColor
= (LONG
)vColour
.GetPixel();
200 ::WinSetPresParam( m_hWnd
201 ,PP_HILITEFOREGROUNDCOLOR
205 vColour
= wxSystemSettingsNative::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
206 lColor
= (LONG
)vColour
.GetPixel();
207 ::WinSetPresParam( m_hWnd
208 ,PP_HILITEBACKGROUNDCOLOR
222 } // end of wxListBox::Create
224 wxListBox::~wxListBox()
227 } // end of wxListBox::~wxListBox
229 void wxListBox::SetupColours()
231 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
232 SetForegroundColour(GetParent()->GetForegroundColour());
233 } // end of wxListBox::SetupColours
235 // ----------------------------------------------------------------------------
236 // implementation of wxListBoxBase methods
237 // ----------------------------------------------------------------------------
239 void wxListBox::DoSetFirstItem(int N
)
241 wxCHECK_RET( IsValid(N
),
242 wxT("invalid index in wxListBox::SetFirstItem") );
244 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX
, MPFROMLONG(N
), (MPARAM
)0);
245 } // end of wxListBox::DoSetFirstItem
247 void wxListBox::DoDeleteOneItem(unsigned int n
)
249 wxCHECK_RET( IsValid(n
),
250 wxT("invalid index in wxListBox::Delete") );
252 #if wxUSE_OWNER_DRAWN
254 m_aItems
.RemoveAt(n
);
255 #endif // wxUSE_OWNER_DRAWN
257 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
259 } // end of wxListBox::DoSetFirstItem
261 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
264 wxClientDataType type
)
267 bool incrementPos
= false;
270 lIndexType
= LIT_SORTASCENDING
;
271 else if (pos
== GetCount())
272 lIndexType
= LIT_END
;
275 lIndexType
= (LONG
)pos
;
281 unsigned int count
= items
.GetCount();
282 for (unsigned int i
= 0; i
< count
; i
++)
284 n
= (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)lIndexType
, (MPARAM
)items
[i
].wx_str());
287 wxLogLastError(wxT("WinSendMsg(LM_INSERTITEM)"));
293 #if wxUSE_OWNER_DRAWN
294 if (HasFlag(wxLB_OWNERDRAW
))
296 wxOwnerDrawn
* pNewItem
= CreateItem(n
); // dummy argument
297 pNewItem
->SetFont(GetFont());
298 m_aItems
.Insert(pNewItem
, n
);
301 AssignNewItemClientData(n
, clientData
, i
, type
);
308 } // end of wxListBox::DoInsertAppendItemsWithData
310 void wxListBox::DoClear()
312 #if wxUSE_OWNER_DRAWN
313 if ( m_windowStyle
& wxLB_OWNERDRAW
)
315 WX_CLEAR_ARRAY(m_aItems
);
317 #endif // wxUSE_OWNER_DRAWN
318 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
321 } // end of wxListBox::Clear
323 void wxListBox::DoSetSelection( int N
, bool bSelect
)
325 wxCHECK_RET( IsValid(N
),
326 wxT("invalid index in wxListBox::SetSelection") );
327 ::WinSendMsg( GetHwnd()
332 if(m_windowStyle
& wxLB_OWNERDRAW
)
334 } // end of wxListBox::SetSelection
336 bool wxListBox::IsSelected( int N
) const
338 wxCHECK_MSG( IsValid(N
), false,
339 wxT("invalid index in wxListBox::Selected") );
343 if (GetWindowStyleFlag() & wxLB_EXTENDED
)
346 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
348 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)(N
- 1), (MPARAM
)0));
352 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
354 return (lItem
== (LONG
)N
&& lItem
!= LIT_NONE
);
355 } // end of wxListBox::IsSelected
357 void* wxListBox::DoGetItemClientData(unsigned int n
) const
359 wxCHECK_MSG( IsValid(n
), NULL
,
360 wxT("invalid index in wxListBox::GetClientData") );
362 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, MPFROMLONG(n
), (MPARAM
)0));
363 } // end of wxListBox::DoGetItemClientData
365 void wxListBox::DoSetItemClientData(unsigned int n
, void* pClientData
)
367 wxCHECK_RET( IsValid(n
),
368 wxT("invalid index in wxListBox::SetClientData") );
370 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(n
), MPFROMP(pClientData
));
371 } // end of wxListBox::DoSetItemClientData
373 bool wxListBox::HasMultipleSelection() const
375 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
376 } // end of wxListBox::HasMultipleSelection
378 int wxListBox::GetSelections( wxArrayInt
& raSelections
) const
384 raSelections
.Empty();
385 if (HasMultipleSelection())
387 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
393 if (lItem
!= LIT_NONE
)
396 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
405 raSelections
.Alloc(nCount
);
406 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
413 raSelections
.Add((int)lItem
);
414 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
421 raSelections
.Add((int)lItem
);
426 else // single-selection listbox
428 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
434 raSelections
.Add((int)lItem
);
438 } // end of wxListBox::GetSelections
440 int wxListBox::GetSelection() const
442 wxCHECK_MSG( !HasMultipleSelection(),
444 wxT("GetSelection() can't be used with multiple-selection "
445 "listboxes, use GetSelections() instead.") );
447 return(LONGFROMMR(::WinSendMsg( GetHwnd()
453 } // end of wxListBox::GetSelection
455 wxString
wxListBox::GetString(unsigned int n
) const
461 wxCHECK_MSG( IsValid(n
), wxEmptyString
,
462 wxT("invalid index in wxListBox::GetClientData") );
464 lLen
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
465 zBuf
= new wxChar
[lLen
+ 1];
466 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)n
, (SHORT
)lLen
), (MPARAM
)zBuf
);
471 } // end of wxListBox::GetString
473 void wxListBox::SetString(unsigned int n
, const wxString
& rsString
)
475 wxCHECK_RET( IsValid(n
),
476 wxT("invalid index in wxListBox::SetString") );
479 // Remember the state of the item
481 bool bWasSelected
= IsSelected(n
);
482 void* pOldData
= NULL
;
483 wxClientData
* pOldObjData
= NULL
;
485 if ( HasClientUntypedData() )
486 pOldData
= GetClientData(n
);
487 else if ( HasClientObjectData() )
488 pOldObjData
= GetClientObject(n
);
491 // Delete and recreate it
493 ::WinSendMsg( GetHwnd()
501 if (n
== (m_nNumItems
- 1))
504 ::WinSendMsg( GetHwnd()
507 ,(MPARAM
)rsString
.wx_str()
511 // Restore the client data
514 SetClientData(n
, pOldData
);
515 else if (pOldObjData
)
516 SetClientObject(n
, pOldObjData
);
519 // We may have lost the selection
523 } // end of wxListBox::SetString
525 unsigned int wxListBox::GetCount() const
530 // ----------------------------------------------------------------------------
532 // ----------------------------------------------------------------------------
534 wxSize
wxListBox::DoGetBestSize() const
537 // Find the widest string
543 wxFont vFont
= (wxFont
)GetFont();
545 for (unsigned int i
= 0; i
< m_nNumItems
; i
++)
547 wxString
vStr(GetString(i
));
549 GetTextExtent( vStr
, &nLine
, NULL
);
550 if (nLine
> nListbox
)
555 // Give it some reasonable default value if there are no strings in the
562 // The listbox should be slightly larger than the widest string
564 wxGetCharSize( GetHWND()
571 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * (wxMax(m_nNumItems
, 7));
573 return wxSize( nListbox
576 } // end of wxListBox::DoGetBestSize
578 // ----------------------------------------------------------------------------
580 // ----------------------------------------------------------------------------
582 bool wxListBox::OS2Command(
584 , WXWORD
WXUNUSED(wId
))
586 wxEventType eEvtType
;
588 if (uParam
== LN_SELECT
)
590 eEvtType
= wxEVT_LISTBOX
;
592 else if (uParam
== LN_ENTER
)
594 eEvtType
= wxEVT_LISTBOX_DCLICK
;
599 // Some event we're not interested in
603 wxCommandEvent
vEvent( eEvtType
607 vEvent
.SetEventObject(this);
609 wxArrayInt aSelections
;
611 int nCount
= GetSelections(aSelections
);
616 if (HasClientObjectData())
617 vEvent
.SetClientObject(GetClientObject(n
));
618 else if ( HasClientUntypedData() )
619 vEvent
.SetClientData(GetClientData(n
));
620 vEvent
.SetString(GetString(n
));
627 return HandleWindowEvent(vEvent
);
628 } // end of wxListBox::OS2Command
630 // ----------------------------------------------------------------------------
631 // wxCheckListBox support
632 // ----------------------------------------------------------------------------
634 #if wxUSE_OWNER_DRAWN
640 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
642 long wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
* pItem
)
645 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
647 POWNERITEM pMeasureStruct
= (POWNERITEM
)pItem
;
651 // Only owner-drawn control should receive this message
653 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
655 vDc
.SetFont(GetFont());
664 pMeasureStruct
->rclItem
.xRight
= (USHORT
)vWidth
;
665 pMeasureStruct
->rclItem
.xLeft
= 0;
666 pMeasureStruct
->rclItem
.yTop
= 0;
667 pMeasureStruct
->rclItem
.yBottom
= 0;
669 vHeight
= (wxCoord
)(vDc
.GetCharHeight() * 2.5);
670 pMeasureStruct
->rclItem
.yTop
= (USHORT
)vHeight
;
672 return long(MRFROM2SHORT((USHORT
)vHeight
, (USHORT
)vWidth
));
673 } // end of wxListBox::OS2OnMeasure
675 bool wxListBox::OS2OnDraw (
676 WXDRAWITEMSTRUCT
* pItem
679 POWNERITEM pDrawStruct
= (POWNERITEM
)pItem
;
684 // Only owner-drawn control should receive this message
686 wxCHECK(((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), false);
690 // The item may be -1 for an empty listbox
692 if (pDrawStruct
->idItem
== -1L)
695 wxListBoxItem
* pData
= (wxListBoxItem
*)m_aItems
[pDrawStruct
->idItem
];
697 wxClientDC
vDc(this);
698 wxPMDCImpl
*impl
= (wxPMDCImpl
*) vDc
.GetImpl();
699 wxPoint
pt1( pDrawStruct
->rclItem
.xLeft
, pDrawStruct
->rclItem
.yTop
);
700 wxPoint
pt2( pDrawStruct
->rclItem
.xRight
, pDrawStruct
->rclItem
.yBottom
);
701 wxRect
vRect( pt1
, pt2
);
703 impl
->SetHPS(pDrawStruct
->hps
);
705 if (pDrawStruct
->fsAttribute
== pDrawStruct
->fsAttributeOld
)
708 // Entire Item needs to be redrawn (either it has reappeared from
709 // behind another window or is being displayed for the first time
711 eAction
= wxOwnerDrawn::wxODDrawAll
;
713 if (pDrawStruct
->fsAttribute
& MIA_HILITED
)
716 // If it is currently selected we let the system handle it
718 eStatus
|= wxOwnerDrawn::wxODSelected
;
720 if (pDrawStruct
->fsAttribute
& MIA_CHECKED
)
723 // If it is currently checked we draw our own
725 eStatus
|= wxOwnerDrawn::wxODChecked
;
726 pDrawStruct
->fsAttributeOld
= pDrawStruct
->fsAttribute
&= ~MIA_CHECKED
;
728 if (pDrawStruct
->fsAttribute
& MIA_DISABLED
)
731 // If it is currently disabled we let the system handle it
733 eStatus
|= wxOwnerDrawn::wxODDisabled
;
736 // Don't really care about framed (indicationg focus) or NoDismiss
741 if (pDrawStruct
->fsAttribute
& MIA_HILITED
)
743 eAction
= wxOwnerDrawn::wxODDrawAll
;
744 eStatus
|= wxOwnerDrawn::wxODSelected
;
746 // Keep the system from trying to highlight with its bogus colors
748 pDrawStruct
->fsAttributeOld
= pDrawStruct
->fsAttribute
&= ~MIA_HILITED
;
750 else if (!(pDrawStruct
->fsAttribute
& MIA_HILITED
))
752 eAction
= wxOwnerDrawn::wxODDrawAll
;
755 // Keep the system from trying to highlight with its bogus colors
757 pDrawStruct
->fsAttribute
= pDrawStruct
->fsAttributeOld
&= ~MIA_HILITED
;
762 // For now we don't care about anything else
763 // just ignore the entire message!
768 return pData
->OnDrawItem( vDc
770 ,(wxOwnerDrawn::wxODAction
)eAction
771 ,(wxOwnerDrawn::wxODStatus
)(eStatus
| wxOwnerDrawn::wxODHidePrefix
)
773 } // end of wxListBox::OS2OnDraw
775 #endif // ndef for wxUSE_OWNER_DRAWN
777 #endif // wxUSE_LISTBOX