MultiColouredTable.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 Main ()
  15. {
  16. // Init
  17. Application.Init ();
  18. // Setup - Create a top-level application window and configure it.
  19. Toplevel appWindow = new ()
  20. {
  21. Title = GetQuitKeyAndName ()
  22. };
  23. _tableView = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  24. var menu = new MenuBar
  25. {
  26. Menus =
  27. [
  28. new ("_File", new MenuItem [] { new ("_Quit", "", Quit) })
  29. ]
  30. };
  31. appWindow.Add (menu);
  32. var statusBar = new StatusBar (new Shortcut [] { new (Application.QuitKey, "Quit", Quit) });
  33. appWindow.Add (statusBar);
  34. appWindow.Add (_tableView);
  35. _tableView.CellActivated += EditCurrentCell;
  36. var dt = new DataTable ();
  37. dt.Columns.Add ("Col1");
  38. dt.Columns.Add ("Col2");
  39. dt.Rows.Add ("some text", "Rainbows and Unicorns are so fun!");
  40. dt.Rows.Add ("some text", "When it rains you get rainbows");
  41. dt.Rows.Add (DBNull.Value, DBNull.Value);
  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. _tableView.ColorScheme = new ()
  46. {
  47. Disabled = appWindow.ColorScheme.Disabled,
  48. HotFocus = appWindow.ColorScheme.HotFocus,
  49. Focus = appWindow.ColorScheme.Focus,
  50. Normal = new (Color.DarkGray, Color.Black)
  51. };
  52. _tableView.Table = new DataTableSource (_table = dt);
  53. // Run - Start the application.
  54. Application.Run (appWindow);
  55. appWindow.Dispose ();
  56. // Shutdown - Calling Application.Shutdown is required.
  57. Application.Shutdown ();
  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.Accepting += (s, e) =>
  85. {
  86. okPressed = true;
  87. Application.RequestStop ();
  88. };
  89. var cancel = new Button { Text = "Cancel" };
  90. cancel.Accepting += (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. d.Dispose ();
  98. enteredText = okPressed ? tf.Text : null;
  99. return okPressed;
  100. }
  101. private void Quit () { Application.RequestStop (); }
  102. private class TableViewColors : TableView
  103. {
  104. protected override void RenderCell (Attribute cellColor, string render, bool isPrimaryCell)
  105. {
  106. int unicorns = render.IndexOf ("unicorns", StringComparison.CurrentCultureIgnoreCase);
  107. int rainbows = render.IndexOf ("rainbows", StringComparison.CurrentCultureIgnoreCase);
  108. for (var i = 0; i < render.Length; i++)
  109. {
  110. if (unicorns != -1 && i >= unicorns && i <= unicorns + 8)
  111. {
  112. Driver.SetAttribute (new (Color.White, cellColor.Background));
  113. }
  114. if (rainbows != -1 && i >= rainbows && i <= rainbows + 8)
  115. {
  116. int letterOfWord = i - rainbows;
  117. switch (letterOfWord)
  118. {
  119. case 0:
  120. Driver.SetAttribute (new (Color.Red, cellColor.Background));
  121. break;
  122. case 1:
  123. Driver.SetAttribute (
  124. new (
  125. Color.BrightRed,
  126. cellColor.Background
  127. )
  128. );
  129. break;
  130. case 2:
  131. Driver.SetAttribute (
  132. new (
  133. Color.BrightYellow,
  134. cellColor.Background
  135. )
  136. );
  137. break;
  138. case 3:
  139. Driver.SetAttribute (new (Color.Green, cellColor.Background));
  140. break;
  141. case 4:
  142. Driver.SetAttribute (
  143. new (
  144. Color.BrightGreen,
  145. cellColor.Background
  146. )
  147. );
  148. break;
  149. case 5:
  150. Driver.SetAttribute (
  151. new (
  152. Color.BrightBlue,
  153. cellColor.Background
  154. )
  155. );
  156. break;
  157. case 6:
  158. Driver.SetAttribute (
  159. new (
  160. Color.BrightCyan,
  161. cellColor.Background
  162. )
  163. );
  164. break;
  165. case 7:
  166. Driver.SetAttribute (new (Color.Cyan, cellColor.Background));
  167. break;
  168. }
  169. }
  170. Driver.AddRune ((Rune)render [i]);
  171. Driver.SetAttribute (cellColor);
  172. }
  173. }
  174. }
  175. }