]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/sample.adb
ncurses 4.1
[ncurses.git] / Ada95 / samples / sample.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                                 Sample                                   --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 --  Version 00.92                                                           --
10 --                                                                          --
11 --  The ncurses Ada95 binding is copyrighted 1996 by                        --
12 --  Juergen Pfeifer, Email: Juergen.Pfeifer@T-Online.de                     --
13 --                                                                          --
14 --  Permission is hereby granted to reproduce and distribute this           --
15 --  binding by any means and for any fee, whether alone or as part          --
16 --  of a larger distribution, in source or in binary form, PROVIDED         --
17 --  this notice is included with any such distribution, and is not          --
18 --  removed from any of its header files. Mention of ncurses and the        --
19 --  author of this binding in any applications linked with it is            --
20 --  highly appreciated.                                                     --
21 --                                                                          --
22 --  This binding comes AS IS with no warranty, implied or expressed.        --
23 ------------------------------------------------------------------------------
24 --  Version Control
25 --  $Revision: 1.5 $
26 ------------------------------------------------------------------------------
27 with Text_IO;
28
29 with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
30 with Ada.Exceptions; use Ada.Exceptions;
31
32 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
33 with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
34 with Terminal_Interface.Curses.Menus; use Terminal_Interface.Curses.Menus;
35 with Terminal_Interface.Curses.Menus.Menu_User_Data;
36 with Terminal_Interface.Curses.Menus.Item_User_Data;
37
38 with Sample.Manifest; use Sample.Manifest;
39 with Sample.Helpers; use Sample.Helpers;
40 with Sample.Function_Key_Setting; use Sample.Function_Key_Setting;
41 with Sample.Keyboard_Handler; use Sample.Keyboard_Handler;
42 with Sample.Header_Handler; use Sample.Header_Handler;
43 with Sample.Explanation; use Sample.Explanation;
44
45 with Sample.Menu_Demo.Handler;
46 with Sample.Curses_Demo;
47 with Sample.Form_Demo;
48 with Sample.Menu_Demo;
49 with Sample.Text_IO_Demo;
50
51 with GNAT.OS_Lib;
52
53 package body Sample is
54
55    type User_Data is
56       record
57          Data : Integer;
58       end record;
59    type User_Access is access User_Data;
60
61    package Ud is new
62      Terminal_Interface.Curses.Menus.Menu_User_Data
63      (User_Data, User_Access);
64
65    package Id is new
66      Terminal_Interface.Curses.Menus.Item_User_Data
67      (User_Data, User_Access);
68
69    procedure Whow is
70       procedure Main_Menu;
71       procedure Main_Menu
72       is
73          function My_Driver (M : Menu;
74                              K : Key_Code;
75                              Pan : Panel) return Boolean;
76
77          package Mh is new Sample.Menu_Demo.Handler (My_Driver);
78
79          I : constant Item_Array (1 .. 4) :=
80            (New_Item ("Curses Core Demo"),
81             New_Item ("Menu Demo"),
82             New_Item ("Form Demo"),
83             New_Item ("Text IO Demo"));
84
85          M : Menu := New_Menu (I);
86
87          D1, D2 : User_Access;
88          I1, I2 : User_Access;
89
90          function My_Driver (M : Menu;
91                              K : Key_Code;
92                              Pan : Panel) return Boolean
93          is
94             Idx : constant Positive := Get_Index (Current (M));
95          begin
96             if K in User_Key_Code'Range then
97                if K = QUIT then
98                   return True;
99                elsif K = SELECT_ITEM then
100                   if Idx in 1 .. 4 then
101                      Hide (Pan);
102                      Update_Panels;
103                   end if;
104                   case Idx is
105                      when 1 => Sample.Curses_Demo.Demo;
106                      when 2 => Sample.Menu_Demo.Demo;
107                      when 3 => Sample.Form_Demo.Demo;
108                      when 4 => Sample.Text_IO_Demo.Demo;
109                      when others => null;
110                   end case;
111                   if Idx in 1 .. 4 then
112                      Top (Pan);
113                      Show (Pan);
114                      Update_Panels;
115                      Update_Screen;
116                   end if;
117                end if;
118             end if;
119             return False;
120          end My_Driver;
121
122       begin
123
124          if Item_Count (M) /= I'Length then
125             raise Constraint_Error;
126          end if;
127
128          D1 := new User_Data'(Data => 4711);
129          Ud.Set_User_Data (M, D1);
130
131          I1 := new User_Data'(Data => 1174);
132          Id.Set_User_Data (I (1), I1);
133
134          Set_Spacing (Men => M, Row => 2);
135
136          Default_Labels;
137          Notepad ("MAINPAD");
138
139          Mh.Drive_Me (M, " Demo ");
140
141          Ud.Get_User_Data (M, D2);
142          pragma Assert (D1 = D2);
143          pragma Assert (D1.Data = D2.Data);
144
145          Id.Get_User_Data (I (1), I2);
146          pragma Assert (I1 = I2);
147          pragma Assert (I1.Data = I2.Data);
148
149          Delete (M);
150       end Main_Menu;
151
152    begin
153       Initialize (PC_Style_With_Index);
154       Init_Header_Handler;
155       Init_Screen;
156
157       if Has_Colors then
158          Start_Color;
159
160          Init_Pair (Pair => Default_Colors,  Fore => Black,   Back => White);
161          Init_Pair (Pair => Menu_Back_Color, Fore => Black,   Back => Cyan);
162          Init_Pair (Pair => Menu_Fore_Color, Fore => Red,     Back => Cyan);
163          Init_Pair (Pair => Menu_Grey_Color, Fore => White,   Back => Cyan);
164          Init_Pair (Pair => Notepad_Color,   Fore => Black,   Back => Yellow);
165          Init_Pair (Pair => Help_Color,      Fore => Blue,    Back => Cyan);
166          Init_Pair (Pair => Form_Back_Color, Fore => Black,   Back => Cyan);
167          Init_Pair (Pair => Form_Fore_Color, Fore => Red,     Back => Cyan);
168          Init_Pair (Pair => Header_Color,    Fore => Black,   Back => Green);
169
170          Set_Background (Ch => (Color => Default_Colors,
171                                 Attr  => Normal_Video,
172                                 Ch    => ' '));
173          Set_Character_Attributes (Attr  => Normal_Video,
174                                    Color => Default_Colors);
175          Erase;
176
177          Set_Soft_Label_Key_Attributes (Color => Header_Color);
178          --  This propagates the attributes to the label window
179          Clear_Soft_Label_Keys; Restore_Soft_Label_Keys;
180       end if;
181
182       Init_Keyboard_Handler;
183
184       Set_Echo_Mode (FALSE);
185       Set_Raw_Mode;
186       Set_Meta_Mode;
187       Set_KeyPad_Mode;
188
189       --  Initialize the Function Key Environment
190       --  We have some fixed key throughout this sample
191       Main_Menu;
192       End_Windows;
193
194    exception
195       when Event : others =>
196          Terminal_Interface.Curses.End_Windows;
197          Text_IO.Put ("Exception: ");
198          Text_IO.Put (Exception_Name (Event));
199          Text_IO.New_Line;
200          GNAT.OS_Lib.OS_Exit (1);
201
202    end Whow;
203
204 end Sample;