CharacterMap.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using NStack;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Terminal.Gui;
  5. namespace UICatalog {
  6. /// <summary>
  7. /// This Scenario demonstrates building a custom control (a class deriving from View) that:
  8. /// - Provides a simple "Character Map" application (like Windows' charmap.exe).
  9. /// - Helps test unicode character rendering in Terminal.Gui
  10. /// - Illustrates how to use ScrollView to do infinite scrolling
  11. /// </summary>
  12. [ScenarioMetadata (Name: "Character Map", Description: "Illustrates a custom control and Unicode")]
  13. [ScenarioCategory ("Text")]
  14. [ScenarioCategory ("Controls")]
  15. class CharacterMap : Scenario {
  16. public override void Setup ()
  17. {
  18. var charMap = new CharMap () { X = 0, Y = 0, Width = CharMap.RowWidth + 2, Height = Dim.Fill(), Start = 0x2500,
  19. ColorScheme = Colors.Dialog};
  20. Win.Add (charMap);
  21. Button CreateBlock(Window win, ustring title, int start, int end, View align)
  22. {
  23. var button = new Button ($"{title} (U+{start:x5}-{end:x5})") {
  24. X = Pos.X (align),
  25. Y = Pos.Bottom (align),
  26. Clicked = () => {
  27. charMap.Start = start;
  28. },
  29. };
  30. win.Add (button);
  31. return button;
  32. };
  33. var label = new Label ("Unicode Blocks:") { X = Pos.Right (charMap) + 2, Y = Pos.Y (charMap) };
  34. Win.Add (label);
  35. var button = CreateBlock (Win, "Currency Symbols", 0x20A0, 0x20CF, label);
  36. button = CreateBlock (Win, "Letterlike Symbols", 0x2100, 0x214F, button);
  37. button = CreateBlock (Win, "Arrows", 0x2190, 0x21ff, button);
  38. button = CreateBlock (Win, "Mathematical symbols", 0x2200, 0x22ff, button);
  39. button = CreateBlock (Win, "Miscellaneous Technical", 0x2300, 0x23ff, button);
  40. button = CreateBlock (Win, "Box Drawing & Geometric Shapes", 0x2500, 0x25ff, button);
  41. button = CreateBlock (Win, "Miscellaneous Symbols", 0x2600, 0x26ff, button);
  42. button = CreateBlock (Win, "Dingbats", 0x2700, 0x27ff, button);
  43. button = CreateBlock (Win, "Braille", 0x2800, 0x28ff, button);
  44. button = CreateBlock (Win, "Miscellaneous Symbols and Arrows", 0x2b00, 0x2bff, button);
  45. button = CreateBlock (Win, "Alphabetic Presentation Forms", 0xFB00, 0xFb4f, button);
  46. button = CreateBlock (Win, "Cuneiform Numbers and Punctuation[1", 0x12400, 0x1240f, button);
  47. button = CreateBlock (Win, "Chess Symbols", 0x1FA00, 0x1FA0f, button);
  48. button = CreateBlock (Win, "End", CharMap.MaxCodePointVal - 16, CharMap.MaxCodePointVal, button);
  49. }
  50. }
  51. class CharMap : ScrollView {
  52. /// <summary>
  53. /// Specifies the starting offset for the character map. The default is 0x2500
  54. /// which is the Box Drawing characters.
  55. /// </summary>
  56. public int Start {
  57. get => _start;
  58. set {
  59. _start = value;
  60. ContentOffset = new Point (0, _start / 16);
  61. SetNeedsDisplay ();
  62. }
  63. }
  64. int _start = 0x2500;
  65. public static int MaxCodePointVal => 0xE0FFF;
  66. // Row Header + space + (space + char + space)
  67. public static int RowHeaderWidth => $"U+{MaxCodePointVal:x5}".Length;
  68. public static int RowWidth => RowHeaderWidth + 1 + (" c ".Length * 16);
  69. public CharMap ()
  70. {
  71. ContentSize = new Size (CharMap.RowWidth, MaxCodePointVal / 16);
  72. ShowVerticalScrollIndicator = true;
  73. ShowHorizontalScrollIndicator = false;
  74. LayoutComplete += (args) => {
  75. if (Bounds.Width <= RowWidth) {
  76. ShowHorizontalScrollIndicator = true;
  77. } else {
  78. ShowHorizontalScrollIndicator = false;
  79. }
  80. };
  81. DrawContent += CharMap_DrawContent;
  82. }
  83. #if true
  84. private void CharMap_DrawContent (Rect viewport)
  85. {
  86. for (int header = 0; header < 16; header++) {
  87. Move (viewport.X + RowHeaderWidth + 1 + (header * 3), 0);
  88. Driver.AddStr ($" {header:x} ");
  89. }
  90. for (int row = 0; row < viewport.Height - 1; row++) {
  91. int val = (-viewport.Y + row) * 16;
  92. if (val < MaxCodePointVal) {
  93. var rowLabel = $"U+{val / 16:x4}x";
  94. Move (0, row + 1);
  95. Driver.AddStr (rowLabel);
  96. for (int col = 0; col < 16; col++) {
  97. Move (viewport.X + RowHeaderWidth + 1 + (col * 3), 0 + row + 1);
  98. Driver.AddStr ($" {(char)((-viewport.Y + row) * 16 + col)} ");
  99. }
  100. }
  101. }
  102. }
  103. #else
  104. public override void OnDrawContent (Rect viewport)
  105. {
  106. CharMap_DrawContent(this, viewport);
  107. base.OnDrawContent (viewport);
  108. }
  109. #endif
  110. }
  111. }