Blame gtk/gtkmisc.c

Packit 98cdb6
/* GTK - The GIMP Toolkit
Packit 98cdb6
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Lesser General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 98cdb6
 * Lesser General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Lesser General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
/*
Packit 98cdb6
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
Packit 98cdb6
 * file for a list of people on the GTK+ Team.  See the ChangeLog
Packit 98cdb6
 * files for a list of changes.  These files are distributed with
Packit 98cdb6
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
#include "config.h"
Packit 98cdb6
#include "gtkcontainer.h"
Packit 98cdb6
#include "gtkmisc.h"
Packit 98cdb6
#include "gtkintl.h"
Packit 98cdb6
#include "gtkprivate.h"
Packit 98cdb6
#include "gtkalias.h"
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
enum {
Packit 98cdb6
  PROP_0,
Packit 98cdb6
  PROP_XALIGN,
Packit 98cdb6
  PROP_YALIGN,
Packit 98cdb6
  PROP_XPAD,
Packit 98cdb6
  PROP_YPAD
Packit 98cdb6
};
Packit 98cdb6
Packit 98cdb6
static void gtk_misc_realize      (GtkWidget    *widget);
Packit 98cdb6
static void gtk_misc_set_property (GObject         *object,
Packit 98cdb6
				   guint            prop_id,
Packit 98cdb6
				   const GValue    *value,
Packit 98cdb6
				   GParamSpec      *pspec);
Packit 98cdb6
static void gtk_misc_get_property (GObject         *object,
Packit 98cdb6
				   guint            prop_id,
Packit 98cdb6
				   GValue          *value,
Packit 98cdb6
				   GParamSpec      *pspec);
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
G_DEFINE_ABSTRACT_TYPE (GtkMisc, gtk_misc, GTK_TYPE_WIDGET)
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_misc_class_init (GtkMiscClass *class)
Packit 98cdb6
{
Packit 98cdb6
  GObjectClass   *gobject_class;
Packit 98cdb6
  GtkWidgetClass *widget_class;
Packit 98cdb6
Packit 98cdb6
  gobject_class = G_OBJECT_CLASS (class);
Packit 98cdb6
  widget_class = (GtkWidgetClass*) class;
Packit 98cdb6
Packit 98cdb6
  gobject_class->set_property = gtk_misc_set_property;
Packit 98cdb6
  gobject_class->get_property = gtk_misc_get_property;
Packit 98cdb6
  
Packit 98cdb6
  widget_class->realize = gtk_misc_realize;
Packit 98cdb6
Packit 98cdb6
  g_object_class_install_property (gobject_class,
Packit 98cdb6
                                   PROP_XALIGN,
Packit 98cdb6
                                   g_param_spec_float ("xalign",
Packit 98cdb6
						       P_("X align"),
Packit 98cdb6
						       P_("The horizontal alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
Packit 98cdb6
						       0.0,
Packit 98cdb6
						       1.0,
Packit 98cdb6
						       0.5,
Packit 98cdb6
						       GTK_PARAM_READWRITE));
Packit 98cdb6
Packit 98cdb6
  g_object_class_install_property (gobject_class,
Packit 98cdb6
                                   PROP_YALIGN,
Packit 98cdb6
                                   g_param_spec_float ("yalign",
Packit 98cdb6
						       P_("Y align"),
Packit 98cdb6
						       P_("The vertical alignment, from 0 (top) to 1 (bottom)"),
Packit 98cdb6
						       0.0,
Packit 98cdb6
						       1.0,
Packit 98cdb6
						       0.5,
Packit 98cdb6
						       GTK_PARAM_READWRITE));
Packit 98cdb6
Packit 98cdb6
  g_object_class_install_property (gobject_class,
Packit 98cdb6
                                   PROP_XPAD,
Packit 98cdb6
                                   g_param_spec_int ("xpad",
Packit 98cdb6
						     P_("X pad"),
Packit 98cdb6
						     P_("The amount of space to add on the left and right of the widget, in pixels"),
Packit 98cdb6
						     0,
Packit 98cdb6
						     G_MAXINT,
Packit 98cdb6
						     0,
Packit 98cdb6
						     GTK_PARAM_READWRITE));
Packit 98cdb6
Packit 98cdb6
  g_object_class_install_property (gobject_class,
Packit 98cdb6
                                   PROP_YPAD,
Packit 98cdb6
                                   g_param_spec_int ("ypad",
Packit 98cdb6
						     P_("Y pad"),
Packit 98cdb6
						     P_("The amount of space to add on the top and bottom of the widget, in pixels"),
Packit 98cdb6
						     0,
Packit 98cdb6
						     G_MAXINT,
Packit 98cdb6
						     0,
Packit 98cdb6
						     GTK_PARAM_READWRITE));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_misc_init (GtkMisc *misc)
Packit 98cdb6
{
Packit 98cdb6
  misc->xalign = 0.5;
Packit 98cdb6
  misc->yalign = 0.5;
Packit 98cdb6
  misc->xpad = 0;
Packit 98cdb6
  misc->ypad = 0;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_misc_set_property (GObject      *object,
Packit 98cdb6
		       guint         prop_id,
Packit 98cdb6
		       const GValue *value,
Packit 98cdb6
		       GParamSpec   *pspec)
Packit 98cdb6
{
Packit 98cdb6
  GtkMisc *misc;
Packit 98cdb6
Packit 98cdb6
  misc = GTK_MISC (object);
Packit 98cdb6
Packit 98cdb6
  switch (prop_id)
Packit 98cdb6
    {
Packit 98cdb6
    case PROP_XALIGN:
Packit 98cdb6
      gtk_misc_set_alignment (misc, g_value_get_float (value), misc->yalign);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_YALIGN:
Packit 98cdb6
      gtk_misc_set_alignment (misc, misc->xalign, g_value_get_float (value));
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_XPAD:
Packit 98cdb6
      gtk_misc_set_padding (misc, g_value_get_int (value), misc->ypad);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_YPAD:
Packit 98cdb6
      gtk_misc_set_padding (misc, misc->xpad, g_value_get_int (value));
Packit 98cdb6
      break;
Packit 98cdb6
    default:
Packit 98cdb6
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 98cdb6
      break;
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_misc_get_property (GObject      *object,
Packit 98cdb6
		       guint         prop_id,
Packit 98cdb6
		       GValue       *value,
Packit 98cdb6
		       GParamSpec   *pspec)
Packit 98cdb6
{
Packit 98cdb6
  GtkMisc *misc;
Packit 98cdb6
Packit 98cdb6
  misc = GTK_MISC (object);
Packit 98cdb6
Packit 98cdb6
  switch (prop_id)
Packit 98cdb6
    {
Packit 98cdb6
    case PROP_XALIGN:
Packit 98cdb6
      g_value_set_float (value, misc->xalign);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_YALIGN:
Packit 98cdb6
      g_value_set_float (value, misc->yalign);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_XPAD:
Packit 98cdb6
      g_value_set_int (value, misc->xpad);
Packit 98cdb6
      break;
Packit 98cdb6
    case PROP_YPAD:
Packit 98cdb6
      g_value_set_int (value, misc->ypad);
Packit 98cdb6
      break;
Packit 98cdb6
    default:
Packit 98cdb6
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 98cdb6
      break;
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
void
Packit 98cdb6
gtk_misc_set_alignment (GtkMisc *misc,
Packit 98cdb6
			gfloat   xalign,
Packit 98cdb6
			gfloat   yalign)
Packit 98cdb6
{
Packit 98cdb6
  GtkWidget *widget;
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (GTK_IS_MISC (misc));
Packit 98cdb6
Packit 98cdb6
  if (xalign < 0.0)
Packit 98cdb6
    xalign = 0.0;
Packit 98cdb6
  else if (xalign > 1.0)
Packit 98cdb6
    xalign = 1.0;
Packit 98cdb6
Packit 98cdb6
  if (yalign < 0.0)
Packit 98cdb6
    yalign = 0.0;
Packit 98cdb6
  else if (yalign > 1.0)
Packit 98cdb6
    yalign = 1.0;
Packit 98cdb6
Packit 98cdb6
  if ((xalign != misc->xalign) || (yalign != misc->yalign))
Packit 98cdb6
    {
Packit 98cdb6
      g_object_freeze_notify (G_OBJECT (misc));
Packit 98cdb6
      if (xalign != misc->xalign)
Packit 98cdb6
	g_object_notify (G_OBJECT (misc), "xalign");
Packit 98cdb6
Packit 98cdb6
      if (yalign != misc->yalign)
Packit 98cdb6
	g_object_notify (G_OBJECT (misc), "yalign");
Packit 98cdb6
Packit 98cdb6
      misc->xalign = xalign;
Packit 98cdb6
      misc->yalign = yalign;
Packit 98cdb6
      
Packit 98cdb6
      /* clear the area that was allocated before the change
Packit 98cdb6
       */
Packit 98cdb6
      widget = GTK_WIDGET (misc);
Packit 98cdb6
      if (gtk_widget_is_drawable (widget))
Packit 98cdb6
        gtk_widget_queue_draw (widget);
Packit 98cdb6
Packit 98cdb6
      g_object_thaw_notify (G_OBJECT (misc));
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gtk_misc_get_alignment:
Packit 98cdb6
 * @misc: a #GtkMisc
Packit 98cdb6
 * @xalign: (out) (allow-none): location to store X alignment of @misc, or %NULL
Packit 98cdb6
 * @yalign: (out) (allow-none): location to store Y alignment of @misc, or %NULL
Packit 98cdb6
 *
Packit 98cdb6
 * Gets the X and Y alignment of the widget within its allocation. 
Packit 98cdb6
 * See gtk_misc_set_alignment().
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gtk_misc_get_alignment (GtkMisc *misc,
Packit 98cdb6
		        gfloat  *xalign,
Packit 98cdb6
			gfloat  *yalign)
Packit 98cdb6
{
Packit 98cdb6
  g_return_if_fail (GTK_IS_MISC (misc));
Packit 98cdb6
Packit 98cdb6
  if (xalign)
Packit 98cdb6
    *xalign = misc->xalign;
Packit 98cdb6
  if (yalign)
Packit 98cdb6
    *yalign = misc->yalign;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
void
Packit 98cdb6
gtk_misc_set_padding (GtkMisc *misc,
Packit 98cdb6
		      gint     xpad,
Packit 98cdb6
		      gint     ypad)
Packit 98cdb6
{
Packit 98cdb6
  GtkRequisition *requisition;
Packit 98cdb6
  
Packit 98cdb6
  g_return_if_fail (GTK_IS_MISC (misc));
Packit 98cdb6
  
Packit 98cdb6
  if (xpad < 0)
Packit 98cdb6
    xpad = 0;
Packit 98cdb6
  if (ypad < 0)
Packit 98cdb6
    ypad = 0;
Packit 98cdb6
  
Packit 98cdb6
  if ((xpad != misc->xpad) || (ypad != misc->ypad))
Packit 98cdb6
    {
Packit 98cdb6
      g_object_freeze_notify (G_OBJECT (misc));
Packit 98cdb6
      if (xpad != misc->xpad)
Packit 98cdb6
	g_object_notify (G_OBJECT (misc), "xpad");
Packit 98cdb6
Packit 98cdb6
      if (ypad != misc->ypad)
Packit 98cdb6
	g_object_notify (G_OBJECT (misc), "ypad");
Packit 98cdb6
Packit 98cdb6
      requisition = &(GTK_WIDGET (misc)->requisition);
Packit 98cdb6
      requisition->width -= misc->xpad * 2;
Packit 98cdb6
      requisition->height -= misc->ypad * 2;
Packit 98cdb6
      
Packit 98cdb6
      misc->xpad = xpad;
Packit 98cdb6
      misc->ypad = ypad;
Packit 98cdb6
      
Packit 98cdb6
      requisition->width += misc->xpad * 2;
Packit 98cdb6
      requisition->height += misc->ypad * 2;
Packit 98cdb6
      
Packit 98cdb6
      if (gtk_widget_is_drawable (GTK_WIDGET (misc)))
Packit 98cdb6
	gtk_widget_queue_resize (GTK_WIDGET (misc));
Packit 98cdb6
Packit 98cdb6
      g_object_thaw_notify (G_OBJECT (misc));
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gtk_misc_get_padding:
Packit 98cdb6
 * @misc: a #GtkMisc
Packit 98cdb6
 * @xpad: (out) (allow-none): location to store padding in the X
Packit 98cdb6
 *        direction, or %NULL
Packit 98cdb6
 * @ypad: (out) (allow-none): location to store padding in the Y
Packit 98cdb6
 *        direction, or %NULL
Packit 98cdb6
 *
Packit 98cdb6
 * Gets the padding in the X and Y directions of the widget. 
Packit 98cdb6
 * See gtk_misc_set_padding().
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gtk_misc_get_padding (GtkMisc *misc,
Packit 98cdb6
		      gint    *xpad,
Packit 98cdb6
		      gint    *ypad)
Packit 98cdb6
{
Packit 98cdb6
  g_return_if_fail (GTK_IS_MISC (misc));
Packit 98cdb6
Packit 98cdb6
  if (xpad)
Packit 98cdb6
    *xpad = misc->xpad;
Packit 98cdb6
  if (ypad)
Packit 98cdb6
    *ypad = misc->ypad;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gtk_misc_realize (GtkWidget *widget)
Packit 98cdb6
{
Packit 98cdb6
  GdkWindowAttr attributes;
Packit 98cdb6
  gint attributes_mask;
Packit 98cdb6
Packit 98cdb6
  gtk_widget_set_realized (widget, TRUE);
Packit 98cdb6
Packit 98cdb6
  if (!gtk_widget_get_has_window (widget))
Packit 98cdb6
    {
Packit 98cdb6
      widget->window = gtk_widget_get_parent_window (widget);
Packit 98cdb6
      g_object_ref (widget->window);
Packit 98cdb6
      widget->style = gtk_style_attach (widget->style, widget->window);
Packit 98cdb6
    }
Packit 98cdb6
  else
Packit 98cdb6
    {
Packit 98cdb6
      attributes.window_type = GDK_WINDOW_CHILD;
Packit 98cdb6
      attributes.x = widget->allocation.x;
Packit 98cdb6
      attributes.y = widget->allocation.y;
Packit 98cdb6
      attributes.width = widget->allocation.width;
Packit 98cdb6
      attributes.height = widget->allocation.height;
Packit 98cdb6
      attributes.wclass = GDK_INPUT_OUTPUT;
Packit 98cdb6
      attributes.visual = gtk_widget_get_visual (widget);
Packit 98cdb6
      attributes.colormap = gtk_widget_get_colormap (widget);
Packit 98cdb6
      attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
Packit 98cdb6
      attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
Packit 98cdb6
Packit 98cdb6
      widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
Packit 98cdb6
      gdk_window_set_user_data (widget->window, widget);
Packit 98cdb6
Packit 98cdb6
      widget->style = gtk_style_attach (widget->style, widget->window);
Packit 98cdb6
      gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
#define __GTK_MISC_C__
Packit 98cdb6
#include "gtkaliasdef.c"