MultiColouredTable.cs 4.8 KB

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