Пример #1
0
bool CActionBuffer::doAction (CGeorgesEditDoc &doc, bool redo, bool &modified, bool firstTime)
{
	IAction::doAction (doc, redo, modified, firstTime);

	modified = false;
	bool ok = true;
	uint index = (uint)redo;

	// Backup old value
	switch (_Type)
	{
	case FormPaste:
		{
			// Fill memstream with the new buffer
			theApp.FillMemStreamWithBuffer (&_Value[index][0], _Value[index].size ());

			// Reserial the document
			nlverify (theApp.SerialFromMemStream (_FormName.c_str (), &doc, _Slot));
			modified = true;
			update (true, Redraw, doc, _FormName.c_str ());
		}
		break;
	case FormArrayDelete:
		{
			if (redo)
			{
				// Get the parent node
				const CFormDfn *parentDfn;
				uint indexDfn;
				const CFormDfn *nodeDfn;
				const CType *nodeType;
				CFormElm *parentNode;
				UFormDfn::TEntryType type;
				bool array;
				CForm *form=doc.getFormPtr ();
				CFormElm *elm = doc.getRootNode (_Slot);
				nlverify ( elm->arrayDeleteNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
					&nodeDfn, &nodeType, &parentNode, type, array, true, atoi (_UserData.c_str ())) );
				modified = true;
			}
			else
			{
				// Insert a node
				const CFormDfn *parentDfn;
				uint indexDfn;
				const CFormDfn *nodeDfn;
				const CType *nodeType;
				CFormElm *parentNode;
				UFormDfn::TEntryType type;
				bool array;
				CForm *form=doc.getFormPtr ();
				CFormElm *elm = doc.getRootNode (_Slot);
				nlverify ( elm->arrayInsertNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
					&nodeDfn, &nodeType, &parentNode, type, array, true, atoi (_UserData.c_str ())) );

				// Paste the node

				// Fill memstream with the new buffer
				theApp.FillMemStreamWithBuffer (&_OldValue[0], _OldValue.size ());

				// Reserial the document
				nlverify (theApp.SerialFromMemStream (_FormName.c_str (), &doc, _Slot));
				modified = true;
			}

			update (true, Redraw, doc, _FormName.c_str ());
		}
		break;
	case FormArraySize:
		{
			if (redo)
			{
				// Get the size
				int size = 0;
				if ( _UserData.empty() || (sscanf (_UserData.c_str(), "%d", &size) == 1) )
				{
					if (size < 0)
						size = 0;
					
					// Array exist ?
					if (size > 0)
					{
						// Get / create the node
						const CFormDfn *parentDfn;
						uint indexDfn;
						const CFormDfn *nodeDfn;
						const CType *nodeType;
						CFormElm *node;
						UFormDfn::TEntryType type;
						bool array;
						bool created;
						CForm *form=doc.getFormPtr ();
						CFormElm *elm = doc.getRootNode (_Slot);
						nlverify ( elm->createNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
							&nodeDfn, &nodeType, &node, type, array, created) );
						nlassert (node);

						// Get the array node
						CFormElmArray *arrayPtr = safe_cast<CFormElmArray*> (node);

						// Backup old size
						uint oldSize = arrayPtr->Elements.size ();

						// Erase old element
						uint i;
						for (i=size; i<oldSize; i++)
						{
							// Delete the element
							if (arrayPtr->Elements[i].Element)
								delete arrayPtr->Elements[i].Element;
						}

						// Resize the array
						arrayPtr->Elements.resize (size);

						// Insert element
						for (i=oldSize; i<(uint)size; i++)
						{
							// Create empty sub node
							char index[512];
							smprintf (index, 512, "[%d]", i);
							nlverify ( arrayPtr->createNodeByName (index, &parentDfn, indexDfn, 
								&nodeDfn, &nodeType, &node, type, array, created) );
						}

						// Document modified
						modified = true;
						update (true, UpdateValues, doc, _FormName.c_str ());
					}
					else
					{
						// Remove the node
						const CFormDfn *parentDfn;
						uint indexDfn;
						const CFormDfn *nodeDfn;
						const CType *nodeType;
						CFormElm *node;
						UFormDfn::TEntryType type;
						bool array;
						CForm *form=doc.getFormPtr ();
						CFormElm *elm = doc.getRootNode (_Slot);
						nlverify ( elm->deleteNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
							&nodeDfn, &nodeType, &node, type, array) );
						nlassert ( (node == NULL) || (node->getForm () != doc.getFormPtr ()) );

						// Document modified
						modified = true;
						update (true, UpdateValues, doc, _FormName.c_str ());
					}
				}
			}
			else
			{
				// Fill memstream with the new buffer
				theApp.FillMemStreamWithBuffer (&_OldValue[0], _OldValue.size ());

				// Reserial the document
				nlverify (theApp.SerialFromMemStream (_FormName.c_str (), &doc, _Slot));
				modified = true;
				update (true, UpdateValues, doc, _FormName.c_str ());
			}
		}
		break;
	case FormVirtualDfnName:
		{
			if (redo)
			{
				// Result ok ?
				if (!_UserData.empty())
				{
					// Try to load the DFN
					CSmartPtr<CFormDfn> newDfn = doc.FormLoader.loadFormDfn (_UserData.c_str (), false);
					if (newDfn)
					{
						// Get / create the node
						const CFormDfn *parentDfn;
						uint indexDfn;
						const CFormDfn *nodeDfn;
						const CType *nodeType;
						CFormElm *node;
						UFormDfn::TEntryType type;
						bool array;
						bool created;
						CForm *form=doc.getFormPtr ();
						CFormElm *elm = doc.getRootNode (_Slot);
						nlverify ( elm->createNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
							&nodeDfn, &nodeType, &node, type, array, created) );
						nlassert (node);

						// Get the atom
						CFormElmVirtualStruct *vStruct = safe_cast<CFormElmVirtualStruct*> (node);

						// Assign it
						vStruct->DfnFilename = _UserData.c_str ();

						// Build the dfn
						vStruct->build (newDfn);
						modified = true;

						update (true, Redraw, doc, _FormName.c_str ());
					}
					else
					{
						ok = false;

						// Output error
						char msg[512];
						smprintf (msg, 512, "Can't read the dfn %s.", _UserData.c_str());
						theApp.outputError (msg);
						update (false, Redraw, doc, _FormName.c_str ());
					}
				}
				else
				{
					// Delete the node
					const CFormDfn *parentDfn;
					uint indexDfn;
					const CFormDfn *nodeDfn;
					const CType *nodeType;
					CFormElm *node;
					UFormDfn::TEntryType type;
					bool array;
					CForm *form=doc.getFormPtr ();
					CFormElm *elm = doc.getRootNode (_Slot);
					nlverify ( elm->deleteNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
						&nodeDfn, &nodeType, &node, type, array) );

					// Document is modified by this view
					modified = true;
					update (true, Redraw, doc, _FormName.c_str ());
				}
			}
			else
			{
				// Fill memstream with the new buffer
				theApp.FillMemStreamWithBuffer (&_OldValue[0], _OldValue.size ());

				// Reserial the document
				nlverify (theApp.SerialFromMemStream (_FormName.c_str (), &doc, _Slot));
				modified = true;
				update (true, Redraw, doc, _FormName.c_str ());
			}
		}
		break;
	default:
		nlstop;
	}	

	return ok;
}
Пример #2
0
bool CActionString::doAction (CGeorgesEditDoc &doc, bool redo, bool &modified, bool firstTime)
{
	IAction::doAction (doc, redo, modified, firstTime);

	modified = false;
	bool ok = true;
	uint index = (uint)redo;
	switch (_Type)
	{
	case TypeType:
		{
			CType *type = doc.getTypePtr ();
			type->Type = (CType::TType)(atoi (_Value[index].c_str ()));
			modified = true;
			update (false, Redraw, doc, _FormName.c_str ());
		}
		break;
	case TypeUI:
		{
			CType *type = doc.getTypePtr ();
			type->UIType = (CType::TUI)(atoi (_Value[index].c_str ()));
			modified = true;
			update (false, Redraw, doc, _FormName.c_str ());
		}
		break;
	case TypeDefault:
		{
			CType *type = doc.getTypePtr ();
			type->Default = _Value[index];
			modified = true;
			if (!firstTime)
				update (false, Redraw, doc, _FormName.c_str ());
		}
		break;
	case TypeMin:
		{
			CType *type = doc.getTypePtr ();
			type->Min = _Value[index];
			modified = true;
			if (!firstTime)
				update (false, Redraw, doc, _FormName.c_str ());
		}
		break;
	case TypeMax:
		{
			CType *type = doc.getTypePtr ();
			type->Max = _Value[index];
			modified = true;
			if (!firstTime)
				update (false, Redraw, doc, _FormName.c_str ());
		}
		break;
	case TypeIncrement:
		{
			CType *type = doc.getTypePtr ();
			type->Increment = _Value[index];
			modified = true;
			if (!firstTime)
				update (false, Redraw, doc, _FormName.c_str ());
		}
		break;
	case FormTypeValue:
	case FormValue:
		{
			// Empty ?
			if (!_Value[index].empty ())
			{
				// Get / create the node
				const CFormDfn *parentDfn;
				uint indexDfn;
				const CFormDfn *nodeDfn;
				const CType *nodeType;
				CFormElm *node;
				UFormDfn::TEntryType type;
				bool array;
				bool created;
				CForm *form=doc.getFormPtr ();
				CFormElm *elm = doc.getRootNode (_Slot);
				nlverify ( elm->createNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
					&nodeDfn, &nodeType, &node, type, array, created) );
				nlassert (node);

				// Set the atom value
				CFormElmAtom *atom = safe_cast<CFormElmAtom*> (node);
				atom->setValue (_Value[index].c_str ());
				modified = true;

				if (firstTime)
					update (created, UpdateLabels, doc, _FormName.c_str ());
				else
					update (created, UpdateValues, doc, _FormName.c_str ());
			}
			else
			{
				if (FormTypeValue == _Type)
				{
					// Get the node
					const CFormDfn *parentDfn;
					uint indexDfn;
					const CFormDfn *nodeDfn;
					const CType *nodeType;
					CFormElm *node;
					UFormDfn::TEntryType type;
					bool array;
					bool parentVDnfArray;
					CForm *form=doc.getFormPtr ();
					CFormElm *elm = doc.getRootNode (_Slot);
					nlverify ( elm->getNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
						&nodeDfn, &nodeType, &node, type, array, parentVDnfArray, true, NLGEORGES_FIRST_ROUND) );
					nlassert (node);
					CFormElmAtom *atom = safe_cast<CFormElmAtom*> (node);
					atom->setValue ("");
				}
				else
				{
					// Remove the node
					const CFormDfn *parentDfn;
					uint indexDfn;
					const CFormDfn *nodeDfn;
					const CType *nodeType;
					CFormElm *node;
					UFormDfn::TEntryType type;
					bool array;
					CForm *form=doc.getFormPtr ();
					CFormElm *elm = doc.getRootNode (_Slot);
					nlverify ( elm->deleteNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
						&nodeDfn, &nodeType, &node, type, array) );
					nlassert ((FormTypeValue == _Type)||(node == NULL));
				}

				modified = true;

				update (true, UpdateValues, doc, _FormName.c_str ());
			}
		}
		break;
	case HeaderVersion:
		{
			uint v0, v1;
			if (sscanf (_Value[index].c_str (), "Version %d.%d", &v0, &v1)==2)
			{
				CFileHeader *header = doc.getHeaderPtr ();
				header->MajorVersion = v0;
				header->MinorVersion = v1;
				modified = true;
				update (false, Redraw, doc, _FormName.c_str ());
			}
		}
		break;
	case HeaderState:
		{
			CFileHeader *header = doc.getHeaderPtr ();
			header->State = (CFileHeader::TState)atoi (_Value[index].c_str ());
			modified = true;
			update (false, Redraw, doc, _FormName.c_str ());
		}
		break;
	case HeaderComments:
		{
			doc.getHeaderPtr ()->Comments = _Value[index];
			modified = true;
			if (!firstTime)
				update (false, Redraw, doc, _FormName.c_str ());
		}
		break;
	case FormArrayRename:
		{
			int idInParent = atoi (_UserData.c_str ());

			// Get the parent node
			const CFormDfn *parentDfn;
			uint indexDfn;
			const CFormDfn *nodeDfn;
			const CType *nodeType;
			CFormElm *node;
			UFormDfn::TEntryType type;
			bool array;
			bool vdfnArray;
			CForm *form=doc.getFormPtr ();
			CFormElm *elm = doc.getRootNode (_Slot);
			nlverify ( elm->getNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
				&nodeDfn, &nodeType, &node, type, array, vdfnArray, true, NLGEORGES_FIRST_ROUND) );
			if (node)
			{
				CFormElmArray* array = safe_cast<CFormElmArray*> (node->getParent ());
				array->Elements[idInParent].Name = _Value[index];
				modified = true;
				update (true, DoNothing, doc, _FormName.c_str ());
			}
		}
		break;
	case FormArrayInsert:
		{
			int idInParent = atoi (_NewValue.c_str ());

			// Insert ?
			if (redo)
			{
				// Get the parent node
				const CFormDfn *parentDfn;
				uint indexDfn;
				const CFormDfn *nodeDfn;
				const CType *nodeType;
				CFormElm *parentNode;
				UFormDfn::TEntryType type;
				bool array;
				CForm *form=doc.getFormPtr ();
				CFormElm *elm = doc.getRootNode (_Slot);
				nlverify ( elm->arrayInsertNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
					&nodeDfn, &nodeType, &parentNode, type, array, true, idInParent) );
				modified = true;
			}
			else
			{
				// Get the parent node
				const CFormDfn *parentDfn;
				uint indexDfn;
				const CFormDfn *nodeDfn;
				const CType *nodeType;
				CFormElm *parentNode;
				UFormDfn::TEntryType type;
				bool array;
				CForm *form=doc.getFormPtr ();
				CFormElm *elm = doc.getRootNode (_Slot);
				nlverify ( elm->arrayDeleteNodeByName (_FormName.c_str (), &parentDfn, indexDfn, 
					&nodeDfn, &nodeType, &parentNode, type, array, true, idInParent) );
				modified = true;
			}

			update (true, Redraw, doc, _FormName.c_str ());
		}
		break;
	default:
		nlstop;
	}

	return ok;
}