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 dguihub.filebrowserdialog;
10 
11 //private import std.utf : toUTFz, toUTF8, encode;
12 
13 import std.conv : to;
14 public import dguihub.core.dialogs.commondialog;
15 
16 import dguihub.core.utils;
17 
18 enum FileBrowseMode {
19    open = 0,
20    save = 1,
21 }
22 // OPENFILENAMEW is in druntime/src/core/sys/windows/commdlg.d
23 class FileBrowserDialog : CommonDialog!(OPENFILENAMEW, string) {
24    private string _filter;
25    private FileBrowseMode _fbm = FileBrowseMode.open;
26 
27    @property public void browseMode(FileBrowseMode fbm) {
28       this._fbm = fbm;
29    }
30 
31    @property public string filter() {
32       return this._filter;
33    }
34 
35    @property public void filter(string f) {
36       this._filter = makeFilter(f);
37    }
38 
39    public override bool showDialog() {
40       import std.array;
41 
42       // dynamic array
43       wchar[] buffer = new wchar[](MAX_PATH + 1);
44       buffer[] = '\0';
45 
46       this._dlgStruct.lStructSize = OPENFILENAMEW.sizeof;
47       this._dlgStruct.hwndOwner = GetActiveWindow();
48       this._dlgStruct.lpstrFilter = toUTFz!(wchar*)(this._filter);
49       this._dlgStruct.lpstrTitle = toUTFz!(wchar*)(this._title);
50       this._dlgStruct.lpstrFile = buffer.ptr;
51       this._dlgStruct.nMaxFile = MAX_PATH;
52       this._dlgStruct.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT;
53 
54       bool res = false;
55 
56       switch (this._fbm) {
57          case FileBrowseMode.open:
58             res = cast(bool)GetOpenFileNameW(&this._dlgStruct);
59             break;
60 
61          case FileBrowseMode.save:
62             res = cast(bool)GetSaveFileNameW(&this._dlgStruct);
63             break;
64 
65          default:
66             assert(false, "Unknown browse mode");
67       }
68 
69       if (res) {
70          this._dlgRes = to!string(fromWStringz(buffer.ptr));
71       }
72 
73       return res;
74    }
75 }