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