Skip to main content

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