1use crate::{
6 ffi, Accessible, AccessibleRole, Align, Buildable, ConstraintTarget, LayoutManager, Overflow,
7 Widget,
8};
9use glib::{
10 object::ObjectType as _,
11 prelude::*,
12 signal::{connect_raw, SignalHandlerId},
13 translate::*,
14};
15use std::boxed::Box as Box_;
16
17glib::wrapper! {
18 #[doc(alias = "GtkDrawingArea")]
19 pub struct DrawingArea(Object<ffi::GtkDrawingArea, ffi::GtkDrawingAreaClass>) @extends Widget, @implements Accessible, Buildable, ConstraintTarget;
20
21 match fn {
22 type_ => || ffi::gtk_drawing_area_get_type(),
23 }
24}
25
26impl DrawingArea {
27 pub const NONE: Option<&'static DrawingArea> = None;
28
29 #[doc(alias = "gtk_drawing_area_new")]
30 pub fn new() -> DrawingArea {
31 assert_initialized_main_thread!();
32 unsafe { Widget::from_glib_none(ffi::gtk_drawing_area_new()).unsafe_cast() }
33 }
34
35 pub fn builder() -> DrawingAreaBuilder {
40 DrawingAreaBuilder::new()
41 }
42}
43
44impl Default for DrawingArea {
45 fn default() -> Self {
46 Self::new()
47 }
48}
49
50#[must_use = "The builder must be built to be used"]
55pub struct DrawingAreaBuilder {
56 builder: glib::object::ObjectBuilder<'static, DrawingArea>,
57}
58
59impl DrawingAreaBuilder {
60 fn new() -> Self {
61 Self {
62 builder: glib::object::Object::builder(),
63 }
64 }
65
66 pub fn content_height(self, content_height: i32) -> Self {
67 Self {
68 builder: self.builder.property("content-height", content_height),
69 }
70 }
71
72 pub fn content_width(self, content_width: i32) -> Self {
73 Self {
74 builder: self.builder.property("content-width", content_width),
75 }
76 }
77
78 pub fn can_focus(self, can_focus: bool) -> Self {
79 Self {
80 builder: self.builder.property("can-focus", can_focus),
81 }
82 }
83
84 pub fn can_target(self, can_target: bool) -> Self {
85 Self {
86 builder: self.builder.property("can-target", can_target),
87 }
88 }
89
90 pub fn css_classes(self, css_classes: impl Into<glib::StrV>) -> Self {
91 Self {
92 builder: self.builder.property("css-classes", css_classes.into()),
93 }
94 }
95
96 pub fn css_name(self, css_name: impl Into<glib::GString>) -> Self {
97 Self {
98 builder: self.builder.property("css-name", css_name.into()),
99 }
100 }
101
102 pub fn cursor(self, cursor: &gdk::Cursor) -> Self {
103 Self {
104 builder: self.builder.property("cursor", cursor.clone()),
105 }
106 }
107
108 pub fn focus_on_click(self, focus_on_click: bool) -> Self {
109 Self {
110 builder: self.builder.property("focus-on-click", focus_on_click),
111 }
112 }
113
114 pub fn focusable(self, focusable: bool) -> Self {
115 Self {
116 builder: self.builder.property("focusable", focusable),
117 }
118 }
119
120 pub fn halign(self, halign: Align) -> Self {
121 Self {
122 builder: self.builder.property("halign", halign),
123 }
124 }
125
126 pub fn has_tooltip(self, has_tooltip: bool) -> Self {
127 Self {
128 builder: self.builder.property("has-tooltip", has_tooltip),
129 }
130 }
131
132 pub fn height_request(self, height_request: i32) -> Self {
133 Self {
134 builder: self.builder.property("height-request", height_request),
135 }
136 }
137
138 pub fn hexpand(self, hexpand: bool) -> Self {
139 Self {
140 builder: self.builder.property("hexpand", hexpand),
141 }
142 }
143
144 pub fn hexpand_set(self, hexpand_set: bool) -> Self {
145 Self {
146 builder: self.builder.property("hexpand-set", hexpand_set),
147 }
148 }
149
150 pub fn layout_manager(self, layout_manager: &impl IsA<LayoutManager>) -> Self {
151 Self {
152 builder: self
153 .builder
154 .property("layout-manager", layout_manager.clone().upcast()),
155 }
156 }
157
158 #[cfg(feature = "v4_18")]
159 #[cfg_attr(docsrs, doc(cfg(feature = "v4_18")))]
160 pub fn limit_events(self, limit_events: bool) -> Self {
161 Self {
162 builder: self.builder.property("limit-events", limit_events),
163 }
164 }
165
166 pub fn margin_bottom(self, margin_bottom: i32) -> Self {
167 Self {
168 builder: self.builder.property("margin-bottom", margin_bottom),
169 }
170 }
171
172 pub fn margin_end(self, margin_end: i32) -> Self {
173 Self {
174 builder: self.builder.property("margin-end", margin_end),
175 }
176 }
177
178 pub fn margin_start(self, margin_start: i32) -> Self {
179 Self {
180 builder: self.builder.property("margin-start", margin_start),
181 }
182 }
183
184 pub fn margin_top(self, margin_top: i32) -> Self {
185 Self {
186 builder: self.builder.property("margin-top", margin_top),
187 }
188 }
189
190 pub fn name(self, name: impl Into<glib::GString>) -> Self {
191 Self {
192 builder: self.builder.property("name", name.into()),
193 }
194 }
195
196 pub fn opacity(self, opacity: f64) -> Self {
197 Self {
198 builder: self.builder.property("opacity", opacity),
199 }
200 }
201
202 pub fn overflow(self, overflow: Overflow) -> Self {
203 Self {
204 builder: self.builder.property("overflow", overflow),
205 }
206 }
207
208 pub fn receives_default(self, receives_default: bool) -> Self {
209 Self {
210 builder: self.builder.property("receives-default", receives_default),
211 }
212 }
213
214 pub fn sensitive(self, sensitive: bool) -> Self {
215 Self {
216 builder: self.builder.property("sensitive", sensitive),
217 }
218 }
219
220 pub fn tooltip_markup(self, tooltip_markup: impl Into<glib::GString>) -> Self {
221 Self {
222 builder: self
223 .builder
224 .property("tooltip-markup", tooltip_markup.into()),
225 }
226 }
227
228 pub fn tooltip_text(self, tooltip_text: impl Into<glib::GString>) -> Self {
229 Self {
230 builder: self.builder.property("tooltip-text", tooltip_text.into()),
231 }
232 }
233
234 pub fn valign(self, valign: Align) -> Self {
235 Self {
236 builder: self.builder.property("valign", valign),
237 }
238 }
239
240 pub fn vexpand(self, vexpand: bool) -> Self {
241 Self {
242 builder: self.builder.property("vexpand", vexpand),
243 }
244 }
245
246 pub fn vexpand_set(self, vexpand_set: bool) -> Self {
247 Self {
248 builder: self.builder.property("vexpand-set", vexpand_set),
249 }
250 }
251
252 pub fn visible(self, visible: bool) -> Self {
253 Self {
254 builder: self.builder.property("visible", visible),
255 }
256 }
257
258 pub fn width_request(self, width_request: i32) -> Self {
259 Self {
260 builder: self.builder.property("width-request", width_request),
261 }
262 }
263
264 pub fn accessible_role(self, accessible_role: AccessibleRole) -> Self {
265 Self {
266 builder: self.builder.property("accessible-role", accessible_role),
267 }
268 }
269
270 #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
273 pub fn build(self) -> DrawingArea {
274 assert_initialized_main_thread!();
275 self.builder.build()
276 }
277}
278
279pub trait DrawingAreaExt: IsA<DrawingArea> + 'static {
280 #[doc(alias = "gtk_drawing_area_get_content_height")]
281 #[doc(alias = "get_content_height")]
282 #[doc(alias = "content-height")]
283 fn content_height(&self) -> i32 {
284 unsafe { ffi::gtk_drawing_area_get_content_height(self.as_ref().to_glib_none().0) }
285 }
286
287 #[doc(alias = "gtk_drawing_area_get_content_width")]
288 #[doc(alias = "get_content_width")]
289 #[doc(alias = "content-width")]
290 fn content_width(&self) -> i32 {
291 unsafe { ffi::gtk_drawing_area_get_content_width(self.as_ref().to_glib_none().0) }
292 }
293
294 #[doc(alias = "gtk_drawing_area_set_content_height")]
295 #[doc(alias = "content-height")]
296 fn set_content_height(&self, height: i32) {
297 unsafe {
298 ffi::gtk_drawing_area_set_content_height(self.as_ref().to_glib_none().0, height);
299 }
300 }
301
302 #[doc(alias = "gtk_drawing_area_set_content_width")]
303 #[doc(alias = "content-width")]
304 fn set_content_width(&self, width: i32) {
305 unsafe {
306 ffi::gtk_drawing_area_set_content_width(self.as_ref().to_glib_none().0, width);
307 }
308 }
309
310 #[doc(alias = "resize")]
311 fn connect_resize<F: Fn(&Self, i32, i32) + 'static>(&self, f: F) -> SignalHandlerId {
312 unsafe extern "C" fn resize_trampoline<
313 P: IsA<DrawingArea>,
314 F: Fn(&P, i32, i32) + 'static,
315 >(
316 this: *mut ffi::GtkDrawingArea,
317 width: std::ffi::c_int,
318 height: std::ffi::c_int,
319 f: glib::ffi::gpointer,
320 ) {
321 let f: &F = &*(f as *const F);
322 f(
323 DrawingArea::from_glib_borrow(this).unsafe_cast_ref(),
324 width,
325 height,
326 )
327 }
328 unsafe {
329 let f: Box_<F> = Box_::new(f);
330 connect_raw(
331 self.as_ptr() as *mut _,
332 c"resize".as_ptr() as *const _,
333 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
334 resize_trampoline::<Self, F> as *const (),
335 )),
336 Box_::into_raw(f),
337 )
338 }
339 }
340
341 #[doc(alias = "content-height")]
342 fn connect_content_height_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
343 unsafe extern "C" fn notify_content_height_trampoline<
344 P: IsA<DrawingArea>,
345 F: Fn(&P) + 'static,
346 >(
347 this: *mut ffi::GtkDrawingArea,
348 _param_spec: glib::ffi::gpointer,
349 f: glib::ffi::gpointer,
350 ) {
351 let f: &F = &*(f as *const F);
352 f(DrawingArea::from_glib_borrow(this).unsafe_cast_ref())
353 }
354 unsafe {
355 let f: Box_<F> = Box_::new(f);
356 connect_raw(
357 self.as_ptr() as *mut _,
358 c"notify::content-height".as_ptr() as *const _,
359 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
360 notify_content_height_trampoline::<Self, F> as *const (),
361 )),
362 Box_::into_raw(f),
363 )
364 }
365 }
366
367 #[doc(alias = "content-width")]
368 fn connect_content_width_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
369 unsafe extern "C" fn notify_content_width_trampoline<
370 P: IsA<DrawingArea>,
371 F: Fn(&P) + 'static,
372 >(
373 this: *mut ffi::GtkDrawingArea,
374 _param_spec: glib::ffi::gpointer,
375 f: glib::ffi::gpointer,
376 ) {
377 let f: &F = &*(f as *const F);
378 f(DrawingArea::from_glib_borrow(this).unsafe_cast_ref())
379 }
380 unsafe {
381 let f: Box_<F> = Box_::new(f);
382 connect_raw(
383 self.as_ptr() as *mut _,
384 c"notify::content-width".as_ptr() as *const _,
385 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
386 notify_content_width_trampoline::<Self, F> as *const (),
387 )),
388 Box_::into_raw(f),
389 )
390 }
391 }
392}
393
394impl<O: IsA<DrawingArea>> DrawingAreaExt for O {}