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

📄 widget.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "wxsheaders.h"
#include "widget.h"

#include "properties/wxsstyleproperty.h"
#include "properties/wxscolourproperty.h"
#include "properties/wxsfontproperty.h"
#include "resources/wxswindowres.h"
#include "wxswidgetfactory.h"
#include "wxsresource.h"
#include "wxswindoweditor.h"
#include "wxswidgetevents.h"
#include <wx/tokenzr.h>
#include <wx/list.h>

#define COLOUR_ENTRY(Name) { _T(#Name), Name },

/** All system colours */
static struct { wxChar* Name; wxUint32 Value; } wxsSystemColours[] =
{
    COLOUR_ENTRY(wxSYS_COLOUR_SCROLLBAR)
    COLOUR_ENTRY(wxSYS_COLOUR_BACKGROUND)
    COLOUR_ENTRY(wxSYS_COLOUR_DESKTOP)
    COLOUR_ENTRY(wxSYS_COLOUR_ACTIVECAPTION)
    COLOUR_ENTRY(wxSYS_COLOUR_INACTIVECAPTION)
    COLOUR_ENTRY(wxSYS_COLOUR_MENU)
    COLOUR_ENTRY(wxSYS_COLOUR_WINDOW)
    COLOUR_ENTRY(wxSYS_COLOUR_WINDOWFRAME)
    COLOUR_ENTRY(wxSYS_COLOUR_MENUTEXT)
    COLOUR_ENTRY(wxSYS_COLOUR_WINDOWTEXT)
    COLOUR_ENTRY(wxSYS_COLOUR_CAPTIONTEXT)
    COLOUR_ENTRY(wxSYS_COLOUR_ACTIVEBORDER)
    COLOUR_ENTRY(wxSYS_COLOUR_INACTIVEBORDER)
    COLOUR_ENTRY(wxSYS_COLOUR_APPWORKSPACE)
    COLOUR_ENTRY(wxSYS_COLOUR_HIGHLIGHT)
    COLOUR_ENTRY(wxSYS_COLOUR_HIGHLIGHTTEXT)
    COLOUR_ENTRY(wxSYS_COLOUR_BTNFACE)
    COLOUR_ENTRY(wxSYS_COLOUR_3DFACE)
    COLOUR_ENTRY(wxSYS_COLOUR_BTNSHADOW)
    COLOUR_ENTRY(wxSYS_COLOUR_3DSHADOW)
    COLOUR_ENTRY(wxSYS_COLOUR_GRAYTEXT)
    COLOUR_ENTRY(wxSYS_COLOUR_BTNTEXT)
    COLOUR_ENTRY(wxSYS_COLOUR_INACTIVECAPTIONTEXT)
    COLOUR_ENTRY(wxSYS_COLOUR_BTNHIGHLIGHT)
    COLOUR_ENTRY(wxSYS_COLOUR_BTNHILIGHT)
    COLOUR_ENTRY(wxSYS_COLOUR_3DHIGHLIGHT)
    COLOUR_ENTRY(wxSYS_COLOUR_3DHILIGHT)
    COLOUR_ENTRY(wxSYS_COLOUR_3DDKSHADOW)
    COLOUR_ENTRY(wxSYS_COLOUR_3DLIGHT)
    COLOUR_ENTRY(wxSYS_COLOUR_INFOTEXT)
    COLOUR_ENTRY(wxSYS_COLOUR_INFOBK)
    COLOUR_ENTRY(wxSYS_COLOUR_LISTBOX)
    COLOUR_ENTRY(wxSYS_COLOUR_HOTLIGHT)
    COLOUR_ENTRY(wxSYS_COLOUR_GRADIENTACTIVECAPTION)
    COLOUR_ENTRY(wxSYS_COLOUR_GRADIENTINACTIVECAPTION)
    COLOUR_ENTRY(wxSYS_COLOUR_MENUHILIGHT)
    COLOUR_ENTRY(wxSYS_COLOUR_MENUBAR)
    { NULL, 0 }
};

/** Number of items in system colours array */
static const int wxsSystemColoursCount = sizeof(wxsSystemColours) / sizeof(wxsSystemColours[0]);

wxsWidget::wxsWidget(wxsWidgetManager* Man,wxsWindowRes* Res,BasePropertiesType pType):
    PropertiesObject(this),
    Manager(Man),
    Preview(NULL),
    Resource(Res),
    Properties(NULL),
    Parent(NULL),
    MaxChildren(0),
    XmlElement(NULL),
    ContainerType(NoContainer),
    Updating(false),
    PropertiesCreated(false),
    BPType(pType),
    AssignedToTree(false),
    Events(NULL)
{
}

wxsWidget::wxsWidget(wxsWidgetManager* Man, wxsWindowRes* Res, bool ISwxWindow, int MaxChild,BasePropertiesType pType):
    PropertiesObject(this),
    Manager(Man),
    Preview(NULL),
    Resource(Res),
    Properties(NULL),
    Parent(NULL),
    MaxChildren(MaxChild),
    XmlElement(NULL),
    ContainerType(ISwxWindow ? ContainerWindow : ContainerSizer ),
    Updating(false),
    PropertiesCreated(false),
    BPType(pType),
    AssignedToTree(false),
    Events(NULL)
{
}

wxsWidget::~wxsWidget()
{
	assert ( Preview == NULL );
	assert ( Properties == NULL );

    while ( GetChildCount() )
    {
        wxsKILL(GetChild(GetChildCount()-1));
    }

    if ( Events )
    {
    	delete Events;
    	Events = NULL;
    }
}

void wxsWidget::AddDefaultProperties(BasePropertiesType pType)
{
    // Adding standard items

    if ( pType & bptVariable )
    {
        PropertiesObject.AddProperty(_("Var Name:"),BaseParams.VarName);
        PropertiesObject.AddProperty(_("Is Member:"),BaseParams.IsMember);
    }

    if ( pType & bptId )
    {
        PropertiesObject.AddProperty(_("Id:"),BaseParams.IdName);
    }

    if ( pType & bptPosition )
    {
        PropertiesObject.Add2IProperty(_("Position:"),BaseParams.PosX,BaseParams.PosY);
        PropertiesObject.AddProperty(_(" Default:"),BaseParams.DefaultPosition);
    }

    if ( pType & bptSize )
    {
        PropertiesObject.Add2IProperty(_("Size:"),BaseParams.SizeX,BaseParams.SizeY);
        PropertiesObject.AddProperty(_(" Default:"),BaseParams.DefaultSize);
    }

    if ( pType & bptEnabled )
    {
    	PropertiesObject.AddProperty(_("Enabled:"),BaseParams.Enabled);
    }

    if ( pType & bptFocused )
    {
    	PropertiesObject.AddProperty(_("Focused:"),BaseParams.Focused);
    }

    if ( pType & bptHidden )
    {
    	PropertiesObject.AddProperty(_("Hidden:"),BaseParams.Hidden);
    }

    if ( pType & bptColours )
    {
    	PropertiesObject.AddProperty(_("Foreground colour:"),
            new wxsColourProperty(&PropertiesObject,BaseParams.FgType,BaseParams.Fg) );
    	PropertiesObject.AddProperty(_("Background colour:"),
            new wxsColourProperty(&PropertiesObject,BaseParams.BgType,BaseParams.Bg) );
    }

    if ( pType & bptFont )
    {
    	PropertiesObject.AddProperty(_("Font:"),
            new wxsFontProperty(&PropertiesObject,BaseParams.UseFont,BaseParams.Font) );
    }

    if ( pType & bptToolTip )
    {
    	PropertiesObject.AddProperty(_("Tool tip:"),BaseParams.ToolTip);
    }

    // Adding style property

    if ( pType & bptStyle )
    {
        if ( GetInfo().Styles != NULL )
        {
            PropertiesObject.AddProperty(
                _T("Style:"),
                new wxsStyleProperty(
                    &PropertiesObject,
                    BaseParams.Style,
                    GetInfo().Styles),
                -1);
        }
    }

}

wxWindow* wxsWidget::CreatePreview(wxWindow* Parent,wxsWindowEditor* Editor)
{
    if ( Preview ) KillPreview();

    /* Creating widget */
    Preview = MyCreatePreview(Parent);
    if ( !Preview ) return NULL;

    /* Creating preview of child widgets */
    int Cnt = IsContainer() ? GetChildCount() : 0;
    for ( int i=0; i<Cnt; i++ )
    {
        GetChild(i)->CreatePreview(Preview,Editor);
    }

    MyFinalUpdatePreview(Preview);

    return Preview;
}

bool wxsWidget::PropertiesUpdated(bool Validate,bool Correct)
{
    if ( Updating ) return true;
    bool Result = true;
    Updating = true;
    if ( GetEditor() )
    {
    	GetEditor()->BuildPreview();
    }
    if ( Validate )
    {
    	Result = GetResource()->CheckBaseProperties(Correct,this);
    }
    GetResource()->NotifyChange();
    Updating = false;
    return Result;
}

void wxsWidget::KillPreview()
{
    /* Killing this one */
    if ( Preview != NULL )
    {
        delete Preview;
        PreviewDestroyed();
    }
}

void wxsWidget::PreviewApplyDefaults(wxWindow* Wnd)
{
	BasePropertiesType pType = GetBPType();
	if ( pType & bptEnabled && !BaseParams.Enabled )
	{
		Wnd->Disable();
	}

	if ( pType & bptFocused && BaseParams.Focused )
	{
		Wnd->SetFocus();
	}

	if ( pType & bptHidden && BaseParams.Hidden )
	{
		Wnd->Hide();
	}

	if ( pType & bptColours )
	{
		if ( BaseParams.FgType != wxsNO_COLOUR )
		{
			if ( BaseParams.FgType == wxsCUSTOM_COLOUR )
			{
				Wnd->SetForegroundColour(BaseParams.Fg);
			}
			else
			{
				Wnd->SetForegroundColour(wxSystemSettings::GetColour((wxSystemColour)BaseParams.FgType));
			}
		}

		if ( BaseParams.BgType != wxsNO_COLOUR )
		{
			if ( BaseParams.BgType == wxsCUSTOM_COLOUR )
			{
				Wnd->SetBackgroundColour(BaseParams.Bg);
			}
			else
			{
				Wnd->SetBackgroundColour(wxSystemSettings::GetColour((wxSystemColour)BaseParams.BgType));
			}
		}
	}

    if ( pType & bptToolTip && BaseParams.ToolTip )
    {
    	Wnd->SetToolTip(BaseParams.ToolTip);
    }

    if ( pType & bptFont && BaseParams.UseFont )
    {
    	Wnd->SetFont(BaseParams.Font);
    }

}

void wxsWidget::XmlAssignElement(TiXmlElement* Elem)
{
    XmlElement = Elem;
}

bool wxsWidget::XmlLoadDefaultsT(BasePropertiesType pType)
{
    assert ( XmlElem() != NULL );
    // Loading event handler enteries
    GetEvents()->XmlLoadFunctions(XmlElem());

    /* Processing position */
    if ( pType & bptPosition )
    {
        BaseParams.DefaultPosition = !XmlGetIntPair(_T("pos"),BaseParams.PosX,BaseParams.PosY);
    }

    /* Processing size */
    if ( pType & bptSize )
    {
        BaseParams.DefaultSize = !XmlGetIntPair(_T("size"),BaseParams.SizeX,BaseParams.SizeY);
    }

    /* Processing id */
    if ( pType & bptId )
    {
        const char* IdName = XmlElem()->Attribute("name");
        BaseParams.IdName = IdName ? wxString ( IdName, wxConvLocal ) : _T("");
    }

    /* Processing variable name and locality */
    if ( pType & bptVariable )
    {
        const char* VarName = XmlElem()->Attribute("variable");
        BaseParams.VarName = VarName ? wxString ( VarName, wxConvUTF8 ) : _T("");
        const char* IsMember = XmlElem()->Attribute("member");
        BaseParams.IsMember = IsMember ? ( strcasecmp(IsMember,"no") != 0 ) : true;
    }

    /* Processing style */
    if ( pType & bptStyle )
    {
        wxString StyleStr = XmlGetVariable(_T("style"));
        wxsStyle* Styles = GetInfo().Styles;
        BaseParams.Style = 0;
        if ( Styles )
        {
            for ( ; Styles->Name != _T(""); Styles++ )
            {
                int Pos = StyleStr.Find(Styles->Name);
                int Len = (int)Styles->Name.Length();
                if ( Pos < 0 ) continue;

                // One character after style in StyleStr should be checked - it
                // must be '|'. Same for character before.

                if ( ( Pos + Len >= (int)StyleStr.Len() || StyleStr.GetChar(Pos+Len) == _T('|') ) &&
                     ( Pos == 0 || StyleStr.GetChar(Pos-1) == _T('|') ) )
                {
                    BaseParams.Style |= Styles->Value;
                }
            }
        }
    }

    if ( pType & bptEnabled )
    {
    	BaseParams.Enabled = XmlGetInteger(_T("enabled"),1) != 0;
    }

    if ( pType & bptFocused )
    {
    	BaseParams.Focused = XmlGetInteger(_T("focused"),0) != 0;
    }

    if ( pType & bptHidden )
    {
    	BaseParams.Hidden = XmlGetInteger(_T("hiddedn"),0) != 0;
    }

    if ( pType & bptColours )
    {
    	wxString Colour = XmlGetVariable(_T("fg"));
    	Colour.Trim(true).Trim(false);
        BaseParams.FgType = wxsNO_COLOUR;
    	if ( !Colour.empty() )
    	{
    		if ( Colour[0] == _T('#') )
    		{
    			// Got web colour
    			long Value = 0;

⌨️ 快捷键说明

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