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