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