/* 
 * © 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_SELECTOR_H_
#define _LF_SELECTOR_H_

#include "mforms/selector.h"

#include "lf_view.h"

namespace mforms
{
namespace gtk
{

class SelectorImpl : public ViewImpl
{
  Gtk::Alignment                    *_align;
  Gtk::ComboBoxText                 *_cbox;
  virtual Gtk::Widget *get_outer() const { return _align; }
  virtual Gtk::Widget *get_inner() const { return _cbox; }

protected:
  SelectorImpl(::mforms::Selector *self)
    : ViewImpl(self)
  {
    _align= Gtk::manage(new Gtk::Alignment(0.5, 0.5, 1, 0));
    _cbox= Gtk::manage(new Gtk::ComboBoxText());
    _cbox->show();
    _align->add(*_cbox);

    _cbox->signal_changed().connect(sigc::bind(sigc::ptr_fun(&SelectorImpl::callback), self));
  }

  static void callback(::mforms::Selector *self)
  {
    GtkAutoLock gtk_auto_lock;
    self->callback();
  }

  static bool create(::mforms::Selector *self)
  {
    return new SelectorImpl(self) != 0;
  }

  static void clear(::mforms::Selector *self)
  {
    SelectorImpl* sel= self->get_data<SelectorImpl>();

    sel->_cbox->clear();
  }

  static int add_item(::mforms::Selector *self, const std::string &item)
  {
    SelectorImpl* sel= self->get_data<SelectorImpl>();

    sel->_cbox->append_text(item);
    return sel->_cbox->get_model()->children().size()-1;
  }

  static void add_items(::mforms::Selector *self, const std::list<std::string> &items)
  {
    SelectorImpl* sel= self->get_data<SelectorImpl>();

    if ( sel )
    {
      for (std::list<std::string>::const_iterator iter = items.begin(); iter != items.end(); ++iter)
        add_item(self, *iter);
    }
  }

  static std::string get_text(::mforms::Selector *self)
  {
    SelectorImpl* sel= self->get_data<SelectorImpl>();
    std::string text;
    
    if ( sel )
    {
      return sel->_cbox->get_active_text();
    }
    return "";
  }

  static void set_index(::mforms::Selector *self, int index)
  {
    SelectorImpl* sel= self->get_data<SelectorImpl>();

    if ( sel )
    {
      sel->_cbox->set_active(index);
    }
  }

  static int get_index(::mforms::Selector *self)
  {
    SelectorImpl* sel= self->get_data<SelectorImpl>();

    if ( sel )
    {
      return sel->_cbox->get_active_row_number();
    }
    
    return -1;
  }

  static int get_item_count(::mforms::Selector *self)
  {
    SelectorImpl* sel= self->get_data<SelectorImpl>();

    if ( sel )
    {
      return (int)sel->_cbox->get_model()->children().size();
    }
    
    return -1;
  }

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

    f->_selector_impl.create    = &SelectorImpl::create;
    f->_selector_impl.clear     = &SelectorImpl::clear;
    f->_selector_impl.add_item  = &SelectorImpl::add_item;
    f->_selector_impl.add_items = &SelectorImpl::add_items;
    f->_selector_impl.set_index = &SelectorImpl::set_index;
    f->_selector_impl.get_index = &SelectorImpl::get_index;
    f->_selector_impl.get_text  = &SelectorImpl::get_text;
    f->_selector_impl.get_item_count = &SelectorImpl::get_item_count;
  }
};

}
}


#endif
