|
5 | 5 | use ffi;
|
6 | 6 | use glib::translate::*;
|
7 | 7 | use glib::object::{Downcast, IsA};
|
| 8 | +use libc::c_char; |
8 | 9 | use std::ptr;
|
9 | 10 | use FileChooserAction;
|
10 | 11 | use FileChooserDialog;
|
| 12 | +use ResponseType; |
11 | 13 | use Widget;
|
12 | 14 | use Window;
|
13 | 15 |
|
14 | 16 | impl FileChooserDialog {
|
15 |
| - pub fn new<T: IsA<Window>>(title: Option<&str>, parent: Option<&T>, action: FileChooserAction) |
16 |
| - -> FileChooserDialog { |
| 17 | + // TODO: Keep the other constructor with buttons support as the only constructor (this one was |
| 18 | + // left for compatibility) and rename it to `new` for consistency. |
| 19 | + pub fn new<T: IsA<Window>>(title: Option<&str>, parent: Option<&T>, action: FileChooserAction) -> FileChooserDialog { |
17 | 20 | assert_initialized_main_thread!();
|
18 | 21 | unsafe {
|
19 |
| - Widget::from_glib_none( |
20 |
| - ffi::gtk_file_chooser_dialog_new(title.to_glib_none().0, parent.to_glib_none().0, |
21 |
| - action.to_glib(), ptr::null_mut())) |
22 |
| - .downcast_unchecked() |
| 22 | + Widget::from_glib_none(ffi::gtk_file_chooser_dialog_new( |
| 23 | + title.to_glib_none().0, |
| 24 | + parent.to_glib_none().0, |
| 25 | + action.to_glib(), |
| 26 | + ptr::null::<c_char>() |
| 27 | + )).downcast_unchecked() |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + pub fn with_buttons<T: IsA<Window>>(title: Option<&str>, parent: Option<&T>, action: FileChooserAction, buttons: &[(&str, ResponseType)]) -> FileChooserDialog { |
| 32 | + assert_initialized_main_thread!(); |
| 33 | + unsafe { |
| 34 | + Widget::from_glib_none(match buttons.len() { |
| 35 | + 0 => { |
| 36 | + ffi::gtk_file_chooser_dialog_new( |
| 37 | + title.to_glib_none().0, |
| 38 | + parent.to_glib_none().0, |
| 39 | + action.to_glib(), |
| 40 | + ptr::null::<c_char>() |
| 41 | + ) |
| 42 | + }, |
| 43 | + 1 => { |
| 44 | + ffi::gtk_file_chooser_dialog_new( |
| 45 | + title.to_glib_none().0, |
| 46 | + parent.to_glib_none().0, |
| 47 | + action.to_glib(), |
| 48 | + buttons[0].0.to_glib_none().0, |
| 49 | + buttons[0].1.to_glib(), |
| 50 | + ptr::null::<c_char>(), |
| 51 | + ) |
| 52 | + }, |
| 53 | + 2 => { |
| 54 | + ffi::gtk_file_chooser_dialog_new( |
| 55 | + title.to_glib_none().0, |
| 56 | + parent.to_glib_none().0, |
| 57 | + action.to_glib(), |
| 58 | + buttons[0].0.to_glib_none().0, |
| 59 | + buttons[0].1.to_glib(), |
| 60 | + (buttons[1].0.to_glib_none() as Stash<*const c_char, str>).0, |
| 61 | + buttons[1].1.to_glib(), |
| 62 | + ptr::null::<c_char>(), |
| 63 | + ) |
| 64 | + }, |
| 65 | + 3 => { |
| 66 | + ffi::gtk_file_chooser_dialog_new( |
| 67 | + title.to_glib_none().0, |
| 68 | + parent.to_glib_none().0, |
| 69 | + action.to_glib(), |
| 70 | + buttons[0].0.to_glib_none().0, |
| 71 | + buttons[0].1.to_glib(), |
| 72 | + (buttons[1].0.to_glib_none() as Stash<*const c_char, str>).0, |
| 73 | + buttons[1].1.to_glib(), |
| 74 | + (buttons[2].0.to_glib_none() as Stash<*const c_char, str>).0, |
| 75 | + buttons[2].1.to_glib(), |
| 76 | + ptr::null::<c_char>(), |
| 77 | + ) |
| 78 | + }, |
| 79 | + _ => { |
| 80 | + // TODO: Support arbitrary number of buttons once variadic functions are supported. |
| 81 | + // See: https://github.com/rust-lang/rust/issues/44930 |
| 82 | + panic!(format!("`FileChooserDialog::with_buttons` does not support 4+ buttons, received {}", buttons.len())) |
| 83 | + } |
| 84 | + }).downcast_unchecked() |
23 | 85 | }
|
24 | 86 | }
|
25 | 87 | }
|
0 commit comments