SplitContainer.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Terminal.Gui.Graphs;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// A <see cref="View"/> consisting of a moveable bar that divides
  9. /// the display area into 2 resizeable panels.
  10. /// </summary>
  11. public class SplitContainer : View {
  12. private SplitContainerLineView splitterLine;
  13. SplitContainer parentSplitPanel;
  14. /// TODO: Might be able to make Border virtual and override here
  15. /// To make this more API friendly
  16. /// <summary>
  17. /// Use this field instead of Border to create an integrated
  18. /// Border in which lines connect with subpanels and splitters
  19. /// seamlessly
  20. /// </summary>
  21. public BorderStyle IntegratedBorder {get;set;}
  22. /// <summary>
  23. /// The <see cref="View"/> showing in the left hand pane of a
  24. /// <see cref="Orientation.Vertical"/> or top of an
  25. /// <see cref="Orientation.Horizontal"/> pane. May be another
  26. /// <see cref="SplitContainer"/> if further splitter subdivisions are
  27. /// desired (e.g. to create a resizeable grid.
  28. /// </summary>
  29. public View Panel1 { get; set; } // TODO: Should not be public set, should be helpers for this
  30. public int Panel1MinSize { get; set; } = 1;
  31. public ustring Panel1Title { get; set; } = string.Empty;
  32. /// <summary>
  33. /// The <see cref="View"/> showing in the right hand pane of a
  34. /// <see cref="Orientation.Vertical"/> or bottom of an
  35. /// <see cref="Orientation.Horizontal"/> pane. May be another
  36. /// <see cref="SplitContainer"/> if further splitter subdivisions are
  37. /// desired (e.g. to create a resizeable grid.
  38. /// </summary>
  39. public View Panel2 { get; set; } // TODO: Should not be public set, should be helpers for this
  40. public int Panel2MinSize { get; set; } = 1;
  41. public ustring Panel2Title { get; set; } = string.Empty;
  42. private Pos splitterDistance = Pos.Percent (50);
  43. private Orientation orientation = Orientation.Vertical;
  44. /// <summary>
  45. /// Creates a new instance of the SplitContainer class.
  46. /// </summary>
  47. public SplitContainer ()
  48. {
  49. splitterLine = new SplitContainerLineView (this);
  50. Panel1 = new View () { Width = Dim.Fill (), Height = Dim.Fill() };
  51. Panel2 = new View () { Width = Dim.Fill (), Height = Dim.Fill () };
  52. this.Add (Panel1);
  53. this.Add (splitterLine);
  54. this.Add (Panel2);
  55. CanFocus = true;
  56. }
  57. /// <summary>
  58. /// Invoked when the <see cref="SplitterDistance"/> is changed
  59. /// </summary>
  60. public event SplitterEventHandler SplitterMoved;
  61. /// <summary>
  62. /// Raises the <see cref="SplitterMoved"/> event
  63. /// </summary>
  64. protected virtual void OnSplitterMoved ()
  65. {
  66. SplitterMoved?.Invoke (this, new SplitterEventArgs (this, splitterDistance));
  67. }
  68. /// <summary>
  69. /// Orientation of the dividing line (Horizontal or Vertical).
  70. /// </summary>
  71. public Orientation Orientation {
  72. get { return orientation; }
  73. set {
  74. orientation = value;
  75. LayoutSubviews ();
  76. }
  77. }
  78. public override void LayoutSubviews ()
  79. {
  80. if(this.IsRootSplitContainer()) {
  81. var contentArea = Bounds;
  82. if(HasBorder())
  83. {
  84. // TODO: Bound with Max/Min
  85. contentArea = new Rect(
  86. contentArea.X + 1,
  87. contentArea.Y + 1,
  88. Math.Max (0, contentArea.Width - 2),
  89. Math.Max (0, contentArea.Height - 2));
  90. }
  91. else if(HasAnyTitles() && IsRootSplitContainer())
  92. {
  93. // TODO: Bound with Max/Min
  94. contentArea = new Rect(
  95. contentArea.X,
  96. contentArea.Y + 1,
  97. contentArea.Width,
  98. Math.Max(0,contentArea.Height - 1));
  99. }
  100. Setup (contentArea);
  101. }
  102. base.LayoutSubviews ();
  103. }
  104. /// <summary>
  105. /// <para>Distance Horizontally or Vertically to the splitter line when
  106. /// neither panel is collapsed.
  107. /// </para>
  108. /// <para>Only absolute values (e.g. 10) and percent values (i.e. <see cref="Pos.Percent(float)"/>)
  109. /// are supported for this property.</para>
  110. /// </summary>
  111. public Pos SplitterDistance {
  112. get { return splitterDistance; }
  113. set {
  114. if (!(value is Pos.PosAbsolute) && !(value is Pos.PosFactor)) {
  115. throw new ArgumentException ($"Only Percent and Absolute values are supported for {nameof (SplitterDistance)} property. Passed value was {value.GetType ().Name}");
  116. }
  117. splitterDistance = value;
  118. GetRootSplitContainer ().LayoutSubviews ();
  119. OnSplitterMoved ();
  120. }
  121. }
  122. /// <inheritdoc/>
  123. public override bool OnEnter (View view)
  124. {
  125. Driver.SetCursorVisibility (CursorVisibility.Invisible);
  126. return base.OnEnter (view);
  127. }
  128. /// <inheritdoc/>
  129. public override void Redraw (Rect bounds)
  130. {
  131. var childTitles = new List<ChildSplitterLine> ();
  132. Driver.SetAttribute (ColorScheme.Normal);
  133. Clear ();
  134. base.Redraw (bounds);
  135. var lc = new LineCanvas(Application.Driver);
  136. var allLines = GetAllChildSplitContainerLineViewRecursively (this);
  137. if (IsRootSplitContainer())
  138. {
  139. if(HasBorder ()) {
  140. lc.AddLine (new Point (0, 0), bounds.Width - 1, Orientation.Horizontal, IntegratedBorder);
  141. lc.AddLine (new Point (0, 0), bounds.Height - 1, Orientation.Vertical, IntegratedBorder);
  142. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Width + 1, Orientation.Horizontal, IntegratedBorder);
  143. lc.AddLine (new Point (bounds.Width - 1, bounds.Height - 1), -bounds.Height + 1, Orientation.Vertical, IntegratedBorder);
  144. }
  145. foreach (var line in allLines.Where(l=>l.Visible))
  146. {
  147. bool isRoot = line == splitterLine;
  148. line.ViewToScreen(0,0,out var x1,out var y1);
  149. var origin = ScreenToView(x1,y1);
  150. var length = line.Orientation == Orientation.Horizontal ?
  151. line.Frame.Width - 1 :
  152. line.Frame.Height - 1;
  153. if(!isRoot) {
  154. if(line.Orientation == Orientation.Horizontal) {
  155. origin.X -= 1;
  156. } else {
  157. origin.Y -= 1;
  158. }
  159. length += 2;
  160. childTitles.Add (
  161. new ChildSplitterLine(line));
  162. }
  163. lc.AddLine(origin,length,line.Orientation,IntegratedBorder);
  164. }
  165. }
  166. Driver.SetAttribute (ColorScheme.Normal);
  167. lc.Draw(this,bounds);
  168. // Redraw the lines so that focus/drag symbol renders
  169. foreach(var line in allLines) {
  170. line.DrawSplitterSymbol ();
  171. }
  172. foreach(var child in childTitles) {
  173. child.DrawTitles ();
  174. }
  175. // Draw Titles over Border
  176. var screen = ViewToScreen (new Rect(0,0,bounds.Width,1));
  177. if (Panel1.Visible && Panel1Title.Length > 0) {
  178. Driver.SetAttribute (Panel1.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
  179. Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, Panel1.Frame.Width, 0), Panel1Title, 0, 0, 0, 0);
  180. }
  181. if (splitterLine.Visible) {
  182. screen = ViewToScreen (splitterLine.Frame);
  183. } else {
  184. screen.X--;
  185. //screen.Y--;
  186. }
  187. if (Orientation == Orientation.Horizontal) {
  188. if (Panel2.Visible && Panel2Title?.Length > 0) {
  189. Driver.SetAttribute (Panel2.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
  190. Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, Panel2.Bounds.Width, 1), Panel2Title, 0, 0, 0, 0);
  191. }
  192. } else {
  193. if (Panel2.Visible && Panel2Title?.Length > 0) {
  194. Driver.SetAttribute (Panel2.HasFocus ? ColorScheme.HotNormal : ColorScheme.Normal);
  195. Driver.DrawWindowTitle (new Rect (screen.X, screen.Y, Panel2.Bounds.Width, 1), Panel2Title, 0, 0, 0, 0);
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// Converts <see cref="Panel1"/> from a regular <see cref="View"/>
  201. /// container to a new nested <see cref="SplitContainer"/>. If <see cref="Panel1"/>
  202. /// is already a <see cref="SplitContainer"/> then returns false.
  203. /// </summary>
  204. /// <remarks>After successful splitting, the returned container's <see cref="Panel1"/>
  205. /// will contain the original content (if any) while <see cref="Panel2"/> will be empty and available
  206. /// for adding to.</remarks>
  207. /// <param name="result">The new <see cref="SplitContainer"/> now showing in
  208. /// <see cref="Panel1"/> or the existing one if it was already been converted before.</param>
  209. /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
  210. /// <see cref="SplitContainer"/>. <see langword="false"/> if it was already a nested
  211. /// <see cref="SplitContainer"/></returns>
  212. public bool TrySplitPanel1(out SplitContainer result)
  213. {
  214. return TrySplit (
  215. () => this.Panel1,
  216. (n) => this.Panel1 = n,
  217. out result);
  218. }
  219. /// <summary>
  220. /// Converts <see cref="Panel2"/> from a regular <see cref="View"/>
  221. /// container to a new nested <see cref="SplitContainer"/>. If <see cref="Panel2"/>
  222. /// is already a <see cref="SplitContainer"/> then returns false.
  223. /// </summary>
  224. /// <remarks>After successful splitting, the returned container's <see cref="Panel1"/>
  225. /// will contain the original content (if any) while <see cref="Panel2"/> will be empty and available
  226. /// for adding to.</remarks>
  227. /// <param name="result">The new <see cref="SplitContainer"/> now showing in
  228. /// <see cref="Panel2"/> or the existing one if it was already been converted before.</param>
  229. /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
  230. /// <see cref="SplitContainer"/>. <see langword="false"/> if it was already a nested
  231. /// <see cref="SplitContainer"/></returns>
  232. public bool TrySplitPanel2 (out SplitContainer result)
  233. {
  234. return TrySplit (
  235. () => this.Panel2,
  236. (n) => this.Panel2 = n,
  237. out result);
  238. }
  239. private bool TrySplit(
  240. Func<View> getter,
  241. Action<SplitContainer> newSplitContainerSetter,
  242. out SplitContainer result)
  243. {
  244. // Get the current panel contents (Panel1 or Panel2)
  245. var toMove = getter();
  246. if (toMove is SplitContainer existing) {
  247. result = existing;
  248. return false;
  249. }
  250. var newContainer = new SplitContainer {
  251. Width = Dim.Fill (),
  252. Height = Dim.Fill (),
  253. };
  254. // Replace current child contents
  255. Remove (toMove);
  256. Add (newContainer);
  257. // Set Panel (1 or 2) to the new container
  258. newSplitContainerSetter(newContainer);
  259. // Set the original content into the first panel of the new container
  260. newContainer.Add (toMove);
  261. newContainer.Panel1 = toMove;
  262. result = newContainer;
  263. return true;
  264. }
  265. private List<SplitContainerLineView> GetAllChildSplitContainerLineViewRecursively (View v)
  266. {
  267. var lines = new List<SplitContainerLineView>();
  268. foreach(var sub in v.Subviews)
  269. {
  270. if(sub is SplitContainerLineView s)
  271. {
  272. lines.Add(s);
  273. }
  274. else {
  275. lines.AddRange(GetAllChildSplitContainerLineViewRecursively(sub));
  276. }
  277. }
  278. return lines;
  279. }
  280. private bool IsRootSplitContainer ()
  281. {
  282. // TODO: don't want to layout subviews since the parent recursively lays them all out
  283. return parentSplitPanel == null;
  284. }
  285. private SplitContainer GetRootSplitContainer ()
  286. {
  287. SplitContainer root = this;
  288. while (root.parentSplitPanel != null) {
  289. root = root.parentSplitPanel;
  290. }
  291. return root;
  292. }
  293. private void Setup (Rect bounds)
  294. {
  295. splitterLine.Orientation = Orientation;
  296. // splitterLine.Text = Panel2.Title;
  297. // TODO: Recursion
  298. if (!Panel1.Visible || !Panel2.Visible) {
  299. View toFullSize = !Panel1.Visible ? Panel2 : Panel1;
  300. splitterLine.Visible = false;
  301. toFullSize.X = bounds.X;
  302. toFullSize.Y = bounds.Y;
  303. toFullSize.Width = bounds.Width;
  304. toFullSize.Height = bounds.Height;
  305. } else {
  306. splitterLine.Visible = true;
  307. splitterDistance = BoundByMinimumSizes (splitterDistance);
  308. Panel1.X = bounds.X;
  309. Panel1.Y = bounds.Y;
  310. switch (Orientation) {
  311. case Orientation.Horizontal:
  312. splitterLine.X = 0;
  313. splitterLine.Y = splitterDistance;
  314. splitterLine.Width = Dim.Fill ();
  315. splitterLine.Height = 1;
  316. splitterLine.LineRune = Driver.HLine;
  317. Panel1.Width = Dim.Fill (HasBorder()? 1:0);
  318. Panel1.Height = new Dim.DimFunc (() =>
  319. splitterDistance.Anchor (bounds.Height));
  320. Panel2.Y = Pos.Bottom (splitterLine);
  321. Panel2.X = bounds.X;
  322. Panel2.Width = bounds.Width;
  323. Panel2.Height = Dim.Fill(HasBorder () ? 1 : 0);
  324. break;
  325. case Orientation.Vertical:
  326. splitterLine.X = splitterDistance;
  327. splitterLine.Y = 0;
  328. splitterLine.Width = 1;
  329. splitterLine.Height = Dim.Fill ();
  330. splitterLine.LineRune = Driver.VLine;
  331. Panel1.Height = Dim.Fill();
  332. Panel1.Width = new Dim.DimFunc (() =>
  333. splitterDistance.Anchor (bounds.Width));
  334. Panel2.X = Pos.Right (splitterLine);
  335. Panel2.Y = bounds.Y;
  336. Panel2.Height = bounds.Height;
  337. Panel2.Width = Dim.Fill(HasBorder()? 1:0);
  338. break;
  339. default: throw new ArgumentOutOfRangeException (nameof (orientation));
  340. };
  341. }
  342. }
  343. /// <summary>
  344. /// Considers <paramref name="pos"/> as a candidate for <see cref="splitterDistance"/>
  345. /// then either returns (if valid) or returns adjusted if invalid with respect to the
  346. /// <see cref="SplitterPanel.MinSize"/> of the panels.
  347. /// </summary>
  348. /// <param name="pos"></param>
  349. /// <returns></returns>
  350. private Pos BoundByMinimumSizes (Pos pos)
  351. {
  352. // if we are not yet initialized then we don't know
  353. // how big we are and therefore cannot sensibly calculate
  354. // how big the panels will be with a given SplitterDistance
  355. if (!IsInitialized) {
  356. return pos;
  357. }
  358. var panel1MinSize = Panel1MinSize;
  359. var panel2MinSize = Panel2MinSize;
  360. // if there is a border then there is less space
  361. // for the panels so we need to make size restrictions
  362. // tighter.
  363. if(HasBorder()) {
  364. panel1MinSize++;
  365. panel2MinSize++;
  366. }
  367. var availableSpace = Orientation == Orientation.Horizontal ? this.Bounds.Height : this.Bounds.Width;
  368. // we probably haven't finished layout even if IsInitialized is true :(
  369. if(availableSpace <= 0) {
  370. return pos;
  371. }
  372. var idealPosition = pos.Anchor (availableSpace);
  373. // bad position because not enough space for Panel1
  374. if (idealPosition < panel1MinSize) {
  375. // TODO: we should preserve Absolute/Percent status here not just force it to absolute
  376. return (Pos)Math.Min (panel1MinSize, availableSpace);
  377. }
  378. // bad position because not enough space for Panel2
  379. if (availableSpace - idealPosition <= panel2MinSize) {
  380. // TODO: we should preserve Absolute/Percent status here not just force it to absolute
  381. // +1 is to allow space for the splitter
  382. return (Pos)Math.Max (availableSpace - (panel2MinSize + 1), 0);
  383. }
  384. // this splitter position is fine, there is enough space for everyone
  385. return pos;
  386. }
  387. private class SplitContainerLineView : LineView {
  388. public SplitContainer Parent { get; private set; }
  389. Point? dragPosition;
  390. Pos dragOrignalPos;
  391. public Point? moveRuneRenderLocation;
  392. public SplitContainerLineView (SplitContainer parent)
  393. {
  394. CanFocus = true;
  395. TabStop = true;
  396. this.Parent = parent;
  397. base.AddCommand (Command.Right, () => {
  398. return MoveSplitter (1, 0);
  399. });
  400. base.AddCommand (Command.Left, () => {
  401. return MoveSplitter (-1, 0);
  402. });
  403. base.AddCommand (Command.LineUp, () => {
  404. return MoveSplitter (0, -1);
  405. });
  406. base.AddCommand (Command.LineDown, () => {
  407. return MoveSplitter (0, 1);
  408. });
  409. AddKeyBinding (Key.CursorRight, Command.Right);
  410. AddKeyBinding (Key.CursorLeft, Command.Left);
  411. AddKeyBinding (Key.CursorUp, Command.LineUp);
  412. AddKeyBinding (Key.CursorDown, Command.LineDown);
  413. }
  414. public override bool ProcessKey (KeyEvent kb)
  415. {
  416. if (!CanFocus || !HasFocus) {
  417. return base.ProcessKey (kb);
  418. }
  419. var result = InvokeKeybindings (kb);
  420. if (result != null)
  421. return (bool)result;
  422. return base.ProcessKey (kb);
  423. }
  424. public override void PositionCursor ()
  425. {
  426. base.PositionCursor ();
  427. var location = moveRuneRenderLocation ??
  428. new Point (Bounds.Width / 2, Bounds.Height / 2);
  429. Move (location.X, location.Y);
  430. }
  431. public override bool OnEnter (View view)
  432. {
  433. Driver.SetCursorVisibility (CursorVisibility.Default);
  434. PositionCursor ();
  435. return base.OnEnter (view);
  436. }
  437. public override void Redraw (Rect bounds)
  438. {
  439. base.Redraw (bounds);
  440. DrawSplitterSymbol ();
  441. }
  442. public void DrawSplitterSymbol()
  443. {
  444. if (CanFocus && HasFocus) {
  445. var location = moveRuneRenderLocation ??
  446. new Point (Bounds.Width / 2, Bounds.Height / 2);
  447. AddRune (location.X, location.Y, Driver.Diamond);
  448. }
  449. }
  450. public override bool MouseEvent (MouseEvent mouseEvent)
  451. {
  452. if (!CanFocus) {
  453. return true;
  454. }
  455. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
  456. // Start a Drag
  457. SetFocus ();
  458. Application.EnsuresTopOnFront ();
  459. if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
  460. dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  461. dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
  462. Application.GrabMouse (this);
  463. if (Orientation == Orientation.Horizontal) {
  464. } else {
  465. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  466. }
  467. }
  468. return true;
  469. } else if (
  470. dragPosition.HasValue &&
  471. (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  472. // Continue Drag
  473. // how far has user dragged from original location?
  474. if (Orientation == Orientation.Horizontal) {
  475. int dy = mouseEvent.Y - dragPosition.Value.Y;
  476. Parent.SplitterDistance = Offset (Y, dy);
  477. moveRuneRenderLocation = new Point (mouseEvent.X, 0);
  478. } else {
  479. int dx = mouseEvent.X - dragPosition.Value.X;
  480. Parent.SplitterDistance = Offset (X, dx);
  481. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  482. }
  483. Parent.SetNeedsDisplay ();
  484. return true;
  485. }
  486. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
  487. // End Drag
  488. Application.UngrabMouse ();
  489. Driver.UncookMouse ();
  490. FinalisePosition (
  491. dragOrignalPos,
  492. Orientation == Orientation.Horizontal ? Y : X);
  493. dragPosition = null;
  494. moveRuneRenderLocation = null;
  495. }
  496. return false;
  497. }
  498. private bool MoveSplitter (int distanceX, int distanceY)
  499. {
  500. if (Orientation == Orientation.Vertical) {
  501. // Cannot move in this direction
  502. if (distanceX == 0) {
  503. return false;
  504. }
  505. var oldX = X;
  506. FinalisePosition (oldX, (Pos)Offset (X, distanceX));
  507. return true;
  508. } else {
  509. // Cannot move in this direction
  510. if (distanceY == 0) {
  511. return false;
  512. }
  513. var oldY = Y;
  514. FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
  515. return true;
  516. }
  517. }
  518. private Pos Offset (Pos pos, int delta)
  519. {
  520. var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
  521. Parent.Bounds.Height : Parent.Bounds.Width);
  522. return posAbsolute + delta;
  523. }
  524. /// <summary>
  525. /// <para>
  526. /// Moves <see cref="parent"/> <see cref="SplitContainer.SplitterDistance"/> to
  527. /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
  528. /// (absolute / relative) that <paramref name="oldValue"/> had.
  529. /// </para>
  530. /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
  531. /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
  532. /// </summary>
  533. /// <param name="oldValue"></param>
  534. /// <param name="newValue"></param>
  535. private void FinalisePosition (Pos oldValue, Pos newValue)
  536. {
  537. if (oldValue is Pos.PosFactor) {
  538. if (Orientation == Orientation.Horizontal) {
  539. Parent.SplitterDistance = ConvertToPosFactor (newValue, Parent.Bounds.Height);
  540. } else {
  541. Parent.SplitterDistance = ConvertToPosFactor (newValue, Parent.Bounds.Width);
  542. }
  543. } else {
  544. Parent.SplitterDistance = newValue;
  545. }
  546. }
  547. /// <summary>
  548. /// <para>
  549. /// Determines the absolute position of <paramref name="p"/> and
  550. /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
  551. /// </para>
  552. /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
  553. /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
  554. /// </summary>
  555. /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
  556. /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
  557. /// <returns></returns>
  558. private Pos ConvertToPosFactor (Pos p, int parentLength)
  559. {
  560. // calculate position in the 'middle' of the cell at p distance along parentLength
  561. float position = p.Anchor (parentLength) + 0.5f;
  562. return new Pos.PosFactor (position / parentLength);
  563. }
  564. }
  565. private bool HasBorder ()
  566. {
  567. return IntegratedBorder != BorderStyle.None;
  568. }
  569. private bool HasAnyTitles()
  570. {
  571. return Panel1Title.Length > 0 || Panel2Title.Length > 0;
  572. }
  573. private class ChildSplitterLine {
  574. readonly SplitContainerLineView currentLine;
  575. internal ChildSplitterLine (SplitContainerLineView currentLine)
  576. {
  577. this.currentLine = currentLine;
  578. }
  579. internal void DrawTitles ()
  580. {
  581. if(currentLine.Orientation == Orientation.Horizontal)
  582. {
  583. var screenRect = currentLine.ViewToScreen (
  584. new Rect(0,0,currentLine.Frame.Width,currentLine.Frame.Height));
  585. Driver.DrawWindowTitle (screenRect, currentLine.Parent.Panel2Title, 0, 0, 0, 0);
  586. }
  587. }
  588. }
  589. }
  590. /// <summary>
  591. /// Provides data for <see cref="SplitContainer"/> events.
  592. /// </summary>
  593. public class SplitterEventArgs : EventArgs {
  594. /// <summary>
  595. /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
  596. /// </summary>
  597. /// <param name="splitContainer"></param>
  598. /// <param name="splitterDistance"></param>
  599. public SplitterEventArgs (SplitContainer splitContainer, Pos splitterDistance)
  600. {
  601. SplitterDistance = splitterDistance;
  602. SplitContainer = splitContainer;
  603. }
  604. /// <summary>
  605. /// New position of the <see cref="SplitContainer.SplitterDistance"/>
  606. /// </summary>
  607. public Pos SplitterDistance { get; }
  608. /// <summary>
  609. /// Container (sender) of the event.
  610. /// </summary>
  611. public SplitContainer SplitContainer { get; }
  612. }
  613. /// <summary>
  614. /// Represents a method that will handle splitter events.
  615. /// </summary>
  616. public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
  617. }