Skip to content

Commit

Permalink
gtk: manually implement FileChooser::add_choice
Browse files Browse the repository at this point in the history
Fixes #574
Moved from #229
  • Loading branch information
bilelmoussaoui committed Oct 12, 2021
1 parent 803192d commit 82815d8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gtk4/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ status = "generate"
trust_return_value_nullability = false
[[object.function]]
name = "add_choice"
ignore = true # TODO: strange type of options and option_labels
manual = true # rust-ify the options param

[[object]]
name = "Gtk.FileChooserDialog"
Expand Down
41 changes: 41 additions & 0 deletions gtk4/src/file_chooser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::FileChooser;
use glib::translate::*;
use glib::IsA;

pub trait FileChooserExtManual: 'static {
#[doc(alias = "gtk_file_chooser_add_choice")]
fn add_choice(&self, id: &str, label: &str, options: Option<&[(&str, &str)]>);
}

impl<O: IsA<FileChooser>> FileChooserExtManual for O {
fn add_choice(&self, id: &str, label: &str, options: Option<&[(&str, &str)]>) {
unsafe {
let (options_ids, options_labels) = if let Some(options) = options {
(
options
.iter()
.map(|o| o.0.to_glib_none().0)
.collect::<Vec<*const libc::c_char>>()
.as_ptr(),
options
.iter()
.map(|o| o.1.to_glib_none().0)
.collect::<Vec<*const libc::c_char>>()
.as_ptr(),
)
} else {
(std::ptr::null(), std::ptr::null())
};

ffi::gtk_file_chooser_add_choice(
self.as_ref().to_glib_none().0,
id.to_glib_none().0,
label.to_glib_none().0,
mut_override(options_ids),
mut_override(options_labels),
);
}
}
}
1 change: 1 addition & 0 deletions gtk4/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ mod entry_completion;
mod enums;
mod event_controller_key;
mod expression_watch;
mod file_chooser;
mod file_chooser_dialog;
mod flow_box;
mod font_chooser;
Expand Down
1 change: 1 addition & 0 deletions gtk4/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use crate::drawing_area::DrawingAreaExtManual;
pub use crate::editable::EditableExtManual;
pub use crate::entry::EntryExtManual;
pub use crate::entry_buffer::EntryBufferExtManual;
pub use crate::file_chooser::FileChooserExtManual;
pub use crate::font_chooser::FontChooserExtManual;
pub use crate::im_context::IMContextExtManual;
pub use crate::media_stream::MediaStreamExtManual;
Expand Down

0 comments on commit 82815d8

Please sign in to comment.