sample-function_key_setting.adb 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ------------------------------------------------------------------------------
  2. -- --
  3. -- GNAT ncurses Binding Samples --
  4. -- --
  5. -- Sample.Function_Key_Setting --
  6. -- --
  7. -- B O D Y --
  8. -- --
  9. ------------------------------------------------------------------------------
  10. -- Copyright (c) 1998,2004 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: Juergen Pfeifer, 1996
  37. -- Version Control
  38. -- $Revision: 1.13 $
  39. -- $Date: 2004/08/21 21:37:00 $
  40. -- Binding Version 01.00
  41. ------------------------------------------------------------------------------
  42. with Ada.Unchecked_Deallocation;
  43. with Sample.Manifest; use Sample.Manifest;
  44. -- This package implements a simple stack of function key label environments.
  45. --
  46. package body Sample.Function_Key_Setting is
  47. Max_Label_Length : constant Positive := 8;
  48. Number_Of_Keys : Label_Number := Label_Number'Last;
  49. Justification : Label_Justification := Left;
  50. subtype Label is String (1 .. Max_Label_Length);
  51. type Label_Array is array (Label_Number range <>) of Label;
  52. type Key_Environment (N : Label_Number := Label_Number'Last);
  53. type Env_Ptr is access Key_Environment;
  54. pragma Controlled (Env_Ptr);
  55. type String_Access is access String;
  56. pragma Controlled (String_Access);
  57. Active_Context : String_Access := new String'("MAIN");
  58. Active_Notepad : Panel := Null_Panel;
  59. type Key_Environment (N : Label_Number := Label_Number'Last) is
  60. record
  61. Prev : Env_Ptr;
  62. Help : String_Access;
  63. Notepad : Panel;
  64. Labels : Label_Array (1 .. N);
  65. end record;
  66. procedure Release_String is
  67. new Ada.Unchecked_Deallocation (String,
  68. String_Access);
  69. procedure Release_Environment is
  70. new Ada.Unchecked_Deallocation (Key_Environment,
  71. Env_Ptr);
  72. Top_Of_Stack : Env_Ptr := null;
  73. procedure Push_Environment (Key : in String;
  74. Reset : in Boolean := True)
  75. is
  76. P : constant Env_Ptr := new Key_Environment (Number_Of_Keys);
  77. begin
  78. -- Store the current labels in the environment
  79. for I in 1 .. Number_Of_Keys loop
  80. Get_Soft_Label_Key (I, P.Labels (I));
  81. if Reset then
  82. Set_Soft_Label_Key (I, " ");
  83. end if;
  84. end loop;
  85. P.Prev := Top_Of_Stack;
  86. -- now store active help context and notepad
  87. P.Help := Active_Context;
  88. P.Notepad := Active_Notepad;
  89. -- The notepad must now vanish and the new notepad is empty.
  90. if P.Notepad /= Null_Panel then
  91. Hide (P.Notepad);
  92. Update_Panels;
  93. end if;
  94. Active_Notepad := Null_Panel;
  95. Active_Context := new String'(Key);
  96. Top_Of_Stack := P;
  97. if Reset then
  98. Refresh_Soft_Label_Keys_Without_Update;
  99. end if;
  100. end Push_Environment;
  101. procedure Pop_Environment
  102. is
  103. P : Env_Ptr := Top_Of_Stack;
  104. begin
  105. if Top_Of_Stack = null then
  106. raise Function_Key_Stack_Error;
  107. else
  108. for I in 1 .. Number_Of_Keys loop
  109. Set_Soft_Label_Key (I, P.Labels (I), Justification);
  110. end loop;
  111. pragma Assert (Active_Context /= null);
  112. Release_String (Active_Context);
  113. Active_Context := P.Help;
  114. Refresh_Soft_Label_Keys_Without_Update;
  115. Notepad_To_Context (P.Notepad);
  116. Top_Of_Stack := P.Prev;
  117. Release_Environment (P);
  118. end if;
  119. end Pop_Environment;
  120. function Context return String
  121. is
  122. begin
  123. if Active_Context /= null then
  124. return Active_Context.all;
  125. else
  126. return "";
  127. end if;
  128. end Context;
  129. function Find_Context (Key : String) return Boolean
  130. is
  131. P : Env_Ptr := Top_Of_Stack;
  132. begin
  133. if Active_Context.all = Key then
  134. return True;
  135. else
  136. loop
  137. exit when P = null;
  138. if P.Help.all = Key then
  139. return True;
  140. else
  141. P := P.Prev;
  142. end if;
  143. end loop;
  144. return False;
  145. end if;
  146. end Find_Context;
  147. procedure Notepad_To_Context (Pan : in Panel)
  148. is
  149. W : Window;
  150. begin
  151. if Active_Notepad /= Null_Panel then
  152. W := Get_Window (Active_Notepad);
  153. Clear (W);
  154. Delete (Active_Notepad);
  155. Delete (W);
  156. end if;
  157. Active_Notepad := Pan;
  158. if Pan /= Null_Panel then
  159. Top (Pan);
  160. end if;
  161. Update_Panels;
  162. Update_Screen;
  163. end Notepad_To_Context;
  164. procedure Initialize (Mode : Soft_Label_Key_Format := PC_Style;
  165. Just : Label_Justification := Left)
  166. is
  167. begin
  168. case Mode is
  169. when PC_Style .. PC_Style_With_Index
  170. => Number_Of_Keys := 12;
  171. when others
  172. => Number_Of_Keys := 8;
  173. end case;
  174. Init_Soft_Label_Keys (Mode);
  175. Justification := Just;
  176. end Initialize;
  177. procedure Default_Labels
  178. is
  179. begin
  180. Set_Soft_Label_Key (FKEY_QUIT, "Quit");
  181. Set_Soft_Label_Key (FKEY_HELP, "Help");
  182. Set_Soft_Label_Key (FKEY_EXPLAIN, "Keys");
  183. Refresh_Soft_Label_Keys_Without_Update;
  184. end Default_Labels;
  185. function Notepad_Window return Window
  186. is
  187. begin
  188. if Active_Notepad /= Null_Panel then
  189. return Get_Window (Active_Notepad);
  190. else
  191. return Null_Window;
  192. end if;
  193. end Notepad_Window;
  194. end Sample.Function_Key_Setting;