2
0

CharacterMap.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #define DRAW_CONTENT
  2. //#define BASE_DRAW_CONTENT
  3. using NStack;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using Terminal.Gui;
  9. using Rune = System.Rune;
  10. namespace UICatalog.Scenarios {
  11. /// <summary>
  12. /// This Scenario demonstrates building a custom control (a class deriving from View) that:
  13. /// - Provides a "Character Map" application (like Windows' charmap.exe).
  14. /// - Helps test unicode character rendering in Terminal.Gui
  15. /// - Illustrates how to use ScrollView to do infinite scrolling
  16. /// </summary>
  17. [ScenarioMetadata (Name: "Character Map",
  18. Description: "A Unicode character set viewier built as a custom control using the ScrollView control.")]
  19. [ScenarioCategory ("Text and Formatting")]
  20. [ScenarioCategory ("Controls")]
  21. [ScenarioCategory ("ScrollView")]
  22. public class CharacterMap : Scenario {
  23. CharMap _charMap;
  24. public override void Setup ()
  25. {
  26. _charMap = new CharMap () {
  27. X = 0,
  28. Y = 0,
  29. Height = Dim.Fill (),
  30. };
  31. var radioItems = new (ustring radioLabel, int start, int end) [] {
  32. CreateRadio("ASCII Control Characters", 0x00, 0x1F),
  33. CreateRadio("C0 Control Characters", 0x80, 0x9f),
  34. CreateRadio("Hangul Jamo", 0x1100, 0x11ff), // This is where wide chars tend to start
  35. CreateRadio("Currency Symbols", 0x20A0, 0x20CF),
  36. CreateRadio("Letter-like Symbols", 0x2100, 0x214F),
  37. CreateRadio("Arrows", 0x2190, 0x21ff),
  38. CreateRadio("Mathematical symbols", 0x2200, 0x22ff),
  39. CreateRadio("Miscellaneous Technical", 0x2300, 0x23ff),
  40. CreateRadio("Box Drawing & Geometric Shapes", 0x2500, 0x25ff),
  41. CreateRadio("Miscellaneous Symbols", 0x2600, 0x26ff),
  42. CreateRadio("Dingbats", 0x2700, 0x27ff),
  43. CreateRadio("Braille", 0x2800, 0x28ff),
  44. CreateRadio("Miscellaneous Symbols & Arrows", 0x2b00, 0x2bff),
  45. CreateRadio("Alphabetic Pres. Forms", 0xFB00, 0xFb4f),
  46. CreateRadio("Cuneiform Num. and Punct.", 0x12400, 0x1240f),
  47. CreateRadio("Chess Symbols", 0x1FA00, 0x1FA0f),
  48. CreateRadio("End", CharMap.MaxCodePointVal - 16, CharMap.MaxCodePointVal),
  49. };
  50. (ustring radioLabel, int start, int end) CreateRadio (ustring title, int start, int end)
  51. {
  52. return ($"{title} (U+{start:x5}-{end:x5})", start, end);
  53. }
  54. Win.Add (_charMap);
  55. var label = new Label ("Jump To Unicode Block:") { X = Pos.Right (_charMap) + 1, Y = Pos.Y (_charMap) };
  56. Win.Add (label);
  57. var jumpList = new RadioGroup (radioItems.Select (t => t.radioLabel).ToArray ()) {
  58. X = Pos.X (label),
  59. Y = Pos.Bottom (label),
  60. Width = radioItems.Max (r => r.radioLabel.Length) + 3,
  61. SelectedItem = 8
  62. };
  63. jumpList.SelectedItemChanged += (args) => {
  64. _charMap.Start = radioItems [args.SelectedItem].start;
  65. };
  66. Win.Add (jumpList);
  67. jumpList.Refresh ();
  68. jumpList.SetFocus ();
  69. _charMap.Width = Dim.Fill () - jumpList.Width;
  70. }
  71. }
  72. class CharMap : ScrollView {
  73. /// <summary>
  74. /// Specifies the starting offset for the character map. The default is 0x2500
  75. /// which is the Box Drawing characters.
  76. /// </summary>
  77. public int Start {
  78. get => _start;
  79. set {
  80. _start = value;
  81. ContentOffset = new Point (0, _start / 16);
  82. SetNeedsDisplay ();
  83. }
  84. }
  85. int _start = 0x2500;
  86. public const int COLUMN_WIDTH = 3;
  87. public const int ROW_HEIGHT = 1;
  88. public static int MaxCodePointVal => 0x10FFFF;
  89. public static int RowLabelWidth => $"U+{MaxCodePointVal:x5}".Length;
  90. public static int RowWidth => RowLabelWidth + (COLUMN_WIDTH * 16);
  91. public CharMap ()
  92. {
  93. ColorScheme = Colors.Dialog;
  94. CanFocus = true;
  95. ContentSize = new Size (CharMap.RowWidth, MaxCodePointVal / 16);
  96. ShowVerticalScrollIndicator = true;
  97. ShowHorizontalScrollIndicator = false;
  98. LayoutComplete += (args) => {
  99. if (Bounds.Width < RowWidth) {
  100. ShowHorizontalScrollIndicator = true;
  101. } else {
  102. ShowHorizontalScrollIndicator = false;
  103. // Snap 1st column into view if it's been scrolled horizontally
  104. ContentOffset = new Point (0, ContentOffset.Y);
  105. SetNeedsDisplay ();
  106. }
  107. };
  108. DrawContent += CharMap_DrawContent;
  109. AddCommand (Command.ScrollUp, () => { ScrollUp (1); return true; });
  110. AddCommand (Command.ScrollDown, () => { ScrollDown (1); return true; });
  111. AddCommand (Command.ScrollLeft, () => { ScrollLeft (1); return true; });
  112. AddCommand (Command.ScrollRight, () => { ScrollRight (1); return true; });
  113. }
  114. private void CharMap_DrawContent (Rect viewport)
  115. {
  116. var oldClip = Driver.Clip;
  117. Driver.Clip = Frame;
  118. // Redraw doesn't know about the scroll indicators, so if off, add one to height
  119. if (!ShowHorizontalScrollIndicator) {
  120. Driver.Clip = new Rect (Frame.X, Frame.Y, Frame.Width, Frame.Height + 1);
  121. }
  122. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.Focus);
  123. Move (0, 0);
  124. Driver.AddStr (new string (' ', RowLabelWidth + 1));
  125. for (int hexDigit = 0; hexDigit < 16; hexDigit++) {
  126. var x = ContentOffset.X + RowLabelWidth + (hexDigit * COLUMN_WIDTH);
  127. if (x > RowLabelWidth - 2) {
  128. Move (x, 0);
  129. Driver.AddStr ($" {hexDigit:x} ");
  130. }
  131. }
  132. //Move (RowWidth, 0);
  133. //Driver.AddRune (' ');
  134. var firstColumnX = viewport.X + RowLabelWidth;
  135. for (int row = -ContentOffset.Y, y = 0; row <= (-ContentOffset.Y) + (Bounds.Height / ROW_HEIGHT); row++, y += ROW_HEIGHT) {
  136. int val = (row) * 16;
  137. Driver.SetAttribute (GetNormalColor ());
  138. Move (firstColumnX, y + 1);
  139. Driver.AddStr (new string (' ', 16 * COLUMN_WIDTH));
  140. if (val < MaxCodePointVal) {
  141. Driver.SetAttribute (GetNormalColor ());
  142. for (int col = 0; col < 16; col++) {
  143. var rune = new Rune ((uint)((uint)val + col));
  144. //if (rune >= 0x00D800 && rune <= 0x00DFFF) {
  145. // if (col == 0) {
  146. // Driver.AddStr ("Reserved for surrogate pairs.");
  147. // }
  148. // continue;
  149. //}
  150. Move (firstColumnX + (col * COLUMN_WIDTH) + 1, y + 1);
  151. Driver.AddRune (rune);
  152. }
  153. Move (0, y + 1);
  154. Driver.SetAttribute (HasFocus ? ColorScheme.HotFocus : ColorScheme.Focus);
  155. var rowLabel = $"U+{val / 16:x4}x ";
  156. Driver.AddStr (rowLabel);
  157. }
  158. }
  159. Driver.Clip = oldClip;
  160. }
  161. protected override void Dispose (bool disposing)
  162. {
  163. DrawContent -= CharMap_DrawContent;
  164. base.Dispose (disposing);
  165. }
  166. }
  167. }