MultiColouredTable.cs 7.7 KB

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