]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/sample-menu_demo.adb
ncurses 5.1
[ncurses.git] / Ada95 / samples / sample-menu_demo.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                              Sample.Menu_Demo                            --
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@gmx.net> 1996
37 --  Version Control
38 --  $Revision: 1.11 $
39 --  Binding Version 01.00
40 ------------------------------------------------------------------------------
41 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
42 with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
43 with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
44 with Terminal_Interface.Curses.Menus.Menu_User_Data;
45 with Terminal_Interface.Curses.Menus.Item_User_Data;
46
47 with Sample.Manifest; use Sample.Manifest;
48 with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
49 with Sample.Menu_Demo.Handler;
50 with Sample.Helpers; use Sample.Helpers;
51 with Sample.Explanation; use Sample.Explanation;
52
53 package body Sample.Menu_Demo is
54
55    package Spacing_Demo is
56       procedure Spacing_Test;
57    end Spacing_Demo;
58
59    package body Spacing_Demo is
60
61       procedure Spacing_Test
62       is
63          function My_Driver (M : Menu;
64                              K : Key_Code;
65                              P : Panel) return Boolean;
66
67          procedure Set_Option_Key;
68          procedure Set_Select_Key;
69          procedure Set_Description_Key;
70          procedure Set_Hide_Key;
71
72          package Mh is new Sample.Menu_Demo.Handler (My_Driver);
73
74          I : Item_Array_Access := new Item_Array'
75            (New_Item ("January",   "31 Days"),
76             New_Item ("February",  "28/29 Days"),
77             New_Item ("March",     "31 Days"),
78             New_Item ("April",     "30 Days"),
79             New_Item ("May",       "31 Days"),
80             New_Item ("June",      "30 Days"),
81             New_Item ("July",      "31 Days"),
82             New_Item ("August",    "31 Days"),
83             New_Item ("September", "30 Days"),
84             New_Item ("October",   "31 Days"),
85             New_Item ("November",  "30 Days"),
86             New_Item ("December",  "31 Days"),
87             Null_Item);
88
89          M : Menu   := New_Menu (I);
90          Flip_State : Boolean := True;
91          Hide_Long  : Boolean := False;
92
93          type Format_Code is (Four_By_1, Four_By_2, Four_By_3);
94          type Operations  is (Flip, Reorder, Reformat, Reselect, Describe);
95
96          type Change is array (Operations) of Boolean;
97          pragma Pack (Change);
98          No_Change : constant Change := Change'(others => False);
99
100          Current_Format : Format_Code := Four_By_1;
101          To_Change : Change := No_Change;
102
103          function My_Driver (M : Menu;
104                              K : Key_Code;
105                              P : Panel) return Boolean
106          is
107          begin
108             To_Change := No_Change;
109             if K in User_Key_Code'Range then
110                if K = QUIT then
111                   return True;
112                end if;
113             end if;
114             if K in Special_Key_Code'Range then
115                case K is
116                   when Key_F4 =>
117                      To_Change (Flip) := True;
118                      return True;
119                   when Key_F5 =>
120                      To_Change (Reformat)  := True;
121                      Current_Format := Four_By_1;
122                      return True;
123                   when Key_F6 =>
124                      To_Change (Reformat)  := True;
125                      Current_Format := Four_By_2;
126                      return True;
127                   when Key_F7 =>
128                      To_Change (Reformat)  := True;
129                      Current_Format := Four_By_3;
130                      return True;
131                   when Key_F8 =>
132                      To_Change (Reorder) := True;
133                      return True;
134                   when Key_F9 =>
135                      To_Change (Reselect) := True;
136                      return True;
137                   when Key_F10 =>
138                      if Current_Format /= Four_By_3 then
139                         To_Change (Describe) := True;
140                         return True;
141                      else
142                         return False;
143                      end if;
144                   when Key_F11 =>
145                      Hide_Long := not Hide_Long;
146                      declare
147                         O : Item_Option_Set;
148                      begin
149                         for J in I'Range loop
150                            Get_Options (I (J), O);
151                            O.Selectable := True;
152                            if Hide_Long then
153                               case J is
154                                  when 1 | 3 | 5 | 7 | 8 | 10 | 12 =>
155                                     O.Selectable := False;
156                                  when others => null;
157                               end case;
158                            end if;
159                            Set_Options (I (J), O);
160                         end loop;
161                      end;
162                      return False;
163                   when others => null;
164                end case;
165             end if;
166             return False;
167          end My_Driver;
168
169          procedure Set_Option_Key
170          is
171             O : Menu_Option_Set;
172          begin
173             if Current_Format = Four_By_1 then
174                Set_Soft_Label_Key (8, "");
175             else
176                Get_Options (M, O);
177                if O.Row_Major_Order then
178                   Set_Soft_Label_Key (8, "O-Col");
179                else
180                   Set_Soft_Label_Key (8, "O-Row");
181                end if;
182             end if;
183             Refresh_Soft_Label_Keys_Without_Update;
184          end Set_Option_Key;
185
186          procedure Set_Select_Key
187          is
188             O : Menu_Option_Set;
189          begin
190             Get_Options (M, O);
191             if O.One_Valued then
192                Set_Soft_Label_Key (9, "Multi");
193             else
194                Set_Soft_Label_Key (9, "Singl");
195             end if;
196             Refresh_Soft_Label_Keys_Without_Update;
197          end Set_Select_Key;
198
199          procedure Set_Description_Key
200          is
201             O : Menu_Option_Set;
202          begin
203             if Current_Format = Four_By_3 then
204                Set_Soft_Label_Key (10, "");
205             else
206                Get_Options (M, O);
207                if O.Show_Descriptions then
208                   Set_Soft_Label_Key (10, "-Desc");
209                else
210                   Set_Soft_Label_Key (10, "+Desc");
211                end if;
212             end if;
213             Refresh_Soft_Label_Keys_Without_Update;
214          end Set_Description_Key;
215
216          procedure Set_Hide_Key
217          is
218          begin
219             if Hide_Long then
220                Set_Soft_Label_Key (11, "Enab");
221             else
222                Set_Soft_Label_Key (11, "Disab");
223             end if;
224             Refresh_Soft_Label_Keys_Without_Update;
225          end Set_Hide_Key;
226
227       begin
228          Push_Environment ("MENU01");
229          Notepad ("MENU-PAD01");
230          Default_Labels;
231          Set_Soft_Label_Key (4, "Flip");
232          Set_Soft_Label_Key (5, "4x1");
233          Set_Soft_Label_Key (6, "4x2");
234          Set_Soft_Label_Key (7, "4x3");
235          Set_Option_Key;
236          Set_Select_Key;
237          Set_Description_Key;
238          Set_Hide_Key;
239
240          Set_Format (M, 4, 1);
241          loop
242             Mh.Drive_Me (M);
243             exit when To_Change = No_Change;
244             if To_Change (Flip) then
245                if Flip_State then
246                   Flip_State := False;
247                   Set_Spacing (M, 3, 2, 0);
248                else
249                   Flip_State := True;
250                   Set_Spacing (M);
251                end if;
252             elsif To_Change (Reformat) then
253                case Current_Format is
254                   when Four_By_1 => Set_Format (M, 4, 1);
255                   when Four_By_2 => Set_Format (M, 4, 2);
256                   when Four_By_3 =>
257                      declare
258                         O : Menu_Option_Set;
259                      begin
260                         Get_Options (M, O);
261                         O.Show_Descriptions := False;
262                         Set_Options (M, O);
263                         Set_Format (M, 4, 3);
264                      end;
265                end case;
266                Set_Option_Key;
267                Set_Description_Key;
268             elsif To_Change (Reorder) then
269                declare
270                   O : Menu_Option_Set;
271                begin
272                   Get_Options (M, O);
273                   O.Row_Major_Order := not O.Row_Major_Order;
274                   Set_Options (M, O);
275                   Set_Option_Key;
276                end;
277             elsif To_Change (Reselect) then
278                declare
279                   O : Menu_Option_Set;
280                begin
281                   Get_Options (M, O);
282                   O.One_Valued := not O.One_Valued;
283                   Set_Options (M, O);
284                   Set_Select_Key;
285                end;
286             elsif To_Change (Describe) then
287                declare
288                   O : Menu_Option_Set;
289                begin
290                   Get_Options (M, O);
291                   O.Show_Descriptions := not O.Show_Descriptions;
292                   Set_Options (M, O);
293                   Set_Description_Key;
294                end;
295             else
296                null;
297             end if;
298          end loop;
299          Set_Spacing (M);
300          Flip_State := True;
301
302          Pop_Environment;
303          pragma Assert (Get_Index (Items (M, 1)) = Get_Index (I (1)));
304          Delete (M);
305          Free (I, True);
306       end Spacing_Test;
307    end Spacing_Demo;
308
309    procedure Demo
310    is
311       --  We use this datatype only to test the instantiation of
312       --  the Menu_User_Data generic package. No functionality
313       --  behind it.
314       type User_Data is new Integer;
315       type User_Data_Access is access User_Data;
316
317       --  Those packages are only instantiated to test the usability.
318       --  No real functionality is shown in the demo.
319       package MUD is new Menu_User_Data (User_Data, User_Data_Access);
320       package IUD is new Item_User_Data (User_Data, User_Data_Access);
321
322       function My_Driver (M : Menu;
323                           K : Key_Code;
324                           P : Panel) return Boolean;
325
326       package Mh is new Sample.Menu_Demo.Handler (My_Driver);
327
328       Itm : Item_Array_Access := new Item_Array'
329         (New_Item ("Menu Layout Options"),
330          New_Item ("Demo of Hook functions"),
331          Null_Item);
332       M : Menu := New_Menu (Itm);
333
334       U1 : User_Data_Access := new User_Data'(4711);
335       U2 : User_Data_Access;
336       U3 : User_Data_Access := new User_Data'(4712);
337       U4 : User_Data_Access;
338
339       function My_Driver (M : Menu;
340                           K : Key_Code;
341                           P : Panel) return Boolean
342       is
343          Idx   : constant Positive := Get_Index (Current (M));
344       begin
345          if K in User_Key_Code'Range then
346             if K = QUIT then
347                return True;
348             elsif K = SELECT_ITEM then
349                if Idx in Itm'Range then
350                   Hide (P);
351                   Update_Panels;
352                end if;
353                case Idx is
354                   when 1 => Spacing_Demo.Spacing_Test;
355                   when others => Not_Implemented;
356                end case;
357                if Idx in Itm'Range then
358                   Top (P);
359                   Show (P);
360                   Update_Panels;
361                   Update_Screen;
362                end if;
363             end if;
364          end if;
365          return False;
366       end My_Driver;
367    begin
368       Push_Environment ("MENU00");
369       Notepad ("MENU-PAD00");
370       Default_Labels;
371       Refresh_Soft_Label_Keys_Without_Update;
372       Set_Pad_Character (M, '|');
373
374       MUD.Set_User_Data (M, U1);
375       IUD.Set_User_Data (Itm (1), U3);
376
377       Mh.Drive_Me (M);
378
379       MUD.Get_User_Data (M, U2);
380       pragma Assert (U1 = U2 and U1.all = 4711);
381
382       IUD.Get_User_Data (Itm (1), U4);
383       pragma Assert (U3 = U4 and U3.all = 4712);
384
385       Pop_Environment;
386       Delete (M);
387       Free (Itm, True);
388    end Demo;
389
390 end Sample.Menu_Demo;