-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathTinyFileDialog.cs
More file actions
86 lines (73 loc) · 3.68 KB
/
TinyFileDialog.cs
File metadata and controls
86 lines (73 loc) · 3.68 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Toolbox.Core;
namespace MapStudio.UI
{
public class TinyFileDialog
{
[DllImport("tinyfiledialogs", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr tinyfd_openFileDialog(string aTitle,
string aDefaultPathAndFile,
int aNumOfFilterPatterns,
string[] aFilterPatterns,
string aSingleFilterDescription,
int aAllowMultipleSelects);
[DllImport("tinyfiledialogs", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr tinyfd_saveFileDialog(string aTitle,
string aDefaultPathAndFile,
int aNumOfFilterPatterns,
string[] aFilterPatterns,
string aSingleFilterDescription);
[DllImport("tinyfiledialogs", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr tinyfd_selectFolderDialog(string aTitle, string aDefaultPathAndFile);
[DllImport("tinyfiledialogs", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int tinyfd_notifyPopup(string aTitle, string aMessage, string aIconType);
[DllImport("tinyfiledialogs", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int tinyfd_messageBox(string aTitle, string aMessage, string aDialogTyle, string aIconType, int aDefaultButton);
public static string OpenFileDialog(List<FileFilter> filters, string fileName, bool multiSelect)
{
string[] filterList = toFilterArray(filters);
fileName ??= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
return stringFromAnsi(tinyfd_openFileDialog("Open File", fileName, filterList.Length, filterList, "", multiSelect ? 1 : 0)); ;
}
private static bool _hasExportedBefore; //quick and dirty bool to prevent initial file dialog from opening in invalid directory
public static string SaveFileDialog(List<FileFilter> filters, string fileName)
{
string[] filterList = toFilterArray(filters);
if (!_hasExportedBefore)
fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
_hasExportedBefore = true;
return stringFromAnsi(tinyfd_saveFileDialog("Save File", fileName, filterList.Length, filterList, "")); ;
}
public static string SelectFolderDialog(string title, string defaultPathAndFile)
{
return stringFromAnsi(tinyfd_selectFolderDialog(title, defaultPathAndFile));
}
public static int MessageBoxInfoOk(string message)
{
return tinyfd_messageBox("Map Studio", message, "ok", "info", 1);
}
public static int MessageBoxErrorOk(string message)
{
return tinyfd_messageBox("Map Studio", message, "ok", "error", 1);
}
public static int MessageBoxInfoYesNo(string message)
{
return tinyfd_messageBox("Map Studio", message, "yesno", "question", 2);
}
private static string[] toFilterArray(List<FileFilter> filters)
{
string[] filterList = new string[filters.Count];
for (int i = 0; i < filters.Count; i++)
filterList[i] = $"*{filters[i].Extension}";
return filterList;
}
// for UTF-8/char
private static string stringFromAnsi(IntPtr ptr)
{
return Marshal.PtrToStringAnsi(ptr);
}
}
}