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

Commit

Permalink
Add support for file chooser buttons
Browse files Browse the repository at this point in the history
This commit adds support for file chooser buttons. They are represented
as a slice of tuples `(text, action)`. The original function uses
varargs to support an arbitrary number of arguments. This implementation
supports up to 3 buttons.

**This is a breaking change.**

See: https://developer.gnome.org/gtk3/stable/GtkFileChooserDialog.html#gtk-file-chooser-dialog-new
  • Loading branch information
demurgos committed Dec 3, 2017
1 parent 42ad618 commit 178a7a5
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/file_chooser_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,31 @@
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 {
pub fn new<T: IsA<Window>>(title: Option<&str>, parent: Option<&T>, action: FileChooserAction, buttons: &[(&str, ResponseType)]) -> 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()))
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 => {
let second_button_text: *const c_char = buttons[1].0.to_glib_none().0;
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(), second_button_text, buttons[1].1.to_glib(), ptr::null::<c_char>())
},
_ => {
let second_button_text: *const c_char = buttons[1].0.to_glib_none().0;
let third_button_text: *const c_char = buttons[2].0.to_glib_none().0;
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(), second_button_text, buttons[1].1.to_glib(), third_button_text, buttons[2].1.to_glib(), ptr::null::<c_char>())
},
})
.downcast_unchecked()
}
}
Expand Down

0 comments on commit 178a7a5

Please sign in to comment.