MultiColouredTable.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using System.Data;
  3. using System.Text;
  4. using Terminal.Gui;
  5. namespace UICatalog.Scenarios {
  6. [ScenarioMetadata (Name: "MultiColouredTable", Description: "Demonstrates how to multi color cell contents.")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("Colors")]
  9. [ScenarioCategory ("TableView")]
  10. public class MultiColouredTable : Scenario {
  11. TableViewColors tableView;
  12. private DataTable table;
  13. public override void Setup ()
  14. {
  15. Win.Title = this.GetName ();
  16. Win.Y = 1; // menu
  17. Win.Height = Dim.Fill (1); // status bar
  18. Application.Top.LayoutSubviews ();
  19. this.tableView = new TableViewColors () {
  20. X = 0,
  21. Y = 0,
  22. Width = Dim.Fill (),
  23. Height = Dim.Fill (1),
  24. };
  25. var menu = new MenuBar (new MenuBarItem [] {
  26. new MenuBarItem ("_File", new MenuItem [] {
  27. new MenuItem ("_Quit", "", () => Quit()),
  28. }),
  29. });
  30. Application.Top.Add (menu);
  31. var statusBar = new StatusBar (new StatusItem [] {
  32. new StatusItem(Application.QuitKey, $"{Application.QuitKey} to Quit", () => Quit()),
  33. });
  34. Application.Top.Add (statusBar);
  35. Win.Add (tableView);
  36. tableView.CellActivated += EditCurrentCell;
  37. var dt = new DataTable ();
  38. dt.Columns.Add ("Col1");
  39. dt.Columns.Add ("Col2");
  40. dt.Rows.Add ("some text", "Rainbows and Unicorns are so fun!");
  41. dt.Rows.Add ("some text", "When it rains you get rainbows");
  42. dt.Rows.Add (DBNull.Value, DBNull.Value);
  43. dt.Rows.Add (DBNull.Value, DBNull.Value);
  44. dt.Rows.Add (DBNull.Value, DBNull.Value);
  45. dt.Rows.Add (DBNull.Value, DBNull.Value);
  46. tableView.ColorScheme = new ColorScheme () {
  47. Disabled = Win.ColorScheme.Disabled,
  48. HotFocus = Win.ColorScheme.HotFocus,
  49. Focus = Win.ColorScheme.Focus,
  50. Normal = new Attribute (Color.DarkGray, Color.Black)
  51. };
  52. tableView.Table = new DataTableSource (this.table = dt);
  53. }
  54. private void Quit ()
  55. {
  56. Application.RequestStop ();
  57. }
  58. private bool GetText (string title, string label, string initialText, out string enteredText)
  59. {
  60. bool okPressed = false;
  61. var ok = new Button ("Ok", is_default: true);
  62. ok.Clicked += (s, e) => { okPressed = true; Application.RequestStop (); };
  63. var cancel = new Button ("Cancel");
  64. cancel.Clicked += (s, e) => { Application.RequestStop (); };
  65. var d = new Dialog (ok, cancel) { Title = title };
  66. var lbl = new Label () {
  67. X = 0,
  68. Y = 1,
  69. Text = label
  70. };
  71. var tf = new TextField () {
  72. Text = initialText,
  73. X = 0,
  74. Y = 2,
  75. Width = Dim.Fill ()
  76. };
  77. d.Add (lbl, tf);
  78. tf.SetFocus ();
  79. Application.Run (d);
  80. enteredText = okPressed ? tf.Text : null;
  81. return okPressed;
  82. }
  83. private void EditCurrentCell (object sender, CellActivatedEventArgs e)
  84. {
  85. if (e.Table == null)
  86. return;
  87. var oldValue = e.Table [e.Row, e.Col].ToString ();
  88. if (GetText ("Enter new value", e.Table.ColumnNames [e.Col], oldValue, out string newText)) {
  89. try {
  90. table.Rows [e.Row] [e.Col] = string.IsNullOrWhiteSpace (newText) ? DBNull.Value : (object)newText;
  91. } catch (Exception ex) {
  92. MessageBox.ErrorQuery (60, 20, "Failed to set text", ex.Message, "Ok");
  93. }
  94. tableView.Update ();
  95. }
  96. }
  97. class TableViewColors : TableView {
  98. protected override void RenderCell (Terminal.Gui.Attribute cellColor, string render, bool isPrimaryCell)
  99. {
  100. int unicorns = render.IndexOf ("unicorns", StringComparison.CurrentCultureIgnoreCase);
  101. int rainbows = render.IndexOf ("rainbows", StringComparison.CurrentCultureIgnoreCase);
  102. for (int i = 0; i < render.Length; i++) {
  103. if (unicorns != -1 && i >= unicorns && i <= unicorns + 8) {
  104. Driver.SetAttribute (new Attribute (Color.White, cellColor.Background));
  105. }
  106. if (rainbows != -1 && i >= rainbows && i <= rainbows + 8) {
  107. var letterOfWord = i - rainbows;
  108. switch (letterOfWord) {
  109. case 0:
  110. Driver.SetAttribute (new Attribute (Color.Red, cellColor.Background));
  111. break;
  112. case 1:
  113. Driver.SetAttribute (new Attribute (Color.BrightRed, cellColor.Background));
  114. break;
  115. case 2:
  116. Driver.SetAttribute (new Attribute (Color.BrightYellow, cellColor.Background));
  117. break;
  118. case 3:
  119. Driver.SetAttribute (new Attribute (Color.Green, cellColor.Background));
  120. break;
  121. case 4:
  122. Driver.SetAttribute (new Attribute (Color.BrightGreen, cellColor.Background));
  123. break;
  124. case 5:
  125. Driver.SetAttribute (new Attribute (Color.BrightBlue, cellColor.Background));
  126. break;
  127. case 6:
  128. Driver.SetAttribute (new Attribute (Color.BrightCyan, cellColor.Background));
  129. break;
  130. case 7:
  131. Driver.SetAttribute (new Attribute (Color.Cyan, cellColor.Background));
  132. break;
  133. }
  134. }
  135. Driver.AddRune ((Rune)render [i]);
  136. Driver.SetAttribute (cellColor);
  137. }
  138. }
  139. }
  140. }
  141. }