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