Skip to content

Commit

Permalink
Merge pull request #612 from gtk-rs/bilelmoussaoui/gtk-file-chooser
Browse files Browse the repository at this point in the history
gtk: manually implement FileChooser::add_choice
  • Loading branch information
sdroege authored Oct 25, 2021
2 parents ef65614 + 0a0a58d commit da88595
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion gtk4/Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1135,9 +1135,10 @@ final_type = true
name = "Gtk.FileChooser"
status = "generate"
trust_return_value_nullability = false
manual_traits = ["FileChooserExtManual"]
[[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
45 changes: 45 additions & 0 deletions gtk4/src/file_chooser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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: &[(&str, &str)]);
}

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

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 da88595

Please sign in to comment.