/* AbiSource Application Framework
 * Copyright (C) 1998 AbiSource, Inc.
 * Copyright (C) 2005 Hubert Figuiere
 * 
 * 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., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */

#include <string.h>

#include "ut_assert.h"
#include "ut_debugmsg.h"
#include "ut_string_class.h"

#include "xap_Dialog.h"
#include "xap_DialogFactory.h"
#include "xap_App.h"
#include "xap_Frame.h"
#include "xap_Widget.h"

/*****************************************************************/

XAP_Dialog::XAP_Dialog(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id,
		       const char * helpUrl )
  : m_pApp ( NULL ), m_pDlgFactory ( pDlgFactory ), m_id ( id ), 
	m_helpUrl(NULL)
{
  m_pApp = pDlgFactory->getApp();

  if (helpUrl)
    m_helpUrl = new UT_String (helpUrl);
  else
    m_helpUrl = new UT_String (); // create an empty one
}

XAP_Dialog::~XAP_Dialog(void)
{
	delete m_helpUrl;
}


int XAP_Dialog::getWidgetValueInt(xap_widget_id wid)
{
	XAP_Widget *w =	getWidget(wid);
	int retval = w->getValueInt();
	delete w;
	return retval;
}


void XAP_Dialog::setWidgetValueInt(xap_widget_id wid, int value)
{
	XAP_Widget *w = getWidget(wid);
	w->setValueInt(value);
	delete w;
}

void XAP_Dialog::setWidgetLabel(xap_widget_id wid, const UT_UTF8String &val)
{
	XAP_Widget *w = getWidget(wid);
	w->setLabel(val);
	delete w;
}

/*****************************************************************/

XAP_Dialog_NonPersistent::XAP_Dialog_NonPersistent(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id, const char * helpUrl )
	: XAP_Dialog(pDlgFactory,id, helpUrl)
{
}

XAP_Dialog_NonPersistent::~XAP_Dialog_NonPersistent(void)
{
}

/*****************************************************************/

XAP_TabbedDialog_NonPersistent::XAP_TabbedDialog_NonPersistent(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id, const char * helpUrl )
	: XAP_Dialog_NonPersistent(pDlgFactory,id, helpUrl),
	  m_pageNum(-1)
{
}

XAP_TabbedDialog_NonPersistent::~XAP_TabbedDialog_NonPersistent(void)
{
}

/*****************************************************************/

XAP_Dialog_Persistent::XAP_Dialog_Persistent(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id, const char * helpUrl)
	: XAP_Dialog(pDlgFactory,id, helpUrl),
	  m_bInUse(false)
{
}

XAP_Dialog_Persistent::~XAP_Dialog_Persistent(void)
{
}

void XAP_Dialog_Persistent::useStart(void)
{
	UT_ASSERT(!m_bInUse);
	m_bInUse = true;
}

void XAP_Dialog_Persistent::useEnd(void)
{
	UT_ASSERT(m_bInUse);
	m_bInUse = false;
}

/*****************************************************************/

XAP_Dialog_FramePersistent::XAP_Dialog_FramePersistent(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id, const char * helpUrl)
	: XAP_Dialog_Persistent(pDlgFactory,id, helpUrl)
{
}

XAP_Dialog_FramePersistent::~XAP_Dialog_FramePersistent(void)
{
}

void XAP_Dialog_FramePersistent::useStart(void)
{
	XAP_Dialog_Persistent::useStart();
}

void XAP_Dialog_FramePersistent::useEnd(void)
{
	XAP_Dialog_Persistent::useEnd();
}

/*****************************************************************/

XAP_Dialog_AppPersistent::XAP_Dialog_AppPersistent(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id, const char * helpUrl)
	: XAP_Dialog_Persistent(pDlgFactory,id, helpUrl)
{
}

XAP_Dialog_AppPersistent::~XAP_Dialog_AppPersistent(void)
{
}

void XAP_Dialog_AppPersistent::useStart(void)
{
	XAP_Dialog_Persistent::useStart();
}

void XAP_Dialog_AppPersistent::useEnd(void)
{
	XAP_Dialog_Persistent::useEnd();
}



XAP_Dialog_Modeless::XAP_Dialog_Modeless(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id id, const char * helpUrl)
	: XAP_Dialog_AppPersistent(pDlgFactory,id, helpUrl)
{
	UT_ASSERT(pDlgFactory);

	m_pDlgFactory = pDlgFactory;
	m_id = id;
	m_pApp = pDlgFactory->getApp();
        m_pDialog = (XAP_Dialog_Modeless *) this;

	UT_ASSERT(m_pApp);
}

XAP_Dialog_Modeless::~XAP_Dialog_Modeless(void)
{
}

void XAP_Dialog_Modeless::useStart(void)
{
  xxx_UT_DEBUGMSG(("Called generic useStart in XAP_DIALOG_Modeless \n"));
}

void XAP_Dialog_Modeless::useEnd(void)
{
  xxx_UT_DEBUGMSG(("Called generic useEnd in XAP_DIALOG_Modeless \n"));
}

void XAP_Dialog_Modeless::modeless_cleanup(void)
{
	UT_sint32 sid = (UT_sint32) getDialogId();
	m_pApp->forgetModelessId( (UT_sint32) sid);
	m_pDlgFactory->releaseDialog(m_pDialog);
}

bool XAP_Dialog_Modeless::isRunning(void)
{
 
	UT_sint32 sid = (UT_sint32) getDialogId();
	return m_pApp->isModelessRunning(sid);
}

XAP_Frame *   XAP_Dialog_Modeless::getActiveFrame(void) const
{
	// This function returns the frame currently connected to a modeless dialog

	XAP_Frame * pFrame = m_pApp->getLastFocussedFrame();
	if(pFrame == (XAP_Frame *) NULL)
	{
		pFrame = m_pApp->getFrame(0);
	}
	return pFrame;
}


void XAP_Dialog_Modeless::setActiveFrame(XAP_Frame *pFrame)
{
	notifyActiveFrame(pFrame);
}


void XAP_Dialog_Modeless::BuildWindowName(char * pWindowName, char * pDialogName, UT_uint32 width)
{
// This function constructs and returns the window name of a modeless dialog by
// concatenating the active frame with the dialog name

	const UT_sint32 count = (UT_sint32) strlen(pDialogName);

	*pWindowName = (char) NULL;
	UT_UTF8String wn = UT_UTF8String(pDialogName);

	XAP_Frame* pFrame = getActiveFrame();
	if (pFrame)
	{
		wn += " - ";
		wn += pFrame->getTitle(width-count-3);
	}

	UT_uint32 len = UT_MIN(wn.byteLength(), width);
	strncpy(pWindowName, wn.utf8_str(), len);
	pWindowName[len] = '\0';
}


