ncurses2-menu_test.adb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- ncurses --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 2000-2004,2006 Free Software Foundation, Inc. --
  11. -- --
  12. -- Permission is hereby granted, free of charge, to any person obtaining a --
  13. -- copy of this software and associated documentation files (the --
  14. -- "Software"), to deal in the Software without restriction, including --
  15. -- without limitation the rights to use, copy, modify, merge, publish, --
  16. -- distribute, distribute with modifications, sublicense, and/or sell --
  17. -- copies of the Software, and to permit persons to whom the Software is --
  18. -- furnished to do so, subject to the following conditions: --
  19. -- --
  20. -- The above copyright notice and this permission notice shall be included --
  21. -- in all copies or substantial portions of the Software. --
  22. -- --
  23. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
  24. -- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
  25. -- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
  26. -- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
  27. -- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
  28. -- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
  29. -- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
  30. -- --
  31. -- Except as contained in this notice, the name(s) of the above copyright --
  32. -- holders shall not be used in advertising or otherwise to promote the --
  33. -- sale, use or other dealings in this Software without prior written --
  34. -- authorization. --
  35. ------------------------------------------------------------------------------
  36. -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
  37. -- Version Control
  38. -- $Revision: 1.6 $
  39. -- $Date: 2006/06/25 14:24:40 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with ncurses2.util; use ncurses2.util;
  43. with Terminal_Interface.Curses; use Terminal_Interface.Curses;
  44. with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
  45. with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
  46. procedure ncurses2.menu_test is
  47. function menu_virtualize (c : Key_Code) return Menu_Request_Code;
  48. procedure xAdd (l : Line_Position; c : Column_Position; s : String);
  49. function menu_virtualize (c : Key_Code) return Menu_Request_Code is
  50. begin
  51. case c is
  52. when Character'Pos (newl) | Key_Exit =>
  53. return Menu_Request_Code'Last + 1; -- MAX_COMMAND? TODO
  54. when Character'Pos ('u') =>
  55. return M_ScrollUp_Line;
  56. when Character'Pos ('d') =>
  57. return M_ScrollDown_Line;
  58. when Character'Pos ('b') | Key_Next_Page =>
  59. return M_ScrollUp_Page;
  60. when Character'Pos ('f') | Key_Previous_Page =>
  61. return M_ScrollDown_Page;
  62. when Character'Pos ('n') | Key_Cursor_Down =>
  63. return M_Next_Item;
  64. when Character'Pos ('p') | Key_Cursor_Up =>
  65. return M_Previous_Item;
  66. when Character'Pos (' ') =>
  67. return M_Toggle_Item;
  68. when Key_Mouse =>
  69. return c;
  70. when others =>
  71. Beep;
  72. return c;
  73. end case;
  74. end menu_virtualize;
  75. MENU_Y : constant Line_Count := 8;
  76. MENU_X : constant Column_Count := 8;
  77. type String_Access is access String;
  78. animals : constant array (Positive range <>) of String_Access :=
  79. (new String'("Lions"),
  80. new String'("Tigers"),
  81. new String'("Bears"),
  82. new String'("(Oh my!)"),
  83. new String'("Newts"),
  84. new String'("Platypi"),
  85. new String'("Lemurs"));
  86. items_a : constant Item_Array_Access :=
  87. new Item_Array (1 .. animals'Last + 1);
  88. tmp : Event_Mask;
  89. procedure xAdd (l : Line_Position; c : Column_Position; s : String) is
  90. begin
  91. Add (Line => l, Column => c, Str => s);
  92. end xAdd;
  93. mrows : Line_Count;
  94. mcols : Column_Count;
  95. menuwin : Window;
  96. m : Menu;
  97. c1 : Key_Code;
  98. c : Driver_Result;
  99. r : Menu_Request_Code;
  100. begin
  101. tmp := Start_Mouse;
  102. xAdd (0, 0, "This is the menu test:");
  103. xAdd (2, 0, " Use up and down arrow to move the select bar.");
  104. xAdd (3, 0, " 'n' and 'p' act like arrows.");
  105. xAdd (4, 0, " 'b' and 'f' scroll up/down (page), 'u' and 'd' (line).");
  106. xAdd (5, 0, " Press return to exit.");
  107. Refresh;
  108. for i in animals'Range loop
  109. items_a (i) := New_Item (animals (i).all);
  110. end loop;
  111. items_a (animals'Last + 1) := Null_Item;
  112. m := New_Menu (items_a);
  113. Set_Format (m, Line_Position (animals'Last + 1) / 2, 1);
  114. Scale (m, mrows, mcols);
  115. menuwin := Create (mrows + 2, mcols + 2, MENU_Y, MENU_X);
  116. Set_Window (m, menuwin);
  117. Set_KeyPad_Mode (menuwin, True);
  118. Box (menuwin); -- 0,0?
  119. Set_Sub_Window (m, Derived_Window (menuwin, mrows, mcols, 1, 1));
  120. Post (m);
  121. loop
  122. c1 := Getchar (menuwin);
  123. r := menu_virtualize (c1);
  124. c := Driver (m, r);
  125. exit when c = Unknown_Request; -- E_UNKNOWN_COMMAND?
  126. if c = Request_Denied then
  127. Beep;
  128. end if;
  129. -- continue ?
  130. end loop;
  131. Move_Cursor (Line => Lines - 2, Column => 0);
  132. Add (Str => "You chose: ");
  133. Add (Str => Name (Current (m)));
  134. Add (Ch => newl);
  135. Pause; -- the C version didn't use Pause, it spelled it out
  136. Post (m, False); -- unpost, not clear :-(
  137. declare begin
  138. Delete (menuwin);
  139. exception when Curses_Exception => null; end;
  140. -- menuwin has children so will raise the exception.
  141. Delete (m);
  142. End_Mouse (tmp);
  143. end ncurses2.menu_test;