// -*- c++ -*-
/* $Id: notebook.ccg,v 1.6 2006/06/16 21:40:01 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 <gtkmm/label.h>
#include <algorithm>


namespace
{
extern "C"
{

_DEPRECATE_IFDEF_START
static GtkNotebook* SignalProxy_WindowCreation_gtk_callback(GtkNotebook*, GtkWidget* page,
                                                            int x, int y, void* data)
{
  Gtk::Notebook::SlotWindowCreation *const
    slot = static_cast<Gtk::Notebook::SlotWindowCreation*>(data);
  try
  {
    return Glib::unwrap((*slot)(Glib::wrap(page), x, y));
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  return 0;
}
_DEPRECATE_IFDEF_END

static void SignalProxy_WindowCreation_gtk_callback_destroy(void* data)
{
  delete static_cast<Gtk::Notebook::SlotWindowCreation*>(data);
}

} // extern "C"
} // anonymous namespace

namespace Gtk
{

namespace Notebook_Helpers
{

/**** Gtk::Notebook_Helpers::PageIterator **********************************/

bool PageIterator::equal(const PageIterator& other) const
{
  return (node_ == other.node_);
}

PageIterator::operator bool() const
{
  return (node_ != 0);
}

PageIterator& PageIterator::operator++()
{
  g_return_val_if_fail(node_ != 0, *this);

  node_ = node_->next;
  return *this;
}

const PageIterator PageIterator::operator++(int)
{
  const PageIterator tmp (*this);
  ++*this;
  return tmp;
}

PageIterator& PageIterator::operator--()
{
  node_ = (node_) ? node_->prev : g_list_last(parent_->gobj()->children);
  return *this;
}

const PageIterator PageIterator::operator--(int)
{
  const PageIterator tmp (*this);
  --*this;
  return tmp;
}


/*********************************************************************
***** Page Properties
*********************************************************************/
_DEPRECATE_IFDEF_START
int Page::get_page_num() const
{
  return g_list_position(parent_->gobj()->children, node_);
}

Widget* Page::get_child() const
{
  return parent_->get_nth_page(get_page_num());
}

Widget* Page::get_tab_label() const
{
  return parent_->get_tab_label(*get_child());
}

void Page::set_tab_label(Widget& tab_label)
{
  parent_->set_tab_label(*get_child(), tab_label);
}

void Page::set_tab_label_text(const Glib::ustring& tab_text)
{
  parent_->set_tab_label_text(*get_child(), tab_text);
}

Glib::ustring Page::get_tab_label_text() const
{
  return parent_->get_tab_label_text(*get_child());
}

Widget* Page::get_menu_label() const
{
  return parent_->get_menu_label(*get_child());
}

void Page::set_menu_label(Widget& menu_label)
{
  parent_->set_menu_label(*get_child(), menu_label);
}

void Page::set_menu_label_text(const Glib::ustring& menu_text)
{
  parent_->set_menu_label_text(*get_child(), menu_text);
}

Glib::ustring Page::get_menu_label_text() const
{
  return parent_->get_menu_label_text(*get_child());
}

void Page::query_tab_label_packing(bool& expand, bool& fill, PackType& pack_type)
{
  parent_->query_tab_label_packing(*get_child(), expand, fill, pack_type);
}

void Page::set_tab_label_packing(bool expand, bool fill, PackType pack_type)
{
  parent_->set_tab_label_packing(*get_child(), expand, fill, pack_type);
}
_DEPRECATE_IFDEF_END

/*********************************************************************
***** List properties
*********************************************************************/
PageList::PageList()
:
  gparent_ (0)
{}

PageList::PageList(GtkNotebook* gparent)
:
  gparent_ (gparent)
{}

PageList::PageList(const PageList& src)
:
  gparent_ (src.gparent_)
{}

PageList& PageList::operator=(const PageList& src)
{
  gparent_ = src.gparent_;
  return *this;
}

_DEPRECATE_IFDEF_START
PageList::size_type PageList::max_size() const
{
  return G_MAXINT;
}

bool PageList::empty() const
{
  return (gparent_->children == 0);
}

PageList::size_type PageList::size() const
{
  return g_list_length(gparent_->children);
}

PageList::value_type PageList::front() const
{
  return *begin();
}

PageList::value_type PageList::back() const
{
  return *--end();
}

PageList::value_type PageList::operator[](size_type index) const
{
  GList *const node = g_list_nth(gparent_->children, index);
  g_assert(node != 0);

  return *iterator(Glib::wrap(gparent_), node);
}

PageList::iterator PageList::insert(PageList::iterator position, const Element& elem)
{
  // Get the iterator index, or -1 for end().
  const int index = (position) ? position->get_page_num() : -1;

  // Give item to GTK+.
  gtk_notebook_insert_page_menu(gparent_,
                                Glib::unwrap(elem.child_),
                                Glib::unwrap(elem.tab_),
                                Glib::unwrap(elem.menu_),
                                index);

  // Return an iterator pointing to the inserted element.
  return --position;
}

void PageList::erase(PageList::iterator start, PageList::iterator stop)
{
  while(start != stop)
    start = erase(start);
}

PageList::iterator PageList::erase(PageList::iterator position)
{
  iterator next (position);

  if(position)
  {
    ++next;
    gtk_container_remove(GTK_CONTAINER(gparent_), position->get_child()->gobj());
  }

  return next;
}

void PageList::remove(const_reference child)
{
//  g_return_if_fail(child.parent_ != 0);
  gtk_container_remove(GTK_CONTAINER(gparent_), child.get_child()->gobj());
}

void PageList::remove(Widget& widget)
{
  gtk_container_remove(GTK_CONTAINER(gparent_), widget.gobj());
}

void PageList::reorder(PageList::iterator position, PageList::iterator page)
{
  gtk_notebook_reorder_child(gparent(), page->get_child()->gobj(), position->get_page_num());
}

PageList::iterator PageList::find(int num)
{
  if (num < 0)
    return end();
  iterator i = begin();
  std::advance(i, num);
  return i;
}

PageList::iterator PageList::find(const_reference c)
{
  iterator i = begin();
  while (i != end() && ((*i).get_child() != c.get_child()))
    ++i;
  return i;
}

PageList::iterator PageList::find(Widget& widget)
{
  iterator i = begin();
  while (i != end() && ((*i).get_child() != &widget))
    ++i;
  return i;
}

PageList::iterator PageList::find(GtkNotebookPage* page)
{
  GList* node = gparent_->children;

  while(node && node->data != page)
    node = node->next;

  return (node) ? iterator(Glib::wrap(gparent_), node) : end();
}

void PageList::clear()
{
  erase(begin(), end());
}

PageList::iterator PageList::begin_() const
{
  return iterator(Glib::wrap(gparent_), gparent_->children);
}

PageList::iterator PageList::end_() const
{
  return iterator(Glib::wrap(gparent_), 0);
}
_DEPRECATE_IFDEF_END

_DEPRECATE_IFDEF_START
Element::Element(Widget* child, Widget* tab, Widget* menu)
:
  child_  (child),
  tab_    (tab),
  menu_   (menu)
{}

Element::Element(Widget& child, Widget& tab, Widget& menu)
:
  child_  (&child),
  tab_    (&tab),
  menu_   (&menu)
{}

Element::Element(Widget& child)
:
  child_  (&child),
  tab_    (0),
  menu_   (0)
{}

TabElem::TabElem(Widget& child, Widget& tab)
:
  Element(&child, &tab, 0)
{}

TabElem::TabElem(Widget& child, const Glib::ustring& label, bool mnemonic)
:
  Element(&child, manage(new Label(label, mnemonic)), 0)
{}

MenuElem::MenuElem(Widget& child, Widget& menu)
:
  Element(&child, 0, &menu)
{}
_DEPRECATE_IFDEF_END

} /* namespace Notebook_Helpers */


_DEPRECATE_IFDEF_START
Notebook::PageList::iterator Notebook::get_current()
{
  return pages().find(get_current_page());
}

Notebook::PageList& Notebook::pages()
{
  pages_proxy_ = PageList(gobj());
  return pages_proxy_;
}

const Notebook::PageList& Notebook::pages() const
{
  pages_proxy_ = PageList(const_cast<GtkNotebook*>(gobj()));
  return pages_proxy_;
}
_DEPRECATE_IFDEF_END

int Notebook::prepend_page(Widget& child, const Glib::ustring& tab_label, bool use_mnemonic)
{
  Label* pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  return prepend_page(child, *pLabel);
}

int Notebook::prepend_page(Widget& child, const Glib::ustring& tab_label, const Glib::ustring& menu_label, bool use_mnemonic)
{
  Label* pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  Label* pLabelMenu = Gtk::manage( new Label(menu_label, use_mnemonic) );
  return prepend_page(child, *pLabel, *pLabelMenu);
}

int Notebook::append_page(Widget& child, const Glib::ustring& tab_label, bool use_mnemonic)
{
  Label* pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  return append_page(child, *pLabel);
}

int Notebook::append_page(Widget& child, const Glib::ustring& tab_label, const Glib::ustring& menu_label, bool use_mnemonic)
{
  Label* pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  Label* pLabelMenu = Gtk::manage( new Label(menu_label, use_mnemonic) );
  return append_page(child, *pLabel, *pLabelMenu);
}

int Notebook::insert_page(Widget& child, const Glib::ustring& tab_label, int position, bool use_mnemonic)
{
  Label* pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  return insert_page(child, *pLabel, position);
}

int Notebook::insert_page(Widget& child, const Glib::ustring& tab_label, const Glib::ustring& menu_label, int position, bool use_mnemonic)
{
  Label* pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  Label* pLabelMenu = Gtk::manage( new Label(menu_label, use_mnemonic) );
  return insert_page(child, *pLabel, *pLabelMenu, position);
}

void Notebook::remove_page(Widget& child)
{
  int pos = page_num(child);
  if(pos != -1)
    remove_page(pos);
}

_DEPRECATE_IFDEF_START
void Notebook::query_tab_label_packing(Widget& child, bool& expand, bool& fill, PackType& pack_type)
{
  gboolean gexpand = false;
  gboolean gfill = false;
  GtkPackType gpack_type = GTK_PACK_START;
  gtk_notebook_query_tab_label_packing(gobj(), child.gobj(), &gexpand, &gfill, &gpack_type);
  expand = gexpand;
  fill = gfill;
  pack_type = (PackType)gpack_type;
}
_DEPRECATE_IFDEF_END

int Notebook::prepend_page(Widget& child)
{
  return gtk_notebook_prepend_page(gobj(), child.gobj(), 0 /* see GTK+ docs */);
}

int Notebook::append_page(Widget& child)
{
  return gtk_notebook_append_page(gobj(), child.gobj(), 0 /* see GTK+ docs */);
}

int Notebook::insert_page(Widget& child, int position)
{
  return gtk_notebook_insert_page(gobj(), child.gobj(), 0 /* see GTK+ docs */, position);
}

_DEPRECATE_IFDEF_START
typedef sigc::slot<Widget*, int, int> SlotWindowCreation;

void Notebook::set_window_creation_hook(const SlotWindowCreation& slot)
{
  SlotWindowCreation* slot_copy = new SlotWindowCreation(slot);
  gtk_notebook_set_window_creation_hook(&SignalProxy_WindowCreation_gtk_callback, slot_copy, &SignalProxy_WindowCreation_gtk_callback_destroy);
}
_DEPRECATE_IFDEF_END

} //namespace Gtk

