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

📄 wxswindowres.cpp

📁 非常好用的可移植的多平台C/C++源代码编辑器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            wxString IdBase = Child->GetInfo().DefaultVarName;
            IdBase.MakeUpper();
            wxString Id;
            int Index = 1;
            do
            {
                Name.Printf(_T("%s%d"),NameBase.c_str(),Index);
                Id.Printf(_T("ID_%s%d"),IdBase.c_str(),Index++);
            }
            while ( ( UpdateVar && NamesMap.find(Name) != NamesMap.end() ) ||
                    ( UpdateId  && IdsMap.find(Id)     != IdsMap.end() ) );

            if ( UpdateVar )
            {
                Params.VarName = Name;
                NamesMap[Name] = Child;
            }
            if ( UpdateId )
            {
                Params.IdName = Id;
                IdsMap[Id] = Child;
            }
            if ( UpdateVar || UpdateId )
            {
            	SetModified();
            }
        }

		UpdateWidgetsVarNameIdReq(NamesMap,IdsMap,Child);
	}
}

void wxsWindowRes::CreateSetsReq(StrMap& NamesMap, StrMap& IdsMap, wxsWidget* Widget, wxsWidget* Without)
{
	int Cnt = Widget->GetChildCount();
	for ( int i=0; i<Cnt; i++ )
	{
		wxsWidget* Child = Widget->GetChild(i);

		if ( Child != Without )
		{
            if ( Child->GetBaseParams().VarName.Length() )
            {
                NamesMap[Child->GetBaseParams().VarName.c_str()] = Child;
            }

            if ( Child->GetBaseParams().IdName.Length() )
            {
                IdsMap[Child->GetBaseParams().IdName.c_str()] = Child;
            }
		}

		CreateSetsReq(NamesMap,IdsMap,Child,Without);
	}
}

bool wxsWindowRes::CheckBaseProperties(bool Correct,wxsWidget* Changed)
{
    StrMap NamesMap;
    StrMap IdsMap;

    if ( Changed == NULL )
    {
    	// Will check all widgets
    	return CheckBasePropertiesReq(RootWidget,Correct,NamesMap,IdsMap);
    }

    // Creating sets of names and ids
   	CreateSetsReq(NamesMap,IdsMap,RootWidget,Changed);

   	// Checkign and correcting changed widget
   	return CorrectOneWidget(NamesMap,IdsMap,Changed,Correct);
}

bool wxsWindowRes::CheckBasePropertiesReq(wxsWidget* Widget,bool Correct,StrMap& NamesMap,StrMap& IdsMap)
{
	bool Result = true;
	int Cnt = Widget->GetChildCount();
	for ( int i=0; i<Cnt; ++i )
	{
		wxsWidget* Child = Widget->GetChild(i);

		if ( !CorrectOneWidget(NamesMap,IdsMap,Child,Correct) )
		{
			if ( !Correct ) return false;
			Result = false;
		}

		NamesMap[Child->GetBaseParams().VarName] = Child;
		IdsMap[Child->GetBaseParams().IdName] = Child;

		if ( ! CheckBasePropertiesReq(Child,Correct,NamesMap,IdsMap) )
		{
			if ( !Correct ) return false;
			Result = false;
		}
	}

	return Result;
}

bool wxsWindowRes::CorrectOneWidget(StrMap& NamesMap,StrMap& IdsMap,wxsWidget* Changed,bool Correct)
{
	bool Valid = true;

    // Validating variable name

    if ( Changed->GetBPType() & wxsWidget::bptVariable )
    {
    	wxString& VarName = Changed->GetBaseParams().VarName;
    	wxString Corrected;
    	VarName.Trim(true);
    	VarName.Trim(false);

    	// first validating produced name

    	if ( VarName.Length() == 0 )
    	{
    		if ( !Correct )
    		{
    			wxMessageBox(_("Item must have variable name"));
    			return false;
    		}

   			// Creating new unique name

   			const wxString& Prefix = Changed->GetInfo().DefaultVarName;
   			for ( int i=1;; ++i )
   			{
   				Corrected.Printf(_T("%s%d"),Prefix.c_str(),i);
   				if ( NamesMap.find(Corrected) == NamesMap.end() ) break;
   			}

    		Valid = false;
    	}
    	else
    	{
    		// Validating name as C++ ideentifier
            if ( wxString(_T("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                          _T("abcdefghijklmnopqrstuvwxyz")
                          _T("_") ).Find(VarName.GetChar(0)) == -1 )
            {
            	if ( !Correct )
            	{
            		wxMessageBox(wxString::Format(_("Invalid character: '%c' in variable name"),VarName.GetChar(0)));
            		return false;
            	}
                Valid = false;
            }
            else
            {
            	Corrected.Append(VarName.GetChar(0));
            }

            for ( size_t i=1; i<VarName.Length(); ++i )
            {
                if ( wxString(_T("0123456789")
                              _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                              _T("abcdefghijklmnopqrstuvwxyz")
                              _T("_") ).Find(VarName.GetChar(i)) == -1 )
                {
                    if ( !Correct )
                    {
                        wxMessageBox(wxString::Format(_("Invalid character: '%c' in variable name"),VarName.GetChar(i)));
                        return false;
                    }
                    Valid = false;
                }
                else
                {
                    Corrected.Append(VarName.GetChar(i));
                }
            }

            // Searching for another widget with same name
            if ( NamesMap.find(Corrected) != NamesMap.end() )
            {
            	if ( !Correct )
            	{
            		wxMessageBox(wxString::Format(_("Item with variable name '%s' already exists"),Corrected.c_str()));
            		return false;
            	}

            	// Generating new unique name

                const wxString& Prefix = Changed->GetInfo().DefaultVarName;
                for ( int i=1;; ++i )
                {
                    Corrected.Printf(_T("%s%d"),Prefix.c_str(),i);
                    if ( NamesMap.find(Corrected) == NamesMap.end() ) break;
                }

            	Valid = false;
            }
    	}

        if ( Correct )
        {
        	VarName = Corrected;
        }
    }

    if ( Changed->GetBPType() & wxsWidget::bptId )
    {
    	wxString& IdName = Changed->GetBaseParams().IdName;
    	wxString Corrected;
    	IdName.Trim(true);
    	IdName.Trim(false);

    	// first validating produced name

    	if ( IdName.Length() == 0 )
    	{
    		if ( !Correct )
    		{
    			wxMessageBox(_("Item must have identifier"));
    			return false;
    		}

   			// Creating new unique name

   			wxString Prefix = Changed->GetInfo().DefaultVarName;
   			Prefix.UpperCase();
   			for ( int i=1;; ++i )
   			{
   				Corrected.Printf(_T("ID_%s%d"),Prefix.c_str(),i);
   				if ( IdsMap.find(Corrected) == IdsMap.end() ) break;
   			}

    		Valid = false;
    	}
    	else
    	{
    		// Validating name as C++ ideentifier
            if ( wxString(_T("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                          _T("abcdefghijklmnopqrstuvwxyz")
                          _T("_") ).Find(IdName.GetChar(0)) == -1 )
            {
            	if ( !Correct )
            	{
            		wxMessageBox(wxString::Format(_("Invalid character: '%c' in variable name"),IdName.GetChar(0)));
            		return false;
            	}
                Valid = false;
            }
            else
            {
            	Corrected.Append(IdName.GetChar(0));
            }

            for ( size_t i=1; i<IdName.Length(); ++i )
            {
                if ( wxString(_T("0123456789")
                              _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
                              _T("abcdefghijklmnopqrstuvwxyz")
                              _T("_") ).Find(IdName.GetChar(i)) == -1 )
                {
                    if ( !Correct )
                    {
                        wxMessageBox(wxString::Format(_("Invalid character: '%c' in variable name"),IdName.GetChar(i)));
                        return false;
                    }
                    Valid = false;
                }
                else
                {
                    Corrected.Append(IdName.GetChar(i));
                }
            }

            // Searching for another widget with same name

            if ( IdsMap.find(Corrected) != IdsMap.end() && Corrected != _T("ID_COMMON") )
            {
            	if ( !Correct )
            	{
            		wxMessageBox(wxString::Format(_("Item with identifier '%s' already exists"),Corrected.c_str()));
            		return false;
            	}

            	// Generating new unique name

                wxString Prefix = Changed->GetInfo().DefaultVarName;
                Prefix.UpperCase();
                for ( int i=1;; ++i )
                {
                    Corrected.Printf(_T("ID_%s%d"),Prefix.c_str(),i);
                    if ( IdsMap.find(Corrected) == IdsMap.end() ) break;
                }

            	Valid = false;
            }
    	}

    	if ( Correct )
    	{
    		IdName = Corrected;
    	}
    }

    if ( !Valid && Correct ) SetModified();

	return Valid;
}

void wxsWindowRes::BuildIdsArray(wxsWidget* Widget,wxArrayString& Array)
{
	int Cnt = Widget->GetChildCount();
	for ( int i=0; i<Cnt; i++ )
	{
		wxsWidget* Child = Widget->GetChild(i);
		if ( Child->GetBPType() & wxsWidget::bptId )
		{
			Array.Add(Child->GetBaseParams().IdName);
		}
		BuildIdsArray(Child,Array);
	}
}

void wxsWindowRes::BuildHeadersArray(wxsWidget* Widget,wxArrayString& Array)
{
	Array.Add(Widget->GetInfo().HeaderFile);
	if ( Widget->GetInfo().ExtHeaderFile.Length() )
	{
        Array.Add(Widget->GetInfo().ExtHeaderFile);
	}
	int Cnt = Widget->GetChildCount();
	for ( int i=0; i<Cnt; i++ )
	{
		wxsWidget* Child = Widget->GetChild(i);
		BuildHeadersArray(Child,Array);
	}
}

void wxsWindowRes::UpdateEventTable()
{
	int TabSize = 4;
	wxString CodeHeader;
	CodeHeader.Printf(wxsBHeaderF("EventTable"),ClassName.c_str());
	wxString Code = CodeHeader;
	Code.Append(_T('\n'));
	CollectEventTableEnteries(Code,RootWidget,TabSize);
	wxsCoder::Get()->AddCode(GetProject()->GetProjectFileName(GetSourceFile()),CodeHeader,Code);
    // Applying modified state
    if ( GetEditor() )
    {
    	// Must process inside editor (updating titile)
    	GetEditor()->SetModified();
    }
    else
    {
        SetModified();
    }

    // Storing change inside undo buffer
    if ( GetEditor() )
    {
    	((wxsWindowEditor*)GetEditor())->GetUndoBuff()->StoreChange();
    }
}

void wxsWindowRes::CollectEventTableEnteries(wxString& Code,wxsWidget* Widget,int TabSize)
{
	int Cnt = Widget->GetChildCount();
    Code += Widget->GetEvents()->GetArrayEnteries(TabSize);
	for ( int i=0; i<Cnt; i++ )
	{
		wxsWidget* Child = Widget->GetChild(i);
		CollectEventTableEnteries(Code,Child,TabSize);
	}
}

void wxsWindowRes::GenXrcFetchingCode(wxString& Code,wxsWidget* Widget,int TabSize)
{
	int Cnt = Widget->GetChildCount();
	for ( int i=0; i<Cnt; i++ )
	{
		wxsWidget* Child = Widget->GetChild(i);
		if ( Child->GetBaseParams().IsMember )
		{
			Code.Append(Child->GetBaseParams().VarName);
			Code.Append(_T(" = XRCCTRL(*this,\""));
			Code.Append(Child->GetBaseParams().IdName);
			Code.Append(_T("\","));
			Code.Append(Child->GetInfo().Name);
			Code.Append(_T(");\n"));
			Code.Append(_T(' '),TabSize);
		}
		GenXrcFetchingCode(Code,Child,TabSize);
	}
}

TiXmlDocument* wxsWindowRes::GenerateXrc()
{
	int EMStore = GetEditMode();
	SetEditMode(wxsResFile);
	TiXmlDocument* Generated = GenerateXml();
	SetEditMode(EMStore);
	return Generated;
}

void wxsWindowRes::SetModified(bool modified)
{
	if ( GetEditor() )
	{
		Modified = modified;
	}
	else
	{
		if ( modified ) Save();
		Modified = false;
	}

	// Changing unmodified entry inside undo buffer
	if ( !modified && GetEditor() )
	{
		((wxsWindowEditor*)GetEditor())->GetUndoBuff()->Saved();
	}
}

void wxsWindowRes::EditorClosed()
{
	AvoidCreation = true;
	GetRootWidget()->KillTree(wxsTREE());
	if ( GetModified() )
	{
		Load();
		wxTreeCtrl* Tree = wxsTREE();
		Tree->SelectItem(GetTreeItemId());
        //GetRootWidget()->BuildTree(Tree,GetTreeItemId());
	}
	AvoidCreation = false;
}

void wxsWindowRes::BuildTree(wxTreeCtrl* Tree,wxTreeItemId WhereToAdd,bool NoWidgets)
{
	SetTreeItemId(
        Tree->AppendItem(
            WhereToAdd,
            GetClassName(),
            -1,-1,
            new wxsResourceTreeData(this) ) );
    if ( !NoWidgets )
    {
        GetRootWidget()->BuildTree(Tree,GetTreeItemId());
    }
}

bool wxsWindowRes::ChangeRootWidget(wxsWidget* NewRoot,bool DeletePrevious)
{
	if ( !NewRoot ) return false;
	// New root must be of the same type as current
	if ( RootWidget->GetInfo().Name != NewRoot->GetInfo().Name ) return false;
    AvoidCreation = true;
	RootWidget->KillTree(wxsTREE());
	if ( GetEditor() )
	{
		((wxsWindowEditor*)GetEditor())->KillPreview();
	}
	if ( DeletePrevious ) wxsFACTORY()->Kill(RootWidget);
	RootWidget = NewRoot;
    wxTreeCtrl* Tree = wxsTREE();
    Tree->SelectItem(GetTreeItemId());
    if ( GetEditor() )
    {
        GetRootWidget()->BuildTree(Tree,GetTreeItemId());
        ((wxsWindowEditor*)GetEditor())->BuildPreview();
    }
    RebuildCode();
    AvoidCreation = false;
	return true;
}

⌨️ 快捷键说明

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