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