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

Draft: subclass: Add missing BuildableImpl vfuncs #900

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
</child>
</object>
</child>
<label-attribute name="hello" />
</template>
</interface>
93 changes: 92 additions & 1 deletion examples/custom_buildable/custom_buildable/imp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use gtk::prelude::*;
use glib::translate::*;
use glib::GString;
use gtk::subclass::prelude::*;
use gtk::{glib, CompositeTemplate};
use gtk::{
prelude::*,
subclass::{BuildableParser, BuildableParserImpl},
};
use std::os::raw::c_char;
use std::{cell::RefCell, collections::HashMap, rc::Rc};

#[derive(Debug, Default, CompositeTemplate)]
#[template(file = "custom_buildable.ui")]
Expand Down Expand Up @@ -64,4 +71,88 @@ impl BuildableImpl for CustomBuildable {
buildable.add_suffix(child.downcast_ref::<gtk::Widget>().unwrap());
};
}

unsafe fn custom_tag_start(
&self,
buildable: &Self::Type,
builder: &gtk::Builder,
child: Option<&glib::Object>,
tag_name: &str,
parser_data: *mut glib::ffi::gpointer,
) -> Option<BuildableParser> {
if let Some(parser) =
self.parent_custom_tag_start(buildable, builder, child, tag_name, parser_data)
{
return Some(parser);
}

let x = tag_name == "label-attribute";
let parser = BuildableParser::new::<CustomBuildableParser>();
*parser_data = &CustomBuildableParser::new() as *const _ as *mut _;
Some(parser)
}

unsafe fn custom_tag_end(
&self,
buildable: &Self::Type,
builder: &gtk::Builder,
child: Option<&glib::Object>,
tag_name: &str,
parser_data: glib::ffi::gpointer,
) {
let parser_data = parser_data as *const _ as *mut _ as CustomBuildableParser;
println!("{:#?}", tag_name);
}
}

#[repr(C)]
pub struct CustomBuildableParser {
pub name: *const c_char,
}

impl CustomBuildableParser {
pub fn new() -> Self {
Self {
name: std::ptr::null_mut(),
}
}
}

unsafe impl BuildableParserImpl for CustomBuildableParser {
type Type = Self;
fn start_element(
parser: &mut Self::Type,
ctx: &gtk::subclass::BuildableParseContext,
element_name: &str,
attributes: HashMap<String, String>,
) -> Result<(), glib::Error> {
unsafe {
parser.name = "hello world".as_ptr() as *const _;
}
println!("start element {}", element_name);
println!("{:#?}", attributes);
Ok(())
}

fn end_element(
parser: &mut Self::Type,
ctx: &gtk::subclass::BuildableParseContext,
element_name: &str,
) -> Result<(), glib::Error> {
unsafe {
println!("{:#?}", from_glib_borrow::<_, Option<GString>>(parser.name));
}
println!("end element {}", element_name);
Ok(())
}

fn text(
parser: &mut Self::Type,
ctx: &gtk::subclass::BuildableParseContext,
text: &str,
) -> Result<(), glib::Error> {
println!("text {}", text);

Ok(())
}
}
217 changes: 197 additions & 20 deletions gtk4/src/subclass/buildable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use glib::translate::*;
use glib::{Cast, GString, Object, Quark, Value};
use once_cell::sync::Lazy;

use super::PtrHolder;
use super::{BuildableParser, PtrHolder};

pub trait BuildableImpl: ObjectImpl {
fn set_id(&self, buildable: &Self::Type, id: &str) {
Expand Down Expand Up @@ -51,35 +51,36 @@ pub trait BuildableImpl: ObjectImpl {
fn construct_child(&self, buildable: &Self::Type, builder: &Builder, name: &str) -> Object {
self.parent_construct_child(buildable, builder, name)
}
/*
Only useful for custom tags, not something the application developer has to often implement
and needs more thinking in terms of how to handle both BuildableParser & the various ptr you're supposed to pass around
fn custom_tag_start(
unsafe fn custom_tag_start(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tagname: &str,
parser: BuildableParser,
data: ptr,
);
fn custom_tag_end(
tag_name: &str,
parser_data: *mut *mut libc::c_void,
Copy link
Member

Choose a reason for hiding this comment

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

How is this pointer used exactly?

Copy link
Member Author

Choose a reason for hiding this comment

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

It is used to pass the state to the BuildableParser, my idea was to create a struct that implements a certain trait and move the struct around instead of passing a random ptr around

Copy link
Member

Choose a reason for hiding this comment

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

Sounds like a good plan :) Where would the struct be freed, is the finish() function guaranteed to be called exactly once with the same parser data, for example?

) -> Option<BuildableParser> {
self.parent_custom_tag_start(buildable, builder, child, tag_name, parser_data)
}
unsafe fn custom_tag_end(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tagname: &str,
data: ptr,
);
fn custom_finished(
tag_name: &str,
parser_data: *mut libc::c_void,
) {
self.parent_custom_tag_end(buildable, builder, child, tag_name, parser_data)
}
unsafe fn custom_finished(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tagname: &str,
data: ptr,
);
*/
tag_name: &str,
parser_data: *mut libc::c_void,
) {
self.parent_custom_finished(buildable, builder, child, tag_name, parser_data)
}
}

pub trait BuildableImplExt: ObjectSubclass {
Expand Down Expand Up @@ -112,6 +113,30 @@ pub trait BuildableImplExt: ObjectSubclass {
builder: &Builder,
name: &str,
) -> Object;
unsafe fn parent_custom_tag_start(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tag_name: &str,
parser_data: *mut *mut libc::c_void,
) -> Option<BuildableParser>;
unsafe fn parent_custom_tag_end(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tag_name: &str,
parser_data: *mut libc::c_void,
);
unsafe fn parent_custom_finished(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tag_name: &str,
parser_data: *mut libc::c_void,
);
}

impl<T: BuildableImpl> BuildableImplExt for T {
Expand Down Expand Up @@ -263,6 +288,88 @@ impl<T: BuildableImpl> BuildableImplExt for T {
))
}
}

unsafe fn parent_custom_tag_start(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tag_name: &str,
parser_data: *mut *mut libc::c_void,
) -> Option<BuildableParser> {
let type_data = Self::type_data();
let parent_iface =
type_data.as_ref().parent_interface::<Buildable>() as *const ffi::GtkBuildableIface;

let func = (*parent_iface)
.custom_tag_start
.expect("no parent \"custom_tag_start\" implementation");

let parser = std::ptr::null_mut();
let ret = from_glib(func(
buildable.unsafe_cast_ref::<Buildable>().to_glib_none().0,
builder.to_glib_none().0,
child.to_glib_none().0,
tag_name.to_glib_none().0,
parser,
parser_data,
));
if ret {
Some(BuildableParser::from_raw(parser))
} else {
None
}
}

unsafe fn parent_custom_tag_end(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tag_name: &str,
parser_data: *mut libc::c_void,
) {
let type_data = Self::type_data();
let parent_iface =
type_data.as_ref().parent_interface::<Buildable>() as *const ffi::GtkBuildableIface;

let func = (*parent_iface)
.custom_tag_end
.expect("no parent \"custom_tag_end\" implementation");

func(
buildable.unsafe_cast_ref::<Buildable>().to_glib_none().0,
builder.to_glib_none().0,
child.to_glib_none().0,
tag_name.to_glib_none().0,
parser_data,
)
}

unsafe fn parent_custom_finished(
&self,
buildable: &Self::Type,
builder: &Builder,
child: Option<&Object>,
tag_name: &str,
parser_data: *mut libc::c_void,
) {
let type_data = Self::type_data();
let parent_iface =
type_data.as_ref().parent_interface::<Buildable>() as *const ffi::GtkBuildableIface;

let func = (*parent_iface)
.custom_finished
.expect("no parent \"custom_finished\" implementation");

func(
buildable.unsafe_cast_ref::<Buildable>().to_glib_none().0,
builder.to_glib_none().0,
child.to_glib_none().0,
tag_name.to_glib_none().0,
parser_data,
)
}
}

unsafe impl<T: BuildableImpl> IsImplementable<T> for Buildable {
Expand All @@ -276,11 +383,9 @@ unsafe impl<T: BuildableImpl> IsImplementable<T> for Buildable {
iface.construct_child = Some(buildable_construct_child::<T>);
iface.parser_finished = Some(buildable_parser_finished::<T>);
iface.get_internal_child = Some(buildable_get_internal_child::<T>);
/* for the future
iface.custom_tag_start = Some(buildable_custom_tag_start::<T>);
iface.custom_tag_end = Some(buildable_custom_tag_end::<T>);
iface.custom_finished = Some(buildable_custom_finished::<T>);
*/
}
}

Expand Down Expand Up @@ -398,3 +503,75 @@ unsafe extern "C" fn buildable_get_internal_child<T: BuildableImpl>(
);
ret
}

unsafe extern "C" fn buildable_custom_tag_start<T: BuildableImpl>(
buildable: *mut ffi::GtkBuildable,
builderptr: *mut ffi::GtkBuilder,
child: *mut glib::gobject_ffi::GObject,
nameptr: *const libc::c_char,
parserptr: *mut ffi::GtkBuildableParser,
data: *mut glib::ffi::gpointer,
) -> glib::ffi::gboolean {
let instance = &*(buildable as *mut T::Instance);
let imp = instance.imp();
let wrap = from_glib_borrow::<_, Buildable>(buildable);
let name = from_glib_borrow::<_, GString>(nameptr);
let child = from_glib_borrow::<_, Option<Object>>(child);

if let Some(parser) = imp.custom_tag_start(
wrap.unsafe_cast_ref(),
&from_glib_borrow(builderptr),
child.as_ref().as_ref(),
&name,
data,
) {
*parserptr = *parser.as_ptr();
true.into_glib()
} else {
false.into_glib()
}
}

unsafe extern "C" fn buildable_custom_tag_end<T: BuildableImpl>(
buildable: *mut ffi::GtkBuildable,
builderptr: *mut ffi::GtkBuilder,
child: *mut glib::gobject_ffi::GObject,
nameptr: *const libc::c_char,
data: glib::ffi::gpointer,
) {
let instance = &*(buildable as *mut T::Instance);
let imp = instance.imp();
let wrap = from_glib_borrow::<_, Buildable>(buildable);
let name = from_glib_borrow::<_, GString>(nameptr);
let child = from_glib_borrow::<_, Option<Object>>(child);

imp.custom_tag_end(
wrap.unsafe_cast_ref(),
&from_glib_borrow(builderptr),
child.as_ref().as_ref(),
&name,
data,
)
}

unsafe extern "C" fn buildable_custom_finished<T: BuildableImpl>(
buildable: *mut ffi::GtkBuildable,
builderptr: *mut ffi::GtkBuilder,
child: *mut glib::gobject_ffi::GObject,
nameptr: *const libc::c_char,
data: glib::ffi::gpointer,
) {
let instance = &*(buildable as *mut T::Instance);
let imp = instance.imp();
let wrap = from_glib_borrow::<_, Buildable>(buildable);
let name = from_glib_borrow::<_, GString>(nameptr);
let child = from_glib_borrow::<_, Option<Object>>(child);

imp.custom_finished(
wrap.unsafe_cast_ref(),
&from_glib_borrow(builderptr),
child.as_ref().as_ref(),
&name,
data,
)
}
Loading