// -*- c++ -*-
/* Copyright (C) 2006 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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/gtkprintoperation.h>

// This Signal Proxy allows the C++ coder to specify a sigc::slot instead of a static function.

static void
SignalProxy_PrintSetupDone_gtk_callback(GtkPageSetup* page_setup, gpointer data)
{
  const Gtk::SlotPrintSetupDone* the_slot = static_cast<Gtk::SlotPrintSetupDone*>(data);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    Glib::RefPtr<Gtk::PageSetup> ps = Glib::wrap(page_setup);
    (*the_slot)(ps);
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch (...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  delete the_slot;
}

namespace Gtk
{

PrintOperationResult
#ifdef GLIBMM_EXCEPTIONS_ENABLED
PrintOperation::run(PrintOperationAction action)
#else
PrintOperation::run(PrintOperationAction action, std::auto_ptr<Glib::Error>& error)
#endif //GLIBMM_EXCEPTIONS_ENABLED
{
  GError* gerror = 0;
  PrintOperationResult res =
    (PrintOperationResult)gtk_print_operation_run(this->gobj(), (GtkPrintOperationAction)action, 0, &gerror);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  if (res == PRINT_OPERATION_RESULT_ERROR)
  {
    gtk_print_operation_get_error(this->gobj(), &gerror);
    ::Glib::Error::throw_exception(gerror);
  }
  #else
  if (res == PRINT_OPERATION_RESULT_ERROR)
  {
    gtk_print_operation_get_error(this->gobj(), &gerror);
    error = ::Glib::Error::throw_exception(gerror);
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED

  return res;
}

Glib::RefPtr<PageSetup>
run_page_setup_dialog(Window& parent,
                      const Glib::RefPtr<const PageSetup>& page_setup,
                      const Glib::RefPtr<const PrintSettings>& print_settings)
{
  // Specify the exact type with template specialization, to avoid possible
  // ambiguities between the const and non-const versions of unwrap() reported
  // by Sun's compiler (specifying unwrap<const Object> was reported
  // not to work):
  return Glib::wrap(
    gtk_print_run_page_setup_dialog(
      parent.gobj(),
      const_cast<GtkPageSetup*>(Glib::unwrap<PageSetup>(page_setup)),
      const_cast<GtkPrintSettings*>(Glib::unwrap<PrintSettings>(print_settings))));

}

void
run_page_setup_dialog_async(Window& parent,
                            const Glib::RefPtr<const PageSetup>& page_setup,
                            const Glib::RefPtr<const PrintSettings>& print_settings,
                            const SlotPrintSetupDone& slot)
{
  SlotPrintSetupDone* slot_copy = new SlotPrintSetupDone(slot);

  // Specify the exact type with template specialization, to avoid possible
  // ambiguities between the const and non-const versions of unwrap() reported
  // by Sun's compiler (specifying unwrap<const Object> was reported
  // not to work):
  gtk_print_run_page_setup_dialog_async(
    parent.gobj(),
    const_cast<GtkPageSetup*>(Glib::unwrap<PageSetup>(page_setup)),
    const_cast<GtkPrintSettings*>(Glib::unwrap<PrintSettings>(print_settings)),
    &SignalProxy_PrintSetupDone_gtk_callback,
    slot_copy);
}

} // namespace Gtk
