1 /**
2  * Dialog module.
3  *
4  * License:
5  *     MIT. See LICENSE for full details.
6  */
7 module tkd.window.dialog.savefiledialog;
8 
9 /**
10  * Imports.
11  */
12 import std.array;
13 import std.regex;
14 import tkd.window.dialog.filedialog;
15 import tkd.window.window;
16 
17 /**
18  * Pops up a dialog box for the user to save a file.
19  *
20  * Example:
21  * ---
22  * auto dialog = new SaveFileDialog("Save a file")
23  * 	.setConfirmOverwrite(true)
24  * 	.setDefaultExtension(".txt")
25  * 	.addFileType("{{All files} {*}}")
26  * 	.addFileType("{{Text files} {.txt}}")
27  * 	.setInitialDirectory("~")
28  * 	.setInitialFile("file.txt")
29  * 	.show();
30  *
31  * string fileToWrite = dialog.getResult();
32  * ---
33  *
34  * Result:
35  *     The full path of the file selected.
36  *
37  * See_Also:
38  *     $(LINK2 ./filedialog.html, tkd.dialog.filedialog) $(BR)
39  */
40 class SaveFileDialog : FileDialog
41 {
42 	/*
43 	 * Configures how the Save dialog reacts when the selected file already 
44 	 * exists, and saving would overwrite it.
45 	 */
46 	private bool _confirmOverwrite = true;
47 
48 	/**
49 	 * Construct the dialog.
50 	 *
51 	 * Params:
52 	 *     parent = The parent window of the dialog.
53 	 *     title = The title of the dialog.
54 	 */
55 	this(Window parent, string title = "Save")
56 	{
57 		super(parent, title);
58 	}
59 
60 	/**
61 	 * Construct the dialog.
62 	 *
63 	 * Params:
64 	 *     title = The title of the dialog.
65 	 */
66 	this(string title = "Save")
67 	{
68 		this(null, title);
69 	}
70 
71 	/**
72 	 * Configures how the Save dialog reacts when the selected file already 
73 	 * exists, and saving would overwrite it. A true value requests a 
74 	 * confirmation dialog be presented to the user. A false value requests 
75 	 * that the overwrite take place without confirmation. Default value is 
76 	 * true.
77 	 *
78 	 * Params:
79 	 *     confirm = Show a dialog to confirm overwriting any file.
80 	 *
81 	 * Returns:
82 	 *     This dialog to aid method chaining.
83 	 */
84 	public auto setConfirmOverwrite(this T)(bool confirm)
85 	{
86 		this._confirmOverwrite = confirm;
87 
88 		return cast(T) this;
89 	}
90 
91 	/**
92 	 * Show the dialog.
93 	 *
94 	 * Returns:
95 	 *     This dialog to aid method chaining.
96 	 */
97 	public auto show(this T)()
98 	{
99 		if (this._parent)
100 		{
101 			// String concatentation is used here to avoid the character escaping done on args.
102 			this._tk.eval("tk_getSaveFile -parent %s -title {%s} -confirmoverwrite %s -defaultextension {%s} -filetypes {" ~ this._fileTypes.join(" ") ~ "} -initialdir {%s} -initialfile {%s} -typevariable %s", this._parent.id, this._title, this._confirmOverwrite, this._defaultExtension, this._initialDirectory, this._initialFile, this._typeVariable);
103 		}
104 		else
105 		{
106 			// String concatentation is used here to avoid the character escaping done on args.
107 			this._tk.eval("tk_getSaveFile -title {%s} -confirmoverwrite %s -defaultextension {%s} -filetypes {" ~ this._fileTypes.join(" ") ~ "} -initialdir {%s} -initialfile {%s} -typevariable %s", this._title, this._confirmOverwrite, this._defaultExtension, this._initialDirectory, this._initialFile, this._typeVariable);
108 		}
109 
110 		string result = this._tk.getResult!(string);
111 
112 		if (match(result, r"^bad window path name").empty)
113 		{
114 			this._results = [result];
115 		}
116 
117 		return cast(T) this;
118 	}
119 }