1 /**
2  * XView module.
3  *
4  * License:
5  *     MIT. See LICENSE for full details.
6  */
7 module tkd.widget.common.xview;
8 
9 /**
10  * These are common commands that apply to all widgets that have them injected.
11  */
12 mixin template XView()
13 {
14 	import std.algorithm;
15 	import std.array;
16 	import std.conv;
17 
18 	/**
19 	 * Get floating point values which represent the viewable portion of the 
20 	 * text. Each element is a real fraction between 0 and 1; together they 
21 	 * describe the horizontal span that is visible in the window. For example, 
22 	 * if the first element is .2 and the second element is .6, 20% of the 
23 	 * entry's text is off-screen to the left, the middle 40% is visible in the 
24 	 * window, and 40% of the text is off-screen to the right.
25 	 *
26 	 * Returns:
27 	 *     An array containing two floating point values.
28 	 */
29 	public double[] getXView()
30 	{
31 		this._tk.eval("%s xview", this.id);
32 		return this._tk.getResult!(string).split().map!(to!(double)).array;
33 	}
34 
35 	/**
36 	 * Adjusts the view in the window so that the position appears at the left 
37 	 * edge of the window. Position must be a fraction between 0.0 (start of 
38 	 * the text) and 1.0 (end of the text).
39 	 *
40 	 * Params:
41 	 *     position = The character index to scroll to.
42 	 *
43 	 * Returns:
44 	 *     This widget to aid method chaining.
45 	 */
46 	public auto setXView(this T)(double position)
47 	{
48 		this._tk.eval("%s xview moveto %s", this.id, position);
49 
50 		return cast(T) this;
51 	}
52 
53 	/**
54 	 * Adjusts the view in the window so that the character index passed is 
55 	 * displayed at the left edge of the window.
56 	 *
57 	 * Params:
58 	 *     charIndex = The character index to scroll to.
59 	 *
60 	 * Returns:
61 	 *     This widget to aid method chaining.
62 	 */
63 	public auto scrollXToChar(this T)(int charIndex)
64 	{
65 		this._tk.eval("%s xview %s", this.id, charIndex);
66 
67 		return cast(T) this;
68 	}
69 
70 	/**
71 	 * Scroll the text by a specified amount of characters. Positive values 
72 	 * scroll text to the left, negative values scroll text to the right.
73 	 *
74 	 * Params:
75 	 *     numberOfChars = The number of characters to scroll by.
76 	 *
77 	 * Returns:
78 	 *     This widget to aid method chaining.
79 	 */
80 	public auto scrollXChars(this T)(int numberOfChars)
81 	{
82 		this._tk.eval("%s xview scroll %s units", this.id, numberOfChars);
83 
84 		return cast(T) this;
85 	}
86 
87 	/**
88 	 * Scroll the text by a specified amount of pages. Positive values scroll 
89 	 * text to the left, negative values scroll text to the right.
90 	 *
91 	 * Params:
92 	 *     numberOfPages = The number of characters to scroll by.
93 	 *
94 	 * Returns:
95 	 *     This widget to aid method chaining.
96 	 */
97 	public auto scrollXPages(this T)(int numberOfPages)
98 	{
99 		this._tk.eval("%s xview scroll %s pages", this.id, numberOfPages);
100 
101 		return cast(T) this;
102 	}
103 }