]> Pileus Git - ~andy/gtk/blob - gtk/gtkmisc.c
applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[~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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtkcontainer.h"
28 #include "gtkmisc.h"
29
30
31 enum {
32   ARG_0,
33   ARG_XALIGN,
34   ARG_YALIGN,
35   ARG_XPAD,
36   ARG_YPAD
37 };
38
39 static void gtk_misc_class_init (GtkMiscClass *klass);
40 static void gtk_misc_init       (GtkMisc      *misc);
41 static void gtk_misc_realize    (GtkWidget    *widget);
42 static void gtk_misc_set_arg    (GtkObject    *object,
43                                  GtkArg       *arg,
44                                  guint         arg_id);
45 static void gtk_misc_get_arg    (GtkObject    *object,
46                                  GtkArg       *arg,
47                                  guint         arg_id);
48
49
50 GtkType
51 gtk_misc_get_type (void)
52 {
53   static GtkType misc_type = 0;
54
55   if (!misc_type)
56     {
57       static const GtkTypeInfo misc_info =
58       {
59         "GtkMisc",
60         sizeof (GtkMisc),
61         sizeof (GtkMiscClass),
62         (GtkClassInitFunc) gtk_misc_class_init,
63         (GtkObjectInitFunc) gtk_misc_init,
64         /* reserved_1 */ NULL,
65         /* reserved_2 */ NULL,
66         (GtkClassInitFunc) NULL,
67       };
68
69       misc_type = gtk_type_unique (GTK_TYPE_WIDGET, &misc_info);
70     }
71
72   return misc_type;
73 }
74
75 static void
76 gtk_misc_class_init (GtkMiscClass *class)
77 {
78   GtkObjectClass *object_class;
79   GtkWidgetClass *widget_class;
80
81   object_class = (GtkObjectClass*) class;
82   widget_class = (GtkWidgetClass*) class;
83
84   gtk_object_add_arg_type ("GtkMisc::xalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_XALIGN);
85   gtk_object_add_arg_type ("GtkMisc::yalign", GTK_TYPE_FLOAT, GTK_ARG_READWRITE, ARG_YALIGN);
86   gtk_object_add_arg_type ("GtkMisc::xpad", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_XPAD);
87   gtk_object_add_arg_type ("GtkMisc::ypad", GTK_TYPE_INT, GTK_ARG_READWRITE, ARG_YPAD);
88
89   object_class->set_arg = gtk_misc_set_arg;
90   object_class->get_arg = gtk_misc_get_arg;
91   
92   widget_class->realize = gtk_misc_realize;
93 }
94
95 static void
96 gtk_misc_init (GtkMisc *misc)
97 {
98   misc->xalign = 0.5;
99   misc->yalign = 0.5;
100   misc->xpad = 0;
101   misc->ypad = 0;
102 }
103
104 static void
105 gtk_misc_set_arg (GtkObject      *object,
106                   GtkArg         *arg,
107                   guint           arg_id)
108 {
109   GtkMisc *misc;
110
111   misc = GTK_MISC (object);
112
113   switch (arg_id)
114     {
115     case ARG_XALIGN:
116       gtk_misc_set_alignment (misc, GTK_VALUE_FLOAT (*arg), misc->yalign);
117       break;
118     case ARG_YALIGN:
119       gtk_misc_set_alignment (misc, misc->xalign, GTK_VALUE_FLOAT (*arg));
120       break;
121     case ARG_XPAD:
122       gtk_misc_set_padding (misc, GTK_VALUE_INT (*arg), misc->ypad);
123       break;
124     case ARG_YPAD:
125       gtk_misc_set_padding (misc, misc->xpad, GTK_VALUE_INT (*arg));
126       break;
127     default:
128       break;
129     }
130 }
131
132 static void
133 gtk_misc_get_arg (GtkObject      *object,
134                   GtkArg         *arg,
135                   guint           arg_id)
136 {
137   GtkMisc *misc;
138
139   misc = GTK_MISC (object);
140
141   switch (arg_id)
142     {
143     case ARG_XALIGN:
144       GTK_VALUE_FLOAT (*arg) = misc->xalign;
145       break;
146     case ARG_YALIGN:
147       GTK_VALUE_FLOAT (*arg) = misc->yalign;
148       break;
149     case ARG_XPAD:
150       GTK_VALUE_INT (*arg) = misc->xpad;
151       break;
152     case ARG_YPAD:
153       GTK_VALUE_INT (*arg) = misc->ypad;
154       break;
155     default:
156       arg->type = GTK_TYPE_INVALID;
157       break;
158     }
159 }
160
161 void
162 gtk_misc_set_alignment (GtkMisc *misc,
163                         gfloat   xalign,
164                         gfloat   yalign)
165 {
166   g_return_if_fail (misc != NULL);
167   g_return_if_fail (GTK_IS_MISC (misc));
168
169   if (xalign < 0.0)
170     xalign = 0.0;
171   else if (xalign > 1.0)
172     xalign = 1.0;
173
174   if (yalign < 0.0)
175     yalign = 0.0;
176   else if (yalign > 1.0)
177     yalign = 1.0;
178
179   if ((xalign != misc->xalign) || (yalign != misc->yalign))
180     {
181       misc->xalign = xalign;
182       misc->yalign = yalign;
183       
184       /* clear the area that was allocated before the change
185        */
186       if (GTK_WIDGET_DRAWABLE (misc))
187         {
188           GtkWidget *widget;
189           
190           widget = GTK_WIDGET (misc);
191           gtk_widget_queue_clear (widget);
192         }
193     }
194 }
195
196 void
197 gtk_misc_set_padding (GtkMisc *misc,
198                       gint     xpad,
199                       gint     ypad)
200 {
201   GtkRequisition *requisition;
202   
203   g_return_if_fail (misc != NULL);
204   g_return_if_fail (GTK_IS_MISC (misc));
205   
206   if (xpad < 0)
207     xpad = 0;
208   if (ypad < 0)
209     ypad = 0;
210   
211   if ((xpad != misc->xpad) || (ypad != misc->ypad))
212     {
213       requisition = &(GTK_WIDGET (misc)->requisition);
214       requisition->width -= misc->xpad * 2;
215       requisition->height -= misc->ypad * 2;
216       
217       misc->xpad = xpad;
218       misc->ypad = ypad;
219       
220       requisition->width += misc->xpad * 2;
221       requisition->height += misc->ypad * 2;
222       
223       if (GTK_WIDGET_DRAWABLE (misc))
224         gtk_widget_queue_resize (GTK_WIDGET (misc));
225     }
226 }
227
228 static void
229 gtk_misc_realize (GtkWidget *widget)
230 {
231   GtkMisc *misc;
232   GdkWindowAttr attributes;
233   gint attributes_mask;
234
235   g_return_if_fail (widget != NULL);
236   g_return_if_fail (GTK_IS_MISC (widget));
237
238   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
239   misc = GTK_MISC (widget);
240
241   if (GTK_WIDGET_NO_WINDOW (widget))
242     {
243       widget->window = gtk_widget_get_parent_window (widget);
244       gdk_window_ref (widget->window);
245       widget->style = gtk_style_attach (widget->style, widget->window);
246     }
247   else
248     {
249       attributes.window_type = GDK_WINDOW_CHILD;
250       attributes.x = widget->allocation.x;
251       attributes.y = widget->allocation.y;
252       attributes.width = widget->allocation.width;
253       attributes.height = widget->allocation.height;
254       attributes.wclass = GDK_INPUT_OUTPUT;
255       attributes.visual = gtk_widget_get_visual (widget);
256       attributes.colormap = gtk_widget_get_colormap (widget);
257       attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
258       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
259
260       widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
261       gdk_window_set_user_data (widget->window, widget);
262
263       widget->style = gtk_style_attach (widget->style, widget->window);
264       gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
265     }
266 }