1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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/>.
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/.
26 #include "gtkdrawingarea.h"
28 #include "gtkstylecontext.h"
32 * SECTION:gtkdrawingarea
33 * @Short_description: A widget for custom user interface elements
34 * @Title: GtkDrawingArea
35 * @See_also: #GtkImage
37 * The #GtkDrawingArea widget is used for creating custom user interface
38 * elements. It's essentially a blank widget; you can draw on it. After
39 * creating a drawing area, the application may want to connect to:
44 * Mouse and button press signals to respond to input from
45 * the user. (Use gtk_widget_add_events() to enable events
46 * you wish to receive.)
51 * The #GtkWidget::realize signal to take any necessary actions
52 * when the widget is instantiated on a particular display.
53 * (Create GDK resources in response to this signal.)
58 * The #GtkWidget::configure-event signal to take any necessary
59 * actions when the widget changes size.
64 * The #GtkWidget::draw signal to handle redrawing the
65 * contents of the widget.
70 * The following code portion demonstrates using a drawing
71 * area to display a circle in the normal widget foreground
74 * Note that GDK automatically clears the exposed area to the
75 * background color before sending the expose event, and that
76 * drawing is implicitly clipped to the exposed area.
79 * <title>Simple GtkDrawingArea usage</title>
82 * draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
84 * guint width, height;
87 * width = gtk_widget_get_allocated_width (widget);
88 * height = gtk_widget_get_allocated_height (widget);
90 * width / 2.0, height / 2.0,
91 * MIN (width, height) / 2.0,
94 * gtk_style_context_get_color (gtk_widget_get_style_context (widget),
97 * gdk_cairo_set_source_rgba (cr, &color);
104 * GtkWidget *drawing_area = gtk_drawing_area_new (<!-- -->);
105 * gtk_widget_set_size_request (drawing_area, 100, 100);
106 * g_signal_connect (G_OBJECT (drawing_area), "draw",
107 * G_CALLBACK (draw_callback), NULL);
111 * Draw signals are normally delivered when a drawing area first comes
112 * onscreen, or when it's covered by another window and then uncovered.
113 * You can also force an expose event by adding to the "damage region"
114 * of the drawing area's window; gtk_widget_queue_draw_area() and
115 * gdk_window_invalidate_rect() are equally good ways to do this.
116 * You'll then get a draw signal for the invalid region.
118 * The available routines for drawing are documented on the <link
119 * linkend="gdk3-Cairo-Interaction">GDK Drawing Primitives</link> page
120 * and the cairo documentation.
122 * To receive mouse events on a drawing area, you will need to enable
123 * them with gtk_widget_add_events(). To receive keyboard events, you
124 * will need to set the "can-focus" property on the drawing area, and you
125 * should probably draw some user-visible indication that the drawing
126 * area is focused. Use gtk_widget_has_focus() in your expose event
127 * handler to decide whether to draw the focus indicator. See
128 * gtk_render_focus() for one way to draw focus.
131 static void gtk_drawing_area_realize (GtkWidget *widget);
132 static void gtk_drawing_area_size_allocate (GtkWidget *widget,
133 GtkAllocation *allocation);
134 static void gtk_drawing_area_send_configure (GtkDrawingArea *darea);
136 G_DEFINE_TYPE (GtkDrawingArea, gtk_drawing_area, GTK_TYPE_WIDGET)
139 gtk_drawing_area_class_init (GtkDrawingAreaClass *class)
141 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
143 widget_class->realize = gtk_drawing_area_realize;
144 widget_class->size_allocate = gtk_drawing_area_size_allocate;
148 gtk_drawing_area_init (GtkDrawingArea *darea)
153 * gtk_drawing_area_new:
155 * Creates a new drawing area.
157 * Returns: a new #GtkDrawingArea
160 gtk_drawing_area_new (void)
162 return g_object_new (GTK_TYPE_DRAWING_AREA, NULL);
166 gtk_drawing_area_realize (GtkWidget *widget)
168 GtkDrawingArea *darea = GTK_DRAWING_AREA (widget);
169 GtkAllocation allocation;
171 GdkWindowAttr attributes;
172 gint attributes_mask;
174 if (!gtk_widget_get_has_window (widget))
176 GTK_WIDGET_CLASS (gtk_drawing_area_parent_class)->realize (widget);
180 gtk_widget_set_realized (widget, TRUE);
182 gtk_widget_get_allocation (widget, &allocation);
184 attributes.window_type = GDK_WINDOW_CHILD;
185 attributes.x = allocation.x;
186 attributes.y = allocation.y;
187 attributes.width = allocation.width;
188 attributes.height = allocation.height;
189 attributes.wclass = GDK_INPUT_OUTPUT;
190 attributes.visual = gtk_widget_get_visual (widget);
191 attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
193 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
195 window = gdk_window_new (gtk_widget_get_parent_window (widget),
196 &attributes, attributes_mask);
197 gdk_window_set_user_data (window, darea);
198 gtk_widget_set_window (widget, window);
200 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
204 gtk_drawing_area_send_configure (GTK_DRAWING_AREA (widget));
208 gtk_drawing_area_size_allocate (GtkWidget *widget,
209 GtkAllocation *allocation)
211 g_return_if_fail (GTK_IS_DRAWING_AREA (widget));
212 g_return_if_fail (allocation != NULL);
214 gtk_widget_set_allocation (widget, allocation);
216 if (gtk_widget_get_realized (widget))
218 if (gtk_widget_get_has_window (widget))
219 gdk_window_move_resize (gtk_widget_get_window (widget),
220 allocation->x, allocation->y,
221 allocation->width, allocation->height);
223 gtk_drawing_area_send_configure (GTK_DRAWING_AREA (widget));
228 gtk_drawing_area_send_configure (GtkDrawingArea *darea)
230 GtkAllocation allocation;
232 GdkEvent *event = gdk_event_new (GDK_CONFIGURE);
234 widget = GTK_WIDGET (darea);
235 gtk_widget_get_allocation (widget, &allocation);
237 event->configure.window = g_object_ref (gtk_widget_get_window (widget));
238 event->configure.send_event = TRUE;
239 event->configure.x = allocation.x;
240 event->configure.y = allocation.y;
241 event->configure.width = allocation.width;
242 event->configure.height = allocation.height;
244 gtk_widget_event (widget, event);
245 gdk_event_free (event);