TableEditor.cs 53 KB

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