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; 12 13 private import std.conv : to; 14 public import dguihub.core.dialogs.commondialog; 15 16 private 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 wchar[] buffer = new wchar[MAX_PATH + 1]; 43 //wchar[MAX_PATH + 1] buffer; 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 53 | OFN_CREATEPROMPT | OFN_OVERWRITEPROMPT; 54 55 bool res = false; 56 57 switch (this._fbm) { 58 case FileBrowseMode.open: 59 res = cast(bool)GetOpenFileNameW(&this._dlgStruct); 60 break; 61 62 case FileBrowseMode.save: 63 res = cast(bool)GetSaveFileNameW(&this._dlgStruct); 64 break; 65 66 default: 67 assert(false, "Unknown browse mode"); 68 } 69 70 if (res) { 71 this._dlgRes = toUTF8(buffer); 72 //this._dlgRes = to!(string)(toUTF8(buffer).ptr); 73 } 74 75 return res; 76 } 77 }