DynamicMenuBar.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. using NStack;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. using System.Text;
  9. using Terminal.Gui;
  10. namespace UICatalog.Scenarios {
  11. [ScenarioMetadata (Name: "Dynamic MenuBar", Description: "Demonstrates how to change a MenuBar dynamically.")]
  12. [ScenarioCategory ("Top Level Windows")]
  13. [ScenarioCategory ("Menus")]
  14. public class DynamicMenuBar : Scenario {
  15. public override void Init (ColorScheme colorScheme)
  16. {
  17. Application.Init ();
  18. Application.Run (new DynamicMenuBarSample ($"CTRL-Q to Close - Scenario: {GetName ()}"));
  19. Application.Shutdown ();
  20. }
  21. public override void Run ()
  22. {
  23. }
  24. public class DynamicMenuItemList {
  25. public ustring Title { get; set; }
  26. public MenuItem MenuItem { get; set; }
  27. public DynamicMenuItemList () { }
  28. public DynamicMenuItemList (ustring title, MenuItem menuItem)
  29. {
  30. Title = title;
  31. MenuItem = menuItem;
  32. }
  33. public override string ToString () => $"{Title}, {MenuItem}";
  34. }
  35. public class DynamicMenuItem {
  36. public ustring title = "_New";
  37. public ustring help = "";
  38. public ustring action = "";
  39. public bool isTopLevel;
  40. public bool hasSubMenu;
  41. public MenuItemCheckStyle checkStyle;
  42. public ustring shortcut;
  43. public DynamicMenuItem () { }
  44. public DynamicMenuItem (ustring title, bool hasSubMenu = false)
  45. {
  46. this.title = title;
  47. this.hasSubMenu = hasSubMenu;
  48. }
  49. public DynamicMenuItem (ustring title, ustring help, ustring action, bool isTopLevel, bool hasSubMenu, MenuItemCheckStyle checkStyle = MenuItemCheckStyle.NoCheck, ustring shortcut = null)
  50. {
  51. this.title = title;
  52. this.help = help;
  53. this.action = action;
  54. this.isTopLevel = isTopLevel;
  55. this.hasSubMenu = hasSubMenu;
  56. this.checkStyle = checkStyle;
  57. this.shortcut = shortcut;
  58. }
  59. }
  60. public class DynamicMenuBarSample : Window {
  61. MenuBar _menuBar;
  62. MenuItem _currentMenuBarItem;
  63. int _currentSelectedMenuBar;
  64. MenuItem _currentEditMenuBarItem;
  65. ListView _lstMenus;
  66. public DynamicMenuItemModel DataContext { get; set; }
  67. public DynamicMenuBarSample (ustring title) : base (title)
  68. {
  69. DataContext = new DynamicMenuItemModel ();
  70. var _frmDelimiter = new FrameView ("Shortcut Delimiter:") {
  71. X = Pos.Center (),
  72. Y = 3,
  73. Width = 25,
  74. Height = 4
  75. };
  76. var _txtDelimiter = new TextField (MenuBar.ShortcutDelimiter.ToString ()) {
  77. X = Pos.Center (),
  78. Width = 2,
  79. };
  80. _txtDelimiter.TextChanged += (_) => MenuBar.ShortcutDelimiter = _txtDelimiter.Text;
  81. _frmDelimiter.Add (_txtDelimiter);
  82. Add (_frmDelimiter);
  83. var _frmMenu = new FrameView ("Menus:") {
  84. Y = 7,
  85. Width = Dim.Percent (50),
  86. Height = Dim.Fill ()
  87. };
  88. var _btnAddMenuBar = new Button ("Add a MenuBar") {
  89. Y = 1,
  90. };
  91. _frmMenu.Add (_btnAddMenuBar);
  92. var _btnMenuBarUp = new Button ("^") {
  93. X = Pos.Center ()
  94. };
  95. _frmMenu.Add (_btnMenuBarUp);
  96. var _btnMenuBarDown = new Button ("v") {
  97. X = Pos.Center (),
  98. Y = Pos.Bottom (_btnMenuBarUp)
  99. };
  100. _frmMenu.Add (_btnMenuBarDown);
  101. var _btnRemoveMenuBar = new Button ("Remove a MenuBar") {
  102. Y = 1
  103. };
  104. _btnRemoveMenuBar.X = Pos.AnchorEnd () - (Pos.Right (_btnRemoveMenuBar) - Pos.Left (_btnRemoveMenuBar));
  105. _frmMenu.Add (_btnRemoveMenuBar);
  106. var _btnPrevious = new Button ("<") {
  107. X = Pos.Left (_btnAddMenuBar),
  108. Y = Pos.Top (_btnAddMenuBar) + 2
  109. };
  110. _frmMenu.Add (_btnPrevious);
  111. var _btnAdd = new Button (" Add ") {
  112. Y = Pos.Top (_btnPrevious) + 2,
  113. };
  114. _btnAdd.X = Pos.AnchorEnd () - (Pos.Right (_btnAdd) - Pos.Left (_btnAdd));
  115. _frmMenu.Add (_btnAdd);
  116. var _btnNext = new Button (">") {
  117. X = Pos.X (_btnAdd),
  118. Y = Pos.Top (_btnPrevious),
  119. };
  120. _frmMenu.Add (_btnNext);
  121. var _lblMenuBar = new Label () {
  122. ColorScheme = Colors.Dialog,
  123. TextAlignment = TextAlignment.Centered,
  124. X = Pos.Right (_btnPrevious) + 1,
  125. Y = Pos.Top (_btnPrevious),
  126. Width = Dim.Fill () - Dim.Function (() => _btnAdd.Frame.Width + 1),
  127. Height = 1
  128. };
  129. _frmMenu.Add (_lblMenuBar);
  130. _lblMenuBar.WantMousePositionReports = true;
  131. _lblMenuBar.CanFocus = true;
  132. var _lblParent = new Label () {
  133. TextAlignment = TextAlignment.Centered,
  134. X = Pos.Right (_btnPrevious) + 1,
  135. Y = Pos.Top (_btnPrevious) + 1,
  136. Width = Dim.Fill () - Dim.Width (_btnAdd) - 1
  137. };
  138. _frmMenu.Add (_lblParent);
  139. var _btnPreviowsParent = new Button ("..") {
  140. X = Pos.Left (_btnAddMenuBar),
  141. Y = Pos.Top (_btnPrevious) + 1
  142. };
  143. _frmMenu.Add (_btnPreviowsParent);
  144. _lstMenus = new ListView (new List<DynamicMenuItemList> ()) {
  145. ColorScheme = Colors.Dialog,
  146. X = Pos.Right (_btnPrevious) + 1,
  147. Y = Pos.Top (_btnPrevious) + 2,
  148. Width = _lblMenuBar.Width,
  149. Height = Dim.Fill (),
  150. };
  151. _frmMenu.Add (_lstMenus);
  152. _lblMenuBar.TabIndex = _btnPrevious.TabIndex + 1;
  153. _lstMenus.TabIndex = _lblMenuBar.TabIndex + 1;
  154. _btnNext.TabIndex = _lstMenus.TabIndex + 1;
  155. _btnAdd.TabIndex = _btnNext.TabIndex + 1;
  156. var _btnRemove = new Button ("Remove") {
  157. X = Pos.Left (_btnAdd),
  158. Y = Pos.Top (_btnAdd) + 1
  159. };
  160. _frmMenu.Add (_btnRemove);
  161. var _btnUp = new Button ("^") {
  162. X = Pos.Right (_lstMenus) + 2,
  163. Y = Pos.Top (_btnRemove) + 2
  164. };
  165. _frmMenu.Add (_btnUp);
  166. var _btnDown = new Button ("v") {
  167. X = Pos.Right (_lstMenus) + 2,
  168. Y = Pos.Top (_btnUp) + 1
  169. };
  170. _frmMenu.Add (_btnDown);
  171. Add (_frmMenu);
  172. var _frmMenuDetails = new DynamicMenuBarDetails ("Menu Details:") {
  173. X = Pos.Right (_frmMenu),
  174. Y = Pos.Top (_frmMenu),
  175. Width = Dim.Fill (),
  176. Height = Dim.Fill (2)
  177. };
  178. Add (_frmMenuDetails);
  179. _btnMenuBarUp.Clicked += () => {
  180. var i = _currentSelectedMenuBar;
  181. var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
  182. if (menuItem != null) {
  183. var menus = _menuBar.Menus;
  184. if (i > 0) {
  185. menus [i] = menus [i - 1];
  186. menus [i - 1] = menuItem;
  187. _currentSelectedMenuBar = i - 1;
  188. _menuBar.SetNeedsDisplay ();
  189. }
  190. }
  191. };
  192. _btnMenuBarDown.Clicked += () => {
  193. var i = _currentSelectedMenuBar;
  194. var menuItem = _menuBar != null && _menuBar.Menus.Length > 0 ? _menuBar.Menus [i] : null;
  195. if (menuItem != null) {
  196. var menus = _menuBar.Menus;
  197. if (i < menus.Length - 1) {
  198. menus [i] = menus [i + 1];
  199. menus [i + 1] = menuItem;
  200. _currentSelectedMenuBar = i + 1;
  201. _menuBar.SetNeedsDisplay ();
  202. }
  203. }
  204. };
  205. _btnUp.Clicked += () => {
  206. var i = _lstMenus.SelectedItem;
  207. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
  208. if (menuItem != null) {
  209. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  210. if (i > 0) {
  211. childrens [i] = childrens [i - 1];
  212. childrens [i - 1] = menuItem;
  213. DataContext.Menus [i] = DataContext.Menus [i - 1];
  214. DataContext.Menus [i - 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
  215. _lstMenus.SelectedItem = i - 1;
  216. }
  217. }
  218. };
  219. _btnDown.Clicked += () => {
  220. var i = _lstMenus.SelectedItem;
  221. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [i].MenuItem : null;
  222. if (menuItem != null) {
  223. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  224. if (i < childrens.Length - 1) {
  225. childrens [i] = childrens [i + 1];
  226. childrens [i + 1] = menuItem;
  227. DataContext.Menus [i] = DataContext.Menus [i + 1];
  228. DataContext.Menus [i + 1] = new DynamicMenuItemList (menuItem.Title, menuItem);
  229. _lstMenus.SelectedItem = i + 1;
  230. }
  231. }
  232. };
  233. _btnPreviowsParent.Clicked += () => {
  234. if (_currentMenuBarItem != null && _currentMenuBarItem.Parent != null) {
  235. var mi = _currentMenuBarItem;
  236. _currentMenuBarItem = _currentMenuBarItem.Parent as MenuBarItem;
  237. SetListViewSource (_currentMenuBarItem, true);
  238. var i = ((MenuBarItem)_currentMenuBarItem).GetChildrenIndex (mi);
  239. if (i > -1) {
  240. _lstMenus.SelectedItem = i;
  241. }
  242. if (_currentMenuBarItem.Parent != null) {
  243. DataContext.Parent = _currentMenuBarItem.Title;
  244. } else {
  245. DataContext.Parent = ustring.Empty;
  246. }
  247. } else {
  248. DataContext.Parent = ustring.Empty;
  249. }
  250. };
  251. var _btnOk = new Button ("Ok") {
  252. X = Pos.Right (_frmMenu) + 20,
  253. Y = Pos.Bottom (_frmMenuDetails),
  254. };
  255. Add (_btnOk);
  256. var _btnCancel = new Button ("Cancel") {
  257. X = Pos.Right (_btnOk) + 3,
  258. Y = Pos.Top (_btnOk),
  259. };
  260. _btnCancel.Clicked += () => {
  261. SetFrameDetails (_currentEditMenuBarItem);
  262. };
  263. Add (_btnCancel);
  264. _lstMenus.SelectedItemChanged += (e) => {
  265. SetFrameDetails ();
  266. };
  267. _btnOk.Clicked += () => {
  268. if (ustring.IsNullOrEmpty (_frmMenuDetails._txtTitle.Text) && _currentEditMenuBarItem != null) {
  269. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  270. } else if (_currentEditMenuBarItem != null) {
  271. var menuItem = new DynamicMenuItem (_frmMenuDetails._txtTitle.Text, _frmMenuDetails._txtHelp.Text,
  272. _frmMenuDetails._txtAction.Text,
  273. _frmMenuDetails._ckbIsTopLevel != null ? _frmMenuDetails._ckbIsTopLevel.Checked : false,
  274. _frmMenuDetails._ckbSubMenu != null ? _frmMenuDetails._ckbSubMenu.Checked : false,
  275. _frmMenuDetails._rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  276. _frmMenuDetails._rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked :
  277. MenuItemCheckStyle.Radio,
  278. _frmMenuDetails._txtShortcut.Text);
  279. UpdateMenuItem (_currentEditMenuBarItem, menuItem, _lstMenus.SelectedItem);
  280. }
  281. };
  282. _btnAdd.Clicked += () => {
  283. if (MenuBar == null) {
  284. MessageBox.ErrorQuery ("Menu Bar Error", "Must add a MenuBar first!", "Ok");
  285. _btnAddMenuBar.SetFocus ();
  286. return;
  287. }
  288. var frameDetails = new DynamicMenuBarDetails (null, _currentMenuBarItem != null);
  289. var item = frameDetails.EnterMenuItem ();
  290. if (item == null) {
  291. return;
  292. }
  293. if (!(_currentMenuBarItem is MenuBarItem)) {
  294. var parent = _currentMenuBarItem.Parent as MenuBarItem;
  295. var idx = parent.GetChildrenIndex (_currentMenuBarItem);
  296. _currentMenuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { }, _currentMenuBarItem.Parent);
  297. _currentMenuBarItem.CheckType = item.checkStyle;
  298. parent.Children [idx] = _currentMenuBarItem;
  299. } else {
  300. MenuItem newMenu = CreateNewMenu (item, _currentMenuBarItem);
  301. var menuBarItem = _currentMenuBarItem as MenuBarItem;
  302. if (menuBarItem == null) {
  303. menuBarItem = new MenuBarItem (_currentMenuBarItem.Title, new MenuItem [] { newMenu }, _currentMenuBarItem.Parent);
  304. } else if (menuBarItem.Children == null) {
  305. menuBarItem.Children = new MenuItem [] { newMenu };
  306. } else {
  307. var childrens = menuBarItem.Children;
  308. Array.Resize (ref childrens, childrens.Length + 1);
  309. childrens [childrens.Length - 1] = newMenu;
  310. menuBarItem.Children = childrens;
  311. }
  312. DataContext.Menus.Add (new DynamicMenuItemList (newMenu.Title, newMenu));
  313. _lstMenus.MoveDown ();
  314. }
  315. };
  316. _btnRemove.Clicked += () => {
  317. var menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  318. if (menuItem != null) {
  319. var childrens = ((MenuBarItem)_currentMenuBarItem).Children;
  320. childrens [_lstMenus.SelectedItem] = null;
  321. int i = 0;
  322. foreach (var c in childrens) {
  323. if (c != null) {
  324. childrens [i] = c;
  325. i++;
  326. }
  327. }
  328. Array.Resize (ref childrens, childrens.Length - 1);
  329. if (childrens.Length == 0) {
  330. if (_currentMenuBarItem.Parent == null) {
  331. ((MenuBarItem)_currentMenuBarItem).Children = null;
  332. //_currentMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentMenuBarItem.Title));
  333. } else {
  334. _currentMenuBarItem = new MenuItem (_currentMenuBarItem.Title, _currentMenuBarItem.Help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, new DynamicMenuItem (_currentEditMenuBarItem.Title)), null, _currentMenuBarItem.Parent);
  335. }
  336. } else {
  337. ((MenuBarItem)_currentMenuBarItem).Children = childrens;
  338. }
  339. DataContext.Menus.RemoveAt (_lstMenus.SelectedItem);
  340. if (_lstMenus.Source.Count > 0 && _lstMenus.SelectedItem > _lstMenus.Source.Count - 1) {
  341. _lstMenus.SelectedItem = _lstMenus.Source.Count - 1;
  342. }
  343. _lstMenus.SetNeedsDisplay ();
  344. SetFrameDetails ();
  345. }
  346. };
  347. _lstMenus.OpenSelectedItem += (e) => {
  348. _currentMenuBarItem = DataContext.Menus [e.Item].MenuItem;
  349. if (!(_currentMenuBarItem is MenuBarItem)) {
  350. MessageBox.ErrorQuery ("Menu Open Error", "Must allows sub menus first!", "Ok");
  351. return;
  352. }
  353. DataContext.Parent = _currentMenuBarItem.Title;
  354. DataContext.Menus = new List<DynamicMenuItemList> ();
  355. SetListViewSource (_currentMenuBarItem, true);
  356. var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [0].MenuItem : null;
  357. SetFrameDetails (menuBarItem);
  358. };
  359. _lstMenus.Enter += (_) => {
  360. var menuBarItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  361. SetFrameDetails (menuBarItem);
  362. };
  363. _btnNext.Clicked += () => {
  364. if (_menuBar != null && _currentSelectedMenuBar + 1 < _menuBar.Menus.Length) {
  365. _currentSelectedMenuBar++;
  366. }
  367. SelectCurrentMenuBarItem ();
  368. };
  369. _btnPrevious.Clicked += () => {
  370. if (_currentSelectedMenuBar - 1 > -1) {
  371. _currentSelectedMenuBar--;
  372. }
  373. SelectCurrentMenuBarItem ();
  374. };
  375. _lblMenuBar.Enter += (e) => {
  376. if (_menuBar?.Menus != null) {
  377. _currentMenuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
  378. SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
  379. }
  380. };
  381. _btnAddMenuBar.Clicked += () => {
  382. var frameDetails = new DynamicMenuBarDetails (null, false);
  383. var item = frameDetails.EnterMenuItem ();
  384. if (item == null) {
  385. return;
  386. }
  387. if (MenuBar == null) {
  388. _menuBar = new MenuBar ();
  389. Add (_menuBar);
  390. }
  391. var newMenu = CreateNewMenu (item) as MenuBarItem;
  392. var menus = _menuBar.Menus;
  393. Array.Resize (ref menus, menus.Length + 1);
  394. menus [^1] = newMenu;
  395. _menuBar.Menus = menus;
  396. _currentMenuBarItem = newMenu;
  397. _currentMenuBarItem.CheckType = item.checkStyle;
  398. _currentSelectedMenuBar = menus.Length - 1;
  399. _menuBar.Menus [_currentSelectedMenuBar] = newMenu;
  400. _lblMenuBar.Text = newMenu.Title;
  401. SetListViewSource (_currentMenuBarItem, true);
  402. SetFrameDetails (_menuBar.Menus [_currentSelectedMenuBar]);
  403. _menuBar.SetNeedsDisplay ();
  404. };
  405. _btnRemoveMenuBar.Clicked += () => {
  406. if (_menuBar == null || _menuBar.Menus.Length == 0) {
  407. return;
  408. }
  409. if (_menuBar != null && _menuBar.Menus.Length > 0) {
  410. _menuBar.Menus [_currentSelectedMenuBar] = null;
  411. int i = 0;
  412. foreach (var m in _menuBar.Menus) {
  413. if (m != null) {
  414. _menuBar.Menus [i] = m;
  415. i++;
  416. }
  417. }
  418. var menus = _menuBar.Menus;
  419. Array.Resize (ref menus, menus.Length - 1);
  420. _menuBar.Menus = menus;
  421. if (_currentSelectedMenuBar - 1 >= 0 && _menuBar.Menus.Length > 0) {
  422. _currentSelectedMenuBar--;
  423. }
  424. _currentMenuBarItem = _menuBar.Menus?.Length > 0 ? _menuBar.Menus [_currentSelectedMenuBar] : null;
  425. }
  426. if (MenuBar != null && _currentMenuBarItem == null && _menuBar.Menus.Length == 0) {
  427. Remove (_menuBar);
  428. _menuBar = null;
  429. DataContext.Menus = new List<DynamicMenuItemList> ();
  430. _currentMenuBarItem = null;
  431. _currentSelectedMenuBar = -1;
  432. _lblMenuBar.Text = ustring.Empty;
  433. } else {
  434. _lblMenuBar.Text = _menuBar.Menus [_currentSelectedMenuBar].Title;
  435. }
  436. SetListViewSource (_currentMenuBarItem, true);
  437. SetFrameDetails (null);
  438. };
  439. SetFrameDetails ();
  440. var ustringConverter = new UStringValueConverter ();
  441. var listWrapperConverter = new ListWrapperConverter ();
  442. var lblMenuBar = new Binding (this, "MenuBar", _lblMenuBar, "Text", ustringConverter);
  443. var lblParent = new Binding (this, "Parent", _lblParent, "Text", ustringConverter);
  444. var lstMenus = new Binding (this, "Menus", _lstMenus, "Source", listWrapperConverter);
  445. void SetFrameDetails (MenuItem menuBarItem = null)
  446. {
  447. MenuItem menuItem;
  448. if (menuBarItem == null) {
  449. menuItem = DataContext.Menus.Count > 0 ? DataContext.Menus [_lstMenus.SelectedItem].MenuItem : null;
  450. } else {
  451. menuItem = menuBarItem;
  452. }
  453. _currentEditMenuBarItem = menuItem;
  454. _frmMenuDetails.EditMenuBarItem (menuItem);
  455. var f = _btnOk.Enabled == _frmMenuDetails.Enabled;
  456. if (!f) {
  457. _btnOk.Enabled = _frmMenuDetails.Enabled;
  458. _btnCancel.Enabled = _frmMenuDetails.Enabled;
  459. }
  460. }
  461. void SelectCurrentMenuBarItem ()
  462. {
  463. MenuBarItem menuBarItem = null;
  464. if (_menuBar?.Menus != null) {
  465. menuBarItem = _menuBar.Menus [_currentSelectedMenuBar];
  466. _lblMenuBar.Text = menuBarItem.Title;
  467. }
  468. SetFrameDetails (menuBarItem);
  469. _currentMenuBarItem = menuBarItem;
  470. DataContext.Menus = new List<DynamicMenuItemList> ();
  471. SetListViewSource (_currentMenuBarItem, true);
  472. _lblParent.Text = ustring.Empty;
  473. }
  474. void SetListViewSource (MenuItem _currentMenuBarItem, bool fill = false)
  475. {
  476. DataContext.Menus = new List<DynamicMenuItemList> ();
  477. var menuBarItem = _currentMenuBarItem as MenuBarItem;
  478. if (menuBarItem != null && menuBarItem?.Children == null) {
  479. return;
  480. }
  481. if (!fill) {
  482. return;
  483. }
  484. if (menuBarItem != null) {
  485. foreach (var child in menuBarItem?.Children) {
  486. var m = new DynamicMenuItemList (child.Title, child);
  487. DataContext.Menus.Add (m);
  488. }
  489. }
  490. }
  491. MenuItem CreateNewMenu (DynamicMenuItem item, MenuItem parent = null)
  492. {
  493. MenuItem newMenu;
  494. if (item.hasSubMenu) {
  495. newMenu = new MenuBarItem (item.title, new MenuItem [] { }, parent);
  496. } else if (parent != null) {
  497. newMenu = new MenuItem (item.title, item.help, null, null, parent);
  498. newMenu.CheckType = item.checkStyle;
  499. newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
  500. newMenu.Shortcut = ShortcutHelper.GetShortcutFromTag (item.shortcut);
  501. } else if (item.isTopLevel) {
  502. newMenu = new MenuBarItem (item.title, item.help, null);
  503. newMenu.Action = _frmMenuDetails.CreateAction (newMenu, item);
  504. } else {
  505. newMenu = new MenuBarItem (item.title, item.help, null);
  506. ((MenuBarItem)newMenu).Children [0].Action = _frmMenuDetails.CreateAction (newMenu, item);
  507. ((MenuBarItem)newMenu).Children [0].Shortcut = ShortcutHelper.GetShortcutFromTag (item.shortcut);
  508. }
  509. return newMenu;
  510. }
  511. void UpdateMenuItem (MenuItem _currentEditMenuBarItem, DynamicMenuItem menuItem, int index)
  512. {
  513. _currentEditMenuBarItem.Title = menuItem.title;
  514. _currentEditMenuBarItem.Help = menuItem.help;
  515. _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
  516. var parent = _currentEditMenuBarItem.Parent as MenuBarItem;
  517. if (parent != null && parent.Children.Length == 1 && _currentEditMenuBarItem.CheckType == MenuItemCheckStyle.Radio) {
  518. _currentEditMenuBarItem.Checked = true;
  519. }
  520. if (menuItem.isTopLevel && _currentEditMenuBarItem is MenuBarItem) {
  521. ((MenuBarItem)_currentEditMenuBarItem).Children = null;
  522. _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
  523. SetListViewSource (_currentEditMenuBarItem, true);
  524. } else if (menuItem.hasSubMenu) {
  525. _currentEditMenuBarItem.Action = null;
  526. if (_currentEditMenuBarItem is MenuBarItem && ((MenuBarItem)_currentEditMenuBarItem).Children == null) {
  527. ((MenuBarItem)_currentEditMenuBarItem).Children = new MenuItem [] { };
  528. } else if (_currentEditMenuBarItem.Parent != null) {
  529. _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
  530. } else {
  531. _currentEditMenuBarItem = new MenuBarItem (_currentEditMenuBarItem.Title, new MenuItem [] { }, _currentEditMenuBarItem.Parent);
  532. }
  533. SetListViewSource (_currentEditMenuBarItem, true);
  534. } else if (_currentEditMenuBarItem is MenuBarItem && _currentEditMenuBarItem.Parent != null) {
  535. _frmMenuDetails.UpdateParent (ref _currentEditMenuBarItem);
  536. _currentEditMenuBarItem = new MenuItem (menuItem.title, menuItem.help, _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem), null, _currentEditMenuBarItem.Parent);
  537. } else {
  538. if (_currentEditMenuBarItem is MenuBarItem) {
  539. ((MenuBarItem)_currentEditMenuBarItem).Children = null;
  540. DataContext.Menus = new List<DynamicMenuItemList> ();
  541. }
  542. _currentEditMenuBarItem.Action = _frmMenuDetails.CreateAction (_currentEditMenuBarItem, menuItem);
  543. _currentEditMenuBarItem.Shortcut = ShortcutHelper.GetShortcutFromTag (menuItem.shortcut);
  544. }
  545. if (_currentEditMenuBarItem.Parent == null) {
  546. DataContext.MenuBar = _currentEditMenuBarItem.Title;
  547. } else {
  548. if (DataContext.Menus.Count == 0) {
  549. DataContext.Menus.Add (new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem));
  550. }
  551. DataContext.Menus [index] = new DynamicMenuItemList (_currentEditMenuBarItem.Title, _currentEditMenuBarItem);
  552. }
  553. _currentEditMenuBarItem.CheckType = menuItem.checkStyle;
  554. SetFrameDetails (_currentEditMenuBarItem);
  555. }
  556. //_frmMenuDetails.Initialized += (s, e) => _frmMenuDetails.Enabled = false;
  557. }
  558. }
  559. public class DynamicMenuBarDetails : FrameView {
  560. public MenuItem _menuItem;
  561. public TextField _txtTitle;
  562. public TextField _txtHelp;
  563. public TextView _txtAction;
  564. public CheckBox _ckbIsTopLevel;
  565. public CheckBox _ckbSubMenu;
  566. public RadioGroup _rbChkStyle;
  567. public TextField _txtShortcut;
  568. bool hasParent;
  569. public DynamicMenuBarDetails (MenuItem menuItem = null, bool hasParent = false) : this (menuItem == null ? "Adding New Menu." : "Editing Menu.")
  570. {
  571. _menuItem = menuItem;
  572. this.hasParent = hasParent;
  573. }
  574. public DynamicMenuBarDetails (ustring title) : base (title)
  575. {
  576. var _lblTitle = new Label ("Title:") {
  577. Y = 1
  578. };
  579. Add (_lblTitle);
  580. _txtTitle = new TextField () {
  581. X = Pos.Right (_lblTitle) + 2,
  582. Y = Pos.Top (_lblTitle),
  583. Width = Dim.Fill ()
  584. };
  585. Add (_txtTitle);
  586. var _lblHelp = new Label ("Help:") {
  587. X = Pos.Left (_lblTitle),
  588. Y = Pos.Bottom (_lblTitle) + 1
  589. };
  590. Add (_lblHelp);
  591. _txtHelp = new TextField () {
  592. X = Pos.Left (_txtTitle),
  593. Y = Pos.Top (_lblHelp),
  594. Width = Dim.Fill ()
  595. };
  596. Add (_txtHelp);
  597. var _lblAction = new Label ("Action:") {
  598. X = Pos.Left (_lblTitle),
  599. Y = Pos.Bottom (_lblHelp) + 1
  600. };
  601. Add (_lblAction);
  602. _txtAction = new TextView () {
  603. X = Pos.Left (_txtTitle),
  604. Y = Pos.Top (_lblAction),
  605. Width = Dim.Fill (),
  606. Height = 5
  607. };
  608. Add (_txtAction);
  609. _ckbIsTopLevel = new CheckBox ("IsTopLevel") {
  610. X = Pos.Left (_lblTitle),
  611. Y = Pos.Bottom (_lblAction) + 5
  612. };
  613. Add (_ckbIsTopLevel);
  614. _ckbSubMenu = new CheckBox ("Has sub-menus") {
  615. X = Pos.Left (_lblTitle),
  616. Y = Pos.Bottom (_ckbIsTopLevel),
  617. Checked = _menuItem == null ? !hasParent : HasSubMenus (_menuItem)
  618. };
  619. Add (_ckbSubMenu);
  620. var _rChkLabels = new ustring [] { "NoCheck", "Checked", "Radio" };
  621. _rbChkStyle = new RadioGroup (_rChkLabels) {
  622. X = Pos.Left (_lblTitle),
  623. Y = Pos.Bottom (_ckbSubMenu) + 1,
  624. };
  625. Add (_rbChkStyle);
  626. var _lblShortcut = new Label ("Shortcut:") {
  627. X = Pos.Right (_ckbSubMenu) + 10,
  628. Y = Pos.Top (_ckbSubMenu)
  629. };
  630. Add (_lblShortcut);
  631. _txtShortcut = new TextField () {
  632. X = Pos.X (_lblShortcut),
  633. Y = Pos.Bottom (_lblShortcut),
  634. Width = Dim.Fill (),
  635. ReadOnly = true
  636. };
  637. _txtShortcut.KeyDown += (e) => {
  638. if (!ProcessKey (e.KeyEvent)) {
  639. return;
  640. }
  641. var k = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  642. if (CheckShortcut (k, true)) {
  643. e.Handled = true;
  644. }
  645. };
  646. bool ProcessKey (KeyEvent ev)
  647. {
  648. switch (ev.Key) {
  649. case Key.CursorUp:
  650. case Key.CursorDown:
  651. case Key.Tab:
  652. case Key.BackTab:
  653. return false;
  654. }
  655. return true;
  656. }
  657. bool CheckShortcut (Key k, bool pre)
  658. {
  659. var m = _menuItem != null ? _menuItem : new MenuItem ();
  660. if (pre && !ShortcutHelper.PreShortcutValidation (k)) {
  661. _txtShortcut.Text = "";
  662. return false;
  663. }
  664. if (!pre) {
  665. if (!ShortcutHelper.PostShortcutValidation (ShortcutHelper.GetShortcutFromTag (_txtShortcut.Text))) {
  666. _txtShortcut.Text = "";
  667. return false;
  668. }
  669. return true;
  670. }
  671. _txtShortcut.Text = ShortcutHelper.GetShortcutTag (k);
  672. return true;
  673. }
  674. _txtShortcut.KeyUp += (e) => {
  675. var k = ShortcutHelper.GetModifiersKey (e.KeyEvent);
  676. if (CheckShortcut (k, false)) {
  677. e.Handled = true;
  678. }
  679. };
  680. Add (_txtShortcut);
  681. var _btnShortcut = new Button ("Clear Shortcut") {
  682. X = Pos.X (_lblShortcut),
  683. Y = Pos.Bottom (_txtShortcut) + 1
  684. };
  685. _btnShortcut.Clicked += () => {
  686. _txtShortcut.Text = "";
  687. };
  688. Add (_btnShortcut);
  689. _ckbIsTopLevel.Toggled += (e) => {
  690. if ((_menuItem != null && _menuItem.Parent != null && _ckbIsTopLevel.Checked) ||
  691. _menuItem == null && hasParent && _ckbIsTopLevel.Checked) {
  692. MessageBox.ErrorQuery ("Invalid IsTopLevel", "Only menu bar can have top level menu item!", "Ok");
  693. _ckbIsTopLevel.Checked = false;
  694. return;
  695. }
  696. if (_ckbIsTopLevel.Checked) {
  697. _ckbSubMenu.Checked = false;
  698. _ckbSubMenu.SetNeedsDisplay ();
  699. _txtHelp.Enabled = true;
  700. _txtAction.Enabled = true;
  701. _txtShortcut.Enabled = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  702. } else {
  703. if (_menuItem == null && !hasParent || _menuItem.Parent == null) {
  704. _ckbSubMenu.Checked = true;
  705. _ckbSubMenu.SetNeedsDisplay ();
  706. _txtShortcut.Enabled = false;
  707. }
  708. _txtHelp.Text = "";
  709. _txtHelp.Enabled = false;
  710. _txtAction.Text = "";
  711. _txtAction.Enabled = false;
  712. }
  713. };
  714. _ckbSubMenu.Toggled += (e) => {
  715. if (_ckbSubMenu.Checked) {
  716. _ckbIsTopLevel.Checked = false;
  717. _ckbIsTopLevel.SetNeedsDisplay ();
  718. _txtHelp.Text = "";
  719. _txtHelp.Enabled = false;
  720. _txtAction.Text = "";
  721. _txtAction.Enabled = false;
  722. _txtShortcut.Text = "";
  723. _txtShortcut.Enabled = false;
  724. } else {
  725. if (!hasParent) {
  726. _ckbIsTopLevel.Checked = true;
  727. _ckbIsTopLevel.SetNeedsDisplay ();
  728. _txtShortcut.Enabled = false;
  729. }
  730. _txtHelp.Enabled = true;
  731. _txtAction.Enabled = true;
  732. _txtShortcut.Enabled = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  733. }
  734. };
  735. //Add (_frmMenuDetails);
  736. }
  737. public DynamicMenuItem EnterMenuItem ()
  738. {
  739. var valid = false;
  740. if (_menuItem == null) {
  741. var m = new DynamicMenuItem ();
  742. _txtTitle.Text = m.title;
  743. _txtHelp.Text = m.help;
  744. _txtAction.Text = m.action;
  745. _ckbIsTopLevel.Checked = false;
  746. _ckbSubMenu.Checked = !hasParent;
  747. _txtHelp.Enabled = hasParent;
  748. _txtAction.Enabled = hasParent;
  749. _txtShortcut.Enabled = hasParent;
  750. } else {
  751. EditMenuBarItem (_menuItem);
  752. }
  753. var _btnOk = new Button ("Ok") {
  754. IsDefault = true,
  755. };
  756. _btnOk.Clicked += () => {
  757. if (ustring.IsNullOrEmpty (_txtTitle.Text)) {
  758. MessageBox.ErrorQuery ("Invalid title", "Must enter a valid title!.", "Ok");
  759. } else {
  760. valid = true;
  761. Application.RequestStop ();
  762. }
  763. };
  764. var _btnCancel = new Button ("Cancel");
  765. _btnCancel.Clicked += () => {
  766. _txtTitle.Text = ustring.Empty;
  767. Application.RequestStop ();
  768. };
  769. var _dialog = new Dialog ("Please enter the menu details.", _btnOk, _btnCancel);
  770. Width = Dim.Fill ();
  771. Height = Dim.Fill () - 1;
  772. _dialog.Add (this);
  773. _txtTitle.SetFocus ();
  774. _txtTitle.CursorPosition = _txtTitle.Text.Length;
  775. Application.Run (_dialog);
  776. if (valid) {
  777. return new DynamicMenuItem (_txtTitle.Text, _txtHelp.Text, _txtAction.Text,
  778. _ckbIsTopLevel != null ? _ckbIsTopLevel.Checked : false,
  779. _ckbSubMenu != null ? _ckbSubMenu.Checked : false,
  780. _rbChkStyle.SelectedItem == 0 ? MenuItemCheckStyle.NoCheck :
  781. _rbChkStyle.SelectedItem == 1 ? MenuItemCheckStyle.Checked : MenuItemCheckStyle.Radio,
  782. _txtShortcut.Text);
  783. } else {
  784. return null;
  785. }
  786. }
  787. public void EditMenuBarItem (MenuItem menuItem)
  788. {
  789. if (menuItem == null) {
  790. hasParent = false;
  791. Enabled = false;
  792. CleanEditMenuBarItem ();
  793. return;
  794. } else {
  795. hasParent = menuItem.Parent != null;
  796. Enabled = true;
  797. }
  798. _menuItem = menuItem;
  799. _txtTitle.Text = menuItem?.Title ?? "";
  800. _txtHelp.Text = menuItem?.Help ?? "";
  801. _txtAction.Text = menuItem != null && menuItem.Action != null ? GetTargetAction (menuItem.Action) : ustring.Empty;
  802. _ckbIsTopLevel.Checked = IsTopLevel (menuItem);
  803. _ckbSubMenu.Checked = HasSubMenus (menuItem);
  804. _txtHelp.Enabled = !_ckbSubMenu.Checked;
  805. _txtAction.Enabled = !_ckbSubMenu.Checked;
  806. _rbChkStyle.SelectedItem = (int)(menuItem?.CheckType ?? MenuItemCheckStyle.NoCheck);
  807. _txtShortcut.Text = menuItem?.ShortcutTag ?? "";
  808. _txtShortcut.Enabled = !_ckbIsTopLevel.Checked && !_ckbSubMenu.Checked;
  809. }
  810. void CleanEditMenuBarItem ()
  811. {
  812. _txtTitle.Text = "";
  813. _txtHelp.Text = "";
  814. _txtAction.Text = "";
  815. _ckbIsTopLevel.Checked = false;
  816. _ckbSubMenu.Checked = false;
  817. _rbChkStyle.SelectedItem = (int)MenuItemCheckStyle.NoCheck;
  818. _txtShortcut.Text = "";
  819. }
  820. ustring GetTargetAction (Action action)
  821. {
  822. var me = action.Target;
  823. if (me == null) {
  824. throw new ArgumentException ();
  825. }
  826. object v = new object ();
  827. foreach (var field in me.GetType ().GetFields ()) {
  828. if (field.Name == "item") {
  829. v = field.GetValue (me);
  830. }
  831. }
  832. return v == null || !(v is DynamicMenuItem item) ? ustring.Empty : item.action;
  833. }
  834. bool IsTopLevel (MenuItem menuItem)
  835. {
  836. var topLevel = menuItem as MenuBarItem;
  837. if (topLevel != null && topLevel.Parent == null && (topLevel.Children == null || topLevel.Children.Length == 0) && topLevel.Action != null) {
  838. return true;
  839. } else {
  840. return false;
  841. }
  842. }
  843. bool HasSubMenus (MenuItem menuItem)
  844. {
  845. var menuBarItem = menuItem as MenuBarItem;
  846. if (menuBarItem != null && menuBarItem.Children != null && (menuBarItem.Children.Length > 0 || menuBarItem.Action == null)) {
  847. return true;
  848. } else {
  849. return false;
  850. }
  851. }
  852. public Action CreateAction (MenuItem menuItem, DynamicMenuItem item)
  853. {
  854. switch (menuItem.CheckType) {
  855. case MenuItemCheckStyle.NoCheck:
  856. return new Action (() => MessageBox.ErrorQuery (item.title, item.action, "Ok"));
  857. case MenuItemCheckStyle.Checked:
  858. return new Action (() => menuItem.Checked = !menuItem.Checked);
  859. case MenuItemCheckStyle.Radio:
  860. break;
  861. }
  862. return new Action (() => {
  863. menuItem.Checked = true;
  864. var parent = menuItem?.Parent as MenuBarItem;
  865. if (parent != null) {
  866. var childrens = parent.Children;
  867. for (int i = 0; i < childrens.Length; i++) {
  868. var child = childrens [i];
  869. if (child != menuItem) {
  870. child.Checked = false;
  871. }
  872. }
  873. }
  874. });
  875. }
  876. public void UpdateParent (ref MenuItem menuItem)
  877. {
  878. var parent = menuItem.Parent as MenuBarItem;
  879. var idx = parent.GetChildrenIndex (menuItem);
  880. if (!(menuItem is MenuBarItem)) {
  881. menuItem = new MenuBarItem (menuItem.Title, new MenuItem [] { }, menuItem.Parent);
  882. if (idx > -1) {
  883. parent.Children [idx] = menuItem;
  884. }
  885. } else {
  886. menuItem = new MenuItem (menuItem.Title, menuItem.Help, CreateAction (menuItem, new DynamicMenuItem ()), null, menuItem.Parent);
  887. if (idx > -1) {
  888. parent.Children [idx] = menuItem;
  889. }
  890. }
  891. }
  892. }
  893. public class DynamicMenuItemModel : INotifyPropertyChanged {
  894. public event PropertyChangedEventHandler PropertyChanged;
  895. private ustring menuBar;
  896. private ustring parent;
  897. private List<DynamicMenuItemList> menus;
  898. public ustring MenuBar {
  899. get => menuBar;
  900. set {
  901. if (value != menuBar) {
  902. menuBar = value;
  903. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  904. }
  905. }
  906. }
  907. public ustring Parent {
  908. get => parent;
  909. set {
  910. if (value != parent) {
  911. parent = value;
  912. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  913. }
  914. }
  915. }
  916. public List<DynamicMenuItemList> Menus {
  917. get => menus;
  918. set {
  919. if (value != menus) {
  920. menus = value;
  921. PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (GetPropertyName ()));
  922. }
  923. }
  924. }
  925. public DynamicMenuItemModel ()
  926. {
  927. Menus = new List<DynamicMenuItemList> ();
  928. }
  929. public string GetPropertyName ([CallerMemberName] string propertyName = null)
  930. {
  931. return propertyName;
  932. }
  933. }
  934. public interface IValueConverter {
  935. object Convert (object value, object parameter = null);
  936. }
  937. public class Binding {
  938. public View Target { get; private set; }
  939. public View Source { get; private set; }
  940. public string SourcePropertyName { get; private set; }
  941. public string TargetPropertyName { get; private set; }
  942. private object sourceDataContext;
  943. private PropertyInfo sourceBindingProperty;
  944. private IValueConverter valueConverter;
  945. public Binding (View source, string sourcePropertyName, View target, string targetPropertyName, IValueConverter valueConverter = null)
  946. {
  947. Target = target;
  948. Source = source;
  949. SourcePropertyName = sourcePropertyName;
  950. TargetPropertyName = targetPropertyName;
  951. sourceDataContext = Source.GetType ().GetProperty ("DataContext").GetValue (Source);
  952. sourceBindingProperty = sourceDataContext.GetType ().GetProperty (SourcePropertyName);
  953. this.valueConverter = valueConverter;
  954. UpdateTarget ();
  955. var notifier = ((INotifyPropertyChanged)sourceDataContext);
  956. if (notifier != null) {
  957. notifier.PropertyChanged += (s, e) => {
  958. if (e.PropertyName == SourcePropertyName) {
  959. UpdateTarget ();
  960. }
  961. };
  962. }
  963. }
  964. private void UpdateTarget ()
  965. {
  966. try {
  967. var sourceValue = sourceBindingProperty.GetValue (sourceDataContext);
  968. if (sourceValue == null) {
  969. return;
  970. }
  971. var finalValue = valueConverter?.Convert (sourceValue) ?? sourceValue;
  972. var targetProperty = Target.GetType ().GetProperty (TargetPropertyName);
  973. targetProperty.SetValue (Target, finalValue);
  974. } catch (Exception ex) {
  975. MessageBox.ErrorQuery ("Binding Error", $"Binding failed: {ex}.", "Ok");
  976. }
  977. }
  978. }
  979. public class ListWrapperConverter : IValueConverter {
  980. public object Convert (object value, object parameter = null)
  981. {
  982. return new ListWrapper ((IList)value);
  983. }
  984. }
  985. public class UStringValueConverter : IValueConverter {
  986. public object Convert (object value, object parameter = null)
  987. {
  988. var data = Encoding.ASCII.GetBytes (value.ToString ());
  989. return ustring.Make (data);
  990. }
  991. }
  992. }
  993. }