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