/***************************************************************************
 *   Copyright (C) 2003-2004 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "unary.h"

#include "libraryitem.h"
#include "flowcode.h"

#include <tdelocale.h>

Item* Unary::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
	return new Unary( (ICNDocument*)itemDocument, newItem, id );
}

LibraryItem* Unary::libraryItem()
{
	return new LibraryItem(
		TQString("flow/unary"),
		i18n("Unary"),
		i18n("Variables"),
		"unary.png",
		LibraryItem::lit_flowpart,
		Unary::construct );
}

Unary::Unary( ICNDocument *icnDocument, bool newItem, const char *id )
	: FlowPart( icnDocument, newItem, (id) ? id : "unary" )
{
	m_name = i18n("Unary");
	m_desc =  i18n("A unary operation involves only one variable. Suppo operations are:<br><ul><li><b>Rotate Left</b> rotates the binary bits of the variable left (discarding the end bits).</li><li><b>Rotate Right</b> rotates the binary bits right (discarding the start bits).</li><li><b>Increment</b> increases the value of the variable by 1. A value of 255 wraps around to 0.</li><li><b>Decrement</b> decreases the value of a variable by 1. A value of 0 wraps around to 255.</li></ul>");
	initProcessSymbol();
	createStdInput();
	createStdOutput();
	
	createProperty( "0-var", Variant::Type::VarName );
	property("0-var")->setValue("x");
	property("0-var")->setCaption( i18n("Variable") );
	
	createProperty( "1-op", Variant::Type::Select );
	property("1-op")->setCaption( i18n("Operation") );
	property("1-op")->setAllowed( TQStringList::split( ',', "Rotate Left,Rotate Right,Increment,Decrement" ) );
	property("1-op")->setValue("Rotate Left");
}

Unary::~Unary()
{
}

void Unary::dataChanged()
{
	setCaption( dataString("0-var") + " " + dataString("1-op") );
}

void Unary::generateMicrobe( FlowCode *code )
{
	const TQString var = dataString("0-var");
	const TQString op = dataString("1-op");
	
	if		( op == "Rotate Left" )		code->addCode( "rotateleft "+var );
	else if ( op == "Rotate Right" )	code->addCode( "rotateright "+var );
	else if ( op == "Increment" )		code->addCode( "increment "+var );
	else if ( op == "Decrement" )		code->addCode( "decrement "+var );
	else; // Hmm...
	code->addCodeBranch( outputPart("stdoutput") );
	
#if 0
	TQString rot = dataString("1-rot");
	
	if ( FlowCode::isLiteral(var) ) return;
	
	TQString newCode;
	
	code->addVariable(var);
	if ( rot == "Left" ) newCode += "rlf " + var + ",1 ; Unary " + var + " left through Carry, place result back in " + var + "\n";
	else newCode += "rrf " + var + ",1 ; Unary " + var + " right through Carry, place result back in " + var + "\n";

	newCode += gotoCode("stdoutput");
	code->addCodeBlock( id(), newCode );
#endif
}


