]> Pileus Git - ~andy/gtk/blob - gtk/gtkdrawingarea.c
Change FSF Address
[~andy/gtk] / gtk / gtkdrawingarea.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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 #include "config.h"
26 #include "gtkdrawingarea.h"
27 #include "gtkintl.h"
28
29
30 /**
31  * SECTION:gtkdrawingarea
32  * @Short_description: A widget for custom user interface elements
33  * @Title: GtkDrawingArea
34  * @See_also: #GtkImage
35  *
36  * The #GtkDrawingArea widget is used for creating custom user interface
37  * elements. It's essentially a blank widget; you can draw on it. After
38  * creating a drawing area, the application may want to connect to:
39  *
40  * <itemizedlist>
41  *   <listitem>
42  *     <para>
43  *     Mouse and button press signals to respond to input from
44  *     the user. (Use gtk_widget_add_events() to enable events
45  *     you wish to receive.)
46  *     </para>
47  *   </listitem>
48  *   <listitem>
49  *     <para>
50  *     The #GtkWidget::realize signal to take any necessary actions
51  *     when the widget is instantiated on a particular display.
52  *     (Create GDK resources in response to this signal.)
53  *     </para>
54  *   </listitem>
55  *   <listitem>
56  *     <para>
57  *     The #GtkWidget::configure-event signal to take any necessary
58  *     actions when the widget changes size.
59  *     </para>
60  *   </listitem>
61  *   <listitem>
62  *     <para>
63  *     The #GtkWidget::draw signal to handle redrawing the
64  *     contents of the widget.
65  *     </para>
66  *   </listitem>
67  * </itemizedlist>
68  *
69  * The following code portion demonstrates using a drawing
70  * area to display a circle in the normal widget foreground
71  * color.
72  *
73  * Note that GDK automatically clears the exposed area to the
74  * background color before sending the expose event, and that
75  * drawing is implicitly clipped to the exposed area.
76  *
77  * <example>
78  * <title>Simple GtkDrawingArea usage</title>
79  * <programlisting>
80  * gboolean
81  * draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
82  * {
83  *   guint width, height;
84  *   GdkRGBA color;
85  *
86  *   width = gtk_widget_get_allocated_width (widget);
87  *   height = gtk_widget_get_allocated_height (widget);
88  *   cairo_arc (cr,
89  *              width / 2.0, height / 2.0,
90  *              MIN (width, height) / 2.0,
91  *              0, 2 * G_PI);
92  *
93  *   gtk_style_context_get_color (gtk_widget_get_style_context (widget),
94  *                                0,
95  *                                &amp;color);
96  *   gdk_cairo_set_source_rgba (cr, &amp;color);
97  *
98  *   cairo_fill (cr);
99  *
100  *  return FALSE;
101  * }
102  * [...]
103  *   GtkWidget &ast;drawing_area = gtk_drawing_area_new (<!-- -->);
104  *   gtk_widget_set_size_request (drawing_area, 100, 100);
105  *   g_signal_connect (G_OBJECT (drawing_area), "draw",
106  *                     G_CALLBACK (draw_callback), NULL);
107  * </programlisting>
108  * </example>
109  *
110  * Draw signals are normally delivered when a drawing area first comes
111  * onscreen, or when it's covered by another window and then uncovered.
112  * You can also force an expose event by adding to the "damage region"
113  * of the drawing area's window; gtk_widget_queue_draw_area() and
114  * gdk_window_invalidate_rect() are equally good ways to do this.
115  * You'll then get a draw signal for the invalid region.
116  *
117  * The available routines for drawing are documented on the <link
118  * linkend="gdk3-Cairo-Interaction">GDK Drawing Primitives</link> page
119  * and the cairo documentation.
120  *
121  * To receive mouse events on a drawing area, you will need to enable
122  * them with gtk_widget_add_events(). To receive keyboard events, you
123  * will need to set the "can-focus" property on the drawing area, and you
124  * should probably draw some user-visible indication that the drawing
125  * area is focused. Use gtk_widget_has_focus() in your expose event
126  * handler to decide whether to draw the focus indicator. See
127  * gtk_render_focus() for one way to draw focus.
128  */
129
130 static void gtk_drawing_area_realize       (GtkWidget           *widget);
131 static void gtk_drawing_area_size_allocate (GtkWidget           *widget,
132                                             GtkAllocation       *allocation);
133 static void gtk_drawing_area_send_configure (GtkDrawingArea     *darea);
134
135 G_DEFINE_TYPE (GtkDrawingArea, gtk_drawing_area, GTK_TYPE_WIDGET)
136
137 static void
138 gtk_drawing_area_class_init (GtkDrawingAreaClass *class)
139 {
140   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
141
142   widget_class->realize = gtk_drawing_area_realize;
143   widget_class->size_allocate = gtk_drawing_area_size_allocate;
144 }
145
146 static void
147 gtk_drawing_area_init (GtkDrawingArea *darea)
148 {
149 }
150
151 /**
152  * gtk_drawing_area_new:
153  *
154  * Creates a new drawing area.
155  *
156  * Returns: a new #GtkDrawingArea
157  */
158 GtkWidget*
159 gtk_drawing_area_new (void)
160 {
161   return g_object_new (GTK_TYPE_DRAWING_AREA, NULL);
162 }
163
164 static void
165 gtk_drawing_area_realize (GtkWidget *widget)
166 {
167   GtkDrawingArea *darea = GTK_DRAWING_AREA (widget);
168   GtkAllocation allocation;
169   GdkWindow *window;
170   GdkWindowAttr attributes;
171   gint attributes_mask;
172
173   if (!gtk_widget_get_has_window (widget))
174     {
175       GTK_WIDGET_CLASS (gtk_drawing_area_parent_class)->realize (widget);
176     }
177   else
178     {
179       gtk_widget_set_realized (widget, TRUE);
180
181       gtk_widget_get_allocation (widget, &allocation);
182
183       attributes.window_type = GDK_WINDOW_CHILD;
184       attributes.x = allocation.x;
185       attributes.y = allocation.y;
186       attributes.width = allocation.width;
187       attributes.height = allocation.height;
188       attributes.wclass = GDK_INPUT_OUTPUT;
189       attributes.visual = gtk_widget_get_visual (widget);
190       attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
191
192       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
193
194       window = gdk_window_new (gtk_widget_get_parent_window (widget),
195                                &attributes, attributes_mask);
196       gdk_window_set_user_data (window, darea);
197       gtk_widget_set_window (widget, window);
198
199       gtk_style_context_set_background (gtk_widget_get_style_context (widget),
200                                         window);
201     }
202
203   gtk_drawing_area_send_configure (GTK_DRAWING_AREA (widget));
204 }
205
206 static void
207 gtk_drawing_area_size_allocate (GtkWidget     *widget,
208                                 GtkAllocation *allocation)
209 {
210   g_return_if_fail (GTK_IS_DRAWING_AREA (widget));
211   g_return_if_fail (allocation != NULL);
212
213   gtk_widget_set_allocation (widget, allocation);
214
215   if (gtk_widget_get_realized (widget))
216     {
217       if (gtk_widget_get_has_window (widget))
218         gdk_window_move_resize (gtk_widget_get_window (widget),
219                                 allocation->x, allocation->y,
220                                 allocation->width, allocation->height);
221
222       gtk_drawing_area_send_configure (GTK_DRAWING_AREA (widget));
223     }
224 }
225
226 static void
227 gtk_drawing_area_send_configure (GtkDrawingArea *darea)
228 {
229   GtkAllocation allocation;
230   GtkWidget *widget;
231   GdkEvent *event = gdk_event_new (GDK_CONFIGURE);
232
233   widget = GTK_WIDGET (darea);
234   gtk_widget_get_allocation (widget, &allocation);
235
236   event->configure.window = g_object_ref (gtk_widget_get_window (widget));
237   event->configure.send_event = TRUE;
238   event->configure.x = allocation.x;
239   event->configure.y = allocation.y;
240   event->configure.width = allocation.width;
241   event->configure.height = allocation.height;
242
243   gtk_widget_event (widget, event);
244   gdk_event_free (event);
245 }