1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gtk.DrawingArea;
26 
27 private import glib.ConstructionException;
28 private import gobject.ObjectG;
29 private import gobject.Signals;
30 private import gtk.Widget;
31 private import gtk.c.functions;
32 public  import gtk.c.types;
33 private import std.algorithm;
34 
35 
36 /**
37  * `GtkDrawingArea` is a widget that allows drawing with cairo.
38  * 
39  * ![An example GtkDrawingArea](drawingarea.png)
40  * 
41  * It’s essentially a blank widget; you can draw on it. After
42  * creating a drawing area, the application may want to connect to:
43  * 
44  * - The [signal@Gtk.Widget::realize] signal to take any necessary actions
45  * when the widget is instantiated on a particular display.
46  * (Create GDK resources in response to this signal.)
47  * 
48  * - The [signal@Gtk.DrawingArea::resize] signal to take any necessary
49  * actions when the widget changes size.
50  * 
51  * - Call [method@Gtk.DrawingArea.set_draw_func] to handle redrawing the
52  * contents of the widget.
53  * 
54  * The following code portion demonstrates using a drawing
55  * area to display a circle in the normal widget foreground
56  * color.
57  * 
58  * ## Simple GtkDrawingArea usage
59  * 
60  * ```c
61  * static void
62  * draw_function (GtkDrawingArea *area,
63  * cairo_t        *cr,
64  * int             width,
65  * int             height,
66  * gpointer        data)
67  * {
68  * GdkRGBA color;
69  * GtkStyleContext *context;
70  * 
71  * context = gtk_widget_get_style_context (GTK_WIDGET (area));
72  * 
73  * cairo_arc (cr,
74  * width / 2.0, height / 2.0,
75  * MIN (width, height) / 2.0,
76  * 0, 2 * G_PI);
77  * 
78  * gtk_style_context_get_color (context,
79  * &color);
80  * gdk_cairo_set_source_rgba (cr, &color);
81  * 
82  * cairo_fill (cr);
83  * }
84  * 
85  * int
86  * main (int argc, char **argv)
87  * {
88  * gtk_init ();
89  * 
90  * GtkWidget *area = gtk_drawing_area_new ();
91  * gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (area), 100);
92  * gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (area), 100);
93  * gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area),
94  * draw_function,
95  * NULL, NULL);
96  * return 0;
97  * }
98  * ```
99  * 
100  * The draw function is normally called when a drawing area first comes
101  * onscreen, or when it’s covered by another window and then uncovered.
102  * You can also force a redraw by adding to the “damage region” of the
103  * drawing area’s window using [method@Gtk.Widget.queue_draw].
104  * This will cause the drawing area to call the draw function again.
105  * 
106  * The available routines for drawing are documented on the
107  * [GDK Drawing Primitives][gdk4-Cairo-Interaction] page
108  * and the cairo documentation.
109  * 
110  * To receive mouse events on a drawing area, you will need to use
111  * event controllers. To receive keyboard events, you will need to set
112  * the “can-focus” property on the drawing area, and you should probably
113  * draw some user-visible indication that the drawing area is focused.
114  * 
115  * If you need more complex control over your widget, you should consider
116  * creating your own `GtkWidget` subclass.
117  */
118 public class DrawingArea : Widget
119 {
120 	/** the main Gtk struct */
121 	protected GtkDrawingArea* gtkDrawingArea;
122 
123 	/** Get the main Gtk struct */
124 	public GtkDrawingArea* getDrawingAreaStruct(bool transferOwnership = false)
125 	{
126 		if (transferOwnership)
127 			ownedRef = false;
128 		return gtkDrawingArea;
129 	}
130 
131 	/** the main Gtk struct as a void* */
132 	protected override void* getStruct()
133 	{
134 		return cast(void*)gtkDrawingArea;
135 	}
136 
137 	/**
138 	 * Sets our main struct and passes it to the parent class.
139 	 */
140 	public this (GtkDrawingArea* gtkDrawingArea, bool ownedRef = false)
141 	{
142 		this.gtkDrawingArea = gtkDrawingArea;
143 		super(cast(GtkWidget*)gtkDrawingArea, ownedRef);
144 	}
145 
146 
147 	/** */
148 	public static GType getType()
149 	{
150 		return gtk_drawing_area_get_type();
151 	}
152 
153 	/**
154 	 * Creates a new drawing area.
155 	 *
156 	 * Returns: a new `GtkDrawingArea`
157 	 *
158 	 * Throws: ConstructionException GTK+ fails to create the object.
159 	 */
160 	public this()
161 	{
162 		auto __p = gtk_drawing_area_new();
163 
164 		if(__p is null)
165 		{
166 			throw new ConstructionException("null returned by new");
167 		}
168 
169 		this(cast(GtkDrawingArea*) __p);
170 	}
171 
172 	/**
173 	 * Retrieves the content height of the `GtkDrawingArea`.
174 	 *
175 	 * Returns: The height requested for content of the drawing area
176 	 */
177 	public int getContentHeight()
178 	{
179 		return gtk_drawing_area_get_content_height(gtkDrawingArea);
180 	}
181 
182 	/**
183 	 * Retrieves the content width of the `GtkDrawingArea`.
184 	 *
185 	 * Returns: The width requested for content of the drawing area
186 	 */
187 	public int getContentWidth()
188 	{
189 		return gtk_drawing_area_get_content_width(gtkDrawingArea);
190 	}
191 
192 	/**
193 	 * Sets the desired height of the contents of the drawing area.
194 	 *
195 	 * Note that because widgets may be allocated larger sizes than they
196 	 * requested, it is possible that the actual height passed to your draw
197 	 * function is larger than the height set here. You can use
198 	 * [method@Gtk.Widget.set_valign] to avoid that.
199 	 *
200 	 * If the height is set to 0 (the default), the drawing area may disappear.
201 	 *
202 	 * Params:
203 	 *     height = the height of contents
204 	 */
205 	public void setContentHeight(int height)
206 	{
207 		gtk_drawing_area_set_content_height(gtkDrawingArea, height);
208 	}
209 
210 	/**
211 	 * Sets the desired width of the contents of the drawing area.
212 	 *
213 	 * Note that because widgets may be allocated larger sizes than they
214 	 * requested, it is possible that the actual width passed to your draw
215 	 * function is larger than the width set here. You can use
216 	 * [method@Gtk.Widget.set_halign] to avoid that.
217 	 *
218 	 * If the width is set to 0 (the default), the drawing area may disappear.
219 	 *
220 	 * Params:
221 	 *     width = the width of contents
222 	 */
223 	public void setContentWidth(int width)
224 	{
225 		gtk_drawing_area_set_content_width(gtkDrawingArea, width);
226 	}
227 
228 	/**
229 	 * Setting a draw function is the main thing you want to do when using
230 	 * a drawing area.
231 	 *
232 	 * The draw function is called whenever GTK needs to draw the contents
233 	 * of the drawing area to the screen.
234 	 *
235 	 * The draw function will be called during the drawing stage of GTK.
236 	 * In the drawing stage it is not allowed to change properties of any
237 	 * GTK widgets or call any functions that would cause any properties
238 	 * to be changed. You should restrict yourself exclusively to drawing
239 	 * your contents in the draw function.
240 	 *
241 	 * If what you are drawing does change, call [method@Gtk.Widget.queue_draw]
242 	 * on the drawing area. This will cause a redraw and will call @draw_func again.
243 	 *
244 	 * Params:
245 	 *     drawFunc = callback that lets you draw
246 	 *         the drawing area's contents
247 	 *     userData = user data passed to @draw_func
248 	 *     destroy = destroy notifier for @user_data
249 	 */
250 	public void setDrawFunc(GtkDrawingAreaDrawFunc drawFunc, void* userData, GDestroyNotify destroy)
251 	{
252 		gtk_drawing_area_set_draw_func(gtkDrawingArea, drawFunc, userData, destroy);
253 	}
254 
255 	/**
256 	 * Emitted once when the widget is realized, and then each time the widget
257 	 * is changed while realized.
258 	 *
259 	 * This is useful in order to keep state up to date with the widget size,
260 	 * like for instance a backing surface.
261 	 *
262 	 * Params:
263 	 *     width = the width of the viewport
264 	 *     height = the height of the viewport
265 	 */
266 	gulong addOnResize(void delegate(int, int, DrawingArea) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
267 	{
268 		return Signals.connect(this, "resize", dlg, connectFlags ^ ConnectFlags.SWAPPED);
269 	}
270 }