1#![allow(deprecated)]
5
6use crate::{
7 ffi, CairoContext, Cursor, Device, Display, Event, FrameClock, GLContext, ModifierType,
8 Monitor, VulkanContext,
9};
10use glib::{
11 object::ObjectType as _,
12 prelude::*,
13 signal::{connect_raw, SignalHandlerId},
14 translate::*,
15};
16use std::boxed::Box as Box_;
17
18glib::wrapper! {
19 #[doc(alias = "GdkSurface")]
20 pub struct Surface(Object<ffi::GdkSurface, ffi::GdkSurfaceClass>);
21
22 match fn {
23 type_ => || ffi::gdk_surface_get_type(),
24 }
25}
26
27impl Surface {
28 pub const NONE: Option<&'static Surface> = None;
29
30 #[doc(alias = "gdk_surface_new_popup")]
31 pub fn new_popup(parent: &impl IsA<Surface>, autohide: bool) -> Surface {
32 skip_assert_initialized!();
33 unsafe {
34 from_glib_full(ffi::gdk_surface_new_popup(
35 parent.as_ref().to_glib_none().0,
36 autohide.into_glib(),
37 ))
38 }
39 }
40
41 #[doc(alias = "gdk_surface_new_toplevel")]
42 pub fn new_toplevel(display: &impl IsA<Display>) -> Surface {
43 skip_assert_initialized!();
44 unsafe {
45 from_glib_full(ffi::gdk_surface_new_toplevel(
46 display.as_ref().to_glib_none().0,
47 ))
48 }
49 }
50}
51
52pub trait SurfaceExt: IsA<Surface> + 'static {
53 #[doc(alias = "gdk_surface_beep")]
54 fn beep(&self) {
55 unsafe {
56 ffi::gdk_surface_beep(self.as_ref().to_glib_none().0);
57 }
58 }
59
60 #[cfg_attr(feature = "v4_18", deprecated = "Since 4.18")]
61 #[allow(deprecated)]
62 #[doc(alias = "gdk_surface_create_cairo_context")]
63 fn create_cairo_context(&self) -> CairoContext {
64 unsafe {
65 from_glib_full(ffi::gdk_surface_create_cairo_context(
66 self.as_ref().to_glib_none().0,
67 ))
68 }
69 }
70
71 #[doc(alias = "gdk_surface_create_gl_context")]
72 fn create_gl_context(&self) -> Result<GLContext, glib::Error> {
73 unsafe {
74 let mut error = std::ptr::null_mut();
75 let ret =
76 ffi::gdk_surface_create_gl_context(self.as_ref().to_glib_none().0, &mut error);
77 if error.is_null() {
78 Ok(from_glib_full(ret))
79 } else {
80 Err(from_glib_full(error))
81 }
82 }
83 }
84
85 #[cfg_attr(feature = "v4_14", deprecated = "Since 4.14")]
86 #[allow(deprecated)]
87 #[doc(alias = "gdk_surface_create_vulkan_context")]
88 fn create_vulkan_context(&self) -> Result<VulkanContext, glib::Error> {
89 unsafe {
90 let mut error = std::ptr::null_mut();
91 let ret =
92 ffi::gdk_surface_create_vulkan_context(self.as_ref().to_glib_none().0, &mut error);
93 if error.is_null() {
94 Ok(from_glib_full(ret))
95 } else {
96 Err(from_glib_full(error))
97 }
98 }
99 }
100
101 #[doc(alias = "gdk_surface_destroy")]
102 fn destroy(&self) {
103 unsafe {
104 ffi::gdk_surface_destroy(self.as_ref().to_glib_none().0);
105 }
106 }
107
108 #[doc(alias = "gdk_surface_get_cursor")]
109 #[doc(alias = "get_cursor")]
110 fn cursor(&self) -> Option<Cursor> {
111 unsafe { from_glib_none(ffi::gdk_surface_get_cursor(self.as_ref().to_glib_none().0)) }
112 }
113
114 #[doc(alias = "gdk_surface_get_device_cursor")]
115 #[doc(alias = "get_device_cursor")]
116 fn device_cursor(&self, device: &impl IsA<Device>) -> Option<Cursor> {
117 unsafe {
118 from_glib_none(ffi::gdk_surface_get_device_cursor(
119 self.as_ref().to_glib_none().0,
120 device.as_ref().to_glib_none().0,
121 ))
122 }
123 }
124
125 #[doc(alias = "gdk_surface_get_device_position")]
126 #[doc(alias = "get_device_position")]
127 fn device_position(&self, device: &impl IsA<Device>) -> Option<(f64, f64, ModifierType)> {
128 unsafe {
129 let mut x = std::mem::MaybeUninit::uninit();
130 let mut y = std::mem::MaybeUninit::uninit();
131 let mut mask = std::mem::MaybeUninit::uninit();
132 let ret = from_glib(ffi::gdk_surface_get_device_position(
133 self.as_ref().to_glib_none().0,
134 device.as_ref().to_glib_none().0,
135 x.as_mut_ptr(),
136 y.as_mut_ptr(),
137 mask.as_mut_ptr(),
138 ));
139 if ret {
140 Some((
141 x.assume_init(),
142 y.assume_init(),
143 from_glib(mask.assume_init()),
144 ))
145 } else {
146 None
147 }
148 }
149 }
150
151 #[doc(alias = "gdk_surface_get_display")]
152 #[doc(alias = "get_display")]
153 fn display(&self) -> Display {
154 unsafe { from_glib_none(ffi::gdk_surface_get_display(self.as_ref().to_glib_none().0)) }
155 }
156
157 #[doc(alias = "gdk_surface_get_frame_clock")]
158 #[doc(alias = "get_frame_clock")]
159 #[doc(alias = "frame-clock")]
160 fn frame_clock(&self) -> FrameClock {
161 unsafe {
162 from_glib_none(ffi::gdk_surface_get_frame_clock(
163 self.as_ref().to_glib_none().0,
164 ))
165 }
166 }
167
168 #[doc(alias = "gdk_surface_get_height")]
169 #[doc(alias = "get_height")]
170 fn height(&self) -> i32 {
171 unsafe { ffi::gdk_surface_get_height(self.as_ref().to_glib_none().0) }
172 }
173
174 #[doc(alias = "gdk_surface_get_mapped")]
175 #[doc(alias = "get_mapped")]
176 #[doc(alias = "mapped")]
177 fn is_mapped(&self) -> bool {
178 unsafe { from_glib(ffi::gdk_surface_get_mapped(self.as_ref().to_glib_none().0)) }
179 }
180
181 #[cfg(feature = "v4_12")]
182 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
183 #[doc(alias = "gdk_surface_get_scale")]
184 #[doc(alias = "get_scale")]
185 fn scale(&self) -> f64 {
186 unsafe { ffi::gdk_surface_get_scale(self.as_ref().to_glib_none().0) }
187 }
188
189 #[doc(alias = "gdk_surface_get_scale_factor")]
190 #[doc(alias = "get_scale_factor")]
191 #[doc(alias = "scale-factor")]
192 fn scale_factor(&self) -> i32 {
193 unsafe { ffi::gdk_surface_get_scale_factor(self.as_ref().to_glib_none().0) }
194 }
195
196 #[doc(alias = "gdk_surface_get_width")]
197 #[doc(alias = "get_width")]
198 fn width(&self) -> i32 {
199 unsafe { ffi::gdk_surface_get_width(self.as_ref().to_glib_none().0) }
200 }
201
202 #[doc(alias = "gdk_surface_hide")]
203 fn hide(&self) {
204 unsafe {
205 ffi::gdk_surface_hide(self.as_ref().to_glib_none().0);
206 }
207 }
208
209 #[doc(alias = "gdk_surface_is_destroyed")]
210 fn is_destroyed(&self) -> bool {
211 unsafe {
212 from_glib(ffi::gdk_surface_is_destroyed(
213 self.as_ref().to_glib_none().0,
214 ))
215 }
216 }
217
218 #[doc(alias = "gdk_surface_queue_render")]
219 fn queue_render(&self) {
220 unsafe {
221 ffi::gdk_surface_queue_render(self.as_ref().to_glib_none().0);
222 }
223 }
224
225 #[doc(alias = "gdk_surface_request_layout")]
226 fn request_layout(&self) {
227 unsafe {
228 ffi::gdk_surface_request_layout(self.as_ref().to_glib_none().0);
229 }
230 }
231
232 #[doc(alias = "gdk_surface_set_cursor")]
233 #[doc(alias = "cursor")]
234 fn set_cursor(&self, cursor: Option<&Cursor>) {
235 unsafe {
236 ffi::gdk_surface_set_cursor(self.as_ref().to_glib_none().0, cursor.to_glib_none().0);
237 }
238 }
239
240 #[doc(alias = "gdk_surface_set_device_cursor")]
241 fn set_device_cursor(&self, device: &impl IsA<Device>, cursor: &Cursor) {
242 unsafe {
243 ffi::gdk_surface_set_device_cursor(
244 self.as_ref().to_glib_none().0,
245 device.as_ref().to_glib_none().0,
246 cursor.to_glib_none().0,
247 );
248 }
249 }
250
251 #[doc(alias = "gdk_surface_set_input_region")]
252 fn set_input_region(&self, region: &cairo::Region) {
253 unsafe {
254 ffi::gdk_surface_set_input_region(
255 self.as_ref().to_glib_none().0,
256 mut_override(region.to_glib_none().0),
257 );
258 }
259 }
260
261 #[cfg_attr(feature = "v4_16", deprecated = "Since 4.16")]
262 #[allow(deprecated)]
263 #[doc(alias = "gdk_surface_set_opaque_region")]
264 fn set_opaque_region(&self, region: Option<&cairo::Region>) {
265 unsafe {
266 ffi::gdk_surface_set_opaque_region(
267 self.as_ref().to_glib_none().0,
268 mut_override(region.to_glib_none().0),
269 );
270 }
271 }
272
273 #[doc(alias = "enter-monitor")]
274 fn connect_enter_monitor<F: Fn(&Self, &Monitor) + 'static>(&self, f: F) -> SignalHandlerId {
275 unsafe extern "C" fn enter_monitor_trampoline<
276 P: IsA<Surface>,
277 F: Fn(&P, &Monitor) + 'static,
278 >(
279 this: *mut ffi::GdkSurface,
280 monitor: *mut ffi::GdkMonitor,
281 f: glib::ffi::gpointer,
282 ) {
283 let f: &F = &*(f as *const F);
284 f(
285 Surface::from_glib_borrow(this).unsafe_cast_ref(),
286 &from_glib_borrow(monitor),
287 )
288 }
289 unsafe {
290 let f: Box_<F> = Box_::new(f);
291 connect_raw(
292 self.as_ptr() as *mut _,
293 c"enter-monitor".as_ptr() as *const _,
294 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
295 enter_monitor_trampoline::<Self, F> as *const (),
296 )),
297 Box_::into_raw(f),
298 )
299 }
300 }
301
302 #[doc(alias = "event")]
303 fn connect_event<F: Fn(&Self, &Event) -> bool + 'static>(&self, f: F) -> SignalHandlerId {
304 unsafe extern "C" fn event_trampoline<
305 P: IsA<Surface>,
306 F: Fn(&P, &Event) -> bool + 'static,
307 >(
308 this: *mut ffi::GdkSurface,
309 event: *mut ffi::GdkEvent,
310 f: glib::ffi::gpointer,
311 ) -> glib::ffi::gboolean {
312 let f: &F = &*(f as *const F);
313 f(
314 Surface::from_glib_borrow(this).unsafe_cast_ref(),
315 &from_glib_borrow(event),
316 )
317 .into_glib()
318 }
319 unsafe {
320 let f: Box_<F> = Box_::new(f);
321 connect_raw(
322 self.as_ptr() as *mut _,
323 c"event".as_ptr() as *const _,
324 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
325 event_trampoline::<Self, F> as *const (),
326 )),
327 Box_::into_raw(f),
328 )
329 }
330 }
331
332 #[doc(alias = "layout")]
333 fn connect_layout<F: Fn(&Self, i32, i32) + 'static>(&self, f: F) -> SignalHandlerId {
334 unsafe extern "C" fn layout_trampoline<P: IsA<Surface>, F: Fn(&P, i32, i32) + 'static>(
335 this: *mut ffi::GdkSurface,
336 width: std::ffi::c_int,
337 height: std::ffi::c_int,
338 f: glib::ffi::gpointer,
339 ) {
340 let f: &F = &*(f as *const F);
341 f(
342 Surface::from_glib_borrow(this).unsafe_cast_ref(),
343 width,
344 height,
345 )
346 }
347 unsafe {
348 let f: Box_<F> = Box_::new(f);
349 connect_raw(
350 self.as_ptr() as *mut _,
351 c"layout".as_ptr() as *const _,
352 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
353 layout_trampoline::<Self, F> as *const (),
354 )),
355 Box_::into_raw(f),
356 )
357 }
358 }
359
360 #[doc(alias = "leave-monitor")]
361 fn connect_leave_monitor<F: Fn(&Self, &Monitor) + 'static>(&self, f: F) -> SignalHandlerId {
362 unsafe extern "C" fn leave_monitor_trampoline<
363 P: IsA<Surface>,
364 F: Fn(&P, &Monitor) + 'static,
365 >(
366 this: *mut ffi::GdkSurface,
367 monitor: *mut ffi::GdkMonitor,
368 f: glib::ffi::gpointer,
369 ) {
370 let f: &F = &*(f as *const F);
371 f(
372 Surface::from_glib_borrow(this).unsafe_cast_ref(),
373 &from_glib_borrow(monitor),
374 )
375 }
376 unsafe {
377 let f: Box_<F> = Box_::new(f);
378 connect_raw(
379 self.as_ptr() as *mut _,
380 c"leave-monitor".as_ptr() as *const _,
381 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
382 leave_monitor_trampoline::<Self, F> as *const (),
383 )),
384 Box_::into_raw(f),
385 )
386 }
387 }
388
389 #[doc(alias = "render")]
390 fn connect_render<F: Fn(&Self, &cairo::Region) -> bool + 'static>(
391 &self,
392 f: F,
393 ) -> SignalHandlerId {
394 unsafe extern "C" fn render_trampoline<
395 P: IsA<Surface>,
396 F: Fn(&P, &cairo::Region) -> bool + 'static,
397 >(
398 this: *mut ffi::GdkSurface,
399 region: *mut cairo::ffi::cairo_region_t,
400 f: glib::ffi::gpointer,
401 ) -> glib::ffi::gboolean {
402 let f: &F = &*(f as *const F);
403 f(
404 Surface::from_glib_borrow(this).unsafe_cast_ref(),
405 &from_glib_borrow(region),
406 )
407 .into_glib()
408 }
409 unsafe {
410 let f: Box_<F> = Box_::new(f);
411 connect_raw(
412 self.as_ptr() as *mut _,
413 c"render".as_ptr() as *const _,
414 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
415 render_trampoline::<Self, F> as *const (),
416 )),
417 Box_::into_raw(f),
418 )
419 }
420 }
421
422 #[doc(alias = "cursor")]
423 fn connect_cursor_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
424 unsafe extern "C" fn notify_cursor_trampoline<P: IsA<Surface>, F: Fn(&P) + 'static>(
425 this: *mut ffi::GdkSurface,
426 _param_spec: glib::ffi::gpointer,
427 f: glib::ffi::gpointer,
428 ) {
429 let f: &F = &*(f as *const F);
430 f(Surface::from_glib_borrow(this).unsafe_cast_ref())
431 }
432 unsafe {
433 let f: Box_<F> = Box_::new(f);
434 connect_raw(
435 self.as_ptr() as *mut _,
436 c"notify::cursor".as_ptr() as *const _,
437 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
438 notify_cursor_trampoline::<Self, F> as *const (),
439 )),
440 Box_::into_raw(f),
441 )
442 }
443 }
444
445 #[doc(alias = "height")]
446 fn connect_height_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
447 unsafe extern "C" fn notify_height_trampoline<P: IsA<Surface>, F: Fn(&P) + 'static>(
448 this: *mut ffi::GdkSurface,
449 _param_spec: glib::ffi::gpointer,
450 f: glib::ffi::gpointer,
451 ) {
452 let f: &F = &*(f as *const F);
453 f(Surface::from_glib_borrow(this).unsafe_cast_ref())
454 }
455 unsafe {
456 let f: Box_<F> = Box_::new(f);
457 connect_raw(
458 self.as_ptr() as *mut _,
459 c"notify::height".as_ptr() as *const _,
460 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
461 notify_height_trampoline::<Self, F> as *const (),
462 )),
463 Box_::into_raw(f),
464 )
465 }
466 }
467
468 #[doc(alias = "mapped")]
469 fn connect_mapped_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
470 unsafe extern "C" fn notify_mapped_trampoline<P: IsA<Surface>, F: Fn(&P) + 'static>(
471 this: *mut ffi::GdkSurface,
472 _param_spec: glib::ffi::gpointer,
473 f: glib::ffi::gpointer,
474 ) {
475 let f: &F = &*(f as *const F);
476 f(Surface::from_glib_borrow(this).unsafe_cast_ref())
477 }
478 unsafe {
479 let f: Box_<F> = Box_::new(f);
480 connect_raw(
481 self.as_ptr() as *mut _,
482 c"notify::mapped".as_ptr() as *const _,
483 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
484 notify_mapped_trampoline::<Self, F> as *const (),
485 )),
486 Box_::into_raw(f),
487 )
488 }
489 }
490
491 #[cfg(feature = "v4_12")]
492 #[cfg_attr(docsrs, doc(cfg(feature = "v4_12")))]
493 #[doc(alias = "scale")]
494 fn connect_scale_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
495 unsafe extern "C" fn notify_scale_trampoline<P: IsA<Surface>, F: Fn(&P) + 'static>(
496 this: *mut ffi::GdkSurface,
497 _param_spec: glib::ffi::gpointer,
498 f: glib::ffi::gpointer,
499 ) {
500 let f: &F = &*(f as *const F);
501 f(Surface::from_glib_borrow(this).unsafe_cast_ref())
502 }
503 unsafe {
504 let f: Box_<F> = Box_::new(f);
505 connect_raw(
506 self.as_ptr() as *mut _,
507 c"notify::scale".as_ptr() as *const _,
508 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
509 notify_scale_trampoline::<Self, F> as *const (),
510 )),
511 Box_::into_raw(f),
512 )
513 }
514 }
515
516 #[doc(alias = "scale-factor")]
517 fn connect_scale_factor_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
518 unsafe extern "C" fn notify_scale_factor_trampoline<
519 P: IsA<Surface>,
520 F: Fn(&P) + 'static,
521 >(
522 this: *mut ffi::GdkSurface,
523 _param_spec: glib::ffi::gpointer,
524 f: glib::ffi::gpointer,
525 ) {
526 let f: &F = &*(f as *const F);
527 f(Surface::from_glib_borrow(this).unsafe_cast_ref())
528 }
529 unsafe {
530 let f: Box_<F> = Box_::new(f);
531 connect_raw(
532 self.as_ptr() as *mut _,
533 c"notify::scale-factor".as_ptr() as *const _,
534 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
535 notify_scale_factor_trampoline::<Self, F> as *const (),
536 )),
537 Box_::into_raw(f),
538 )
539 }
540 }
541
542 #[doc(alias = "width")]
543 fn connect_width_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
544 unsafe extern "C" fn notify_width_trampoline<P: IsA<Surface>, F: Fn(&P) + 'static>(
545 this: *mut ffi::GdkSurface,
546 _param_spec: glib::ffi::gpointer,
547 f: glib::ffi::gpointer,
548 ) {
549 let f: &F = &*(f as *const F);
550 f(Surface::from_glib_borrow(this).unsafe_cast_ref())
551 }
552 unsafe {
553 let f: Box_<F> = Box_::new(f);
554 connect_raw(
555 self.as_ptr() as *mut _,
556 c"notify::width".as_ptr() as *const _,
557 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
558 notify_width_trampoline::<Self, F> as *const (),
559 )),
560 Box_::into_raw(f),
561 )
562 }
563 }
564}
565
566impl<O: IsA<Surface>> SurfaceExt for O {}