MultiColouredTable.cs 4.8 KB

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