ncurses2-slk_test.adb 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.7 $
  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 Ada.Strings.Unbounded;
  45. with Interfaces.C;
  46. with Terminal_Interface.Curses.Aux;
  47. procedure ncurses2.slk_test is
  48. procedure myGet (Win : in Window := Standard_Window;
  49. Str : out Ada.Strings.Unbounded.Unbounded_String;
  50. Len : in Integer := -1);
  51. procedure myGet (Win : in Window := Standard_Window;
  52. Str : out Ada.Strings.Unbounded.Unbounded_String;
  53. Len : in Integer := -1)
  54. is
  55. use Ada.Strings.Unbounded;
  56. use Interfaces.C;
  57. use Terminal_Interface.Curses.Aux;
  58. function Wgetnstr (Win : Window;
  59. Str : char_array;
  60. Len : int) return int;
  61. pragma Import (C, Wgetnstr, "wgetnstr");
  62. -- FIXME: how to construct "(Len > 0) ? Len : 80"?
  63. Ask : constant Interfaces.C.size_t := Interfaces.C.size_t'Val (Len + 80);
  64. Txt : char_array (0 .. Ask);
  65. begin
  66. Txt (0) := Interfaces.C.char'First;
  67. if Wgetnstr (Win, Txt, Txt'Length) = Curses_Err then
  68. raise Curses_Exception;
  69. end if;
  70. Str := To_Unbounded_String (To_Ada (Txt, True));
  71. end myGet;
  72. use Int_IO;
  73. use Ada.Strings.Unbounded;
  74. c : Key_Code;
  75. buf : Unbounded_String;
  76. c2 : Character;
  77. fmt : Label_Justification := Centered;
  78. tmp : Integer;
  79. begin
  80. c := CTRL ('l');
  81. loop
  82. Move_Cursor (Line => 0, Column => 0);
  83. c2 := Code_To_Char (c);
  84. case c2 is
  85. when Character'Val (Character'Pos ('l') mod 16#20#) => -- CTRL('l')
  86. Erase;
  87. Switch_Character_Attribute (Attr => (Bold_Character => True,
  88. others => False));
  89. Add (Line => 0, Column => 20,
  90. Str => "Soft Key Exerciser");
  91. Switch_Character_Attribute (On => False,
  92. Attr => (Bold_Character => True,
  93. others => False));
  94. Move_Cursor (Line => 2, Column => 0);
  95. P ("Available commands are:");
  96. P ("");
  97. P ("^L -- refresh screen");
  98. P ("a -- activate or restore soft keys");
  99. P ("d -- disable soft keys");
  100. P ("c -- set centered format for labels");
  101. P ("l -- set left-justified format for labels");
  102. P ("r -- set right-justified format for labels");
  103. P ("[12345678] -- set label; labels are numbered 1 through 8");
  104. P ("e -- erase stdscr (should not erase labels)");
  105. P ("s -- test scrolling of shortened screen");
  106. P ("x, q -- return to main menu");
  107. P ("");
  108. P ("Note: if activating the soft keys causes your terminal to");
  109. P ("scroll up one line, your terminal auto-scrolls when anything");
  110. P ("is written to the last screen position. The ncurses code");
  111. P ("does not yet handle this gracefully.");
  112. Refresh;
  113. Restore_Soft_Label_Keys;
  114. when 'a' =>
  115. Restore_Soft_Label_Keys;
  116. when 'e' =>
  117. Clear;
  118. when 's' =>
  119. Add (Line => 20, Column => 0,
  120. Str => "Press Q to stop the scrolling-test: ");
  121. loop
  122. c := Getchar;
  123. c2 := Code_To_Char (c);
  124. exit when c2 = 'Q';
  125. -- c = ERR?
  126. -- TODO when c is not a character (arrow key)
  127. -- the behavior is different from the C version.
  128. Add (Ch => c2);
  129. end loop;
  130. when 'd' =>
  131. Clear_Soft_Label_Keys;
  132. when 'l' =>
  133. fmt := Left;
  134. when 'c' =>
  135. fmt := Centered;
  136. when 'r' =>
  137. fmt := Right;
  138. when '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' =>
  139. Add (Line => 20, Column => 0,
  140. Str => "Please enter the label value: ");
  141. Set_Echo_Mode (SwitchOn => True);
  142. myGet (Str => buf);
  143. Set_Echo_Mode (SwitchOn => False);
  144. tmp := ctoi (c2);
  145. Set_Soft_Label_Key (Label_Number (tmp), To_String (buf), fmt);
  146. Refresh_Soft_Label_Keys;
  147. Move_Cursor (Line => 20, Column => 0);
  148. Clear_To_End_Of_Line;
  149. when 'x' | 'q' =>
  150. exit;
  151. -- the C version needed a goto, ha ha
  152. -- breaks exit the case not the loop because fall-throuh
  153. -- happens in C!
  154. when others =>
  155. Beep;
  156. end case;
  157. c := Getchar;
  158. -- TODO exit when c = EOF
  159. end loop;
  160. Erase;
  161. End_Windows;
  162. end ncurses2.slk_test;