more convenient API for wxGenericValidator + wxLB_SINGLE

For a single-selection wxListBox, using an int to transfer
data to/from wxGenericValidator is more convenient than
wxArrayInt.
This commit is contained in:
Bill Su 2023-12-12 20:58:40 -05:00
parent ed02e03585
commit cfc5bc6ff2
2 changed files with 57 additions and 0 deletions

View file

@ -21,6 +21,9 @@
For example, wxButton and wxTextCtrl transfer data to and from a
wxString variable; wxListBox uses a wxArrayInt; wxCheckBox uses a boolean.
@since 3.2.5
A wxLB_SINGLE wxListBox can also use an int.
For more information, please see @ref overview_validator.
@library{wxcore}

View file

@ -377,6 +377,17 @@ bool wxGenericValidator::TransferToWindow()
return true;
}
else if (m_pInt)
{
wxCHECK_MSG(
!(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)),
false,
"multi-select control requires wxArrayInt"
);
pControl->Check(*m_pInt);
return true;
}
else
return false;
} else
@ -398,6 +409,17 @@ bool wxGenericValidator::TransferToWindow()
for ( i = 0 ; i < count; i++ )
pControl->SetSelection(m_pArrayInt->Item(i));
return true;
}
else if (m_pInt)
{
wxCHECK_MSG(
!(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)),
false,
"multi-select control requires wxArrayInt"
);
pControl->SetSelection(*m_pInt);
return true;
}
} else
@ -654,6 +676,26 @@ bool wxGenericValidator::TransferFromWindow()
return true;
}
else if (m_pInt)
{
wxCHECK_MSG(
!(pControl->GetWindowStyle()& (wxLB_MULTIPLE || wxLB_EXTENDED)),
false,
"multi-select control requires wxArrayInt"
);
size_t i,
count = pControl->GetCount();
for ( i = 0; i < count; i++ )
{
if (pControl->IsChecked(i))
{
*m_pInt = i;
}
}
return true;
}
else
return false;
} else
@ -676,6 +718,18 @@ bool wxGenericValidator::TransferFromWindow()
m_pArrayInt->Add(i);
}
return true;
}
else if (m_pInt)
{
wxCHECK_MSG(
!(pControl->GetWindowStyle() & (wxLB_MULTIPLE || wxLB_EXTENDED)),
false,
"multi-select control requires wxArrayInt"
);
*m_pInt = pControl->GetSelection();
return true;
}
} else