]> Pileus Git - ~andy/gtk/blob - gtk/gtkeventbox.c
changed reversed_[12] to reserved_[12] in gtk_*_get_type functions.
[~andy/gtk] / gtk / gtkeventbox.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 "gtksignal.h"
20 #include "gtkeventbox.h"
21
22
23 static void gtk_event_box_class_init               (GtkEventBoxClass *klass);
24 static void gtk_event_box_init                     (GtkEventBox      *event_box);
25 static void gtk_event_box_realize                  (GtkWidget        *widget);
26 static void gtk_event_box_size_request             (GtkWidget        *widget,
27                                                     GtkRequisition   *requisition);
28 static void gtk_event_box_size_allocate            (GtkWidget        *widget,
29                                                     GtkAllocation    *allocation);
30 static void gtk_event_box_draw                     (GtkWidget    *widget,
31                                                    GdkRectangle *area);
32 static gint gtk_event_box_expose                   (GtkWidget      *widget,
33                                                    GdkEventExpose *event);
34
35
36 GtkType
37 gtk_event_box_get_type (void)
38 {
39   static GtkType event_box_type = 0;
40
41   if (!event_box_type)
42     {
43       GtkTypeInfo event_box_info =
44       {
45         "GtkEventBox",
46         sizeof (GtkEventBox),
47         sizeof (GtkEventBoxClass),
48         (GtkClassInitFunc) gtk_event_box_class_init,
49         (GtkObjectInitFunc) gtk_event_box_init,
50         /* reserved_1 */ NULL,
51         /* reserved_2 */ NULL,
52         (GtkClassInitFunc) NULL,
53       };
54
55       event_box_type = gtk_type_unique (gtk_bin_get_type (), &event_box_info);
56     }
57
58   return event_box_type;
59 }
60
61 static void
62 gtk_event_box_class_init (GtkEventBoxClass *class)
63 {
64   GtkWidgetClass *widget_class;
65
66   widget_class = (GtkWidgetClass*) class;
67
68   widget_class->realize = gtk_event_box_realize;
69   widget_class->size_request = gtk_event_box_size_request;
70   widget_class->size_allocate = gtk_event_box_size_allocate;
71   widget_class->draw = gtk_event_box_draw;
72   widget_class->expose_event = gtk_event_box_expose;
73 }
74
75 static void
76 gtk_event_box_init (GtkEventBox *event_box)
77 {
78   GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
79   GTK_WIDGET_SET_FLAGS (event_box, GTK_BASIC);
80 }
81
82 GtkWidget*
83 gtk_event_box_new (void)
84 {
85   return GTK_WIDGET ( gtk_type_new (gtk_event_box_get_type ()));
86 }
87
88 static void
89 gtk_event_box_realize (GtkWidget *widget)
90 {
91   GdkWindowAttr attributes;
92   gint attributes_mask;
93   gint border_width;
94
95   g_return_if_fail (widget != NULL);
96   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
97
98   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
99
100   border_width = GTK_CONTAINER (widget)->border_width;
101   
102   attributes.x = widget->allocation.x + border_width;
103   attributes.y = widget->allocation.y + border_width;
104   attributes.width = widget->allocation.width - 2*border_width;
105   attributes.height = widget->allocation.height - 2*border_width;
106   attributes.window_type = GDK_WINDOW_CHILD;
107   attributes.wclass = GDK_INPUT_OUTPUT;
108   attributes.visual = gtk_widget_get_visual (widget);
109   attributes.colormap = gtk_widget_get_colormap (widget);
110   attributes.event_mask = gtk_widget_get_events (widget)
111                         | GDK_BUTTON_MOTION_MASK
112                         | GDK_BUTTON_PRESS_MASK
113                         | GDK_BUTTON_RELEASE_MASK
114                         | GDK_EXPOSURE_MASK
115                         | GDK_ENTER_NOTIFY_MASK
116                         | GDK_LEAVE_NOTIFY_MASK;
117
118   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
119
120   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
121   gdk_window_set_user_data (widget->window, widget);
122
123   widget->style = gtk_style_attach (widget->style, widget->window);
124   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
125 }
126
127 static void
128 gtk_event_box_size_request (GtkWidget      *widget,
129                         GtkRequisition *requisition)
130 {
131   GtkBin *bin;
132
133   g_return_if_fail (widget != NULL);
134   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
135   g_return_if_fail (requisition != NULL);
136
137   bin = GTK_BIN (widget);
138
139   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
140   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
141
142   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
143     {
144       gtk_widget_size_request (bin->child, &bin->child->requisition);
145
146       requisition->width += bin->child->requisition.width;
147       requisition->height += bin->child->requisition.height;
148     }
149 }
150
151 static void
152 gtk_event_box_size_allocate (GtkWidget     *widget,
153                             GtkAllocation *allocation)
154 {
155   GtkBin *bin;
156   GtkAllocation child_allocation;
157
158   g_return_if_fail (widget != NULL);
159   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
160   g_return_if_fail (allocation != NULL);
161
162   widget->allocation = *allocation;
163   bin = GTK_BIN (widget);
164
165   child_allocation.x = 0;
166   child_allocation.y = 0;
167   child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
168   child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
169
170   if (GTK_WIDGET_REALIZED (widget))
171     {
172       gdk_window_move_resize (widget->window,
173                               allocation->x + GTK_CONTAINER (widget)->border_width,
174                               allocation->y + GTK_CONTAINER (widget)->border_width,
175                               child_allocation.width,
176                               child_allocation.height);
177     }
178   
179   if (bin->child)
180     {
181       gtk_widget_size_allocate (bin->child, &child_allocation);
182     }
183 }
184
185 static void
186 gtk_event_box_draw (GtkWidget    *widget,
187                    GdkRectangle *area)
188 {
189   GtkBin *bin;
190   GdkRectangle tmp_area;
191   GdkRectangle child_area;
192
193   g_return_if_fail (widget != NULL);
194   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
195
196   if (GTK_WIDGET_DRAWABLE (widget))
197     {
198       bin = GTK_BIN (widget);
199       tmp_area = *area;
200       tmp_area.x -= GTK_CONTAINER (widget)->border_width;
201       tmp_area.y -= GTK_CONTAINER (widget)->border_width;
202       
203       if (bin->child)
204         {
205           if (gtk_widget_intersect (bin->child, &tmp_area, &child_area))
206             gtk_widget_draw (bin->child, &child_area);
207         }
208     }
209 }
210
211 static gint
212 gtk_event_box_expose (GtkWidget      *widget,
213                      GdkEventExpose *event)
214 {
215   GtkBin *bin;
216   GdkEventExpose child_event;
217
218   g_return_val_if_fail (widget != NULL, FALSE);
219   g_return_val_if_fail (GTK_IS_EVENT_BOX (widget), FALSE);
220   g_return_val_if_fail (event != NULL, FALSE);
221
222   if (GTK_WIDGET_DRAWABLE (widget))
223     {
224       bin = GTK_BIN (widget);
225
226       child_event = *event;
227       if (bin->child &&
228           GTK_WIDGET_NO_WINDOW (bin->child) &&
229           gtk_widget_intersect (bin->child, &event->area, &child_event.area))
230         gtk_widget_event (bin->child, (GdkEvent*) &child_event);
231     }
232
233   return FALSE;
234 }
235