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

📄 resource.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/////////////////////////////////////////////////////////////////////////////
// Name:        contrib/src/deprecated/resource.cpp
// Purpose:     Resource system
// Author:      Julian Smart
// Modified by:
// Created:     04/01/98
// RCS-ID:      $Id: resource.cpp,v 1.22 2006/04/14 19:54:51 ABX Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include "wx/deprecated/setup.h"

#if wxUSE_WX_RESOURCES

#ifdef __VISUALC__
#pragma warning(disable:4706)   // assignment within conditional expression
#endif // VC++

#ifndef WX_PRECOMP
    #include "wx/list.h"
    #include "wx/hash.h"
    #include "wx/gdicmn.h"
    #include "wx/utils.h"
    #include "wx/types.h"
    #include "wx/menu.h"
    #include "wx/stattext.h"
    #include "wx/button.h"
    #include "wx/bmpbuttn.h"
    #include "wx/radiobox.h"
    #include "wx/listbox.h"
    #include "wx/choice.h"
    #include "wx/checkbox.h"
    #include "wx/settings.h"
    #include "wx/slider.h"
    #include "wx/icon.h"
    #include "wx/statbox.h"
    #include "wx/statbmp.h"
    #include "wx/gauge.h"
    #include "wx/textctrl.h"
    #include "wx/msgdlg.h"
    #include "wx/intl.h"
#endif

#include "wx/treebase.h"
#include "wx/listctrl.h"

#if wxUSE_RADIOBTN
#include "wx/radiobut.h"
#endif

#if wxUSE_SCROLLBAR
#include "wx/scrolbar.h"
#endif

#if wxUSE_COMBOBOX
#include "wx/combobox.h"
#endif

#include "wx/splitter.h"
#include "wx/toolbar.h"

#include "wx/validate.h"
#include "wx/log.h"
#include "wx/module.h"

#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "wx/string.h"
#include "wx/settings.h"
#include "wx/stream.h"

#include "wx/deprecated/resource.h"
#include "wx/deprecated/wxexpr.h"

#if !WXWIN_COMPATIBILITY_2_4
static inline wxChar* copystring(const wxChar* s)
    { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); }
#endif

// Forward (private) declarations
bool wxResourceInterpretResources(wxResourceTable& table, wxExprDatabase& db);
wxItemResource *wxResourceInterpretDialog(wxResourceTable& table, wxExpr *expr, bool isPanel = false);
wxItemResource *wxResourceInterpretControl(wxResourceTable& table, wxExpr *expr);
wxItemResource *wxResourceInterpretMenu(wxResourceTable& table, wxExpr *expr);
wxItemResource *wxResourceInterpretMenuBar(wxResourceTable& table, wxExpr *expr);
wxItemResource *wxResourceInterpretString(wxResourceTable& table, wxExpr *expr);
wxItemResource *wxResourceInterpretBitmap(wxResourceTable& table, wxExpr *expr);
wxItemResource *wxResourceInterpretIcon(wxResourceTable& table, wxExpr *expr);
// Interpret list expression
wxFont wxResourceInterpretFontSpec(wxExpr *expr);

bool wxResourceReadOneResource(FILE *fd, wxExprDatabase& db, bool *eof, wxResourceTable *table = (wxResourceTable *) NULL);
bool wxResourceReadOneResource(wxInputStream *fd, wxExprDatabase& db, bool *eof, wxResourceTable *table) ;
bool wxResourceParseIncludeFile(const wxString& f, wxResourceTable *table = (wxResourceTable *) NULL);

wxResourceTable *wxDefaultResourceTable = (wxResourceTable *) NULL;

char *wxResourceBuffer = (char *) NULL;
long wxResourceBufferSize = 0;
long wxResourceBufferCount = 0;
int wxResourceStringPtr = 0;

void wxInitializeResourceSystem()
{
    if (!wxDefaultResourceTable)
        wxDefaultResourceTable = new wxResourceTable;
}

void wxCleanUpResourceSystem()
{
    delete wxDefaultResourceTable;
    if (wxResourceBuffer)
        delete[] wxResourceBuffer;
}

// Module to ensure the resource system data gets initialized
// and cleaned up.

class wxResourceModule: public wxModule
{
public:
    wxResourceModule() : wxModule() {}
    virtual bool OnInit() { wxInitializeResourceSystem(); return true; }
    virtual void OnExit() { wxCleanUpResourceSystem();  }

    DECLARE_DYNAMIC_CLASS(wxResourceModule)
};

IMPLEMENT_DYNAMIC_CLASS(wxResourceModule, wxModule)


IMPLEMENT_DYNAMIC_CLASS(wxItemResource, wxObject)
IMPLEMENT_DYNAMIC_CLASS(wxResourceTable, wxHashTable)

wxItemResource::wxItemResource()
{
    m_itemType = wxEmptyString;
    m_title = wxEmptyString;
    m_name = wxEmptyString;
    m_windowStyle = 0;
    m_x = m_y = m_width = m_height = 0;
    m_value1 = m_value2 = m_value3 = m_value5 = 0;
    m_value4 = wxEmptyString;
    m_windowId = 0;
    m_exStyle = 0;
}

wxItemResource::~wxItemResource()
{
    wxNode *node = m_children.GetFirst();
    while (node)
    {
        wxItemResource *item = (wxItemResource *)node->GetData();
        delete item;
        delete node;
        node = m_children.GetFirst();
    }
}

/*
* Resource table
*/

wxResourceTable::wxResourceTable():wxHashTable(wxKEY_STRING), identifiers(wxKEY_STRING)
{
}

wxResourceTable::~wxResourceTable()
{
    ClearTable();
}

wxItemResource *wxResourceTable::FindResource(const wxString& name) const
{
    wxItemResource *item = (wxItemResource *)Get(WXSTRINGCAST name);
    return item;
}

void wxResourceTable::AddResource(wxItemResource *item)
{
    wxString name = item->GetName();
    if (name.empty())
        name = item->GetTitle();
    if (name.empty())
        name = wxT("no name");

    // Delete existing resource, if any.
    Delete(name);

    Put(name, item);
}

bool wxResourceTable::DeleteResource(const wxString& name)
{
    wxItemResource *item = (wxItemResource *)Delete(WXSTRINGCAST name);
    if (item)
    {
        // See if any resource has this as its child; if so, delete from
        // parent's child list.
        BeginFind();
        wxHashTable::Node *node = Next();
        while (node != NULL)
        {
            wxItemResource *parent = (wxItemResource *)node->GetData();
            if (parent->GetChildren().Member(item))
            {
                parent->GetChildren().DeleteObject(item);
                break;
            }
            node = Next();
        }

        delete item;
        return true;
    }
    else
        return false;
}

bool wxResourceTable::ParseResourceFile( wxInputStream *is )
{
    wxExprDatabase db;
    int len = is->GetSize() ;

    bool eof = false;
    while ( is->TellI() + 10 < len) // it's a hack because the streams dont support EOF
    {
        wxResourceReadOneResource(is, db, &eof, this) ;
    }
    return wxResourceInterpretResources(*this, db);
}

bool wxResourceTable::ParseResourceFile(const wxString& filename)
{
    wxExprDatabase db;

    FILE *fd = wxFopen(filename, wxT("r"));
    if (!fd)
        return false;
    bool eof = false;
    while (wxResourceReadOneResource(fd, db, &eof, this) && !eof)
    {
        // Loop
    }
    fclose(fd);
    return wxResourceInterpretResources(*this, db);
}

bool wxResourceTable::ParseResourceData(const wxString& data)
{
    wxExprDatabase db;
    if (!db.ReadFromString(data))
    {
        wxLogWarning(_("Ill-formed resource file syntax."));
        return false;
    }

    return wxResourceInterpretResources(*this, db);
}

bool wxResourceTable::RegisterResourceBitmapData(const wxString& name, char bits[], int width, int height)
{
    // Register pre-loaded bitmap data
    wxItemResource *item = new wxItemResource;
    //  item->SetType(wxRESOURCE_TYPE_XBM_DATA);
    item->SetType(wxT("wxXBMData"));
    item->SetName(name);
    item->SetValue1((long)bits);
    item->SetValue2((long)width);
    item->SetValue3((long)height);
    AddResource(item);
    return true;
}

bool wxResourceTable::RegisterResourceBitmapData(const wxString& name, char **data)
{
    // Register pre-loaded bitmap data
    wxItemResource *item = new wxItemResource;
    //  item->SetType(wxRESOURCE_TYPE_XPM_DATA);
    item->SetType(wxT("wxXPMData"));
    item->SetName(name);
    item->SetValue1((long)data);
    AddResource(item);
    return true;
}

bool wxResourceTable::SaveResource(const wxString& WXUNUSED(filename))
{
    return false;
}

void wxResourceTable::ClearTable()
{
    BeginFind();
    wxHashTable::Node *node = Next();
    while (node)
    {
        wxHashTable::Node *next = Next();
        wxItemResource *item = (wxItemResource *)node->GetData();
        delete item;
        delete node;
        node = next;
    }
}

wxControl *wxResourceTable::CreateItem(wxWindow *parent, const wxItemResource* childResource, const wxItemResource* parentResource) const
{
    int id = childResource->GetId();
    if ( id == 0 )
        id = wxID_ANY;

    bool dlgUnits = ((parentResource->GetResourceStyle() & wxRESOURCE_DIALOG_UNITS) != 0);

    wxControl *control = (wxControl *) NULL;
    wxString itemType(childResource->GetType());

    wxPoint pos;
    wxSize size;
    if (dlgUnits)
    {
        pos = parent->ConvertDialogToPixels(wxPoint(childResource->GetX(), childResource->GetY()));
        size = parent->ConvertDialogToPixels(wxSize(childResource->GetWidth(), childResource->GetHeight()));
    }
    else
    {
        pos = wxPoint(childResource->GetX(), childResource->GetY());
        size = wxSize(childResource->GetWidth(), childResource->GetHeight());
    }

    if (itemType == wxString(wxT("wxButton")) || itemType == wxString(wxT("wxBitmapButton")))
    {
        if (!childResource->GetValue4().empty())
        {
            // Bitmap button
            wxBitmap bitmap = childResource->GetBitmap();
            if (!bitmap.Ok())
            {
                bitmap = wxResourceCreateBitmap(childResource->GetValue4(), (wxResourceTable *)this);
                ((wxItemResource*) childResource)->SetBitmap(bitmap);
            }
            if (!bitmap.Ok())
#if defined(__WXPM__)
                //
                // OS/2 uses integer id's to access resources, not file name strings
                //
                bitmap.LoadFile(wxCROSS_BITMAP, wxBITMAP_TYPE_BMP_RESOURCE);
#else
                bitmap.LoadFile(wxT("cross_bmp"), wxBITMAP_TYPE_BMP_RESOURCE);
#endif
            control = new wxBitmapButton(parent, id, bitmap, pos, size,
                childResource->GetStyle() | wxBU_AUTODRAW, wxDefaultValidator, childResource->GetName());
        }
        else
            // Normal, text button
            control = new wxButton(parent, id, childResource->GetTitle(), pos, size,
            childResource->GetStyle(), wxDefaultValidator, childResource->GetName());
    }
    else if (itemType == wxString(wxT("wxMessage")) || itemType == wxString(wxT("wxStaticText")) ||
        itemType == wxString(wxT("wxStaticBitmap")))
    {
        if (!childResource->GetValue4().empty() || itemType == wxString(wxT("wxStaticBitmap")) )
        {
            // Bitmap message
            wxBitmap bitmap = childResource->GetBitmap();
            if (!bitmap.Ok())
            {
                bitmap = wxResourceCreateBitmap(childResource->GetValue4(), (wxResourceTable *)this);
                ((wxItemResource*) childResource)->SetBitmap(bitmap);
            }
#if wxUSE_BITMAP_MESSAGE
#ifdef __WXMSW__
            // Use a default bitmap
            if (!bitmap.Ok())
                bitmap.LoadFile(wxT("cross_bmp"), wxBITMAP_TYPE_BMP_RESOURCE);
#endif

            if (bitmap.Ok())
                control = new wxStaticBitmap(parent, id, bitmap, pos, size,
                childResource->GetStyle(), childResource->GetName());
#endif
        }
        else
        {
            control = new wxStaticText(parent, id, childResource->GetTitle(), pos, size,
                childResource->GetStyle(), childResource->GetName());
        }

⌨️ 快捷键说明

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