/*
 * Copyright (c) 2003 Benedikt Meurer (benedikt.meurer@unix-ag.uni-siegen.de)
 *               2004 Jean-François Wauthy (pollux@xfce.org)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef	HAVE_CONFIG_H
#include <config.h>
#endif /* !HAVE_CONFIG_H */

#include <gtk/gtk.h>

#include <libxfce4util/libxfce4util.h>

#include "vpages_page.h"

static void vpages_page_class_init (VpagesPageClass *);
static void vpages_page_init (VpagesPage *);
static void vpages_page_finalize (GObject *);

/* signal handlers */
static void checked_cb (GtkWidget *, VpagesPage *);

static GtkWidgetClass *parent_class = NULL;

GtkType
vpages_page_get_type (void)
{
  static GtkType vpages_page_type = 0;

  if (!vpages_page_type) {
    static const GTypeInfo vpages_page_info = {
      sizeof (VpagesPageClass),
      NULL,
      NULL,
      (GClassInitFunc) vpages_page_class_init,
      NULL,
      NULL,
      sizeof (VpagesPage),
      0,
      (GInstanceInitFunc) vpages_page_init,
      NULL
    };

    vpages_page_type = g_type_register_static (GTK_TYPE_TABLE, "VpagesPage", &vpages_page_info, 0);
  }

  return (vpages_page_type);
}

static void
vpages_page_class_init (VpagesPageClass * klass)
{
  GObjectClass *object_class;

  object_class = G_OBJECT_CLASS (klass);
  object_class->finalize = vpages_page_finalize;

  parent_class = gtk_type_class (gtk_table_get_type ());
}

static void
vpages_page_init (VpagesPage * pg)
{
  GtkWidget *label;

  gtk_container_set_border_width (GTK_CONTAINER (pg), 6);
  gtk_table_resize (GTK_TABLE (pg), 6, 2);
  gtk_table_set_col_spacings (GTK_TABLE (pg), 4);
  gtk_table_set_row_spacings (GTK_TABLE (pg), 4);
  gtk_table_set_homogeneous (GTK_TABLE (pg), FALSE);

  /* see a2ps documentation for more information on the string meaning */
  pg->clineno = gtk_check_button_new_with_label (_("Line numbers:"));
  gtk_widget_show (pg->clineno);
  g_signal_connect (G_OBJECT (pg->clineno), "toggled", G_CALLBACK (checked_cb), pg);
  gtk_table_attach (GTK_TABLE (pg), pg->clineno, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);

  pg->lineno = gtk_spin_button_new_with_range (1.0, 100.0, 1.0);
  gtk_widget_set_sensitive (pg->lineno, FALSE);
  gtk_widget_show (pg->lineno);
  gtk_table_attach (GTK_TABLE (pg), pg->lineno, 1, 2, 0, 1, GTK_EXPAND | GTK_FILL, 0, 0, 0);

  pg->clpp = gtk_check_button_new_with_label (_("Lines per page:"));
  gtk_widget_show (pg->clpp);
  g_signal_connect (G_OBJECT (pg->clpp), "toggled", G_CALLBACK (checked_cb), pg);
  gtk_table_attach (GTK_TABLE (pg), pg->clpp, 0, 1, 1, 2, GTK_FILL, 0, 0, 0);

  pg->lpp = gtk_spin_button_new_with_range (1.0, 100.0, 1.0);
  gtk_widget_set_sensitive (pg->lpp, FALSE);
  gtk_widget_show (pg->lpp);
  gtk_table_attach (GTK_TABLE (pg), pg->lpp, 1, 2, 1, 2, GTK_EXPAND | GTK_FILL, 0, 0, 0);

  pg->ccpl = gtk_check_button_new_with_label (_("Chars per line:"));
  gtk_widget_show (pg->ccpl);
  g_signal_connect (G_OBJECT (pg->ccpl), "toggled", G_CALLBACK (checked_cb), pg);
  gtk_table_attach (GTK_TABLE (pg), pg->ccpl, 0, 1, 2, 3, GTK_FILL, 0, 0, 0);

  pg->cpl = gtk_spin_button_new_with_range (1.0, 100.0, 1.0);
  gtk_widget_set_sensitive (pg->cpl, FALSE);
  gtk_widget_show (pg->cpl);
  gtk_table_attach (GTK_TABLE (pg), pg->cpl, 1, 2, 2, 3, GTK_EXPAND | GTK_FILL, 0, 0, 0);

  label = gtk_label_new (_("Tabulation size:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
  gtk_widget_show (label);
  gtk_table_attach (GTK_TABLE (pg), label, 0, 1, 3, 4, GTK_FILL, 0, 0, 0);

  pg->tabsize = gtk_spin_button_new_with_range (1.0, 32.0, 1.0);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (pg->tabsize), 8.0);
  gtk_widget_show (pg->tabsize);
  gtk_table_attach (GTK_TABLE (pg), pg->tabsize, 1, 2, 3, 4, GTK_EXPAND | GTK_FILL, 0, 0, 0);

  /* see a2ps documentation for more information on the string meaning */
  label = gtk_label_new (_("Non-printable format:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
  gtk_widget_show (label);
  gtk_table_attach (GTK_TABLE (pg), label, 0, 1, 4, 5, GTK_FILL, 0, 0, 0);

  pg->nprfmt = gtk_combo_new ();
  gtk_widget_show (pg->nprfmt);
  gtk_table_attach (GTK_TABLE (pg), pg->nprfmt, 1, 2, 4, 5, GTK_EXPAND | GTK_FILL, 0, 0, 0);
}

static void
vpages_page_finalize (GObject * object)
{
  g_return_if_fail (object != NULL);
  g_return_if_fail (VPAGES_IS_PAGE (object));

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

GtkWidget *
vpages_page_new (void)
{
  VpagesPage *pg;
  GList *list;

  pg = VPAGES_PAGE (g_object_new (vpages_page_get_type (), NULL));

  if ((list = xfprint_option_list (non_printable_fmts)) != NULL) {
    gtk_combo_set_popdown_strings (GTK_COMBO (pg->nprfmt), list);
    g_list_free (list);
  }

  return (GTK_WIDGET (pg));
}

void
vpages_page_set_settings (VpagesPage * pg, const XfprintSettingsVirtualPages * vp)
{
  g_return_if_fail (pg != NULL);
  g_return_if_fail (vp != NULL);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (pg->clineno), (vp->linenumbers > 0));
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (pg->lineno), vp->linenumbers);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (pg->clpp), (vp->lpp > 0));
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (pg->lpp), vp->lpp);

  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (pg->ccpl), (vp->cpl > 0));
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (pg->cpl), vp->cpl);

  gtk_spin_button_set_value (GTK_SPIN_BUTTON (pg->tabsize), vp->tabsize);

  gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (pg->nprfmt)->entry),
                      xfprint_option_alias (non_printable_fmts, vp->nonprtfmt));
}

void
vpages_page_get_settings (VpagesPage * pg, XfprintSettingsVirtualPages * vp)
{
  g_return_if_fail (pg != NULL);
  g_return_if_fail (vp != NULL);

  vp->linenumbers = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (pg->clineno))
    ? gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (pg->lineno))
    : 0;

  vp->lpp = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (pg->clpp))
    ? gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (pg->lpp))
    : 0;

  vp->cpl = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (pg->ccpl))
    ? gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (pg->cpl))
    : 0;

  vp->tabsize = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (pg->tabsize));

  vp->nonprtfmt = xfprint_option_name (non_printable_fmts,
                                       gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (pg->nprfmt)->entry)));
}

static void
checked_cb (GtkWidget * btn, VpagesPage * pg)
{
  gboolean active;

  active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (btn));

  if (btn == pg->clineno)
    gtk_widget_set_sensitive (pg->lineno, active);
  else if (btn == pg->clpp)
    gtk_widget_set_sensitive (pg->lpp, active);
  else if (btn == pg->ccpl)
    gtk_widget_set_sensitive (pg->cpl, active);
}
