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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
use crate::enums::{Color, Font};
use crate::prelude::*;
use crate::utils::FlString;
use fltk_sys::dialog::*;
use std::{
    ffi::{CStr, CString},
    mem,
    os::raw,
    path::{Path, PathBuf},
};

/// Color modes to be used with the color chooser
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ColorMode {
    /// Rgb color mode
    Rgb = 0,
    /// Byte color mode
    Byte = 1,
    /// Hex color mode
    Hex = 2,
    /// Hsv color mode
    Hsv = 3,
}

/// FLTK's NativeFileChooser
#[derive(Debug)]
pub struct FileDialog {
    inner: *mut Fl_Native_File_Chooser,
}

/// FLTK's NativeFileChooser
pub type NativeFileChooser = FileDialog;

/// Defines the type of dialog, which can be changed dynamically using the `set_type()` method
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FileDialogType {
    /// Browse file
    BrowseFile = 0,
    /// Browse dir
    BrowseDir,
    /// Browse multiple files
    BrowseMultiFile,
    /// Browse multiple dirs
    BrowseMultiDir,
    /// Browse save file
    BrowseSaveFile,
    /// Browse save directory
    BrowseSaveDir,
}

crate::macros::widget::impl_widget_type!(FileDialogType);

/// Alias for `NativeFileChooserType`
pub type NativeFileChooserType = FileDialogType;

bitflags::bitflags! {
    /// Defines the File dialog options, which can be set using the `set_option()` method.
    #[derive(Debug, Copy, Clone, PartialEq, Eq)]
    pub struct FileDialogOptions: i32 {
        /// No options
        const NoOptions = 0;
        /// Confirm on save as
        const SaveAsConfirm = 1;
        /// New folder option
        const NewFolder = 2;
        /// Enable preview
        const Preview = 4;
        /// Use extension filter
        const UseFilterExt = 8;
    }
}

/// Alias to `FileDialogOptions`
pub type NativeFileChooserOptions = FileDialogOptions;

/// Rusult of try_show
#[derive(Debug, Copy, Clone)]
pub enum FileDialogAction {
    /// Cancelled operation
    Cancelled,
    /// User chose a file/dir
    Success,
}

/// Alias to `FileDialogAction`
pub type NativeFileChooserAction = FileDialogAction;

impl FileDialog {
    /// Creates an new file dialog
    pub fn new(op: FileDialogType) -> FileDialog {
        unsafe {
            let file_dialog = Fl_Native_File_Chooser_new(mem::transmute(op));
            assert!(!file_dialog.is_null());
            FileDialog { inner: file_dialog }
        }
    }

    /// Returns the chosen file name
    pub fn filename(&self) -> PathBuf {
        assert!(!self.inner.is_null());
        unsafe {
            let cnt = Fl_Native_File_Chooser_count(self.inner);
            if cnt == 0 {
                return PathBuf::new();
            }
            let x = Fl_Native_File_Chooser_filenames(self.inner, 0);
            PathBuf::from(
                CStr::from_ptr(x as *mut raw::c_char)
                    .to_string_lossy()
                    .to_string(),
            )
        }
    }

    /// Returns the chosen file names
    pub fn filenames(&self) -> Vec<PathBuf> {
        assert!(!self.inner.is_null());
        unsafe {
            let cnt = Fl_Native_File_Chooser_count(self.inner);
            let mut names: Vec<PathBuf> = vec![];
            for i in 0..cnt {
                let x = Fl_Native_File_Chooser_filenames(self.inner, i);
                names.push(PathBuf::from(
                    CStr::from_ptr(x as *mut raw::c_char)
                        .to_string_lossy()
                        .to_string(),
                ));
            }
            names
        }
    }

    /// Returns the preset directory
    pub fn directory(&self) -> PathBuf {
        assert!(!self.inner.is_null());
        unsafe {
            let x = Fl_Native_File_Chooser_directory(self.inner);
            if x.is_null() {
                PathBuf::from("")
            } else {
                PathBuf::from(
                    CStr::from_ptr(x as *mut raw::c_char)
                        .to_string_lossy()
                        .to_string(),
                )
            }
        }
    }

    /// Sets the starting directory
    /// # Errors
    /// Errors on non-existent path
    pub fn set_directory<P: AsRef<Path>>(&mut self, dir: &P) -> Result<(), FltkError> {
        assert!(!self.inner.is_null());
        self.set_directory_(dir.as_ref())
    }

    fn set_directory_(&mut self, dir: &Path) -> Result<(), FltkError> {
        assert!(!self.inner.is_null());
        let dir = CString::new(dir.to_str().ok_or_else(|| {
            FltkError::Unknown(String::from("Failed to convert path to string"))
        })?)?;
        unsafe { Fl_Native_File_Chooser_set_directory(self.inner, dir.as_ptr()) }
        Ok(())
    }

    /// Shows the file dialog
    pub fn try_show(&mut self) -> Result<FileDialogAction, FltkError> {
        assert!(!self.inner.is_null());
        unsafe {
            match Fl_Native_File_Chooser_show(self.inner) {
                0 => Ok(FileDialogAction::Success),
                -1 => Err(FltkError::Unknown(self.error_message().unwrap())),
                1 => Ok(FileDialogAction::Cancelled),
                _ => unreachable!(),
            }
        }
    }

    /// Shows the file dialog
    pub fn show(&mut self) {
        assert!(!self.inner.is_null());
        unsafe {
            Fl_Native_File_Chooser_show(self.inner);
        }
    }

    /// Sets the option for the dialog
    pub fn set_option(&mut self, opt: FileDialogOptions) {
        assert!(!self.inner.is_null());
        unsafe { Fl_Native_File_Chooser_set_option(self.inner, opt.bits()) }
    }

    /// Sets the type for the dialog
    pub fn set_type(&mut self, op: FileDialogType) {
        assert!(!self.inner.is_null());
        unsafe { Fl_Native_File_Chooser_set_type(self.inner, op as i32) }
    }

    /// Sets the title for the dialog
    pub fn set_title(&mut self, title: &str) {
        assert!(!self.inner.is_null());
        let title = CString::safe_new(title);
        unsafe { Fl_Native_File_Chooser_set_title(self.inner, title.as_ptr()) }
    }

    /// Sets the filter for the dialog, can be:
    /// A single wildcard (e.g. `"*.txt"`).
    /// Multiple wildcards (e.g. `"*.{cxx,h,H}"`).
    /// A descriptive name followed by a `\t` and a wildcard (e.g. `"Text Files\t*.txt"`).
    /// A list of separate wildcards with a `\n` between each (e.g. `"*.{cxx,H}\n*.txt"`).
    /// A list of descriptive names and wildcards (e.g. `"C++ Files\t*.{cxx,H}\nTxt Files\t*.txt"`)
    pub fn set_filter(&mut self, f: &str) {
        assert!(!self.inner.is_null());
        let f = CString::safe_new(f);
        unsafe { Fl_Native_File_Chooser_set_filter(self.inner, f.as_ptr()) }
    }

    /// Sets the default filename for the dialog
    pub fn set_preset_file(&mut self, f: &str) {
        assert!(!self.inner.is_null());
        let f = CString::safe_new(f);
        unsafe { Fl_Native_File_Chooser_set_preset_file(self.inner, f.as_ptr()) }
    }

    /// returns the error message from the file dialog
    pub fn error_message(&self) -> Option<String> {
        assert!(!self.inner.is_null());
        unsafe {
            let err_msg = Fl_Native_File_Chooser_errmsg(self.inner);
            if err_msg.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(err_msg as *mut raw::c_char)
                        .to_string_lossy()
                        .to_string(),
                )
            }
        }
    }
}

impl Drop for FileDialog {
    fn drop(&mut self) {
        if !self.inner.is_null() {
            unsafe { Fl_Native_File_Chooser_delete(self.inner) }
            self.inner = std::ptr::null_mut();
        }
    }
}

/// Displays a message box
pub fn message(x: i32, y: i32, txt: &str) {
    unsafe {
        let txt = CString::safe_new(txt);
        Fl_message(x, y, txt.as_ptr())
    }
}

/// Displays an alert box
pub fn alert(x: i32, y: i32, txt: &str) {
    unsafe {
        let txt = CString::safe_new(txt);
        Fl_alert(x, y, txt.as_ptr())
    }
}

#[deprecated(since = "1.3.1", note = "please use `choice2` instead")]
/// Displays a choice box with up to three choices. Choosing a value returns its index from the arguments
pub fn choice(x: i32, y: i32, txt: &str, b0: &str, b1: &str, b2: &str) -> i32 {
    unsafe {
        let txt = CString::safe_new(txt);
        let b0 = CString::safe_new(b0);
        let b1 = CString::safe_new(b1);
        let b2 = CString::safe_new(b2);
        Fl_choice(x, y, txt.as_ptr(), b0.as_ptr(), b1.as_ptr(), b2.as_ptr())
    }
}

/// Displays a choice box with up to three choices.
/// Closing the dialog returns None. Choosing a value returns its index from the arguments.
pub fn choice2(x: i32, y: i32, txt: &str, b0: &str, b1: &str, b2: &str) -> Option<i32> {
    unsafe {
        let txt = CString::safe_new(txt);
        let b0 = CString::safe_new(b0);
        let b1 = CString::safe_new(b1);
        let b2 = CString::safe_new(b2);
        let ret = Fl_choice_n(x, y, txt.as_ptr(), b0.as_ptr(), b1.as_ptr(), b2.as_ptr());
        if ret < 0 {
            None
        } else {
            Some(ret)
        }
    }
}

/// Displays an input box, which returns the inputted string.
/// Can be used for gui io
pub fn input(x: i32, y: i32, txt: &str, deflt: &str) -> Option<String> {
    unsafe {
        let temp = CString::safe_new(deflt);
        let txt = CString::safe_new(txt);
        let x = Fl_input(x, y, txt.as_ptr(), temp.as_ptr());
        if x.is_null() {
            None
        } else {
            Some(
                CStr::from_ptr(x as *const raw::c_char)
                    .to_string_lossy()
                    .to_string(),
            )
        }
    }
}

/// Shows an input box, but with hidden string
pub fn password(x: i32, y: i32, txt: &str, deflt: &str) -> Option<String> {
    unsafe {
        let temp = CString::safe_new(deflt);
        let txt = CString::safe_new(txt);
        let x = Fl_password(x, y, txt.as_ptr(), temp.as_ptr());
        if x.is_null() {
            None
        } else {
            Some(
                CStr::from_ptr(x as *const raw::c_char)
                    .to_string_lossy()
                    .to_string(),
            )
        }
    }
}

/// Displays a message box, the dialog is positioned at the pointer hotspot
pub fn message_default(txt: &str) {
    unsafe {
        let txt = CString::safe_new(txt);
        Fl_message2(txt.as_ptr())
    }
}

/// Displays an alert box, the dialog is positioned at the pointer hotspot
pub fn alert_default(txt: &str) {
    unsafe {
        let txt = CString::safe_new(txt);
        Fl_alert2(txt.as_ptr())
    }
}

#[deprecated(since = "1.3.1", note = "please use `choice2_default` instead")]
/// Displays a choice box with up to three choices.
/// The dialog is positioned at the pointer hotspot
pub fn choice_default(txt: &str, b0: &str, b1: &str, b2: &str) -> i32 {
    unsafe {
        let txt = CString::safe_new(txt);
        let b0 = CString::safe_new(b0);
        let b1 = CString::safe_new(b1);
        let b2 = CString::safe_new(b2);
        Fl_choice2(txt.as_ptr(), b0.as_ptr(), b1.as_ptr(), b2.as_ptr())
    }
}

/// Displays a choice box with up to three choices.
/// An empty choice will not be shown. Closing the dialog returns None. Choosing a value returns its index from the arguments.
/// The dialog is positioned at the pointer hotspot
pub fn choice2_default(txt: &str, b0: &str, b1: &str, b2: &str) -> Option<i32> {
    unsafe {
        let txt = CString::safe_new(txt);
        let b0 = CString::safe_new(b0);
        let b1 = CString::safe_new(b1);
        let b2 = CString::safe_new(b2);
        let ret = Fl_choice2_n(txt.as_ptr(), b0.as_ptr(), b1.as_ptr(), b2.as_ptr());
        if ret < 0 {
            None
        } else {
            Some(ret)
        }
    }
}

/// Displays an input box, which returns the inputted string.
/// Can be used for gui io.
/// The dialog is positioned at the pointer hotspot
pub fn input_default(txt: &str, deflt: &str) -> Option<String> {
    unsafe {
        let temp = CString::safe_new(deflt);
        let txt = CString::safe_new(txt);
        let x = Fl_input2(txt.as_ptr(), temp.as_ptr());
        if x.is_null() {
            None
        } else {
            Some(
                CStr::from_ptr(x as *const raw::c_char)
                    .to_string_lossy()
                    .to_string(),
            )
        }
    }
}

/// Shows an input box, but with hidden string.
/// The dialog is positioned at the pointer hotspot
pub fn password_default(txt: &str, deflt: &str) -> Option<String> {
    unsafe {
        let temp = CString::safe_new(deflt);
        let txt = CString::safe_new(txt);
        let x = Fl_password2(txt.as_ptr(), temp.as_ptr());
        if x.is_null() {
            None
        } else {
            Some(
                CStr::from_ptr(x as *const raw::c_char)
                    .to_string_lossy()
                    .to_string(),
            )
        }
    }
}

/// Creates a help dialog
#[derive(Debug)]
pub struct HelpDialog {
    inner: *mut Fl_Help_Dialog,
}

impl Default for HelpDialog {
    fn default() -> Self {
        unsafe {
            let help_dialog = Fl_Help_Dialog_new();
            assert!(!help_dialog.is_null());
            HelpDialog { inner: help_dialog }
        }
    }
}

impl HelpDialog {
    /// Creates a new Help dialog with position(x, y) and size(w, h)
    pub fn new(x: i32, y: i32, w: i32, h: i32) -> HelpDialog {
        let mut temp = HelpDialog::default();
        temp.resize(x, y, w, h);
        temp
    }

    /// Hides the help dialog
    pub fn hide(&mut self) {
        unsafe { Fl_Help_Dialog_hide(self.inner) }
    }

    /// Loads a file for the help dialog
    /// # Errors
    /// Errors on non-existent path
    pub fn load<P: AsRef<Path>>(&mut self, file: P) -> Result<(), FltkError> {
        self.load_(file.as_ref())
    }

    fn load_(&mut self, file: &Path) -> Result<(), FltkError> {
        let f = file
            .to_str()
            .ok_or_else(|| FltkError::Unknown(String::from("Failed to convert path to string")))?;
        let f = CString::new(f)?;
        unsafe {
            match Fl_Help_Dialog_load(self.inner, f.as_ptr()) {
                0 => Ok(()),
                _ => Err(FltkError::Internal(FltkErrorKind::ResourceNotFound)),
            }
        }
    }

    /// Sets the position of the help dialog
    pub fn position(&mut self, x: i32, y: i32) {
        unsafe { Fl_Help_Dialog_position(self.inner, x, y) }
    }

    /// Resizes the help dialog
    pub fn resize(&mut self, x: i32, y: i32, w: i32, h: i32) {
        unsafe { Fl_Help_Dialog_resize(self.inner, x, y, w, h) }
    }

    /// Shows the help dialog
    pub fn show(&mut self) {
        unsafe { Fl_Help_Dialog_show(self.inner) }
    }

    /// Sets the text size
    pub fn set_text_size(&mut self, s: i32) {
        unsafe { Fl_Help_Dialog_set_text_size(self.inner, s) }
    }

    /// Returns the text size
    pub fn text_size(&self) -> i32 {
        unsafe { Fl_Help_Dialog_text_size(self.inner) }
    }

    /// Sets the value of the help dialog
    pub fn set_value(&mut self, f: &str) {
        let f = CString::safe_new(f);
        unsafe { Fl_Help_Dialog_set_value(self.inner, f.as_ptr()) }
    }

    /// Returns the value of the help dialog
    pub fn value(&self) -> Option<String> {
        unsafe {
            let val = Fl_Help_Dialog_value(self.inner);
            if val.is_null() {
                None
            } else {
                Some(CStr::from_ptr(val).to_string_lossy().to_string())
            }
        }
    }

    /// Returns whether the help dialog is visible
    pub fn visible(&self) -> bool {
        unsafe { Fl_Help_Dialog_visible(self.inner) != 0 }
    }

    /// Returns whether the help dialog is visible
    pub fn shown(&self) -> bool {
        unsafe { Fl_Help_Dialog_visible(self.inner) != 0 }
    }

    /// Returns the width of the help dialog
    pub fn width(&self) -> i32 {
        unsafe { Fl_Help_Dialog_w(self.inner) }
    }

    /// Returns the height of the help dialog
    pub fn height(&self) -> i32 {
        unsafe { Fl_Help_Dialog_h(self.inner) }
    }

    /// Returns the width of the help dialog
    pub fn w(&self) -> i32 {
        unsafe { Fl_Help_Dialog_w(self.inner) }
    }

    /// Returns the height of the help dialog
    pub fn h(&self) -> i32 {
        unsafe { Fl_Help_Dialog_h(self.inner) }
    }

    /// Returns the x position of the help dialog
    pub fn x(&self) -> i32 {
        unsafe { Fl_Help_Dialog_x(self.inner) }
    }

    /// Returns the y position of the help dialog
    pub fn y(&self) -> i32 {
        unsafe { Fl_Help_Dialog_y(self.inner) }
    }
}

impl Drop for HelpDialog {
    fn drop(&mut self) {
        unsafe { Fl_Help_Dialog_delete(self.inner) }
    }
}

/// Defines the type of beep to be passed to the beep function
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum BeepType {
    /// Default beep
    Default = 0,
    /// Message beep
    Message,
    /// Error beep
    Error,
    /// Question beep
    Question,
    /// Password sound
    Password,
    /// Notification sound
    Notification,
}

/// Emits a beep
pub fn beep(tp: BeepType) {
    unsafe { Fl_beep(tp as i32) }
}

/**
    FLTK's own `FileChooser`. Which differs for the Native `FileDialog`
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let app = app::App::default();
        let mut win = window::Window::default().with_size(900, 300);
        let mut chooser = dialog::FileChooser::new(
            ".",                    // directory
            "*",                    // filter or pattern
            dialog::FileChooserType::Multi, // chooser type
            "Title Of Chooser",     // title
        );
        chooser.show();
        chooser.window().set_pos(300, 300);
        // Block until user picks something.
        //     (The other way to do this is to use a callback())
        //
        while chooser.shown() {
            app::wait();
        }
        // User hit cancel?
        if chooser.value(1).is_none() {
            println!("(User hit 'Cancel')");
            return;
        }
        // Print what the user picked
        println!("--------------------");
        println!("DIRECTORY: '{}'", chooser.directory().unwrap());
        println!("    VALUE: '{}'", chooser.value(1).unwrap()); // value starts at 1!
        println!("    COUNT: {} files selected", chooser.count());
        // Multiple files? Show all of them
        if chooser.count() > 1 {
            for t in 1..=chooser.count() {
                println!(" VALUE[{}]: '{}'", t, chooser.value(t).unwrap());
            }
        }
        win.end();
        win.show();
        app.run().unwrap();
    }
    ```
*/
pub struct FileChooser {
    inner: *mut Fl_File_Chooser,
}

bitflags::bitflags! {
    /// The types of FileChooser
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct FileChooserType: i32 {
        /// Single file
        const Single = 0;
        /// Multiple files
        const Multi = 1;
        /// Allow creation of file/dir
        const Create = 2;
        /// Directory
        const Directory = 4;
    }
}

impl FileChooser {
    /// Instantiates a new `FileChooser`
    pub fn new<P: AsRef<Path>>(
        dir: P,
        pattern: &str,
        typ: FileChooserType,
        title: &str,
    ) -> FileChooser {
        Self::new_(dir.as_ref(), pattern, typ, title)
    }

    fn new_(dir: &Path, pattern: &str, typ: FileChooserType, title: &str) -> FileChooser {
        let dir = if let Some(dir) = dir.to_str() {
            dir
        } else {
            "."
        };
        let dir = CString::safe_new(dir);
        let pattern = CString::safe_new(pattern);
        let title = CString::safe_new(title);
        unsafe {
            let ptr = Fl_File_Chooser_new(
                dir.as_ptr(),
                pattern.as_ptr(),
                typ.bits(),
                title.into_raw() as _,
            );
            assert!(!ptr.is_null());
            FileChooser { inner: ptr }
        }
    }
    /// Deletes a `FileChooser`
    /// # Safety
    /// Can invalidate the underlying pointer
    pub unsafe fn delete(dlg: Self) {
        Fl_File_Chooser_delete(dlg.inner)
    }

    /// Gets the new button of the `FileChooser`
    pub fn new_button(&self) -> Option<impl ButtonExt> {
        assert!(!self.inner.is_null());
        unsafe {
            let ptr = Fl_File_Chooser_newButton(self.inner);
            if ptr.is_null() {
                None
            } else {
                Some(crate::button::Button::from_widget_ptr(ptr as *mut _))
            }
        }
    }

    /// Gets the preview button of the `FileChooser`
    pub fn preview_button(&self) -> Option<impl ButtonExt> {
        assert!(!self.inner.is_null());
        unsafe {
            let ptr = Fl_File_Chooser_previewButton(self.inner);
            if ptr.is_null() {
                None
            } else {
                Some(crate::button::CheckButton::from_widget_ptr(
                    ptr as *mut fltk_sys::widget::Fl_Widget,
                ))
            }
        }
    }

    /// Gets the show hidden button of the `FileChooser`
    pub fn show_hidden_button(&self) -> Option<impl ButtonExt> {
        assert!(!self.inner.is_null());
        unsafe {
            let ptr = Fl_File_Chooser_showHiddenButton(self.inner);
            if ptr.is_null() {
                None
            } else {
                Some(crate::button::CheckButton::from_widget_ptr(
                    ptr as *mut fltk_sys::widget::Fl_Widget,
                ))
            }
        }
    }

    /// Sets the callback of the `FileChooser`
    pub fn set_callback<F: FnMut(&mut Self) + 'static>(&mut self, cb: F) {
        assert!(!self.inner.is_null());
        unsafe {
            unsafe extern "C" fn shim(arg1: *mut Fl_File_Chooser, data: *mut raw::c_void) {
                let mut wid = FileChooser { inner: arg1 };
                let a: *mut Box<dyn FnMut(&mut FileChooser)> =
                    data as *mut Box<dyn FnMut(&mut FileChooser)>;
                let f: &mut (dyn FnMut(&mut FileChooser)) = &mut **a;
                let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&mut wid)));
            }
            let _old_data = self.user_data();
            let a: *mut Box<dyn FnMut(&mut Self)> = Box::into_raw(Box::new(Box::new(cb)));
            let data: *mut raw::c_void = a as *mut raw::c_void;
            let callback: Option<
                unsafe extern "C" fn(arg1: *mut Fl_File_Chooser, data: *mut raw::c_void),
            > = Some(shim);
            Fl_File_Chooser_set_callback(self.inner, callback, data)
        }
    }

    /// Sets the color of the `FileChooser`
    pub fn set_color(&mut self, c: Color) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_color(self.inner, c.bits()) }
    }

    /// Gets the color of the `FileChooser`
    pub fn color(&self) -> Color {
        assert!(!self.inner.is_null());
        unsafe { mem::transmute(Fl_File_Chooser_color(self.inner)) }
    }

    /// Gets the count of chosen items
    pub fn count(&self) -> i32 {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_count(self.inner) }
    }

    /// Sets the directory of the `FileChooser`
    pub fn set_directory<P: AsRef<Path>>(&mut self, dir: P) {
        self.set_directory_(dir.as_ref())
    }

    fn set_directory_(&mut self, dir: &Path) {
        assert!(!self.inner.is_null());
        if let Some(dir) = dir.to_str() {
            let dir = CString::safe_new(dir);
            unsafe { Fl_File_Chooser_set_directory(self.inner, dir.as_ptr()) }
        }
    }

    /// Gets the directory of the `FileChooser`
    pub fn directory(&self) -> Option<String> {
        assert!(!self.inner.is_null());
        unsafe {
            let ptr = Fl_File_Chooser_directory(self.inner);
            if ptr.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(ptr as *mut raw::c_char)
                        .to_string_lossy()
                        .to_string(),
                )
            }
        }
    }

    /// Sets the filter for the dialog, can be:
    /// Multiple patterns can be used by separating them with tabs, like "*.jpg\t*.png\t*.gif\t*".
    /// In addition, you can provide human-readable labels with the patterns inside parenthesis,
    /// like "JPEG Files (*.jpg)\tPNG Files (*.png)\tGIF Files (*.gif)\tAll Files (*)
    /// And "Rust Files (*.{rs,txt,toml})"
    pub fn set_filter(&mut self, pattern: &str) {
        assert!(!self.inner.is_null());
        let pattern = CString::safe_new(pattern);
        unsafe { Fl_File_Chooser_set_filter(self.inner, pattern.as_ptr()) }
    }

    /// Gets the filter of the `FileChooser`
    pub fn filter(&self) -> Option<String> {
        assert!(!self.inner.is_null());
        unsafe {
            let ptr = Fl_File_Chooser_filter(self.inner);
            if ptr.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(ptr as *mut raw::c_char)
                        .to_string_lossy()
                        .to_string(),
                )
            }
        }
    }

    /// Gets the current filename filter selection
    pub fn filter_value(&self) -> i32 {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_filter_value(self.inner) }
    }

    /// Sets the filter value using an index to the '\t'separated filters
    pub fn set_filter_value(&mut self, f: i32) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_filter_value(self.inner, f) }
    }

    /// Hides the file chooser
    pub fn hide(&mut self) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_hide(self.inner) }
    }

    /// Sets the icon size of the `FileChooser`
    pub fn set_icon_size(&mut self, s: u8) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_iconsize(self.inner, s) }
    }

    /// Gets the icon size of the `FileChooser`
    pub fn icon_size(&self) -> u8 {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_iconsize(self.inner) }
    }

    /// Sets the label of the `FileChooser`
    pub fn set_label(&mut self, l: &str) {
        assert!(!self.inner.is_null());
        let l = CString::safe_new(l);
        let _old = unsafe { CString::from_raw(Fl_File_Chooser_label(self.inner) as _) };
        unsafe { Fl_File_Chooser_set_label(self.inner, l.into_raw() as _) }
    }

    /// Gets the label of the `FileChooser`
    pub fn label(&self) -> String {
        assert!(!self.inner.is_null());
        unsafe {
            let ptr = Fl_File_Chooser_label(self.inner);
            if ptr.is_null() {
                String::from("")
            } else {
                CStr::from_ptr(ptr as *mut raw::c_char)
                    .to_string_lossy()
                    .to_string()
            }
        }
    }

    /// Sets the label of the Ok button
    pub fn set_ok_label(&mut self, l: &'static str) {
        assert!(!self.inner.is_null());
        let l = CString::safe_new(l);
        unsafe { Fl_File_Chooser_set_ok_label(self.inner, l.into_raw() as _) }
    }

    /// Gets the label of the Ok button
    pub fn ok_label(&self) -> String {
        assert!(!self.inner.is_null());
        unsafe {
            let ptr = Fl_File_Chooser_ok_label(self.inner);
            if ptr.is_null() {
                String::from("")
            } else {
                CStr::from_ptr(ptr as *mut raw::c_char)
                    .to_string_lossy()
                    .to_string()
            }
        }
    }

    /// Add preview to the `FileChooser`
    pub fn set_preview(&mut self, e: bool) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_preview(self.inner, e as i32) }
    }

    /// Returns whether preview is enabled for the `FileChooser`
    pub fn preview(&self) -> bool {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_preview(self.inner) != 0 }
    }

    /// Rescan the directory
    pub fn rescan(&mut self) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_rescan(self.inner) }
    }

    /// Rescan the directory while keeping the file name
    pub fn rescan_keep_filename(&mut self) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_rescan_keep_filename(self.inner) }
    }

    /// Shows the File Chooser
    pub fn show(&mut self) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_show(self.inner) }
    }

    /// Returns whether the file chooser is shown
    pub fn shown(&self) -> bool {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_shown(self.inner) != 0 }
    }

    /// Sets the text color of the file chooser
    pub fn set_text_color(&mut self, c: Color) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_text_color(self.inner, c.bits()) }
    }

    /// Gets the text color of the file chooser
    pub fn text_color(&self) -> Color {
        assert!(!self.inner.is_null());
        unsafe { mem::transmute(Fl_File_Chooser_text_color(self.inner)) }
    }

    /// Sets the text font of the file chooser
    pub fn set_text_font(&mut self, f: Font) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_text_font(self.inner, f.bits()) }
    }

    /// Gets the text font of the file chooser
    pub fn text_font(&self) -> Font {
        assert!(!self.inner.is_null());
        unsafe { mem::transmute(Fl_File_Chooser_text_font(self.inner)) }
    }

    /// Sets the text size of the file chooser
    pub fn set_text_size(&mut self, s: i32) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_text_size(self.inner, s) }
    }

    /// Gets the text size of the file chooser
    pub fn text_size(&self) -> i32 {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_text_size(self.inner) }
    }

    /// Sets the type of the `FileChooser`
    pub fn set_type(&mut self, t: FileChooserType) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_type(self.inner, t.bits()) }
    }

    /// Gets the type of the `FileChooser`
    pub fn get_type(&self) -> FileChooserType {
        assert!(!self.inner.is_null());
        unsafe { mem::transmute(Fl_File_Chooser_type(self.inner)) }
    }

    /// Gets the user data of the `FileChooser`
    /// # Safety
    /// Can invalidate the user data while the `FileChooser` is in use
    pub unsafe fn user_data(&self) -> Option<Box<dyn FnMut()>> {
        let ptr = Fl_File_Chooser_user_data(self.inner);
        if ptr.is_null() {
            None
        } else {
            let x = ptr as *mut Box<dyn FnMut()>;
            let x = Box::from_raw(x);
            Fl_File_Chooser_set_callback(self.inner, None, std::ptr::null_mut());
            Some(*x)
        }
    }

    /// Gets the file or dir name chosen by the `FileChooser`
    pub fn value(&mut self, f: i32) -> Option<String> {
        assert!(!self.inner.is_null());
        let f = if f == 0 { 1 } else { f };
        unsafe {
            let ptr = Fl_File_Chooser_value(self.inner, f);
            if ptr.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(ptr as *mut raw::c_char)
                        .to_string_lossy()
                        .to_string(),
                )
            }
        }
    }

    /// Sets the file or dir name chosen by the `FileChooser`
    pub fn set_value(&mut self, filename: &str) {
        assert!(!self.inner.is_null());
        let filename = CString::safe_new(filename);
        unsafe { Fl_File_Chooser_set_value(self.inner, filename.as_ptr()) }
    }

    /// Returns whether the `FileChooser` is visible or not
    pub fn visible(&self) -> bool {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_visible(self.inner) != 0 }
    }

    /// Return dialog window
    pub fn window(&self) -> impl WindowExt {
        // Shouldn't fail
        unsafe {
            let win_ptr = self
                .new_button()
                .unwrap()
                .parent()
                .unwrap()
                .parent()
                .unwrap()
                .as_widget_ptr();
            crate::window::Window::from_widget_ptr(win_ptr)
        }
    }

    /// Set "Add favorites" label
    pub fn set_add_favorites_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_add_favorites_label(msg.into_raw() as _) }
    }

    /// Set "All Files" label
    pub fn set_all_files_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_all_files_label(msg.into_raw() as _) }
    }

    /// Set "Custom Filter" label
    pub fn set_custom_filter_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_custom_filter_label(msg.into_raw() as _) }
    }

    /// Set "Existing file" label
    pub fn set_existing_file_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_existing_file_label(msg.into_raw() as _) }
    }

    /// Set "Favorites" label
    pub fn set_favorites_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_favorites_label(msg.into_raw() as _) }
    }

    /// Set "Filename" label
    pub fn set_filename_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_filename_label(msg.into_raw() as _) }
    }

    /// Set "Filesystems" label
    pub fn set_filesystems_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_filesystems_label(msg.into_raw() as _) }
    }

    /// Set "Manage favorites" label
    pub fn set_manage_favorites_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_manage_favorites_label(msg.into_raw() as _) }
    }

    /// Set "New directory" label
    pub fn set_new_directory_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_new_directory_label(msg.into_raw() as _) }
    }

    /// Set "New directory" tooltip
    pub fn set_new_directory_tooltip(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_new_directory_tooltip(msg.into_raw() as _) }
    }

    /// Set "Preview" label
    pub fn set_preview_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_preview_label(msg.into_raw() as _) }
    }

    /// Set "Save" label
    pub fn set_save_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_save_label(msg.into_raw() as _) }
    }

    /// Set "Show" label
    pub fn set_show_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_show_label(msg.into_raw() as _) }
    }

    /// Set "hidden" label
    pub fn set_hidden_label(msg: &'static str) {
        let msg = CString::safe_new(msg);
        unsafe { Fl_File_Chooser_set_hidden_label(msg.into_raw() as _) }
    }

    /// Set the position of the file chooser
    pub fn set_position(&mut self, x: i32, y: i32) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_position(self.inner, x, y) }
    }

    /// Set the size of the file chooser
    pub fn set_size(&mut self, w: i32, h: i32) {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_set_size(self.inner, w, h) }
    }

    /// Get the x pos of the file chooser
    pub fn x(&self) -> i32 {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_x(self.inner) }
    }

    /// Get the y pos of the file chooser
    pub fn y(&self) -> i32 {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_y(self.inner) }
    }

    /// Get the width of the file chooser
    pub fn w(&self) -> i32 {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_w(self.inner) }
    }

    /// Get the width of the file chooser
    pub fn h(&self) -> i32 {
        assert!(!self.inner.is_null());
        unsafe { Fl_File_Chooser_h(self.inner) }
    }

    /// Get the size of the file chooser
    pub fn size(&self) -> (i32, i32) {
        (self.w(), self.h())
    }

    /// Get the position of the file chooser
    pub fn pos(&self) -> (i32, i32) {
        (self.x(), self.y())
    }
}

impl Drop for FileChooser {
    fn drop(&mut self) {
        unsafe { Fl_File_Chooser_delete(self.inner) }
    }
}

/// Shows a directory chooser returning a String
pub fn dir_chooser(message: &str, fname: &str, relative: bool) -> Option<String> {
    unsafe {
        let message = CString::safe_new(message);
        let fname = CString::safe_new(fname);
        let ptr = Fl_dir_chooser(message.as_ptr(), fname.as_ptr(), relative as i32);
        if ptr.is_null() {
            None
        } else {
            Some(
                CStr::from_ptr(ptr as *mut raw::c_char)
                    .to_string_lossy()
                    .to_string(),
            )
        }
    }
}

/**
    Shows a file chooser returning a String.
    The pattern field takes the same argument the [`FileChooser::set_filter`](`crate::dialog::FileChooser::set_filter`) method.
    Example:
    ```rust,no_run
    use fltk::{prelude::*, *};
    fn main() {
        let app = app::App::default();
        let mut win = window::Window::default().with_size(900, 300);
        let file = dialog::file_chooser("Choose File", "*.rs", ".", true).unwrap();
        println!("{}", file);
        win.end();
        win.show();
        app.run().unwrap();
    }
    ```
*/
pub fn file_chooser<P: AsRef<Path>>(
    message: &str,
    pattern: &str,
    dir: P,
    relative: bool,
) -> Option<String> {
    file_chooser_(message, pattern, dir.as_ref(), relative)
}

fn file_chooser_(message: &str, pattern: &str, dir: &Path, relative: bool) -> Option<String> {
    if let Some(dir) = dir.to_str() {
        let message = CString::safe_new(message);
        let pattern = CString::safe_new(pattern);
        let dir = CString::safe_new(dir);
        unsafe {
            let ptr = Fl_file_chooser(
                message.as_ptr(),
                pattern.as_ptr(),
                dir.as_ptr(),
                relative as i32,
            );
            if ptr.is_null() {
                None
            } else {
                Some(
                    CStr::from_ptr(ptr as *mut raw::c_char)
                        .to_string_lossy()
                        .to_string(),
                )
            }
        }
    } else {
        None
    }
}

/// Spawns a color chooser dialog.
pub fn color_chooser(name: &str, cmode: ColorMode) -> Option<(u8, u8, u8)> {
    unsafe {
        let name = CString::safe_new(name);
        let mut r = 255;
        let mut g = 255;
        let mut b = 255;
        let ret = Fl_color_chooser(name.as_ptr(), &mut r, &mut g, &mut b, cmode as i32);
        if ret == 0 {
            None
        } else {
            Some((r, g, b))
        }
    }
}

/// Spawns a color chooser dialog.
pub fn color_chooser_with_default(name: &str, cmode: ColorMode, col: (u8, u8, u8)) -> (u8, u8, u8) {
    unsafe {
        let name = CString::safe_new(name);
        let mut r = col.0;
        let mut g = col.1;
        let mut b = col.2;
        let ret = Fl_color_chooser(name.as_ptr(), &mut r, &mut g, &mut b, cmode as i32);
        if ret == 0 {
            col
        } else {
            (r, g, b)
        }
    }
}

/// Set the next dialog's title
pub fn message_title(title: &str) {
    let title = CString::safe_new(title);
    unsafe { Fl_message_title(title.as_ptr() as _) }
}

/// Set the default title for a dialog
pub fn message_title_default(title: &str) {
    let title = CString::safe_new(title);
    unsafe { Fl_message_title_default(title.as_ptr() as _) }
}

/// Get the frame holding the icon of FLTK's dialog boxes
pub fn message_icon() -> impl WidgetExt {
    unsafe { crate::frame::Frame::from_widget_ptr(Fl_message_icon() as _) }
}

/// Set whether hotspot is enabled for FLTK's dialog boxes
pub fn message_set_hotspot(enabled: bool) {
    unsafe { Fl_message_set_hotspot(enabled as _) }
}

/// Get whether hotspot is enabled for FLTK's dialog boxes
pub fn message_hotspot() -> bool {
    unsafe { Fl_message_hotspot() != 0 }
}

/// Set the font and font size of FLTK's dialog boxes
pub fn message_set_font(font: Font, sz: i32) {
    unsafe { Fl_message_set_font(font.bits(), sz) }
}

/// Set the next dialog's icon label
pub fn message_icon_label(label: &str) {
    let label = CString::safe_new(label);
    unsafe { Fl_message_icon_label(label.into_raw() as _) }
}