gtk2/gtk/gtkaspectframe.c

362 lines
11 KiB
C
Raw Normal View History

1997-11-24 22:37:52 +00:00
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* GtkAspectFrame: Ensure that the child window has a specified aspect ratio
* or, if obey_child, has the same aspect ratio as its requested size
*
* Copyright Owen Taylor 4/9/97
*
* 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
1997-11-24 22:37:52 +00:00
*/
/*
* Modified by the GTK+ Team and others 1997-1999. 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/.
*/
1997-11-24 22:37:52 +00:00
#include "gtkaspectframe.h"
static void gtk_aspect_frame_class_init (GtkAspectFrameClass *klass);
static void gtk_aspect_frame_init (GtkAspectFrame *aspect_frame);
static void gtk_aspect_frame_draw (GtkWidget *widget,
GdkRectangle *area);
static void gtk_aspect_frame_paint (GtkWidget *widget,
GdkRectangle *area);
static gint gtk_aspect_frame_expose (GtkWidget *widget,
GdkEventExpose *event);
static void gtk_aspect_frame_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
#define MAX_RATIO 10000.0
#define MIN_RATIO 0.0001
reimplemented the signal storage system to use a linear array rather than Wed May 6 02:16:34 1998 Tim Janik <timj@gtk.org> * gtk/gtksignal.c: reimplemented the signal storage system to use a linear array rather than a hash table. be carefull *not* to keep any GtkSignal pointers across invokations of gtk_signal_next_and_invalidate() and therefore gtk_signal_new[v](). general code cleanups, made all allocations through memchunks. (gtk_signal_lookup): we now do the lookup of signals through a key_id for the signal names to avoid multiple hashing of the signal name on lookups for the several hirarchy levels of an object. Tue May 5 15:33:27 1998 Tim Janik <timj@gtk.org> * gtk/gtkmain.c (gtk_init): added gtk_signal_init() to avoid repeatedly checking for signal initialization in gtksignal.c. * gtk/gtktypeutils.c (TYPE_NODES_BLOCK_SIZE): reseted this to 200 after a long debugging period ;) Mon May 4 00:30:11 1998 Tim Janik <timj@gtk.org> * gtk/gtktypeutils.h (GTK_TYPE_IS_A): new macro to speedup gtk_type_is_a(). * gtk/gtktypeutils.c: reimplemented the type storage system to use a linear array rather than a hash table. it actually speeded up testgtk for a *considerable* amount. be carefull *not* to keep any GtkTypeNode pointers across invokations of gtk_type_node_next_and_invalidate() and therefore gtk_type_unique()! * gtk/gtkobject.h (GTK_IS_OBJECT_CLASS): new macro to test for the inheritance of a class pointer. (GTK_OBJECT): modifications for speedups. (GTK_IS_OBJECT): likewise. (GTK_OBJECT_CLASS): likewise. (suggested by owen). * gtk/gtkcontainer.h (GTK_IS_CONTAINER_CLASS): new macro. * gtk/gtkwidget.h (GTK_IS_WIDGET_CLASS): new macro. * gtk/gtk.defs (GtkTooltips): define GtkTooltips as object not as boxed, since its derivation changed 4 month ago ;(. showed up with the unique type name checking code in gtk_type_unique(). * random guint->GtkType and macro fixups.
1998-05-06 01:43:56 +00:00
GtkType
configure.in acheader.h gdk/gdkwindow.c Check for Shape extension both on Sun May 3 13:38:22 1998 Owen Taylor <otaylor@gtk.org> * configure.in acheader.h gdk/gdkwindow.c Check for Shape extension both on the client and server side. (And, more importantly, check for the shape extension so we may include -lXext even when compiling with --disable-xshm) Don't set override_redirect on all shaped windows. It isn't necessary. * gdk/gdkwindow.c: Set ->colormap to NULL for root and foreign windows. Use this to check if we need to get the colormap from X. Fri May 1 22:32:47 1998 Owen Taylor <otaylor@gtk.org> * gtk/gtkbutton.c (gtk_button_paint): Draw the areas between the default and the button always in GTK_STATE_NORMAL. * gtk/gtkrange.c (gtk_range_style_set): Added a style_set callback. Fri May 1 16:40:57 1998 Owen Taylor <otaylor@gtk.org> * gdk/gdkpixmap.c (gdk_pixmap_colormap_create_from_xpmp[_d]): Fix a buffer overflow on pixmaps that claim to have more than 31 characters per pixel. (gdk_pixmap_read_string): Don't wrap around strings longer than half of address space ;-) * gtk/gtk[vh]ruler.c gtk/gtkinputdialog.c: Expand some buffers that were used for printing integers. * */* (almost): Style: All int foo () { ... } changed to int foo (void) { ... } ^^^^^^^ This is why some many files changed Even where there were proper prototypes elsewhere. * gdk/gxid.c (handle_claim_device): Some extra checks. It isn't safe against being fed bad X id's, but at least it should be safe against deleting all your files.
1998-05-03 22:41:32 +00:00
gtk_aspect_frame_get_type (void)
1997-11-24 22:37:52 +00:00
{
reimplemented the signal storage system to use a linear array rather than Wed May 6 02:16:34 1998 Tim Janik <timj@gtk.org> * gtk/gtksignal.c: reimplemented the signal storage system to use a linear array rather than a hash table. be carefull *not* to keep any GtkSignal pointers across invokations of gtk_signal_next_and_invalidate() and therefore gtk_signal_new[v](). general code cleanups, made all allocations through memchunks. (gtk_signal_lookup): we now do the lookup of signals through a key_id for the signal names to avoid multiple hashing of the signal name on lookups for the several hirarchy levels of an object. Tue May 5 15:33:27 1998 Tim Janik <timj@gtk.org> * gtk/gtkmain.c (gtk_init): added gtk_signal_init() to avoid repeatedly checking for signal initialization in gtksignal.c. * gtk/gtktypeutils.c (TYPE_NODES_BLOCK_SIZE): reseted this to 200 after a long debugging period ;) Mon May 4 00:30:11 1998 Tim Janik <timj@gtk.org> * gtk/gtktypeutils.h (GTK_TYPE_IS_A): new macro to speedup gtk_type_is_a(). * gtk/gtktypeutils.c: reimplemented the type storage system to use a linear array rather than a hash table. it actually speeded up testgtk for a *considerable* amount. be carefull *not* to keep any GtkTypeNode pointers across invokations of gtk_type_node_next_and_invalidate() and therefore gtk_type_unique()! * gtk/gtkobject.h (GTK_IS_OBJECT_CLASS): new macro to test for the inheritance of a class pointer. (GTK_OBJECT): modifications for speedups. (GTK_IS_OBJECT): likewise. (GTK_OBJECT_CLASS): likewise. (suggested by owen). * gtk/gtkcontainer.h (GTK_IS_CONTAINER_CLASS): new macro. * gtk/gtkwidget.h (GTK_IS_WIDGET_CLASS): new macro. * gtk/gtk.defs (GtkTooltips): define GtkTooltips as object not as boxed, since its derivation changed 4 month ago ;(. showed up with the unique type name checking code in gtk_type_unique(). * random guint->GtkType and macro fixups.
1998-05-06 01:43:56 +00:00
static GtkType aspect_frame_type = 0;
1997-11-24 22:37:52 +00:00
if (!aspect_frame_type)
{
static const GtkTypeInfo aspect_frame_info =
1997-11-24 22:37:52 +00:00
{
"GtkAspectFrame",
sizeof (GtkAspectFrame),
sizeof (GtkAspectFrameClass),
(GtkClassInitFunc) gtk_aspect_frame_class_init,
(GtkObjectInitFunc) gtk_aspect_frame_init,
/* reserved_1 */ NULL,
/* reserved_2 */ NULL,
call the base class init fucntions from all parent types upon class Sun Jun 28 04:29:10 1998 Tim Janik <timj@gtk.org> * gtk/gtktypeutils.c (gtk_type_class_init): call the base class init fucntions from all parent types upon class initialization. * gtk/gtkcontainer.c: (gtk_container_get_type): announce gtk_container_base_class_init to the type system. (gtk_container_base_class_init): new function to feature base class initialization. (gtk_container_get_child_arg): (gtk_container_set_child_arg): call the GtkContainerClass get_child_arg and set_child_arg methods of the class indicated through the argument name. * gtk/gtkobject.c: (gtk_object_base_class_init): new function to feature base class initialization. (gtk_object_init_type): announce gtk_object_base_class_init to the type system. (gtk_object_class_init): setup the get_arg and set_arg pointers for GtkObjectClass. (gtk_object_setv): (gtk_object_getv): call the GtkObjectClass get_arg and set_arg methods, instead of bothering the type system with this. * gtk/gtkaccellabel.c: * gtk/gtkbutton.c: * gtk/gtkradiobutton.c: * gtk/gtktable.c: * gtk/gtktogglebutton.c: * gtk/gtktipsquery.c: * gtk/gtkbox.c: * gtk/gtkpacker.c: * gtk/gtkwidget.c: * gtk/gtkwindow.c: * gtk/gtkframe.c: * gtk/gtkmisc.c: * gtk/gtklabel.c: set the object_class->{g|s}et_arg pointers to the corresponding gtk_*_{g|s]et_arg functions and updated the gtk_*_get_type functions wrt GtkTypeInfo initialization. changed a lot of the set/get arg functions to take a GtkObject argument. gtk/gtkadjustment.c: gtk/gtkalignment.c: gtk/gtkarrow.c: gtk/gtkaspectframe.c: gtk/gtkbbox.c: gtk/gtkbin.c: gtk/gtkcheckbutton.c: gtk/gtkcheckmenuitem.c: gtk/gtkclist.c: gtk/gtkcolorsel.c: gtk/gtkcombo.c: gtk/gtkctree.c: gtk/gtkcurve.c: gtk/gtkdata.c: gtk/gtkdialog.c: gtk/gtkdrawingarea.c: gtk/gtkeditable.c: gtk/gtkentry.c: gtk/gtkeventbox.c: gtk/gtkfilesel.c: gtk/gtkfixed.c: gtk/gtkfontsel.c: gtk/gtkgamma.c: gtk/gtkhandlebox.c: gtk/gtkhbbox.c: gtk/gtkhbox.c: gtk/gtkhpaned.c: gtk/gtkhruler.c: gtk/gtkhscale.c: gtk/gtkhscrollbar.c: gtk/gtkhseparator.c: gtk/gtkimage.c: gtk/gtkinputdialog.c: gtk/gtkitem.c: gtk/gtkitemfactory.c: gtk/gtklist.c: gtk/gtklistitem.c: gtk/gtkmenu.c: gtk/gtkmenubar.c: gtk/gtkmenuitem.c: gtk/gtkmenushell.c: gtk/gtknotebook.c: gtk/gtkoptionmenu.c: gtk/gtkpaned.c: gtk/gtkpixmap.c: gtk/gtkpreview.c: gtk/gtkprogressbar.c: gtk/gtkradiomenuitem.c: gtk/gtkrange.c: gtk/gtkruler.c: gtk/gtkscale.c: gtk/gtkscrollbar.c: gtk/gtkscrolledwindow.c: gtk/gtkseparator.c: gtk/gtkspinbutton.c: gtk/gtkstatusbar.c: gtk/gtktext.c: gtk/gtktoolbar.c: gtk/gtktooltips.c: gtk/gtktree.c: gtk/gtktreeitem.c: gtk/gtkvbbox.c: gtk/gtkvbox.c: gtk/gtkviewport.c: gtk/gtkvpaned.c: gtk/gtkvruler.c: gtk/gtkvscale.c: gtk/gtkvscrollbar.c: gtk/gtkvseparator.c: updated the GtkTypeInfo initialization code to match the modified GtkTypeInfo structure.
1998-06-28 07:46:10 +00:00
(GtkClassInitFunc) NULL,
1997-11-24 22:37:52 +00:00
};
aspect_frame_type = gtk_type_unique (gtk_frame_get_type (), &aspect_frame_info);
}
return aspect_frame_type;
}
static void
gtk_aspect_frame_class_init (GtkAspectFrameClass *class)
{
GtkWidgetClass *widget_class;
widget_class = (GtkWidgetClass*) class;
widget_class->draw = gtk_aspect_frame_draw;
widget_class->expose_event = gtk_aspect_frame_expose;
widget_class->size_allocate = gtk_aspect_frame_size_allocate;
}
static void
gtk_aspect_frame_init (GtkAspectFrame *aspect_frame)
{
aspect_frame->xalign = 0.5;
aspect_frame->yalign = 0.5;
aspect_frame->ratio = 1.0;
aspect_frame->obey_child = 1;
aspect_frame->center_allocation.x = -1;
aspect_frame->center_allocation.y = -1;
aspect_frame->center_allocation.width = 1;
aspect_frame->center_allocation.height = 1;
}
GtkWidget*
gtk_aspect_frame_new (const gchar *label,
gfloat xalign,
gfloat yalign,
gfloat ratio,
gint obey_child)
{
GtkAspectFrame *aspect_frame;
aspect_frame = gtk_type_new (gtk_aspect_frame_get_type ());
aspect_frame->xalign = CLAMP (xalign, 0.0, 1.0);
aspect_frame->yalign = CLAMP (yalign, 0.0, 1.0);
aspect_frame->ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
aspect_frame->obey_child = obey_child;
gtk_frame_set_label (GTK_FRAME(aspect_frame), label);
return GTK_WIDGET (aspect_frame);
}
void
gtk_aspect_frame_set (GtkAspectFrame *aspect_frame,
gfloat xalign,
gfloat yalign,
gfloat ratio,
gint obey_child)
{
g_return_if_fail (aspect_frame != NULL);
g_return_if_fail (GTK_IS_ASPECT_FRAME (aspect_frame));
xalign = CLAMP (xalign, 0.0, 1.0);
yalign = CLAMP (yalign, 0.0, 1.0);
ratio = CLAMP (ratio, MIN_RATIO, MAX_RATIO);
if ((aspect_frame->xalign != xalign) ||
(aspect_frame->yalign != yalign) ||
(aspect_frame->ratio != ratio) ||
(aspect_frame->obey_child != obey_child))
{
GtkWidget *widget = GTK_WIDGET(aspect_frame);
1997-11-24 22:37:52 +00:00
aspect_frame->xalign = xalign;
aspect_frame->yalign = yalign;
aspect_frame->ratio = ratio;
aspect_frame->obey_child = obey_child;
if (GTK_WIDGET_DRAWABLE(widget))
gtk_widget_queue_clear (widget);
gtk_widget_queue_resize (widget);
1997-11-24 22:37:52 +00:00
}
}
static void
gtk_aspect_frame_paint (GtkWidget *widget,
GdkRectangle *area)
{
GtkFrame *frame;
gint height_extra;
gint label_area_width;
gint x, y, x2, y2;
1997-11-24 22:37:52 +00:00
GtkAllocation *allocation;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
g_return_if_fail (area != NULL);
if (GTK_WIDGET_DRAWABLE (widget))
{
frame = GTK_FRAME (widget);
allocation = &GTK_ASPECT_FRAME(widget)->center_allocation;
height_extra = frame->label_height - widget->style->klass->xthickness;
height_extra = MAX (height_extra, 0);
x = GTK_CONTAINER (frame)->border_width;
y = GTK_CONTAINER (frame)->border_width;
if (frame->label)
{
label_area_width = (allocation->width +
GTK_CONTAINER (frame)->border_width * 2 -
widget->style->klass->xthickness * 2);
x2 = ((label_area_width - frame->label_width) * frame->label_xalign +
GTK_CONTAINER (frame)->border_width + widget->style->klass->xthickness);
y2 = (GTK_CONTAINER (frame)->border_width + widget->style->font->ascent);
gtk_paint_shadow_gap (widget->style, widget->window,
GTK_STATE_NORMAL, frame->shadow_type,
area, widget, "frame",
allocation->x + x,
allocation->y + y + height_extra / 2,
allocation->width - x * 2,
allocation->height - y * 2 - height_extra / 2,
GTK_POS_TOP,
x2 + 2 - x, frame->label_width - 4);
gtk_paint_string (widget->style, widget->window, GTK_WIDGET_STATE (widget),
area, widget, "frame",
allocation->x + x2 + 3,
allocation->y + y2,
frame->label);
1997-11-24 22:37:52 +00:00
}
else
gtk_paint_shadow (widget->style, widget->window,
GTK_STATE_NORMAL, frame->shadow_type,
area, widget, "frame",
allocation->x + x,
allocation->y + y + height_extra / 2,
allocation->width - x * 2,
allocation->height - y * 2 - height_extra / 2);
1997-11-24 22:37:52 +00:00
}
}
/* the only modification to the next two routines is to call
gtk_aspect_frame_paint instead of gtk_frame_paint */
static void
gtk_aspect_frame_draw (GtkWidget *widget,
GdkRectangle *area)
{
GtkBin *bin;
GdkRectangle child_area;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
g_return_if_fail (area != NULL);
if (GTK_WIDGET_DRAWABLE (widget))
{
bin = GTK_BIN (widget);
gtk_aspect_frame_paint (widget, area);
if (bin->child && gtk_widget_intersect (bin->child, area, &child_area))
gtk_widget_draw (bin->child, &child_area);
}
}
static gint
gtk_aspect_frame_expose (GtkWidget *widget,
GdkEventExpose *event)
{
GtkBin *bin;
GdkEventExpose child_event;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (GTK_IS_ASPECT_FRAME (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
if (GTK_WIDGET_DRAWABLE (widget))
{
bin = GTK_BIN (widget);
gtk_aspect_frame_paint (widget, &event->area);
child_event = *event;
if (bin->child &&
GTK_WIDGET_NO_WINDOW (bin->child) &&
gtk_widget_intersect (bin->child, &event->area, &child_event.area))
gtk_widget_event (bin->child, (GdkEvent*) &child_event);
}
return FALSE;
}
static void
gtk_aspect_frame_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkFrame *frame;
GtkAspectFrame *aspect_frame;
GtkBin *bin;
GtkAllocation child_allocation;
gint x,y;
gint width,height;
gdouble ratio;
g_return_if_fail (widget != NULL);
g_return_if_fail (GTK_IS_ASPECT_FRAME (widget));
g_return_if_fail (allocation != NULL);
aspect_frame = GTK_ASPECT_FRAME (widget);
frame = GTK_FRAME (widget);
bin = GTK_BIN (widget);
if (GTK_WIDGET_DRAWABLE (widget) &&
1997-11-24 22:37:52 +00:00
((widget->allocation.x != allocation->x) ||
(widget->allocation.y != allocation->y) ||
(widget->allocation.width != allocation->width) ||
(widget->allocation.height != allocation->height)) &&
(widget->allocation.width != 0) &&
(widget->allocation.height != 0))
gdk_window_clear_area (widget->window,
widget->allocation.x,
widget->allocation.y,
widget->allocation.width,
widget->allocation.height);
widget->allocation = *allocation;
if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
{
if (aspect_frame->obey_child)
{
GtkRequisition child_requisition;
gtk_widget_get_child_requisition (bin->child, &child_requisition);
if (child_requisition.height != 0)
1997-11-24 22:37:52 +00:00
{
ratio = (gdouble)child_requisition.width /
child_requisition.height;
1997-11-24 22:37:52 +00:00
if (ratio < MIN_RATIO) ratio = MIN_RATIO;
}
else if (child_requisition.width != 0)
ratio = MAX_RATIO;
1997-11-24 22:37:52 +00:00
else
ratio = 1.0;
}
else
ratio = aspect_frame->ratio;
x = (GTK_CONTAINER (frame)->border_width +
GTK_WIDGET (frame)->style->klass->xthickness);
width = allocation->width - x * 2;
y = (GTK_CONTAINER (frame)->border_width +
MAX (frame->label_height, GTK_WIDGET (frame)->style->klass->ythickness));
height = (allocation->height - y -
GTK_CONTAINER (frame)->border_width -
GTK_WIDGET (frame)->style->klass->ythickness);
/* make sure we don't allocate a negative width or height,
* since that will be cast to a (very big) guint16 */
width = MAX (1, width);
height = MAX (1, height);
if (ratio * height > width)
1997-11-24 22:37:52 +00:00
{
child_allocation.width = width;
child_allocation.height = width/ratio + 0.5;
1997-11-24 22:37:52 +00:00
}
else
1997-11-24 22:37:52 +00:00
{
child_allocation.width = ratio*height + 0.5;
1997-11-24 22:37:52 +00:00
child_allocation.height = height;
}
child_allocation.x = aspect_frame->xalign * (width - child_allocation.width) + allocation->x + x;
child_allocation.y = aspect_frame->yalign * (height - child_allocation.height) + allocation->y + y;
aspect_frame->center_allocation.width = child_allocation.width + 2*x;
aspect_frame->center_allocation.x = child_allocation.x - x;
aspect_frame->center_allocation.height = child_allocation.height + y +
GTK_CONTAINER (frame)->border_width +
GTK_WIDGET (frame)->style->klass->ythickness;
aspect_frame->center_allocation.y = child_allocation.y - y;
gtk_widget_size_allocate (bin->child, &child_allocation);
}
}