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
// Take a look at the license at the top of the repository in the LICENSE file.

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

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`][crate::FileChooserDialog]. This function is analogous to
    /// `gtk_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`][crate::FileChooserDialog]
    #[doc(alias = "gtk_file_chooser_dialog_new")]
    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.map(|p| p.as_ref()).to_glib_none().0,
                action.into_glib(),
                ptr::null::<c_char>(),
            ))
            .unsafe_cast()
        }
    }

    #[doc(alias = "gtk_file_chooser_dialog_new")]
    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.map(|p| p.as_ref()).to_glib_none().0,
                        action.into_glib(),
                        ptr::null::<c_char>()
                    )
                },
                1 => {
                    ffi::gtk_file_chooser_dialog_new(
                        title.to_glib_none().0,
                        parent.map(|p| p.as_ref()).to_glib_none().0,
                        action.into_glib(),
                        buttons[0].0.to_glib_none().0,
                        buttons[0].1.into_glib(),
                        ptr::null::<c_char>(),
                    )
                },
                2 => {
                    ffi::gtk_file_chooser_dialog_new(
                        title.to_glib_none().0,
                        parent.map(|p| p.as_ref()).to_glib_none().0,
                        action.into_glib(),
                        buttons[0].0.to_glib_none().0,
                        buttons[0].1.into_glib(),
                        (buttons[1].0.to_glib_none() as Stash<*const c_char, str>).0,
                        buttons[1].1.into_glib(),
                        ptr::null::<c_char>(),
                    )
                },
                3 => {
                    ffi::gtk_file_chooser_dialog_new(
                        title.to_glib_none().0,
                        parent.map(|p| p.as_ref()).to_glib_none().0,
                        action.into_glib(),
                        buttons[0].0.to_glib_none().0,
                        buttons[0].1.into_glib(),
                        (buttons[1].0.to_glib_none() as Stash<*const c_char, str>).0,
                        buttons[1].1.into_glib(),
                        (buttons[2].0.to_glib_none() as Stash<*const c_char, str>).0,
                        buttons[2].1.into_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!("`FileChooserDialog::with_buttons` does not support 4+ buttons, received {}", buttons.len())
                }
            }).unsafe_cast()
        }
    }
}