TableEditor.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using Terminal.Gui;
  5. using System.Linq;
  6. using System.Globalization;
  7. using static Terminal.Gui.TableView;
  8. namespace UICatalog.Scenarios {
  9. [ScenarioMetadata (Name: "TableEditor", Description: "Implements data table editor using the TableView control.")]
  10. [ScenarioCategory ("TableView")]
  11. [ScenarioCategory ("Controls")]
  12. [ScenarioCategory ("Dialogs")]
  13. [ScenarioCategory ("Text and Formatting")]
  14. [ScenarioCategory ("Top Level Windows")]
  15. public class TableEditor : Scenario {
  16. TableView tableView;
  17. private MenuItem miAlwaysShowHeaders;
  18. private MenuItem miHeaderOverline;
  19. private MenuItem miHeaderMidline;
  20. private MenuItem miHeaderUnderline;
  21. private MenuItem miShowHorizontalScrollIndicators;
  22. private MenuItem miCellLines;
  23. private MenuItem miFullRowSelect;
  24. private MenuItem miExpandLastColumn;
  25. private MenuItem miSmoothScrolling;
  26. private MenuItem miAlternatingColors;
  27. private MenuItem miCursor;
  28. ColorScheme redColorScheme;
  29. ColorScheme redColorSchemeAlt;
  30. ColorScheme alternatingColorScheme;
  31. public override void Setup ()
  32. {
  33. Win.Title = this.GetName ();
  34. Win.Y = 1; // menu
  35. Win.Height = Dim.Fill (1); // status bar
  36. Application.Top.LayoutSubviews ();
  37. this.tableView = new TableView () {
  38. X = 0,
  39. Y = 0,
  40. Width = Dim.Fill (),
  41. Height = Dim.Fill (1),
  42. };
  43. var menu = new MenuBar (new MenuBarItem [] {
  44. new MenuBarItem ("_File", new MenuItem [] {
  45. new MenuItem ("_OpenBigExample", "", () => OpenExample(true)),
  46. new MenuItem ("_OpenSmallExample", "", () => OpenExample(false)),
  47. new MenuItem ("OpenCharacter_Map","",()=>OpenUnicodeMap()),
  48. new MenuItem ("_CloseExample", "", () => CloseExample()),
  49. new MenuItem ("_Quit", "", () => Quit()),
  50. }),
  51. new MenuBarItem ("_View", new MenuItem [] {
  52. miAlwaysShowHeaders = new MenuItem ("_AlwaysShowHeaders", "", () => ToggleAlwaysShowHeader()){Checked = tableView.Style.AlwaysShowHeaders, CheckType = MenuItemCheckStyle.Checked },
  53. miHeaderOverline = new MenuItem ("_HeaderOverLine", "", () => ToggleOverline()){Checked = tableView.Style.ShowHorizontalHeaderOverline, CheckType = MenuItemCheckStyle.Checked },
  54. miHeaderMidline = new MenuItem ("_HeaderMidLine", "", () => ToggleHeaderMidline()){Checked = tableView.Style.ShowVerticalHeaderLines, CheckType = MenuItemCheckStyle.Checked },
  55. miHeaderUnderline = new MenuItem ("_HeaderUnderLine", "", () => ToggleUnderline()){Checked = tableView.Style.ShowHorizontalHeaderUnderline, CheckType = MenuItemCheckStyle.Checked },
  56. miShowHorizontalScrollIndicators = new MenuItem ("_HorizontalScrollIndicators", "", () => ToggleHorizontalScrollIndicators()){Checked = tableView.Style.ShowHorizontalScrollIndicators, CheckType = MenuItemCheckStyle.Checked },
  57. miFullRowSelect =new MenuItem ("_FullRowSelect", "", () => ToggleFullRowSelect()){Checked = tableView.FullRowSelect, CheckType = MenuItemCheckStyle.Checked },
  58. miCellLines =new MenuItem ("_CellLines", "", () => ToggleCellLines()){Checked = tableView.Style.ShowVerticalCellLines, CheckType = MenuItemCheckStyle.Checked },
  59. miExpandLastColumn = new MenuItem ("_ExpandLastColumn", "", () => ToggleExpandLastColumn()){Checked = tableView.Style.ExpandLastColumn, CheckType = MenuItemCheckStyle.Checked },
  60. miSmoothScrolling = new MenuItem ("_SmoothHorizontalScrolling", "", () => ToggleSmoothScrolling()){Checked = tableView.Style.SmoothHorizontalScrolling, CheckType = MenuItemCheckStyle.Checked },
  61. new MenuItem ("_AllLines", "", () => ToggleAllCellLines()),
  62. new MenuItem ("_NoLines", "", () => ToggleNoCellLines()),
  63. miAlternatingColors = new MenuItem ("Alternating Colors", "", () => ToggleAlternatingColors()){CheckType = MenuItemCheckStyle.Checked},
  64. miCursor = new MenuItem ("Invert Selected Cell First Character", "", () => ToggleInvertSelectedCellFirstCharacter()){Checked = tableView.Style.InvertSelectedCellFirstCharacter,CheckType = MenuItemCheckStyle.Checked},
  65. new MenuItem ("_ClearColumnStyles", "", () => ClearColumnStyles()),
  66. new MenuItem ("Sho_w All Columns", "", ()=>ShowAllColumns())
  67. }),
  68. new MenuBarItem ("_Column", new MenuItem [] {
  69. new MenuItem ("_Set Max Width", "", SetMaxWidth),
  70. new MenuItem ("_Set Min Width", "", SetMinWidth),
  71. new MenuItem ("_Set MinAcceptableWidth", "",SetMinAcceptableWidth),
  72. new MenuItem ("_Set All MinAcceptableWidth=1", "",SetMinAcceptableWidthToOne),
  73. }),
  74. });
  75. Application.Top.Add (menu);
  76. var statusBar = new StatusBar (new StatusItem [] {
  77. new StatusItem(Key.F2, "~F2~ OpenExample", () => OpenExample(true)),
  78. new StatusItem(Key.F3, "~F3~ CloseExample", () => CloseExample()),
  79. new StatusItem(Key.F4, "~F4~ OpenSimple", () => OpenSimple(true)),
  80. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Quit()),
  81. });
  82. Application.Top.Add (statusBar);
  83. Win.Add (tableView);
  84. var selectedCellLabel = new Label () {
  85. X = 0,
  86. Y = Pos.Bottom (tableView),
  87. Text = "0,0",
  88. Width = Dim.Fill (),
  89. TextAlignment = TextAlignment.Right
  90. };
  91. Win.Add (selectedCellLabel);
  92. tableView.SelectedCellChanged += (s, e) => { selectedCellLabel.Text = $"{tableView.SelectedRow},{tableView.SelectedColumn}"; };
  93. tableView.CellActivated += EditCurrentCell;
  94. tableView.KeyPress += TableViewKeyPress;
  95. SetupScrollBar ();
  96. redColorScheme = new ColorScheme () {
  97. Disabled = Win.ColorScheme.Disabled,
  98. HotFocus = Win.ColorScheme.HotFocus,
  99. Focus = Win.ColorScheme.Focus,
  100. Normal = Application.Driver.MakeAttribute (Color.Red, Win.ColorScheme.Normal.Background)
  101. };
  102. alternatingColorScheme = new ColorScheme () {
  103. Disabled = Win.ColorScheme.Disabled,
  104. HotFocus = Win.ColorScheme.HotFocus,
  105. Focus = Win.ColorScheme.Focus,
  106. Normal = Application.Driver.MakeAttribute (Color.White, Color.BrightBlue)
  107. };
  108. redColorSchemeAlt = new ColorScheme () {
  109. Disabled = Win.ColorScheme.Disabled,
  110. HotFocus = Win.ColorScheme.HotFocus,
  111. Focus = Win.ColorScheme.Focus,
  112. Normal = Application.Driver.MakeAttribute (Color.Red, Color.BrightBlue)
  113. };
  114. // if user clicks the mouse in TableView
  115. tableView.MouseClick += (s,e) => {
  116. tableView.ScreenToCell (e.MouseEvent.X, e.MouseEvent.Y, out DataColumn clickedCol);
  117. if (clickedCol != null) {
  118. if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)) {
  119. // left click in a header
  120. SortColumn (clickedCol);
  121. } else if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked)) {
  122. // right click in a header
  123. ShowHeaderContextMenu (clickedCol, e);
  124. }
  125. }
  126. };
  127. tableView.AddKeyBinding (Key.Space, Command.ToggleChecked);
  128. }
  129. private void ShowAllColumns ()
  130. {
  131. foreach (var colStyle in tableView.Style.ColumnStyles) {
  132. colStyle.Value.Visible = true;
  133. }
  134. tableView.Update ();
  135. }
  136. private void SortColumn (DataColumn clickedCol)
  137. {
  138. var sort = GetProposedNewSortOrder (clickedCol, out var isAsc);
  139. SortColumn (clickedCol, sort, isAsc);
  140. }
  141. private void SortColumn (DataColumn clickedCol, string sort, bool isAsc)
  142. {
  143. // set a sort order
  144. tableView.Table.DefaultView.Sort = sort;
  145. // copy the rows from the view
  146. var sortedCopy = tableView.Table.DefaultView.ToTable ();
  147. tableView.Table.Rows.Clear ();
  148. foreach (DataRow r in sortedCopy.Rows) {
  149. tableView.Table.ImportRow (r);
  150. }
  151. foreach (DataColumn col in tableView.Table.Columns) {
  152. // remove any lingering sort indicator
  153. col.ColumnName = TrimArrows (col.ColumnName);
  154. // add a new one if this the one that is being sorted
  155. if (col == clickedCol) {
  156. col.ColumnName += isAsc ? '▲' : '▼';
  157. }
  158. }
  159. tableView.Update ();
  160. }
  161. private string TrimArrows (string columnName)
  162. {
  163. return columnName.TrimEnd ('▼', '▲');
  164. }
  165. private string StripArrows (string columnName)
  166. {
  167. return columnName.Replace ("▼", "").Replace ("▲", "");
  168. }
  169. private string GetProposedNewSortOrder (DataColumn clickedCol, out bool isAsc)
  170. {
  171. // work out new sort order
  172. var sort = tableView.Table.DefaultView.Sort;
  173. if (sort?.EndsWith ("ASC") ?? false) {
  174. sort = $"{clickedCol.ColumnName} DESC";
  175. isAsc = false;
  176. } else {
  177. sort = $"{clickedCol.ColumnName} ASC";
  178. isAsc = true;
  179. }
  180. return sort;
  181. }
  182. private void ShowHeaderContextMenu (DataColumn clickedCol, MouseEventEventArgs e)
  183. {
  184. var sort = GetProposedNewSortOrder (clickedCol, out var isAsc);
  185. var contextMenu = new ContextMenu (e.MouseEvent.X + 1, e.MouseEvent.Y + 1,
  186. new MenuBarItem (new MenuItem [] {
  187. new MenuItem ($"Hide {TrimArrows(clickedCol.ColumnName)}", "", () => HideColumn(clickedCol)),
  188. new MenuItem ($"Sort {StripArrows(sort)}","",()=>SortColumn(clickedCol,sort,isAsc)),
  189. })
  190. );
  191. contextMenu.Show ();
  192. }
  193. private void HideColumn (DataColumn clickedCol)
  194. {
  195. var style = tableView.Style.GetOrCreateColumnStyle (clickedCol);
  196. style.Visible = false;
  197. tableView.Update ();
  198. }
  199. private DataColumn GetColumn ()
  200. {
  201. if (tableView.Table == null)
  202. return null;
  203. if (tableView.SelectedColumn < 0 || tableView.SelectedColumn > tableView.Table.Columns.Count)
  204. return null;
  205. return tableView.Table.Columns [tableView.SelectedColumn];
  206. }
  207. private void SetMinAcceptableWidthToOne ()
  208. {
  209. foreach (DataColumn c in tableView.Table.Columns) {
  210. var style = tableView.Style.GetOrCreateColumnStyle (c);
  211. style.MinAcceptableWidth = 1;
  212. }
  213. }
  214. private void SetMinAcceptableWidth ()
  215. {
  216. var col = GetColumn ();
  217. RunColumnWidthDialog (col, "MinAcceptableWidth", (s, v) => s.MinAcceptableWidth = v, (s) => s.MinAcceptableWidth);
  218. }
  219. private void SetMinWidth ()
  220. {
  221. var col = GetColumn ();
  222. RunColumnWidthDialog (col, "MinWidth", (s, v) => s.MinWidth = v, (s) => s.MinWidth);
  223. }
  224. private void SetMaxWidth ()
  225. {
  226. var col = GetColumn ();
  227. RunColumnWidthDialog (col, "MaxWidth", (s, v) => s.MaxWidth = v, (s) => s.MaxWidth);
  228. }
  229. private void RunColumnWidthDialog (DataColumn col, string prompt, Action<ColumnStyle, int> setter, Func<ColumnStyle, int> getter)
  230. {
  231. var accepted = false;
  232. var ok = new Button ("Ok", is_default: true);
  233. ok.Clicked += (s,e) => { accepted = true; Application.RequestStop (); };
  234. var cancel = new Button ("Cancel");
  235. cancel.Clicked += (s,e) => { Application.RequestStop (); };
  236. var d = new Dialog (ok, cancel) { Title = prompt };
  237. var style = tableView.Style.GetOrCreateColumnStyle (col);
  238. var lbl = new Label () {
  239. X = 0,
  240. Y = 1,
  241. Text = col.ColumnName
  242. };
  243. var tf = new TextField () {
  244. Text = getter (style).ToString (),
  245. X = 0,
  246. Y = 2,
  247. Width = Dim.Fill ()
  248. };
  249. d.Add (lbl, tf);
  250. tf.SetFocus ();
  251. Application.Run (d);
  252. if (accepted) {
  253. try {
  254. setter (style, int.Parse (tf.Text.ToString ()));
  255. } catch (Exception ex) {
  256. MessageBox.ErrorQuery (60, 20, "Failed to set", ex.Message, "Ok");
  257. }
  258. tableView.Update ();
  259. }
  260. }
  261. private void SetupScrollBar ()
  262. {
  263. var _scrollBar = new ScrollBarView (tableView, true);
  264. _scrollBar.ChangedPosition += (s,e) => {
  265. tableView.RowOffset = _scrollBar.Position;
  266. if (tableView.RowOffset != _scrollBar.Position) {
  267. _scrollBar.Position = tableView.RowOffset;
  268. }
  269. tableView.SetNeedsDisplay ();
  270. };
  271. /*
  272. _scrollBar.OtherScrollBarView.ChangedPosition += (s,e) => {
  273. _listView.LeftItem = _scrollBar.OtherScrollBarView.Position;
  274. if (_listView.LeftItem != _scrollBar.OtherScrollBarView.Position) {
  275. _scrollBar.OtherScrollBarView.Position = _listView.LeftItem;
  276. }
  277. _listView.SetNeedsDisplay ();
  278. };*/
  279. tableView.DrawContent += (s,e) => {
  280. _scrollBar.Size = tableView.Table?.Rows?.Count ?? 0;
  281. _scrollBar.Position = tableView.RowOffset;
  282. // _scrollBar.OtherScrollBarView.Size = _listView.Maxlength - 1;
  283. // _scrollBar.OtherScrollBarView.Position = _listView.LeftItem;
  284. _scrollBar.Refresh ();
  285. };
  286. }
  287. private void TableViewKeyPress (object sender, KeyEventEventArgs e)
  288. {
  289. if (e.KeyEvent.Key == Key.DeleteChar) {
  290. if (tableView.FullRowSelect) {
  291. // Delete button deletes all rows when in full row mode
  292. foreach (int toRemove in tableView.GetAllSelectedCells ().Select (p => p.Y).Distinct ().OrderByDescending (i => i))
  293. tableView.Table.Rows.RemoveAt (toRemove);
  294. } else {
  295. // otherwise set all selected cells to null
  296. foreach (var pt in tableView.GetAllSelectedCells ()) {
  297. tableView.Table.Rows [pt.Y] [pt.X] = DBNull.Value;
  298. }
  299. }
  300. tableView.Update ();
  301. e.Handled = true;
  302. }
  303. }
  304. private void ClearColumnStyles ()
  305. {
  306. tableView.Style.ColumnStyles.Clear ();
  307. tableView.Update ();
  308. }
  309. private void ToggleAlwaysShowHeader ()
  310. {
  311. miAlwaysShowHeaders.Checked = !miAlwaysShowHeaders.Checked;
  312. tableView.Style.AlwaysShowHeaders = (bool)miAlwaysShowHeaders.Checked;
  313. tableView.Update ();
  314. }
  315. private void ToggleOverline ()
  316. {
  317. miHeaderOverline.Checked = !miHeaderOverline.Checked;
  318. tableView.Style.ShowHorizontalHeaderOverline = (bool)miHeaderOverline.Checked;
  319. tableView.Update ();
  320. }
  321. private void ToggleHeaderMidline ()
  322. {
  323. miHeaderMidline.Checked = !miHeaderMidline.Checked;
  324. tableView.Style.ShowVerticalHeaderLines = (bool)miHeaderMidline.Checked;
  325. tableView.Update ();
  326. }
  327. private void ToggleUnderline ()
  328. {
  329. miHeaderUnderline.Checked = !miHeaderUnderline.Checked;
  330. tableView.Style.ShowHorizontalHeaderUnderline = (bool)miHeaderUnderline.Checked;
  331. tableView.Update ();
  332. }
  333. private void ToggleHorizontalScrollIndicators ()
  334. {
  335. miShowHorizontalScrollIndicators.Checked = !miShowHorizontalScrollIndicators.Checked;
  336. tableView.Style.ShowHorizontalScrollIndicators = (bool)miShowHorizontalScrollIndicators.Checked;
  337. tableView.Update ();
  338. }
  339. private void ToggleFullRowSelect ()
  340. {
  341. miFullRowSelect.Checked = !miFullRowSelect.Checked;
  342. tableView.FullRowSelect = (bool)miFullRowSelect.Checked;
  343. tableView.Update ();
  344. }
  345. private void ToggleExpandLastColumn ()
  346. {
  347. miExpandLastColumn.Checked = !miExpandLastColumn.Checked;
  348. tableView.Style.ExpandLastColumn = (bool)miExpandLastColumn.Checked;
  349. tableView.Update ();
  350. }
  351. private void ToggleSmoothScrolling ()
  352. {
  353. miSmoothScrolling.Checked = !miSmoothScrolling.Checked;
  354. tableView.Style.SmoothHorizontalScrolling = (bool)miSmoothScrolling.Checked;
  355. tableView.Update ();
  356. }
  357. private void ToggleCellLines ()
  358. {
  359. miCellLines.Checked = !miCellLines.Checked;
  360. tableView.Style.ShowVerticalCellLines = (bool)miCellLines.Checked;
  361. tableView.Update ();
  362. }
  363. private void ToggleAllCellLines ()
  364. {
  365. tableView.Style.ShowHorizontalHeaderOverline = true;
  366. tableView.Style.ShowVerticalHeaderLines = true;
  367. tableView.Style.ShowHorizontalHeaderUnderline = true;
  368. tableView.Style.ShowVerticalCellLines = true;
  369. miHeaderOverline.Checked = true;
  370. miHeaderMidline.Checked = true;
  371. miHeaderUnderline.Checked = true;
  372. miCellLines.Checked = true;
  373. tableView.Update ();
  374. }
  375. private void ToggleNoCellLines ()
  376. {
  377. tableView.Style.ShowHorizontalHeaderOverline = false;
  378. tableView.Style.ShowVerticalHeaderLines = false;
  379. tableView.Style.ShowHorizontalHeaderUnderline = false;
  380. tableView.Style.ShowVerticalCellLines = false;
  381. miHeaderOverline.Checked = false;
  382. miHeaderMidline.Checked = false;
  383. miHeaderUnderline.Checked = false;
  384. miCellLines.Checked = false;
  385. tableView.Update ();
  386. }
  387. private void ToggleAlternatingColors ()
  388. {
  389. //toggle menu item
  390. miAlternatingColors.Checked = !miAlternatingColors.Checked;
  391. if (miAlternatingColors.Checked == true) {
  392. tableView.Style.RowColorGetter = (a) => { return a.RowIndex % 2 == 0 ? alternatingColorScheme : null; };
  393. } else {
  394. tableView.Style.RowColorGetter = null;
  395. }
  396. tableView.SetNeedsDisplay ();
  397. }
  398. private void ToggleInvertSelectedCellFirstCharacter ()
  399. {
  400. //toggle menu item
  401. miCursor.Checked = !miCursor.Checked;
  402. tableView.Style.InvertSelectedCellFirstCharacter = (bool)miCursor.Checked;
  403. tableView.SetNeedsDisplay ();
  404. }
  405. private void CloseExample ()
  406. {
  407. tableView.Table = null;
  408. }
  409. private void Quit ()
  410. {
  411. Application.RequestStop ();
  412. }
  413. private void OpenExample (bool big)
  414. {
  415. tableView.Table = BuildDemoDataTable (big ? 30 : 5, big ? 1000 : 5);
  416. SetDemoTableStyles ();
  417. }
  418. private void OpenUnicodeMap ()
  419. {
  420. tableView.Table = BuildUnicodeMap ();
  421. tableView.Update ();
  422. }
  423. private DataTable BuildUnicodeMap ()
  424. {
  425. var dt = new DataTable ();
  426. // add cols called 0 to 9
  427. for (int i = 0; i < 10; i++) {
  428. var col = dt.Columns.Add (i.ToString (), typeof (uint));
  429. var style = tableView.Style.GetOrCreateColumnStyle (col);
  430. style.RepresentationGetter = (o) => new Rune ((uint)o).ToString ();
  431. }
  432. // add cols called a to z
  433. for (int i = 'a'; i < 'a' + 26; i++) {
  434. var col = dt.Columns.Add (((char)i).ToString (), typeof (uint));
  435. var style = tableView.Style.GetOrCreateColumnStyle (col);
  436. style.RepresentationGetter = (o) => new Rune ((uint)o).ToString ();
  437. }
  438. // now add table contents
  439. List<uint> runes = new List<uint> ();
  440. foreach (var range in Ranges) {
  441. for (uint i = range.Start; i <= range.End; i++) {
  442. runes.Add (i);
  443. }
  444. }
  445. DataRow dr = null;
  446. for (int i = 0; i < runes.Count; i++) {
  447. if (dr == null || i % dt.Columns.Count == 0) {
  448. dr = dt.Rows.Add ();
  449. }
  450. dr [i % dt.Columns.Count] = runes [i].ToString ();
  451. }
  452. return dt;
  453. }
  454. class UnicodeRange {
  455. public uint Start;
  456. public uint End;
  457. public string Category;
  458. public UnicodeRange (uint start, uint end, string category)
  459. {
  460. this.Start = start;
  461. this.End = end;
  462. this.Category = category;
  463. }
  464. }
  465. List<UnicodeRange> Ranges = new List<UnicodeRange> {
  466. new UnicodeRange (0x0000, 0x001F, "ASCII Control Characters"),
  467. new UnicodeRange (0x0080, 0x009F, "C0 Control Characters"),
  468. new UnicodeRange(0x1100, 0x11ff,"Hangul Jamo"), // This is where wide chars tend to start
  469. new UnicodeRange(0x20A0, 0x20CF,"Currency Symbols"),
  470. new UnicodeRange(0x2100, 0x214F,"Letterlike Symbols"),
  471. new UnicodeRange(0x2190, 0x21ff,"Arrows" ),
  472. new UnicodeRange(0x2200, 0x22ff,"Mathematical symbols"),
  473. new UnicodeRange(0x2300, 0x23ff,"Miscellaneous Technical"),
  474. new UnicodeRange(0x2500, 0x25ff,"Box Drawing & Geometric Shapes"),
  475. new UnicodeRange(0x2600, 0x26ff,"Miscellaneous Symbols"),
  476. new UnicodeRange(0x2700, 0x27ff,"Dingbats"),
  477. new UnicodeRange(0x2800, 0x28ff,"Braille"),
  478. new UnicodeRange(0x2b00, 0x2bff,"Miscellaneous Symbols and Arrows"),
  479. new UnicodeRange(0xFB00, 0xFb4f,"Alphabetic Presentation Forms"),
  480. new UnicodeRange(0x12400, 0x1240f,"Cuneiform Numbers and Punctuation"),
  481. new UnicodeRange(0x1FA00, 0x1FA0f,"Chess Symbols"),
  482. new UnicodeRange((uint)(CharMap.MaxCodePointVal - 16), (uint)CharMap.MaxCodePointVal,"End"),
  483. new UnicodeRange (0x0020 ,0x007F ,"Basic Latin"),
  484. new UnicodeRange (0x00A0 ,0x00FF ,"Latin-1 Supplement"),
  485. new UnicodeRange (0x0100 ,0x017F ,"Latin Extended-A"),
  486. new UnicodeRange (0x0180 ,0x024F ,"Latin Extended-B"),
  487. new UnicodeRange (0x0250 ,0x02AF ,"IPA Extensions"),
  488. new UnicodeRange (0x02B0 ,0x02FF ,"Spacing Modifier Letters"),
  489. new UnicodeRange (0x0300 ,0x036F ,"Combining Diacritical Marks"),
  490. new UnicodeRange (0x0370 ,0x03FF ,"Greek and Coptic"),
  491. new UnicodeRange (0x0400 ,0x04FF ,"Cyrillic"),
  492. new UnicodeRange (0x0500 ,0x052F ,"Cyrillic Supplementary"),
  493. new UnicodeRange (0x0530 ,0x058F ,"Armenian"),
  494. new UnicodeRange (0x0590 ,0x05FF ,"Hebrew"),
  495. new UnicodeRange (0x0600 ,0x06FF ,"Arabic"),
  496. new UnicodeRange (0x0700 ,0x074F ,"Syriac"),
  497. new UnicodeRange (0x0780 ,0x07BF ,"Thaana"),
  498. new UnicodeRange (0x0900 ,0x097F ,"Devanagari"),
  499. new UnicodeRange (0x0980 ,0x09FF ,"Bengali"),
  500. new UnicodeRange (0x0A00 ,0x0A7F ,"Gurmukhi"),
  501. new UnicodeRange (0x0A80 ,0x0AFF ,"Gujarati"),
  502. new UnicodeRange (0x0B00 ,0x0B7F ,"Oriya"),
  503. new UnicodeRange (0x0B80 ,0x0BFF ,"Tamil"),
  504. new UnicodeRange (0x0C00 ,0x0C7F ,"Telugu"),
  505. new UnicodeRange (0x0C80 ,0x0CFF ,"Kannada"),
  506. new UnicodeRange (0x0D00 ,0x0D7F ,"Malayalam"),
  507. new UnicodeRange (0x0D80 ,0x0DFF ,"Sinhala"),
  508. new UnicodeRange (0x0E00 ,0x0E7F ,"Thai"),
  509. new UnicodeRange (0x0E80 ,0x0EFF ,"Lao"),
  510. new UnicodeRange (0x0F00 ,0x0FFF ,"Tibetan"),
  511. new UnicodeRange (0x1000 ,0x109F ,"Myanmar"),
  512. new UnicodeRange (0x10A0 ,0x10FF ,"Georgian"),
  513. new UnicodeRange (0x1100 ,0x11FF ,"Hangul Jamo"),
  514. new UnicodeRange (0x1200 ,0x137F ,"Ethiopic"),
  515. new UnicodeRange (0x13A0 ,0x13FF ,"Cherokee"),
  516. new UnicodeRange (0x1400 ,0x167F ,"Unified Canadian Aboriginal Syllabics"),
  517. new UnicodeRange (0x1680 ,0x169F ,"Ogham"),
  518. new UnicodeRange (0x16A0 ,0x16FF ,"Runic"),
  519. new UnicodeRange (0x1700 ,0x171F ,"Tagalog"),
  520. new UnicodeRange (0x1720 ,0x173F ,"Hanunoo"),
  521. new UnicodeRange (0x1740 ,0x175F ,"Buhid"),
  522. new UnicodeRange (0x1760 ,0x177F ,"Tagbanwa"),
  523. new UnicodeRange (0x1780 ,0x17FF ,"Khmer"),
  524. new UnicodeRange (0x1800 ,0x18AF ,"Mongolian"),
  525. new UnicodeRange (0x1900 ,0x194F ,"Limbu"),
  526. new UnicodeRange (0x1950 ,0x197F ,"Tai Le"),
  527. new UnicodeRange (0x19E0 ,0x19FF ,"Khmer Symbols"),
  528. new UnicodeRange (0x1D00 ,0x1D7F ,"Phonetic Extensions"),
  529. new UnicodeRange (0x1E00 ,0x1EFF ,"Latin Extended Additional"),
  530. new UnicodeRange (0x1F00 ,0x1FFF ,"Greek Extended"),
  531. new UnicodeRange (0x2000 ,0x206F ,"General Punctuation"),
  532. new UnicodeRange (0x2070 ,0x209F ,"Superscripts and Subscripts"),
  533. new UnicodeRange (0x20A0 ,0x20CF ,"Currency Symbols"),
  534. new UnicodeRange (0x20D0 ,0x20FF ,"Combining Diacritical Marks for Symbols"),
  535. new UnicodeRange (0x2100 ,0x214F ,"Letterlike Symbols"),
  536. new UnicodeRange (0x2150 ,0x218F ,"Number Forms"),
  537. new UnicodeRange (0x2190 ,0x21FF ,"Arrows"),
  538. new UnicodeRange (0x2200 ,0x22FF ,"Mathematical Operators"),
  539. new UnicodeRange (0x2300 ,0x23FF ,"Miscellaneous Technical"),
  540. new UnicodeRange (0x2400 ,0x243F ,"Control Pictures"),
  541. new UnicodeRange (0x2440 ,0x245F ,"Optical Character Recognition"),
  542. new UnicodeRange (0x2460 ,0x24FF ,"Enclosed Alphanumerics"),
  543. new UnicodeRange (0x2500 ,0x257F ,"Box Drawing"),
  544. new UnicodeRange (0x2580 ,0x259F ,"Block Elements"),
  545. new UnicodeRange (0x25A0 ,0x25FF ,"Geometric Shapes"),
  546. new UnicodeRange (0x2600 ,0x26FF ,"Miscellaneous Symbols"),
  547. new UnicodeRange (0x2700 ,0x27BF ,"Dingbats"),
  548. new UnicodeRange (0x27C0 ,0x27EF ,"Miscellaneous Mathematical Symbols-A"),
  549. new UnicodeRange (0x27F0 ,0x27FF ,"Supplemental Arrows-A"),
  550. new UnicodeRange (0x2800 ,0x28FF ,"Braille Patterns"),
  551. new UnicodeRange (0x2900 ,0x297F ,"Supplemental Arrows-B"),
  552. new UnicodeRange (0x2980 ,0x29FF ,"Miscellaneous Mathematical Symbols-B"),
  553. new UnicodeRange (0x2A00 ,0x2AFF ,"Supplemental Mathematical Operators"),
  554. new UnicodeRange (0x2B00 ,0x2BFF ,"Miscellaneous Symbols and Arrows"),
  555. new UnicodeRange (0x2E80 ,0x2EFF ,"CJK Radicals Supplement"),
  556. new UnicodeRange (0x2F00 ,0x2FDF ,"Kangxi Radicals"),
  557. new UnicodeRange (0x2FF0 ,0x2FFF ,"Ideographic Description Characters"),
  558. new UnicodeRange (0x3000 ,0x303F ,"CJK Symbols and Punctuation"),
  559. new UnicodeRange (0x3040 ,0x309F ,"Hiragana"),
  560. new UnicodeRange (0x30A0 ,0x30FF ,"Katakana"),
  561. new UnicodeRange (0x3100 ,0x312F ,"Bopomofo"),
  562. new UnicodeRange (0x3130 ,0x318F ,"Hangul Compatibility Jamo"),
  563. new UnicodeRange (0x3190 ,0x319F ,"Kanbun"),
  564. new UnicodeRange (0x31A0 ,0x31BF ,"Bopomofo Extended"),
  565. new UnicodeRange (0x31F0 ,0x31FF ,"Katakana Phonetic Extensions"),
  566. new UnicodeRange (0x3200 ,0x32FF ,"Enclosed CJK Letters and Months"),
  567. new UnicodeRange (0x3300 ,0x33FF ,"CJK Compatibility"),
  568. new UnicodeRange (0x3400 ,0x4DBF ,"CJK Unified Ideographs Extension A"),
  569. new UnicodeRange (0x4DC0 ,0x4DFF ,"Yijing Hexagram Symbols"),
  570. new UnicodeRange (0x4E00 ,0x9FFF ,"CJK Unified Ideographs"),
  571. new UnicodeRange (0xA000 ,0xA48F ,"Yi Syllables"),
  572. new UnicodeRange (0xA490 ,0xA4CF ,"Yi Radicals"),
  573. new UnicodeRange (0xAC00 ,0xD7AF ,"Hangul Syllables"),
  574. new UnicodeRange (0xD800 ,0xDB7F ,"High Surrogates"),
  575. new UnicodeRange (0xDB80 ,0xDBFF ,"High Private Use Surrogates"),
  576. new UnicodeRange (0xDC00 ,0xDFFF ,"Low Surrogates"),
  577. new UnicodeRange (0xE000 ,0xF8FF ,"Private Use Area"),
  578. new UnicodeRange (0xF900 ,0xFAFF ,"CJK Compatibility Ideographs"),
  579. new UnicodeRange (0xFB00 ,0xFB4F ,"Alphabetic Presentation Forms"),
  580. new UnicodeRange (0xFB50 ,0xFDFF ,"Arabic Presentation Forms-A"),
  581. new UnicodeRange (0xFE00 ,0xFE0F ,"Variation Selectors"),
  582. new UnicodeRange (0xFE20 ,0xFE2F ,"Combining Half Marks"),
  583. new UnicodeRange (0xFE30 ,0xFE4F ,"CJK Compatibility Forms"),
  584. new UnicodeRange (0xFE50 ,0xFE6F ,"Small Form Variants"),
  585. new UnicodeRange (0xFE70 ,0xFEFF ,"Arabic Presentation Forms-B"),
  586. new UnicodeRange (0xFF00 ,0xFFEF ,"Halfwidth and Fullwidth Forms"),
  587. new UnicodeRange (0xFFF0 ,0xFFFF ,"Specials"),
  588. new UnicodeRange (0x10000, 0x1007F ,"Linear B Syllabary"),
  589. new UnicodeRange (0x10080, 0x100FF ,"Linear B Ideograms"),
  590. new UnicodeRange (0x10100, 0x1013F ,"Aegean Numbers"),
  591. new UnicodeRange (0x10300, 0x1032F ,"Old Italic"),
  592. new UnicodeRange (0x10330, 0x1034F ,"Gothic"),
  593. new UnicodeRange (0x10380, 0x1039F ,"Ugaritic"),
  594. new UnicodeRange (0x10400, 0x1044F ,"Deseret"),
  595. new UnicodeRange (0x10450, 0x1047F ,"Shavian"),
  596. new UnicodeRange (0x10480, 0x104AF ,"Osmanya"),
  597. new UnicodeRange (0x10800, 0x1083F ,"Cypriot Syllabary"),
  598. new UnicodeRange (0x1D000, 0x1D0FF ,"Byzantine Musical Symbols"),
  599. new UnicodeRange (0x1D100, 0x1D1FF ,"Musical Symbols"),
  600. new UnicodeRange (0x1D300, 0x1D35F ,"Tai Xuan Jing Symbols"),
  601. new UnicodeRange (0x1D400, 0x1D7FF ,"Mathematical Alphanumeric Symbols"),
  602. new UnicodeRange (0x1F600, 0x1F532 ,"Emojis Symbols"),
  603. new UnicodeRange (0x20000, 0x2A6DF ,"CJK Unified Ideographs Extension B"),
  604. new UnicodeRange (0x2F800, 0x2FA1F ,"CJK Compatibility Ideographs Supplement"),
  605. new UnicodeRange (0xE0000, 0xE007F ,"Tags"),
  606. };
  607. private void SetDemoTableStyles ()
  608. {
  609. var alignMid = new TableView.ColumnStyle () {
  610. Alignment = TextAlignment.Centered
  611. };
  612. var alignRight = new TableView.ColumnStyle () {
  613. Alignment = TextAlignment.Right
  614. };
  615. var dateFormatStyle = new TableView.ColumnStyle () {
  616. Alignment = TextAlignment.Right,
  617. RepresentationGetter = (v) => v is DateTime d ? d.ToString ("yyyy-MM-dd") : v.ToString ()
  618. };
  619. var negativeRight = new TableView.ColumnStyle () {
  620. Format = "0.##",
  621. MinWidth = 10,
  622. AlignmentGetter = (v) => v is double d ?
  623. // align negative values right
  624. d < 0 ? TextAlignment.Right :
  625. // align positive values left
  626. TextAlignment.Left :
  627. // not a double
  628. TextAlignment.Left,
  629. ColorGetter = (a) => a.CellValue is double d ?
  630. // color 0 and negative values red
  631. d <= 0.0000001 ? a.RowIndex % 2 == 0 && miAlternatingColors.Checked == true ? redColorSchemeAlt : redColorScheme :
  632. // use normal scheme for positive values
  633. null :
  634. // not a double
  635. null
  636. };
  637. tableView.Style.ColumnStyles.Add (tableView.Table.Columns ["DateCol"], dateFormatStyle);
  638. tableView.Style.ColumnStyles.Add (tableView.Table.Columns ["DoubleCol"], negativeRight);
  639. tableView.Style.ColumnStyles.Add (tableView.Table.Columns ["NullsCol"], alignMid);
  640. tableView.Style.ColumnStyles.Add (tableView.Table.Columns ["IntCol"], alignRight);
  641. tableView.Update ();
  642. }
  643. private void OpenSimple (bool big)
  644. {
  645. tableView.Table = BuildSimpleDataTable (big ? 30 : 5, big ? 1000 : 5);
  646. }
  647. private void EditCurrentCell (object sender, CellActivatedEventArgs e)
  648. {
  649. if (e.Table == null)
  650. return;
  651. var o = e.Table.Rows [e.Row] [e.Col];
  652. var title = o is uint u ? GetUnicodeCategory (u) + $"(0x{o:X4})" : "Enter new value";
  653. var oldValue = e.Table.Rows [e.Row] [e.Col].ToString ();
  654. bool okPressed = false;
  655. var ok = new Button ("Ok", is_default: true);
  656. ok.Clicked += (s,e) => { okPressed = true; Application.RequestStop (); };
  657. var cancel = new Button ("Cancel");
  658. cancel.Clicked += (s,e) => { Application.RequestStop (); };
  659. var d = new Dialog (ok, cancel) { Title = title };
  660. var lbl = new Label () {
  661. X = 0,
  662. Y = 1,
  663. Text = e.Table.Columns [e.Col].ColumnName
  664. };
  665. var tf = new TextField () {
  666. Text = oldValue,
  667. X = 0,
  668. Y = 2,
  669. Width = Dim.Fill ()
  670. };
  671. d.Add (lbl, tf);
  672. tf.SetFocus ();
  673. Application.Run (d);
  674. if (okPressed) {
  675. try {
  676. e.Table.Rows [e.Row] [e.Col] = string.IsNullOrWhiteSpace (tf.Text.ToString ()) ? DBNull.Value : (object)tf.Text;
  677. } catch (Exception ex) {
  678. MessageBox.ErrorQuery (60, 20, "Failed to set text", ex.Message, "Ok");
  679. }
  680. tableView.Update ();
  681. }
  682. }
  683. private string GetUnicodeCategory (uint u)
  684. {
  685. return Ranges.FirstOrDefault (r => u >= r.Start && u <= r.End)?.Category ?? "Unknown";
  686. }
  687. /// <summary>
  688. /// Generates a new demo <see cref="DataTable"/> with the given number of <paramref name="cols"/> (min 5) and <paramref name="rows"/>
  689. /// </summary>
  690. /// <param name="cols"></param>
  691. /// <param name="rows"></param>
  692. /// <returns></returns>
  693. public static DataTable BuildDemoDataTable (int cols, int rows)
  694. {
  695. var dt = new DataTable ();
  696. int explicitCols = 6;
  697. dt.Columns.Add (new DataColumn ("StrCol", typeof (string)));
  698. dt.Columns.Add (new DataColumn ("DateCol", typeof (DateTime)));
  699. dt.Columns.Add (new DataColumn ("IntCol", typeof (int)));
  700. dt.Columns.Add (new DataColumn ("DoubleCol", typeof (double)));
  701. dt.Columns.Add (new DataColumn ("NullsCol", typeof (string)));
  702. dt.Columns.Add (new DataColumn ("Unicode", typeof (string)));
  703. for (int i = 0; i < cols - explicitCols; i++) {
  704. dt.Columns.Add ("Column" + (i + explicitCols));
  705. }
  706. var r = new Random (100);
  707. for (int i = 0; i < rows; i++) {
  708. List<object> row = new List<object> (){
  709. "Some long text that is super cool",
  710. new DateTime(2000+i,12,25),
  711. r.Next(i),
  712. (r.NextDouble()*i)-0.5 /*add some negatives to demo styles*/,
  713. DBNull.Value,
  714. "Les Mise" + Char.ConvertFromUtf32(Int32.Parse("0301", NumberStyles.HexNumber)) + "rables"
  715. };
  716. for (int j = 0; j < cols - explicitCols; j++) {
  717. row.Add ("SomeValue" + r.Next (100));
  718. }
  719. dt.Rows.Add (row.ToArray ());
  720. }
  721. return dt;
  722. }
  723. /// <summary>
  724. /// Builds a simple table in which cell values contents are the index of the cell. This helps testing that scrolling etc is working correctly and not skipping out any rows/columns when paging
  725. /// </summary>
  726. /// <param name="cols"></param>
  727. /// <param name="rows"></param>
  728. /// <returns></returns>
  729. public static DataTable BuildSimpleDataTable (int cols, int rows)
  730. {
  731. var dt = new DataTable ();
  732. for (int c = 0; c < cols; c++) {
  733. dt.Columns.Add ("Col" + c);
  734. }
  735. for (int r = 0; r < rows; r++) {
  736. var newRow = dt.NewRow ();
  737. for (int c = 0; c < cols; c++) {
  738. newRow [c] = $"R{r}C{c}";
  739. }
  740. dt.Rows.Add (newRow);
  741. }
  742. return dt;
  743. }
  744. }
  745. }