1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2013-2016, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use glib::object::{Cast, IsA};
use glib::translate::*;
use gtk_sys;
use libc::c_char;
use std::ptr;
use FileChooserAction;
use FileChooserDialog;
use ResponseType;
use Widget;
use Window;

impl 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.
    /// Creates a new `FileChooserDialog`. This function is analogous to
    /// `Dialog::new_with_buttons`.
    /// ## `title`
    /// Title of the dialog, or `None`
    /// ## `parent`
    /// Transient parent of the dialog, or `None`
    /// ## `action`
    /// Open or save mode for the dialog
    /// ## `first_button_text`
    /// stock ID or text to go in the first button, or `None`
    ///
    /// # Returns
    ///
    /// a new `FileChooserDialog`
    pub fn new<T: IsA<Window>>(
        title: Option<&str>,
        parent: Option<&T>,
        action: FileChooserAction,
    ) -> FileChooserDialog {
        assert_initialized_main_thread!();
        unsafe {
            Widget::from_glib_none(gtk_sys::gtk_file_chooser_dialog_new(
                title.to_glib_none().0,
                parent.map(|p| p.as_ref()).to_glib_none().0,
                action.to_glib(),
                ptr::null::<c_char>(),
            ))
            .unsafe_cast()
        }
    }

    /// This function creates a `FileChooserDialog` with buttons:
    ///
    /// ```no_run
    /// # use gtk::prelude::*;
    /// # use gtk::{FileChooserAction, FileChooserDialog, ResponseType, Window};
    /// let dialog = FileChooserDialog::with_buttons::<Window>(
    ///     Some("Open File"),
    ///     None,
    ///     FileChooserAction::Open,
    ///     &[("_Cancel", ResponseType::Cancel), ("_Open", ResponseType::Accept)]
    /// );
    /// ```
    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 => {
                    gtk_sys::gtk_file_chooser_dialog_new(
                        title.to_glib_none().0,
                        parent.map(|p| p.as_ref()).to_glib_none().0,
                        action.to_glib(),
                        ptr::null::<c_char>()
                    )
                },
                1 => {
                    gtk_sys::gtk_file_chooser_dialog_new(
                        title.to_glib_none().0,
                        parent.map(|p| p.as_ref()).to_glib_none().0,
                        action.to_glib(),
                        buttons[0].0.to_glib_none().0,
                        buttons[0].1.to_glib(),
                        ptr::null::<c_char>(),
                    )
                },
                2 => {
                    gtk_sys::gtk_file_chooser_dialog_new(
                        title.to_glib_none().0,
                        parent.map(|p| p.as_ref()).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(),
                        ptr::null::<c_char>(),
                    )
                },
                3 => {
                    gtk_sys::gtk_file_chooser_dialog_new(
                        title.to_glib_none().0,
                        parent.map(|p| p.as_ref()).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()))
                }
            }).unsafe_cast()
        }
    }
}