void CXTPReportRecord::TreeRelease()
{
	if (HasChildren())
	{
		for (int nChild = 0; nChild < GetChilds()->GetCount(); nChild++)
		{
			GetChilds()->GetAt(nChild)->TreeRelease();
		}
	}
	InternalRelease();
}
void CXTPReportRecord::TreeAddRef()
{
	InternalAddRef();
	if (HasChildren())
	{
		for (int nChild = 0; nChild < GetChilds()->GetCount(); nChild++)
		{
			GetChilds()->GetAt(nChild)->TreeAddRef();
		}
	}
}
std::vector<KochSegment> KochSegment::Divide( eGrowthDirection dir )
{   
    if (was_iterating == false)
        return GetChilds();

    std::vector<KochSegment> cur_segs;

    if (iter_order >= 10)
        return cur_segs;

    RandomAS rand;
    for (int i = 0; i < 5; i++)
    {
        std::vector<KochSegment> cur_segs_ = childs.at( i ).Divide();
        if (!cur_segs_.empty())
        {
            for (int j = 0; j < cur_segs_.size(); j++)
            {
                cur_segs.push_back( cur_segs_.at( j ) );    
            }
        }
    }
    RefreshKochPolygon();
    return  cur_segs;
}
void CXTPPropertyGridItemFlags::UpdateChilds()
{
	CXTPPropertyGridItems* pItems = GetChilds();
	for (int i = 0; i < pItems->GetCount(); i++)
	{
		CXTPPropertyGridItemFlag* pItem = (CXTPPropertyGridItemFlag*)pItems->GetAt(i);
		pItem->SetBool((m_nValue & pItem->m_dwFlag) == pItem->m_dwFlag);
		pItem->SetReadOnly(GetReadOnly());
	}
}
void CXTPPropertyGridItemFlags::OnConstraintsChanged()
{
	GetChilds()->Clear();

	CXTPPropertyGridItemConstraints* pConstraints = GetConstraints();

	int i;

	for (i = 0; i < pConstraints->GetCount(); i++)
	{
		AddChildItem(new CXTPPropertyGridItemFlag(pConstraints->GetAt(i), (int)pConstraints->GetConstraintAt(i)->m_dwData));
	}
	UpdateChilds();
	m_strDefaultValue = m_strValue = GetFlagsString();

	CXTPPropertyGridItems* pItems = GetChilds();
	for (i = 0; i < pItems->GetCount(); i++)
	{
		CXTPPropertyGridItemFlag* pItem = (CXTPPropertyGridItemFlag*)pItems->GetAt(i);
		pItem->SetDefaultValue(pItem->GetValue());
	}

}
void CXTPReportRecord::DoPropExchange(CXTPPropExchange* pPX)
{
	PX_Bool(pPX, _T("Locked"), m_bLocked);
	PX_Bool(pPX, _T("Editable"), m_bEditable);

	BOOL bPreview = m_pPreviewItem != NULL;
	PX_Bool(pPX, _T("Preview"), bPreview);


	int nCount = GetItemCount();

	CXTPPropExchangeEnumeratorPtr pEnumItems(pPX->GetEnumerator(_T("Item")));

	if (pPX->IsStoring())
	{
		POSITION posItem = pEnumItems->GetPosition((DWORD)nCount);

		for (int i = 0; i < nCount; i++)
		{
			CXTPReportRecordItem* pItem = GetItem(i);
			ASSERT(pItem);
			if (!pItem)
				AfxThrowArchiveException(CArchiveException::badClass);

			CXTPPropExchangeSection secItem(pEnumItems->GetNext(posItem));
			PX_Object(&secItem, pItem, RUNTIME_CLASS(CXTPReportRecordItem));
		}
	}
	else
	{
		RemoveAll();

		POSITION posItem = pEnumItems->GetPosition();

		while (posItem)
		{
			CXTPReportRecordItem* pItem = NULL;

			CXTPPropExchangeSection sec(pEnumItems->GetNext(posItem));
			PX_Object(&sec, pItem, RUNTIME_CLASS(CXTPReportRecordItem));

			if (!pItem)
				AfxThrowArchiveException(CArchiveException::badClass);

			AddItem(pItem);
		}
	}

	//------------------------------------------------------------
	if (bPreview)
	{
		CXTPPropExchangeSection secPreviewItem(pPX->GetSection(_T("PreviewItem")));

		if (pPX->IsLoading())
		{
			CMDTARGET_RELEASE(m_pPreviewItem);
		}

		PX_Object(&secPreviewItem, m_pPreviewItem, RUNTIME_CLASS(CXTPReportRecordItemPreview));

		if (m_pPreviewItem && pPX->IsLoading())
		{
			m_pPreviewItem->m_pRecord = this;
		}
	}

	//------------------------------------------------------------
	if (pPX->GetSchema() > _XTP_SCHEMA_1041)
	{
		BOOL bHasChildren = HasChildren();
		PX_Bool(pPX, _T("HasChildren"), bHasChildren, FALSE);

		if (bHasChildren)
		{
			CXTPPropExchangeSection secChildren(pPX->GetSection(_T("Children")));
			GetChilds()->_DoPropExchange(&secChildren);
		}
		else if (m_pChildren)
		{
			m_pChildren->RemoveAll();
		}
	}
}
void CSyntTree::Imprimir(int n)
{
	int i;

	for (i = 0; i < n; i++)
		printf("    ");

	int hijo = 0;

	switch(GetType())
	{
		case SYNT_INSTRUCCION:
			printf("Instrucción: \n");
			break;

		case SYNT_RETURN:
			printf("Return: \n");
			break;

		case SYNT_FOR:
			printf("Instrucción For:\n");
			
			for (i = 0; i < n + 1; i++)
				printf("    ");
			printf("Inicialización:\n");
			GetChild(hijo++)->Imprimir(n + 2);

			for (i = 0; i < n + 1; i++)
				printf("    ");
			printf("Condicion:\n");
			GetChild(hijo++)->Imprimir(n + 2);

			for (i = 0; i < n + 1; i++)
				printf("    ");
			printf("Incremento:\n");
			GetChild(hijo++)->Imprimir(n + 2);

			for (i = 0; i < n + 1; i++)
				printf("    ");
			printf("Ciclo:\n");
			GetChild(hijo++)->Imprimir(n + 2);
			break;

		case SYNT_WHILE:
			printf("Instrucción While:\n");
			
			for (i = 0; i < n + 1; i++)
				printf("    ");
			printf("Condición:\n");
			GetChild(hijo++)->Imprimir(n + 2);

			for (i = 0; i < n + 1; i++)
				printf("    ");
			printf("Ciclo:\n");
			GetChild(hijo++)->Imprimir(n + 2);
			break;

		case SYNT_IF:
			printf("Instrucción If:\n");
			
			for (i = 0; i < n + 1; i++)
				printf("    ");
			printf("Condición:\n");
			GetChild(hijo++)->Imprimir(n + 2);

			for (i = 0; i < n + 1; i++)
				printf("    ");
			printf("Si es verdad:\n");
			GetChild(hijo++)->Imprimir(n + 2);

			if (GetChilds() == 3)
			{
				for (i = 0; i < n + 1; i++)
					printf("    ");
				printf("Si es falso:\n");
				GetChild(hijo++)->Imprimir(n + 2);
			}
			break;

		case SYNT_INSTRUCCION_NULA:
			printf("Instrucción nula\n");
			break;

		case SYNT_LISTA_FUNCIONES:
			printf("Lista de funciones:\n");
			break;

		case SYNT_LISTA_INSTRUCCIONES:
			printf("Lista de instrucciones:\n");
			break;

		case SYNT_IDENTIFICADOR:
			printf("Identificador: %s\n", GetString());
			break;

		case SYNT_VARIABLE:
			printf("Variable: %s\n", GetString());
			break;
		
		case SYNT_CONSTANTE:
			printf("Valor constante ");

			if (strcmp(GetReturnType()->GetNombre(), SIMBOLO_INT) == 0)
				printf("(entero) %ld\n", GetInteger());
			else
				if (strcmp(GetReturnType()->GetNombre(), SIMBOLO_FLOAT) == 0)
					printf("(float) %f\n", GetFloat());
				else
					if (strcmp(GetReturnType()->GetNombre(), SIMBOLO_STRING) == 0)
						printf("(cadena) %s\n", GetString());
			break;

		case SYNT_OP_ASIGNACION:
			printf ("Operador =\n");
			break;

		case SYNT_OP_MAS:
			printf ("Operador +\n");
			break;

		case SYNT_OP_MENOS:
			printf ("Operador -\n");
			break;

		case SYNT_OP_POR:
			printf ("Operador *\n");
			break;

		case SYNT_OP_DIV:
			printf ("Operador /\n");
			break;

		case SYNT_LLAMADO_FUNCION:
			printf("LLamado a función: %s\n", GetString());
			break;

		case SYNT_DECLARACION_VARIABLE:
			printf("Declaración de variables del tipo %s\n", GetString());
			break;

		case SYNT_DECLARACION_FUNCION:
			printf("Declaración de la función: %s\n", GetString());
			break;
		
		case SYNT_CONVERSION:
			printf("Conversion desde %s a %s\n", GetChild(0)->GetReturnType()->GetNombre(), GetReturnType()->GetNombre());
			break;

		case SYNT_TABLA_SIMBOLOS:
			printf("Tabla de simbolos\n");
			m_pTablaSimbolos->Imprimir(n + 1);
			break;
	}

	n++;

	for (; hijo < GetChilds(); hijo++)
		GetChild(hijo)->Imprimir(n);
}
void CXTPReportGroupRow::SetCaption(LPCTSTR lpszCaption)
{
	CString strGroupCaption(lpszCaption);

	if (strGroupCaption ==  _T("x"))
		strGroupCaption.Empty();

	if (m_strFormula.IsEmpty())
	{
		if (m_strGroupText == strGroupCaption)
			return;

		m_strGroupText = strGroupCaption;
		if (m_strGroupLabel.IsEmpty())
			m_strGroupLabel = m_strGroupText;

		XTPMarkupReleaseElement(m_pMarkupUIElement);

		if (m_pControl && (m_pControl->GetMarkupContext()))
			m_pMarkupUIElement = XTPMarkupParseText(m_pControl->GetMarkupContext(), m_strGroupText);
	}
	else
	{
		CString strCaption(strGroupCaption);
		double d(0.0);
		int m(0), M(0), n(0), N(0);
		CString s, t, u, v;
		CString sFormula(m_strFormula);
		sFormula.Replace(_T("(C"), _T("(R*C"));
		sFormula.Replace(_T(":C"), _T("(:R*C"));
		int pos = sFormula.Find(_T("SUMSUB("));
		int nextPos(-1);
//Simplified format for group row formulas - SUMSUB(C#:C#) as R# ignored and dynamically recreated
//For prev ver formulas - clean R# parts
//For multiple formulas case can be SUMSUB(R#Ci1:R#Cl1) SUMSUB(R#Ci2:R#Cl2) ... SUMSUB(R#Cim:R#Clm)
//ROW range ignored as used dynamic based on group row childs
//Space-separated string split between spaces and
//Instead "if (pos > -1)" can use loop "while (pos > -1)"
		//if (pos > -1)
		while (pos > -1)
		{
			CString sNextFormula, strCapt, strCol;
			if (sFormula.GetLength() > pos + 7)
				nextPos = sFormula.Find(_T("SUMSUB("), pos + 7);
			if (nextPos > -1)
				sNextFormula = sFormula.Mid(nextPos);

			if (GetChilds(FALSE) != NULL)
			{
				int mMax = m_pControl->GetRows()->GetCount();
				int nMax = m_pControl->GetColumns()->GetCount();
//CODE FOR RECALC - pattern SUMSUB(R#C#:R#C#) or * instead of # - means all row or column
//ROW range ignored as used dynamic based on group row childs
				sFormula.Replace(_T(" "), _T(""));
				if (sFormula.GetLength() > pos + 7)
					sFormula = sFormula.Mid(pos + 7);
				pos = sFormula.Find(_T(":"));
				if (pos > -1)
				{
					s = sFormula.Left(pos);
					t = sFormula.Mid(pos + 1);
					pos = s.Find(_T("C"));
					if (pos > -1)
					{
						u = s.Left(pos);
						s = s.Mid(pos + 1);
						u.Replace(_T("R"), _T(""));
						if (u == _T("*"))
							m = 0;
						else
							m = _ttoi(u);
						if (s == _T("*"))
							n = 0;
						else
							n = _ttoi(s);

						pos = t.Find(_T("C"));
						if (pos > -1)
						{
							u = t.Left(pos);
							t = t.Mid(pos + 1);
							u.Replace(_T("R"), _T(""));
							if (u == _T("*"))
								M = mMax;
							else
								M = _ttoi(u);
							t.Replace(_T(")"), _T(""));
							if (t == _T("*"))
								N = nMax;
							else
								N = _ttoi(t);

							if (n == N - 1)
								d = 0;
							CalculateByChilds(this, n, N, d);

							CString sFmt = GetFormatString();
							if (sFmt.IsEmpty() || sFmt == _T("%s"))
								sFmt = _T("%f");
							else if (sFmt == _T("%d"))
								sFmt = _T("%.0f");

							strCapt.Format(sFmt, d);
							strCol.Format(_T(" [%d]"), n);
							strCapt = strCol + strCapt;
						}
					}
				}
			}
			if (nextPos > -1)
			{
				sFormula = sNextFormula;
				pos = sFormula.Find(_T("SUMSUB("));
				sNextFormula.Empty();
			}
			strCaption += strCapt;
		}
		if (m_strGroupText == strCaption)
			return;

		m_strGroupText = m_strGroupLabel + strCaption;

		XTPMarkupReleaseElement(m_pMarkupUIElement);

		if (m_pControl && (m_pControl->GetMarkupContext()))
			m_pMarkupUIElement = XTPMarkupParseText(m_pControl->GetMarkupContext(), m_strGroupText);
	}
}