TableEditor.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using Terminal.Gui;
  9. namespace UICatalog.Scenarios;
  10. [ScenarioMetadata ("TableEditor", "Implements data table editor using the TableView control.")]
  11. [ScenarioCategory ("TableView")]
  12. [ScenarioCategory ("Controls")]
  13. [ScenarioCategory ("Dialogs")]
  14. [ScenarioCategory ("Text and Formatting")]
  15. [ScenarioCategory ("Top Level Windows")]
  16. public class TableEditor : Scenario
  17. {
  18. private readonly HashSet<FileSystemInfo> _checkedFileSystemInfos = new ();
  19. private readonly List<IDisposable> _toDispose = new ();
  20. private readonly List<UnicodeRange> Ranges = new ()
  21. {
  22. new (
  23. 0x0000,
  24. 0x001F,
  25. "ASCII Control Characters"
  26. ),
  27. new (0x0080, 0x009F, "C0 Control Characters"),
  28. new (
  29. 0x1100,
  30. 0x11ff,
  31. "Hangul Jamo"
  32. ), // This is where wide chars tend to start
  33. new (0x20A0, 0x20CF, "Currency Symbols"),
  34. new (0x2100, 0x214F, "Letterlike Symbols"),
  35. new (0x2190, 0x21ff, "Arrows"),
  36. new (0x2200, 0x22ff, "Mathematical symbols"),
  37. new (
  38. 0x2300,
  39. 0x23ff,
  40. "Miscellaneous Technical"
  41. ),
  42. new (
  43. 0x2500,
  44. 0x25ff,
  45. "Box Drawing & Geometric Shapes"
  46. ),
  47. new (0x2600, 0x26ff, "Miscellaneous Symbols"),
  48. new (0x2700, 0x27ff, "Dingbats"),
  49. new (0x2800, 0x28ff, "Braille"),
  50. new (
  51. 0x2b00,
  52. 0x2bff,
  53. "Miscellaneous Symbols and Arrows"
  54. ),
  55. new (
  56. 0xFB00,
  57. 0xFb4f,
  58. "Alphabetic Presentation Forms"
  59. ),
  60. new (
  61. 0x12400,
  62. 0x1240f,
  63. "Cuneiform Numbers and Punctuation"
  64. ),
  65. new (
  66. (uint)(CharMap.MaxCodePoint - 16),
  67. (uint)CharMap.MaxCodePoint,
  68. "End"
  69. ),
  70. new (0x0020, 0x007F, "Basic Latin"),
  71. new (0x00A0, 0x00FF, "Latin-1 Supplement"),
  72. new (0x0100, 0x017F, "Latin Extended-A"),
  73. new (0x0180, 0x024F, "Latin Extended-B"),
  74. new (0x0250, 0x02AF, "IPA Extensions"),
  75. new (
  76. 0x02B0,
  77. 0x02FF,
  78. "Spacing Modifier Letters"
  79. ),
  80. new (
  81. 0x0300,
  82. 0x036F,
  83. "Combining Diacritical Marks"
  84. ),
  85. new (0x0370, 0x03FF, "Greek and Coptic"),
  86. new (0x0400, 0x04FF, "Cyrillic"),
  87. new (0x0500, 0x052F, "Cyrillic Supplementary"),
  88. new (0x0530, 0x058F, "Armenian"),
  89. new (0x0590, 0x05FF, "Hebrew"),
  90. new (0x0600, 0x06FF, "Arabic"),
  91. new (0x0700, 0x074F, "Syriac"),
  92. new (0x0780, 0x07BF, "Thaana"),
  93. new (0x0900, 0x097F, "Devanagari"),
  94. new (0x0980, 0x09FF, "Bengali"),
  95. new (0x0A00, 0x0A7F, "Gurmukhi"),
  96. new (0x0A80, 0x0AFF, "Gujarati"),
  97. new (0x0B00, 0x0B7F, "Oriya"),
  98. new (0x0B80, 0x0BFF, "Tamil"),
  99. new (0x0C00, 0x0C7F, "Telugu"),
  100. new (0x0C80, 0x0CFF, "Kannada"),
  101. new (0x0D00, 0x0D7F, "Malayalam"),
  102. new (0x0D80, 0x0DFF, "Sinhala"),
  103. new (0x0E00, 0x0E7F, "Thai"),
  104. new (0x0E80, 0x0EFF, "Lao"),
  105. new (0x0F00, 0x0FFF, "Tibetan"),
  106. new (0x1000, 0x109F, "Myanmar"),
  107. new (0x10A0, 0x10FF, "Georgian"),
  108. new (0x1100, 0x11FF, "Hangul Jamo"),
  109. new (0x1200, 0x137F, "Ethiopic"),
  110. new (0x13A0, 0x13FF, "Cherokee"),
  111. new (
  112. 0x1400,
  113. 0x167F,
  114. "Unified Canadian Aboriginal Syllabics"
  115. ),
  116. new (0x1680, 0x169F, "Ogham"),
  117. new (0x16A0, 0x16FF, "Runic"),
  118. new (0x1700, 0x171F, "Tagalog"),
  119. new (0x1720, 0x173F, "Hanunoo"),
  120. new (0x1740, 0x175F, "Buhid"),
  121. new (0x1760, 0x177F, "Tagbanwa"),
  122. new (0x1780, 0x17FF, "Khmer"),
  123. new (0x1800, 0x18AF, "Mongolian"),
  124. new (0x1900, 0x194F, "Limbu"),
  125. new (0x1950, 0x197F, "Tai Le"),
  126. new (0x19E0, 0x19FF, "Khmer Symbols"),
  127. new (0x1D00, 0x1D7F, "Phonetic Extensions"),
  128. new (
  129. 0x1E00,
  130. 0x1EFF,
  131. "Latin Extended Additional"
  132. ),
  133. new (0x1F00, 0x1FFF, "Greek Extended"),
  134. new (0x2000, 0x206F, "General Punctuation"),
  135. new (
  136. 0x2070,
  137. 0x209F,
  138. "Superscripts and Subscripts"
  139. ),
  140. new (0x20A0, 0x20CF, "Currency Symbols"),
  141. new (
  142. 0x20D0,
  143. 0x20FF,
  144. "Combining Diacritical Marks for Symbols"
  145. ),
  146. new (0x2100, 0x214F, "Letterlike Symbols"),
  147. new (0x2150, 0x218F, "Number Forms"),
  148. new (0x2190, 0x21FF, "Arrows"),
  149. new (0x2200, 0x22FF, "Mathematical Operators"),
  150. new (
  151. 0x2300,
  152. 0x23FF,
  153. "Miscellaneous Technical"
  154. ),
  155. new (0x2400, 0x243F, "Control Pictures"),
  156. new (
  157. 0x2440,
  158. 0x245F,
  159. "Optical Character Recognition"
  160. ),
  161. new (0x2460, 0x24FF, "Enclosed Alphanumerics"),
  162. new (0x2500, 0x257F, "Box Drawing"),
  163. new (0x2580, 0x259F, "Block Elements"),
  164. new (0x25A0, 0x25FF, "Geometric Shapes"),
  165. new (0x2600, 0x26FF, "Miscellaneous Symbols"),
  166. new (0x2700, 0x27BF, "Dingbats"),
  167. new (
  168. 0x27C0,
  169. 0x27EF,
  170. "Miscellaneous Mathematical Symbols-A"
  171. ),
  172. new (0x27F0, 0x27FF, "Supplemental Arrows-A"),
  173. new (0x2800, 0x28FF, "Braille Patterns"),
  174. new (0x2900, 0x297F, "Supplemental Arrows-B"),
  175. new (
  176. 0x2980,
  177. 0x29FF,
  178. "Miscellaneous Mathematical Symbols-B"
  179. ),
  180. new (
  181. 0x2A00,
  182. 0x2AFF,
  183. "Supplemental Mathematical Operators"
  184. ),
  185. new (
  186. 0x2B00,
  187. 0x2BFF,
  188. "Miscellaneous Symbols and Arrows"
  189. ),
  190. new (
  191. 0x2E80,
  192. 0x2EFF,
  193. "CJK Radicals Supplement"
  194. ),
  195. new (0x2F00, 0x2FDF, "Kangxi Radicals"),
  196. new (
  197. 0x2FF0,
  198. 0x2FFF,
  199. "Ideographic Description Characters"
  200. ),
  201. new (
  202. 0x3000,
  203. 0x303F,
  204. "CJK Symbols and Punctuation"
  205. ),
  206. new (0x3040, 0x309F, "Hiragana"),
  207. new (0x30A0, 0x30FF, "Katakana"),
  208. new (0x3100, 0x312F, "Bopomofo"),
  209. new (
  210. 0x3130,
  211. 0x318F,
  212. "Hangul Compatibility Jamo"
  213. ),
  214. new (0x3190, 0x319F, "Kanbun"),
  215. new (0x31A0, 0x31BF, "Bopomofo Extended"),
  216. new (
  217. 0x31F0,
  218. 0x31FF,
  219. "Katakana Phonetic Extensions"
  220. ),
  221. new (
  222. 0x3200,
  223. 0x32FF,
  224. "Enclosed CJK Letters and Months"
  225. ),
  226. new (0x3300, 0x33FF, "CJK Compatibility"),
  227. new (
  228. 0x3400,
  229. 0x4DBF,
  230. "CJK Unified Ideographs Extension A"
  231. ),
  232. new (
  233. 0x4DC0,
  234. 0x4DFF,
  235. "Yijing Hexagram Symbols"
  236. ),
  237. new (0x4E00, 0x9FFF, "CJK Unified Ideographs"),
  238. new (0xA000, 0xA48F, "Yi Syllables"),
  239. new (0xA490, 0xA4CF, "Yi Radicals"),
  240. new (0xAC00, 0xD7AF, "Hangul Syllables"),
  241. new (0xD800, 0xDB7F, "High Surrogates"),
  242. new (
  243. 0xDB80,
  244. 0xDBFF,
  245. "High Private Use Surrogates"
  246. ),
  247. new (0xDC00, 0xDFFF, "Low Surrogates"),
  248. new (0xE000, 0xF8FF, "Private Use Area"),
  249. new (
  250. 0xF900,
  251. 0xFAFF,
  252. "CJK Compatibility Ideographs"
  253. ),
  254. new (
  255. 0xFB00,
  256. 0xFB4F,
  257. "Alphabetic Presentation Forms"
  258. ),
  259. new (
  260. 0xFB50,
  261. 0xFDFF,
  262. "Arabic Presentation Forms-A"
  263. ),
  264. new (0xFE00, 0xFE0F, "Variation Selectors"),
  265. new (0xFE20, 0xFE2F, "Combining Half Marks"),
  266. new (
  267. 0xFE30,
  268. 0xFE4F,
  269. "CJK Compatibility Forms"
  270. ),
  271. new (0xFE50, 0xFE6F, "Small Form Variants"),
  272. new (
  273. 0xFE70,
  274. 0xFEFF,
  275. "Arabic Presentation Forms-B"
  276. ),
  277. new (
  278. 0xFF00,
  279. 0xFFEF,
  280. "Halfwidth and Fullwidth Forms"
  281. ),
  282. new (0xFFF0, 0xFFFF, "Specials"),
  283. new (0x10000, 0x1007F, "Linear B Syllabary"),
  284. new (0x10080, 0x100FF, "Linear B Ideograms"),
  285. new (0x10100, 0x1013F, "Aegean Numbers"),
  286. new (0x10300, 0x1032F, "Old Italic"),
  287. new (0x10330, 0x1034F, "Gothic"),
  288. new (0x10380, 0x1039F, "Ugaritic"),
  289. new (0x10400, 0x1044F, "Deseret"),
  290. new (0x10450, 0x1047F, "Shavian"),
  291. new (0x10480, 0x104AF, "Osmanya"),
  292. new (0x10800, 0x1083F, "Cypriot Syllabary"),
  293. new (
  294. 0x1D000,
  295. 0x1D0FF,
  296. "Byzantine Musical Symbols"
  297. ),
  298. new (0x1D100, 0x1D1FF, "Musical Symbols"),
  299. new (
  300. 0x1D300,
  301. 0x1D35F,
  302. "Tai Xuan Jing Symbols"
  303. ),
  304. new (
  305. 0x1D400,
  306. 0x1D7FF,
  307. "Mathematical Alphanumeric Symbols"
  308. ),
  309. new (0x1F600, 0x1F532, "Emojis Symbols"),
  310. new (
  311. 0x20000,
  312. 0x2A6DF,
  313. "CJK Unified Ideographs Extension B"
  314. ),
  315. new (
  316. 0x2F800,
  317. 0x2FA1F,
  318. "CJK Compatibility Ideographs Supplement"
  319. ),
  320. new (0xE0000, 0xE007F, "Tags")
  321. };
  322. private ColorScheme _alternatingColorScheme;
  323. private DataTable _currentTable;
  324. private MenuItem _miAlternatingColors;
  325. private MenuItem _miAlwaysShowHeaders;
  326. private MenuItem _miAlwaysUseNormalColorForVerticalCellLines;
  327. private MenuItem _miBottomline;
  328. private MenuItem _miCellLines;
  329. private MenuItem _miCheckboxes;
  330. private MenuItem _miCursor;
  331. private MenuItem _miExpandLastColumn;
  332. private MenuItem _miFullRowSelect;
  333. private MenuItem _miHeaderMidline;
  334. private MenuItem _miHeaderOverline;
  335. private MenuItem _miHeaderUnderline;
  336. private MenuItem _miRadioboxes;
  337. private MenuItem _miShowHeaders;
  338. private MenuItem _miShowHorizontalScrollIndicators;
  339. private MenuItem _miSmoothScrolling;
  340. private ColorScheme _redColorScheme;
  341. private ColorScheme _redColorSchemeAlt;
  342. private TableView _tableView;
  343. /// <summary>
  344. /// Generates a new demo <see cref="DataTable"/> with the given number of <paramref name="cols"/> (min 5) and
  345. /// <paramref name="rows"/>
  346. /// </summary>
  347. /// <param name="cols"></param>
  348. /// <param name="rows"></param>
  349. /// <returns></returns>
  350. public static DataTable BuildDemoDataTable (int cols, int rows)
  351. {
  352. var dt = new DataTable ();
  353. var explicitCols = 6;
  354. dt.Columns.Add (new DataColumn ("StrCol", typeof (string)));
  355. dt.Columns.Add (new DataColumn ("DateCol", typeof (DateTime)));
  356. dt.Columns.Add (new DataColumn ("IntCol", typeof (int)));
  357. dt.Columns.Add (new DataColumn ("DoubleCol", typeof (double)));
  358. dt.Columns.Add (new DataColumn ("NullsCol", typeof (string)));
  359. dt.Columns.Add (new DataColumn ("Unicode", typeof (string)));
  360. for (var i = 0; i < cols - explicitCols; i++)
  361. {
  362. dt.Columns.Add ("Column" + (i + explicitCols));
  363. }
  364. var r = new Random (100);
  365. for (var i = 0; i < rows; i++)
  366. {
  367. List<object> row = new ()
  368. {
  369. "Some long text that is super cool",
  370. new DateTime (2000 + i, 12, 25),
  371. r.Next (i),
  372. r.NextDouble () * i - 0.5 /*add some negatives to demo styles*/,
  373. DBNull.Value,
  374. "Les Mise"
  375. + char.ConvertFromUtf32 (int.Parse ("0301", NumberStyles.HexNumber))
  376. + "rables"
  377. };
  378. for (var j = 0; j < cols - explicitCols; j++)
  379. {
  380. row.Add ("SomeValue" + r.Next (100));
  381. }
  382. dt.Rows.Add (row.ToArray ());
  383. }
  384. return dt;
  385. }
  386. /// <summary>
  387. /// Builds a simple table in which cell values contents are the index of the cell. This helps testing that
  388. /// scrolling etc is working correctly and not skipping out any rows/columns when paging
  389. /// </summary>
  390. /// <param name="cols"></param>
  391. /// <param name="rows"></param>
  392. /// <returns></returns>
  393. public static DataTable BuildSimpleDataTable (int cols, int rows)
  394. {
  395. var dt = new DataTable ();
  396. for (var c = 0; c < cols; c++)
  397. {
  398. dt.Columns.Add ("Col" + c);
  399. }
  400. for (var r = 0; r < rows; r++)
  401. {
  402. DataRow newRow = dt.NewRow ();
  403. for (var c = 0; c < cols; c++)
  404. {
  405. newRow [c] = $"R{r}C{c}";
  406. }
  407. dt.Rows.Add (newRow);
  408. }
  409. return dt;
  410. }
  411. public override void Setup ()
  412. {
  413. Win.Title = GetName ();
  414. Win.Y = 1; // menu
  415. Win.Height = Dim.Fill (1); // status bar
  416. _tableView = new() { X = 0, Y = 0, Width = Dim.Fill (), Height = Dim.Fill (1) };
  417. var menu = new MenuBar
  418. {
  419. Menus =
  420. [
  421. new (
  422. "_File",
  423. new MenuItem []
  424. {
  425. new (
  426. "_OpenBigExample",
  427. "",
  428. () => OpenExample (true)
  429. ),
  430. new (
  431. "_OpenSmallExample",
  432. "",
  433. () => OpenExample (false)
  434. ),
  435. new (
  436. "OpenCharacter_Map",
  437. "",
  438. () => OpenUnicodeMap ()
  439. ),
  440. new (
  441. "OpenTreeExample",
  442. "",
  443. () => OpenTreeExample ()
  444. ),
  445. new (
  446. "_CloseExample",
  447. "",
  448. () => CloseExample ()
  449. ),
  450. new ("_Quit", "", () => Quit ())
  451. }
  452. ),
  453. new (
  454. "_View",
  455. new []
  456. {
  457. _miShowHeaders =
  458. new (
  459. "_ShowHeaders",
  460. "",
  461. () => ToggleShowHeaders ()
  462. )
  463. {
  464. Checked = _tableView.Style.ShowHeaders,
  465. CheckType = MenuItemCheckStyle.Checked
  466. },
  467. _miAlwaysShowHeaders =
  468. new (
  469. "_AlwaysShowHeaders",
  470. "",
  471. () => ToggleAlwaysShowHeaders ()
  472. )
  473. {
  474. Checked = _tableView.Style.AlwaysShowHeaders,
  475. CheckType = MenuItemCheckStyle.Checked
  476. },
  477. _miHeaderOverline =
  478. new (
  479. "_HeaderOverLine",
  480. "",
  481. () => ToggleOverline ()
  482. )
  483. {
  484. Checked = _tableView.Style
  485. .ShowHorizontalHeaderOverline,
  486. CheckType = MenuItemCheckStyle.Checked
  487. },
  488. _miHeaderMidline = new (
  489. "_HeaderMidLine",
  490. "",
  491. () => ToggleHeaderMidline ()
  492. )
  493. {
  494. Checked = _tableView.Style
  495. .ShowVerticalHeaderLines,
  496. CheckType = MenuItemCheckStyle.Checked
  497. },
  498. _miHeaderUnderline = new (
  499. "_HeaderUnderLine",
  500. "",
  501. () => ToggleUnderline ()
  502. )
  503. {
  504. Checked = _tableView.Style
  505. .ShowHorizontalHeaderUnderline,
  506. CheckType = MenuItemCheckStyle.Checked
  507. },
  508. _miBottomline = new (
  509. "_BottomLine",
  510. "",
  511. () => ToggleBottomline ()
  512. )
  513. {
  514. Checked = _tableView.Style
  515. .ShowHorizontalBottomline,
  516. CheckType = MenuItemCheckStyle
  517. .Checked
  518. },
  519. _miShowHorizontalScrollIndicators =
  520. new (
  521. "_HorizontalScrollIndicators",
  522. "",
  523. () =>
  524. ToggleHorizontalScrollIndicators ()
  525. )
  526. {
  527. Checked = _tableView.Style
  528. .ShowHorizontalScrollIndicators,
  529. CheckType = MenuItemCheckStyle.Checked
  530. },
  531. _miFullRowSelect = new (
  532. "_FullRowSelect",
  533. "",
  534. () => ToggleFullRowSelect ()
  535. )
  536. {
  537. Checked = _tableView.FullRowSelect,
  538. CheckType = MenuItemCheckStyle.Checked
  539. },
  540. _miCellLines = new (
  541. "_CellLines",
  542. "",
  543. () => ToggleCellLines ()
  544. )
  545. {
  546. Checked = _tableView.Style
  547. .ShowVerticalCellLines,
  548. CheckType = MenuItemCheckStyle
  549. .Checked
  550. },
  551. _miExpandLastColumn =
  552. new (
  553. "_ExpandLastColumn",
  554. "",
  555. () => ToggleExpandLastColumn ()
  556. )
  557. {
  558. Checked = _tableView.Style.ExpandLastColumn,
  559. CheckType = MenuItemCheckStyle.Checked
  560. },
  561. _miAlwaysUseNormalColorForVerticalCellLines =
  562. new (
  563. "_AlwaysUseNormalColorForVerticalCellLines",
  564. "",
  565. () =>
  566. ToggleAlwaysUseNormalColorForVerticalCellLines ()
  567. )
  568. {
  569. Checked = _tableView.Style
  570. .AlwaysUseNormalColorForVerticalCellLines,
  571. CheckType = MenuItemCheckStyle.Checked
  572. },
  573. _miSmoothScrolling =
  574. new (
  575. "_SmoothHorizontalScrolling",
  576. "",
  577. () => ToggleSmoothScrolling ()
  578. )
  579. {
  580. Checked = _tableView.Style
  581. .SmoothHorizontalScrolling,
  582. CheckType = MenuItemCheckStyle.Checked
  583. },
  584. new ("_AllLines", "", () => ToggleAllCellLines ()),
  585. new ("_NoLines", "", () => ToggleNoCellLines ()),
  586. _miCheckboxes = new (
  587. "_Checkboxes",
  588. "",
  589. () => ToggleCheckboxes (false)
  590. )
  591. {
  592. Checked = false,
  593. CheckType = MenuItemCheckStyle.Checked
  594. },
  595. _miRadioboxes = new (
  596. "_Radioboxes",
  597. "",
  598. () => ToggleCheckboxes (true)
  599. )
  600. {
  601. Checked = false,
  602. CheckType = MenuItemCheckStyle.Checked
  603. },
  604. _miAlternatingColors =
  605. new (
  606. "Alternating Colors",
  607. "",
  608. () => ToggleAlternatingColors ()
  609. ) { CheckType = MenuItemCheckStyle.Checked },
  610. _miCursor =
  611. new (
  612. "Invert Selected Cell First Character",
  613. "",
  614. () =>
  615. ToggleInvertSelectedCellFirstCharacter ()
  616. )
  617. {
  618. Checked = _tableView.Style
  619. .InvertSelectedCellFirstCharacter,
  620. CheckType = MenuItemCheckStyle.Checked
  621. },
  622. new (
  623. "_ClearColumnStyles",
  624. "",
  625. () => ClearColumnStyles ()
  626. ),
  627. new ("Sho_w All Columns", "", () => ShowAllColumns ())
  628. }
  629. ),
  630. new (
  631. "_Column",
  632. new MenuItem []
  633. {
  634. new ("_Set Max Width", "", SetMaxWidth),
  635. new ("_Set Min Width", "", SetMinWidth),
  636. new (
  637. "_Set MinAcceptableWidth",
  638. "",
  639. SetMinAcceptableWidth
  640. ),
  641. new (
  642. "_Set All MinAcceptableWidth=1",
  643. "",
  644. SetMinAcceptableWidthToOne
  645. )
  646. }
  647. )
  648. ]
  649. };
  650. Top.Add (menu);
  651. var statusBar = new StatusBar (
  652. new StatusItem []
  653. {
  654. new (
  655. KeyCode.F2,
  656. "~F2~ OpenExample",
  657. () => OpenExample (true)
  658. ),
  659. new (
  660. KeyCode.F3,
  661. "~F3~ CloseExample",
  662. () => CloseExample ()
  663. ),
  664. new (
  665. KeyCode.F4,
  666. "~F4~ OpenSimple",
  667. () => OpenSimple (true)
  668. ),
  669. new (
  670. Application.QuitKey,
  671. $"{Application.QuitKey} to Quit",
  672. () => Quit ()
  673. )
  674. }
  675. );
  676. Top.Add (statusBar);
  677. Win.Add (_tableView);
  678. var selectedCellLabel = new Label
  679. {
  680. X = 0,
  681. Y = Pos.Bottom (_tableView),
  682. Text = "0,0",
  683. Width = Dim.Fill (),
  684. TextAlignment = Alignment.End
  685. };
  686. Win.Add (selectedCellLabel);
  687. _tableView.SelectedCellChanged += (s, e) => { selectedCellLabel.Text = $"{_tableView.SelectedRow},{_tableView.SelectedColumn}"; };
  688. _tableView.CellActivated += EditCurrentCell;
  689. _tableView.KeyDown += TableViewKeyPress;
  690. SetupScrollBar ();
  691. _redColorScheme = new()
  692. {
  693. Disabled = Win.ColorScheme.Disabled,
  694. HotFocus = Win.ColorScheme.HotFocus,
  695. Focus = Win.ColorScheme.Focus,
  696. Normal = new (Color.Red, Win.ColorScheme.Normal.Background)
  697. };
  698. _alternatingColorScheme = new()
  699. {
  700. Disabled = Win.ColorScheme.Disabled,
  701. HotFocus = Win.ColorScheme.HotFocus,
  702. Focus = Win.ColorScheme.Focus,
  703. Normal = new (Color.White, Color.BrightBlue)
  704. };
  705. _redColorSchemeAlt = new()
  706. {
  707. Disabled = Win.ColorScheme.Disabled,
  708. HotFocus = Win.ColorScheme.HotFocus,
  709. Focus = Win.ColorScheme.Focus,
  710. Normal = new (Color.Red, Color.BrightBlue)
  711. };
  712. // if user clicks the mouse in TableView
  713. _tableView.MouseClick += (s, e) =>
  714. {
  715. if (_currentTable == null)
  716. {
  717. return;
  718. }
  719. _tableView.ScreenToCell (e.MouseEvent.Position, out int? clickedCol);
  720. if (clickedCol != null)
  721. {
  722. if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
  723. {
  724. // left click in a header
  725. SortColumn (clickedCol.Value);
  726. }
  727. else if (e.MouseEvent.Flags.HasFlag (MouseFlags.Button3Clicked))
  728. {
  729. // right click in a header
  730. ShowHeaderContextMenu (clickedCol.Value, e);
  731. }
  732. }
  733. };
  734. _tableView.KeyBindings.Add (Key.Space, Command.Accept);
  735. }
  736. protected override void Dispose (bool disposing)
  737. {
  738. base.Dispose (disposing);
  739. foreach (IDisposable d in _toDispose)
  740. {
  741. d.Dispose ();
  742. }
  743. }
  744. private DataTable BuildUnicodeMap ()
  745. {
  746. var dt = new DataTable ();
  747. // add cols called 0 to 9
  748. for (var i = 0; i < 10; i++)
  749. {
  750. DataColumn col = dt.Columns.Add (i.ToString (), typeof (uint));
  751. ColumnStyle style = _tableView.Style.GetOrCreateColumnStyle (col.Ordinal);
  752. style.RepresentationGetter = o => new Rune ((uint)o).ToString ();
  753. }
  754. // add cols called a to z
  755. for (int i = 'a'; i < 'a' + 26; i++)
  756. {
  757. DataColumn col = dt.Columns.Add (((char)i).ToString (), typeof (uint));
  758. ColumnStyle style = _tableView.Style.GetOrCreateColumnStyle (col.Ordinal);
  759. style.RepresentationGetter = o => new Rune ((uint)o).ToString ();
  760. }
  761. // now add table contents
  762. List<uint> runes = new ();
  763. foreach (UnicodeRange range in Ranges)
  764. {
  765. for (uint i = range.Start; i <= range.End; i++)
  766. {
  767. runes.Add (i);
  768. }
  769. }
  770. DataRow dr = null;
  771. for (var i = 0; i < runes.Count; i++)
  772. {
  773. if (dr == null || i % dt.Columns.Count == 0)
  774. {
  775. dr = dt.Rows.Add ();
  776. }
  777. dr [i % dt.Columns.Count] = runes [i].ToString ();
  778. }
  779. return dt;
  780. }
  781. private void CheckOrUncheckFile (FileSystemInfo info, bool check)
  782. {
  783. if (check)
  784. {
  785. _checkedFileSystemInfos.Add (info);
  786. }
  787. else
  788. {
  789. _checkedFileSystemInfos.Remove (info);
  790. }
  791. }
  792. private void ClearColumnStyles ()
  793. {
  794. _tableView.Style.ColumnStyles.Clear ();
  795. _tableView.Update ();
  796. }
  797. private void CloseExample () { _tableView.Table = null; }
  798. private void EditCurrentCell (object sender, CellActivatedEventArgs e)
  799. {
  800. if (e.Table is not DataTableSource || _currentTable == null)
  801. {
  802. return;
  803. }
  804. int tableCol = ToTableCol (e.Col);
  805. if (tableCol < 0)
  806. {
  807. return;
  808. }
  809. object o = _currentTable.Rows [e.Row] [tableCol];
  810. string title = o is uint u ? GetUnicodeCategory (u) + $"(0x{o:X4})" : "Enter new value";
  811. var oldValue = _currentTable.Rows [e.Row] [tableCol].ToString ();
  812. var okPressed = false;
  813. var ok = new Button { Text = "Ok", IsDefault = true };
  814. ok.Accept += (s, e) =>
  815. {
  816. okPressed = true;
  817. Application.RequestStop ();
  818. };
  819. var cancel = new Button { Text = "Cancel" };
  820. cancel.Accept += (s, e) => { Application.RequestStop (); };
  821. var d = new Dialog { Title = title, Buttons = [ok, cancel] };
  822. var lbl = new Label { X = 0, Y = 1, Text = _tableView.Table.ColumnNames [e.Col] };
  823. var tf = new TextField { Text = oldValue, X = 0, Y = 2, Width = Dim.Fill () };
  824. d.Add (lbl, tf);
  825. tf.SetFocus ();
  826. Application.Run (d);
  827. d.Dispose ();
  828. if (okPressed)
  829. {
  830. try
  831. {
  832. _currentTable.Rows [e.Row] [tableCol] =
  833. string.IsNullOrWhiteSpace (tf.Text) ? DBNull.Value : tf.Text;
  834. }
  835. catch (Exception ex)
  836. {
  837. MessageBox.ErrorQuery (60, 20, "Failed to set text", ex.Message, "Ok");
  838. }
  839. _tableView.Update ();
  840. }
  841. }
  842. private IEnumerable<FileSystemInfo> GetChildren (FileSystemInfo arg)
  843. {
  844. try
  845. {
  846. return arg is DirectoryInfo d ? d.GetFileSystemInfos () : Enumerable.Empty<FileSystemInfo> ();
  847. }
  848. catch (Exception)
  849. {
  850. // Permission denied etc
  851. return Enumerable.Empty<FileSystemInfo> ();
  852. }
  853. }
  854. private int? GetColumn ()
  855. {
  856. if (_tableView.Table == null)
  857. {
  858. return null;
  859. }
  860. if (_tableView.SelectedColumn < 0 || _tableView.SelectedColumn > _tableView.Table.Columns)
  861. {
  862. return null;
  863. }
  864. return _tableView.SelectedColumn;
  865. }
  866. private string GetHumanReadableFileSize (FileSystemInfo fsi)
  867. {
  868. if (fsi is not FileInfo fi)
  869. {
  870. return null;
  871. }
  872. long value = fi.Length;
  873. CultureInfo culture = CultureInfo.CurrentUICulture;
  874. return GetHumanReadableFileSize (value, culture);
  875. }
  876. private string GetHumanReadableFileSize (long value, CultureInfo culture)
  877. {
  878. const long ByteConversion = 1024;
  879. string [] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
  880. if (value < 0)
  881. {
  882. return "-" + GetHumanReadableFileSize (-value, culture);
  883. }
  884. if (value == 0)
  885. {
  886. return "0.0 bytes";
  887. }
  888. var mag = (int)Math.Log (value, ByteConversion);
  889. double adjustedSize = value / Math.Pow (1000, mag);
  890. return string.Format (culture.NumberFormat, "{0:n2} {1}", adjustedSize, SizeSuffixes [mag]);
  891. }
  892. private string GetProposedNewSortOrder (int clickedCol, out bool isAsc)
  893. {
  894. // work out new sort order
  895. string sort = _currentTable.DefaultView.Sort;
  896. string colName = _tableView.Table.ColumnNames [clickedCol];
  897. if (sort?.EndsWith ("ASC") ?? false)
  898. {
  899. sort = $"{colName} DESC";
  900. isAsc = false;
  901. }
  902. else
  903. {
  904. sort = $"{colName} ASC";
  905. isAsc = true;
  906. }
  907. return sort;
  908. }
  909. private string GetUnicodeCategory (uint u) { return Ranges.FirstOrDefault (r => u >= r.Start && u <= r.End)?.Category ?? "Unknown"; }
  910. private bool HasCheckboxes () { return _tableView.Table is CheckBoxTableSourceWrapperBase; }
  911. private void HideColumn (int clickedCol)
  912. {
  913. ColumnStyle style = _tableView.Style.GetOrCreateColumnStyle (clickedCol);
  914. style.Visible = false;
  915. _tableView.Update ();
  916. }
  917. private void OpenExample (bool big)
  918. {
  919. SetTable (BuildDemoDataTable (big ? 30 : 5, big ? 1000 : 5));
  920. SetDemoTableStyles ();
  921. }
  922. private void OpenSimple (bool big) { SetTable (BuildSimpleDataTable (big ? 30 : 5, big ? 1000 : 5)); }
  923. private void OpenTreeExample ()
  924. {
  925. _tableView.Style.ColumnStyles.Clear ();
  926. TreeView<FileSystemInfo> tree = new ()
  927. {
  928. AspectGetter = f => f.Name, TreeBuilder = new DelegateTreeBuilder<FileSystemInfo> (GetChildren)
  929. };
  930. TreeTableSource<FileSystemInfo> source = new (
  931. _tableView,
  932. "Name",
  933. tree,
  934. new()
  935. {
  936. { "Extension", f => f.Extension },
  937. { "CreationTime", f => f.CreationTime },
  938. { "FileSize", GetHumanReadableFileSize }
  939. }
  940. );
  941. HashSet<string> seen = new ();
  942. try
  943. {
  944. foreach (string path in Environment.GetLogicalDrives ())
  945. {
  946. tree.AddObject (new DirectoryInfo (path));
  947. }
  948. }
  949. catch (Exception e)
  950. {
  951. MessageBox.ErrorQuery ("Could not find local drives", e.Message, "Ok");
  952. }
  953. _tableView.Table = source;
  954. _toDispose.Add (tree);
  955. }
  956. private void OpenUnicodeMap ()
  957. {
  958. SetTable (BuildUnicodeMap ());
  959. _tableView.Update ();
  960. }
  961. private void Quit () { Application.RequestStop (); }
  962. private void RunColumnWidthDialog (
  963. int? col,
  964. string prompt,
  965. Action<ColumnStyle, int> setter,
  966. Func<ColumnStyle, int> getter
  967. )
  968. {
  969. if (col == null)
  970. {
  971. return;
  972. }
  973. var accepted = false;
  974. var ok = new Button { Text = "Ok", IsDefault = true };
  975. ok.Accept += (s, e) =>
  976. {
  977. accepted = true;
  978. Application.RequestStop ();
  979. };
  980. var cancel = new Button { Text = "Cancel" };
  981. cancel.Accept += (s, e) => { Application.RequestStop (); };
  982. var d = new Dialog { Title = prompt, Buttons = [ok, cancel] };
  983. ColumnStyle style = _tableView.Style.GetOrCreateColumnStyle (col.Value);
  984. var lbl = new Label { X = 0, Y = 1, Text = _tableView.Table.ColumnNames [col.Value] };
  985. var tf = new TextField { Text = getter (style).ToString (), X = 0, Y = 2, Width = Dim.Fill () };
  986. d.Add (lbl, tf);
  987. tf.SetFocus ();
  988. Application.Run (d);
  989. d.Dispose ();
  990. if (accepted)
  991. {
  992. try
  993. {
  994. setter (style, int.Parse (tf.Text));
  995. }
  996. catch (Exception ex)
  997. {
  998. MessageBox.ErrorQuery (60, 20, "Failed to set", ex.Message, "Ok");
  999. }
  1000. _tableView.Update ();
  1001. }
  1002. }
  1003. private void SetDemoTableStyles ()
  1004. {
  1005. _tableView.Style.ColumnStyles.Clear ();
  1006. var alignMid = new ColumnStyle { Alignment = Alignment.Center };
  1007. var alignRight = new ColumnStyle { Alignment = Alignment.End };
  1008. var dateFormatStyle = new ColumnStyle
  1009. {
  1010. Alignment = Alignment.End,
  1011. RepresentationGetter = v =>
  1012. v is DateTime d ? d.ToString ("yyyy-MM-dd") : v.ToString ()
  1013. };
  1014. var negativeRight = new ColumnStyle
  1015. {
  1016. Format = "0.##",
  1017. MinWidth = 10,
  1018. AlignmentGetter = v => v is double d
  1019. ?
  1020. // align negative values right
  1021. d < 0
  1022. ? Alignment.End
  1023. :
  1024. // align positive values left
  1025. Alignment.Start
  1026. :
  1027. // not a double
  1028. Alignment.Start,
  1029. ColorGetter = a => a.CellValue is double d
  1030. ?
  1031. // color 0 and negative values red
  1032. d <= 0.0000001
  1033. ? a.RowIndex % 2 == 0
  1034. && _miAlternatingColors.Checked == true
  1035. ? _redColorSchemeAlt
  1036. : _redColorScheme
  1037. :
  1038. // use normal scheme for positive values
  1039. null
  1040. :
  1041. // not a double
  1042. null
  1043. };
  1044. _tableView.Style.ColumnStyles.Add (_currentTable.Columns ["DateCol"].Ordinal, dateFormatStyle);
  1045. _tableView.Style.ColumnStyles.Add (_currentTable.Columns ["DoubleCol"].Ordinal, negativeRight);
  1046. _tableView.Style.ColumnStyles.Add (_currentTable.Columns ["NullsCol"].Ordinal, alignMid);
  1047. _tableView.Style.ColumnStyles.Add (_currentTable.Columns ["IntCol"].Ordinal, alignRight);
  1048. _tableView.Update ();
  1049. }
  1050. private void SetMaxWidth ()
  1051. {
  1052. int? col = GetColumn ();
  1053. RunColumnWidthDialog (col, "MaxWidth", (s, v) => s.MaxWidth = v, s => s.MaxWidth);
  1054. }
  1055. private void SetMinAcceptableWidth ()
  1056. {
  1057. int? col = GetColumn ();
  1058. RunColumnWidthDialog (
  1059. col,
  1060. "MinAcceptableWidth",
  1061. (s, v) => s.MinAcceptableWidth = v,
  1062. s => s.MinAcceptableWidth
  1063. );
  1064. }
  1065. private void SetMinAcceptableWidthToOne ()
  1066. {
  1067. for (var i = 0; i < _tableView.Table.Columns; i++)
  1068. {
  1069. ColumnStyle style = _tableView.Style.GetOrCreateColumnStyle (i);
  1070. style.MinAcceptableWidth = 1;
  1071. }
  1072. }
  1073. private void SetMinWidth ()
  1074. {
  1075. int? col = GetColumn ();
  1076. RunColumnWidthDialog (col, "MinWidth", (s, v) => s.MinWidth = v, s => s.MinWidth);
  1077. }
  1078. private void SetTable (DataTable dataTable) { _tableView.Table = new DataTableSource (_currentTable = dataTable); }
  1079. private void SetupScrollBar ()
  1080. {
  1081. var scrollBar = new ScrollBarView (_tableView, true);
  1082. scrollBar.ChangedPosition += (s, e) =>
  1083. {
  1084. _tableView.RowOffset = scrollBar.Position;
  1085. if (_tableView.RowOffset != scrollBar.Position)
  1086. {
  1087. scrollBar.Position = _tableView.RowOffset;
  1088. }
  1089. _tableView.SetNeedsDisplay ();
  1090. };
  1091. /*
  1092. scrollBar.OtherScrollBarView.ChangedPosition += (s,e) => {
  1093. tableView.LeftItem = scrollBar.OtherScrollBarView.Position;
  1094. if (tableView.LeftItem != scrollBar.OtherScrollBarView.Position) {
  1095. scrollBar.OtherScrollBarView.Position = tableView.LeftItem;
  1096. }
  1097. tableView.SetNeedsDisplay ();
  1098. };*/
  1099. _tableView.DrawContent += (s, e) =>
  1100. {
  1101. scrollBar.Size = _tableView.Table?.Rows ?? 0;
  1102. scrollBar.Position = _tableView.RowOffset;
  1103. //scrollBar.OtherScrollBarView.Size = tableView.Maxlength - 1;
  1104. //scrollBar.OtherScrollBarView.Position = tableView.LeftItem;
  1105. scrollBar.Refresh ();
  1106. };
  1107. }
  1108. private void ShowAllColumns ()
  1109. {
  1110. foreach (KeyValuePair<int, ColumnStyle> colStyle in _tableView.Style.ColumnStyles)
  1111. {
  1112. colStyle.Value.Visible = true;
  1113. }
  1114. _tableView.Update ();
  1115. }
  1116. private void ShowHeaderContextMenu (int clickedCol, MouseEventEventArgs e)
  1117. {
  1118. if (HasCheckboxes () && clickedCol == 0)
  1119. {
  1120. return;
  1121. }
  1122. string sort = GetProposedNewSortOrder (clickedCol, out bool isAsc);
  1123. string colName = _tableView.Table.ColumnNames [clickedCol];
  1124. var contextMenu = new ContextMenu
  1125. {
  1126. Position = new (e.MouseEvent.Position.X + 1, e.MouseEvent.Position.Y + 1),
  1127. MenuItems = new (
  1128. [
  1129. new (
  1130. $"Hide {TrimArrows (colName)}",
  1131. "",
  1132. () => HideColumn (clickedCol)
  1133. ),
  1134. new (
  1135. $"Sort {StripArrows (sort)}",
  1136. "",
  1137. () => SortColumn (
  1138. clickedCol,
  1139. sort,
  1140. isAsc
  1141. )
  1142. )
  1143. ]
  1144. )
  1145. };
  1146. contextMenu.Show ();
  1147. }
  1148. private void SortColumn (int clickedCol)
  1149. {
  1150. string sort = GetProposedNewSortOrder (clickedCol, out bool isAsc);
  1151. // don't try to sort on the toggled column
  1152. if (HasCheckboxes () && clickedCol == 0)
  1153. {
  1154. return;
  1155. }
  1156. SortColumn (clickedCol, sort, isAsc);
  1157. }
  1158. private void SortColumn (int clickedCol, string sort, bool isAsc)
  1159. {
  1160. if (_currentTable == null)
  1161. {
  1162. return;
  1163. }
  1164. // set a sort order
  1165. _currentTable.DefaultView.Sort = sort;
  1166. // copy the rows from the view
  1167. DataTable sortedCopy = _currentTable.DefaultView.ToTable ();
  1168. _currentTable.Rows.Clear ();
  1169. foreach (DataRow r in sortedCopy.Rows)
  1170. {
  1171. _currentTable.ImportRow (r);
  1172. }
  1173. foreach (DataColumn col in _currentTable.Columns)
  1174. {
  1175. // remove any lingering sort indicator
  1176. col.ColumnName = TrimArrows (col.ColumnName);
  1177. // add a new one if this the one that is being sorted
  1178. if (col.Ordinal == clickedCol)
  1179. {
  1180. col.ColumnName += isAsc ? CM.Glyphs.UpArrow : CM.Glyphs.DownArrow;
  1181. }
  1182. }
  1183. _tableView.Update ();
  1184. }
  1185. private string StripArrows (string columnName) { return columnName.Replace ($"{CM.Glyphs.DownArrow}", "").Replace ($"{CM.Glyphs.UpArrow}", ""); }
  1186. private void TableViewKeyPress (object sender, Key e)
  1187. {
  1188. if (_currentTable == null)
  1189. {
  1190. return;
  1191. }
  1192. if (e.KeyCode == KeyCode.Delete)
  1193. {
  1194. if (_tableView.FullRowSelect)
  1195. {
  1196. // Delete button deletes all rows when in full row mode
  1197. foreach (int toRemove in _tableView.GetAllSelectedCells ()
  1198. .Select (p => p.Y)
  1199. .Distinct ()
  1200. .OrderByDescending (i => i))
  1201. {
  1202. _currentTable.Rows.RemoveAt (toRemove);
  1203. }
  1204. }
  1205. else
  1206. {
  1207. // otherwise set all selected cells to null
  1208. foreach (Point pt in _tableView.GetAllSelectedCells ())
  1209. {
  1210. _currentTable.Rows [pt.Y] [pt.X] = DBNull.Value;
  1211. }
  1212. }
  1213. _tableView.Update ();
  1214. e.Handled = true;
  1215. }
  1216. }
  1217. private void ToggleAllCellLines ()
  1218. {
  1219. _tableView.Style.ShowHorizontalHeaderOverline = true;
  1220. _tableView.Style.ShowVerticalHeaderLines = true;
  1221. _tableView.Style.ShowHorizontalHeaderUnderline = true;
  1222. _tableView.Style.ShowVerticalCellLines = true;
  1223. _miHeaderOverline.Checked = true;
  1224. _miHeaderMidline.Checked = true;
  1225. _miHeaderUnderline.Checked = true;
  1226. _miCellLines.Checked = true;
  1227. _tableView.Update ();
  1228. }
  1229. private void ToggleAlternatingColors ()
  1230. {
  1231. //toggle menu item
  1232. _miAlternatingColors.Checked = !_miAlternatingColors.Checked;
  1233. if (_miAlternatingColors.Checked == true)
  1234. {
  1235. _tableView.Style.RowColorGetter = a => { return a.RowIndex % 2 == 0 ? _alternatingColorScheme : null; };
  1236. }
  1237. else
  1238. {
  1239. _tableView.Style.RowColorGetter = null;
  1240. }
  1241. _tableView.SetNeedsDisplay ();
  1242. }
  1243. private void ToggleAlwaysShowHeaders ()
  1244. {
  1245. _miAlwaysShowHeaders.Checked = !_miAlwaysShowHeaders.Checked;
  1246. _tableView.Style.AlwaysShowHeaders = (bool)_miAlwaysShowHeaders.Checked;
  1247. _tableView.Update ();
  1248. }
  1249. private void ToggleAlwaysUseNormalColorForVerticalCellLines ()
  1250. {
  1251. _miAlwaysUseNormalColorForVerticalCellLines.Checked =
  1252. !_miAlwaysUseNormalColorForVerticalCellLines.Checked;
  1253. _tableView.Style.AlwaysUseNormalColorForVerticalCellLines =
  1254. (bool)_miAlwaysUseNormalColorForVerticalCellLines.Checked;
  1255. _tableView.Update ();
  1256. }
  1257. private void ToggleBottomline ()
  1258. {
  1259. _miBottomline.Checked = !_miBottomline.Checked;
  1260. _tableView.Style.ShowHorizontalBottomline = (bool)_miBottomline.Checked;
  1261. _tableView.Update ();
  1262. }
  1263. private void ToggleCellLines ()
  1264. {
  1265. _miCellLines.Checked = !_miCellLines.Checked;
  1266. _tableView.Style.ShowVerticalCellLines = (bool)_miCellLines.Checked;
  1267. _tableView.Update ();
  1268. }
  1269. private void ToggleCheckboxes (bool radio)
  1270. {
  1271. if (_tableView.Table is CheckBoxTableSourceWrapperBase wrapper)
  1272. {
  1273. // unwrap it to remove check boxes
  1274. _tableView.Table = wrapper.Wrapping;
  1275. _miCheckboxes.Checked = false;
  1276. _miRadioboxes.Checked = false;
  1277. // if toggling off checkboxes/radio
  1278. if (wrapper.UseRadioButtons == radio)
  1279. {
  1280. return;
  1281. }
  1282. }
  1283. ITableSource source;
  1284. // Either toggling on checkboxes/radio or switching from radio to checkboxes (or vice versa)
  1285. if (_tableView.Table is TreeTableSource<FileSystemInfo> treeSource)
  1286. {
  1287. source = new CheckBoxTableSourceWrapperByObject<FileSystemInfo> (
  1288. _tableView,
  1289. treeSource,
  1290. _checkedFileSystemInfos.Contains,
  1291. CheckOrUncheckFile
  1292. ) { UseRadioButtons = radio };
  1293. }
  1294. else
  1295. {
  1296. source = new CheckBoxTableSourceWrapperByIndex (_tableView, _tableView.Table) { UseRadioButtons = radio };
  1297. }
  1298. _tableView.Table = source;
  1299. if (radio)
  1300. {
  1301. _miRadioboxes.Checked = true;
  1302. _miCheckboxes.Checked = false;
  1303. }
  1304. else
  1305. {
  1306. _miRadioboxes.Checked = false;
  1307. _miCheckboxes.Checked = true;
  1308. }
  1309. }
  1310. private void ToggleExpandLastColumn ()
  1311. {
  1312. _miExpandLastColumn.Checked = !_miExpandLastColumn.Checked;
  1313. _tableView.Style.ExpandLastColumn = (bool)_miExpandLastColumn.Checked;
  1314. _tableView.Update ();
  1315. }
  1316. private void ToggleFullRowSelect ()
  1317. {
  1318. _miFullRowSelect.Checked = !_miFullRowSelect.Checked;
  1319. _tableView.FullRowSelect = (bool)_miFullRowSelect.Checked;
  1320. _tableView.Update ();
  1321. }
  1322. private void ToggleHeaderMidline ()
  1323. {
  1324. _miHeaderMidline.Checked = !_miHeaderMidline.Checked;
  1325. _tableView.Style.ShowVerticalHeaderLines = (bool)_miHeaderMidline.Checked;
  1326. _tableView.Update ();
  1327. }
  1328. private void ToggleHorizontalScrollIndicators ()
  1329. {
  1330. _miShowHorizontalScrollIndicators.Checked = !_miShowHorizontalScrollIndicators.Checked;
  1331. _tableView.Style.ShowHorizontalScrollIndicators = (bool)_miShowHorizontalScrollIndicators.Checked;
  1332. _tableView.Update ();
  1333. }
  1334. private void ToggleInvertSelectedCellFirstCharacter ()
  1335. {
  1336. //toggle menu item
  1337. _miCursor.Checked = !_miCursor.Checked;
  1338. _tableView.Style.InvertSelectedCellFirstCharacter = (bool)_miCursor.Checked;
  1339. _tableView.SetNeedsDisplay ();
  1340. }
  1341. private void ToggleNoCellLines ()
  1342. {
  1343. _tableView.Style.ShowHorizontalHeaderOverline = false;
  1344. _tableView.Style.ShowVerticalHeaderLines = false;
  1345. _tableView.Style.ShowHorizontalHeaderUnderline = false;
  1346. _tableView.Style.ShowVerticalCellLines = false;
  1347. _miHeaderOverline.Checked = false;
  1348. _miHeaderMidline.Checked = false;
  1349. _miHeaderUnderline.Checked = false;
  1350. _miCellLines.Checked = false;
  1351. _tableView.Update ();
  1352. }
  1353. private void ToggleOverline ()
  1354. {
  1355. _miHeaderOverline.Checked = !_miHeaderOverline.Checked;
  1356. _tableView.Style.ShowHorizontalHeaderOverline = (bool)_miHeaderOverline.Checked;
  1357. _tableView.Update ();
  1358. }
  1359. private void ToggleShowHeaders ()
  1360. {
  1361. _miShowHeaders.Checked = !_miShowHeaders.Checked;
  1362. _tableView.Style.ShowHeaders = (bool)_miShowHeaders.Checked;
  1363. _tableView.Update ();
  1364. }
  1365. private void ToggleSmoothScrolling ()
  1366. {
  1367. _miSmoothScrolling.Checked = !_miSmoothScrolling.Checked;
  1368. _tableView.Style.SmoothHorizontalScrolling = (bool)_miSmoothScrolling.Checked;
  1369. _tableView.Update ();
  1370. }
  1371. private void ToggleUnderline ()
  1372. {
  1373. _miHeaderUnderline.Checked = !_miHeaderUnderline.Checked;
  1374. _tableView.Style.ShowHorizontalHeaderUnderline = (bool)_miHeaderUnderline.Checked;
  1375. _tableView.Update ();
  1376. }
  1377. private int ToTableCol (int col)
  1378. {
  1379. if (HasCheckboxes ())
  1380. {
  1381. return col - 1;
  1382. }
  1383. return col;
  1384. }
  1385. private string TrimArrows (string columnName)
  1386. {
  1387. return columnName.TrimEnd (
  1388. (char)CM.Glyphs.UpArrow.Value,
  1389. (char)CM.Glyphs.DownArrow.Value
  1390. );
  1391. }
  1392. private class UnicodeRange
  1393. {
  1394. public readonly string Category;
  1395. public readonly uint End;
  1396. public readonly uint Start;
  1397. public UnicodeRange (uint start, uint end, string category)
  1398. {
  1399. Start = start;
  1400. End = end;
  1401. Category = category;
  1402. }
  1403. }
  1404. }