// -*- c++ -*-
/* $Id: box.ccg,v 1.1 2003/01/21 13:38:42 murrayc Exp $ */

/* 
 *
 * Copyright 1998-2002 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gtk/gtk.h>
#include <glibmm/wrap.h>

namespace Gtk
{

namespace Box_Helpers
{

Widget* Child::get_widget() const
{ 
  return Glib::wrap(gobj()->widget);
}

void Child::set_options(PackOptions options, guint padding)
{
  const bool expand = (options == PACK_EXPAND_PADDING || options == PACK_EXPAND_WIDGET);
  const bool fill   = (options == PACK_EXPAND_WIDGET);

  set_options(expand, fill, padding);
}

void Child::set_options(bool expand, bool fill, guint padding)
{
  gobj()->expand  = expand;
  gobj()->fill    = fill;
  gobj()->padding = padding;
}

void Child::set_pack(PackType pack)
{
  gobj()->pack = pack;
}


/**************************************************************************/


typedef Box_Helpers::BoxList::iterator box_iterator;

box_iterator BoxList::insert(box_iterator position, const Element& e)
{
  iterator i;
  bool expand = (e.options_ == PACK_EXPAND_PADDING) || (e.options_ == PACK_EXPAND_WIDGET);
  bool fill = (e.options_ == PACK_EXPAND_WIDGET);

  if (e.pack_ == PACK_START)
    gtk_box_pack_start(gparent(), (e.widget_? e.widget_->gobj() : 0),
                       (gboolean)expand, (gboolean)fill, e.padding_);
  else
    gtk_box_pack_end(gparent(), (e.widget_ ? e.widget_->gobj() : 0),
                       (gboolean)expand, (gboolean)fill, e.padding_);

  i = --end();

  if (position!=end())
	{
    reorder(i, position);
    i = --position;
  }
  return i;
}

// Non-standard
void BoxList::reorder(box_iterator loc, box_iterator pos)
{
  int position = g_list_position(glist(), pos.node_);
  gtk_box_reorder_child(gparent(), loc->gobj()->widget, position);
}

} /* namespace Box_Helpers */

Box::BoxList& Box::children()
{
  children_proxy_ = BoxList(gobj());
  return children_proxy_;
}

const Box::BoxList& Box::children() const
{
  children_proxy_ = BoxList(const_cast<GtkBox*>(gobj()));
  return children_proxy_;
}

void Box::pack_start(Widget& child, PackOptions options, guint padding)
{
  bool expand = (options == PACK_EXPAND_PADDING) || (options == PACK_EXPAND_WIDGET);
  bool fill = (options == PACK_EXPAND_WIDGET);

  gtk_box_pack_start(gobj(), child.gobj(), (gboolean)expand, (gboolean)fill, padding);
}

void Box::pack_end(Widget& child, PackOptions options, guint padding)
{
  bool expand = (options == PACK_EXPAND_PADDING) || (options == PACK_EXPAND_WIDGET);
  bool fill = (options == PACK_EXPAND_WIDGET);

  gtk_box_pack_end(gobj(), child.gobj(), (gboolean)expand, (gboolean)fill, padding);
}


} /* namespace Gtk */

