SplitContainer.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 and <see cref="Panel1Title"/> (if any) while
  204. /// <see cref="Panel2"/> will be empty and available for adding to.
  205. /// for adding to.</remarks>
  206. /// <param name="result">The new <see cref="SplitContainer"/> now showing in
  207. /// <see cref="Panel1"/> or the existing one if it was already been converted before.</param>
  208. /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
  209. /// <see cref="SplitContainer"/>. <see langword="false"/> if it was already a nested
  210. /// <see cref="SplitContainer"/></returns>
  211. public bool TrySplitPanel1(out SplitContainer result)
  212. {
  213. // when splitting a panel into 2 sub panels we will need to migrate
  214. // the title too
  215. var title = Panel1Title;
  216. bool returnValue = TrySplit (
  217. this.Panel1,
  218. (newSplitContainer) => {
  219. this.Panel1 = newSplitContainer;
  220. // Move title to new container
  221. Panel1Title = string.Empty;
  222. newSplitContainer.Panel1Title = title;
  223. },
  224. out result);
  225. return returnValue;
  226. }
  227. /// <summary>
  228. /// Converts <see cref="Panel2"/> from a regular <see cref="View"/>
  229. /// container to a new nested <see cref="SplitContainer"/>. If <see cref="Panel2"/>
  230. /// is already a <see cref="SplitContainer"/> then returns false.
  231. /// </summary>
  232. /// <remarks>After successful splitting, the returned container's <see cref="Panel1"/>
  233. /// will contain the original content and <see cref="Panel2Title"/> (if any) while
  234. /// <see cref="Panel2"/> will be empty and available for adding to.
  235. /// for adding to.</remarks>
  236. /// <param name="result">The new <see cref="SplitContainer"/> now showing in
  237. /// <see cref="Panel2"/> or the existing one if it was already been converted before.</param>
  238. /// <returns><see langword="true"/> if a <see cref="View"/> was converted to a new nested
  239. /// <see cref="SplitContainer"/>. <see langword="false"/> if it was already a nested
  240. /// <see cref="SplitContainer"/></returns>
  241. public bool TrySplitPanel2 (out SplitContainer result)
  242. {
  243. // when splitting a panel into 2 sub panels we will need to migrate
  244. // the title too
  245. var title = Panel2Title;
  246. bool returnValue = TrySplit (
  247. this.Panel2,
  248. (newSplitContainer) => {
  249. this.Panel2 = newSplitContainer;
  250. // Move title to new container
  251. Panel2Title = string.Empty;
  252. // Content always goes into Panel1 of the new container
  253. // so that is where the title goes too
  254. newSplitContainer.Panel1Title = title;
  255. },
  256. out result);
  257. return returnValue;
  258. }
  259. private bool TrySplit(
  260. View toMove,
  261. Action<SplitContainer> newSplitContainerSetter,
  262. out SplitContainer result)
  263. {
  264. if (toMove is SplitContainer existing) {
  265. result = existing;
  266. return false;
  267. }
  268. var newContainer = new SplitContainer {
  269. Width = Dim.Fill (),
  270. Height = Dim.Fill (),
  271. parentSplitPanel = this,
  272. };
  273. // Take everything out of the Panel we are moving
  274. var childViews = toMove.Subviews.ToArray();
  275. toMove.RemoveAll ();
  276. // Remove the panel itself and replace it with the new SplitContainer
  277. Remove (toMove);
  278. Add (newContainer);
  279. newSplitContainerSetter(newContainer);
  280. // Add the original content into the first panel of the new container
  281. foreach(var childView in childViews) {
  282. newContainer.Panel1.Add (childView);
  283. }
  284. result = newContainer;
  285. return true;
  286. }
  287. private List<SplitContainerLineView> GetAllChildSplitContainerLineViewRecursively (View v)
  288. {
  289. var lines = new List<SplitContainerLineView>();
  290. foreach(var sub in v.Subviews)
  291. {
  292. if(sub is SplitContainerLineView s)
  293. {
  294. if(s.Parent.GetRootSplitContainer() == this) {
  295. lines.Add (s);
  296. }
  297. }
  298. else {
  299. lines.AddRange(GetAllChildSplitContainerLineViewRecursively(sub));
  300. }
  301. }
  302. return lines;
  303. }
  304. private bool IsRootSplitContainer ()
  305. {
  306. // TODO: don't want to layout subviews since the parent recursively lays them all out
  307. return parentSplitPanel == null;
  308. }
  309. private SplitContainer GetRootSplitContainer ()
  310. {
  311. SplitContainer root = this;
  312. while (root.parentSplitPanel != null) {
  313. root = root.parentSplitPanel;
  314. }
  315. return root;
  316. }
  317. private void Setup (Rect bounds)
  318. {
  319. splitterLine.Orientation = Orientation;
  320. // splitterLine.Text = Panel2.Title;
  321. // TODO: Recursion
  322. if (!Panel1.Visible || !Panel2.Visible) {
  323. View toFullSize = !Panel1.Visible ? Panel2 : Panel1;
  324. splitterLine.Visible = false;
  325. toFullSize.X = bounds.X;
  326. toFullSize.Y = bounds.Y;
  327. toFullSize.Width = bounds.Width;
  328. toFullSize.Height = bounds.Height;
  329. } else {
  330. splitterLine.Visible = true;
  331. splitterDistance = BoundByMinimumSizes (splitterDistance);
  332. Panel1.X = bounds.X;
  333. Panel1.Y = bounds.Y;
  334. switch (Orientation) {
  335. case Orientation.Horizontal:
  336. splitterLine.X = 0;
  337. splitterLine.Y = splitterDistance;
  338. splitterLine.Width = Dim.Fill ();
  339. splitterLine.Height = 1;
  340. splitterLine.LineRune = Driver.HLine;
  341. Panel1.Width = Dim.Fill (HasBorder()? 1:0);
  342. Panel1.Height = new Dim.DimFunc (() =>
  343. splitterDistance.Anchor (bounds.Height));
  344. Panel2.Y = Pos.Bottom (splitterLine);
  345. Panel2.X = bounds.X;
  346. Panel2.Width = bounds.Width;
  347. Panel2.Height = Dim.Fill(HasBorder () ? 1 : 0);
  348. break;
  349. case Orientation.Vertical:
  350. splitterLine.X = splitterDistance;
  351. splitterLine.Y = 0;
  352. splitterLine.Width = 1;
  353. splitterLine.Height = Dim.Fill ();
  354. splitterLine.LineRune = Driver.VLine;
  355. Panel1.Height = Dim.Fill();
  356. Panel1.Width = new Dim.DimFunc (() =>
  357. splitterDistance.Anchor (bounds.Width));
  358. Panel2.X = Pos.Right (splitterLine);
  359. Panel2.Y = bounds.Y;
  360. Panel2.Height = bounds.Height;
  361. Panel2.Width = Dim.Fill(HasBorder()? 1:0);
  362. break;
  363. default: throw new ArgumentOutOfRangeException (nameof (orientation));
  364. };
  365. }
  366. }
  367. /// <summary>
  368. /// Considers <paramref name="pos"/> as a candidate for <see cref="splitterDistance"/>
  369. /// then either returns (if valid) or returns adjusted if invalid with respect to the
  370. /// <see cref="SplitterPanel.MinSize"/> of the panels.
  371. /// </summary>
  372. /// <param name="pos"></param>
  373. /// <returns></returns>
  374. private Pos BoundByMinimumSizes (Pos pos)
  375. {
  376. // if we are not yet initialized then we don't know
  377. // how big we are and therefore cannot sensibly calculate
  378. // how big the panels will be with a given SplitterDistance
  379. if (!IsInitialized) {
  380. return pos;
  381. }
  382. var panel1MinSize = Panel1MinSize;
  383. var panel2MinSize = Panel2MinSize;
  384. // if there is a border then there is less space
  385. // for the panels so we need to make size restrictions
  386. // tighter.
  387. if(HasBorder()) {
  388. panel1MinSize++;
  389. panel2MinSize++;
  390. }
  391. var availableSpace = Orientation == Orientation.Horizontal ? this.Bounds.Height : this.Bounds.Width;
  392. // we probably haven't finished layout even if IsInitialized is true :(
  393. if(availableSpace <= 0) {
  394. return pos;
  395. }
  396. var idealPosition = pos.Anchor (availableSpace);
  397. // bad position because not enough space for Panel1
  398. if (idealPosition < panel1MinSize) {
  399. // TODO: we should preserve Absolute/Percent status here not just force it to absolute
  400. return (Pos)Math.Min (panel1MinSize, availableSpace);
  401. }
  402. // bad position because not enough space for Panel2
  403. if (availableSpace - idealPosition <= panel2MinSize) {
  404. // TODO: we should preserve Absolute/Percent status here not just force it to absolute
  405. // +1 is to allow space for the splitter
  406. return (Pos)Math.Max (availableSpace - (panel2MinSize + 1), 0);
  407. }
  408. // this splitter position is fine, there is enough space for everyone
  409. return pos;
  410. }
  411. private class SplitContainerLineView : LineView {
  412. public SplitContainer Parent { get; private set; }
  413. Point? dragPosition;
  414. Pos dragOrignalPos;
  415. public Point? moveRuneRenderLocation;
  416. public SplitContainerLineView (SplitContainer parent)
  417. {
  418. CanFocus = true;
  419. TabStop = true;
  420. this.Parent = parent;
  421. base.AddCommand (Command.Right, () => {
  422. return MoveSplitter (1, 0);
  423. });
  424. base.AddCommand (Command.Left, () => {
  425. return MoveSplitter (-1, 0);
  426. });
  427. base.AddCommand (Command.LineUp, () => {
  428. return MoveSplitter (0, -1);
  429. });
  430. base.AddCommand (Command.LineDown, () => {
  431. return MoveSplitter (0, 1);
  432. });
  433. AddKeyBinding (Key.CursorRight, Command.Right);
  434. AddKeyBinding (Key.CursorLeft, Command.Left);
  435. AddKeyBinding (Key.CursorUp, Command.LineUp);
  436. AddKeyBinding (Key.CursorDown, Command.LineDown);
  437. }
  438. public override bool ProcessKey (KeyEvent kb)
  439. {
  440. if (!CanFocus || !HasFocus) {
  441. return base.ProcessKey (kb);
  442. }
  443. var result = InvokeKeybindings (kb);
  444. if (result != null)
  445. return (bool)result;
  446. return base.ProcessKey (kb);
  447. }
  448. public override void PositionCursor ()
  449. {
  450. base.PositionCursor ();
  451. var location = moveRuneRenderLocation ??
  452. new Point (Bounds.Width / 2, Bounds.Height / 2);
  453. Move (location.X, location.Y);
  454. }
  455. public override bool OnEnter (View view)
  456. {
  457. Driver.SetCursorVisibility (CursorVisibility.Default);
  458. PositionCursor ();
  459. return base.OnEnter (view);
  460. }
  461. public override void Redraw (Rect bounds)
  462. {
  463. base.Redraw (bounds);
  464. DrawSplitterSymbol ();
  465. }
  466. public void DrawSplitterSymbol()
  467. {
  468. if (CanFocus && HasFocus) {
  469. var location = moveRuneRenderLocation ??
  470. new Point (Bounds.Width / 2, Bounds.Height / 2);
  471. AddRune (location.X, location.Y, Driver.Diamond);
  472. }
  473. }
  474. public override bool MouseEvent (MouseEvent mouseEvent)
  475. {
  476. if (!CanFocus) {
  477. return true;
  478. }
  479. if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed)) {
  480. // Start a Drag
  481. SetFocus ();
  482. Application.EnsuresTopOnFront ();
  483. if (mouseEvent.Flags == MouseFlags.Button1Pressed) {
  484. dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  485. dragOrignalPos = Orientation == Orientation.Horizontal ? Y : X;
  486. Application.GrabMouse (this);
  487. if (Orientation == Orientation.Horizontal) {
  488. } else {
  489. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  490. }
  491. }
  492. return true;
  493. } else if (
  494. dragPosition.HasValue &&
  495. (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
  496. // Continue Drag
  497. // how far has user dragged from original location?
  498. if (Orientation == Orientation.Horizontal) {
  499. int dy = mouseEvent.Y - dragPosition.Value.Y;
  500. Parent.SplitterDistance = Offset (Y, dy);
  501. moveRuneRenderLocation = new Point (mouseEvent.X, 0);
  502. } else {
  503. int dx = mouseEvent.X - dragPosition.Value.X;
  504. Parent.SplitterDistance = Offset (X, dx);
  505. moveRuneRenderLocation = new Point (0, Math.Max (1, Math.Min (Bounds.Height - 2, mouseEvent.Y)));
  506. }
  507. Parent.SetNeedsDisplay ();
  508. return true;
  509. }
  510. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && dragPosition.HasValue) {
  511. // End Drag
  512. Application.UngrabMouse ();
  513. Driver.UncookMouse ();
  514. FinalisePosition (
  515. dragOrignalPos,
  516. Orientation == Orientation.Horizontal ? Y : X);
  517. dragPosition = null;
  518. moveRuneRenderLocation = null;
  519. }
  520. return false;
  521. }
  522. private bool MoveSplitter (int distanceX, int distanceY)
  523. {
  524. if (Orientation == Orientation.Vertical) {
  525. // Cannot move in this direction
  526. if (distanceX == 0) {
  527. return false;
  528. }
  529. var oldX = X;
  530. FinalisePosition (oldX, (Pos)Offset (X, distanceX));
  531. return true;
  532. } else {
  533. // Cannot move in this direction
  534. if (distanceY == 0) {
  535. return false;
  536. }
  537. var oldY = Y;
  538. FinalisePosition (oldY, (Pos)Offset (Y, distanceY));
  539. return true;
  540. }
  541. }
  542. private Pos Offset (Pos pos, int delta)
  543. {
  544. var posAbsolute = pos.Anchor (Orientation == Orientation.Horizontal ?
  545. Parent.Bounds.Height : Parent.Bounds.Width);
  546. return posAbsolute + delta;
  547. }
  548. /// <summary>
  549. /// <para>
  550. /// Moves <see cref="parent"/> <see cref="SplitContainer.SplitterDistance"/> to
  551. /// <see cref="Pos"/> <paramref name="newValue"/> preserving <see cref="Pos"/> format
  552. /// (absolute / relative) that <paramref name="oldValue"/> had.
  553. /// </para>
  554. /// <remarks>This ensures that if splitter location was e.g. 50% before and you move it
  555. /// to absolute 5 then you end up with 10% (assuming a parent had 50 width). </remarks>
  556. /// </summary>
  557. /// <param name="oldValue"></param>
  558. /// <param name="newValue"></param>
  559. private void FinalisePosition (Pos oldValue, Pos newValue)
  560. {
  561. if (oldValue is Pos.PosFactor) {
  562. if (Orientation == Orientation.Horizontal) {
  563. Parent.SplitterDistance = ConvertToPosFactor (newValue, Parent.Bounds.Height);
  564. } else {
  565. Parent.SplitterDistance = ConvertToPosFactor (newValue, Parent.Bounds.Width);
  566. }
  567. } else {
  568. Parent.SplitterDistance = newValue;
  569. }
  570. }
  571. /// <summary>
  572. /// <para>
  573. /// Determines the absolute position of <paramref name="p"/> and
  574. /// returns a <see cref="Pos.PosFactor"/> that describes the percentage of that.
  575. /// </para>
  576. /// <para>Effectively turning any <see cref="Pos"/> into a <see cref="Pos.PosFactor"/>
  577. /// (as if created with <see cref="Pos.Percent(float)"/>)</para>
  578. /// </summary>
  579. /// <param name="p">The <see cref="Pos"/> to convert to <see cref="Pos.Percent(float)"/></param>
  580. /// <param name="parentLength">The Height/Width that <paramref name="p"/> lies within</param>
  581. /// <returns></returns>
  582. private Pos ConvertToPosFactor (Pos p, int parentLength)
  583. {
  584. // calculate position in the 'middle' of the cell at p distance along parentLength
  585. float position = p.Anchor (parentLength) + 0.5f;
  586. return new Pos.PosFactor (position / parentLength);
  587. }
  588. }
  589. private bool HasBorder ()
  590. {
  591. return IntegratedBorder != BorderStyle.None;
  592. }
  593. private bool HasAnyTitles()
  594. {
  595. return Panel1Title.Length > 0 || Panel2Title.Length > 0;
  596. }
  597. private class ChildSplitterLine {
  598. readonly SplitContainerLineView currentLine;
  599. internal ChildSplitterLine (SplitContainerLineView currentLine)
  600. {
  601. this.currentLine = currentLine;
  602. }
  603. internal void DrawTitles ()
  604. {
  605. if(currentLine.Orientation == Orientation.Horizontal)
  606. {
  607. var screenRect = currentLine.ViewToScreen (
  608. new Rect(0,0,currentLine.Frame.Width,currentLine.Frame.Height));
  609. Driver.DrawWindowTitle (screenRect, currentLine.Parent.Panel2Title, 0, 0, 0, 0);
  610. }
  611. }
  612. }
  613. }
  614. /// <summary>
  615. /// Provides data for <see cref="SplitContainer"/> events.
  616. /// </summary>
  617. public class SplitterEventArgs : EventArgs {
  618. /// <summary>
  619. /// Creates a new instance of the <see cref="SplitterEventArgs"/> class.
  620. /// </summary>
  621. /// <param name="splitContainer"></param>
  622. /// <param name="splitterDistance"></param>
  623. public SplitterEventArgs (SplitContainer splitContainer, Pos splitterDistance)
  624. {
  625. SplitterDistance = splitterDistance;
  626. SplitContainer = splitContainer;
  627. }
  628. /// <summary>
  629. /// New position of the <see cref="SplitContainer.SplitterDistance"/>
  630. /// </summary>
  631. public Pos SplitterDistance { get; }
  632. /// <summary>
  633. /// Container (sender) of the event.
  634. /// </summary>
  635. public SplitContainer SplitContainer { get; }
  636. }
  637. /// <summary>
  638. /// Represents a method that will handle splitter events.
  639. /// </summary>
  640. public delegate void SplitterEventHandler (object sender, SplitterEventArgs e);
  641. }