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