Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gtk: Add missing IMContextImpl methods #1394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions gtk4/src/subclass/im_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ pub trait IMContextImpl: IMContextImplExt + ObjectImpl {
fn set_surrounding(&self, text: &str, cursor_index: i32) {
self.parent_set_surrounding(text, cursor_index)
}
#[cfg(feature = "v4_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
fn set_surrounding_with_selection(&self, text: &str, cursor_index: i32, anchor_index: i32) {
self.parent_set_surrounding_with_selection(text, cursor_index, anchor_index)
}
#[cfg(feature = "v4_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
fn surrounding_with_selection(&self) -> Option<(glib::GString, i32, i32)> {
self.parent_surrounding_with_selection()
}
fn set_use_preedit(&self, use_preedit: bool) {
self.parent_set_use_preedit(use_preedit)
}
Expand Down Expand Up @@ -279,6 +289,59 @@ pub trait IMContextImplExt: ObjectSubclass {
}
}

#[cfg(feature = "v4_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
fn parent_set_surrounding_with_selection(
&self,
text: &str,
cursor_index: i32,
anchor_index: i32,
) {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkIMContextClass;
if let Some(f) = (*parent_class).set_surrounding_with_selection {
f(
self.obj().unsafe_cast_ref::<IMContext>().to_glib_none().0,
text.to_glib_none().0,
text.len() as i32,
cursor_index,
anchor_index,
)
}
}
}
#[cfg(feature = "v4_2")]
#[cfg_attr(docsrs, doc(cfg(feature = "v4_2")))]
fn parent_surrounding_with_selection(&self) -> Option<(glib::GString, i32, i32)> {
unsafe {
let data = Self::type_data();
let parent_class = data.as_ref().parent_class() as *mut ffi::GtkIMContextClass;
if let Some(f) = (*parent_class).get_surrounding_with_selection {
let mut cursor_index = std::mem::MaybeUninit::uninit();
let mut anchor_index = std::mem::MaybeUninit::uninit();
let mut text = std::ptr::null_mut();
let res = f(
self.obj().unsafe_cast_ref::<IMContext>().to_glib_none().0,
&mut text,
cursor_index.as_mut_ptr(),
anchor_index.as_mut_ptr(),
);
if from_glib(res) {
Some((
glib::GString::from_glib_none(text),
cursor_index.assume_init(),
anchor_index.assume_init(),
))
} else {
None
}
} else {
None
}
}
}

fn parent_set_use_preedit(&self, use_preedit: bool) {
unsafe {
let data = Self::type_data();
Expand Down Expand Up @@ -329,6 +392,13 @@ unsafe impl<T: IMContextImpl> IsSubclassable<T> for IMContext {
klass.set_cursor_location = Some(im_context_set_cursor_location::<T>);
klass.set_surrounding = Some(im_context_set_surrounding::<T>);
klass.set_use_preedit = Some(im_context_set_use_preedit::<T>);
#[cfg(any(feature = "v4_2", docsrs))]
{
klass.set_surrounding_with_selection =
Some(im_context_set_surrounding_with_selection::<T>);
klass.get_surrounding_with_selection =
Some(im_context_get_surrounding_with_selection::<T>);
};
#[cfg(any(feature = "v4_10", docsrs))]
{
klass.activate_osk = Some(im_context_activate_osk::<T>);
Expand Down Expand Up @@ -506,6 +576,44 @@ unsafe extern "C" fn im_context_set_use_preedit<T: IMContextImpl>(
imp.set_use_preedit(from_glib(use_preedit))
}

#[cfg(any(feature = "v4_2", docsrs))]
unsafe extern "C" fn im_context_set_surrounding_with_selection<T: IMContextImpl>(
ptr: *mut ffi::GtkIMContext,
text: *const libc::c_char,
len: i32,
cursor_index: i32,
anchor_index: i32,
) {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();

imp.set_surrounding_with_selection(
&glib::GString::from_glib_none_num(text, len as usize),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why create a copy here, and not use e.g. glib::GStr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in other places here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, we don't use glib::GStr in subclassing code at all. That is why I kept it using &str. But anyways, will update once #1678 lands

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to use if otherwise you just have to convert to a NUL-terminated string again in the end. I don't know if that's the case here

cursor_index,
anchor_index,
)
}

#[cfg(any(feature = "v4_2", docsrs))]
unsafe extern "C" fn im_context_get_surrounding_with_selection<T: IMContextImpl>(
ptr: *mut ffi::GtkIMContext,
textptr: *mut *mut libc::c_char,
cursor_indexptr: *mut libc::c_int,
anchor_indexptr: *mut libc::c_int,
) -> glib::ffi::gboolean {
let instance = &*(ptr as *mut T::Instance);
let imp = instance.imp();
match imp.surrounding_with_selection() {
Some((text, cursor_index, anchor_index)) => {
*cursor_indexptr = cursor_index;
*anchor_indexptr = anchor_index;
*textptr = text.into_glib_ptr();
true.into_glib()
}
None => false.into_glib(),
}
}

#[cfg(any(feature = "v4_10", docsrs))]
unsafe extern "C" fn im_context_activate_osk<T: IMContextImpl>(ptr: *mut ffi::GtkIMContext) {
let instance = &*(ptr as *mut T::Instance);
Expand Down