ListColumns.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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 test 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. _listColView.KeyBindings.ReplaceCommands (Key.Space, Command.Accept);
  100. // Setup menu checkboxes
  101. _topLineCheckBox = new ()
  102. {
  103. Title = "_TopLine",
  104. CheckedState = _listColView.Style.ShowHorizontalHeaderOverline ? CheckState.Checked : CheckState.UnChecked
  105. };
  106. _topLineCheckBox.CheckedStateChanged += (s, e) => ToggleTopline ();
  107. _bottomLineCheckBox = new ()
  108. {
  109. Title = "_BottomLine",
  110. CheckedState = _listColView.Style.ShowHorizontalBottomline ? CheckState.Checked : CheckState.UnChecked
  111. };
  112. _bottomLineCheckBox.CheckedStateChanged += (s, e) => ToggleBottomline ();
  113. _cellLinesCheckBox = new ()
  114. {
  115. Title = "_CellLines",
  116. CheckedState = _listColView.Style.ShowVerticalCellLines ? CheckState.Checked : CheckState.UnChecked
  117. };
  118. _cellLinesCheckBox.CheckedStateChanged += (s, e) => ToggleCellLines ();
  119. _expandLastColumnCheckBox = new ()
  120. {
  121. Title = "_ExpandLastColumn",
  122. CheckedState = _listColView.Style.ExpandLastColumn ? CheckState.Checked : CheckState.UnChecked
  123. };
  124. _expandLastColumnCheckBox.CheckedStateChanged += (s, e) => ToggleExpandLastColumn ();
  125. _alwaysUseNormalColorForVerticalCellLinesCheckBox = new ()
  126. {
  127. Title = "_AlwaysUseNormalColorForVerticalCellLines",
  128. CheckedState = _listColView.Style.AlwaysUseNormalColorForVerticalCellLines ? CheckState.Checked : CheckState.UnChecked
  129. };
  130. _alwaysUseNormalColorForVerticalCellLinesCheckBox.CheckedStateChanged += (s, e) => ToggleAlwaysUseNormalColorForVerticalCellLines ();
  131. _smoothScrollingCheckBox = new ()
  132. {
  133. Title = "_SmoothHorizontalScrolling",
  134. CheckedState = _listColView.Style.SmoothHorizontalScrolling ? CheckState.Checked : CheckState.UnChecked
  135. };
  136. _smoothScrollingCheckBox.CheckedStateChanged += (s, e) => ToggleSmoothScrolling ();
  137. _alternatingColorsCheckBox = new ()
  138. {
  139. Title = "Alternating Colors"
  140. };
  141. _alternatingColorsCheckBox.CheckedStateChanged += (s, e) => ToggleAlternatingColors ();
  142. _cursorCheckBox = new ()
  143. {
  144. Title = "Invert Selected Cell First Character",
  145. CheckedState = _listColView.Style.InvertSelectedCellFirstCharacter ? CheckState.Checked : CheckState.UnChecked
  146. };
  147. _cursorCheckBox.CheckedStateChanged += (s, e) => ToggleInvertSelectedCellFirstCharacter ();
  148. _orientVerticalCheckBox = new ()
  149. {
  150. Title = "_OrientVertical",
  151. CheckedState = listColStyle.Orientation == Orientation.Vertical ? CheckState.Checked : CheckState.UnChecked
  152. };
  153. _orientVerticalCheckBox.CheckedStateChanged += (s, e) => ToggleVerticalOrientation ();
  154. _scrollParallelCheckBox = new ()
  155. {
  156. Title = "_ScrollParallel",
  157. CheckedState = listColStyle.ScrollParallel ? CheckState.Checked : CheckState.UnChecked
  158. };
  159. _scrollParallelCheckBox.CheckedStateChanged += (s, e) => ToggleScrollParallel ();
  160. menuBar.Add (
  161. new MenuBarItem (
  162. "_File",
  163. [
  164. new MenuItem
  165. {
  166. Title = "Open_BigListExample",
  167. Action = () => OpenSimpleList (true)
  168. },
  169. new MenuItem
  170. {
  171. Title = "Open_SmListExample",
  172. Action = () => OpenSimpleList (false)
  173. },
  174. new MenuItem
  175. {
  176. Title = "_CloseExample",
  177. Action = CloseExample
  178. },
  179. new MenuItem
  180. {
  181. Title = "_Quit",
  182. Action = Quit
  183. }
  184. ]
  185. )
  186. );
  187. menuBar.Add (
  188. new MenuBarItem (
  189. "_View",
  190. [
  191. new MenuItem
  192. {
  193. CommandView = _topLineCheckBox
  194. },
  195. new MenuItem
  196. {
  197. CommandView = _bottomLineCheckBox
  198. },
  199. new MenuItem
  200. {
  201. CommandView = _cellLinesCheckBox
  202. },
  203. new MenuItem
  204. {
  205. CommandView = _expandLastColumnCheckBox
  206. },
  207. new MenuItem
  208. {
  209. CommandView = _alwaysUseNormalColorForVerticalCellLinesCheckBox
  210. },
  211. new MenuItem
  212. {
  213. CommandView = _smoothScrollingCheckBox
  214. },
  215. new MenuItem
  216. {
  217. CommandView = _alternatingColorsCheckBox
  218. },
  219. new MenuItem
  220. {
  221. CommandView = _cursorCheckBox
  222. }
  223. ]
  224. )
  225. );
  226. menuBar.Add (
  227. new MenuBarItem (
  228. "_List",
  229. [
  230. new MenuItem
  231. {
  232. CommandView = _orientVerticalCheckBox
  233. },
  234. new MenuItem
  235. {
  236. CommandView = _scrollParallelCheckBox
  237. },
  238. new MenuItem
  239. {
  240. Title = "Set _Max Cell Width",
  241. Action = SetListMaxWidth
  242. },
  243. new MenuItem
  244. {
  245. Title = "Set Mi_n Cell Width",
  246. Action = SetListMinWidth
  247. }
  248. ]
  249. )
  250. );
  251. // Add views in order of visual appearance
  252. appWindow.Add (menuBar, _listColView, selectedCellLabel, statusBar);
  253. Application.Run (appWindow);
  254. appWindow.Dispose ();
  255. Application.Shutdown ();
  256. }
  257. private void CloseExample ()
  258. {
  259. if (_listColView is { })
  260. {
  261. _listColView.Table = null;
  262. }
  263. }
  264. private void OpenSimpleList (bool big) { SetTable (BuildSimpleList (big ? 1023 : 31)); }
  265. private void Quit () { Application.RequestStop (); }
  266. private void RunListWidthDialog (string prompt, Action<TableView, int> setter, Func<TableView, int> getter)
  267. {
  268. if (_listColView is null)
  269. {
  270. return;
  271. }
  272. var accepted = false;
  273. Button ok = new () { Text = "Ok", IsDefault = true };
  274. ok.Accepting += (s, e) =>
  275. {
  276. accepted = true;
  277. Application.RequestStop ();
  278. };
  279. Button cancel = new () { Text = "Cancel" };
  280. cancel.Accepting += (s, e) => { Application.RequestStop (); };
  281. Dialog d = new () { Title = prompt, Buttons = [ok, cancel] };
  282. TextField tf = new () { Text = getter (_listColView).ToString (), X = 0, Y = 0, Width = Dim.Fill () };
  283. d.Add (tf);
  284. tf.SetFocus ();
  285. Application.Run (d);
  286. d.Dispose ();
  287. if (accepted)
  288. {
  289. try
  290. {
  291. setter (_listColView, int.Parse (tf.Text));
  292. }
  293. catch (Exception ex)
  294. {
  295. MessageBox.ErrorQuery (Application.Instance, 60, 20, "Failed to set", ex.Message, "Ok");
  296. }
  297. }
  298. }
  299. private void SetListMaxWidth ()
  300. {
  301. RunListWidthDialog ("MaxCellWidth", (s, v) => s.MaxCellWidth = v, s => s.MaxCellWidth);
  302. _listColView?.SetNeedsDraw ();
  303. }
  304. private void SetListMinWidth ()
  305. {
  306. RunListWidthDialog ("MinCellWidth", (s, v) => s.MinCellWidth = v, s => s.MinCellWidth);
  307. _listColView?.SetNeedsDraw ();
  308. }
  309. private void SetTable (IList list)
  310. {
  311. if (_listColView is null)
  312. {
  313. return;
  314. }
  315. _listColView.Table = new ListTableSource (list, _listColView);
  316. if (_listColView.Table is ListTableSource listTableSource)
  317. {
  318. _currentTable = listTableSource.DataTable;
  319. }
  320. }
  321. private void TableViewKeyPress (object? sender, Key e)
  322. {
  323. if (_currentTable is null || _listColView is null)
  324. {
  325. return;
  326. }
  327. if (e.KeyCode == Key.Delete)
  328. {
  329. // set all selected cells to null
  330. foreach (Point pt in _listColView.GetAllSelectedCells ())
  331. {
  332. _currentTable.Rows [pt.Y] [pt.X] = DBNull.Value;
  333. }
  334. _listColView.Update ();
  335. e.Handled = true;
  336. }
  337. }
  338. private void ToggleAlternatingColors ()
  339. {
  340. if (_listColView is null || _alternatingColorsCheckBox is null)
  341. {
  342. return;
  343. }
  344. if (_alternatingColorsCheckBox.CheckedState == CheckState.Checked)
  345. {
  346. _listColView.Style.RowColorGetter = a => a.RowIndex % 2 == 0 ? _alternatingScheme : null;
  347. }
  348. else
  349. {
  350. _listColView.Style.RowColorGetter = null;
  351. }
  352. _listColView.SetNeedsDraw ();
  353. }
  354. private void ToggleAlwaysUseNormalColorForVerticalCellLines ()
  355. {
  356. if (_listColView is null || _alwaysUseNormalColorForVerticalCellLinesCheckBox is null)
  357. {
  358. return;
  359. }
  360. _listColView.Style.AlwaysUseNormalColorForVerticalCellLines =
  361. _alwaysUseNormalColorForVerticalCellLinesCheckBox.CheckedState == CheckState.Checked;
  362. _listColView.Update ();
  363. }
  364. private void ToggleBottomline ()
  365. {
  366. if (_listColView is null || _bottomLineCheckBox is null)
  367. {
  368. return;
  369. }
  370. _listColView.Style.ShowHorizontalBottomline = _bottomLineCheckBox.CheckedState == CheckState.Checked;
  371. _listColView.Update ();
  372. }
  373. private void ToggleCellLines ()
  374. {
  375. if (_listColView is null || _cellLinesCheckBox is null)
  376. {
  377. return;
  378. }
  379. _listColView.Style.ShowVerticalCellLines = _cellLinesCheckBox.CheckedState == CheckState.Checked;
  380. _listColView.Update ();
  381. }
  382. private void ToggleExpandLastColumn ()
  383. {
  384. if (_listColView is null || _expandLastColumnCheckBox is null)
  385. {
  386. return;
  387. }
  388. _listColView.Style.ExpandLastColumn = _expandLastColumnCheckBox.CheckedState == CheckState.Checked;
  389. _listColView.Update ();
  390. }
  391. private void ToggleInvertSelectedCellFirstCharacter ()
  392. {
  393. if (_listColView is null || _cursorCheckBox is null)
  394. {
  395. return;
  396. }
  397. _listColView.Style.InvertSelectedCellFirstCharacter = _cursorCheckBox.CheckedState == CheckState.Checked;
  398. _listColView.SetNeedsDraw ();
  399. }
  400. private void ToggleScrollParallel ()
  401. {
  402. if (_listColView?.Table is not ListTableSource listTableSource || _scrollParallelCheckBox is null)
  403. {
  404. return;
  405. }
  406. listTableSource.Style.ScrollParallel = _scrollParallelCheckBox.CheckedState == CheckState.Checked;
  407. _listColView.SetNeedsDraw ();
  408. }
  409. private void ToggleSmoothScrolling ()
  410. {
  411. if (_listColView is null || _smoothScrollingCheckBox is null)
  412. {
  413. return;
  414. }
  415. _listColView.Style.SmoothHorizontalScrolling = _smoothScrollingCheckBox.CheckedState == CheckState.Checked;
  416. _listColView.Update ();
  417. }
  418. private void ToggleTopline ()
  419. {
  420. if (_listColView is null || _topLineCheckBox is null)
  421. {
  422. return;
  423. }
  424. _listColView.Style.ShowHorizontalHeaderOverline = _topLineCheckBox.CheckedState == CheckState.Checked;
  425. _listColView.Update ();
  426. }
  427. private void ToggleVerticalOrientation ()
  428. {
  429. if (_listColView?.Table is not ListTableSource listTableSource || _orientVerticalCheckBox is null)
  430. {
  431. return;
  432. }
  433. listTableSource.Style.Orientation = _orientVerticalCheckBox.CheckedState == CheckState.Checked
  434. ? Orientation.Vertical
  435. : Orientation.Horizontal;
  436. _listColView.SetNeedsDraw ();
  437. }
  438. }