]> Pileus Git - ~andy/gtk/blob - gtk/gtkmisc.c
0ef8f0731c0367f4d6f3fb293b68bc4bdd648ee6
[~andy/gtk] / gtk / gtkmisc.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include "gtkcontainer.h"
19 #include "gtkmisc.h"
20
21
22 static void gtk_misc_class_init (GtkMiscClass *klass);
23 static void gtk_misc_init       (GtkMisc      *misc);
24 static void gtk_misc_realize    (GtkWidget    *widget);
25
26
27 guint
28 gtk_misc_get_type ()
29 {
30   static guint misc_type = 0;
31
32   if (!misc_type)
33     {
34       GtkTypeInfo misc_info =
35       {
36         "GtkMisc",
37         sizeof (GtkMisc),
38         sizeof (GtkMiscClass),
39         (GtkClassInitFunc) gtk_misc_class_init,
40         (GtkObjectInitFunc) gtk_misc_init,
41         (GtkArgFunc) NULL,
42       };
43
44       misc_type = gtk_type_unique (gtk_widget_get_type (), &misc_info);
45     }
46
47   return misc_type;
48 }
49
50 static void
51 gtk_misc_class_init (GtkMiscClass *class)
52 {
53   GtkWidgetClass *widget_class;
54
55   widget_class = (GtkWidgetClass*) class;
56
57   widget_class->realize = gtk_misc_realize;
58 }
59
60 static void
61 gtk_misc_init (GtkMisc *misc)
62 {
63   GTK_WIDGET_SET_FLAGS (misc, GTK_BASIC);
64
65   misc->xalign = 0.5;
66   misc->yalign = 0.5;
67   misc->xpad = 0;
68   misc->ypad = 0;
69 }
70
71 void
72 gtk_misc_set_alignment (GtkMisc *misc,
73                         gfloat   xalign,
74                         gfloat   yalign)
75 {
76   g_return_if_fail (misc != NULL);
77   g_return_if_fail (GTK_IS_MISC (misc));
78
79   if (xalign < 0.0)
80     xalign = 0.0;
81   else if (xalign > 1.0)
82     xalign = 1.0;
83
84   if (yalign < 0.0)
85     yalign = 0.0;
86   else if (yalign > 1.0)
87     yalign = 1.0;
88
89   if ((xalign != misc->xalign) || (yalign != misc->yalign))
90     {
91       misc->xalign = xalign;
92       misc->yalign = yalign;
93
94       /* clear the area that was allocated before the change
95       */
96       if (GTK_WIDGET_VISIBLE (misc))
97         {
98           GtkWidget *widget;
99
100           widget = GTK_WIDGET (misc);
101           gdk_window_clear_area (widget->window,
102                                  widget->allocation.x,
103                                  widget->allocation.y,
104                                  widget->allocation.width,
105                                  widget->allocation.height);
106         }
107
108       gtk_widget_queue_draw (GTK_WIDGET (misc));
109     }
110 }
111
112 void
113 gtk_misc_set_padding (GtkMisc *misc,
114                       gint     xpad,
115                       gint     ypad)
116 {
117   GtkRequisition *requisition;
118
119   g_return_if_fail (misc != NULL);
120   g_return_if_fail (GTK_IS_MISC (misc));
121
122   if (xpad < 0)
123     xpad = 0;
124   if (ypad < 0)
125     ypad = 0;
126
127   if ((xpad != misc->xpad) || (ypad != misc->ypad))
128     {
129       requisition = &(GTK_WIDGET (misc)->requisition);
130       requisition->width -= misc->xpad * 2;
131       requisition->height -= misc->ypad * 2;
132
133       misc->xpad = xpad;
134       misc->ypad = ypad;
135
136       requisition->width += misc->xpad * 2;
137       requisition->height += misc->ypad * 2;
138
139       if (GTK_WIDGET (misc)->parent && GTK_WIDGET_VISIBLE (misc))
140         gtk_widget_queue_resize (GTK_WIDGET (misc));
141     }
142 }
143
144 static void
145 gtk_misc_realize (GtkWidget *widget)
146 {
147   GtkMisc *misc;
148   GdkWindowAttr attributes;
149   gint attributes_mask;
150
151   g_return_if_fail (widget != NULL);
152   g_return_if_fail (GTK_IS_MISC (widget));
153
154   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
155   misc = GTK_MISC (widget);
156
157   if (GTK_WIDGET_NO_WINDOW (widget))
158     {
159       widget->window = widget->parent->window;
160       widget->style = gtk_style_attach (widget->style, widget->window);
161     }
162   else
163     {
164       attributes.window_type = GDK_WINDOW_CHILD;
165       attributes.x = widget->allocation.x;
166       attributes.y = widget->allocation.y;
167       attributes.width = widget->allocation.width;
168       attributes.height = widget->allocation.height;
169       attributes.wclass = GDK_INPUT_OUTPUT;
170       attributes.visual = gtk_widget_get_visual (widget);
171       attributes.colormap = gtk_widget_get_colormap (widget);
172       attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
173       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
174
175       widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
176       gdk_window_set_user_data (widget->window, widget);
177
178       widget->style = gtk_style_attach (widget->style, widget->window);
179       gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
180     }
181 }