TableEditor.cs 54 KB

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