1 /** 2 * Dialog module. 3 * 4 * License: 5 * MIT. See LICENSE for full details. 6 */ 7 module tkd.window.dialog.colordialog; 8 9 /** 10 * Imports. 11 */ 12 import std.regex; 13 import tkd.element.color; 14 import tkd.window.dialog.dialog; 15 import tkd.window.window; 16 17 /** 18 * Pops up a dialog box for the user to select a color. 19 * 20 * Example: 21 * --- 22 * auto dialog = new ColorDialog("Select a color") 23 * .setInitialColor(Color.white) 24 * .show(); 25 * 26 * string color = dialog.getResult(); 27 * --- 28 * 29 * Result: 30 * The web style hex color selected. 31 * 32 * See_Also: 33 * $(LINK2 ./dialog.html, tkd.dialog.dialog) $(BR) 34 */ 35 class ColorDialog : Dialog 36 { 37 /** 38 * The initial color to display in the dialog. 39 */ 40 private string _inititalColor = Color.white; 41 42 /** 43 * Construct the dialog. 44 * 45 * Params: 46 * parent = The parent window of the dialog. 47 * title = The title of the dialog. 48 */ 49 this(Window parent, string title = "Color") 50 { 51 super(parent, title); 52 } 53 54 /** 55 * Construct the dialog. 56 * 57 * Params: 58 * title = The title of the dialog. 59 */ 60 this(string title = "Color") 61 { 62 this(null, title); 63 } 64 65 /** 66 * Set the initial color to display in the dialog. 67 * Use colors from the preset color $(LINK2 ../../element/color.html, list) or a web style hex color. 68 * 69 * Params: 70 * color = The initial color. 71 * 72 * Returns: 73 * This dialog to aid method chaining. 74 * 75 * See_Also: 76 * $(LINK2 ../../element/color.html, tkd.widget.color) $(BR) 77 * 78 * Caveats: 79 * If the passed color is not recognised the dialog will fail to show. 80 */ 81 public auto setInitialColor(this T)(string color) 82 { 83 this._inititalColor = color; 84 85 return cast(T) this; 86 } 87 88 /** 89 * Show the dialog. 90 * 91 * Returns: 92 * This dialog to aid method chaining. 93 */ 94 public auto show(this T)() 95 { 96 if (this._parent) 97 { 98 this._tk.eval("tk_chooseColor -parent %s -title {%s} -initialcolor {%s}", this._parent.id, this._title, this._inititalColor); 99 } 100 else 101 { 102 this._tk.eval("tk_chooseColor -title {%s} -initialcolor {%s}", this._title, this._inititalColor); 103 } 104 105 string result = this._tk.getResult!(string); 106 107 if (match(result, r"^unknown color name").empty) 108 { 109 this._results = [result]; 110 } 111 112 return cast(T) this; 113 } 114 }