MultiColouredTable.cs 4.7 KB

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