1 /**
2  * Dialog module.
3  *
4  * License:
5  *     MIT. See LICENSE for full details.
6  */
7 module tkd.window.dialog.fontdialog;
8 
9 /**
10  * Imports.
11  */
12 import tkd.window.dialog.dialog;
13 import tkd.element.element;
14 import tkd.window.window;
15 
16 /**
17  * The font dialog allows users to choose a font installed on the system. It 
18  * uses the native platform font selection dialog where available, or a dialog 
19  * implemented in Tcl/Tk otherwise. Unlike most of the other dialogs, the 
20  * fontchooser does not return an immediate result because on some platforms 
21  * (Mac OSX) the standard font dialog is modeless while on others (Windows) it 
22  * is modal. To accommodate this difference, all user interaction with the 
23  * dialog will be communicated to the caller via commands or virtual events.
24  *
25  * Example:
26  * ---
27  * auto dialog = new FontDialog("Select a font")
28  * 	.setCommand(delegate(CommandArgs args){
29  * 		string font = args.dialog.font;
30  * 	})
31  * 	.show();
32  * ---
33  *
34  * Additional_Events:
35  *     Additional events that can also be bound to using the $(LINK2 ../../element/uielement.html#UiElement.bind, bind) method.
36  *     $(P
37  *         <<TkFontchooserFontChanged>>,
38  *         <<TkFontchooserVisibility>>,
39  *     )
40  *
41  * Result:
42  *     The result is empty as all interaction takes place via callbacks.
43  *
44  * See_Also:
45  *     $(LINK2 ./dialog.html, tkd.dialog.dialog) $(BR)
46  */
47 class FontDialog : Dialog
48 {
49 	/**
50 	 * The name of the callback to execute when a new font is choosen.
51 	 */
52 	private string _command;
53 
54 	/**
55 	 * Construct the dialog.
56 	 *
57 	 * Params:
58 	 *     parent = The parent window of the dialog.
59 	 *     title = The title of the dialog.
60 	 */
61 	this(Window parent, string title = "Font")
62 	{
63 		super(parent, title);
64 	}
65 
66 	/**
67 	 * Construct the dialog.
68 	 *
69 	 * Params:
70 	 *     title = The title of the dialog.
71 	 */
72 	this(string title = "Font")
73 	{
74 		this(null, title);
75 	}
76 
77 	/**
78 	 * Set the callback to execute when a new font is choosen. When the 
79 	 * callback is executed information about the choosen font is stored as a 
80 	 * string in the CommandArgs.Dialog struct.
81 	 *
82 	 * Params:
83 	 *     callback = The delegate callback to execute when invoking the command.
84 	 *
85 	 * Returns:
86 	 *     This dialog to aid method chaining.
87 	 *
88 	 * Callback_Arguments:
89 	 *     These are the fields within the callback's $(LINK2 
90 	 *     ../../element/element.html#CommandArgs, CommandArgs) parameter which 
91 	 *     are populated by this method when the callback is executed. 
92 	 *     $(P
93 	 *         $(PARAM_TABLE
94 	 *             $(PARAM_ROW CommandArgs.element, The element that executed the callback.)
95 	 *             $(PARAM_ROW CommandArgs.callback, The callback which was executed.)
96 	 *             $(PARAM_ROW CommandArgs.dialog.font, Font information populated from dialog interaction.)
97 	 *         )
98 	 *     )
99 	 *
100 	 * See_Also:
101 	 *     $(LINK2 ../../element/element.html#CommandArgs, tkd.element.element.CommandArgs) $(BR)
102 	 *     $(LINK2 ../../element/element.html#CommandCallback, tkd.element.element.CommandCallback) $(BR)
103 	 */
104 	public auto setCommand(this T)(CommandCallback callback)
105 	{
106 		this._command = this.createCommand(callback);
107 		this.configureDialog();
108 
109 		return cast(T) this;
110 	}
111 
112 	/**
113 	 * Remove a previously set command.
114 	 *
115 	 * Returns:
116 	 *     This dialog to aid method chaining.
117 	 */
118 	public auto removeCommand(this T)()
119 	{
120 		this._command = "";
121 		this._tk.deleteCommand(this.getCommandName());
122 		this.configureDialog();
123 
124 		return cast(T) this;
125 	}
126 
127 	/**
128 	 * Configure the dialog with the various properties.
129 	 */
130 	private void configureDialog()
131 	{
132 		if (this._parent)
133 		{
134 			this._tk.eval("tk fontchooser configure -parent %s -title {%s} -command {%s}", this._parent.id, this._title, this._command);
135 		}
136 		else
137 		{
138 			this._tk.eval("tk fontchooser configure -title {%s} -command {%s}", this._title, this._command);
139 		}
140 	}
141 
142 	/**
143 	 * Show the dialog.
144 	 *
145 	 * Returns:
146 	 *     This dialog to aid method chaining.
147 	 */
148 	public auto show(this T)()
149 	{
150 		this._tk.eval("tk fontchooser show");
151 
152 		return cast(T) this;
153 	}
154 
155 	/**
156 	 * Hide the dialog.
157 	 *
158 	 * Returns:
159 	 *     This dialog to aid method chaining.
160 	 */
161 	public auto hide(this T)()
162 	{
163 		this._tk.eval("tk fontchooser hide");
164 
165 		return cast(T) this;
166 	}
167 
168 	/**
169 	 * Check if the dialog is visible.
170 	 *
171 	 * Returns:
172 	 *     This dialog to aid method chaining.
173 	 */
174 	public bool isVisible()
175 	{
176 		this._tk.eval("tk fontchooser configure -visible");
177 
178 		return this._tk.getResult!(int) == 1;
179 	}
180 }