MultiColouredTable.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using System;
  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. // Init
  16. Application.Init ();
  17. // Setup - Create a top-level application window and configure it.
  18. Toplevel appWindow = new ()
  19. {
  20. Title = GetQuitKeyAndName ()
  21. };
  22. _tableView = new () { X = 0, Y = 1, Width = Dim.Fill (), Height = Dim.Fill (1) };
  23. var menu = new MenuBar
  24. {
  25. Menus =
  26. [
  27. new ("_File", new MenuItem [] { new ("_Quit", "", Quit) })
  28. ]
  29. };
  30. appWindow.Add (menu);
  31. var statusBar = new StatusBar (new Shortcut [] { new (Application.QuitKey, "Quit", Quit) });
  32. appWindow.Add (statusBar);
  33. appWindow.Add (_tableView);
  34. _tableView.CellActivated += EditCurrentCell;
  35. var dt = new DataTable ();
  36. dt.Columns.Add ("Col1");
  37. dt.Columns.Add ("Col2");
  38. dt.Rows.Add ("some text", "Rainbows and Unicorns are so fun!");
  39. dt.Rows.Add ("some text", "When it rains you get rainbows");
  40. dt.Rows.Add (DBNull.Value, DBNull.Value);
  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. _tableView.SetScheme (new ()
  45. {
  46. Disabled = appWindow.GetAttributeForRole (VisualRole.Disabled),
  47. HotFocus = appWindow.GetAttributeForRole (VisualRole.HotFocus),
  48. Focus = appWindow.GetAttributeForRole (VisualRole.Focus),
  49. Normal = new (Color.DarkGray, Color.Black)
  50. });
  51. _tableView.Table = new DataTableSource (_table = dt);
  52. // Run - Start the application.
  53. Application.Run (appWindow);
  54. appWindow.Dispose ();
  55. // Shutdown - Calling Application.Shutdown is required.
  56. Application.Shutdown ();
  57. }
  58. private void EditCurrentCell (object sender, CellActivatedEventArgs e)
  59. {
  60. if (e.Table == null)
  61. {
  62. return;
  63. }
  64. var oldValue = e.Table [e.Row, e.Col].ToString ();
  65. if (GetText ("Enter new value", e.Table.ColumnNames [e.Col], oldValue, out string newText))
  66. {
  67. try
  68. {
  69. _table.Rows [e.Row] [e.Col] =
  70. string.IsNullOrWhiteSpace (newText) ? DBNull.Value : newText;
  71. }
  72. catch (Exception ex)
  73. {
  74. MessageBox.ErrorQuery (60, 20, "Failed to set text", ex.Message, "Ok");
  75. }
  76. _tableView.Update ();
  77. }
  78. }
  79. private bool GetText (string title, string label, string initialText, out string enteredText)
  80. {
  81. var okPressed = false;
  82. var ok = new Button { Text = "Ok", IsDefault = true };
  83. ok.Accepting += (s, e) =>
  84. {
  85. okPressed = true;
  86. Application.RequestStop ();
  87. };
  88. var cancel = new Button { Text = "Cancel" };
  89. cancel.Accepting += (s, e) => { Application.RequestStop (); };
  90. var d = new Dialog { Title = title, Buttons = [ok, cancel] };
  91. var lbl = new Label { X = 0, Y = 1, Text = label };
  92. var tf = new TextField { Text = initialText, X = 0, Y = 2, Width = Dim.Fill () };
  93. d.Add (lbl, tf);
  94. tf.SetFocus ();
  95. Application.Run (d);
  96. d.Dispose ();
  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. SetAttribute (new (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. SetAttribute (new (Color.Red, cellColor.Background));
  120. break;
  121. case 1:
  122. SetAttribute (
  123. new (
  124. Color.BrightRed,
  125. cellColor.Background
  126. )
  127. );
  128. break;
  129. case 2:
  130. SetAttribute (
  131. new (
  132. Color.BrightYellow,
  133. cellColor.Background
  134. )
  135. );
  136. break;
  137. case 3:
  138. SetAttribute (new (Color.Green, cellColor.Background));
  139. break;
  140. case 4:
  141. SetAttribute (
  142. new (
  143. Color.BrightGreen,
  144. cellColor.Background
  145. )
  146. );
  147. break;
  148. case 5:
  149. SetAttribute (
  150. new (
  151. Color.BrightBlue,
  152. cellColor.Background
  153. )
  154. );
  155. break;
  156. case 6:
  157. SetAttribute (
  158. new (
  159. Color.BrightCyan,
  160. cellColor.Background
  161. )
  162. );
  163. break;
  164. case 7:
  165. SetAttribute (new (Color.Cyan, cellColor.Background));
  166. break;
  167. }
  168. }
  169. AddRune ((Rune)render [i]);
  170. SetAttribute (cellColor);
  171. }
  172. }
  173. }
  174. }