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