xmlres.cpp

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,711 行 · 第 1/4 页

CPP
1,711
字号
        }

        if (modif)
        {
            wxLogTrace(_T("xrc"),
                       _T("opening file '%s'"), m_data[i].File.c_str());

            wxInputStream *stream = NULL;

#           if wxUSE_FILESYSTEM
            file = fsys.OpenFile(m_data[i].File);
            if (file)
                stream = file->GetStream();
#           else
            stream = new wxFileInputStream(m_data[i].File);
#           endif

            if (stream)
            {
                delete m_data[i].Doc;
                m_data[i].Doc = new wxXmlDocument;
            }
            if (!stream || !m_data[i].Doc->Load(*stream, encoding))
            {
                wxLogError(_("Cannot load resources from file '%s'."),
                           m_data[i].File.c_str());
                wxDELETE(m_data[i].Doc);
                rt = false;
            }
            else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
            {
                wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
                wxDELETE(m_data[i].Doc);
                rt = false;
            }
            else
            {
                long version;
                int v1, v2, v3, v4;
                wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
                                      wxT("version"), wxT("0.0.0.0"));
                if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
                    &v1, &v2, &v3, &v4) == 4)
                    version = v1*256*256*256+v2*256*256+v3*256+v4;
                else
                    version = 0;
                if (m_version == -1)
                    m_version = version;
                if (m_version != version)
                {
                    wxLogError(_("Resource files must have same version number!"));
                    rt = false;
                }

                ProcessPlatformProperty(m_data[i].Doc->GetRoot());
#if wxUSE_FILESYSTEM
                m_data[i].Time = file->GetModificationTime();
#else
                m_data[i].Time = wxDateTime(wxFileModificationTime(m_data[i].File));
#endif
            }

#           if wxUSE_FILESYSTEM
                wxDELETE(file);
                wxUnusedVar(file);
#           else
                wxDELETE(stream);
#           endif
        }
    }

    return rt;
}


wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
                                         const wxString& name,
                                         const wxString& classname,
                                         bool recursive)
{
    wxString dummy;
    wxXmlNode *node;

    // first search for match at the top-level nodes (as this is
    // where the resource is most commonly looked for):
    for (node = parent->GetChildren(); node; node = node->GetNext())
    {
        if ( node->GetType() == wxXML_ELEMENT_NODE &&
                 (node->GetName() == wxT("object") ||
                  node->GetName() == wxT("object_ref")) &&
             node->GetPropVal(wxT("name"), &dummy) && dummy == name )
        {
            wxString cls(node->GetPropVal(wxT("class"), wxEmptyString));
            if (!classname || cls == classname)
                return node;
            // object_ref may not have 'class' property:
            if (cls.empty() && node->GetName() == wxT("object_ref"))
            {
                wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
                if (refName.empty())
                    continue;
                wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
                if (refNode &&
                    refNode->GetPropVal(wxT("class"), wxEmptyString) == classname)
                {
                    return node;
                }
            }
        }
    }

    if ( recursive )
        for (node = parent->GetChildren(); node; node = node->GetNext())
        {
            if ( node->GetType() == wxXML_ELEMENT_NODE &&
                 (node->GetName() == wxT("object") ||
                  node->GetName() == wxT("object_ref")) )
            {
                wxXmlNode* found = DoFindResource(node, name, classname, true);
                if ( found )
                    return found;
            }
        }

   return NULL;
}

wxXmlNode *wxXmlResource::FindResource(const wxString& name,
                                       const wxString& classname,
                                       bool recursive)
{
    UpdateResources(); //ensure everything is up-to-date

    wxString dummy;
    for (size_t f = 0; f < m_data.GetCount(); f++)
    {
        if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL )
            continue;

        wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(),
                                          name, classname, recursive);
        if ( found )
        {
#if wxUSE_FILESYSTEM
            m_curFileSystem.ChangePathTo(m_data[f].File);
#endif
            return found;
        }
    }

    wxLogError(_("XRC resource '%s' (class '%s') not found!"),
               name.c_str(), classname.c_str());
    return NULL;
}

static void MergeNodes(wxXmlNode& dest, wxXmlNode& with)
{
    // Merge properties:
    for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext())
    {
        wxXmlProperty *dprop;
        for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext())
        {

            if ( dprop->GetName() == prop->GetName() )
            {
                dprop->SetValue(prop->GetValue());
                break;
            }
        }

        if ( !dprop )
            dest.AddProperty(prop->GetName(), prop->GetValue());
   }

    // Merge child nodes:
    for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext())
    {
        wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
        wxXmlNode *dnode;

        for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
        {
            if ( dnode->GetName() == node->GetName() &&
                 dnode->GetPropVal(wxT("name"), wxEmptyString) == name &&
                 dnode->GetType() == node->GetType() )
            {
                MergeNodes(*dnode, *node);
                break;
            }
        }

        if ( !dnode )
            dest.AddChild(new wxXmlNode(*node));
    }

    if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().Length() )
         dest.SetContent(with.GetContent());
}

wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent,
                                           wxObject *instance,
                                           wxXmlResourceHandler *handlerToUse)
{
    if (node == NULL) return NULL;

    // handling of referenced resource
    if ( node->GetName() == wxT("object_ref") )
    {
        wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
        wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);

        if ( !refNode )
        {
            wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
                       refName.c_str());
            return NULL;
        }

        wxXmlNode copy(*refNode);
        MergeNodes(copy, *node);

        return CreateResFromNode(&copy, parent, instance);
    }

    wxXmlResourceHandler *handler;

    if (handlerToUse)
    {
        if (handlerToUse->CanHandle(node))
        {
            return handlerToUse->CreateResource(node, parent, instance);
        }
    }
    else if (node->GetName() == wxT("object"))
    {
        wxList::compatibility_iterator ND = m_handlers.GetFirst();
        while (ND)
        {
            handler = (wxXmlResourceHandler*)ND->GetData();
            if (handler->CanHandle(node))
            {
                return handler->CreateResource(node, parent, instance);
            }
            ND = ND->GetNext();
        }
    }

    wxLogError(_("No handler found for XML node '%s', class '%s'!"),
               node->GetName().c_str(),
               node->GetPropVal(wxT("class"), wxEmptyString).c_str());
    return NULL;
}


#include "wx/listimpl.cpp"
WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList);
WX_DEFINE_LIST(wxXmlSubclassFactoriesList);

wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL;

/*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
{
    if (!ms_subclassFactories)
    {
        ms_subclassFactories = new wxXmlSubclassFactoriesList;
    }
    ms_subclassFactories->Append(factory);
}

class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
{
public:
    ~wxXmlSubclassFactoryCXX() {}

    wxObject *Create(const wxString& className)
    {
        wxClassInfo* classInfo = wxClassInfo::FindClass(className);

        if (classInfo)
            return classInfo->CreateObject();
        else
            return NULL;
    }
};




wxXmlResourceHandler::wxXmlResourceHandler()
        : m_node(NULL), m_parent(NULL), m_instance(NULL),
          m_parentAsWindow(NULL)
{}



wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
{
    wxXmlNode *myNode = m_node;
    wxString myClass = m_class;
    wxObject *myParent = m_parent, *myInstance = m_instance;
    wxWindow *myParentAW = m_parentAsWindow;

    m_instance = instance;
    if (!m_instance && node->HasProp(wxT("subclass")) &&
        !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
    {
        wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString);
        if (!subclass.empty())
        {
            for (wxXmlSubclassFactoriesList::compatibility_iterator i = wxXmlResource::ms_subclassFactories->GetFirst();
                 i; i = i->GetNext())
            {
                m_instance = i->GetData()->Create(subclass);
                if (m_instance)
                    break;
            }

            if (!m_instance)
            {
                wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
                wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
                           subclass.c_str(), name.c_str());
            }
        }
    }

    m_node = node;
    m_class = node->GetPropVal(wxT("class"), wxEmptyString);
    m_parent = parent;
    m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);

    wxObject *returned = DoCreateResource();

    m_node = myNode;
    m_class = myClass;
    m_parent = myParent; m_parentAsWindow = myParentAW;
    m_instance = myInstance;

    return returned;
}


void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
{
    m_styleNames.Add(name);
    m_styleValues.Add(value);
}



void wxXmlResourceHandler::AddWindowStyles()
{
    XRC_ADD_STYLE(wxCLIP_CHILDREN);

    // the border styles all have the old and new names, recognize both for now
    XRC_ADD_STYLE(wxSIMPLE_BORDER); XRC_ADD_STYLE(wxBORDER_SIMPLE);
    XRC_ADD_STYLE(wxSUNKEN_BORDER); XRC_ADD_STYLE(wxBORDER_SUNKEN);
    XRC_ADD_STYLE(wxDOUBLE_BORDER); XRC_ADD_STYLE(wxBORDER_DOUBLE);
    XRC_ADD_STYLE(wxRAISED_BORDER); XRC_ADD_STYLE(wxBORDER_RAISED);
    XRC_ADD_STYLE(wxSTATIC_BORDER); XRC_ADD_STYLE(wxBORDER_STATIC);
    XRC_ADD_STYLE(wxNO_BORDER);     XRC_ADD_STYLE(wxBORDER_NONE);

    XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
    XRC_ADD_STYLE(wxWANTS_CHARS);
    XRC_ADD_STYLE(wxTAB_TRAVERSAL);
    XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
    XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE);
    XRC_ADD_STYLE(wxALWAYS_SHOW_SB);
    XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS);
    XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY);
    XRC_ADD_STYLE(wxALWAYS_SHOW_SB);
}



bool wxXmlResourceHandler::HasParam(const wxString& param)
{
    return (GetParamNode(param) != NULL);
}


int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
{
    wxString s = GetParamValue(param);

    if (!s) return defaults;

    wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
    int style = 0;
    int index;
    wxString fl;
    while (tkn.HasMoreTokens())
    {
        fl = tkn.GetNextToken();
        index = m_styleNames.Index(fl);
        if (index != wxNOT_FOUND)
            style |= m_styleValues[index];
        else
            wxLogError(_("Unknown style flag ") + fl);
    }
    return style;
}



wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
{
    wxXmlNode *parNode = GetParamNode(param);
    wxString str1(GetNodeContent(parNode));
    wxString str2;
    const wxChar *dt;
    wxChar amp_char;

    // VS: First version of XRC resources used $ instead of & (which is
    //     illegal in XML), but later I realized that '_' fits this purpose
    //     much better (because &File means "File with F underlined").
    if (m_resource->CompareVersion(2,3,0,1) < 0)
        amp_char = wxT('$');
    else
        amp_char = wxT('_');

    for (dt = str1.c_str(); *dt; dt++)
    {
        // Remap amp_char to &, map double amp_char to amp_char (for things
        // like "&File..." -- this is illegal in XML, so we use "_File..."):
        if (*dt == amp_char)
        {

⌨️ 快捷键说明

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