]> Pileus Git - ~andy/gtk/blob - gtk/gtkmisc.c
main part for GtkArgSetFunc/GtkArgGetFunc implementation.
[~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         (GtkArgSetFunc) NULL,
42         (GtkArgGetFunc) NULL,
43       };
44
45       misc_type = gtk_type_unique (gtk_widget_get_type (), &misc_info);
46     }
47
48   return misc_type;
49 }
50
51 static void
52 gtk_misc_class_init (GtkMiscClass *class)
53 {
54   GtkWidgetClass *widget_class;
55
56   widget_class = (GtkWidgetClass*) class;
57
58   widget_class->realize = gtk_misc_realize;
59 }
60
61 static void
62 gtk_misc_init (GtkMisc *misc)
63 {
64   GTK_WIDGET_SET_FLAGS (misc, GTK_BASIC);
65
66   misc->xalign = 0.5;
67   misc->yalign = 0.5;
68   misc->xpad = 0;
69   misc->ypad = 0;
70 }
71
72 void
73 gtk_misc_set_alignment (GtkMisc *misc,
74                         gfloat   xalign,
75                         gfloat   yalign)
76 {
77   g_return_if_fail (misc != NULL);
78   g_return_if_fail (GTK_IS_MISC (misc));
79
80   if (xalign < 0.0)
81     xalign = 0.0;
82   else if (xalign > 1.0)
83     xalign = 1.0;
84
85   if (yalign < 0.0)
86     yalign = 0.0;
87   else if (yalign > 1.0)
88     yalign = 1.0;
89
90   if ((xalign != misc->xalign) || (yalign != misc->yalign))
91     {
92       misc->xalign = xalign;
93       misc->yalign = yalign;
94
95       /* clear the area that was allocated before the change
96       */
97       if (GTK_WIDGET_VISIBLE (misc))
98         {
99           GtkWidget *widget;
100
101           widget = GTK_WIDGET (misc);
102           gdk_window_clear_area (widget->window,
103                                  widget->allocation.x,
104                                  widget->allocation.y,
105                                  widget->allocation.width,
106                                  widget->allocation.height);
107         }
108
109       gtk_widget_queue_draw (GTK_WIDGET (misc));
110     }
111 }
112
113 void
114 gtk_misc_set_padding (GtkMisc *misc,
115                       gint     xpad,
116                       gint     ypad)
117 {
118   GtkRequisition *requisition;
119
120   g_return_if_fail (misc != NULL);
121   g_return_if_fail (GTK_IS_MISC (misc));
122
123   if (xpad < 0)
124     xpad = 0;
125   if (ypad < 0)
126     ypad = 0;
127
128   if ((xpad != misc->xpad) || (ypad != misc->ypad))
129     {
130       requisition = &(GTK_WIDGET (misc)->requisition);
131       requisition->width -= misc->xpad * 2;
132       requisition->height -= misc->ypad * 2;
133
134       misc->xpad = xpad;
135       misc->ypad = ypad;
136
137       requisition->width += misc->xpad * 2;
138       requisition->height += misc->ypad * 2;
139
140       if (GTK_WIDGET (misc)->parent && GTK_WIDGET_VISIBLE (misc))
141         gtk_widget_queue_resize (GTK_WIDGET (misc));
142     }
143 }
144
145 static void
146 gtk_misc_realize (GtkWidget *widget)
147 {
148   GtkMisc *misc;
149   GdkWindowAttr attributes;
150   gint attributes_mask;
151
152   g_return_if_fail (widget != NULL);
153   g_return_if_fail (GTK_IS_MISC (widget));
154
155   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
156   misc = GTK_MISC (widget);
157
158   if (GTK_WIDGET_NO_WINDOW (widget))
159     {
160       widget->window = widget->parent->window;
161       widget->style = gtk_style_attach (widget->style, widget->window);
162     }
163   else
164     {
165       attributes.window_type = GDK_WINDOW_CHILD;
166       attributes.x = widget->allocation.x;
167       attributes.y = widget->allocation.y;
168       attributes.width = widget->allocation.width;
169       attributes.height = widget->allocation.height;
170       attributes.wclass = GDK_INPUT_OUTPUT;
171       attributes.visual = gtk_widget_get_visual (widget);
172       attributes.colormap = gtk_widget_get_colormap (widget);
173       attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
174       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
175
176       widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
177       gdk_window_set_user_data (widget->window, widget);
178
179       widget->style = gtk_style_attach (widget->style, widget->window);
180       gdk_window_set_back_pixmap (widget->window, NULL, TRUE);
181     }
182 }