PosDim.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /// <para>
  27. /// It is possible to reference coordinates of another view by using the methods
  28. /// Left(View), Right(View), Bottom(View), Top(View). The X(View) and Y(View) are
  29. /// aliases to Left(View) and Top(View) respectively.
  30. /// </para>
  31. /// </remarks>
  32. public class Pos {
  33. internal virtual int Anchor (int width)
  34. {
  35. return 0;
  36. }
  37. class PosFactor : Pos {
  38. float factor;
  39. public PosFactor (float n)
  40. {
  41. this.factor = n;
  42. }
  43. internal override int Anchor (int width)
  44. {
  45. return (int)(width * factor);
  46. }
  47. public override string ToString ()
  48. {
  49. return $"Pos.Factor({factor})";
  50. }
  51. }
  52. /// <summary>
  53. /// Creates a percentage Pos object
  54. /// </summary>
  55. /// <returns>The percent Pos object.</returns>
  56. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  57. public static Pos Percent (float n)
  58. {
  59. if (n < 0 || n > 100)
  60. throw new ArgumentException ("Percent value must be between 0 and 100");
  61. return new PosFactor (n / 100);
  62. }
  63. static PosAnchorEnd endNoMargin;
  64. class PosAnchorEnd : Pos {
  65. int n;
  66. public PosAnchorEnd (int n)
  67. {
  68. this.n = n;
  69. }
  70. internal override int Anchor (int width)
  71. {
  72. return width - n;
  73. }
  74. public override string ToString ()
  75. {
  76. return $"Pos.AnchorEnd(margin={n})";
  77. }
  78. }
  79. /// <summary>
  80. /// Creates a Pos object that is anchored to the end of the dimension, useful to flush
  81. /// the layout from the end.
  82. /// </summary>
  83. /// <returns>The Pos object anchored to the end (the bottom or the right side).</returns>
  84. /// <param name="margin">Optional margin to set aside.</param>
  85. public static Pos AnchorEnd (int margin = 0)
  86. {
  87. if (margin < 0)
  88. throw new ArgumentException ("Margin must be positive");
  89. if (margin == 0) {
  90. if (endNoMargin == null)
  91. endNoMargin = new PosAnchorEnd (0);
  92. return endNoMargin;
  93. }
  94. return new PosAnchorEnd (margin);
  95. }
  96. internal class PosCenter : Pos {
  97. internal override int Anchor (int width)
  98. {
  99. return width / 2;
  100. }
  101. public override string ToString ()
  102. {
  103. return "Pos.Center";
  104. }
  105. }
  106. static PosCenter pCenter;
  107. /// <summary>
  108. /// Returns a Pos object that can be used to center the views.
  109. /// </summary>
  110. /// <returns>The center Pos.</returns>
  111. public static Pos Center ()
  112. {
  113. if (pCenter == null)
  114. pCenter = new PosCenter ();
  115. return pCenter;
  116. }
  117. class PosAbsolute : Pos {
  118. int n;
  119. public PosAbsolute (int n) { this.n = n; }
  120. public override string ToString ()
  121. {
  122. return $"Pos.Absolute({n})";
  123. }
  124. internal override int Anchor (int width)
  125. {
  126. return n;
  127. }
  128. }
  129. /// <summary>
  130. /// Creates an Absolute Pos from the specified integer value.
  131. /// </summary>
  132. /// <returns>The Absolute Pos.</returns>
  133. /// <param name="n">The value to convert to the pos.</param>
  134. public static implicit operator Pos (int n)
  135. {
  136. return new PosAbsolute (n);
  137. }
  138. class PosCombine : Pos {
  139. Pos left, right;
  140. bool add;
  141. public PosCombine (bool add, Pos left, Pos right)
  142. {
  143. this.left = left;
  144. this.right = right;
  145. this.add = add;
  146. }
  147. internal override int Anchor (int width)
  148. {
  149. var la = left.Anchor (width);
  150. var ra = right.Anchor (width);
  151. if (add)
  152. return la + ra;
  153. else
  154. return la - ra;
  155. }
  156. }
  157. /// <summary>
  158. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  159. /// </summary>
  160. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  161. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  162. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  163. public static Pos operator + (Pos left, Pos right)
  164. {
  165. return new PosCombine (true, left, right);
  166. }
  167. /// <summary>
  168. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  169. /// </summary>
  170. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  171. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  172. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  173. public static Pos operator - (Pos left, Pos right)
  174. {
  175. return new PosCombine (false, left, right);
  176. }
  177. internal class PosView : Pos {
  178. public View Target;
  179. int side;
  180. public PosView (View view, int side)
  181. {
  182. Target = view;
  183. this.side = side;
  184. }
  185. internal override int Anchor (int width)
  186. {
  187. switch (side) {
  188. case 0: return Target.Frame.X;
  189. case 1: return Target.Frame.Y;
  190. case 2: return Target.Frame.Right;
  191. case 3: return Target.Frame.Bottom;
  192. default:
  193. return 0;
  194. }
  195. }
  196. }
  197. /// <summary>
  198. /// Returns a Pos object tracks the Left (X) position of the specified view.
  199. /// </summary>
  200. /// <returns>The Position that depends on the other view.</returns>
  201. /// <param name="view">The view that will be tracked.</param>
  202. public static Pos Left (View view) => new PosView (view, 0);
  203. /// <summary>
  204. /// Returns a Pos object tracks the Left (X) position of the specified view.
  205. /// </summary>
  206. /// <returns>The Position that depends on the other view.</returns>
  207. /// <param name="view">The view that will be tracked.</param>
  208. public static Pos X (View view) => new PosView (view, 0);
  209. /// <summary>
  210. /// Returns a Pos object tracks the Top (Y) position of the specified view.
  211. /// </summary>
  212. /// <returns>The Position that depends on the other view.</returns>
  213. /// <param name="view">The view that will be tracked.</param>
  214. public static Pos Top (View view) => new PosView (view, 1);
  215. /// <summary>
  216. /// Returns a Pos object tracks the Top (Y) position of the specified view.
  217. /// </summary>
  218. /// <returns>The Position that depends on the other view.</returns>
  219. /// <param name="view">The view that will be tracked.</param>
  220. public static Pos Y (View view) => new PosView (view, 1);
  221. /// <summary>
  222. /// Returns a Pos object tracks the Right (X+Width) coordinate of the specified view.
  223. /// </summary>
  224. /// <returns>The Position that depends on the other view.</returns>
  225. /// <param name="view">The view that will be tracked.</param>
  226. public static Pos Right (View view) => new PosView (view, 2);
  227. /// <summary>
  228. /// Returns a Pos object tracks the Bottom (Y+Height) coordinate of the specified view.
  229. /// </summary>
  230. /// <returns>The Position that depends on the other view.</returns>
  231. /// <param name="view">The view that will be tracked.</param>
  232. public static Pos Bottom (View view) => new PosView (view, 3);
  233. }
  234. /// <summary>
  235. /// </summary>
  236. /// <remarks>
  237. /// <para>
  238. /// Use the Dim objects on the Width or Height properties of a view to control the position.
  239. /// </para>
  240. /// <para>
  241. /// These can be used to set the absolute position, when merely assigning an
  242. /// integer value (via the implicit integer to Pos conversion), and they can be combined
  243. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the postion
  244. /// of the view 3 characters to the left after centering for example.
  245. /// </para>
  246. /// </remarks>
  247. public class Dim {
  248. internal virtual int Anchor (int width)
  249. {
  250. return 0;
  251. }
  252. class DimFactor : Dim {
  253. float factor;
  254. public DimFactor (float n)
  255. {
  256. this.factor = n;
  257. }
  258. internal override int Anchor (int width)
  259. {
  260. return (int)(width * factor);
  261. }
  262. public override string ToString ()
  263. {
  264. return $"Dim.Factor({factor})";
  265. }
  266. }
  267. /// <summary>
  268. /// Creates a percentage Dim object
  269. /// </summary>
  270. /// <returns>The percent Dim object.</returns>
  271. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  272. public static Dim Percent (float n)
  273. {
  274. if (n < 0 || n > 100)
  275. throw new ArgumentException ("Percent value must be between 0 and 100");
  276. return new DimFactor (n / 100);
  277. }
  278. class DimAbsolute : Dim {
  279. int n;
  280. public DimAbsolute (int n) { this.n = n; }
  281. public override string ToString ()
  282. {
  283. return $"Dim.Absolute({n})";
  284. }
  285. internal override int Anchor (int width)
  286. {
  287. return n;
  288. }
  289. }
  290. class DimFill : Dim {
  291. int margin;
  292. public DimFill (int margin) { this.margin = margin; }
  293. public override string ToString ()
  294. {
  295. return $"Dim.Fill(margin={margin})";
  296. }
  297. internal override int Anchor (int width)
  298. {
  299. return width-margin;
  300. }
  301. }
  302. static DimFill zeroMargin;
  303. /// <summary>
  304. /// Creates a Dim object that fills the dimension, but leaves the specified number of colums for a margin.
  305. /// </summary>
  306. /// <returns>The Fill dimension.</returns>
  307. /// <param name="margin">Margin to use.</param>
  308. public static Dim Fill (int margin = 0)
  309. {
  310. if (margin == 0) {
  311. if (zeroMargin == null)
  312. zeroMargin = new DimFill (0);
  313. return zeroMargin;
  314. }
  315. return new DimFill (margin);
  316. }
  317. /// <summary>
  318. /// Creates an Absolute Pos from the specified integer value.
  319. /// </summary>
  320. /// <returns>The Absolute Pos.</returns>
  321. /// <param name="n">The value to convert to the pos.</param>
  322. public static implicit operator Dim (int n)
  323. {
  324. return new DimAbsolute (n);
  325. }
  326. class DimCombine : Dim {
  327. Dim left, right;
  328. bool add;
  329. public DimCombine (bool add, Dim left, Dim right)
  330. {
  331. this.left = left;
  332. this.right = right;
  333. this.add = add;
  334. }
  335. internal override int Anchor (int width)
  336. {
  337. var la = left.Anchor (width);
  338. var ra = right.Anchor (width);
  339. if (add)
  340. return la + ra;
  341. else
  342. return la - ra;
  343. }
  344. }
  345. /// <summary>
  346. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  347. /// </summary>
  348. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  349. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  350. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  351. public static Dim operator + (Dim left, Dim right)
  352. {
  353. return new DimCombine (true, left, right);
  354. }
  355. /// <summary>
  356. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  357. /// </summary>
  358. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  359. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  360. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  361. public static Dim operator - (Dim left, Dim right)
  362. {
  363. return new DimCombine (false, left, right);
  364. }
  365. internal class DimView : Dim {
  366. public View Target;
  367. int side;
  368. public DimView (View view, int side)
  369. {
  370. Target = view;
  371. this.side = side;
  372. }
  373. internal override int Anchor (int width)
  374. {
  375. switch (side) {
  376. case 0: return Target.Frame.Height;
  377. case 1: return Target.Frame.Width;
  378. default:
  379. return 0;
  380. }
  381. }
  382. }
  383. /// <summary>
  384. /// Returns a Dim object tracks the Width of the specified view.
  385. /// </summary>
  386. /// <returns>The dimension of the other view.</returns>
  387. /// <param name="view">The view that will be tracked.</param>
  388. public static Dim Width (View view) => new DimView (view, 1);
  389. /// <summary>
  390. /// Returns a Dim object tracks the Height of the specified view.
  391. /// </summary>
  392. /// <returns>The dimension of the other view.</returns>
  393. /// <param name="view">The view that will be tracked.</param>
  394. public static Dim Height (View view) => new DimView (view, 0);
  395. }
  396. }