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
.Insert(pNewItem
, nIndex
);
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 WX_CLEAR_ARRAY(m_aItems
);
337 // Then create new ones
339 for (size_t ui
= 0; ui
< (size_t)m_nNumItems
; ui
++)
341 wxOwnerDrawn
* pNewItem
= CreateItem(ui
);
343 pNewItem
->SetName(raChoices
[ui
]);
344 m_aItems
.Add(pNewItem
);
345 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(ui
), MPFROMP(pNewItem
));
348 #endif // wxUSE_OWNER_DRAWN
349 ::WinShowWindow(GetHwnd(), TRUE
);
350 } // end of wxListBox::DoSetItems
352 int wxListBox::FindString(
353 const wxString
& rsString
361 for (nPos
= 0; nPos
< m_nNumItems
; nPos
++)
363 lTextLength
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)nPos
, (MPARAM
)0));
364 zStr
= new char[lTextLength
+ 1];
365 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT(nPos
, (SHORT
)lTextLength
), (MPARAM
)zStr
);
366 if (rsString
== (char*)zStr
)
374 } // end of wxListBox::FindString
376 void wxListBox::Clear()
378 #if wxUSE_OWNER_DRAWN
379 size_t lUiCount
= m_aItems
.Count();
381 while (lUiCount
-- != 0)
383 delete m_aItems
[lUiCount
];
387 #else // !wxUSE_OWNER_DRAWN
388 if (HasClientObjectData())
390 for (size_t n
= 0; n
< (size_t)m_lNumItems
; n
++)
392 delete GetClientObject(n
);
395 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
396 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
399 } // end of wxListBox::Clear
401 void wxListBox::SetSelection(
406 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
407 wxT("invalid index in wxListBox::SetSelection") );
408 ::WinSendMsg( GetHwnd()
413 } // end of wxListBox::SetSelection
415 bool wxListBox::IsSelected(
419 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, FALSE
,
420 wxT("invalid index in wxListBox::Selected") );
424 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)N
, (MPARAM
)0));
425 return (lItem
!= LIT_NONE
);
426 } // end of wxListBox::IsSelected
428 wxClientData
* wxListBox::DoGetItemClientObject(
432 return (wxClientData
*)DoGetItemClientData(n
);
435 void* wxListBox::DoGetItemClientData(
439 wxCHECK_MSG( n
>= 0 && n
< m_nNumItems
, NULL
,
440 wxT("invalid index in wxListBox::GetClientData") );
442 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, MPFROMLONG(n
), (MPARAM
)0));
443 } // end of wxListBox::DoGetItemClientData
445 void wxListBox::DoSetItemClientObject(
447 , wxClientData
* pClientData
450 DoSetItemClientData( n
453 } // end of wxListBox::DoSetItemClientObject
455 void wxListBox::DoSetItemClientData(
460 wxCHECK_RET( n
>= 0 && n
< m_nNumItems
,
461 wxT("invalid index in wxListBox::SetClientData") );
463 #if wxUSE_OWNER_DRAWN
464 if ( m_windowStyle
& wxLB_OWNERDRAW
)
467 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
468 // in OnMeasure/OnDraw.
470 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
472 #endif // wxUSE_OWNER_DRAWN
474 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(n
), MPFROMP(pClientData
));
475 } // end of wxListBox::DoSetItemClientData
477 bool wxListBox::HasMultipleSelection() const
479 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
480 } // end of wxListBox::HasMultipleSelection
482 int wxListBox::GetSelections(
483 wxArrayInt
& raSelections
490 raSelections
.Empty();
491 if (HasMultipleSelection())
493 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
499 if (lItem
!= LIT_NONE
)
502 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
511 raSelections
.Alloc(nCount
);
512 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
519 raSelections
.Add((int)lItem
);
520 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
527 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
, "",
571 wxT("invalid index in wxListBox::GetClientData") );
573 lLen
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)N
, (MPARAM
)0));
574 zBuf
= new char[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
)((SHORT
)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
700 for (int i
= 0; i
< m_nNumItems
; i
++)
702 wxString
vStr(GetString(i
));
708 if (nLine
> nListbox
)
713 // Give it some reasonable default value if there are no strings in the
720 // The listbox should be slightly larger than the widest string
722 wxGetCharSize( GetHWND()
729 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * (wxMax(m_nNumItems
, 7));
731 return wxSize( nListbox
734 } // end of wxListBox::DoGetBestSize
736 // ----------------------------------------------------------------------------
738 // ----------------------------------------------------------------------------
740 bool wxListBox::OS2Command(
742 , WXWORD
WXUNUSED(wId
))
744 wxEventType eEvtType
;
746 if (uParam
== LN_SELECT
)
748 eEvtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
750 if (uParam
== LN_ENTER
)
752 eEvtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
757 // Some event we're not interested in
761 wxCommandEvent
vEvent( eEvtType
765 vEvent
.SetEventObject(this);
767 wxArrayInt aSelections
;
769 int nCount
= GetSelections(aSelections
);
774 if (HasClientObjectData())
775 vEvent
.SetClientObject(GetClientObject(n
));
776 else if ( HasClientUntypedData() )
777 vEvent
.SetClientData(GetClientData(n
));
778 vEvent
.SetString(GetString(n
));
784 vEvent
.m_commandInt
= n
;
785 return GetEventHandler()->ProcessEvent(vEvent
);
786 } // end of wxListBox::OS2Command
788 // ----------------------------------------------------------------------------
789 // wxCheckListBox support
790 // ----------------------------------------------------------------------------
792 #if wxUSE_OWNER_DRAWN
798 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
800 bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
*item
)
803 // TODO: Get to this eventually
808 bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT
*item
)
811 // TODO: Get to this eventually
815 #endif // ndef for wxUSE_OWNER_DRAWN
817 #endif // ndef for wxUSE_LISTBOX