/*
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *			(C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#include <dbsdglobal.h>
#include <dialogs.h>

#include <qfile.h>
#include <qtextstream.h>

bool frmTextBrowser::setDisplaySource(const QString & src) {
	setCaption(src);

	QFile  f(src);
	if (f.open(IO_ReadOnly)) {
		QTextStream s(&f);
		s.setEncoding(QTextStream::UnicodeUTF8);
		
		textBrowser->setText(s.read());
		f.close();
	} else
		return false;

	return true;
}


void frmTextBrowser::setDisplayText(const QString & text) {
	textBrowser->setText(text);
}


void frmTextBrowser::init() {
	textBrowser->setReadOnly(true);
	btnClose->setIconSet(DBSDGlobal::getIconLoader()->loadIcon("fileclose", DBSDIconLoader::Button));
	btnClearSearch->setPixmap(DBSDGlobal::getIconLoader()->loadIcon("locationbar_erase", DBSDIconLoader::Button));
	btnSearch->setIconSet(DBSDGlobal::getIconLoader()->loadIcon("find", DBSDIconLoader::Button));
}


void frmTextBrowser::find()
{
	if(!textBrowser->find(editSearchText->text(), false, false, true))
	{
		int para = 0, index = 0;
		textBrowser->getCursorPosition(&para, &index);
		if(para != 0 && index != 0)
		{
			if(Dialogs::question(tr("No matches found. Start from the beginning?")))
			{
				textBrowser->setCursorPosition(0, 0);
				find();
			}
		}
		else
			Dialogs::warnDlg(tr("No matches found."));
	}
}


void frmTextBrowser::clearSearch()
{
	editSearchText->clear();
}
