PosDim.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. /// <summary>
  139. /// Creates an Absolute Pos from the specified integer value.
  140. /// </summary>
  141. /// <returns>The Absolute Pos.</returns>
  142. /// <param name="n">The value to convert to the pos.</param>
  143. public static Pos At (int n)
  144. {
  145. return new PosAbsolute (n);
  146. }
  147. class PosCombine : Pos {
  148. Pos left, right;
  149. bool add;
  150. public PosCombine (bool add, Pos left, Pos right)
  151. {
  152. this.left = left;
  153. this.right = right;
  154. this.add = add;
  155. }
  156. internal override int Anchor (int width)
  157. {
  158. var la = left.Anchor (width);
  159. var ra = right.Anchor (width);
  160. if (add)
  161. return la + ra;
  162. else
  163. return la - ra;
  164. }
  165. }
  166. /// <summary>
  167. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  168. /// </summary>
  169. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  170. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  171. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  172. public static Pos operator + (Pos left, Pos right)
  173. {
  174. return new PosCombine (true, left, right);
  175. }
  176. /// <summary>
  177. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  178. /// </summary>
  179. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  180. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  181. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  182. public static Pos operator - (Pos left, Pos right)
  183. {
  184. return new PosCombine (false, left, right);
  185. }
  186. internal class PosView : Pos {
  187. public View Target;
  188. int side;
  189. public PosView (View view, int side)
  190. {
  191. Target = view;
  192. this.side = side;
  193. }
  194. internal override int Anchor (int width)
  195. {
  196. switch (side) {
  197. case 0: return Target.Frame.X;
  198. case 1: return Target.Frame.Y;
  199. case 2: return Target.Frame.Right;
  200. case 3: return Target.Frame.Bottom;
  201. default:
  202. return 0;
  203. }
  204. }
  205. }
  206. /// <summary>
  207. /// Returns a Pos object tracks the Left (X) position of the specified view.
  208. /// </summary>
  209. /// <returns>The Position that depends on the other view.</returns>
  210. /// <param name="view">The view that will be tracked.</param>
  211. public static Pos Left (View view) => new PosView (view, 0);
  212. /// <summary>
  213. /// Returns a Pos object tracks the Left (X) position of the specified view.
  214. /// </summary>
  215. /// <returns>The Position that depends on the other view.</returns>
  216. /// <param name="view">The view that will be tracked.</param>
  217. public static Pos X (View view) => new PosView (view, 0);
  218. /// <summary>
  219. /// Returns a Pos object tracks the Top (Y) position of the specified view.
  220. /// </summary>
  221. /// <returns>The Position that depends on the other view.</returns>
  222. /// <param name="view">The view that will be tracked.</param>
  223. public static Pos Top (View view) => new PosView (view, 1);
  224. /// <summary>
  225. /// Returns a Pos object tracks the Top (Y) position of the specified view.
  226. /// </summary>
  227. /// <returns>The Position that depends on the other view.</returns>
  228. /// <param name="view">The view that will be tracked.</param>
  229. public static Pos Y (View view) => new PosView (view, 1);
  230. /// <summary>
  231. /// Returns a Pos object tracks the Right (X+Width) coordinate of the specified view.
  232. /// </summary>
  233. /// <returns>The Position that depends on the other view.</returns>
  234. /// <param name="view">The view that will be tracked.</param>
  235. public static Pos Right (View view) => new PosView (view, 2);
  236. /// <summary>
  237. /// Returns a Pos object tracks the Bottom (Y+Height) coordinate of the specified view.
  238. /// </summary>
  239. /// <returns>The Position that depends on the other view.</returns>
  240. /// <param name="view">The view that will be tracked.</param>
  241. public static Pos Bottom (View view) => new PosView (view, 3);
  242. }
  243. /// <summary>
  244. /// </summary>
  245. /// <remarks>
  246. /// <para>
  247. /// Use the Dim objects on the Width or Height properties of a view to control the position.
  248. /// </para>
  249. /// <para>
  250. /// These can be used to set the absolute position, when merely assigning an
  251. /// integer value (via the implicit integer to Pos conversion), and they can be combined
  252. /// to produce more useful layouts, like: Pos.Center - 3, which would shift the postion
  253. /// of the view 3 characters to the left after centering for example.
  254. /// </para>
  255. /// </remarks>
  256. public class Dim {
  257. internal virtual int Anchor (int width)
  258. {
  259. return 0;
  260. }
  261. class DimFactor : Dim {
  262. float factor;
  263. public DimFactor (float n)
  264. {
  265. this.factor = n;
  266. }
  267. internal override int Anchor (int width)
  268. {
  269. return (int)(width * factor);
  270. }
  271. public override string ToString ()
  272. {
  273. return $"Dim.Factor({factor})";
  274. }
  275. }
  276. /// <summary>
  277. /// Creates a percentage Dim object
  278. /// </summary>
  279. /// <returns>The percent Dim object.</returns>
  280. /// <param name="n">A value between 0 and 100 representing the percentage.</param>
  281. public static Dim Percent (float n)
  282. {
  283. if (n < 0 || n > 100)
  284. throw new ArgumentException ("Percent value must be between 0 and 100");
  285. return new DimFactor (n / 100);
  286. }
  287. class DimAbsolute : Dim {
  288. int n;
  289. public DimAbsolute (int n) { this.n = n; }
  290. public override string ToString ()
  291. {
  292. return $"Dim.Absolute({n})";
  293. }
  294. internal override int Anchor (int width)
  295. {
  296. return n;
  297. }
  298. }
  299. class DimFill : Dim {
  300. int margin;
  301. public DimFill (int margin) { this.margin = margin; }
  302. public override string ToString ()
  303. {
  304. return $"Dim.Fill(margin={margin})";
  305. }
  306. internal override int Anchor (int width)
  307. {
  308. return width-margin;
  309. }
  310. }
  311. static DimFill zeroMargin;
  312. /// <summary>
  313. /// Creates a Dim object that fills the dimension, but leaves the specified number of colums for a margin.
  314. /// </summary>
  315. /// <returns>The Fill dimension.</returns>
  316. /// <param name="margin">Margin to use.</param>
  317. public static Dim Fill (int margin = 0)
  318. {
  319. if (margin == 0) {
  320. if (zeroMargin == null)
  321. zeroMargin = new DimFill (0);
  322. return zeroMargin;
  323. }
  324. return new DimFill (margin);
  325. }
  326. /// <summary>
  327. /// Creates an Absolute Pos from the specified integer value.
  328. /// </summary>
  329. /// <returns>The Absolute Pos.</returns>
  330. /// <param name="n">The value to convert to the pos.</param>
  331. public static implicit operator Dim (int n)
  332. {
  333. return new DimAbsolute (n);
  334. }
  335. /// <summary>
  336. /// Creates an Absolute Pos from the specified integer value.
  337. /// </summary>
  338. /// <returns>The Absolute Pos.</returns>
  339. /// <param name="n">The value to convert to the pos.</param>
  340. public static Dim Sized (int n)
  341. {
  342. return new DimAbsolute (n);
  343. }
  344. class DimCombine : Dim {
  345. Dim left, right;
  346. bool add;
  347. public DimCombine (bool add, Dim left, Dim right)
  348. {
  349. this.left = left;
  350. this.right = right;
  351. this.add = add;
  352. }
  353. internal override int Anchor (int width)
  354. {
  355. var la = left.Anchor (width);
  356. var ra = right.Anchor (width);
  357. if (add)
  358. return la + ra;
  359. else
  360. return la - ra;
  361. }
  362. }
  363. /// <summary>
  364. /// Adds a <see cref="Terminal.Gui.Pos"/> to a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  365. /// </summary>
  366. /// <param name="left">The first <see cref="Terminal.Gui.Pos"/> to add.</param>
  367. /// <param name="right">The second <see cref="Terminal.Gui.Pos"/> to add.</param>
  368. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
  369. public static Dim operator + (Dim left, Dim right)
  370. {
  371. return new DimCombine (true, left, right);
  372. }
  373. /// <summary>
  374. /// Subtracts a <see cref="Terminal.Gui.Pos"/> from a <see cref="Terminal.Gui.Pos"/>, yielding a new <see cref="T:Terminal.Gui.Pos"/>.
  375. /// </summary>
  376. /// <param name="left">The <see cref="Terminal.Gui.Pos"/> to subtract from (the minuend).</param>
  377. /// <param name="right">The <see cref="Terminal.Gui.Pos"/> to subtract (the subtrahend).</param>
  378. /// <returns>The <see cref="T:Terminal.Gui.Pos"/> that is the <c>left</c> minus <c>right</c>.</returns>
  379. public static Dim operator - (Dim left, Dim right)
  380. {
  381. return new DimCombine (false, left, right);
  382. }
  383. internal class DimView : Dim {
  384. public View Target;
  385. int side;
  386. public DimView (View view, int side)
  387. {
  388. Target = view;
  389. this.side = side;
  390. }
  391. internal override int Anchor (int width)
  392. {
  393. switch (side) {
  394. case 0: return Target.Frame.Height;
  395. case 1: return Target.Frame.Width;
  396. default:
  397. return 0;
  398. }
  399. }
  400. }
  401. /// <summary>
  402. /// Returns a Dim object tracks the Width of the specified view.
  403. /// </summary>
  404. /// <returns>The dimension of the other view.</returns>
  405. /// <param name="view">The view that will be tracked.</param>
  406. public static Dim Width (View view) => new DimView (view, 1);
  407. /// <summary>
  408. /// Returns a Dim object tracks the Height of the specified view.
  409. /// </summary>
  410. /// <returns>The dimension of the other view.</returns>
  411. /// <param name="view">The view that will be tracked.</param>
  412. public static Dim Height (View view) => new DimView (view, 0);
  413. }
  414. }