]> Pileus Git - ~andy/gtk/blob - gtk/gtkmisc.c
call the base class init fucntions from all parent types upon class
[~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       GtkTypeInfo misc_info =
50       {
51         "GtkMisc",
52         sizeof (GtkMisc),
53         sizeof (GtkMiscClass),
54         (GtkClassInitFunc) gtk_misc_class_init,
55         (GtkObjectInitFunc) gtk_misc_init,
56         /* reversed_1 */ NULL,
57         /* reversed_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_DOUBLE, GTK_ARG_READWRITE, ARG_XALIGN);
77   gtk_object_add_arg_type ("GtkMisc::yalign", GTK_TYPE_DOUBLE, 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   GTK_WIDGET_SET_FLAGS (misc, GTK_BASIC);
91
92   misc->xalign = 0.5;
93   misc->yalign = 0.5;
94   misc->xpad = 0;
95   misc->ypad = 0;
96 }
97
98 static void
99 gtk_misc_set_arg (GtkObject      *object,
100                   GtkArg         *arg,
101                   guint           arg_id)
102 {
103   GtkMisc *misc;
104
105   misc = GTK_MISC (object);
106
107   switch (arg_id)
108     {
109     case ARG_XALIGN:
110       gtk_misc_set_alignment (misc, GTK_VALUE_DOUBLE (*arg), misc->yalign);
111       break;
112     case ARG_YALIGN:
113       gtk_misc_set_alignment (misc, misc->xalign, GTK_VALUE_DOUBLE (*arg));
114       break;
115     case ARG_XPAD:
116       gtk_misc_set_padding (misc, GTK_VALUE_INT (*arg), misc->ypad);
117       break;
118     case ARG_YPAD:
119       gtk_misc_set_padding (misc, misc->xpad, GTK_VALUE_INT (*arg));
120       break;
121     default:
122       break;
123     }
124 }
125
126 static void
127 gtk_misc_get_arg (GtkObject      *object,
128                   GtkArg         *arg,
129                   guint           arg_id)
130 {
131   GtkMisc *misc;
132
133   misc = GTK_MISC (object);
134
135   switch (arg_id)
136     {
137     case ARG_XALIGN:
138       GTK_VALUE_DOUBLE (*arg) = misc->xalign;
139       break;
140     case ARG_YALIGN:
141       GTK_VALUE_DOUBLE (*arg) = misc->yalign;
142       break;
143     case ARG_XPAD:
144       GTK_VALUE_INT (*arg) = misc->xpad;
145       break;
146     case ARG_YPAD:
147       GTK_VALUE_INT (*arg) = misc->ypad;
148       break;
149     default:
150       arg->type = GTK_TYPE_INVALID;
151       break;
152     }
153 }
154
155 void
156 gtk_misc_set_alignment (GtkMisc *misc,
157                         gfloat   xalign,
158                         gfloat   yalign)
159 {
160   g_return_if_fail (misc != NULL);
161   g_return_if_fail (GTK_IS_MISC (misc));
162
163   if (xalign < 0.0)
164     xalign = 0.0;
165   else if (xalign > 1.0)
166     xalign = 1.0;
167
168   if (yalign < 0.0)
169     yalign = 0.0;
170   else if (yalign > 1.0)
171     yalign = 1.0;
172
173   if ((xalign != misc->xalign) || (yalign != misc->yalign))
174     {
175       misc->xalign = xalign;
176       misc->yalign = yalign;
177       
178       /* clear the area that was allocated before the change
179        */
180       if (GTK_WIDGET_DRAWABLE (misc))
181         {
182           GtkWidget *widget;
183           
184           widget = GTK_WIDGET (misc);
185           gdk_window_clear_area (widget->window,
186                                  widget->allocation.x,
187                                  widget->allocation.y,
188                                  widget->allocation.width,
189                                  widget->allocation.height);
190           gtk_widget_queue_draw (GTK_WIDGET (misc));
191         }
192     }
193 }
194
195 void
196 gtk_misc_set_padding (GtkMisc *misc,
197                       gint     xpad,
198                       gint     ypad)
199 {
200   GtkRequisition *requisition;
201   
202   g_return_if_fail (misc != NULL);
203   g_return_if_fail (GTK_IS_MISC (misc));
204   
205   if (xpad < 0)
206     xpad = 0;
207   if (ypad < 0)
208     ypad = 0;
209   
210   if ((xpad != misc->xpad) || (ypad != misc->ypad))
211     {
212       requisition = &(GTK_WIDGET (misc)->requisition);
213       requisition->width -= misc->xpad * 2;
214       requisition->height -= misc->ypad * 2;
215       
216       misc->xpad = xpad;
217       misc->ypad = ypad;
218       
219       requisition->width += misc->xpad * 2;
220       requisition->height += misc->ypad * 2;
221       
222       if (GTK_WIDGET_DRAWABLE (misc))
223         gtk_widget_queue_resize (GTK_WIDGET (misc));
224     }
225 }
226
227 static void
228 gtk_misc_realize (GtkWidget *widget)
229 {
230   GtkMisc *misc;
231   GdkWindowAttr attributes;
232   gint attributes_mask;
233
234   g_return_if_fail (widget != NULL);
235   g_return_if_fail (GTK_IS_MISC (widget));
236
237   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
238   misc = GTK_MISC (widget);
239
240   if (GTK_WIDGET_NO_WINDOW (widget))
241     {
242       widget->window = gtk_widget_get_parent_window (widget);
243       gdk_window_ref (widget->window);
244       widget->style = gtk_style_attach (widget->style, widget->window);
245     }
246   else
247     {
248       attributes.window_type = GDK_WINDOW_CHILD;
249       attributes.x = widget->allocation.x;
250       attributes.y = widget->allocation.y;
251       attributes.width = widget->allocation.width;
252       attributes.height = widget->allocation.height;
253       attributes.wclass = GDK_INPUT_OUTPUT;
254       attributes.visual = gtk_widget_get_visual (widget);
255       attributes.colormap = gtk_widget_get_colormap (widget);
256       attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
257       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
258
259       widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
260       gdk_window_set_user_data (widget->window, widget);
261
262       widget->style = gtk_style_attach (widget->style, widget->window);
263       gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
264     }
265 }