/* -*- Mode: C; c-file-style: "gnu"; tab-width: 8 -*- */
/* eggassistant.c
 * Copyright (C) 2005 Carlos Garnacho Parro
 *
 * 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 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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gtk/gtk.h>
#include "eggassistantpage.h"

#define EGG_ASSISTANT_PAGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_ASSISTANT_PAGE, EggAssistantPagePrivate))

typedef struct _EggAssistantPagePrivate EggAssistantPagePrivate;

struct _EggAssistantPagePrivate
{
  gchar     *title;

  GdkPixbuf *header_image;
  GdkPixbuf *watermark;
};

static void egg_assistant_page_class_init (EggAssistantPageClass *class);
static void egg_assistant_page_init       (EggAssistantPage      *assistant);
static void egg_assistant_page_finalize   (GObject               *object);

static void egg_assistant_page_get_property (GObject      *object,
					     guint         prop_id,
					     GValue       *value,
					     GParamSpec   *pspec);
static void egg_assistant_page_set_property (GObject      *object,
					     guint         prop_id,
					     const GValue *value,
					     GParamSpec   *pspec);

static void egg_assistant_page_size_request  (GtkWidget      *widget,
					      GtkRequisition *requisition);
static void egg_assistant_page_size_allocate (GtkWidget      *widget,
					      GtkAllocation  *allocation);

enum {
  PROP_0,
  PROP_TITLE,
  PROP_HEADER_IMAGE,
  PROP_WATERMARK
};

enum {
  PREPARE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE (EggAssistantPage, egg_assistant_page, GTK_TYPE_BIN);


static void
egg_assistant_page_class_init (EggAssistantPageClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

  object_class->finalize = egg_assistant_page_finalize;
  object_class->set_property = egg_assistant_page_set_property;
  object_class->get_property = egg_assistant_page_get_property;

  widget_class->size_request  = egg_assistant_page_size_request;
  widget_class->size_allocate = egg_assistant_page_size_allocate;

  g_object_class_install_property (object_class,
                                   PROP_TITLE,
                                   g_param_spec_string ("title",
                                                        "Title",
                                                        "Title for the page",
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
                                   PROP_HEADER_IMAGE,
                                   g_param_spec_object ("header-image",
                                                        "Header image",
                                                        "Header image for the page",
							GDK_TYPE_PIXBUF,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
                                   PROP_WATERMARK,
                                   g_param_spec_object ("watermark",
                                                        "Watermark",
                                                        "Side watermark for the page",
							GDK_TYPE_PIXBUF,
                                                        G_PARAM_READWRITE));
  signals[PREPARE] =
    g_signal_new ("prepare",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (EggAssistantPageClass, prepare),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  g_type_class_add_private (class, sizeof (EggAssistantPagePrivate));
}

static void
egg_assistant_page_init (EggAssistantPage *page)
{
  EggAssistantPagePrivate *priv;

  priv = EGG_ASSISTANT_PAGE_GET_PRIVATE (page);

  priv->title        = NULL;
  priv->header_image = NULL;
  priv->watermark    = NULL;
}

static void
egg_assistant_page_set_property (GObject      *object,
				 guint         prop_id,
				 const GValue *value,
				 GParamSpec   *pspec)
{
  EggAssistantPage *page;
  EggAssistantPagePrivate *priv;
  GObject *obj;

  page = EGG_ASSISTANT_PAGE (object);
  priv = EGG_ASSISTANT_PAGE_GET_PRIVATE (page);

  switch (prop_id)
    {
    case PROP_TITLE:
      g_free (priv->title);
      priv->title = g_value_dup_string (value);
      break;
    case PROP_HEADER_IMAGE:
      obj = g_value_get_object (value);

      if (priv->header_image)
	{
	  g_object_unref (priv->header_image);
	  priv->header_image = NULL;
	}

      if (obj)
	priv->header_image = g_object_ref (obj);

      break;
    case PROP_WATERMARK:
      obj = g_value_get_object (value);

      if (priv->watermark)
	{
	  g_object_unref (priv->watermark);
	  priv->watermark = NULL;
	}

      if (obj)
	priv->watermark = g_object_ref (obj);

      break;
    }
}

static void
egg_assistant_page_get_property (GObject      *object,
				 guint         prop_id,
				 GValue       *value,
				 GParamSpec   *pspec)
{
  EggAssistantPage *page;
  EggAssistantPagePrivate *priv;
  GObject *obj;

  page = EGG_ASSISTANT_PAGE (object);
  priv = EGG_ASSISTANT_PAGE_GET_PRIVATE (page);

  switch (prop_id)
    {
    case PROP_TITLE:
      g_value_set_string (value, priv->title);
      break;
    case PROP_HEADER_IMAGE:
      g_value_set_object (value, priv->header_image);
      break;
    case PROP_WATERMARK:
      g_value_set_object (value, priv->watermark);
      break;
    }
}


static void
egg_assistant_page_finalize (GObject *object)
{
  EggAssistantPage *page;
  EggAssistantPagePrivate *priv;

  page = EGG_ASSISTANT_PAGE (object);
  priv = EGG_ASSISTANT_PAGE_GET_PRIVATE (page);

  if (priv)
    {
      g_free (priv->title);

      if (priv->header_image)
	g_object_unref (priv->header_image);

      if (priv->watermark)
	g_object_unref (priv->watermark);
    }

  if (G_OBJECT_CLASS (egg_assistant_page_parent_class)->finalize)
    (* G_OBJECT_CLASS (egg_assistant_page_parent_class)->finalize) (object);
}

static void
egg_assistant_page_size_request (GtkWidget      *widget,
				 GtkRequisition *requisition)
{
  GtkWidget *child;
  GtkRequisition child_requisition;
  gint border;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (widget));

  child  = GTK_BIN (widget)->child;
  border = GTK_CONTAINER (widget)->border_width;

  requisition->width = requisition->height = border * 2;

  if (child && GTK_WIDGET_VISIBLE (child))
    {
      gtk_widget_size_request (GTK_BIN (widget)->child, &child_requisition);

      requisition->width  += child_requisition.width;
      requisition->height += child_requisition.height;
    }
}

static void
egg_assistant_page_size_allocate (GtkWidget      *widget,
				  GtkAllocation  *allocation)
{
  gint border;
  GtkAllocation child_allocation;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (widget));

  widget->allocation = *allocation;
  border = GTK_CONTAINER (widget)->border_width;

  child_allocation.x = allocation->x + border;
  child_allocation.y = allocation->y + border;
  child_allocation.width  = allocation->width  - 2 * border;
  child_allocation.height = allocation->height - 2 * border;

  if (GTK_BIN (widget)->child)
    gtk_widget_size_allocate (GTK_BIN (widget)->child, &child_allocation);
}

GtkWidget*
egg_assistant_page_new (const gchar *title, GdkPixbuf *header_image, GtkWidget *child)
{
  GtkWidget *page;

  page = g_object_new (EGG_TYPE_ASSISTANT_PAGE,
		       "header-image", header_image,
		       "title", title,
		       "child", child,
		       NULL);
  return page;
}

const gchar*
egg_assistant_page_get_title (EggAssistantPage *page)
{
  EggAssistantPagePrivate *priv;

  g_return_if_fail (page != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (page));

  priv = EGG_ASSISTANT_PAGE_GET_PRIVATE (page);
  return priv->title;
}

void
egg_assistant_page_set_title (EggAssistantPage *page, const gchar *title)
{
  g_return_if_fail (page != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (page));

  g_object_set (G_OBJECT (page), "title", title, NULL);
}

GdkPixbuf*
egg_assistant_page_get_header_image (EggAssistantPage *page)
{
  EggAssistantPagePrivate *priv;

  g_return_if_fail (page != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (page));

  priv = EGG_ASSISTANT_PAGE_GET_PRIVATE (page);
  return priv->header_image;
}

void
egg_assistant_page_set_header_image (EggAssistantPage *page, GdkPixbuf *pixbuf)
{
  g_return_if_fail (page != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (page));

  g_object_set (G_OBJECT (page), "header-image", pixbuf, NULL);
}

GdkPixbuf*
egg_assistant_page_get_watermark (EggAssistantPage *page)
{
  EggAssistantPagePrivate *priv;

  g_return_if_fail (page != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (page));

  priv = EGG_ASSISTANT_PAGE_GET_PRIVATE (page);
  return priv->watermark;
}

void
egg_assistant_page_set_watermark (EggAssistantPage *page, GdkPixbuf *pixbuf)
{
  g_return_if_fail (page != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (page));

  g_object_set (G_OBJECT (page), "watermark", pixbuf, NULL);
}

void
egg_assistant_page_prepare (EggAssistantPage *page)
{
  g_return_if_fail (page != NULL);
  g_return_if_fail (EGG_IS_ASSISTANT_PAGE (page));
  
  g_signal_emit (G_OBJECT (page), signals[PREPARE], 0);
}
