/* 
 * © 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#ifndef _LF_VIEW_H_
#define _LF_VIEW_H_

#include <mforms/mforms.h>


#include "lf_base.h"
#include "lf_mforms.h"

namespace mforms { namespace gtk {

class ViewImpl : public ObjectImpl
{
public:
  // get the widget that must be used to add it to other containers
  virtual Gtk::Widget *get_outer() const= 0;

  // get the widget that does the actual work. most of the time it will be the same as the outer one
  virtual Gtk::Widget *get_inner() const
  {
    return get_outer();
  }

protected:
  ViewImpl(::mforms::View *view)
    : ObjectImpl(view)
  {
  }

  static void show(::mforms::View *self, bool show)
  {
    ViewImpl *view = self->get_data<ViewImpl>();

    if ( view )
    {
      Gtk::Widget* widget = view->get_outer();
      if (show)
        widget->show();
      else
        widget->hide();
    }
  }
  
  static bool is_shown(::mforms::View *self)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if (view)
      return view->get_outer()->is_visible();
    return false;
  }


  static void set_tooltip(::mforms::View *self, const std::string &text)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if (view)
    {
      view->get_outer()->set_tooltip_text(text);
      view->get_outer()->set_has_tooltip(!text.empty());
    }
  }


  static int get_width(::mforms::View *self)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if ( view )
    {
      Gtk::Widget* widget = view->get_outer();
      //int w, h;
      //widget->get_size_request(w, h);
      return widget->get_allocation().get_width();
    }
    return 0;
  }

  static int get_height(::mforms::View *self)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if ( view )
    {
      Gtk::Widget* widget = view->get_outer();
      //int w, h;
      //widget->get_size_request(w, h);
      return widget->get_allocation().get_height();
    }
    return 0;
  }

  static int get_preferred_width(::mforms::View *self)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if ( view )
    {
      return view->get_preferred_width();
    }
    return 0;
  }
  
  virtual int get_preferred_width()
  {
    int w, h;
    get_outer()->get_size_request(w, h);
    return w;
  }

  static int get_preferred_height(::mforms::View *self)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if ( view )
      return view->get_preferred_height();
    return 0;
  }
  
  virtual int get_preferred_height()
  {
    int w, h;
    get_outer()->get_size_request(w, h);
    return h;
  }

  static int get_x(::mforms::View *self)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if ( view )
    {
      Gtk::Widget* widget = view->get_outer();
      return widget->get_allocation().get_x();
    }
    return 0;
  }

  static int get_y(::mforms::View *self)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if ( view )
    {
      Gtk::Widget* widget = view->get_outer();
      return widget->get_allocation().get_y();
    }
    return 0;
  }
  
  static void set_size(::mforms::View *self, int w, int h)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if ( view )
      view->set_size(w, h);
  }
  
  virtual void set_size(int width, int height)
  {
    get_outer()->set_size_request(width, height);
  }

  static void set_position(::mforms::View *self, int x, int y)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if (view)
    {
      self->get_parent()->get_data<ViewImpl>()->move_child(view, x, y);
    }
  }

  static void set_enabled(::mforms::View *self, bool flag)
  {
    ViewImpl *view = self->get_data<ViewImpl>();
    if (view)
    {
      Gtk::Widget* widget = view->get_outer();
      widget->set_sensitive(flag);
    }
  }


  static void set_name(::mforms::View *view, const std::string &name)
  {
  }
  
  static void relayout(::mforms::View *view)
  {
    // noop
  }


  void size_changed()
  {
    if (get_outer()->is_realized())
      ((::mforms::View*)owner)->on_resize();
  }

  void setup()
  {
    get_outer()->signal_size_allocate().connect(sigc::hide(sigc::mem_fun(this, &ViewImpl::size_changed)));
    get_outer()->signal_realize().connect(sigc::mem_fun(this, &ViewImpl::size_changed));
  }
  
  
  virtual void move_child(ViewImpl *child, int x, int y)
  {
    throw std::logic_error("container does not implement required method");
  }

  static void set_layout_dirty(::mforms::View* /*self*/)
  {
  }

public:
  static void init()
  {
    ::mforms::ControlFactory *f= ::mforms::ControlFactory::get_instance();

    f->_view_impl.get_width            = &ViewImpl::get_width;
    f->_view_impl.get_height           = &ViewImpl::get_height;
    f->_view_impl.get_preferred_width  = &ViewImpl::get_preferred_width;
    f->_view_impl.get_preferred_height = &ViewImpl::get_preferred_height;
    f->_view_impl.set_size             = &ViewImpl::set_size;

    f->_view_impl.get_x                = &ViewImpl::get_x;
    f->_view_impl.get_y                = &ViewImpl::get_y;
    f->_view_impl.set_position         = &ViewImpl::set_position;

    f->_view_impl.show                 = &ViewImpl::show;
    f->_view_impl.is_shown             = &ViewImpl::is_shown;

    f->_view_impl.set_tooltip          = &ViewImpl::set_tooltip;
    f->_view_impl.set_name             = &ViewImpl::set_name;

    f->_view_impl.set_enabled          = &ViewImpl::set_enabled;
    f->_view_impl.relayout             = &ViewImpl::relayout;
    f->_view_impl.set_layout_dirty     = &ViewImpl::set_layout_dirty;
  };
};

};
};

#endif
