jeremyletang / rgtk

GTK+ bindings and wrappers for Rust (DEPRECATED SEE https://github.com/rust-gnome )
GNU Lesser General Public License v3.0
121 stars 22 forks source link

Missing GtkBuilder methods #173

Closed gsingh93 closed 9 years ago

gsingh93 commented 9 years ago

Why are methods like these commented out in the source:

pub fn gtk_builder_add_from_file(builder: *mut C_GtkBuilder, file_name: *const c_char, error: *mut *mut C_GError) -> c_uint;

This particular method is a very useful one, as it's used to instantiate the view from a Glade file.

GuillaumeGomez commented 9 years ago

It can be for multiple reasons : one of the needed types isn't binded yet, the code hasn't been updated or it is just a forgotten one. Don't hesitate to bind it if you want or need it !

gsingh93 commented 9 years ago

I'll look into it. I haven't messed with FFI before, but I'll see what I can do.

On Sat, Jan 3, 2015, 3:41 PM Guillaume Gomez notifications@github.com wrote:

It can be for multiple reasons : one of the needed types isn't binded yet, the code hasn't been updated or it is just a forgotten one. Don't hesitate to bind it if you want or need it !

— Reply to this email directly or view it on GitHub https://github.com/jeremyletang/rgtk/issues/173#issuecomment-68608758.

GuillaumeGomez commented 9 years ago

Don't hesitate to ask if you need any help.

gsingh93 commented 9 years ago

Ok, I'm starting to figure out how this works. Uncommenting the methods I need causes no problems, so the only issue is implementing the safe wrappers. I need to implement GtkBuilder which inherits from GObject. Where would I put that, and is there an example of another object I can look at?

Also, the generated bindings seem to refer to C_GObject, but I had to change it to glib::ffi::C_GObject. I see other references to glib::ffi::C_GObject in the code. Can I just import this at the top and just refer to C_GObject, or is there a reason it was done like this (i.e. some kind of import shadowing issue)?

GuillaumeGomez commented 9 years ago

You have an example here. You need to create a C_GtkBuild object in ffi if it doesn't exist and call the impl_GObjectFunctions! macro to implement the needed functions. Normally, you'll never need to have to handle C_GObject yourself, but maybe I'm missing something. Do you have an example where you'd need to handle C_GObject yourself ?

gsingh93 commented 9 years ago

Regarding C_GObject, here's an example of other code that's referencing it, and here's an example of a method I need that's returning it.

GuillaumeGomez commented 9 years ago

For the gtk cast, it's very common but I'm not sure it's used. For this function, you can do something like :

pub fn get_object<T: GObject>(&self) -> T {
    ...
}

But I don't remember if we created a g_object trait. I check that !

gsingh93 commented 9 years ago

I created a file called builder.rs in src/gtk/widgets, but it's not getting built. Not only can code not import it, if I put invalid Rust code in it, I get no build errors. Do I have to do something other than add the file to get it to build?

GuillaumeGomez commented 9 years ago

You have to add :

mod builder.rs;

in src/gtk/widgets/mod.rs and also add a pub use and all the stuff. You'll have to do it in the src/gtk/mod.rs file. Look at how we did with other files.

GuillaumeGomez commented 9 years ago

Now there is a GObjectTrait so you will be able to write the code like I did just above.

gsingh93 commented 9 years ago

I'm not sure what type to return:

pub fn get_object<T: GObjectTrait>(&self, name: &str) -> T {
    let tmp = unsafe {
        name.with_c_str(|c_str| {
            ffi::gtk_builder_get_object(self.pointer, c_str)
        })
    };
    // TODO: Return object
}
GuillaumeGomez commented 9 years ago

I don't think it'll work but it's worth a try:

pub fn get_object<T: GObjectTrait>(&self, name: &str) -> T {
    let tmp = unsafe {
        name.with_c_str(|c_str| {
            ffi::gtk_builder_get_object(self.pointer, c_str)
        })
    };
    unsafe {
        ::std::mem::transmute(GtkBuild {
            pointer: ::std::mem::transmute(tmp)
        })
    }
}

Yes, it's SUPER ugly.

gsingh93 commented 9 years ago
error: cannot transmute to or from a type that contains type parameters in its interior

Should there be some GObject struct that we should be able to return here?

GuillaumeGomez commented 9 years ago

Hum... Try this:

pub fn get_object<T: GObjectTrait>(&self, name: &str) -> T {
    let tmp = unsafe {
        name.with_c_str(|c_str| {
            ffi::gtk_builder_get_object(self.pointer, c_str)
        })
    };
    unsafe {
        ::std::mem::transmute(GtkBuild {
            pointer: tmp
        })
    }
}
gsingh93 commented 9 years ago
mismatched types: expected `*mut gtk::ffi::C_GtkBuilder`, found `*mut glib::ffi::C_GObject` (expected struct gtk::ffi::C_GtkBuilder, found struct glib::ffi::C_GObject)

If you want to take a closer look, I've pushed what I have so far here: https://github.com/gsingh93/rgtk

GuillaumeGomez commented 9 years ago

Oh my bad, I forgot:

pub fn get_object<T: GObjectTrait>(&self, name: &str) -> T {
    let tmp = unsafe {
        name.with_c_str(|c_str| {
            ffi::gtk_builder_get_object(self.pointer, c_str)
        })
    };
    unsafe {
        ::std::mem::transmute(GtkBuild {
            pointer: tmp as *mut ffi::C_GtkBuilder
        })
    }
}

My goal in here is just to have a similar struct to the one which will be returned. They're pretty much all the same.

gsingh93 commented 9 years ago

Back to the old error:

src/gtk/widgets/builder.rs:91:13: 91:34 error: cannot transmute to or from a type that contains type parameters in its interior [E0139]
src/gtk/widgets/builder.rs:91             ::std::mem::transmute(Builder {
                                          ^~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
GuillaumeGomez commented 9 years ago

Ok, I'll watch that later. Comment this part of the code and continue.

gsingh93 commented 9 years ago

The other methods I needed are working, so I just need to get this one working. Let me know when you get around to looking at it.

GuillaumeGomez commented 9 years ago

I'd liked to but I can't work on it as long as this PR isn't fixed and merged.

gsingh93 commented 9 years ago

Can you look at this again?

GuillaumeGomez commented 9 years ago

I'm taking a look as soon as possible (today or tomorrow).

GuillaumeGomez commented 9 years ago

@gsingh93: Look at the PR #188.