gtk/auto/drawing_area.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Align, Buildable, Container, Widget};
6use glib::{prelude::*, translate::*};
7use std::fmt;
8
9glib::wrapper! {
10 /// The [`DrawingArea`][crate::DrawingArea] widget is used for creating custom user interface
11 /// elements. It’s essentially a blank widget; you can draw on it. After
12 /// creating a drawing area, the application may want to connect to:
13 ///
14 /// - Mouse and button press signals to respond to input from
15 /// the user. (Use [`WidgetExtManual::add_events()`][crate::prelude::WidgetExtManual::add_events()] to enable events
16 /// you wish to receive.)
17 ///
18 /// - The [`realize`][struct@crate::Widget#realize] signal to take any necessary actions
19 /// when the widget is instantiated on a particular display.
20 /// (Create GDK resources in response to this signal.)
21 ///
22 /// - The [`size-allocate`][struct@crate::Widget#size-allocate] signal to take any necessary
23 /// actions when the widget changes size.
24 ///
25 /// - The [`draw`][struct@crate::Widget#draw] signal to handle redrawing the
26 /// contents of the widget.
27 ///
28 /// The following code portion demonstrates using a drawing
29 /// area to display a circle in the normal widget foreground
30 /// color.
31 ///
32 /// Note that GDK automatically clears the exposed area before sending
33 /// the expose event, and that drawing is implicitly clipped to the exposed
34 /// area. If you want to have a theme-provided background, you need
35 /// to call [`render_background()`][crate::render_background()] in your ::draw method.
36 ///
37 /// ## Simple GtkDrawingArea usage
38 ///
39 ///
40 ///
41 /// **⚠️ The following code is in C ⚠️**
42 ///
43 /// ```C
44 /// gboolean
45 /// draw_callback (GtkWidget *widget, cairo_t *cr, gpointer data)
46 /// {
47 /// guint width, height;
48 /// GdkRGBA color;
49 /// GtkStyleContext *context;
50 ///
51 /// context = gtk_widget_get_style_context (widget);
52 ///
53 /// width = gtk_widget_get_allocated_width (widget);
54 /// height = gtk_widget_get_allocated_height (widget);
55 ///
56 /// gtk_render_background (context, cr, 0, 0, width, height);
57 ///
58 /// cairo_arc (cr,
59 /// width / 2.0, height / 2.0,
60 /// MIN (width, height) / 2.0,
61 /// 0, 2 * G_PI);
62 ///
63 /// gtk_style_context_get_color (context,
64 /// gtk_style_context_get_state (context),
65 /// &color);
66 /// gdk_cairo_set_source_rgba (cr, &color);
67 ///
68 /// cairo_fill (cr);
69 ///
70 /// return FALSE;
71 /// }
72 /// [...]
73 /// GtkWidget *drawing_area = gtk_drawing_area_new ();
74 /// gtk_widget_set_size_request (drawing_area, 100, 100);
75 /// g_signal_connect (G_OBJECT (drawing_area), "draw",
76 /// G_CALLBACK (draw_callback), NULL);
77 /// ```
78 ///
79 /// Draw signals are normally delivered when a drawing area first comes
80 /// onscreen, or when it’s covered by another window and then uncovered.
81 /// You can also force an expose event by adding to the “damage region”
82 /// of the drawing area’s window; [`WidgetExt::queue_draw_area()`][crate::prelude::WidgetExt::queue_draw_area()] and
83 /// [`Window::invalidate_rect()`][crate::gdk::Window::invalidate_rect()] are equally good ways to do this.
84 /// You’ll then get a draw signal for the invalid region.
85 ///
86 /// The available routines for drawing are documented on the
87 /// [GDK Drawing Primitives][gdk3-Cairo-Interaction] page
88 /// and the cairo documentation.
89 ///
90 /// To receive mouse events on a drawing area, you will need to enable
91 /// them with [`WidgetExtManual::add_events()`][crate::prelude::WidgetExtManual::add_events()]. To receive keyboard events, you
92 /// will need to set the “can-focus” property on the drawing area, and you
93 /// should probably draw some user-visible indication that the drawing
94 /// area is focused. Use [`WidgetExt::has_focus()`][crate::prelude::WidgetExt::has_focus()] in your expose event
95 /// handler to decide whether to draw the focus indicator. See
96 /// [`render_focus()`][crate::render_focus()] for one way to draw focus.
97 ///
98 /// # Implements
99 ///
100 /// [`WidgetExt`][trait@crate::prelude::WidgetExt], [`trait@glib::ObjectExt`], [`BuildableExt`][trait@crate::prelude::BuildableExt], [`WidgetExtManual`][trait@crate::prelude::WidgetExtManual], [`BuildableExtManual`][trait@crate::prelude::BuildableExtManual]
101 #[doc(alias = "GtkDrawingArea")]
102 pub struct DrawingArea(Object<ffi::GtkDrawingArea, ffi::GtkDrawingAreaClass>) @extends Widget, @implements Buildable;
103
104 match fn {
105 type_ => || ffi::gtk_drawing_area_get_type(),
106 }
107}
108
109impl DrawingArea {
110 pub const NONE: Option<&'static DrawingArea> = None;
111
112 /// Creates a new drawing area.
113 ///
114 /// # Returns
115 ///
116 /// a new [`DrawingArea`][crate::DrawingArea]
117 #[doc(alias = "gtk_drawing_area_new")]
118 pub fn new() -> DrawingArea {
119 assert_initialized_main_thread!();
120 unsafe { Widget::from_glib_none(ffi::gtk_drawing_area_new()).unsafe_cast() }
121 }
122
123 // rustdoc-stripper-ignore-next
124 /// Creates a new builder-pattern struct instance to construct [`DrawingArea`] objects.
125 ///
126 /// This method returns an instance of [`DrawingAreaBuilder`](crate::builders::DrawingAreaBuilder) which can be used to create [`DrawingArea`] objects.
127 pub fn builder() -> DrawingAreaBuilder {
128 DrawingAreaBuilder::new()
129 }
130}
131
132impl Default for DrawingArea {
133 fn default() -> Self {
134 Self::new()
135 }
136}
137
138// rustdoc-stripper-ignore-next
139/// A [builder-pattern] type to construct [`DrawingArea`] objects.
140///
141/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
142#[must_use = "The builder must be built to be used"]
143pub struct DrawingAreaBuilder {
144 builder: glib::object::ObjectBuilder<'static, DrawingArea>,
145}
146
147impl DrawingAreaBuilder {
148 fn new() -> Self {
149 Self {
150 builder: glib::object::Object::builder(),
151 }
152 }
153
154 pub fn app_paintable(self, app_paintable: bool) -> Self {
155 Self {
156 builder: self.builder.property("app-paintable", app_paintable),
157 }
158 }
159
160 pub fn can_default(self, can_default: bool) -> Self {
161 Self {
162 builder: self.builder.property("can-default", can_default),
163 }
164 }
165
166 pub fn can_focus(self, can_focus: bool) -> Self {
167 Self {
168 builder: self.builder.property("can-focus", can_focus),
169 }
170 }
171
172 pub fn events(self, events: gdk::EventMask) -> Self {
173 Self {
174 builder: self.builder.property("events", events),
175 }
176 }
177
178 /// Whether to expand in both directions. Setting this sets both [`hexpand`][struct@crate::Widget#hexpand] and [`vexpand`][struct@crate::Widget#vexpand]
179 pub fn expand(self, expand: bool) -> Self {
180 Self {
181 builder: self.builder.property("expand", expand),
182 }
183 }
184
185 /// Whether the widget should grab focus when it is clicked with the mouse.
186 ///
187 /// This property is only relevant for widgets that can take focus.
188 ///
189 /// Before 3.20, several widgets (GtkButton, GtkFileChooserButton,
190 /// GtkComboBox) implemented this property individually.
191 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
192 Self {
193 builder: self.builder.property("focus-on-click", focus_on_click),
194 }
195 }
196
197 /// How to distribute horizontal space if widget gets extra space, see [`Align`][crate::Align]
198 pub fn halign(self, halign: Align) -> Self {
199 Self {
200 builder: self.builder.property("halign", halign),
201 }
202 }
203
204 pub fn has_default(self, has_default: bool) -> Self {
205 Self {
206 builder: self.builder.property("has-default", has_default),
207 }
208 }
209
210 pub fn has_focus(self, has_focus: bool) -> Self {
211 Self {
212 builder: self.builder.property("has-focus", has_focus),
213 }
214 }
215
216 /// Enables or disables the emission of [`query-tooltip`][struct@crate::Widget#query-tooltip] on `widget`.
217 /// A value of [`true`] indicates that `widget` can have a tooltip, in this case
218 /// the widget will be queried using [`query-tooltip`][struct@crate::Widget#query-tooltip] to determine
219 /// whether it will provide a tooltip or not.
220 ///
221 /// Note that setting this property to [`true`] for the first time will change
222 /// the event masks of the GdkWindows of this widget to include leave-notify
223 /// and motion-notify events. This cannot and will not be undone when the
224 /// property is set to [`false`] again.
225 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
226 Self {
227 builder: self.builder.property("has-tooltip", has_tooltip),
228 }
229 }
230
231 pub fn height_request(self, height_request: i32) -> Self {
232 Self {
233 builder: self.builder.property("height-request", height_request),
234 }
235 }
236
237 /// Whether to expand horizontally. See [`WidgetExt::set_hexpand()`][crate::prelude::WidgetExt::set_hexpand()].
238 pub fn hexpand(self, hexpand: bool) -> Self {
239 Self {
240 builder: self.builder.property("hexpand", hexpand),
241 }
242 }
243
244 /// Whether to use the [`hexpand`][struct@crate::Widget#hexpand] property. See [`WidgetExt::is_hexpand_set()`][crate::prelude::WidgetExt::is_hexpand_set()].
245 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
246 Self {
247 builder: self.builder.property("hexpand-set", hexpand_set),
248 }
249 }
250
251 pub fn is_focus(self, is_focus: bool) -> Self {
252 Self {
253 builder: self.builder.property("is-focus", is_focus),
254 }
255 }
256
257 /// Sets all four sides' margin at once. If read, returns max
258 /// margin on any side.
259 pub fn margin(self, margin: i32) -> Self {
260 Self {
261 builder: self.builder.property("margin", margin),
262 }
263 }
264
265 /// Margin on bottom side of widget.
266 ///
267 /// This property adds margin outside of the widget's normal size
268 /// request, the margin will be added in addition to the size from
269 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
270 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
271 Self {
272 builder: self.builder.property("margin-bottom", margin_bottom),
273 }
274 }
275
276 /// Margin on end of widget, horizontally. This property supports
277 /// left-to-right and right-to-left text directions.
278 ///
279 /// This property adds margin outside of the widget's normal size
280 /// request, the margin will be added in addition to the size from
281 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
282 pub fn margin_end(self, margin_end: i32) -> Self {
283 Self {
284 builder: self.builder.property("margin-end", margin_end),
285 }
286 }
287
288 /// Margin on start of widget, horizontally. This property supports
289 /// left-to-right and right-to-left text directions.
290 ///
291 /// This property adds margin outside of the widget's normal size
292 /// request, the margin will be added in addition to the size from
293 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
294 pub fn margin_start(self, margin_start: i32) -> Self {
295 Self {
296 builder: self.builder.property("margin-start", margin_start),
297 }
298 }
299
300 /// Margin on top side of widget.
301 ///
302 /// This property adds margin outside of the widget's normal size
303 /// request, the margin will be added in addition to the size from
304 /// [`WidgetExt::set_size_request()`][crate::prelude::WidgetExt::set_size_request()] for example.
305 pub fn margin_top(self, margin_top: i32) -> Self {
306 Self {
307 builder: self.builder.property("margin-top", margin_top),
308 }
309 }
310
311 pub fn name(self, name: impl Into<glib::GString>) -> Self {
312 Self {
313 builder: self.builder.property("name", name.into()),
314 }
315 }
316
317 pub fn no_show_all(self, no_show_all: bool) -> Self {
318 Self {
319 builder: self.builder.property("no-show-all", no_show_all),
320 }
321 }
322
323 /// The requested opacity of the widget. See [`WidgetExt::set_opacity()`][crate::prelude::WidgetExt::set_opacity()] for
324 /// more details about window opacity.
325 ///
326 /// Before 3.8 this was only available in GtkWindow
327 pub fn opacity(self, opacity: f64) -> Self {
328 Self {
329 builder: self.builder.property("opacity", opacity),
330 }
331 }
332
333 pub fn parent(self, parent: &impl IsA<Container>) -> Self {
334 Self {
335 builder: self.builder.property("parent", parent.clone().upcast()),
336 }
337 }
338
339 pub fn receives_default(self, receives_default: bool) -> Self {
340 Self {
341 builder: self.builder.property("receives-default", receives_default),
342 }
343 }
344
345 pub fn sensitive(self, sensitive: bool) -> Self {
346 Self {
347 builder: self.builder.property("sensitive", sensitive),
348 }
349 }
350
351 /// Sets the text of tooltip to be the given string, which is marked up
352 /// with the [Pango text markup language][PangoMarkupFormat].
353 /// Also see [`Tooltip::set_markup()`][crate::Tooltip::set_markup()].
354 ///
355 /// This is a convenience property which will take care of getting the
356 /// tooltip shown if the given string is not [`None`]: [`has-tooltip`][struct@crate::Widget#has-tooltip]
357 /// will automatically be set to [`true`] and there will be taken care of
358 /// [`query-tooltip`][struct@crate::Widget#query-tooltip] in the default signal handler.
359 ///
360 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and [`tooltip-markup`][struct@crate::Widget#tooltip-markup]
361 /// are set, the last one wins.
362 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
363 Self {
364 builder: self
365 .builder
366 .property("tooltip-markup", tooltip_markup.into()),
367 }
368 }
369
370 /// Sets the text of tooltip to be the given string.
371 ///
372 /// Also see [`Tooltip::set_text()`][crate::Tooltip::set_text()].
373 ///
374 /// This is a convenience property which will take care of getting the
375 /// tooltip shown if the given string is not [`None`]: [`has-tooltip`][struct@crate::Widget#has-tooltip]
376 /// will automatically be set to [`true`] and there will be taken care of
377 /// [`query-tooltip`][struct@crate::Widget#query-tooltip] in the default signal handler.
378 ///
379 /// Note that if both [`tooltip-text`][struct@crate::Widget#tooltip-text] and [`tooltip-markup`][struct@crate::Widget#tooltip-markup]
380 /// are set, the last one wins.
381 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
382 Self {
383 builder: self.builder.property("tooltip-text", tooltip_text.into()),
384 }
385 }
386
387 /// How to distribute vertical space if widget gets extra space, see [`Align`][crate::Align]
388 pub fn valign(self, valign: Align) -> Self {
389 Self {
390 builder: self.builder.property("valign", valign),
391 }
392 }
393
394 /// Whether to expand vertically. See [`WidgetExt::set_vexpand()`][crate::prelude::WidgetExt::set_vexpand()].
395 pub fn vexpand(self, vexpand: bool) -> Self {
396 Self {
397 builder: self.builder.property("vexpand", vexpand),
398 }
399 }
400
401 /// Whether to use the [`vexpand`][struct@crate::Widget#vexpand] property. See [`WidgetExt::is_vexpand_set()`][crate::prelude::WidgetExt::is_vexpand_set()].
402 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
403 Self {
404 builder: self.builder.property("vexpand-set", vexpand_set),
405 }
406 }
407
408 pub fn visible(self, visible: bool) -> Self {
409 Self {
410 builder: self.builder.property("visible", visible),
411 }
412 }
413
414 pub fn width_request(self, width_request: i32) -> Self {
415 Self {
416 builder: self.builder.property("width-request", width_request),
417 }
418 }
419
420 // rustdoc-stripper-ignore-next
421 /// Build the [`DrawingArea`].
422 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
423 pub fn build(self) -> DrawingArea {
424 self.builder.build()
425 }
426}
427
428impl fmt::Display for DrawingArea {
429 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
430 f.write_str("DrawingArea")
431 }
432}