GetSelections() changed to work with wxArrayInt

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@130 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1998-06-22 22:09:38 +00:00
parent 1d7929284e
commit 144833307d

View File

@@ -42,6 +42,9 @@
#include "wx/ownerdrw.h" #include "wx/ownerdrw.h"
#endif #endif
#include "wx/dynarray.h"
#include "wx/log.h"
#if !USE_SHARED_LIBRARY #if !USE_SHARED_LIBRARY
IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
#endif #endif
@@ -89,11 +92,11 @@ bool wxListBox::MSWCommand(const WXUINT param, const WXWORD WXUNUSED(id))
if (param == LBN_SELCHANGE) if (param == LBN_SELCHANGE)
{ {
wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
int *liste = NULL; wxArrayInt aSelections;
int count = GetSelections(&liste) ; int count = GetSelections(aSelections);
if (count && liste) if ( count > 0 )
{ {
event.m_commandInt = liste[0] ; event.m_commandInt = aSelections[0] ;
event.m_clientData = GetClientData(event.m_commandInt); event.m_clientData = GetClientData(event.m_commandInt);
wxString str(GetString(event.m_commandInt)); wxString str(GetString(event.m_commandInt));
if (str != "") if (str != "")
@@ -133,7 +136,6 @@ wxListBox::wxListBox(void)
{ {
m_noItems = 0; m_noItems = 0;
m_selected = 0; m_selected = 0;
m_selections = NULL;
} }
bool wxListBox::Create(wxWindow *parent, const wxWindowID id, bool wxListBox::Create(wxWindow *parent, const wxWindowID id,
@@ -147,7 +149,6 @@ bool wxListBox::Create(wxWindow *parent, const wxWindowID id,
m_noItems = n; m_noItems = n;
m_hWnd = 0; m_hWnd = 0;
m_selected = 0; m_selected = 0;
m_selections = NULL;
SetName(name); SetName(name);
SetValidator(validator); SetValidator(validator);
@@ -256,8 +257,6 @@ wxListBox::~wxListBox(void)
delete m_aItems[uiCount]; delete m_aItems[uiCount];
} }
#endif #endif
DELETEA(m_selections);
} }
void wxListBox::SetupColours(void) void wxListBox::SetupColours(void)
@@ -408,63 +407,51 @@ char *wxListBox::GetClientData(const int N) const
void wxListBox::SetClientData(const int N, char *Client_data) void wxListBox::SetClientData(const int N, char *Client_data)
{ {
(void)SendMessage(hwnd, LB_SETITEMDATA, N, (LONG)Client_data); if ( ListBox_SetItemData(hwnd, N, Client_data) == LB_ERR )
/* wxLogDebug("LB_SETITEMDATA failed");
if (result == LB_ERR)
return -1;
else
return 0;
*/
} }
// Return number of selections and an array of selected integers // Return number of selections and an array of selected integers
// Use selections field to store data, which will be cleaned up int wxListBox::GetSelections(wxArrayInt& aSelections) const
// by destructor if necessary.
int wxListBox::GetSelections(int **list_selections) const
{ {
wxListBox *nonConst = (wxListBox *)this; // const is a white lie! aSelections.Empty();
if (nonConst->m_selections)
{ delete[] nonConst->m_selections; nonConst->m_selections = NULL; };
if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED))
{ {
int no_sel = (int)SendMessage(hwnd, LB_GETSELCOUNT, 0, 0); int no_sel = ListBox_GetSelCount(hwnd);
if (no_sel == 0) if (no_sel != 0) {
return 0; int *selections = new int[no_sel];
nonConst->m_selections = new int[no_sel]; if ( ListBox_GetSelItems(hwnd, no_sel, selections) == LB_ERR ) {
SendMessage(hwnd, LB_GETSELITEMS, no_sel, (LONG)m_selections); wxFAIL_MSG("This listbox can't have single-selection style!");
*list_selections = m_selections; }
aSelections.Alloc(no_sel);
for ( int n = 0; n < no_sel; n++ )
aSelections.Add(selections[n]);
delete [] selections;
}
return no_sel; return no_sel;
} }
else else // single-selection listbox
{ {
int sel = (int)SendMessage(hwnd, LB_GETCURSEL, 0, 0); aSelections.Add(ListBox_GetCurSel(hwnd));
if (sel == LB_ERR)
return 0;
nonConst->m_selections = new int[1];
nonConst->m_selections[0] = sel;
*list_selections = m_selections;
return 1; return 1;
} }
} }
// Get single selection, for single choice list items // Get single selection, for single choice list items
int wxListBox::GetSelection(void) const int wxListBox::GetSelection() const
{ {
wxListBox *nonConst = (wxListBox *)this; // const is a white lie! wxCHECK_MSG( !(m_windowStyle & wxLB_MULTIPLE) &&
if (nonConst->m_selections) !(m_windowStyle & wxLB_EXTENDED),
{ delete[] nonConst->m_selections; nonConst->m_selections = NULL; }; -1,
if ((m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED)) "GetSelection() can't be used with multiple-selection "
return -1; "listboxes, use GetSelections() instead." );
else
{ return ListBox_GetCurSel(hwnd);
int sel = (int)SendMessage(hwnd, LB_GETCURSEL, 0, 0);
if (sel == LB_ERR)
return -1;
else
{
return sel;
}
}
} }
// Find string for position // Find string for position
@@ -794,7 +781,7 @@ long wxListBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
{ {
// only owner-drawn control should receive this message // only owner-drawn control should receive this message
wxCHECK_RET( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item; MEASUREITEMSTRUCT *pStruct = (MEASUREITEMSTRUCT *)item;
@@ -812,13 +799,13 @@ bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item)
bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item) bool wxListBox::MSWOnDraw(WXDRAWITEMSTRUCT *item)
{ {
// only owner-drawn control should receive this message // only owner-drawn control should receive this message
wxCHECK_RET( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE ); wxCHECK( ((m_windowStyle & wxLB_OWNERDRAW) == wxLB_OWNERDRAW), FALSE );
DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item; DRAWITEMSTRUCT *pStruct = (DRAWITEMSTRUCT *)item;
wxListBoxItem *pItem = (wxListBoxItem *)SendMessage(hwnd, LB_GETITEMDATA, wxListBoxItem *pItem = (wxListBoxItem *)SendMessage(hwnd, LB_GETITEMDATA,
pStruct->itemID, 0); pStruct->itemID, 0);
wxCHECK_RET( (int)pItem != LB_ERR, FALSE ); wxCHECK( (int)pItem != LB_ERR, FALSE );
wxDC dc; wxDC dc;
dc.SetHDC((WXHDC)pStruct->hDC, FALSE); dc.SetHDC((WXHDC)pStruct->hDC, FALSE);