1 ///////////////////////////////////////////////////////////////////////////////
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"
25 #include "wx/scrolwin.h"
31 #include "wx/dynarray.h"
37 #include "wx/ownerdrw.h"
40 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
42 // ============================================================================
43 // list box item declaration and implementation
44 // ============================================================================
48 class wxListBoxItem
: public wxOwnerDrawn
51 wxListBoxItem(const wxString
& rsStr
= "");
54 wxListBoxItem::wxListBoxItem(
62 // No bitmaps/checkmarks
65 } // end of wxListBoxItem::wxListBoxItem
67 wxOwnerDrawn
* wxListBox::CreateItem(
71 return new wxListBoxItem();
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(
93 , const wxString asChoices
[]
96 , const wxValidator
& rValidator
98 , const wxString
& rsName
107 SetValidator(rValidator
);
111 pParent
->AddChild(this);
113 wxSystemSettings vSettings
;
115 SetBackgroundColour(vSettings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
116 SetForegroundColour(pParent
->GetForegroundColour());
118 m_windowId
= (vId
== -1) ? (int)NewControlId() : vId
;
122 int nWidth
= rSize
.x
;
123 int nHeight
= rSize
.y
;
125 m_windowStyle
= lStyle
;
129 if (m_windowStyle
& wxCLIP_SIBLINGS
)
130 lStyle
|= WS_CLIPSIBLINGS
;
131 if (m_windowStyle
& wxLB_MULTIPLE
)
132 lStyle
|= LS_MULTIPLESEL
;
133 else if (m_windowStyle
& wxLB_EXTENDED
)
134 lStyle
|= LS_EXTENDEDSEL
;
135 if (m_windowStyle
& wxLB_HSCROLL
)
136 lStyle
|= LS_HORZSCROLL
;
137 if (m_windowStyle
& wxLB_OWNERDRAW
)
138 lStyle
|= LS_OWNERDRAW
;
141 // Without this style, you get unexpected heights, so e.g. constraint layout
142 // doesn't work properly
144 lStyle
|= LS_NOADJUSTPOS
;
146 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
) // Parent
147 ,WC_LISTBOX
// Default Listbox class
148 ,"LISTBOX" // Control's name
149 ,lStyle
// Initial Style
150 ,0, 0, 0, 0 // Position and size
151 ,GetWinHwnd(pParent
) // Owner
153 ,(HMENU
)m_windowId
// Id
154 ,NULL
// Control Data
155 ,NULL
// Presentation Parameters
163 // Subclass again for purposes of dialog editing mode
169 for (lUi
= 0; lUi
< (LONG
)n
; lUi
++)
171 Append(asChoices
[lUi
]);
173 wxFont
* pTextFont
= new wxFont( 10
181 // Set standard wxWindows colors for Listbox items and highlighting
185 vColour
.Set(wxString("WHITE"));
187 LONG lColor
= (LONG
)vColour
.GetPixel();
189 ::WinSetPresParam( m_hWnd
190 ,PP_HILITEFOREGROUNDCOLOR
194 vColour
.Set(wxString("NAVY"));
195 lColor
= (LONG
)vColour
.GetPixel();
196 ::WinSetPresParam( m_hWnd
197 ,PP_HILITEBACKGROUNDCOLOR
209 } // end of wxListBox::Create
211 wxListBox::~wxListBox()
213 #if wxUSE_OWNER_DRAWN
214 size_t lUiCount
= m_aItems
.Count();
216 while (lUiCount
-- != 0)
218 delete m_aItems
[lUiCount
];
220 #endif // wxUSE_OWNER_DRAWN
221 } // end of wxListBox::~wxListBox
223 void wxListBox::SetupColours()
225 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
226 SetForegroundColour(GetParent()->GetForegroundColour());
227 } // end of wxListBox::SetupColours
229 // ----------------------------------------------------------------------------
230 // implementation of wxListBoxBase methods
231 // ----------------------------------------------------------------------------
233 void wxListBox::DoSetFirstItem(
237 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
238 wxT("invalid index in wxListBox::SetFirstItem") );
240 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX
, MPFROMLONG(N
), (MPARAM
)0);
241 } // end of wxListBox::DoSetFirstItem
243 void wxListBox::Delete(
247 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
248 wxT("invalid index in wxListBox::Delete") );
250 #if wxUSE_OWNER_DRAWN
252 m_aItems
.RemoveAt(N
);
253 #else // !wxUSE_OWNER_DRAWN
254 if (HasClientObjectData())
256 delete GetClientObject(N
);
258 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
260 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)N
, (MPARAM
)0);
262 } // end of wxListBox::DoSetFirstItem
264 int wxListBox::DoAppend(
265 const wxString
& rsItem
269 SHORT nIndexType
= 0;
271 if (m_windowStyle
& wxLB_SORT
)
272 nIndexType
= LIT_SORTASCENDING
;
274 nIndexType
= LIT_END
;
275 nIndex
= (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)nIndexType
, (MPARAM
)rsItem
.c_str());
278 #if wxUSE_OWNER_DRAWN
279 if (m_windowStyle
& wxLB_OWNERDRAW
)
281 wxOwnerDrawn
* pNewItem
= CreateItem(nIndex
); // dummy argument
283 pNewItem
->SetName(rsItem
);
284 m_aItems
.Add(pNewItem
);
285 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)((SHORT
)nIndex
), MPFROMP(pNewItem
));
286 pNewItem
->SetFont(GetFont());
290 } // end of wxListBox::DoAppend
292 void wxListBox::DoSetItems(
293 const wxArrayString
& raChoices
294 , void** ppClientData
297 BOOL bHideAndShow
= IsShown();
300 SHORT nIndexType
= 0;
304 ::WinShowWindow(GetHwnd(), FALSE
);
306 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
307 m_nNumItems
= raChoices
.GetCount();
308 for (i
= 0; i
< m_nNumItems
; i
++)
311 if (m_windowStyle
& wxLB_SORT
)
312 nIndexType
= LIT_SORTASCENDING
;
314 nIndexType
= LIT_END
;
315 ::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)nIndexType
, (MPARAM
)raChoices
[i
].c_str());
319 #if wxUSE_OWNER_DRAWN
320 wxASSERT_MSG(ppClientData
[i
] == NULL
,
321 wxT("Can't use client data with owner-drawn listboxes"));
322 #else // !wxUSE_OWNER_DRAWN
323 ::WinSendMsg(WinUtil_GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(lCount
), MPFROMP(ppClientData
[i
]));
324 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
328 #if wxUSE_OWNER_DRAWN
329 if ( m_windowStyle
& wxLB_OWNERDRAW
)
332 // First delete old items
334 size_t lUi
= m_aItems
.Count();
338 delete m_aItems
[lUi
];
343 // Then create new ones
345 for (lUi
= 0; lUi
< (size_t)m_nNumItems
; lUi
++)
347 wxOwnerDrawn
* pNewItem
= CreateItem(lUi
);
349 pNewItem
->SetName(raChoices
[lUi
]);
350 m_aItems
.Add(pNewItem
);
351 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(lUi
), MPFROMP(pNewItem
));
354 #endif // wxUSE_OWNER_DRAWN
355 ::WinShowWindow(GetHwnd(), TRUE
);
356 } // end of wxListBox::DoSetItems
358 int wxListBox::FindString(
359 const wxString
& rsString
367 for (nPos
= 0; nPos
< m_nNumItems
; nPos
++)
369 lTextLength
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)nPos
, (MPARAM
)0));
370 zStr
= new char[lTextLength
+ 1];
371 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT(nPos
, (SHORT
)lTextLength
), (MPARAM
)zStr
);
372 if (rsString
== (char*)zStr
)
380 } // end of wxListBox::FindString
382 void wxListBox::Clear()
384 #if wxUSE_OWNER_DRAWN
385 size_t lUiCount
= m_aItems
.Count();
387 while (lUiCount
-- != 0)
389 delete m_aItems
[lUiCount
];
393 #else // !wxUSE_OWNER_DRAWN
394 if (HasClientObjectData())
396 for (size_t n
= 0; n
< (size_t)m_lNumItems
; n
++)
398 delete GetClientObject(n
);
401 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
402 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
405 } // end of wxListBox::Clear
407 void wxListBox::SetSelection(
412 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
413 wxT("invalid index in wxListBox::SetSelection") );
414 ::WinSendMsg( GetHwnd()
419 } // end of wxListBox::SetSelection
421 bool wxListBox::IsSelected(
425 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, FALSE
,
426 wxT("invalid index in wxListBox::Selected") );
430 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)N
, (MPARAM
)0));
431 return (lItem
!= LIT_NONE
);
432 } // end of wxListBox::IsSelected
434 wxClientData
* wxListBox::DoGetItemClientObject(
438 return (wxClientData
*)DoGetItemClientData(n
);
441 void* wxListBox::DoGetItemClientData(
445 wxCHECK_MSG( n
>= 0 && n
< m_nNumItems
, NULL
,
446 wxT("invalid index in wxListBox::GetClientData") );
448 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, MPFROMLONG(n
), (MPARAM
)0));
449 } // end of wxListBox::DoGetItemClientData
451 void wxListBox::DoSetItemClientObject(
453 , wxClientData
* pClientData
456 DoSetItemClientData( n
459 } // end of wxListBox::DoSetItemClientObject
461 void wxListBox::DoSetItemClientData(
466 wxCHECK_RET( n
>= 0 && n
< m_nNumItems
,
467 wxT("invalid index in wxListBox::SetClientData") );
469 #if wxUSE_OWNER_DRAWN
470 if ( m_windowStyle
& wxLB_OWNERDRAW
)
473 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
474 // in OnMeasure/OnDraw.
476 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
478 #endif // wxUSE_OWNER_DRAWN
480 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(n
), MPFROMP(pClientData
));
481 } // end of wxListBox::DoSetItemClientData
483 bool wxListBox::HasMultipleSelection() const
485 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
486 } // end of wxListBox::HasMultipleSelection
488 int wxListBox::GetSelections(
489 wxArrayInt
& raSelections
496 raSelections
.Empty();
497 if (HasMultipleSelection())
499 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
505 if (lItem
!= LIT_NONE
)
508 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
517 raSelections
.Alloc(nCount
);
518 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
525 raSelections
.Add((int)lItem
);
526 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
533 raSelections
.Add((int)lItem
);
539 else // single-selection listbox
541 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
547 raSelections
.Add((int)lItem
);
551 } // end of wxListBox::GetSelections
553 int wxListBox::GetSelection() const
555 wxCHECK_MSG( !HasMultipleSelection(),
557 wxT("GetSelection() can't be used with multiple-selection "
558 "listboxes, use GetSelections() instead.") );
560 return(LONGFROMMR(::WinSendMsg( GetHwnd()
566 } // end of wxListBox::GetSelection
568 wxString
wxListBox::GetString(
576 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, "",
577 wxT("invalid index in wxListBox::GetClientData") );
579 lLen
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)N
, (MPARAM
)0));
580 zBuf
= new char[lLen
+ 1];
581 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)N
, (SHORT
)lLen
), (MPARAM
)zBuf
);
586 } // end of wxListBox::GetString
588 void wxListBox::DoInsertItems(
589 const wxArrayString
& asItems
593 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
594 wxT("invalid index in wxListBox::InsertItems") );
596 int nItems
= asItems
.GetCount();
598 for (int i
= 0; i
< nItems
; i
++)
599 ::WinSendMsg(GetHwnd(), LM_INSERTITEM
, MPFROMLONG((LONG
)(i
+ nPos
)), (MPARAM
)asItems
[i
].c_str());
600 m_nNumItems
+= nItems
;
601 } // end of wxListBox::DoInsertItems
603 void wxListBox::SetString(
605 , const wxString
& rsString
608 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
609 wxT("invalid index in wxListBox::SetString") );
612 // Remember the state of the item
614 bool bWasSelected
= IsSelected(N
);
615 void* pOldData
= NULL
;
616 wxClientData
* pOldObjData
= NULL
;
618 if (m_clientDataItemsType
== wxClientData_Void
)
619 pOldData
= GetClientData(N
);
620 else if (m_clientDataItemsType
== wxClientData_Object
)
621 pOldObjData
= GetClientObject(N
);
624 // Delete and recreate it
626 ::WinSendMsg( GetHwnd()
634 if (N
== m_nNumItems
- 1)
637 ::WinSendMsg( GetHwnd()
640 ,(MPARAM
)rsString
.c_str()
644 // Restore the client data
650 else if (pOldObjData
)
656 // We may have lost the selection
661 #if wxUSE_OWNER_DRAWN
662 if (m_windowStyle
& wxLB_OWNERDRAW
)
664 // Update item's text
666 m_aItems
[N
]->SetName(rsString
);
667 #endif //USE_OWNER_DRAWN
668 } // end of wxListBox::SetString
670 int wxListBox::GetCount() const
675 // ----------------------------------------------------------------------------
677 // ----------------------------------------------------------------------------
679 wxSize
wxListBox::DoGetBestSize() const
682 // Find the widest string
689 for (int i
= 0; i
< m_nNumItems
; i
++)
691 wxString
vStr(GetString(i
));
697 if (nLine
> nListbox
)
702 // Give it some reasonable default value if there are no strings in the
709 // The listbox should be slightly larger than the widest string
711 wxGetCharSize( GetHWND()
718 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * (wxMax(m_nNumItems
, 7));
720 return wxSize( nListbox
723 } // end of wxListBox::DoGetBestSize
725 // ----------------------------------------------------------------------------
727 // ----------------------------------------------------------------------------
729 bool wxListBox::OS2Command(
731 , WXWORD
WXUNUSED(wId
))
733 wxEventType eEvtType
;
735 if (uParam
== LN_SELECT
)
737 eEvtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
739 if (uParam
== LN_ENTER
)
741 eEvtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
746 // Some event we're not interested in
750 wxCommandEvent
vEvent( eEvtType
754 vEvent
.SetEventObject(this);
756 wxArrayInt aSelections
;
758 int nCount
= GetSelections(aSelections
);
763 if (HasClientObjectData())
764 vEvent
.SetClientObject(GetClientObject(n
));
765 else if ( HasClientUntypedData() )
766 vEvent
.SetClientData(GetClientData(n
));
767 vEvent
.SetString(GetString(n
));
773 vEvent
.m_commandInt
= n
;
774 return GetEventHandler()->ProcessEvent(vEvent
);
775 } // end of wxListBox::OS2Command
777 // ----------------------------------------------------------------------------
778 // wxCheckListBox support
779 // ----------------------------------------------------------------------------
781 #if wxUSE_OWNER_DRAWN
787 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
789 bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
*item
)
792 // TODO: Get to this eventually
797 bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT
*item
)
800 // TODO: Get to this eventually
804 #endif // ndef for wxUSE_OWNER_DRAWN
806 #endif // ndef for wxUSE_LISTBOX