/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * 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.
 */

/*
 * Modified by the GTK+ Team and others 1997-2001.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include <math.h>
#include "gtkresizegrip.h"
#include "gtkwindow.h"
#include "gtkintl.h"

enum 
{
  PROP_0,
  PROP_EDGE,
  PROP_LAST
};


static void gtk_resize_grip_class_init (GtkResizeGripClass  *klass);
static void gtk_resize_grip_init       (GtkResizeGrip       *grip);
static void gtk_resize_grip_realize        (GtkWidget        *widget);
static void gtk_resize_grip_unrealize      (GtkWidget        *widget);
static void gtk_resize_grip_map            (GtkWidget        *widget);
static void gtk_resize_grip_unmap          (GtkWidget        *widget);
static void gtk_resize_grip_size_request   (GtkWidget        *widget,
					    GtkRequisition   *requisition);
static void gtk_resize_grip_size_allocate  (GtkWidget        *widget,
					    GtkAllocation    *allocation);
static gint gtk_resize_grip_expose     (GtkWidget      *widget,
					GdkEventExpose *event);
static void gtk_resize_grip_set_property (GObject         *object,
					  guint            prop_id,
					  const GValue    *value,
					  GParamSpec      *pspec);
static void gtk_resize_grip_get_property (GObject         *object,
					  guint            prop_id,
					  GValue          *value,
					  GParamSpec      *pspec);
static gboolean gtk_resize_grip_button_press (GtkWidget      *widget,
					      GdkEventButton *event);

static GtkWidgetClass *parent_class = NULL;

GType
gtk_resize_grip_get_type (void)
{
  static GType grip_type = 0;

  if (!grip_type)
    {
      static const GTypeInfo grip_info =
      {
	sizeof (GtkResizeGripClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
	(GClassInitFunc) gtk_resize_grip_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
	sizeof (GtkResizeGrip),
	0,		/* n_preallocs */
	(GInstanceInitFunc) gtk_resize_grip_init,
      };

      grip_type = g_type_register_static (GTK_TYPE_MISC, "GtkResizeGrip",
					  &grip_info, 0);
    }

  return grip_type;
}

static void
gtk_resize_grip_class_init (GtkResizeGripClass *class)
{
  GObjectClass *gobject_class;
  GtkWidgetClass *widget_class;

  gobject_class = (GObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;

  parent_class = g_type_class_peek_parent (class);

  gobject_class->set_property = gtk_resize_grip_set_property;
  gobject_class->get_property = gtk_resize_grip_get_property;
  
  g_object_class_install_property (gobject_class,
                                   PROP_EDGE,
                                   g_param_spec_enum ("edge",
                                                      _("Grip edge"),
                                                      _("The edge in which the grip is drawn"),
						      GDK_TYPE_WINDOW_EDGE,
						      GDK_WINDOW_EDGE_SOUTH_EAST,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
  
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("grip_width",
							     _("Grip width"),
							     _("Width of the resize grip"),
							     1,
							     G_MAXINT,
							     18,
							     G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("grip_height",
							     _("Grip height"),
							     _("Height of the resize grip"),
							     1,
							     G_MAXINT,
							     18,
							     G_PARAM_READABLE));

  widget_class->realize = gtk_resize_grip_realize;
  widget_class->unrealize = gtk_resize_grip_unrealize;
  widget_class->map = gtk_resize_grip_map;
  widget_class->unmap = gtk_resize_grip_unmap;
  widget_class->size_request = gtk_resize_grip_size_request;
  widget_class->size_allocate = gtk_resize_grip_size_allocate;
  widget_class->expose_event = gtk_resize_grip_expose;
  widget_class->button_press_event = gtk_resize_grip_button_press;
}

static void
gtk_resize_grip_set_property (GObject         *object,
			      guint            prop_id,
			      const GValue    *value,
			      GParamSpec      *pspec)
{
  GtkResizeGrip *grip;
  
  grip = GTK_RESIZE_GRIP (object);

  switch (prop_id)
    {
    case PROP_EDGE:
      gtk_resize_grip_set_edge (grip, g_value_get_enum (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}


static void
gtk_resize_grip_get_property (GObject         *object,
			      guint            prop_id,
			      GValue          *value,
			      GParamSpec      *pspec)
{
  GtkResizeGrip *grip;
  
  grip = GTK_RESIZE_GRIP (object);

  switch (prop_id)
    {
    case PROP_EDGE:
      g_value_set_enum (value, grip->edge);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_resize_grip_init (GtkResizeGrip *grip)
{
  GTK_WIDGET_SET_FLAGS (grip, GTK_NO_WINDOW);

  grip->edge = GDK_WINDOW_EDGE_SOUTH_EAST;
  grip->event_window = NULL;
}

static void
gtk_resize_grip_realize (GtkWidget *widget)
{
  GtkResizeGrip *grip;
  GdkWindowAttr attributes;
  gint attributes_mask;
  
  grip = GTK_RESIZE_GRIP (widget);
  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_ONLY;
  attributes.event_mask = gtk_widget_get_events (widget) | GDK_BUTTON_PRESS_MASK;

  attributes_mask = GDK_WA_X | GDK_WA_Y;

  widget->window = gtk_widget_get_parent_window (widget);
  g_object_ref (widget->window);
  
  grip->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
				       &attributes, attributes_mask);
  gdk_window_set_user_data (grip->event_window, grip);

  widget->style = gtk_style_attach (widget->style, widget->window);
}

static void
gtk_resize_grip_unrealize (GtkWidget *widget)
{
  GtkResizeGrip *grip = GTK_RESIZE_GRIP (widget);

  if (grip->event_window)
    {
      gdk_window_set_user_data (grip->event_window, NULL);
      gdk_window_destroy (grip->event_window);
      grip->event_window = NULL;
    }
  
  GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}

static void
gtk_resize_grip_map (GtkWidget *widget)
{
  GtkResizeGrip *grip = GTK_RESIZE_GRIP (widget);
  
  GTK_WIDGET_CLASS (parent_class)->map (widget);

  if (grip->event_window)
    gdk_window_show (grip->event_window);
}

static void
gtk_resize_grip_unmap (GtkWidget *widget)
{
  GtkResizeGrip *grip = GTK_RESIZE_GRIP (widget);
    
  if (grip->event_window)
    gdk_window_hide (grip->event_window);

  GTK_WIDGET_CLASS (parent_class)->unmap (widget);
}

static void
gtk_resize_grip_size_request (GtkWidget      *widget,
			      GtkRequisition *requisition)
{
  GtkResizeGrip *grip = GTK_RESIZE_GRIP (widget);
  gtk_widget_style_get (widget, 
			"grip_width", &requisition->width,
			"grip_height", &requisition->height,
			NULL);
}

static GdkWindowEdge
get_grip_edge (GtkResizeGrip *grip)
{
  if (gtk_widget_get_direction (GTK_WIDGET (grip)) == GTK_TEXT_DIR_LTR) 
    return grip->edge; 
  else
    switch (grip->edge)
      {
      case GDK_WINDOW_EDGE_NORTH_WEST:
	return GDK_WINDOW_EDGE_NORTH_EAST;
      case GDK_WINDOW_EDGE_NORTH_EAST:
	return GDK_WINDOW_EDGE_NORTH_WEST;
      case GDK_WINDOW_EDGE_WEST:
	return GDK_WINDOW_EDGE_EAST;
      case GDK_WINDOW_EDGE_EAST:
	return GDK_WINDOW_EDGE_WEST;
      case GDK_WINDOW_EDGE_SOUTH_WEST:
	return GDK_WINDOW_EDGE_SOUTH_EAST;
      case GDK_WINDOW_EDGE_SOUTH_EAST:
	return GDK_WINDOW_EDGE_SOUTH_WEST;
      default:
	return grip->edge;
      }
}

static void
gtk_resize_grip_size_allocate (GtkWidget     *widget,
			       GtkAllocation *allocation)
{
  GtkResizeGrip *grip = GTK_RESIZE_GRIP (widget);

  widget->allocation = *allocation;

  if (GTK_WIDGET_REALIZED (widget))
    {
      gint x, y, width, height;
      GdkWindowEdge edge;
     
      x = allocation->x;
      y = allocation->y;
      width = allocation->width;
      height = allocation->height;
      edge = get_grip_edge (grip);
      
      /* keep the following code in sync with gtk_default_draw_resize_grip() !! */
      switch (edge)
	{
	case GDK_WINDOW_EDGE_NORTH_WEST:
	  /* make it square */
	  if (width < height)
	    {
	      height = width;
	    }
	  else if (height < width)
	    {
	      width = height;
	    }
	  break;
	case GDK_WINDOW_EDGE_NORTH:
	  if (width < height)
	    {
	      height = width;
	    }
	  break;
	case GDK_WINDOW_EDGE_NORTH_EAST:
	  /* make it square, aligning to top right */
	  if (width < height)
	    {
	      height = width;
	    }
	  else if (height < width)
	    {
	      x += (width - height);
	      width = height;
	}
	  break;
	case GDK_WINDOW_EDGE_WEST:
	  if (height < width)
	    {
	      width = height;
	    }
	  break;
	case GDK_WINDOW_EDGE_EAST:
	  /* aligning to right */
	  if (height < width)
	    {
	      x += (width - height);
	      width = height;
	    }
	  break;
	case GDK_WINDOW_EDGE_SOUTH_WEST:
	  /* make it square, aligning to bottom left */
	  if (width < height)
	    {
	      y += (height - width);
	      height = width;
	    }
	  else if (height < width)
	    {
	      width = height;
	    }
	  break;
	case GDK_WINDOW_EDGE_SOUTH:
	  /* align to bottom */
	  if (width < height)
	    {
	      y += (height - width);
	      height = width;
	    }
	  break;
	case GDK_WINDOW_EDGE_SOUTH_EAST:
	  /* make it square, aligning to bottom right */
	  if (width < height)
	    {
	      y += (height - width);
	      height = width;
	    }
	  else if (height < width)
	    {
	      x += (width - height);
	      width = height;
	    }
	  break;
	default:
	  g_assert_not_reached ();
	}
      gdk_window_move_resize (grip->event_window,
			      x, y ,width, height);
    }
}

GtkWidget*
gtk_resize_grip_new (void)
{
  return g_object_new (GTK_TYPE_RESIZE_GRIP, NULL);
}

GtkWidget*
gtk_resize_grip_new_with_edge (GdkWindowEdge edge)
{
  return g_object_new (GTK_TYPE_RESIZE_GRIP, "edge", edge, NULL);
}

void
gtk_resize_grip_set_edge (GtkResizeGrip *grip,
			  GdkWindowEdge  edge)
{
  g_return_if_fail (GTK_IS_RESIZE_GRIP (grip));

  if (grip->edge != edge)
    {
      grip->edge = edge;

      g_object_notify (G_OBJECT (grip), "edge");

      if (GTK_WIDGET_DRAWABLE (grip))
	gtk_widget_queue_draw (GTK_WIDGET (grip));
    }
}

GdkWindowEdge
gtk_resize_grip_get_edge (GtkResizeGrip *grip)
{
  g_return_val_if_fail (GTK_IS_RESIZE_GRIP (grip), GDK_WINDOW_EDGE_SOUTH_EAST);
  
  return grip->edge;
}

static gboolean 
gtk_resize_grip_expose (GtkWidget      *widget,
			GdkEventExpose *event)
{
  if (GTK_WIDGET_DRAWABLE (widget))
    gtk_paint_resize_grip (widget->style,
			   widget->window,
			   GTK_WIDGET_STATE (widget),
			   NULL,
			   widget,
			   "resize_grip",
			   get_grip_edge (GTK_RESIZE_GRIP (widget)),
			   widget->allocation.x, widget->allocation.y,
			   widget->allocation.width, widget->allocation.height);

  return FALSE;
}

static gboolean
gtk_resize_grip_button_press (GtkWidget      *widget,
			      GdkEventButton *event)
{
  GtkResizeGrip *grip;
  GtkWidget *ancestor;
  GdkWindowEdge edge;
  
  grip = GTK_RESIZE_GRIP (widget);

  if (event->type != GDK_BUTTON_PRESS)
    return FALSE;
  
  ancestor = gtk_widget_get_toplevel (widget);

  if (!GTK_IS_WINDOW (ancestor))
    return FALSE;

  edge = get_grip_edge (grip);

  if (event->button == 1)
    gtk_window_begin_resize_drag (GTK_WINDOW (ancestor),
                                  edge,
                                  event->button,
                                  event->x_root, event->y_root,
                                  event->time);
  else if (event->button == 2)
    gtk_window_begin_move_drag (GTK_WINDOW (ancestor),
                                event->button,
                                event->x_root, event->y_root,
                                event->time);
  else
    return FALSE;
  
  return TRUE;
}
