/*
* Copyright (C) 2007 André Gaul <gaul@web-yard.de>
* 
* 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.
*
*/

#ifndef __widgetloader_builder_include__
#define __widgetloader_builder_include__

#include <gtkmm.h>
#include <gtk/gtkbuilder.h>

namespace Gtk
{
/*
* WidgetLoader template class
*/
template <class T_WIDGET> class BuilderWidgetLoader
: public T_WIDGET
{
    public:

    /*
    * constructor takes a RefPtr<Builder> to the glade data
    * and the glade name of the widget (of type T_WIDGET) 
    * to load.
    */
    BuilderWidgetLoader(const Glib::RefPtr<Builder>& refbuilder, const Glib::ustring& widgetname)
    /*
    * with help of get_widget the underlying
    * T_WIDGET class is initialized 
    */
    : T_WIDGET( get_widget(refbuilder, widgetname))
    {}

private:

    typedef typename T_WIDGET::BaseObjectType widget_type;

    /*
    * static helper function
    * returns pointer to GtkWidget object
    * (if widgetname exists in glade xml)
    */
    static widget_type *get_widget(
          const Glib::RefPtr<Builder>& refbuilder
        , const Glib::ustring&         widgetname
    ) 
    {
        //get pointer from glade 
        widget_type *base_widget = (widget_type *)gtk_builder_get_object( refbuilder->gobj(), widgetname.c_str() );
                
        if (!base_widget) 
            throw Glib::Error("Base widget \"" + widgetname + "not found in UI.");
                
        //check reference count
        Glib::ObjectBase *object_base = Glib::ObjectBase::_get_current_wrapper((GObject*) base_widget );

        if (object_base)
        {
            throw Glib::Error("object already exists") ;
        }
        else
        {
            Glib::RefPtr<Builder> refThis (refbuilder);
            //increase reference count
            refThis->reference(); 
        }

        return base_widget;
    };
};
}

#endif
