Added proper COW to wxPGChoices, moved wxPGChoices code from propgrid.cpp to property.cpp (to match header organization), removed some now-unneeded helper functions

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@58651 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Jaakko Salli 2009-02-04 16:45:23 +00:00
parent d818458781
commit 2728c3bfe7
6 changed files with 274 additions and 284 deletions

View File

@ -873,11 +873,7 @@ public:
void AssignData( wxPGChoicesData* data );
/** Delete all choices. */
void Clear()
{
if ( m_data != wxPGChoicesEmptyData )
m_data->Clear();
}
void Clear();
/**
Returns a real copy of the choices.
@ -980,16 +976,7 @@ public:
}
// Creates exclusive copy of current choices
void SetExclusive()
{
if ( m_data->m_refCount != 1 )
{
wxPGChoicesData* data = new wxPGChoicesData();
data->CopyDataFrom(m_data);
Free();
m_data = data;
}
}
void AllocExclusive();
// Returns data, increases refcount.
wxPGChoicesData* GetData()
@ -1922,14 +1909,6 @@ public:
*/
void SetValueImage( wxBitmap& bmp );
/** If property has choices and they are not yet exclusive, new such copy
of them will be created.
*/
void SetChoicesExclusive()
{
m_choices.SetExclusive();
}
/** Sets selected choice and changes property value.
Tries to retain value type, although currently if it is not string,

View File

@ -1273,12 +1273,6 @@ public:
*/
bool SetChoices( wxPGChoices& choices );
/**
If property has choices and they are not yet exclusive, new such copy
of them will be created.
*/
void SetChoicesExclusive();
/**
Sets client data (void*) of a property.
@ -1599,7 +1593,7 @@ public:
/**
Creates exclusive copy of current choices.
*/
void SetExclusive();
void AllocExclusive();
/**
Returns array of choice labels.

View File

@ -1494,27 +1494,17 @@ void FormMain::PopulateWithExamples ()
240) );
pg->GetProperty(wxT("EnumProperty 2"))->AddChoice(wxT("Testing Extra"), 360);
// Add a second time to test that the caching works. Also use
// short form of constructor list + SetChoices.
prop = new wxEnumProperty(wxT("EnumProperty 3"), wxPG_LABEL);
pg->Append( prop );
prop->SetChoices(soc);
prop->SetValue(360);
pg->SetPropertyHelpString(prop,
wxT("Should have same choices as EnumProperty 2"));
// Here we only display the original 'soc' choices
pg->Append( new wxEnumProperty(wxT("EnumProperty 3"),wxPG_LABEL,
soc, 240 ) );
// 'soc' plus one exclusive extra choice "4th only"
pg->Append( new wxEnumProperty(wxT("EnumProperty 4"),wxPG_LABEL,
soc, 240 ) );
pg->GetProperty(wxT("EnumProperty 4"))->AddChoice(wxT("4th only"), 360);
pg->SetPropertyHelpString(wxT("EnumProperty 4"),
wxT("Should have same choices as EnumProperty 2"));
pg->Append( new wxEnumProperty(wxT("EnumProperty 5"),wxPG_LABEL,
soc, 240 ) );
pg->GetProperty(wxT("EnumProperty 5"))->SetChoicesExclusive();
pg->GetProperty(wxT("EnumProperty 5"))->AddChoice(wxT("5th only"), 360);
pg->SetPropertyHelpString(wxT("EnumProperty 5"),
wxT("Should have one extra item when compared to EnumProperty 4"));
wxT("Should have one extra item when compared to EnumProperty 3"));
// Password property example.
pg->Append( new wxStringProperty(wxT("Password"),wxPG_LABEL, wxT("password")) );

View File

@ -1423,8 +1423,6 @@ bool wxSystemColourProperty::DoSetAttribute( const wxString& name, wxVariant& va
{
int ival = wxPGVariantToInt(value);
SetChoicesExclusive(); // Make sure we don't corrupt colour lists of other properties
if ( ival && (m_flags & wxPG_PROP_HIDE_CUSTOM_COLOUR) )
{
// Show custom choice

View File

@ -2505,6 +2505,270 @@ void wxPropertyCategory::CalculateTextExtent( wxWindow* wnd, const wxFont& font
m_textExtent = x;
}
// -----------------------------------------------------------------------
// wxPGChoices
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, int value )
{
AllocExclusive();
wxPGChoiceEntry entry(label, value);
return m_data->Insert( -1, entry );
}
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, const wxBitmap& bitmap, int value )
{
AllocExclusive();
wxPGChoiceEntry entry(label, value);
entry.SetBitmap(bitmap);
return m_data->Insert( -1, entry );
}
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Insert( const wxPGChoiceEntry& entry, int index )
{
AllocExclusive();
return m_data->Insert( index, entry );
}
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Insert( const wxString& label, int index, int value )
{
AllocExclusive();
wxPGChoiceEntry entry(label, value);
return m_data->Insert( index, entry );
}
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::AddAsSorted( const wxString& label, int value )
{
AllocExclusive();
size_t index = 0;
while ( index < GetCount() )
{
int cmpRes = GetLabel(index).Cmp(label);
if ( cmpRes > 0 )
break;
index++;
}
wxPGChoiceEntry entry(label, value);
return m_data->Insert( index, entry );
}
// -----------------------------------------------------------------------
void wxPGChoices::Add( const wxChar** labels, const ValArrItem* values )
{
AllocExclusive();
unsigned int itemcount = 0;
const wxChar** p = &labels[0];
while ( *p ) { p++; itemcount++; }
unsigned int i;
for ( i = 0; i < itemcount; i++ )
{
int value = i;
if ( values )
value = values[i];
wxPGChoiceEntry entry(labels[i], value);
m_data->Insert( i, entry );
}
}
// -----------------------------------------------------------------------
void wxPGChoices::Add( const wxArrayString& arr, const wxArrayInt& arrint )
{
AllocExclusive();
unsigned int i;
unsigned int itemcount = arr.size();
for ( i = 0; i < itemcount; i++ )
{
int value = i;
if ( &arrint && arrint.size() )
value = arrint[i];
wxPGChoiceEntry entry(arr[i], value);
m_data->Insert( i, entry );
}
}
// -----------------------------------------------------------------------
void wxPGChoices::RemoveAt(size_t nIndex, size_t count)
{
AllocExclusive();
wxASSERT( m_data->m_refCount != 0xFFFFFFF );
m_data->m_items.erase(m_data->m_items.begin()+nIndex,
m_data->m_items.begin()+nIndex+count);
}
// -----------------------------------------------------------------------
void wxPGChoices::Clear()
{
if ( m_data != wxPGChoicesEmptyData )
{
AllocExclusive();
m_data->Clear();
}
}
// -----------------------------------------------------------------------
int wxPGChoices::Index( const wxString& str ) const
{
if ( IsOk() )
{
unsigned int i;
for ( i=0; i< m_data->GetCount(); i++ )
{
const wxPGChoiceEntry& entry = m_data->Item(i);
if ( entry.HasText() && entry.GetText() == str )
return i;
}
}
return -1;
}
// -----------------------------------------------------------------------
int wxPGChoices::Index( int val ) const
{
if ( IsOk() )
{
unsigned int i;
for ( i=0; i< m_data->GetCount(); i++ )
{
const wxPGChoiceEntry& entry = m_data->Item(i);
if ( entry.GetValue() == val )
return i;
}
}
return -1;
}
// -----------------------------------------------------------------------
wxArrayString wxPGChoices::GetLabels() const
{
wxArrayString arr;
unsigned int i;
if ( this && IsOk() )
for ( i=0; i<GetCount(); i++ )
arr.push_back(GetLabel(i));
return arr;
}
// -----------------------------------------------------------------------
wxArrayInt wxPGChoices::GetValuesForStrings( const wxArrayString& strings ) const
{
wxArrayInt arr;
if ( IsOk() )
{
unsigned int i;
for ( i=0; i< strings.size(); i++ )
{
int index = Index(strings[i]);
if ( index >= 0 )
arr.Add(GetValue(index));
else
arr.Add(wxPG_INVALID_VALUE);
}
}
return arr;
}
// -----------------------------------------------------------------------
wxArrayInt wxPGChoices::GetIndicesForStrings( const wxArrayString& strings,
wxArrayString* unmatched ) const
{
wxArrayInt arr;
if ( IsOk() )
{
unsigned int i;
for ( i=0; i< strings.size(); i++ )
{
const wxString& str = strings[i];
int index = Index(str);
if ( index >= 0 )
arr.Add(index);
else if ( unmatched )
unmatched->Add(str);
}
}
return arr;
}
// -----------------------------------------------------------------------
void wxPGChoices::AllocExclusive()
{
EnsureData();
if ( m_data->m_refCount != 1 )
{
wxPGChoicesData* data = new wxPGChoicesData();
data->CopyDataFrom(m_data);
Free();
m_data = data;
}
}
// -----------------------------------------------------------------------
void wxPGChoices::AssignData( wxPGChoicesData* data )
{
Free();
if ( data != wxPGChoicesEmptyData )
{
m_data = data;
data->m_refCount++;
}
}
// -----------------------------------------------------------------------
void wxPGChoices::Init()
{
m_data = wxPGChoicesEmptyData;
}
// -----------------------------------------------------------------------
void wxPGChoices::Free()
{
if ( m_data != wxPGChoicesEmptyData )
{
m_data->DecRef();
m_data = wxPGChoicesEmptyData;
}
}
// -----------------------------------------------------------------------
// wxPGAttributeStorage
// -----------------------------------------------------------------------

View File

@ -5287,241 +5287,6 @@ wxPGChoiceEntry& wxPGChoicesData::Insert( int index,
return ownEntry;
}
// -----------------------------------------------------------------------
// wxPGChoices
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, int value )
{
EnsureData();
wxPGChoiceEntry entry(label, value);
return m_data->Insert( -1, entry );
}
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Add( const wxString& label, const wxBitmap& bitmap, int value )
{
EnsureData();
wxPGChoiceEntry entry(label, value);
entry.SetBitmap(bitmap);
return m_data->Insert( -1, entry );
}
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Insert( const wxPGChoiceEntry& entry, int index )
{
EnsureData();
return m_data->Insert( index, entry );
}
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::Insert( const wxString& label, int index, int value )
{
EnsureData();
wxPGChoiceEntry entry(label, value);
return m_data->Insert( index, entry );
}
// -----------------------------------------------------------------------
wxPGChoiceEntry& wxPGChoices::AddAsSorted( const wxString& label, int value )
{
EnsureData();
size_t index = 0;
while ( index < GetCount() )
{
int cmpRes = GetLabel(index).Cmp(label);
if ( cmpRes > 0 )
break;
index++;
}
wxPGChoiceEntry entry(label, value);
return m_data->Insert( index, entry );
}
// -----------------------------------------------------------------------
void wxPGChoices::Add( const wxChar** labels, const ValArrItem* values )
{
EnsureData();
unsigned int itemcount = 0;
const wxChar** p = &labels[0];
while ( *p ) { p++; itemcount++; }
unsigned int i;
for ( i = 0; i < itemcount; i++ )
{
int value = i;
if ( values )
value = values[i];
wxPGChoiceEntry entry(labels[i], value);
m_data->Insert( i, entry );
}
}
// -----------------------------------------------------------------------
void wxPGChoices::Add( const wxArrayString& arr, const wxArrayInt& arrint )
{
EnsureData();
unsigned int i;
unsigned int itemcount = arr.size();
for ( i = 0; i < itemcount; i++ )
{
int value = i;
if ( &arrint && arrint.size() )
value = arrint[i];
wxPGChoiceEntry entry(arr[i], value);
m_data->Insert( i, entry );
}
}
// -----------------------------------------------------------------------
void wxPGChoices::RemoveAt(size_t nIndex, size_t count)
{
wxASSERT( m_data->m_refCount != 0xFFFFFFF );
m_data->m_items.erase(m_data->m_items.begin()+nIndex,
m_data->m_items.begin()+nIndex+count);
}
// -----------------------------------------------------------------------
int wxPGChoices::Index( const wxString& str ) const
{
if ( IsOk() )
{
unsigned int i;
for ( i=0; i< m_data->GetCount(); i++ )
{
const wxPGChoiceEntry& entry = m_data->Item(i);
if ( entry.HasText() && entry.GetText() == str )
return i;
}
}
return -1;
}
// -----------------------------------------------------------------------
int wxPGChoices::Index( int val ) const
{
if ( IsOk() )
{
unsigned int i;
for ( i=0; i< m_data->GetCount(); i++ )
{
const wxPGChoiceEntry& entry = m_data->Item(i);
if ( entry.GetValue() == val )
return i;
}
}
return -1;
}
// -----------------------------------------------------------------------
wxArrayString wxPGChoices::GetLabels() const
{
wxArrayString arr;
unsigned int i;
if ( this && IsOk() )
for ( i=0; i<GetCount(); i++ )
arr.push_back(GetLabel(i));
return arr;
}
// -----------------------------------------------------------------------
wxArrayInt wxPGChoices::GetValuesForStrings( const wxArrayString& strings ) const
{
wxArrayInt arr;
if ( IsOk() )
{
unsigned int i;
for ( i=0; i< strings.size(); i++ )
{
int index = Index(strings[i]);
if ( index >= 0 )
arr.Add(GetValue(index));
else
arr.Add(wxPG_INVALID_VALUE);
}
}
return arr;
}
// -----------------------------------------------------------------------
wxArrayInt wxPGChoices::GetIndicesForStrings( const wxArrayString& strings,
wxArrayString* unmatched ) const
{
wxArrayInt arr;
if ( IsOk() )
{
unsigned int i;
for ( i=0; i< strings.size(); i++ )
{
const wxString& str = strings[i];
int index = Index(str);
if ( index >= 0 )
arr.Add(index);
else if ( unmatched )
unmatched->Add(str);
}
}
return arr;
}
// -----------------------------------------------------------------------
void wxPGChoices::AssignData( wxPGChoicesData* data )
{
Free();
if ( data != wxPGChoicesEmptyData )
{
m_data = data;
data->m_refCount++;
}
}
// -----------------------------------------------------------------------
void wxPGChoices::Init()
{
m_data = wxPGChoicesEmptyData;
}
// -----------------------------------------------------------------------
void wxPGChoices::Free()
{
if ( m_data != wxPGChoicesEmptyData )
{
m_data->DecRef();
m_data = wxPGChoicesEmptyData;
}
}
// -----------------------------------------------------------------------
// wxPropertyGridEvent
// -----------------------------------------------------------------------