Skip to content

Commit 7a987a7

Browse files
gdk: Manually implement Cursor.new_from_callback
1 parent 85e6727 commit 7a987a7

File tree

4 files changed

+72
-60
lines changed

4 files changed

+72
-60
lines changed

gdk4/Gir.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ status = "generate"
376376
name = "Gdk.Cursor"
377377
status = "generate"
378378
generate_builder = true
379+
[[object.function]]
380+
name = "new_from_callback"
381+
manual = true # handle &mut in the callback
379382

380383
[[object]]
381384
name = "Gdk.Device"

gdk4/src/auto/cursor.rs

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
use crate::Texture;
66
use glib::{prelude::*, translate::*};
7-
#[cfg(feature = "v4_16")]
8-
#[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
9-
use std::boxed::Box as Box_;
107

118
glib::wrapper! {
129
#[doc(alias = "GdkCursor")]
@@ -18,63 +15,6 @@ glib::wrapper! {
1815
}
1916

2017
impl Cursor {
21-
#[cfg(feature = "v4_16")]
22-
#[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
23-
#[doc(alias = "gdk_cursor_new_from_callback")]
24-
#[doc(alias = "new_from_callback")]
25-
pub fn from_callback<P: Fn(&Cursor, i32, f64, i32, i32, i32, i32) -> Texture + 'static>(
26-
callback: P,
27-
fallback: Option<&Cursor>,
28-
) -> Option<Cursor> {
29-
assert_initialized_main_thread!();
30-
let callback_data: Box_<P> = Box_::new(callback);
31-
unsafe extern "C" fn callback_func<
32-
P: Fn(&Cursor, i32, f64, i32, i32, i32, i32) -> Texture + 'static,
33-
>(
34-
cursor: *mut ffi::GdkCursor,
35-
cursor_size: libc::c_int,
36-
scale: libc::c_double,
37-
width: *mut libc::c_int,
38-
height: *mut libc::c_int,
39-
hotspot_x: *mut libc::c_int,
40-
hotspot_y: *mut libc::c_int,
41-
data: glib::ffi::gpointer,
42-
) -> *mut ffi::GdkTexture {
43-
let cursor = from_glib_borrow(cursor);
44-
let callback = &*(data as *mut P);
45-
(*callback)(
46-
&cursor,
47-
cursor_size,
48-
scale,
49-
width,
50-
height,
51-
hotspot_x,
52-
hotspot_y,
53-
)
54-
/*Not checked*/
55-
.to_glib_none()
56-
.0
57-
}
58-
let callback = Some(callback_func::<P> as _);
59-
unsafe extern "C" fn destroy_func<
60-
P: Fn(&Cursor, i32, f64, i32, i32, i32, i32) -> Texture + 'static,
61-
>(
62-
data: glib::ffi::gpointer,
63-
) {
64-
let _callback = Box_::from_raw(data as *mut P);
65-
}
66-
let destroy_call2 = Some(destroy_func::<P> as _);
67-
let super_callback0: Box_<P> = callback_data;
68-
unsafe {
69-
from_glib_full(ffi::gdk_cursor_new_from_callback(
70-
callback,
71-
Box_::into_raw(super_callback0) as *mut _,
72-
destroy_call2,
73-
fallback.to_glib_none().0,
74-
))
75-
}
76-
}
77-
7818
#[doc(alias = "gdk_cursor_new_from_name")]
7919
#[doc(alias = "new_from_name")]
8020
pub fn from_name(name: &str, fallback: Option<&Cursor>) -> Option<Cursor> {

gdk4/src/cursor.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Take a look at the license at the top of the repository in the LICENSE file.
2+
3+
use crate::{Cursor, Texture};
4+
use glib::translate::*;
5+
use std::boxed::Box as Box_;
6+
7+
impl Cursor {
8+
#[cfg(feature = "v4_16")]
9+
#[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
10+
#[doc(alias = "gdk_cursor_new_from_callback")]
11+
#[doc(alias = "new_from_callback")]
12+
pub fn from_callback<
13+
P: Fn(&Cursor, i32, f64, &mut i32, &mut i32, &mut i32, &mut i32) -> Texture + 'static,
14+
>(
15+
callback: P,
16+
fallback: Option<&Cursor>,
17+
) -> Option<Cursor> {
18+
assert_initialized_main_thread!();
19+
let callback_data: Box_<P> = Box_::new(callback);
20+
unsafe extern "C" fn callback_func<
21+
P: Fn(&Cursor, i32, f64, &mut i32, &mut i32, &mut i32, &mut i32) -> Texture + 'static,
22+
>(
23+
cursor: *mut ffi::GdkCursor,
24+
cursor_size: libc::c_int,
25+
scale: libc::c_double,
26+
width: *mut libc::c_int,
27+
height: *mut libc::c_int,
28+
hotspot_x: *mut libc::c_int,
29+
hotspot_y: *mut libc::c_int,
30+
data: glib::ffi::gpointer,
31+
) -> *mut ffi::GdkTexture {
32+
let cursor = from_glib_borrow(cursor);
33+
let callback = &*(data as *mut P);
34+
(*callback)(
35+
&cursor,
36+
cursor_size,
37+
scale,
38+
&mut *width,
39+
&mut *height,
40+
&mut *hotspot_x,
41+
&mut *hotspot_y,
42+
)
43+
/*Not checked*/
44+
.to_glib_none()
45+
.0
46+
}
47+
let callback = Some(callback_func::<P> as _);
48+
unsafe extern "C" fn destroy_func<
49+
P: Fn(&Cursor, i32, f64, &mut i32, &mut i32, &mut i32, &mut i32) -> Texture + 'static,
50+
>(
51+
data: glib::ffi::gpointer,
52+
) {
53+
let _callback = Box_::from_raw(data as *mut P);
54+
}
55+
let destroy_call2 = Some(destroy_func::<P> as _);
56+
let super_callback0: Box_<P> = callback_data;
57+
unsafe {
58+
from_glib_full(ffi::gdk_cursor_new_from_callback(
59+
callback,
60+
Box_::into_raw(super_callback0) as *mut _,
61+
destroy_call2,
62+
fallback.to_glib_none().0,
63+
))
64+
}
65+
}
66+
}

gdk4/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ mod content_formats_builder;
7070
mod content_provider;
7171
mod content_serializer;
7272
mod crossing_event;
73+
#[cfg(feature = "v4_16")]
74+
#[cfg_attr(docsrs, doc(cfg(feature = "v4_16")))]
75+
mod cursor;
7376
mod delete_event;
7477
mod display;
7578
#[cfg(feature = "v4_14")]

0 commit comments

Comments
 (0)