/*
 * Copyright 2003 The gtkmm Development Team
 *
 * 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <gtkmm/liststore.h>
#include <gtkmm/cellrenderertext.h>
#include <gtkmm/treeview_private.h> //For SignalProxy_RowSeparator.
#include <gtk/gtk.h>

namespace Gtk
{

ComboBox::ComboBox(bool has_entry)
:
  _CONSTRUCT("has-entry", gboolean(has_entry))
{}

ComboBox::ComboBox(const Glib::RefPtr<TreeModel>& model, bool has_entry)
:
  _CONSTRUCT("model", Glib::unwrap(model), "has-entry", gboolean(has_entry))
{}

ComboBox::ComboBox(const Glib::RefPtr<CellArea>& cell_area, bool has_entry)
:
  _CONSTRUCT("cell-area", Glib::unwrap(cell_area), "has-entry", gboolean(has_entry))
{}

void ComboBox::unset_active()
{
  gtk_combo_box_set_active(gobj(), -1 /* see GTK+ docs */);
}

TreeModel::iterator ComboBox::get_active()
{
  Gtk::TreeModel::iterator iter;

  auto model = get_model();
  if(model)
  {
    gtk_combo_box_get_active_iter(gobj(), iter.gobj());

    //It must be given the model, because the C++ wrapper has extra information.
    iter.set_model_gobject(model->gobj());
  }

  return iter;
}

TreeModel::const_iterator ComboBox::get_active() const
{
  Gtk::TreeModel::iterator iter;

  const auto model = get_model();
  if(model)
  {
    gtk_combo_box_get_active_iter(const_cast<GtkComboBox*>(gobj()), iter.gobj());

    //It must be given the model, because the C++ wrapper has extra information.
    iter.set_model_gobject(const_cast<GtkTreeModel*>(model->gobj()));
  }

  return iter;
}


void ComboBox::unset_model()
{
  gtk_combo_box_set_model(gobj(), 0);
}


void ComboBox::set_row_separator_func(const SlotRowSeparator& slot)
{
  //Create a copy of the slot. A pointer to this will be passed through the callback's data parameter.
  //It will be deleted when SignalProxy_RowSeparator_gtk_callback_destroy() is called.
  auto slot_copy = new SlotRowSeparator(slot);

  gtk_combo_box_set_row_separator_func(gobj(),
      &TreeView_Private::SignalProxy_RowSeparator_gtk_callback, slot_copy,
      &TreeView_Private::SignalProxy_RowSeparator_gtk_callback_destroy);
}

void ComboBox::unset_row_separator_func()
{
  gtk_combo_box_set_row_separator_func(gobj(), 0, 0, 0 /* See C docs. */);
}

Entry* ComboBox::get_entry()
{
  auto widget = Glib::wrap(gtk_bin_get_child(GTK_BIN(gobj())));
  return dynamic_cast<Gtk::Entry*>(widget);
}

const Entry* ComboBox::get_entry() const
{
  const auto widget = Glib::wrap(gtk_bin_get_child(GTK_BIN(gobj())));
  return dynamic_cast<const Gtk::Entry*>(widget);
}

Glib::ustring ComboBox::get_entry_text() const
{
  const auto entry = get_entry();
  if(!entry)
    return Glib::ustring();
  else
    return entry->get_text();
}


} // namespace Gtk
