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 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
44 // ============================================================================
45 // list box item declaration and implementation
46 // ============================================================================
50 class wxListBoxItem
: public wxOwnerDrawn
53 wxListBoxItem(wxListBox
*parent
)
54 { m_parent
= parent
; }
56 wxListBox
*GetParent() const
60 { return m_parent
->GetItemIndex(const_cast<wxListBoxItem
*>(this)); }
62 wxString
GetName() const
63 { return m_parent
->GetString(GetIndex()); }
69 wxOwnerDrawn
* wxListBox::CreateItem( size_t WXUNUSED(n
) )
71 return new wxListBoxItem(this);
72 } // end of wxListBox::CreateItem
74 #endif //USE_OWNER_DRAWN
76 // ============================================================================
77 // list box control implementation
78 // ============================================================================
81 wxListBox::wxListBox()
85 } // end of wxListBox::wxListBox
87 bool wxListBox::Create(
92 , const wxArrayString
& asChoices
94 , const wxValidator
& rValidator
95 , const wxString
& rsName
98 wxCArrayString
chs(asChoices
);
100 return Create(pParent
, vId
, rPos
, rSize
, chs
.GetCount(), chs
.GetStrings(),
101 lStyle
, rValidator
, rsName
);
104 bool wxListBox::Create( wxWindow
* pParent
,
109 const wxString asChoices
[],
111 const wxValidator
& rValidator
,
112 const wxString
& rsName
)
120 SetValidator(rValidator
);
124 pParent
->AddChild(this);
126 wxSystemSettings vSettings
;
128 SetBackgroundColour(vSettings
.GetColour(wxSYS_COLOUR_WINDOW
));
129 SetForegroundColour(pParent
->GetForegroundColour());
131 m_windowId
= (vId
== -1) ? (int)NewControlId() : vId
;
135 int nWidth
= rSize
.x
;
136 int nHeight
= rSize
.y
;
138 m_windowStyle
= lStyle
;
142 if (m_windowStyle
& wxCLIP_SIBLINGS
)
143 lStyle
|= WS_CLIPSIBLINGS
;
144 if (m_windowStyle
& wxLB_MULTIPLE
)
145 lStyle
|= LS_MULTIPLESEL
;
146 else if (m_windowStyle
& wxLB_EXTENDED
)
147 lStyle
|= LS_EXTENDEDSEL
;
148 if (m_windowStyle
& wxLB_HSCROLL
)
149 lStyle
|= LS_HORZSCROLL
;
150 if (m_windowStyle
& wxLB_OWNERDRAW
)
151 lStyle
|= LS_OWNERDRAW
;
154 // Without this style, you get unexpected heights, so e.g. constraint layout
155 // doesn't work properly
157 lStyle
|= LS_NOADJUSTPOS
;
159 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
) // Parent
160 ,WC_LISTBOX
// Default Listbox class
161 ,"LISTBOX" // Control's name
162 ,lStyle
// Initial Style
163 ,0, 0, 0, 0 // Position and size
164 ,GetWinHwnd(pParent
) // Owner
166 ,(HMENU
)m_windowId
// Id
167 ,NULL
// Control Data
168 ,NULL
// Presentation Parameters
176 // Subclass again for purposes of dialog editing mode
182 for (lUi
= 0; lUi
< (LONG
)n
; lUi
++)
184 Append(asChoices
[lUi
]);
186 wxFont
* pTextFont
= new wxFont( 10
194 // Set OS/2 system colours for Listbox items and highlighting
198 vColour
= wxSystemSettingsNative::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
200 LONG lColor
= (LONG
)vColour
.GetPixel();
202 ::WinSetPresParam( m_hWnd
203 ,PP_HILITEFOREGROUNDCOLOR
207 vColour
= wxSystemSettingsNative::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
208 lColor
= (LONG
)vColour
.GetPixel();
209 ::WinSetPresParam( m_hWnd
210 ,PP_HILITEBACKGROUNDCOLOR
224 } // end of wxListBox::Create
226 wxListBox::~wxListBox()
229 } // end of wxListBox::~wxListBox
231 void wxListBox::SetupColours()
233 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
234 SetForegroundColour(GetParent()->GetForegroundColour());
235 } // end of wxListBox::SetupColours
237 // ----------------------------------------------------------------------------
238 // implementation of wxListBoxBase methods
239 // ----------------------------------------------------------------------------
241 void wxListBox::DoSetFirstItem(int N
)
243 wxCHECK_RET( IsValid(N
),
244 wxT("invalid index in wxListBox::SetFirstItem") );
246 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX
, MPFROMLONG(N
), (MPARAM
)0);
247 } // end of wxListBox::DoSetFirstItem
249 void wxListBox::DoDeleteOneItem(unsigned int n
)
251 wxCHECK_RET( IsValid(n
),
252 wxT("invalid index in wxListBox::Delete") );
254 #if wxUSE_OWNER_DRAWN
256 m_aItems
.RemoveAt(n
);
257 #endif // wxUSE_OWNER_DRAWN
259 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)n
, (MPARAM
)0);
261 } // end of wxListBox::DoSetFirstItem
263 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
266 wxClientDataType type
)
270 bool incrementPos
= false;
273 lIndexType
= LIT_SORTASCENDING
;
274 else if (pos
== GetCount())
275 lIndexType
= LIT_END
;
278 lIndexType
= (LONG
)pos
;
284 unsigned int count
= items
.GetCount();
285 for (unsigned int i
= 0; i
< count
; i
++)
287 n
= (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)lIndexType
, (MPARAM
)items
[i
].wx_str());
290 wxLogLastError(wxT("WinSendMsg(LM_INSERTITEM)"));
296 #if wxUSE_OWNER_DRAWN
297 if (HasFlag(wxLB_OWNERDRAW
))
299 wxOwnerDrawn
* pNewItem
= CreateItem(n
); // dummy argument
300 pNewItem
->SetFont(GetFont());
301 m_aItems
.Insert(pNewItem
, n
);
304 AssignNewItemClientData(n
, clientData
, i
, type
);
311 } // end of wxListBox::DoInsertAppendItemsWithData
313 void wxListBox::DoClear()
315 #if wxUSE_OWNER_DRAWN
316 if ( m_windowStyle
& wxLB_OWNERDRAW
)
318 WX_CLEAR_ARRAY(m_aItems
);
320 #endif // wxUSE_OWNER_DRAWN
321 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
324 } // end of wxListBox::Clear
326 void wxListBox::DoSetSelection( int N
, bool bSelect
)
328 wxCHECK_RET( IsValid(N
),
329 wxT("invalid index in wxListBox::SetSelection") );
330 ::WinSendMsg( GetHwnd()
335 if(m_windowStyle
& wxLB_OWNERDRAW
)
337 } // end of wxListBox::SetSelection
339 bool wxListBox::IsSelected( int N
) const
341 wxCHECK_MSG( IsValid(N
), false,
342 wxT("invalid index in wxListBox::Selected") );
346 if (GetWindowStyleFlag() & wxLB_EXTENDED
)
349 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
351 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)(N
- 1), (MPARAM
)0));
355 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
357 return (lItem
== (LONG
)N
&& lItem
!= LIT_NONE
);
358 } // end of wxListBox::IsSelected
360 void* wxListBox::DoGetItemClientData(unsigned int n
) const
362 wxCHECK_MSG( IsValid(n
), NULL
,
363 wxT("invalid index in wxListBox::GetClientData") );
365 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, MPFROMLONG(n
), (MPARAM
)0));
366 } // end of wxListBox::DoGetItemClientData
368 void wxListBox::DoSetItemClientData(unsigned int n
, void* pClientData
)
370 wxCHECK_RET( IsValid(n
),
371 wxT("invalid index in wxListBox::SetClientData") );
373 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(n
), MPFROMP(pClientData
));
374 } // end of wxListBox::DoSetItemClientData
376 bool wxListBox::HasMultipleSelection() const
378 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
379 } // end of wxListBox::HasMultipleSelection
381 int wxListBox::GetSelections( wxArrayInt
& raSelections
) const
387 raSelections
.Empty();
388 if (HasMultipleSelection())
390 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
396 if (lItem
!= LIT_NONE
)
399 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
408 raSelections
.Alloc(nCount
);
409 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
416 raSelections
.Add((int)lItem
);
417 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
424 raSelections
.Add((int)lItem
);
429 else // single-selection listbox
431 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
437 raSelections
.Add((int)lItem
);
441 } // end of wxListBox::GetSelections
443 int wxListBox::GetSelection() const
445 wxCHECK_MSG( !HasMultipleSelection(),
447 wxT("GetSelection() can't be used with multiple-selection "
448 "listboxes, use GetSelections() instead.") );
450 return(LONGFROMMR(::WinSendMsg( GetHwnd()
456 } // end of wxListBox::GetSelection
458 wxString
wxListBox::GetString(unsigned int n
) const
464 wxCHECK_MSG( IsValid(n
), wxEmptyString
,
465 wxT("invalid index in wxListBox::GetClientData") );
467 lLen
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)n
, (MPARAM
)0));
468 zBuf
= new wxChar
[lLen
+ 1];
469 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)n
, (SHORT
)lLen
), (MPARAM
)zBuf
);
474 } // end of wxListBox::GetString
476 void wxListBox::SetString(unsigned int n
, const wxString
& rsString
)
478 wxCHECK_RET( IsValid(n
),
479 wxT("invalid index in wxListBox::SetString") );
482 // Remember the state of the item
484 bool bWasSelected
= IsSelected(n
);
485 void* pOldData
= NULL
;
486 wxClientData
* pOldObjData
= NULL
;
488 if ( HasClientUntypedData() )
489 pOldData
= GetClientData(n
);
490 else if ( HasClientObjectData() )
491 pOldObjData
= GetClientObject(n
);
494 // Delete and recreate it
496 ::WinSendMsg( GetHwnd()
504 if (n
== (m_nNumItems
- 1))
507 ::WinSendMsg( GetHwnd()
510 ,(MPARAM
)rsString
.wx_str()
514 // Restore the client data
517 SetClientData(n
, pOldData
);
518 else if (pOldObjData
)
519 SetClientObject(n
, pOldObjData
);
522 // We may have lost the selection
526 } // end of wxListBox::SetString
528 unsigned int wxListBox::GetCount() const
533 // ----------------------------------------------------------------------------
535 // ----------------------------------------------------------------------------
537 wxSize
wxListBox::DoGetBestSize() const
540 // Find the widest string
546 wxFont vFont
= (wxFont
)GetFont();
548 for (unsigned int i
= 0; i
< m_nNumItems
; i
++)
550 wxString
vStr(GetString(i
));
552 GetTextExtent( vStr
, &nLine
, NULL
);
553 if (nLine
> nListbox
)
558 // Give it some reasonable default value if there are no strings in the
565 // The listbox should be slightly larger than the widest string
567 wxGetCharSize( GetHWND()
574 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * (wxMax(m_nNumItems
, 7));
576 return wxSize( nListbox
579 } // end of wxListBox::DoGetBestSize
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 bool wxListBox::OS2Command(
587 , WXWORD
WXUNUSED(wId
))
589 wxEventType eEvtType
;
591 if (uParam
== LN_SELECT
)
593 eEvtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
595 else if (uParam
== LN_ENTER
)
597 eEvtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
602 // Some event we're not interested in
606 wxCommandEvent
vEvent( eEvtType
610 vEvent
.SetEventObject(this);
612 wxArrayInt aSelections
;
614 int nCount
= GetSelections(aSelections
);
619 if (HasClientObjectData())
620 vEvent
.SetClientObject(GetClientObject(n
));
621 else if ( HasClientUntypedData() )
622 vEvent
.SetClientData(GetClientData(n
));
623 vEvent
.SetString(GetString(n
));
630 return HandleWindowEvent(vEvent
);
631 } // end of wxListBox::OS2Command
633 // ----------------------------------------------------------------------------
634 // wxCheckListBox support
635 // ----------------------------------------------------------------------------
637 #if wxUSE_OWNER_DRAWN
643 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
645 long wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
* pItem
)
648 pItem
= (WXMEASUREITEMSTRUCT
*)new OWNERITEM
;
650 POWNERITEM pMeasureStruct
= (POWNERITEM
)pItem
;
654 // Only owner-drawn control should receive this message
656 wxCHECK( ((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), FALSE
);
658 vDc
.SetFont(GetFont());
667 pMeasureStruct
->rclItem
.xRight
= (USHORT
)vWidth
;
668 pMeasureStruct
->rclItem
.xLeft
= 0;
669 pMeasureStruct
->rclItem
.yTop
= 0;
670 pMeasureStruct
->rclItem
.yBottom
= 0;
672 vHeight
= (wxCoord
)(vDc
.GetCharHeight() * 2.5);
673 pMeasureStruct
->rclItem
.yTop
= (USHORT
)vHeight
;
675 return long(MRFROM2SHORT((USHORT
)vHeight
, (USHORT
)vWidth
));
676 } // end of wxListBox::OS2OnMeasure
678 bool wxListBox::OS2OnDraw (
679 WXDRAWITEMSTRUCT
* pItem
682 POWNERITEM pDrawStruct
= (POWNERITEM
)pItem
;
687 // Only owner-drawn control should receive this message
689 wxCHECK(((m_windowStyle
& wxLB_OWNERDRAW
) == wxLB_OWNERDRAW
), false);
693 // The item may be -1 for an empty listbox
695 if (pDrawStruct
->idItem
== -1L)
698 wxListBoxItem
* pData
= (wxListBoxItem
*)m_aItems
[pDrawStruct
->idItem
];
700 wxClientDC
vDc(this);
701 wxPMDCImpl
*impl
= (wxPMDCImpl
*) vDc
.GetImpl();
702 wxPoint
pt1( pDrawStruct
->rclItem
.xLeft
, pDrawStruct
->rclItem
.yTop
);
703 wxPoint
pt2( pDrawStruct
->rclItem
.xRight
, pDrawStruct
->rclItem
.yBottom
);
704 wxRect
vRect( pt1
, pt2
);
706 impl
->SetHPS(pDrawStruct
->hps
);
708 if (pDrawStruct
->fsAttribute
== pDrawStruct
->fsAttributeOld
)
711 // Entire Item needs to be redrawn (either it has reappeared from
712 // behind another window or is being displayed for the first time
714 eAction
= wxOwnerDrawn::wxODDrawAll
;
716 if (pDrawStruct
->fsAttribute
& MIA_HILITED
)
719 // If it is currently selected we let the system handle it
721 eStatus
|= wxOwnerDrawn::wxODSelected
;
723 if (pDrawStruct
->fsAttribute
& MIA_CHECKED
)
726 // If it is currently checked we draw our own
728 eStatus
|= wxOwnerDrawn::wxODChecked
;
729 pDrawStruct
->fsAttributeOld
= pDrawStruct
->fsAttribute
&= ~MIA_CHECKED
;
731 if (pDrawStruct
->fsAttribute
& MIA_DISABLED
)
734 // If it is currently disabled we let the system handle it
736 eStatus
|= wxOwnerDrawn::wxODDisabled
;
739 // Don't really care about framed (indicationg focus) or NoDismiss
744 if (pDrawStruct
->fsAttribute
& MIA_HILITED
)
746 eAction
= wxOwnerDrawn::wxODDrawAll
;
747 eStatus
|= wxOwnerDrawn::wxODSelected
;
749 // Keep the system from trying to highlight with its bogus colors
751 pDrawStruct
->fsAttributeOld
= pDrawStruct
->fsAttribute
&= ~MIA_HILITED
;
753 else if (!(pDrawStruct
->fsAttribute
& MIA_HILITED
))
755 eAction
= wxOwnerDrawn::wxODDrawAll
;
758 // Keep the system from trying to highlight with its bogus colors
760 pDrawStruct
->fsAttribute
= pDrawStruct
->fsAttributeOld
&= ~MIA_HILITED
;
765 // For now we don't care about anything else
766 // just ignore the entire message!
771 return pData
->OnDrawItem( vDc
773 ,(wxOwnerDrawn::wxODAction
)eAction
774 ,(wxOwnerDrawn::wxODStatus
)(eStatus
| wxOwnerDrawn::wxODHidePrefix
)
776 } // end of wxListBox::OS2OnDraw
778 #endif // ndef for wxUSE_OWNER_DRAWN
780 #endif // wxUSE_LISTBOX