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