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"
30 #include "wx/dynarray.h"
36 #include "wx/ownerdrw.h"
39 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
41 // ============================================================================
42 // list box item declaration and implementation
43 // ============================================================================
47 class wxListBoxItem
: public wxOwnerDrawn
50 wxListBoxItem(const wxString
& rsStr
= "");
53 wxListBoxItem::wxListBoxItem(
61 // No bitmaps/checkmarks
64 } // end of wxListBoxItem::wxListBoxItem
66 wxOwnerDrawn
* wxListBox::CreateItem(
70 return new wxListBoxItem();
71 } // end of wxListBox::CreateItem
73 #endif //USE_OWNER_DRAWN
75 // ============================================================================
76 // list box control implementation
77 // ============================================================================
80 wxListBox::wxListBox()
84 } // end of wxListBox::wxListBox
86 bool wxListBox::Create(
92 , const wxString asChoices
[]
95 , const wxValidator
& rValidator
97 , const wxString
& rsName
106 SetValidator(rValidator
);
110 pParent
->AddChild(this);
112 wxSystemSettings vSettings
;
114 SetBackgroundColour(vSettings
.GetSystemColour(wxSYS_COLOUR_WINDOW
));
115 SetForegroundColour(pParent
->GetForegroundColour());
117 m_windowId
= (vId
== -1) ? (int)NewControlId() : vId
;
121 int nWidth
= rSize
.x
;
122 int nHeight
= rSize
.y
;
124 m_windowStyle
= lStyle
;
128 if (m_windowStyle
& wxCLIP_SIBLINGS
)
129 lStyle
|= WS_CLIPSIBLINGS
;
130 if (m_windowStyle
& wxLB_MULTIPLE
)
131 lStyle
|= LS_MULTIPLESEL
;
132 else if (m_windowStyle
& wxLB_EXTENDED
)
133 lStyle
|= LS_EXTENDEDSEL
;
134 if (m_windowStyle
& wxLB_HSCROLL
)
135 lStyle
|= LS_HORZSCROLL
;
136 if (m_windowStyle
& wxLB_OWNERDRAW
)
137 lStyle
|= LS_OWNERDRAW
;
140 // Without this style, you get unexpected heights, so e.g. constraint layout
141 // doesn't work properly
143 lStyle
|= LS_NOADJUSTPOS
;
145 m_hWnd
= (WXHWND
)::WinCreateWindow( GetWinHwnd(pParent
) // Parent
146 ,WC_LISTBOX
// Default Listbox class
147 ,"LISTBOX" // Control's name
148 ,lStyle
// Initial Style
149 ,0, 0, 0, 0 // Position and size
150 ,GetWinHwnd(pParent
) // Owner
152 ,(HMENU
)m_windowId
// Id
153 ,NULL
// Control Data
154 ,NULL
// Presentation Parameters
162 // Subclass again for purposes of dialog editing mode
168 for (lUi
= 0; lUi
< (LONG
)n
; lUi
++)
170 Append(asChoices
[lUi
]);
172 SetFont(pParent
->GetFont());
179 } // end of wxListBox::Create
181 wxListBox::~wxListBox()
183 #if wxUSE_OWNER_DRAWN
184 size_t lUiCount
= m_aItems
.Count();
186 while (lUiCount
-- != 0)
188 delete m_aItems
[lUiCount
];
190 #endif // wxUSE_OWNER_DRAWN
191 } // end of wxListBox::~wxListBox
193 void wxListBox::SetupColours()
195 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
196 SetForegroundColour(GetParent()->GetForegroundColour());
197 } // end of wxListBox::SetupColours
199 // ----------------------------------------------------------------------------
200 // implementation of wxListBoxBase methods
201 // ----------------------------------------------------------------------------
203 void wxListBox::DoSetFirstItem(
207 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
208 wxT("invalid index in wxListBox::SetFirstItem") );
210 ::WinSendMsg(GetHwnd(), LM_SETTOPINDEX
, MPFROMLONG(N
), (MPARAM
)0);
211 } // end of wxListBox::DoSetFirstItem
213 void wxListBox::Delete(
217 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
218 wxT("invalid index in wxListBox::Delete") );
220 #if wxUSE_OWNER_DRAWN
222 m_aItems
.RemoveAt(N
);
223 #else // !wxUSE_OWNER_DRAWN
224 if (HasClientObjectData())
226 delete GetClientObject(N
);
228 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
230 ::WinSendMsg(GetHwnd(), LM_DELETEITEM
, (MPARAM
)N
, (MPARAM
)0);
232 } // end of wxListBox::DoSetFirstItem
234 int wxListBox::DoAppend(
235 const wxString
& rsItem
239 SHORT nIndexType
= 0;
241 if (m_windowStyle
& wxLB_SORT
)
242 nIndexType
= LIT_SORTASCENDING
;
244 nIndexType
= LIT_END
;
245 nIndex
= (int)::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)nIndexType
, (MPARAM
)rsItem
.c_str());
248 #if wxUSE_OWNER_DRAWN
249 if (m_windowStyle
& wxLB_OWNERDRAW
)
251 wxOwnerDrawn
* pNewItem
= CreateItem(nIndex
); // dummy argument
253 pNewItem
->SetName(rsItem
);
254 m_aItems
.Add(pNewItem
);
255 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, (MPARAM
)((SHORT
)nIndex
), MPFROMP(pNewItem
));
256 pNewItem
->SetFont(GetFont());
260 } // end of wxListBox::DoAppend
262 void wxListBox::DoSetItems(
263 const wxArrayString
& raChoices
264 , void** ppClientData
267 BOOL bHideAndShow
= IsShown();
270 SHORT nIndexType
= 0;
274 ::WinShowWindow(GetHwnd(), FALSE
);
276 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
277 m_nNumItems
= raChoices
.GetCount();
278 for (i
= 0; i
< m_nNumItems
; i
++)
281 if (m_windowStyle
& wxLB_SORT
)
282 nIndexType
= LIT_SORTASCENDING
;
284 nIndexType
= LIT_END
;
285 ::WinSendMsg(GetHwnd(), LM_INSERTITEM
, (MPARAM
)nIndexType
, (MPARAM
)raChoices
[i
].c_str());
289 #if wxUSE_OWNER_DRAWN
290 wxASSERT_MSG(ppClientData
[i
] == NULL
,
291 wxT("Can't use client data with owner-drawn listboxes"));
292 #else // !wxUSE_OWNER_DRAWN
293 ::WinSendMsg(WinUtil_GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(lCount
), MPFROMP(ppClientData
[i
]));
294 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
298 #if wxUSE_OWNER_DRAWN
299 if ( m_windowStyle
& wxLB_OWNERDRAW
)
302 // First delete old items
304 size_t lUi
= m_aItems
.Count();
308 delete m_aItems
[lUi
];
313 // Then create new ones
315 for (lUi
= 0; lUi
< (size_t)m_nNumItems
; lUi
++)
317 wxOwnerDrawn
* pNewItem
= CreateItem(lUi
);
319 pNewItem
->SetName(raChoices
[lUi
]);
320 m_aItems
.Add(pNewItem
);
321 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(lUi
), MPFROMP(pNewItem
));
324 #endif // wxUSE_OWNER_DRAWN
325 ::WinShowWindow(GetHwnd(), TRUE
);
326 } // end of wxListBox::DoSetItems
328 int wxListBox::FindString(
329 const wxString
& rsString
337 for (nPos
= 0; nPos
< m_nNumItems
; nPos
++)
339 lTextLength
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)nPos
, (MPARAM
)0));
340 zStr
= new char[lTextLength
+ 1];
341 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT(nPos
, (SHORT
)lTextLength
), (MPARAM
)zStr
);
342 if (rsString
== (char*)zStr
)
350 } // end of wxListBox::FindString
352 void wxListBox::Clear()
354 #if wxUSE_OWNER_DRAWN
355 size_t lUiCount
= m_aItems
.Count();
357 while (lUiCount
-- != 0)
359 delete m_aItems
[lUiCount
];
363 #else // !wxUSE_OWNER_DRAWN
364 if (HasClientObjectData())
366 for (size_t n
= 0; n
< (size_t)m_lNumItems
; n
++)
368 delete GetClientObject(n
);
371 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
372 ::WinSendMsg(GetHwnd(), LM_DELETEALL
, (MPARAM
)0, (MPARAM
)0);
375 } // end of wxListBox::Clear
377 void wxListBox::SetSelection(
382 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
383 wxT("invalid index in wxListBox::SetSelection") );
384 ::WinSendMsg( GetHwnd()
389 } // end of wxListBox::SetSelection
391 bool wxListBox::IsSelected(
395 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, FALSE
,
396 wxT("invalid index in wxListBox::Selected") );
400 lItem
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYSELECTION
, (MPARAM
)N
, (MPARAM
)0));
401 return (lItem
!= LIT_NONE
);
402 } // end of wxListBox::IsSelected
404 wxClientData
* wxListBox::DoGetItemClientObject(
408 return (wxClientData
*)DoGetItemClientData(n
);
411 void* wxListBox::DoGetItemClientData(
415 wxCHECK_MSG( n
>= 0 && n
< m_nNumItems
, NULL
,
416 wxT("invalid index in wxListBox::GetClientData") );
418 return((void *)::WinSendMsg(GetHwnd(), LM_QUERYITEMHANDLE
, MPFROMLONG(n
), (MPARAM
)0));
419 } // end of wxListBox::DoGetItemClientData
421 void wxListBox::DoSetItemClientObject(
423 , wxClientData
* pClientData
426 DoSetItemClientData( n
429 } // end of wxListBox::DoSetItemClientObject
431 void wxListBox::DoSetItemClientData(
436 wxCHECK_RET( n
>= 0 && n
< m_nNumItems
,
437 wxT("invalid index in wxListBox::SetClientData") );
439 #if wxUSE_OWNER_DRAWN
440 if ( m_windowStyle
& wxLB_OWNERDRAW
)
443 // Client data must be pointer to wxOwnerDrawn, otherwise we would crash
444 // in OnMeasure/OnDraw.
446 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
448 #endif // wxUSE_OWNER_DRAWN
450 ::WinSendMsg(GetHwnd(), LM_SETITEMHANDLE
, MPFROMLONG(n
), MPFROMP(pClientData
));
451 } // end of wxListBox::DoSetItemClientData
453 bool wxListBox::HasMultipleSelection() const
455 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
456 } // end of wxListBox::HasMultipleSelection
458 int wxListBox::GetSelections(
459 wxArrayInt
& raSelections
466 raSelections
.Empty();
467 if (HasMultipleSelection())
469 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
475 if (lItem
!= LIT_NONE
)
478 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
487 raSelections
.Alloc(nCount
);
488 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
495 raSelections
.Add((int)lItem
);
496 while ((lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
503 raSelections
.Add((int)lItem
);
509 else // single-selection listbox
511 lItem
= LONGFROMMR(::WinSendMsg( GetHwnd()
517 raSelections
.Add((int)lItem
);
521 } // end of wxListBox::GetSelections
523 int wxListBox::GetSelection() const
525 wxCHECK_MSG( !HasMultipleSelection(),
527 wxT("GetSelection() can't be used with multiple-selection "
528 "listboxes, use GetSelections() instead.") );
530 return(LONGFROMMR(::WinSendMsg( GetHwnd()
536 } // end of wxListBox::GetSelection
538 wxString
wxListBox::GetString(
546 wxCHECK_MSG( N
>= 0 && N
< m_nNumItems
, "",
547 wxT("invalid index in wxListBox::GetClientData") );
549 lLen
= LONGFROMMR(::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXTLENGTH
, (MPARAM
)N
, (MPARAM
)0));
550 zBuf
= new char[lLen
+ 1];
551 ::WinSendMsg(GetHwnd(), LM_QUERYITEMTEXT
, MPFROM2SHORT((SHORT
)N
, (SHORT
)lLen
), (MPARAM
)zBuf
);
556 } // end of wxListBox::GetString
558 void wxListBox::DoInsertItems(
559 const wxArrayString
& asItems
563 wxCHECK_RET( nPos
>= 0 && nPos
<= m_nNumItems
,
564 wxT("invalid index in wxListBox::InsertItems") );
566 int nItems
= asItems
.GetCount();
568 for (int i
= 0; i
< nItems
; i
++)
569 ::WinSendMsg(GetHwnd(), LM_INSERTITEM
, MPFROMLONG((LONG
)(i
+ nPos
)), (MPARAM
)asItems
[i
].c_str());
570 m_nNumItems
+= nItems
;
571 } // end of wxListBox::DoInsertItems
573 void wxListBox::SetString(
575 , const wxString
& rsString
578 wxCHECK_RET( N
>= 0 && N
< m_nNumItems
,
579 wxT("invalid index in wxListBox::SetString") );
582 // Remember the state of the item
584 bool bWasSelected
= IsSelected(N
);
585 void* pOldData
= NULL
;
586 wxClientData
* pOldObjData
= NULL
;
588 if (m_clientDataItemsType
== wxClientData_Void
)
589 pOldData
= GetClientData(N
);
590 else if (m_clientDataItemsType
== wxClientData_Object
)
591 pOldObjData
= GetClientObject(N
);
594 // Delete and recreate it
596 ::WinSendMsg( GetHwnd()
604 if (N
== m_nNumItems
- 1)
607 ::WinSendMsg( GetHwnd()
610 ,(MPARAM
)rsString
.c_str()
614 // Restore the client data
620 else if (pOldObjData
)
626 // We may have lost the selection
631 #if wxUSE_OWNER_DRAWN
632 if (m_windowStyle
& wxLB_OWNERDRAW
)
634 // Update item's text
636 m_aItems
[N
]->SetName(rsString
);
637 #endif //USE_OWNER_DRAWN
638 } // end of wxListBox::SetString
640 int wxListBox::GetCount() const
645 // ----------------------------------------------------------------------------
647 // ----------------------------------------------------------------------------
649 wxSize
wxListBox::DoGetBestSize() const
652 // Find the widest string
659 for (int i
= 0; i
< m_nNumItems
; i
++)
661 wxString
vStr(GetString(i
));
667 if (nLine
> nListbox
)
672 // Give it some reasonable default value if there are no strings in the
679 // The listbox should be slightly larger than the widest string
681 wxGetCharSize( GetHWND()
688 int hListbox
= EDIT_HEIGHT_FROM_CHAR_HEIGHT(nCy
) * (wxMax(m_nNumItems
, 7));
690 return wxSize( nListbox
693 } // end of wxListBox::DoGetBestSize
695 // ----------------------------------------------------------------------------
697 // ----------------------------------------------------------------------------
699 bool wxListBox::OS2Command(
701 , WXWORD
WXUNUSED(wId
))
703 wxEventType eEvtType
;
705 if (uParam
== LN_SELECT
)
707 eEvtType
= wxEVT_COMMAND_LISTBOX_SELECTED
;
709 if (uParam
== LN_ENTER
)
711 eEvtType
= wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
;
716 // Some event we're not interested in
720 wxCommandEvent
vEvent( eEvtType
724 vEvent
.SetEventObject(this);
726 wxArrayInt aSelections
;
728 int nCount
= GetSelections(aSelections
);
733 if (HasClientObjectData())
734 vEvent
.SetClientObject(GetClientObject(n
));
735 else if ( HasClientUntypedData() )
736 vEvent
.SetClientData(GetClientData(n
));
737 vEvent
.SetString(GetString(n
));
743 vEvent
.m_commandInt
= n
;
744 return GetEventHandler()->ProcessEvent(vEvent
);
745 } // end of wxListBox::OS2Command
747 // ----------------------------------------------------------------------------
748 // wxCheckListBox support
749 // ----------------------------------------------------------------------------
751 #if wxUSE_OWNER_DRAWN
757 #define OWNER_DRAWN_LISTBOX_EXTRA_SPACE (1)
759 bool wxListBox::OS2OnMeasure(WXMEASUREITEMSTRUCT
*item
)
762 // TODO: Get to this eventually
767 bool wxListBox::OS2OnDraw(WXDRAWITEMSTRUCT
*item
)
770 // TODO: Get to this eventually
774 #endif // ndef for wxUSE_OWNER_DRAWN
776 #endif // ndef for wxUSE_LISTBOX