// -*- c++ -*-

/* $Id: XmlWidget.cpp,v 1.2 2003/10/10 21:26:56 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 "derived.h"
#include <glade/glade-xml.h>

namespace Gnome {
namespace Glade {

/** DerivedBase constructors
 */
DerivedBase::DerivedBase(const std::string& filename,
	const std::string& widgetname)
{
	try {
		xml_ = Gnome::Glade::Xml::create(filename);
	} catch (Gnome::Glade::XmlError&) {
		throw;
	}
	baseWidget = glade_xml_get_widget(xml_->gobj(), widgetname.c_str());
	if (!baseWidget) {
		throw WidgetNotFoundError("Base widget \"" + widgetname +
			"not found in glade file \"" +
			xml_->get_filename() + "\".");
	}
}

/**
 */
DerivedBase::DerivedBase(Glib::RefPtr<Gnome::Glade::Xml> xml,
	const std::string& widgetname)
{
	xml_ = xml;
	baseWidget = glade_xml_get_widget(xml->gobj(), widgetname.c_str());
	if (!baseWidget) {
		throw WidgetNotFoundError("Base widget \"" + widgetname +
			"not found in glade file \"" + xml->get_filename() + "\".");
	}
}

/** DerivedBase::get_widget()
 */
Gtk::Widget* DerivedBase::get_widget(const Glib::ustring& name)
{
	Gtk::Widget* widget = xml_->get_widget(name);
	if (!widget) {
		throw WidgetNotFoundError("Widget \"" + name +
			"\" not found in glade file \"" +
			xml_->gobj()->filename + "\".");
	}
	return widget;
}

/** DerivedBase::get_widget_checked()
 */
Gtk::Widget* DerivedBase::get_widget_checked(const Glib::ustring& name,
	GType type)
{
	Gtk::Widget* widget = get_widget(name);
	if (!g_type_is_a(G_OBJECT_TYPE(widget->gobj()), type)) {
		throw WidgetCastError("Widget \"" + name + "\" (in glade file \"" +
			xml_->gobj()->filename + "\") is of type " +
			G_OBJECT_TYPE_NAME(widget->gobj()) + " but " +
			g_type_name(type) + " was expected.");
	}
	return widget;
}

} // namespace Glade
} // namespace Gnome
