⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propgrid.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    // Make sure id is not already in use
    wxPGHashMapConstants::iterator it;
    it = socs->find(id);

    wxASSERT_MSG( it == socs->end(),
        wxT("id for this set of choices was already in use") );
#endif

#ifdef __WXDEBUG__
    soc->m_origValueArray = (const long*)&values;
#endif

    // Copy arrays
    soc->SetLabels(labels);
    if ( &values )
        soc->SetValues(values);

    // Cannot use SetId because we have zero refcount
    soc->SetupId(id);
    (*socs)[id] = soc;

    /*
    wxLogDebug(wxT("Added SOC %X - id: 0x%X (items = %i, first = %s)"),
        (unsigned int)soc,
        (unsigned int)id,
        (int)soc->GetCount(),
        soc->GetLabel(0).c_str());
    */

    soc->Ref();
    return soc;
}

// -----------------------------------------------------------------------

wxPGConstants* wxPropertyGrid::AddConstantsArray( wxPGConstants& constants )
{
    wxPGHashMapConstants* socs =
        (wxPGHashMapConstants*) wxPGGlobalVars->m_dictConstants;

    wxASSERT( constants.IsOk() );

    size_t id = constants.GetId();
    if ( id )
    {
        // We can't use constants directly since it may be a temporary.
        wxPGHashMapConstants::iterator it;
        it = socs->find(id);

        if ( it != socs->end() )
        {
            wxPGConstants* soc = it->second;
            wxASSERT( !soc->IsTemporary() );
            soc->Ref();
            return soc;
        }
    }

    // If its a temporary array (zero refcount), then create a permanent
    // version (zero id meant it didn't exist).
    wxASSERT( constants.IsTemporary() );

    wxPGConstants* soc = AddConstantsArray(constants.GetLabels(),
                                           constants.GetValues(),
                                           false);

    constants.SetId(soc->GetId());

    return soc;
}

// -----------------------------------------------------------------------

// Creates "permanent" wxPGConstants. NOTE: Creates with
// zero refcount, so Ref has to be called.
wxPGConstants* wxPropertyGrid::CreateConstantsArray(size_t id)
{
    wxPGHashMapConstants* socs =
        (wxPGHashMapConstants*) wxPGGlobalVars->m_dictConstants;

    wxPGConstants* soc = new wxPGConstants();
    if (!id)
        id = (size_t) soc;

    wxASSERT ( id );
    soc->SetupId(id);

#ifdef __WXDEBUG__
    soc->m_origValueArray = (const long*)&soc->GetValues();
#endif

    (*socs)[id] = soc;

    return soc;
}

// -----------------------------------------------------------------------

wxPGConstants* wxPropertyGrid::GetConstantsArray(size_t id)
{
    wxPGHashMapConstants* socs =
        (wxPGHashMapConstants*) wxPGGlobalVars->m_dictConstants;

    wxPGHashMapConstants::iterator it;
    it = socs->find(id);
    if ( it != socs->end() )
        return (wxPGConstants*) it->second;
    return (wxPGConstants*) NULL;
}

// -----------------------------------------------------------------------

void wxPropertyContainerMethods::SetPropertyChoices(wxPGId id, wxPGConstants& choices)
{
    wxPGProperty* p = wxPGIdToPtr(id);
    wxASSERT( p );

    wxPGChoiceInfo ci;
    ci.m_constants = (wxPGConstants**) NULL;

    p->GetChoiceInfo(&ci);

    //wxASSERT_MSG( ci.m_constants, wxT("this property does not have choices") );

    if ( ci.m_constants )
    {
        wxPGConstants* cons = *ci.m_constants;
        wxPGUnRefChoices( cons );

        *ci.m_constants = wxPropertyGrid::AddConstantsArray(choices);
    }
}

// -----------------------------------------------------------------------

void wxPropertyContainerMethods::AddPropertyChoice(wxPGId id,
                                                   const wxString& label,
                                                   int value )
{
    wxASSERT (wxPGIdToPtr(id));

    wxPGConstants& cons = GetPropertyChoices(id);

    // If no real choices yet, then we need to setup them
    if ( !cons.IsOk() )
    {
        wxArrayString labels;
        wxArrayInt values;
        labels.Add(label);
        if ( value != wxPG_INVALID_VALUE )
            values.Add(value);

        // This will, of course, fail if property cannot have choices
        wxPGIdToPtr(id)->SetChoices(0,labels,values);
    }
    else
        cons.Add(label,value);
}

// -----------------------------------------------------------------------
// wxBaseEnumPropertyClass
// -----------------------------------------------------------------------

// Class body is in propdev.h.

wxBaseEnumPropertyClass::wxBaseEnumPropertyClass( const wxString& label, const wxString& name )
    : wxPGProperty(label,name)
{
}

/** If has values array, then returns number at index with value -
    otherwise just returns the value.
*/
int wxBaseEnumPropertyClass::GetIndexForValue( int value ) const
{
    return value;
}

void wxBaseEnumPropertyClass::DoSetValue ( wxPGVariant value )
{
    int intval = (int) value.GetLong();
    m_index = GetIndexForValue(intval);
}

wxPGVariant wxBaseEnumPropertyClass::DoGetValue () const
{
    if ( m_index < 0 )
        return wxPGVariant((long)-1);

    int val;
    GetEntry(m_index,&val);

    return val;
}

wxString wxBaseEnumPropertyClass::GetValueAsString ( int ) const
{
    if ( m_index >= 0 )
    {
        int unused_val;
        const wxString* pstr = GetEntry( m_index, &unused_val );

        if ( pstr )
            return *pstr;
    }
    return wxEmptyString;
}

bool wxBaseEnumPropertyClass::SetValueFromString ( const wxString& text, int WXUNUSED(arg_flags) )
{
    size_t i = 0;
    const wxString* entry_label;
    int entry_value;
    int use_index = -1;
    long use_value = 0;

    entry_label = GetEntry(i,&entry_value);
    while ( entry_label )
    {
        //wxLogDebug(wxT("entry_label: %s"),entry_label->c_str());
        if ( text.CmpNoCase(*entry_label) == 0 )
        {
            use_index = (int)i;
            use_value = (long)entry_value;
            break;
        }

        i++;
        entry_label = GetEntry(i,&entry_value);
    }

    if ( m_index != use_index )
    {
        // FIXME: Why can't this be virtual call?
        wxBaseEnumPropertyClass::DoSetValue ( use_value );

        return TRUE;
    }
    /*}
    else if ( arg_flags & wxPG_REPORT_ERROR )
    {
        wxString s;
        s.Printf ( wxT("\"%s\" was not among valid choices."), text.c_str() );
        ShowError(s);
    }*/
    return FALSE;
}

bool wxBaseEnumPropertyClass::SetValueFromInt ( long value, int arg_flags )
{
    if ( arg_flags & wxPG_FULL_VALUE )
    {
        DoSetValue(value);
        return TRUE;
    }
    else
    {
        if ( m_index != value )
        {
            m_index = value;
            return TRUE;
        }
    }
    return FALSE;
}

// -----------------------------------------------------------------------
// wxEnumProperty
// -----------------------------------------------------------------------

// Class body is in propdev.h.

wxPGProperty* wxEnumProperty ( const wxString& label, const wxString& name, const wxChar** labels,
    const long* values, unsigned int itemcount, int value )
{
    return new wxEnumPropertyClass (label,name,labels,values,itemcount,value);
}

wxPGProperty* wxEnumProperty ( const wxString& label, const wxString& name, 
    wxPGConstants& constants, int value )
{
    return new wxEnumPropertyClass (label,name,constants,value);
}

WX_PG_IMPLEMENT_CLASSINFO(wxEnumProperty)

WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxEnumProperty,long,Choice)

wxEnumPropertyClass::wxEnumPropertyClass ( const wxString& label, const wxString& name, const wxChar** labels,
    const long* values, unsigned int itemcount, int value ) : wxBaseEnumPropertyClass(label,name)
{
    m_constants = (wxPGConstants*) NULL;
    m_index = 0;

    if ( labels )
    {
        m_constants = wxPropertyGrid::AddConstantsArray(labels,values,itemcount);

        if ( GetItemCount() )
            wxEnumPropertyClass::DoSetValue( value );
    }
    else
        m_constants = &wxPGGlobalVars->m_emptyConstants;
}

wxEnumPropertyClass::wxEnumPropertyClass ( const wxString& label, const wxString& name, 
    wxPGConstants& constants, int value )
    : wxBaseEnumPropertyClass(label,name)
{
    //m_itemCount = constants.GetCount();
    //constants.RealizeArrays(&m_arrLabels,&m_arrValues);

    m_constants = wxPropertyGrid::AddConstantsArray(constants);

    if ( GetItemCount() )
        wxEnumPropertyClass::DoSetValue( value );
}

int wxEnumPropertyClass::GetIndexForValue( int value ) const
{
    const wxArrayInt& arrValues = m_constants->GetValues();

    if ( arrValues.GetCount() )
    {
        int intval = arrValues.Index(value);

        // TODO: Use real default instead of 0.
        if ( intval < 0 )
            intval = 0;

        return intval;
    }
    return value;
}

wxEnumPropertyClass::~wxEnumPropertyClass () 
{
    wxPGUnRefChoices(m_constants);
}

const wxString* wxEnumPropertyClass::GetEntry( size_t index, int* pvalue ) const
{
    if ( index < m_constants->GetCount() )
    {
        const wxArrayInt& arrValues = m_constants->GetValues();

        int value = (int)index;
        if ( arrValues.GetCount() )
            value = arrValues[index];

        *pvalue = value;

        return &m_constants->GetLabel(index);
    }
    return (const wxString*) NULL;
}

int wxEnumPropertyClass::GetChoiceInfo ( wxPGChoiceInfo* choiceinfo )
{
    if ( choiceinfo )
    {
        choiceinfo->m_itemCount = m_constants->GetCount();
        if ( m_constants->GetCount() )
            choiceinfo->m_arrWxString = (wxString*)&m_constants->GetLabel(0);
        if ( !(m_flags & wxPG_PROP_STATIC_CHOICES) )
            choiceinfo->m_constants = &m_constants;
    }
    return m_index;
}

// -----------------------------------------------------------------------
// wxFlagsProperty
// -----------------------------------------------------------------------

// Class body is in propdev.h.

wxPGProperty* wxFlagsProperty( const wxString& label, const wxString& name, const wxChar** labels,
    const long* values, unsigned int itemcount, int value )
{
    return new wxFlagsPropertyClass (label,name,labels,values,itemcount,value);
}

wxPGProperty* wxFlagsProperty( const wxString& label, const wxString& name, 
    wxPGConstants& constants, int value )
{
    return new wxFlagsPropertyClass (label,name,constants,value);
}

WX_PG_IMPLEMENT_CLASSINFO(wxFlagsProperty)

WX_PG_IMPLEMENT_PROPERTY_CLASS_PLAIN(wxFlagsProperty,long,TextCtrl)

void wxFlagsPropertyClass::Init ()
{
    //wxFlagsPropertyClass::DoSetValue( value );

    long value = m_value;

    //
    // Generate children
    //
    unsigned int i;

    // Delete old children
    for ( i=0; i<m_children.GetCount(); i++ )
        delete ( (wxPGProperty*) m_children[i] );

    const wxArrayInt& values = GetValues();

    for ( i=0; i<GetItemCount(); i++ )
    {
        bool child_val;
        if ( values.GetCount() )
            child_val = ( value & values[i] )?TRUE:FALSE;
        else
            child_val = ( value & (1<<i) )?TRUE:FALSE;

        wxPGProperty* bool_prop;

    #if wxUSE_INTL
        if ( wxPGGlobalVars->m_autoGetTranslation )
        {
            bool_prop = wxBoolProperty( ::wxGetTranslation ( GetLabel(i) ), wxEmptyString, child_val );
        }
        else
    #endif
        {
            bool_prop = wxBoolProperty( GetLabel(i), wxEmptyString, child_val );
        }
        AddChild(bool_prop);
    }
}

wxFlagsPropertyClass::wxFlagsPropertyClass ( const wxString& label, const wxString& name,
    const wxChar** labels, const long* values, unsigned int itemcount,
    long value ) : wxPGPropertyWithChildren(label,name)
{

    //m_constants = (wxPGConstants*) NULL;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -