// -*- c++ -*-
#ifndef _LIBGLADEMM_DERIVED_H
#define _LIBGLADEMM_DERIVED_H

/* $Id: XmlWidget.h,v 1.2 2003/10/06 23:34:54 palm Exp $ */

/* 
 * Copyright (C) 2003 Christer Palm <palm@nogui.se>
 *
 * 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 <gtkmm.h>
#include <libglademm/xml.h>

namespace Gnome
{
namespace Glade
{


/** Thrown when a requested widget is not found in XML file
 */
class WidgetNotFoundError : public XmlError
{
public:
	explicit WidgetNotFoundError(const Glib::ustring& message) :
		XmlError(message)
		{ }
	virtual ~WidgetNotFoundError() throw()
		{ }
};


/** Thrown when an invalid cast is attempted
 */
class WidgetCastError : public XmlError
{
public:
	explicit WidgetCastError(const Glib::ustring& message) :
		XmlError(message)
		{ }
	virtual ~WidgetCastError() throw()
		{ }
};


/** Base class for Derived widget
 */
class DerivedBase
{
private:
	Glib::RefPtr<Gnome::Glade::Xml> xml_;
	GtkWidget* baseWidget;

protected:
	DerivedBase(Glib::RefPtr<Gnome::Glade::Xml> xml,
		const std::string& widgetname);
	DerivedBase(const std::string& filename, const std::string& widgetname);

	inline GtkWidget* get_base_widget()
		{ return baseWidget; }

private:
	Gtk::Widget* get_widget_checked(const Glib::ustring& name, GType type);

public:
	/** Returns a Glib::RefPtr to the libglademm Xml object to which this
	 *  widget belongs
     */
	Glib::RefPtr<Gnome::Glade::Xml> get_xml()
		{ return xml_; }

	/** Looks up the widget @a
	 */
	Gtk::Widget* get_widget(const Glib::ustring& name);

	/**
	 */
	template <class T_Widget>
	inline T_Widget* get_widget(const Glib::ustring& name, T_Widget*& widget)
	{
		widget = dynamic_cast<T_Widget*>
			(get_widget_checked(name, T_Widget::get_base_type()));
		if (!widget) {
			throw Exception("dynamic_cast<> failed.");
		}
		return widget;
	}
};

/** Convenience template for creating derived libGlade widgets
 */
template<class T_widget>
class Derived : public DerivedBase, public T_widget
{
public:
	Derived(const std::string& filename, const std::string& widgetname) :
		DerivedBase(filename, widgetname),
		T_widget((typename T_widget::BaseObjectType*)
			DerivedBase::get_base_widget())
		{ }

	Derived(Glib::RefPtr<Gnome::Glade::Xml> xml, const std::string& widgetname) :
		DerivedBase(xml, widgetname),
		T_widget((typename T_widget::BaseObjectType*)
			DerivedBase::get_base_widget())
		{ }
};

} // namespace Glade
} // namespace Gnome

#endif // _LIBGLADEMM_DERIVED_H
