]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/sample.adb
2d96142c723c41d08bcd646d18ebba0514dd0c7e
[ncurses.git] / Ada95 / samples / sample.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                                 Sample                                   --
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 Text_IO;
42
43 with Ada.Exceptions; use Ada.Exceptions;
44
45 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
46 with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
47 with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
48 with Terminal_Interface.Curses.Menus.Menu_User_Data;
49 with Terminal_Interface.Curses.Menus.Item_User_Data;
50
51 with Sample.Manifest; use Sample.Manifest;
52 with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
53 with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
54 with Sample.Header_Handler; use Sample.Header_Handler;
55 with Sample.Explanation; use Sample.Explanation;
56
57 with Sample.Menu_Demo.Handler;
58 with Sample.Curses_Demo;
59 with Sample.Form_Demo;
60 with Sample.Menu_Demo;
61 with Sample.Text_IO_Demo;
62
63 with GNAT.OS_Lib;
64
65 package body Sample is
66
67    type User_Data is
68       record
69          Data : Integer;
70       end record;
71    type User_Access is access User_Data;
72
73    package Ud is new
74      Terminal_Interface.Curses.Menus.Menu_User_Data
75      (User_Data, User_Access);
76
77    package Id is new
78      Terminal_Interface.Curses.Menus.Item_User_Data
79      (User_Data, User_Access);
80
81    procedure Whow is
82       procedure Main_Menu;
83       procedure Main_Menu
84       is
85          function My_Driver (M : Menu;
86                              K : Key_Code;
87                              Pan : Panel) return Boolean;
88
89          package Mh is new Sample.Menu_Demo.Handler (My_Driver);
90
91          I : Item_Array_Access := new Item_Array'
92            (New_Item ("Curses Core Demo"),
93             New_Item ("Menu Demo"),
94             New_Item ("Form Demo"),
95             New_Item ("Text IO Demo"),
96             Null_Item);
97
98          M : Menu := New_Menu (I);
99
100          D1, D2 : User_Access;
101          I1, I2 : User_Access;
102
103          function My_Driver (M : Menu;
104                              K : Key_Code;
105                              Pan : Panel) return Boolean
106          is
107             Idx : constant Positive := Get_Index (Current (M));
108          begin
109             if K in User_Key_Code'Range then
110                if K = QUIT then
111                   return True;
112                elsif K = SELECT_ITEM then
113                   if Idx in 1 .. 4 then
114                      Hide (Pan);
115                      Update_Panels;
116                   end if;
117                   case Idx is
118                      when 1 => Sample.Curses_Demo.Demo;
119                      when 2 => Sample.Menu_Demo.Demo;
120                      when 3 => Sample.Form_Demo.Demo;
121                      when 4 => Sample.Text_IO_Demo.Demo;
122                      when others => null;
123                   end case;
124                   if Idx in 1 .. 4 then
125                      Top (Pan);
126                      Show (Pan);
127                      Update_Panels;
128                      Update_Screen;
129                   end if;
130                end if;
131             end if;
132             return False;
133          end My_Driver;
134
135       begin
136
137          if (1 + Item_Count (M)) /= I'Length then
138             raise Constraint_Error;
139          end if;
140
141          D1 := new User_Data'(Data => 4711);
142          Ud.Set_User_Data (M, D1);
143
144          I1 := new User_Data'(Data => 1174);
145          Id.Set_User_Data (I (1), I1);
146
147          Set_Spacing (Men => M, Row => 2);
148
149          Default_Labels;
150          Notepad ("MAINPAD");
151
152          Mh.Drive_Me (M, " Demo ");
153
154          Ud.Get_User_Data (M, D2);
155          pragma Assert (D1 = D2);
156          pragma Assert (D1.Data = D2.Data);
157
158          Id.Get_User_Data (I (1), I2);
159          pragma Assert (I1 = I2);
160          pragma Assert (I1.Data = I2.Data);
161
162          Delete (M);
163          Free (I, True);
164       end Main_Menu;
165
166    begin
167       Initialize (PC_Style_With_Index);
168       Init_Header_Handler;
169       Init_Screen;
170
171       if Has_Colors then
172          Start_Color;
173
174          Init_Pair (Pair => Default_Colors,  Fore => Black,   Back => White);
175          Init_Pair (Pair => Menu_Back_Color, Fore => Black,   Back => Cyan);
176          Init_Pair (Pair => Menu_Fore_Color, Fore => Red,     Back => Cyan);
177          Init_Pair (Pair => Menu_Grey_Color, Fore => White,   Back => Cyan);
178          Init_Pair (Pair => Notepad_Color,   Fore => Black,   Back => Yellow);
179          Init_Pair (Pair => Help_Color,      Fore => Blue,    Back => Cyan);
180          Init_Pair (Pair => Form_Back_Color, Fore => Black,   Back => Cyan);
181          Init_Pair (Pair => Form_Fore_Color, Fore => Red,     Back => Cyan);
182          Init_Pair (Pair => Header_Color,    Fore => Black,   Back => Green);
183
184          Set_Background (Ch => (Color => Default_Colors,
185                                 Attr  => Normal_Video,
186                                 Ch    => ' '));
187          Set_Character_Attributes (Attr  => Normal_Video,
188                                    Color => Default_Colors);
189          Erase;
190
191          Set_Soft_Label_Key_Attributes (Color => Header_Color);
192          --  This propagates the attributes to the label window
193          Clear_Soft_Label_Keys; Restore_Soft_Label_Keys;
194       end if;
195
196       Init_Keyboard_Handler;
197
198       Set_Echo_Mode (FALSE);
199       Set_Raw_Mode;
200       Set_Meta_Mode;
201       Set_KeyPad_Mode;
202
203       --  Initialize the Function Key Environment
204       --  We have some fixed key throughout this sample
205       Main_Menu;
206       End_Windows;
207
208    exception
209       when Event : others =>
210          Terminal_Interface.Curses.End_Windows;
211          Text_IO.Put ("Exception: ");
212          Text_IO.Put (Exception_Name (Event));
213          Text_IO.New_Line;
214          GNAT.OS_Lib.OS_Exit (1);
215
216    end Whow;
217
218 end Sample;