PosDim.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // PosDim.cs: Pos and Dim objects for view dimensions.
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. using System;
  8. namespace Terminal.Gui {
  9. /// <summary>
  10. /// Describes a position which can be an absolute value, a percentage, centered, or
  11. /// relative to the ending dimension. Integer values are implicitly convertible to
  12. /// an absolute Pos. These objects are created using the static methods Percent,
  13. /// AnchorEnd and Center. The Pos objects can be combined with the addition and
  14. /// subtraction operators.
  15. /// </summary>
  16. /// <remarks>
  17. /// <para>
  18. /// Use the Pos objects on the X or Y properties of a view to control the position.
  19. /// </para>
  20. /// <para>
  21. /// These can be used to set the absolute position, when merely assigning an
  22. /// integer value (via the implicit integer to Pos conversion), and they can be combined
  23. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the postion
  24. /// of the view 3 characters to the left after centering for example.
  25. /// </para>
  26. /// </remarks>
  27. public class Pos {
  28. internal virtual int Anchor (int width)
  29. {
  30. return 0;
  31. }
  32. class PosFactor : Pos {
  33. float factor;
  34. public PosFactor (float n)
  35. {
  36. this.factor = n;
  37. }
  38. internal override int Anchor (int width)
  39. {
  40. return (int)(width * factor);
  41. }
  42. public override string ToString ()
  43. {
  44. return $"Pos.Factor({factor})";
  45. }
  46. }
  47. /// <summary>
  48. /// Creates a percentage Pos object
  49. /// </summary>
  50. /// <returns>The percent Pos object.</returns>
  51. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  52. public static Pos Percent (float n)
  53. {
  54. if (n < 0 || n > 100)
  55. throw new ArgumentException ("Percent value must be between 0 and 100");
  56. return new PosFactor (n / 100);
  57. }
  58. static PosAnchorEnd endNoMargin;
  59. class PosAnchorEnd : Pos {
  60. int n;
  61. public PosAnchorEnd (int n)
  62. {
  63. this.n = n;
  64. }
  65. internal override int Anchor (int width)
  66. {
  67. return width - n;
  68. }
  69. public override string ToString ()
  70. {
  71. return $"Pos.AnchorEnd(margin={n})";
  72. }
  73. }
  74. /// <summary>
  75. /// Creates a Pos object that is anchored to the end of the dimension, useful to flush
  76. /// the layout from the end.
  77. /// </summary>
  78. /// <returns>The Pos object anchored to the end (the bottom or the right side).</returns>
  79. /// <param name="margin">Optional margin to set aside.</param>
  80. public static Pos AnchorEnd (int margin = 0)
  81. {
  82. if (margin < 0)
  83. throw new ArgumentException ("Margin must be positive");
  84. if (margin == 0) {
  85. if (endNoMargin == null)
  86. endNoMargin = new PosAnchorEnd (0);
  87. return endNoMargin;
  88. }
  89. return new PosAnchorEnd (margin);
  90. }
  91. internal class PosCenter : Pos {
  92. internal override int Anchor (int width)
  93. {
  94. return width / 2;
  95. }
  96. public override string ToString ()
  97. {
  98. return "Pos.Center";
  99. }
  100. }
  101. static PosCenter pCenter;
  102. /// <summary>
  103. /// Returns a Pos object that can be used to center the views.
  104. /// </summary>
  105. /// <returns>The center Pos.</returns>
  106. public static Pos Center ()
  107. {
  108. if (pCenter == null)
  109. pCenter = new PosCenter ();
  110. return pCenter;
  111. }
  112. class PosAbsolute : Pos {
  113. int n;
  114. public PosAbsolute (int n) { this.n = n; }
  115. public override string ToString ()
  116. {
  117. return $"Pos.Absolute({n})";
  118. }
  119. internal override int Anchor (int width)
  120. {
  121. return n;
  122. }
  123. }
  124. /// <summary>
  125. /// Creates an Absolute Pos from the specified integer value.
  126. /// </summary>
  127. /// <returns>The Absolute Pos.</returns>
  128. /// <param name="n">The value to convert to the pos.</param>
  129. public static implicit operator Pos (int n)
  130. {
  131. return new PosAbsolute (n);
  132. }
  133. class PosCombine : Pos {
  134. Pos left, right;
  135. bool add;
  136. public PosCombine (bool add, Pos left, Pos right)
  137. {
  138. this.left = left;
  139. this.right = right;
  140. this.add = add;
  141. }
  142. internal override int Anchor (int width)
  143. {
  144. var la = left.Anchor (width);
  145. var ra = right.Anchor (width);
  146. if (add)
  147. return la + ra;
  148. else
  149. return la - ra;
  150. }
  151. }
  152. /// <summary>
  153. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  154. /// </summary>
  155. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  156. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  157. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  158. public static Pos operator + (Pos left, Pos right)
  159. {
  160. return new PosCombine (true, left, right);
  161. }
  162. /// <summary>
  163. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  164. /// </summary>
  165. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  166. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  167. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  168. public static Pos operator - (Pos left, Pos right)
  169. {
  170. return new PosCombine (false, left, right);
  171. }
  172. }
  173. /// <summary>
  174. /// </summary>
  175. /// <remarks>
  176. /// <para>
  177. /// Use the Dim objects on the Width or Height properties of a view to control the position.
  178. /// </para>
  179. /// <para>
  180. /// These can be used to set the absolute position, when merely assigning an
  181. /// integer value (via the implicit integer to Pos conversion), and they can be combined
  182. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the postion
  183. /// of the view 3 characters to the left after centering for example.
  184. /// </para>
  185. /// </remarks>
  186. public class Dim {
  187. internal virtual int Anchor (int width)
  188. {
  189. return 0;
  190. }
  191. class DimFactor : Dim {
  192. float factor;
  193. public DimFactor (float n)
  194. {
  195. this.factor = n;
  196. }
  197. internal override int Anchor (int width)
  198. {
  199. return (int)(width * factor);
  200. }
  201. public override string ToString ()
  202. {
  203. return $"Dim.Factor({factor})";
  204. }
  205. }
  206. /// <summary>
  207. /// Creates a percentage Dim object
  208. /// </summary>
  209. /// <returns>The percent Dim object.</returns>
  210. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  211. public static Dim Percent (float n)
  212. {
  213. if (n < 0 || n > 100)
  214. throw new ArgumentException ("Percent value must be between 0 and 100");
  215. return new DimFactor (n / 100);
  216. }
  217. class DimAbsolute : Dim {
  218. int n;
  219. public DimAbsolute (int n) { this.n = n; }
  220. public override string ToString ()
  221. {
  222. return $"Dim.Absolute({n})";
  223. }
  224. internal override int Anchor (int width)
  225. {
  226. return n;
  227. }
  228. }
  229. class DimFill : Dim {
  230. int margin;
  231. public DimFill (int margin) { this.margin = margin; }
  232. public override string ToString ()
  233. {
  234. return $"Dim.Fill(margin={margin})";
  235. }
  236. internal override int Anchor (int width)
  237. {
  238. return width-margin;
  239. }
  240. }
  241. static DimFill zeroMargin;
  242. /// <summary>
  243. /// Creates a Dim object that fills the dimension, but leaves the specified number of colums for a margin.
  244. /// </summary>
  245. /// <returns>The Fill dimension.</returns>
  246. /// <param name="margin">Margin to use.</param>
  247. public static Dim Fill (int margin = 0)
  248. {
  249. if (margin == 0) {
  250. if (zeroMargin == null)
  251. zeroMargin = new DimFill (0);
  252. return zeroMargin;
  253. }
  254. return new DimFill (margin);
  255. }
  256. /// <summary>
  257. /// Creates an Absolute Pos from the specified integer value.
  258. /// </summary>
  259. /// <returns>The Absolute Pos.</returns>
  260. /// <param name="n">The value to convert to the pos.</param>
  261. public static implicit operator Dim (int n)
  262. {
  263. return new DimAbsolute (n);
  264. }
  265. class DimCombine : Dim {
  266. Dim left, right;
  267. bool add;
  268. public DimCombine (bool add, Dim left, Dim right)
  269. {
  270. this.left = left;
  271. this.right = right;
  272. this.add = add;
  273. }
  274. internal override int Anchor (int width)
  275. {
  276. var la = left.Anchor (width);
  277. var ra = right.Anchor (width);
  278. if (add)
  279. return la + ra;
  280. else
  281. return la - ra;
  282. }
  283. }
  284. /// <summary>
  285. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  286. /// </summary>
  287. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  288. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  289. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  290. public static Dim operator + (Dim left, Dim right)
  291. {
  292. return new DimCombine (true, left, right);
  293. }
  294. /// <summary>
  295. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  296. /// </summary>
  297. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  298. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  299. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  300. public static Dim operator - (Dim left, Dim right)
  301. {
  302. return new DimCombine (false, left, right);
  303. }
  304. }
  305. }