/* Copyright (C) 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, see <https://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <gtkmm/label.h>
#include <gtkmm/selectionlistmodelimpl.h>
#include <algorithm>

namespace Gtk
{

int Notebook::prepend_page(Widget& child, const Glib::ustring& tab_label, bool use_mnemonic)
{
  auto 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)
{
  auto pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  auto 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)
{
  auto 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)
{
  auto pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  auto 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)
{
  auto 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)
{
  auto pLabel = Gtk::manage( new Label(tab_label, use_mnemonic) );
  auto 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);
}

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

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

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

Glib::RefPtr<Gio::ListModel> Notebook::get_pages()
{
  // gtk_notebook_get_pages() returns a pointer to a private GObject class
  // that implements GListModel and GtkSelectionModel.
  // Such an instance is wrapped in a Gtk::SelectionListModelImpl.
  // gtk_notebook_get_pages() gives us a ref (transfer full).
  GListModel* notebook_pages = gtk_notebook_get_pages(gobj());
  if (GTK_IS_SELECTION_MODEL(notebook_pages))
  {
    // Look up current C++ wrapper instance:
    Glib::ObjectBase* pCppObject = Glib::ObjectBase::_get_current_wrapper((GObject*)notebook_pages);
    if (!pCppObject)
      pCppObject = new SelectionListModelImpl((GObject*)notebook_pages);
    return Glib::make_refptr_for_instance<Gio::ListModel>(dynamic_cast<Gio::ListModel*>(pCppObject));
  }
  return Glib::wrap(notebook_pages);
}

} //namespace Gtk
