1 /**
2  * Dialog module.
3  *
4  * License:
5  *     MIT. See LICENSE for full details.
6  */
7 module tkd.window.dialog.directorydialog;
8 
9 /**
10  * Imports.
11  */
12 import std.regex;
13 import tkd.window.dialog.dialog;
14 import tkd.window.window;
15 
16 /**
17  * Pops up a dialog box for the user to select a directory.
18  *
19  * Example:
20  * ---
21  * auto dialog = new DirectoryDialog("Select a directory")
22  * .setInitialDirectory("~")
23  * .setDirectoryMustExist(true)
24  * .show();
25  *
26  * string directory = dialog.getResult();
27  * ---
28  *
29  * Result:
30  *     The full path of the directory selected.
31  *
32  * See_Also:
33  *     $(LINK2 ./dialog.html, tkd.dialog.dialog) $(BR)
34  */
35 class DirectoryDialog : Dialog
36 {
37 	/**
38 	 * The initial directory to start in the dialog.
39 	 */
40 	private string _initialDirectory;
41 
42 	/**
43 	 * Whether or not the choosen directory must exist in the file system.
44 	 */
45 	private bool _directoryMustExist;
46 
47 	/**
48 	 * Construct the dialog.
49 	 *
50 	 * Params:
51 	 *     parent = The parent window of the dialog.
52 	 *     title = The title of the dialog.
53 	 */
54 	this(Window parent, string title = "Directory")
55 	{
56 		super(parent, title);
57 	}
58 
59 	/**
60 	 * Construct the dialog.
61 	 *
62 	 * Params:
63 	 *     title = The title of the dialog.
64 	 */
65 	this(string title = "Directory")
66 	{
67 		this(null, title);
68 	}
69 
70 	/**
71 	 * Set the initial directory in the dialog.
72 	 *
73 	 * Params:
74 	 *     directory = The initial directory.
75 	 *
76 	 * Returns:
77 	 *     This dialog to aid method chaining.
78 	 */
79 	public auto setInitialDirectory(this T)(string directory)
80 	{
81 		this._initialDirectory = directory;
82 
83 		return cast(T) this;
84 	}
85 
86 	/**
87 	 * Set if the directory must exist.
88 	 *
89 	 * Params:
90 	 *     mustExist = Determins if the directory must exist.
91 	 *
92 	 * Returns:
93 	 *     This dialog to aid method chaining.
94 	 */
95 	public auto setDirectoryMustExist(this T)(bool mustExist)
96 	{
97 		this._directoryMustExist = mustExist;
98 
99 		return cast(T) this;
100 	}
101 
102 	/**
103 	 * Show the dialog.
104 	 *
105 	 * Returns:
106 	 *     This dialog to aid method chaining.
107 	 */
108 	public auto show(this T)()
109 	{
110 		if (this._parent)
111 		{
112 			this._tk.eval("tk_chooseDirectory -parent %s -title {%s} -initialdir {%s} -mustexist %s", this._parent.id, this._title, this._initialDirectory, this._directoryMustExist);
113 		}
114 		else
115 		{
116 			this._tk.eval("tk_chooseDirectory -title {%s} -initialdir {%s} -mustexist %s", this._title, this._initialDirectory, this._directoryMustExist);
117 		}
118 
119 		string result = this._tk.getResult!(string);
120 
121 		if (match(result, r"^bad window path name").empty)
122 		{
123 			this._results = [result];
124 		}
125 
126 		return cast(T) this;
127 	}
128 }