MultiColouredTable.cs 7.6 KB

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