Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Add support for file chooser buttons #602

Merged
merged 1 commit into from
Dec 11, 2017
Merged
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
74 changes: 68 additions & 6 deletions src/file_chooser_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,83 @@
use ffi;
use glib::translate::*;
use glib::object::{Downcast, IsA};
use libc::c_char;
use std::ptr;
use FileChooserAction;
use FileChooserDialog;
use ResponseType;
use Widget;
use Window;

impl FileChooserDialog {
pub fn new<T: IsA<Window>>(title: Option<&str>, parent: Option<&T>, action: FileChooserAction)
-> FileChooserDialog {
// TODO: Keep the other constructor with buttons support as the only constructor (this one was
// left for compatibility) and rename it to `new` for consistency.
pub fn new<T: IsA<Window>>(title: Option<&str>, parent: Option<&T>, action: FileChooserAction) -> FileChooserDialog {
assert_initialized_main_thread!();
unsafe {
Widget::from_glib_none(
ffi::gtk_file_chooser_dialog_new(title.to_glib_none().0, parent.to_glib_none().0,
action.to_glib(), ptr::null_mut()))
.downcast_unchecked()
Widget::from_glib_none(ffi::gtk_file_chooser_dialog_new(
title.to_glib_none().0,
parent.to_glib_none().0,
action.to_glib(),
ptr::null::<c_char>()
)).downcast_unchecked()
}
}

pub fn with_buttons<T: IsA<Window>>(title: Option<&str>, parent: Option<&T>, action: FileChooserAction, buttons: &[(&str, ResponseType)]) -> FileChooserDialog {
assert_initialized_main_thread!();
unsafe {
Widget::from_glib_none(match buttons.len() {
0 => {
ffi::gtk_file_chooser_dialog_new(
title.to_glib_none().0,
parent.to_glib_none().0,
action.to_glib(),
ptr::null::<c_char>()
)
},
1 => {
ffi::gtk_file_chooser_dialog_new(
title.to_glib_none().0,
parent.to_glib_none().0,
action.to_glib(),
buttons[0].0.to_glib_none().0,
buttons[0].1.to_glib(),
ptr::null::<c_char>(),
)
},
2 => {
ffi::gtk_file_chooser_dialog_new(
title.to_glib_none().0,
parent.to_glib_none().0,
action.to_glib(),
buttons[0].0.to_glib_none().0,
buttons[0].1.to_glib(),
(buttons[1].0.to_glib_none() as Stash<*const c_char, str>).0,
Copy link
Member

Choose a reason for hiding this comment

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

Stash IMHO don't needed here and in other places

Copy link
Member

Choose a reason for hiding this comment

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

Forgot that it var_arg :( so rust can't infer type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Exactly, I can omit it for the first text but not for the other ones.

buttons[1].1.to_glib(),
ptr::null::<c_char>(),
)
},
3 => {
ffi::gtk_file_chooser_dialog_new(
title.to_glib_none().0,
parent.to_glib_none().0,
action.to_glib(),
buttons[0].0.to_glib_none().0,
buttons[0].1.to_glib(),
(buttons[1].0.to_glib_none() as Stash<*const c_char, str>).0,
buttons[1].1.to_glib(),
(buttons[2].0.to_glib_none() as Stash<*const c_char, str>).0,
buttons[2].1.to_glib(),
ptr::null::<c_char>(),
)
},
_ => {
// TODO: Support arbitrary number of buttons once variadic functions are supported.
// See: https://github.com/rust-lang/rust/issues/44930
panic!(format!("`FileChooserDialog::with_buttons` does not support 4+ buttons, received {}", buttons.len()))
}
}).downcast_unchecked()
}
}
}