1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dgui.filebrowserdialog;
10 
11 private import std.utf: toUTFz, toUTF8;
12 private import std.conv: to;
13 public import dgui.core.dialogs.commondialog;
14 private import dgui.core.utils;
15 
16 enum FileBrowseMode
17 {
18 	open = 0,
19 	save = 1,
20 }
21 
22 class FileBrowserDialog: CommonDialog!(OPENFILENAMEW, string)
23 {
24 	private string _filter;
25 	private FileBrowseMode _fbm = FileBrowseMode.open;
26 
27 	@property public void browseMode(FileBrowseMode fbm)
28 	{
29 		this._fbm = fbm;
30 	}
31 
32 	@property public string filter()
33 	{
34 		return this._filter;
35 	}
36 
37 	@property public void filter(string f)
38 	{
39 		this._filter = makeFilter(f);
40 	}
41 
42 	public override bool showDialog()
43 	{
44 		wchar[MAX_PATH + 1] buffer;
45 		buffer[] = '\0';
46 
47 		this._dlgStruct.lStructSize = OPENFILENAMEW.sizeof;
48 		this._dlgStruct.hwndOwner = GetActiveWindow();
49 		this._dlgStruct.lpstrFilter = toUTFz!(wchar*)(this._filter);
50 		this._dlgStruct.lpstrTitle = toUTFz!(wchar*)(this._title);
51 		this._dlgStruct.lpstrFile = buffer.ptr;
52 		this._dlgStruct.nMaxFile = MAX_PATH;
53 		this._dlgStruct.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT;
54 
55 		bool res = false;
56 
57 		switch(this._fbm)
58 		{
59 			case FileBrowseMode.open:
60 				res = cast(bool)GetOpenFileNameW(&this._dlgStruct);
61 				break;
62 
63 			case FileBrowseMode.save:
64 				res = cast(bool)GetSaveFileNameW(&this._dlgStruct);
65 				break;
66 
67 			default:
68 				assert(false, "Unknown browse mode");
69 		}
70 
71 		if(res)
72 		{
73 			this._dlgRes = to!(string)(toUTF8(buffer).ptr);
74 		}
75 
76 		return res;
77 	}
78 }