ListColumns.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. #nullable enable
  2. using System.Collections;
  3. using System.Data;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("ListColumns", "Implements a columned list via a data table.")]
  6. [ScenarioCategory ("TableView")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("Dialogs")]
  9. [ScenarioCategory ("Text and Formatting")]
  10. [ScenarioCategory ("Scrolling")]
  11. public class ListColumns : Scenario
  12. {
  13. private Scheme? _alternatingScheme;
  14. private DataTable? _currentTable;
  15. private TableView? _listColView;
  16. private CheckBox? _alternatingColorsCheckBox;
  17. private CheckBox? _alwaysUseNormalColorForVerticalCellLinesCheckBox;
  18. private CheckBox? _bottomlineCheckBox;
  19. private CheckBox? _cellLinesCheckBox;
  20. private CheckBox? _cursorCheckBox;
  21. private CheckBox? _expandLastColumnCheckBox;
  22. private CheckBox? _orientVerticalCheckBox;
  23. private CheckBox? _scrollParallelCheckBox;
  24. private CheckBox? _smoothScrollingCheckBox;
  25. private CheckBox? _toplineCheckBox;
  26. /// <summary>
  27. /// Builds a simple list in which values are the index. This helps testing that scrolling etc is working
  28. /// correctly and not skipping out values when paging
  29. /// </summary>
  30. /// <param name="items"></param>
  31. /// <returns></returns>
  32. public static IList BuildSimpleList (int items)
  33. {
  34. List<object> list = [];
  35. for (var i = 0; i < items; i++)
  36. {
  37. list.Add ("Item " + i);
  38. }
  39. return list;
  40. }
  41. public override void Main ()
  42. {
  43. Application.Init ();
  44. Window appWindow = new ()
  45. {
  46. Title = GetQuitKeyAndName (),
  47. BorderStyle = LineStyle.None
  48. };
  49. // MenuBar
  50. MenuBar menuBar = new ();
  51. _listColView = new ()
  52. {
  53. Y = Pos.Bottom(menuBar),
  54. Width = Dim.Fill (),
  55. Height = Dim.Fill (1),
  56. Style = new ()
  57. {
  58. ShowHeaders = false,
  59. ShowHorizontalHeaderOverline = false,
  60. ShowHorizontalHeaderUnderline = false,
  61. ShowHorizontalBottomline = false,
  62. ExpandLastColumn = false
  63. }
  64. };
  65. ListColumnStyle listColStyle = new ();
  66. // Status Bar
  67. StatusBar statusBar = new (
  68. [
  69. new (Key.F2, "OpenBigListEx", () => OpenSimpleList (true)),
  70. new (Key.F3, "CloseExample", CloseExample),
  71. new (Key.F4, "OpenSmListEx", () => OpenSimpleList (false)),
  72. new (Application.QuitKey, "Quit", Quit)
  73. ]
  74. );
  75. // Selected cell label
  76. Label selectedCellLabel = new ()
  77. {
  78. X = 0,
  79. Y = Pos.Bottom (_listColView),
  80. Text = "0,0",
  81. Width = Dim.Fill (),
  82. TextAlignment = Alignment.End
  83. };
  84. _listColView.SelectedCellChanged += (s, e) =>
  85. {
  86. if (_listColView is { })
  87. {
  88. selectedCellLabel.Text = $"{_listColView.SelectedRow},{_listColView.SelectedColumn}";
  89. }
  90. };
  91. _listColView.KeyDown += TableViewKeyPress;
  92. _alternatingScheme = new ()
  93. {
  94. Disabled = appWindow.GetAttributeForRole (VisualRole.Disabled),
  95. HotFocus = appWindow.GetAttributeForRole (VisualRole.HotFocus),
  96. Focus = appWindow.GetAttributeForRole (VisualRole.Focus),
  97. Normal = new (Color.White, Color.BrightBlue)
  98. };
  99. // if user clicks the mouse in TableView
  100. _listColView.MouseClick += (s, e) => { _listColView.ScreenToCell (e.Position, out int? clickedCol); };
  101. _listColView.KeyBindings.ReplaceCommands (Key.Space, Command.Accept);
  102. // Setup menu checkboxes
  103. _toplineCheckBox = new ()
  104. {
  105. Title = "_TopLine",
  106. CheckedState = _listColView.Style.ShowHorizontalHeaderOverline ? CheckState.Checked : CheckState.UnChecked
  107. };
  108. _toplineCheckBox.CheckedStateChanged += (s, e) => ToggleTopline ();
  109. _bottomlineCheckBox = new ()
  110. {
  111. Title = "_BottomLine",
  112. CheckedState = _listColView.Style.ShowHorizontalBottomline ? CheckState.Checked : CheckState.UnChecked
  113. };
  114. _bottomlineCheckBox.CheckedStateChanged += (s, e) => ToggleBottomline ();
  115. _cellLinesCheckBox = new ()
  116. {
  117. Title = "_CellLines",
  118. CheckedState = _listColView.Style.ShowVerticalCellLines ? CheckState.Checked : CheckState.UnChecked
  119. };
  120. _cellLinesCheckBox.CheckedStateChanged += (s, e) => ToggleCellLines ();
  121. _expandLastColumnCheckBox = new ()
  122. {
  123. Title = "_ExpandLastColumn",
  124. CheckedState = _listColView.Style.ExpandLastColumn ? CheckState.Checked : CheckState.UnChecked
  125. };
  126. _expandLastColumnCheckBox.CheckedStateChanged += (s, e) => ToggleExpandLastColumn ();
  127. _alwaysUseNormalColorForVerticalCellLinesCheckBox = new ()
  128. {
  129. Title = "_AlwaysUseNormalColorForVerticalCellLines",
  130. CheckedState = _listColView.Style.AlwaysUseNormalColorForVerticalCellLines ? CheckState.Checked : CheckState.UnChecked
  131. };
  132. _alwaysUseNormalColorForVerticalCellLinesCheckBox.CheckedStateChanged += (s, e) => ToggleAlwaysUseNormalColorForVerticalCellLines ();
  133. _smoothScrollingCheckBox = new ()
  134. {
  135. Title = "_SmoothHorizontalScrolling",
  136. CheckedState = _listColView.Style.SmoothHorizontalScrolling ? CheckState.Checked : CheckState.UnChecked
  137. };
  138. _smoothScrollingCheckBox.CheckedStateChanged += (s, e) => ToggleSmoothScrolling ();
  139. _alternatingColorsCheckBox = new ()
  140. {
  141. Title = "Alternating Colors"
  142. };
  143. _alternatingColorsCheckBox.CheckedStateChanged += (s, e) => ToggleAlternatingColors ();
  144. _cursorCheckBox = new ()
  145. {
  146. Title = "Invert Selected Cell First Character",
  147. CheckedState = _listColView.Style.InvertSelectedCellFirstCharacter ? CheckState.Checked : CheckState.UnChecked
  148. };
  149. _cursorCheckBox.CheckedStateChanged += (s, e) => ToggleInvertSelectedCellFirstCharacter ();
  150. _orientVerticalCheckBox = new ()
  151. {
  152. Title = "_OrientVertical",
  153. CheckedState = listColStyle.Orientation == Orientation.Vertical ? CheckState.Checked : CheckState.UnChecked
  154. };
  155. _orientVerticalCheckBox.CheckedStateChanged += (s, e) => ToggleVerticalOrientation ();
  156. _scrollParallelCheckBox = new ()
  157. {
  158. Title = "_ScrollParallel",
  159. CheckedState = listColStyle.ScrollParallel ? CheckState.Checked : CheckState.UnChecked
  160. };
  161. _scrollParallelCheckBox.CheckedStateChanged += (s, e) => ToggleScrollParallel ();
  162. menuBar.Add (
  163. new MenuBarItem (
  164. "_File",
  165. [
  166. new MenuItem
  167. {
  168. Title = "Open_BigListExample",
  169. Action = () => OpenSimpleList (true)
  170. },
  171. new MenuItem
  172. {
  173. Title = "Open_SmListExample",
  174. Action = () => OpenSimpleList (false)
  175. },
  176. new MenuItem
  177. {
  178. Title = "_CloseExample",
  179. Action = CloseExample
  180. },
  181. new MenuItem
  182. {
  183. Title = "_Quit",
  184. Action = Quit
  185. }
  186. ]
  187. )
  188. );
  189. menuBar.Add (
  190. new MenuBarItem (
  191. "_View",
  192. [
  193. new MenuItem
  194. {
  195. CommandView = _toplineCheckBox
  196. },
  197. new MenuItem
  198. {
  199. CommandView = _bottomlineCheckBox
  200. },
  201. new MenuItem
  202. {
  203. CommandView = _cellLinesCheckBox
  204. },
  205. new MenuItem
  206. {
  207. CommandView = _expandLastColumnCheckBox
  208. },
  209. new MenuItem
  210. {
  211. CommandView = _alwaysUseNormalColorForVerticalCellLinesCheckBox
  212. },
  213. new MenuItem
  214. {
  215. CommandView = _smoothScrollingCheckBox
  216. },
  217. new MenuItem
  218. {
  219. CommandView = _alternatingColorsCheckBox
  220. },
  221. new MenuItem
  222. {
  223. CommandView = _cursorCheckBox
  224. }
  225. ]
  226. )
  227. );
  228. menuBar.Add (
  229. new MenuBarItem (
  230. "_List",
  231. [
  232. new MenuItem
  233. {
  234. CommandView = _orientVerticalCheckBox
  235. },
  236. new MenuItem
  237. {
  238. CommandView = _scrollParallelCheckBox
  239. },
  240. new MenuItem
  241. {
  242. Title = "Set _Max Cell Width",
  243. Action = SetListMaxWidth
  244. },
  245. new MenuItem
  246. {
  247. Title = "Set Mi_n Cell Width",
  248. Action = SetListMinWidth
  249. }
  250. ]
  251. )
  252. );
  253. // Add views in order of visual appearance
  254. appWindow.Add (menuBar, _listColView, selectedCellLabel, statusBar);
  255. Application.Run (appWindow);
  256. appWindow.Dispose ();
  257. Application.Shutdown ();
  258. }
  259. private void CloseExample ()
  260. {
  261. if (_listColView is { })
  262. {
  263. _listColView.Table = null;
  264. }
  265. }
  266. private void OpenSimpleList (bool big) { SetTable (BuildSimpleList (big ? 1023 : 31)); }
  267. private void Quit () { Application.RequestStop (); }
  268. private void RunListWidthDialog (string prompt, Action<TableView, int> setter, Func<TableView, int> getter)
  269. {
  270. if (_listColView is null)
  271. {
  272. return;
  273. }
  274. var accepted = false;
  275. Button ok = new () { Text = "Ok", IsDefault = true };
  276. ok.Accepting += (s, e) =>
  277. {
  278. accepted = true;
  279. Application.RequestStop ();
  280. };
  281. Button cancel = new () { Text = "Cancel" };
  282. cancel.Accepting += (s, e) => { Application.RequestStop (); };
  283. Dialog d = new () { Title = prompt, Buttons = [ok, cancel] };
  284. TextField tf = new () { Text = getter (_listColView).ToString (), X = 0, Y = 0, Width = Dim.Fill () };
  285. d.Add (tf);
  286. tf.SetFocus ();
  287. Application.Run (d);
  288. d.Dispose ();
  289. if (accepted)
  290. {
  291. try
  292. {
  293. setter (_listColView, int.Parse (tf.Text));
  294. }
  295. catch (Exception ex)
  296. {
  297. MessageBox.ErrorQuery (Application.Instance, 60, 20, "Failed to set", ex.Message, "Ok");
  298. }
  299. }
  300. }
  301. private void SetListMaxWidth ()
  302. {
  303. RunListWidthDialog ("MaxCellWidth", (s, v) => s.MaxCellWidth = v, s => s.MaxCellWidth);
  304. _listColView?.SetNeedsDraw ();
  305. }
  306. private void SetListMinWidth ()
  307. {
  308. RunListWidthDialog ("MinCellWidth", (s, v) => s.MinCellWidth = v, s => s.MinCellWidth);
  309. _listColView?.SetNeedsDraw ();
  310. }
  311. private void SetTable (IList list)
  312. {
  313. if (_listColView is null)
  314. {
  315. return;
  316. }
  317. _listColView.Table = new ListTableSource (list, _listColView);
  318. if (_listColView.Table is ListTableSource listTableSource)
  319. {
  320. _currentTable = listTableSource.DataTable;
  321. }
  322. }
  323. private void TableViewKeyPress (object? sender, Key e)
  324. {
  325. if (_currentTable is null || _listColView is null)
  326. {
  327. return;
  328. }
  329. if (e.KeyCode == Key.Delete)
  330. {
  331. // set all selected cells to null
  332. foreach (Point pt in _listColView.GetAllSelectedCells ())
  333. {
  334. _currentTable.Rows [pt.Y] [pt.X] = DBNull.Value;
  335. }
  336. _listColView.Update ();
  337. e.Handled = true;
  338. }
  339. }
  340. private void ToggleAlternatingColors ()
  341. {
  342. if (_listColView is null || _alternatingColorsCheckBox is null)
  343. {
  344. return;
  345. }
  346. if (_alternatingColorsCheckBox.CheckedState == CheckState.Checked)
  347. {
  348. _listColView.Style.RowColorGetter = a => a.RowIndex % 2 == 0 ? _alternatingScheme : null;
  349. }
  350. else
  351. {
  352. _listColView.Style.RowColorGetter = null;
  353. }
  354. _listColView.SetNeedsDraw ();
  355. }
  356. private void ToggleAlwaysUseNormalColorForVerticalCellLines ()
  357. {
  358. if (_listColView is null || _alwaysUseNormalColorForVerticalCellLinesCheckBox is null)
  359. {
  360. return;
  361. }
  362. _listColView.Style.AlwaysUseNormalColorForVerticalCellLines =
  363. _alwaysUseNormalColorForVerticalCellLinesCheckBox.CheckedState == CheckState.Checked;
  364. _listColView.Update ();
  365. }
  366. private void ToggleBottomline ()
  367. {
  368. if (_listColView is null || _bottomlineCheckBox is null)
  369. {
  370. return;
  371. }
  372. _listColView.Style.ShowHorizontalBottomline = _bottomlineCheckBox.CheckedState == CheckState.Checked;
  373. _listColView.Update ();
  374. }
  375. private void ToggleCellLines ()
  376. {
  377. if (_listColView is null || _cellLinesCheckBox is null)
  378. {
  379. return;
  380. }
  381. _listColView.Style.ShowVerticalCellLines = _cellLinesCheckBox.CheckedState == CheckState.Checked;
  382. _listColView.Update ();
  383. }
  384. private void ToggleExpandLastColumn ()
  385. {
  386. if (_listColView is null || _expandLastColumnCheckBox is null)
  387. {
  388. return;
  389. }
  390. _listColView.Style.ExpandLastColumn = _expandLastColumnCheckBox.CheckedState == CheckState.Checked;
  391. _listColView.Update ();
  392. }
  393. private void ToggleInvertSelectedCellFirstCharacter ()
  394. {
  395. if (_listColView is null || _cursorCheckBox is null)
  396. {
  397. return;
  398. }
  399. _listColView.Style.InvertSelectedCellFirstCharacter = _cursorCheckBox.CheckedState == CheckState.Checked;
  400. _listColView.SetNeedsDraw ();
  401. }
  402. private void ToggleScrollParallel ()
  403. {
  404. if (_listColView?.Table is not ListTableSource listTableSource || _scrollParallelCheckBox is null)
  405. {
  406. return;
  407. }
  408. listTableSource.Style.ScrollParallel = _scrollParallelCheckBox.CheckedState == CheckState.Checked;
  409. _listColView.SetNeedsDraw ();
  410. }
  411. private void ToggleSmoothScrolling ()
  412. {
  413. if (_listColView is null || _smoothScrollingCheckBox is null)
  414. {
  415. return;
  416. }
  417. _listColView.Style.SmoothHorizontalScrolling = _smoothScrollingCheckBox.CheckedState == CheckState.Checked;
  418. _listColView.Update ();
  419. }
  420. private void ToggleTopline ()
  421. {
  422. if (_listColView is null || _toplineCheckBox is null)
  423. {
  424. return;
  425. }
  426. _listColView.Style.ShowHorizontalHeaderOverline = _toplineCheckBox.CheckedState == CheckState.Checked;
  427. _listColView.Update ();
  428. }
  429. private void ToggleVerticalOrientation ()
  430. {
  431. if (_listColView?.Table is not ListTableSource listTableSource || _orientVerticalCheckBox is null)
  432. {
  433. return;
  434. }
  435. listTableSource.Style.Orientation = _orientVerticalCheckBox.CheckedState == CheckState.Checked
  436. ? Orientation.Vertical
  437. : Orientation.Horizontal;
  438. _listColView.SetNeedsDraw ();
  439. }
  440. }