/*
Copyright (C) 2006 - 2009 Evan Teran
                   eteran@alum.rit.edu

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.

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 "DialogInputValue.h"
#include "QULongValidator.h"
#include "QLongValidator.h"
#include "Debugger.h"

#include <QRegExpValidator>

#if defined(EDB_X86)
	#define SNUM_FMT	"%d"
	#define UNUM_FMT	"%u"
	#define MAX_HEX		"8"
#elif defined(EDB_X86_64)
	#define SNUM_FMT	"%lld"
	#define UNUM_FMT	"%llu"
	#define MAX_HEX		"16"
#endif

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
DialogInputValue::DialogInputValue(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
		
	ui.hexInput->setValidator(new QRegExpValidator(QRegExp("[A-Fa-f0-9]{0," MAX_HEX "}"), this));
	ui.signedInput->setValidator(new QLongValidator(LONG_MIN, LONG_MAX, this));
	ui.unsignedInput->setValidator(new QULongValidator(0, ULONG_MAX, this));
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
edb::reg_t DialogInputValue::value() const {
	bool ok;
	return ui.hexInput->text().toULongLong(&ok, 16);
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogInputValue::setValue(edb::reg_t value) {
	QString temp;
	ui.hexInput->setText(edb::v1::formatPointer(value));
	ui.signedInput->setText(temp.sprintf(SNUM_FMT, value));
	ui.unsignedInput->setText(temp.sprintf(UNUM_FMT, value));
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogInputValue::on_hexInput_textEdited(const QString &s) {
	bool ok;
	edb::reg_t value = s.toULongLong(&ok, 16);
	
	if(!ok) {
		value = 0;
	}
	
	QString temp;
	ui.signedInput->setText(temp.sprintf(SNUM_FMT, value));
	ui.unsignedInput->setText(temp.sprintf(UNUM_FMT, value));
	
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogInputValue::on_signedInput_textEdited(const QString &s) {
	bool ok;
	edb::reg_t value = s.toLongLong(&ok, 10);
	
	if(!ok) {
		value = 0;
	}
	
	QString temp;
	ui.hexInput->setText(edb::v1::formatPointer(value));
	ui.unsignedInput->setText(temp.sprintf(UNUM_FMT, value));
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
void DialogInputValue::on_unsignedInput_textEdited(const QString &s) {
	bool ok;
	edb::reg_t value = s.toULongLong(&ok, 10);
	
	if(!ok) {
		value = 0;
	}
	QString temp;
	ui.hexInput->setText(edb::v1::formatPointer(value));
	ui.signedInput->setText(temp.sprintf(SNUM_FMT, value));
}
