-
Notifications
You must be signed in to change notification settings - Fork 60
/
dialog_ffi.rs
286 lines (238 loc) · 8.29 KB
/
dialog_ffi.rs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use super::super::AsGtkDialog;
use crate::FileDialog;
use gtk_sys::GtkFileChooserNative;
use std::{
ffi::{CStr, CString},
ops::Deref,
path::{Path, PathBuf},
ptr,
};
#[repr(i32)]
pub enum GtkFileChooserAction {
Open = 0,
Save = 1,
SelectFolder = 2,
// CreateFolder = 3,
}
pub struct GtkFileDialog {
pub ptr: *mut GtkFileChooserNative,
}
impl GtkFileDialog {
fn new(title: &str, action: GtkFileChooserAction) -> Self {
let title = CString::new(title).unwrap();
let ptr = unsafe {
let dialog = gtk_sys::gtk_file_chooser_native_new(
title.as_ptr(),
ptr::null_mut(),
action as i32,
// passing null for the texts will use the default text, which has full support for i18n
std::ptr::null(),
std::ptr::null(),
);
dialog as _
};
Self { ptr }
}
fn add_filters(&mut self, filters: &[crate::file_dialog::Filter]) {
for f in filters.iter() {
if let Ok(name) = CString::new(f.name.as_str()) {
unsafe {
let filter = gtk_sys::gtk_file_filter_new();
let paterns: Vec<_> = f
.extensions
.iter()
.filter_map(|e| CString::new(format!("*.{}", e)).ok())
.collect();
gtk_sys::gtk_file_filter_set_name(filter, name.as_ptr());
for p in paterns.iter() {
gtk_sys::gtk_file_filter_add_pattern(filter, p.as_ptr());
}
gtk_sys::gtk_file_chooser_add_filter(self.ptr as _, filter);
}
}
}
}
fn set_file_name(&self, name: Option<&str>) {
if let Some(name) = name {
if let Ok(name) = CString::new(name) {
unsafe {
gtk_sys::gtk_file_chooser_set_filename(self.ptr as _, name.as_ptr());
}
}
}
}
fn set_current_name(&self, name: Option<&str>) {
if let Some(name) = name {
if let Ok(name) = CString::new(name) {
unsafe {
gtk_sys::gtk_file_chooser_set_current_name(self.ptr as _, name.as_ptr());
}
}
}
}
fn set_path(&self, path: Option<&Path>) {
if let Some(path) = path {
if let Some(path) = path.to_str() {
if let Ok(path) = CString::new(path) {
unsafe {
gtk_sys::gtk_file_chooser_set_current_folder(self.ptr as _, path.as_ptr());
}
}
}
}
}
pub fn get_result(&self) -> Option<PathBuf> {
let cstr = unsafe {
let chosen_filename = gtk_sys::gtk_file_chooser_get_filename(self.ptr as _);
CStr::from_ptr(chosen_filename).to_str()
};
if let Ok(cstr) = cstr {
Some(PathBuf::from(cstr.to_owned()))
} else {
None
}
}
pub fn get_results(&self) -> Vec<PathBuf> {
#[derive(Debug)]
struct FileList(*mut glib_sys::GSList);
impl Iterator for FileList {
type Item = glib_sys::GSList;
fn next(&mut self) -> Option<Self::Item> {
let curr_ptr = self.0;
if !curr_ptr.is_null() {
let curr = unsafe { *curr_ptr };
self.0 = curr.next;
Some(curr)
} else {
None
}
}
}
let chosen_filenames =
unsafe { gtk_sys::gtk_file_chooser_get_filenames(self.ptr as *mut _) };
let paths: Vec<PathBuf> = FileList(chosen_filenames)
.filter_map(|item| {
let cstr = unsafe { CStr::from_ptr(item.data as _).to_str() };
if let Ok(cstr) = cstr {
Some(PathBuf::from(cstr.to_owned()))
} else {
None
}
})
.collect();
paths
}
pub fn run(&self) -> i32 {
unsafe { gtk_sys::gtk_native_dialog_run(self.ptr as *mut _) }
}
}
impl GtkFileDialog {
pub fn build_pick_file(opt: &FileDialog) -> Self {
let mut dialog = GtkFileDialog::new(
opt.title.as_deref().unwrap_or("Open File"),
GtkFileChooserAction::Open,
);
dialog.add_filters(&opt.filters);
dialog.set_path(opt.starting_directory.as_deref());
if let (Some(mut path), Some(file_name)) =
(opt.starting_directory.to_owned(), opt.file_name.as_deref())
{
path.push(file_name);
dialog.set_file_name(path.deref().to_str());
} else {
dialog.set_file_name(opt.file_name.as_deref());
}
dialog
}
pub fn build_save_file(opt: &FileDialog) -> Self {
let mut dialog = GtkFileDialog::new(
opt.title.as_deref().unwrap_or("Save File"),
GtkFileChooserAction::Save,
);
unsafe { gtk_sys::gtk_file_chooser_set_do_overwrite_confirmation(dialog.ptr as _, 1) };
dialog.add_filters(&opt.filters);
dialog.set_path(opt.starting_directory.as_deref());
if let (Some(mut path), Some(file_name)) =
(opt.starting_directory.to_owned(), opt.file_name.as_deref())
{
path.push(file_name);
if path.exists() {
// the user edited an existing document
dialog.set_file_name(path.deref().to_str());
} else {
// the user just created a new document
dialog.set_current_name(opt.file_name.as_deref());
}
} else {
// the user just created a new document
dialog.set_current_name(opt.file_name.as_deref());
}
dialog
}
pub fn build_pick_folder(opt: &FileDialog) -> Self {
let dialog = GtkFileDialog::new(
opt.title.as_deref().unwrap_or("Select Folder"),
GtkFileChooserAction::SelectFolder,
);
dialog.set_path(opt.starting_directory.as_deref());
if let (Some(mut path), Some(file_name)) =
(opt.starting_directory.to_owned(), opt.file_name.as_deref())
{
path.push(file_name);
dialog.set_file_name(path.deref().to_str());
} else {
dialog.set_file_name(opt.file_name.as_deref());
}
dialog
}
pub fn build_pick_folders(opt: &FileDialog) -> Self {
let dialog = GtkFileDialog::new(
opt.title.as_deref().unwrap_or("Select Folder"),
GtkFileChooserAction::SelectFolder,
);
unsafe { gtk_sys::gtk_file_chooser_set_select_multiple(dialog.ptr as _, 1) };
dialog.set_path(opt.starting_directory.as_deref());
if let (Some(mut path), Some(file_name)) =
(opt.starting_directory.to_owned(), opt.file_name.as_deref())
{
path.push(file_name);
dialog.set_file_name(path.deref().to_str());
} else {
dialog.set_file_name(opt.file_name.as_deref());
}
dialog
}
pub fn build_pick_files(opt: &FileDialog) -> Self {
let mut dialog = GtkFileDialog::new(
opt.title.as_deref().unwrap_or("Open File"),
GtkFileChooserAction::Open,
);
unsafe { gtk_sys::gtk_file_chooser_set_select_multiple(dialog.ptr as _, 1) };
dialog.add_filters(&opt.filters);
dialog.set_path(opt.starting_directory.as_deref());
if let (Some(mut path), Some(file_name)) =
(opt.starting_directory.to_owned(), opt.file_name.as_deref())
{
path.push(file_name);
dialog.set_file_name(path.deref().to_str());
} else {
dialog.set_file_name(opt.file_name.as_deref());
}
dialog
}
}
impl AsGtkDialog for GtkFileDialog {
fn gtk_dialog_ptr(&self) -> *mut gtk_sys::GtkDialog {
self.ptr as *mut _
}
unsafe fn show(&self) {
gtk_sys::gtk_native_dialog_show(self.ptr as *mut _);
}
}
impl Drop for GtkFileDialog {
fn drop(&mut self) {
unsafe {
gtk_sys::gtk_native_dialog_destroy(self.ptr as _);
}
}
}