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