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 if (GetWindowStyleFlag() & wxLB_EXTENDED
)
427 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
429 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)(N
- 1), (MPARAM
)0));
433 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)LIT_FIRST
, (MPARAM
)0));
435 return (lItem
== (LONG
)N
&& lItem
!= LIT_NONE
);
436 } // end of wxListBox::IsSelected
438 wxClientData
* wxListBox::DoGetItemClientObject(
442 return (wxClientData
*)DoGetItemClientData(n
);
445 void* wxListBox::DoGetItemClientData(
449 wxCHECK_MSG( n
>= 0 && n
< m_nNumItems
, NULL
,
450 wxT("invalid index in wxListBox::GetClientData") );
452 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, MPFROMLONG(n
), (MPARAM
)0));
453 } // end of wxListBox::DoGetItemClientData
455 void wxListBox::DoSetItemClientObject(
457 , wxClientData
* pClientData
460 DoSetItemClientData( n
463 } // end of wxListBox::DoSetItemClientObject
465 void wxListBox::DoSetItemClientData(
470 wxCHECK_RET( n
>= 0 && n
< m_nNumItems
,
471 wxT("invalid index in wxListBox::SetClientData") );
473 #if wxUSE_OWNER_DRAWN
474 if ( m_windowStyle
& wxLB_OWNERDRAW
)
477 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
478 // in OnMeasure/OnDraw.
480 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
482 #endif // wxUSE_OWNER_DRAWN
484 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(n
), MPFROMP(pClientData
));
485 } // end of wxListBox::DoSetItemClientData
487 bool wxListBox::HasMultipleSelection() const
489 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
490 } // end of wxListBox::HasMultipleSelection
492 int wxListBox::GetSelections(
493 wxArrayInt
& raSelections
500 raSelections
.Empty();
501 if (HasMultipleSelection())
503 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
509 if (lItem
!= LIT_NONE
)
512 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
521 raSelections
.Alloc(nCount
);
522 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
529 raSelections
.Add((int)lItem
);
530 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
537 raSelections
.Add((int)lItem
);
543 else // single-selection listbox
545 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
551 raSelections
.Add((int)lItem
);
555 } // end of wxListBox::GetSelections
557 int wxListBox::GetSelection() const
559 wxCHECK_MSG( !HasMultipleSelection(),
561 wxT("GetSelection() can't be used with multiple-selection "
562 "listboxes, use GetSelections() instead.") );
564 return(LONGFROMMR(::WinSendMsg( GetHwnd()
570 } // end of wxListBox::GetSelection
572 wxString
wxListBox::GetString(
580 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, "",
581 wxT("invalid index in wxListBox::GetClientData") );
583 lLen
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)N
, (MPARAM
)0));
584 zBuf
= new char[lLen
+ 1];
585 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)N
, (SHORT
)lLen
), (MPARAM
)zBuf
);
590 } // end of wxListBox::GetString
592 void wxListBox::DoInsertItems(
593 const wxArrayString
& asItems
597 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
598 wxT("invalid index in wxListBox::InsertItems") );
600 int nItems
= asItems
.GetCount();
602 for (int i
= 0; i
< nItems
; i
++)
604 int nIndex
= (int)::WinSendMsg( GetHwnd()
606 ,MPFROMLONG((LONG
)(i
+ nPos
))
607 ,(MPARAM
)asItems
[i
].c_str()
610 wxOwnerDrawn
* pNewItem
= CreateItem(nIndex
);
612 pNewItem
->SetName(asItems
[i
]);
613 pNewItem
->SetFont(GetFont());
614 m_aItems
.Insert(pNewItem
, nIndex
);
615 ::WinSendMsg( GetHwnd()
617 ,(MPARAM
)((SHORT
)nIndex
)
620 m_nNumItems
+= nItems
;
622 } // end of wxListBox::DoInsertItems
624 void wxListBox::SetString(
626 , const wxString
& rsString
629 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
630 wxT("invalid index in wxListBox::SetString") );
633 // Remember the state of the item
635 bool bWasSelected
= IsSelected(N
);
636 void* pOldData
= NULL
;
637 wxClientData
* pOldObjData
= NULL
;
639 if (m_clientDataItemsType
== wxClientData_Void
)
640 pOldData
= GetClientData(N
);
641 else if (m_clientDataItemsType
== wxClientData_Object
)
642 pOldObjData
= GetClientObject(N
);
645 // Delete and recreate it
647 ::WinSendMsg( GetHwnd()
655 if (N
== m_nNumItems
- 1)
658 ::WinSendMsg( GetHwnd()
661 ,(MPARAM
)rsString
.c_str()
665 // Restore the client data
671 else if (pOldObjData
)
677 // We may have lost the selection
682 #if wxUSE_OWNER_DRAWN
683 if (m_windowStyle
& wxLB_OWNERDRAW
)
685 // Update item's text
687 m_aItems
[N
]->SetName(rsString
);
688 #endif //USE_OWNER_DRAWN
689 } // end of wxListBox::SetString
691 int wxListBox::GetCount() const
696 // ----------------------------------------------------------------------------
698 // ----------------------------------------------------------------------------
700 wxSize
wxListBox::DoGetBestSize() const
703 // Find the widest string
710 for (int i
= 0; i
< m_nNumItems
; i
++)
712 wxString
vStr(GetString(i
));
718 if (nLine
> nListbox
)
723 // Give it some reasonable default value if there are no strings in the
730 // The listbox should be slightly larger than the widest string
732 wxGetCharSize( GetHWND()
739 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * (wxMax(m_nNumItems
, 7));
741 return wxSize( nListbox
744 } // end of wxListBox::DoGetBestSize
746 // ----------------------------------------------------------------------------
748 // ----------------------------------------------------------------------------
750 bool wxListBox::OS2Command(
752 , WXWORD
WXUNUSED(wId
))
754 wxEventType eEvtType
;
756 if (uParam
== LN_SELECT
)
758 eEvtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
760 if (uParam
== LN_ENTER
)
762 eEvtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
767 // Some event we're not interested in
771 wxCommandEvent
vEvent( eEvtType
775 vEvent
.SetEventObject(this);
777 wxArrayInt aSelections
;
779 int nCount
= GetSelections(aSelections
);
784 if (HasClientObjectData())
785 vEvent
.SetClientObject(GetClientObject(n
));
786 else if ( HasClientUntypedData() )
787 vEvent
.SetClientData(GetClientData(n
));
788 vEvent
.SetString(GetString(n
));
794 vEvent
.m_commandInt
= n
;
795 return GetEventHandler()->ProcessEvent(vEvent
);
796 } // end of wxListBox::OS2Command
798 // ----------------------------------------------------------------------------
799 // wxCheckListBox support
800 // ----------------------------------------------------------------------------
802 #if wxUSE_OWNER_DRAWN
808 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
810 bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
*item
)
813 // TODO: Get to this eventually
818 bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT
*item
)
821 // TODO: Get to this eventually
825 #endif // ndef for wxUSE_OWNER_DRAWN
827 #endif // ndef for wxUSE_LISTBOX