//
//   File : class_lcd.cpp
//   Creation date : Mon Gen 27 14:30:48 CEST 2005 by Tonino Imbesi(Grifisx)
//         and Alessandro Carbone(Noldor)
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net)
//
//   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 opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//


#include "class_lcd.h"
#include "kvi_error.h"
#include "kvi_debug.h"

#include "kvi_locale.h"
#include "kvi_iconmanager.h"

/*
	@doc: lcdnumber
	@keyterms:
		lcdnumber object class, lcd
	@title:
		lcdnumber class
	@type:
		class
	@short:
		The lcdNumber widget displays a number with LCD-like digits.
	@inherits:

	@description:
		It can display a number in just about any size. It can display decimal,
		hexadecimal, octal or binary numbers.
	@functions:
		!fn: $setDisplayStr(<number:string>)
		Displays the number represented by the string s.
		!fn: $setDisplayInt(<number:integer>)
		Sets the displayed value rounded to the nearest integer to number.
		!fn: $setDisplayDouble(<number:double>)
		Sets the displayed value to number (double).
		!fn: $setMode(<mode:string>)
		Sets the current display mode (number base): valid mode are HEX,BIN,OCT,DEC.
		!fn: $setSegmentStyles(<style:string>)
		Sets the style of the lcdnumber, valid styles are: Outline,Filled,Flat.
		!fn: $setNumDigits(<number:integer>)
		Sets the lcd number value.
		!fn: <boolean> $checkOverflow()
		Returns TRUE if num is too big to be displayed in its entirety;
		otherwise returns FALSE. 
		!fn: $setSmallDecimalPoint(<bflag:boolean>)
		Sets the the decimal point style, valid bool values are 1 or 0.
		!fn: <boolean> $checkOverflow(<number:integer>)
		Returns 1 (TRUE) if num is too big to be displayed in its entirety; otherwise returns 0 (FALSE).
*/

KVSO_BEGIN_REGISTERCLASS(KviKvsObject_lcd,"lcdnumber","widget")
	
	KVSO_REGISTER_HANDLER(KviKvsObject_lcd,"setDisplayStr", functiondisplayStr)
	KVSO_REGISTER_HANDLER(KviKvsObject_lcd,"setDisplayInt", functiondisplayInt)
	KVSO_REGISTER_HANDLER(KviKvsObject_lcd,"setDisplayDouble", functiondisplayDouble)
	KVSO_REGISTER_HANDLER(KviKvsObject_lcd,"setMode", functionsetMode)
	KVSO_REGISTER_HANDLER(KviKvsObject_lcd,"setSegmentStyle", functionsetSegmentStyle)
	KVSO_REGISTER_HANDLER(KviKvsObject_lcd,"setNumDigits", functionsetNumDigits)
	KVSO_REGISTER_HANDLER(KviKvsObject_lcd,"setSmallDecimalPoint",functionsetSmallDecimalPoint)
	KVSO_REGISTER_HANDLER(KviKvsObject_lcd,"checkOverflow", functioncheckOverflow)
KVSO_END_REGISTERCLASS(KviKvsObject_lcd)

KVSO_BEGIN_CONSTRUCTOR(KviKvsObject_lcd,KviKvsObject_widget)

KVSO_END_CONSTRUCTOR(KviKvsObject_lcd)


KVSO_BEGIN_DESTRUCTOR(KviKvsObject_lcd)

KVSO_END_CONSTRUCTOR(KviKvsObject_lcd)

bool KviKvsObject_lcd::init(KviKvsRunTimeContext * pContext,KviKvsVariantList *pParams)
{
	setObject(TQT_TQOBJECT(new TQLCDNumber(parentScriptWidget(), name())), true);
	return true;
}

bool KviKvsObject_lcd::functiondisplayInt(KviKvsObjectFunctionCall *c)
{
	kvs_int_t iDigit;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("number",KVS_PT_INT,0,iDigit)
	KVSO_PARAMETERS_END(c)
    if(widget())((TQLCDNumber *)widget())->display((int)iDigit);
    return true;
}

bool KviKvsObject_lcd::functiondisplayStr(KviKvsObjectFunctionCall *c)
{
	TQString szText;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("number",KVS_PT_STRING,0,szText)
	KVSO_PARAMETERS_END(c)
    if(widget()) ((TQLCDNumber *)widget())->display(szText);
    return true;
}

bool KviKvsObject_lcd::functiondisplayDouble(KviKvsObjectFunctionCall *c)
{
	kvs_real_t dDigit;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("number",KVS_PT_DOUBLE,0,dDigit)
	KVSO_PARAMETERS_END(c)
    if(widget()) ((TQLCDNumber *)widget())->display(dDigit);
    return true;
}
bool KviKvsObject_lcd::functionsetMode(KviKvsObjectFunctionCall *c)
{
	TQString szMode;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("mode",KVS_PT_STRING,0,szMode)
	KVSO_PARAMETERS_END(c)
	if(!widget())return true;
	if(KviTQString::equalCI(szMode, "HEX")) ((TQLCDNumber *)widget())->setHexMode();
	else if(KviTQString::equalCI(szMode, "DEC")) ((TQLCDNumber *)widget())->setDecMode();
	else if(KviTQString::equalCI(szMode, "BIN")) ((TQLCDNumber *)widget())->setBinMode();
	else if(KviTQString::equalCI(szMode, "OCT")) ((TQLCDNumber *)widget())->setOctMode();
	else c->warning( __tr2qs("Unknown mode "));
	return true;
}
bool KviKvsObject_lcd::functionsetSegmentStyle(KviKvsObjectFunctionCall *c)
{
	TQString szStyle;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("style",KVS_PT_STRING,0,szStyle)
	KVSO_PARAMETERS_END(c)
	if(!widget())return true;
	if(KviTQString::equalCI(szStyle, "Outline")) ((TQLCDNumber *)widget())->setSegmentStyle(TQLCDNumber::Outline);
	else if(KviTQString::equalCI(szStyle, "Filled")) ((TQLCDNumber *)widget())->setSegmentStyle(TQLCDNumber::Filled);
	else if(KviTQString::equalCI(szStyle, "Flat")) ((TQLCDNumber *)widget())->setSegmentStyle(TQLCDNumber::Flat);
	else c->warning( __tr2qs("Unknown segment style "));
	return true;
}
bool KviKvsObject_lcd::functionsetNumDigits(KviKvsObjectFunctionCall *c)
{
	kvs_real_t digit;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("digit",KVS_PT_DOUBLE,0,digit)
	KVSO_PARAMETERS_END(c)
    if(widget()) ((TQLCDNumber *)widget())->setNumDigits(digit);
    return true;
}
bool KviKvsObject_lcd::functionsetSmallDecimalPoint(KviKvsObjectFunctionCall *c)
{
	bool bFlag;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("bflag",KVS_PT_BOOLEAN,0,bFlag)
	KVSO_PARAMETERS_END(c)
    if(widget()) ((TQLCDNumber *)widget())->setSmallDecimalPoint(bFlag);
	return true;
}
bool KviKvsObject_lcd::functioncheckOverflow(KviKvsObjectFunctionCall *c)
{
	kvs_real_t iDigit;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("digit",KVS_PT_DOUBLE,0,iDigit)
	KVSO_PARAMETERS_END(c)
    if(widget())c->returnValue()->setBoolean(((TQLCDNumber *)widget())->checkOverflow(iDigit));
    return true;
}