]> ncurses.scripts.mit.edu Git - ncurses.git/blob - Ada95/samples/ncurses2-demo_panels.adb
ncurses 5.6 - patch 20080503
[ncurses.git] / Ada95 / samples / ncurses2-demo_panels.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                       GNAT ncurses Binding Samples                       --
4 --                                                                          --
5 --                                 ncurses                                  --
6 --                                                                          --
7 --                                 B O D Y                                  --
8 --                                                                          --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000,2004 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: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
37 --  Version Control
38 --  $Revision: 1.4 $
39 --  $Date: 2004/08/21 21:37:00 $
40 --  Binding Version 01.00
41 ------------------------------------------------------------------------------
42 with ncurses2.util; use ncurses2.util;
43 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
44 with Terminal_Interface.Curses.Panels; use Terminal_Interface.Curses.Panels;
45 with Terminal_Interface.Curses.Panels.User_Data;
46
47 with ncurses2.genericPuts;
48
49 procedure ncurses2.demo_panels (nap_mseci : Integer) is
50    use Int_IO;
51
52    function  mkpanel (color : Color_Number;
53                       rows  : Line_Count;
54                       cols  : Column_Count;
55                       tly   : Line_Position;
56                       tlx   : Column_Position) return Panel;
57    procedure rmpanel (pan : in out Panel);
58    procedure pflush;
59    procedure wait_a_while (msec : Integer);
60    procedure saywhat (text : String);
61    procedure fill_panel (pan : Panel);
62
63    nap_msec : Integer := nap_mseci;
64
65    function mkpanel (color : Color_Number;
66                      rows  : Line_Count;
67                      cols  : Column_Count;
68                      tly   : Line_Position;
69                      tlx   : Column_Position) return Panel is
70       win : Window;
71       pan : Panel := Null_Panel;
72    begin
73       win := New_Window (rows, cols, tly, tlx);
74       if Null_Window /= win then
75          pan := New_Panel (win);
76          if pan = Null_Panel then
77             Delete (win);
78          elsif Has_Colors then
79             declare
80                fg, bg : Color_Number;
81             begin
82                if color = Blue then
83                   fg := White;
84                else
85                   fg := Black;
86                end if;
87                bg := color;
88                Init_Pair (Color_Pair (color), fg, bg);
89                Set_Background (win, (Ch => ' ',
90                                      Attr => Normal_Video,
91                                      Color => Color_Pair (color)));
92             end;
93          else
94             Set_Background (win, (Ch => ' ',
95                                   Attr => (Bold_Character => True,
96                                            others => False),
97                                   Color => Color_Pair (color)));
98          end if;
99       end if;
100       return pan;
101    end mkpanel;
102
103    procedure rmpanel (pan : in out Panel) is
104       win : Window := Panel_Window (pan);
105    begin
106       Delete (pan);
107       Delete (win);
108    end rmpanel;
109
110    procedure pflush is
111    begin
112       Update_Panels;
113       Update_Screen;
114    end pflush;
115
116    procedure wait_a_while (msec : Integer) is
117    begin
118       --  The C version had some #ifdef blocks here
119       if msec = 1 then
120          Getchar;
121       else
122          Nap_Milli_Seconds (msec);
123       end if;
124    end wait_a_while;
125
126    procedure saywhat (text : String) is
127    begin
128       Move_Cursor (Line => Lines - 1, Column => 0);
129       Clear_To_End_Of_Line;
130       Add (Str => text);
131    end saywhat;
132
133    --  from sample-curses_demo.adb
134    type User_Data is new String (1 .. 2);
135    type User_Data_Access is access all User_Data;
136    package PUD is new Panels.User_Data (User_Data, User_Data_Access);
137
138    use PUD;
139
140    procedure fill_panel (pan : Panel) is
141       win : constant Window := Panel_Window (pan);
142       num : constant Character := Get_User_Data (pan) (2);
143       tmp6 : String (1 .. 6) := "-panx-";
144       maxy : Line_Count;
145       maxx : Column_Count;
146
147    begin
148       Move_Cursor (win, 1, 1);
149       tmp6 (5) := num;
150       Add (win, Str => tmp6);
151       Clear_To_End_Of_Line (win);
152       Box (win);
153       Get_Size (win, maxy, maxx);
154       for y in 2 .. maxy - 2 loop
155          for x in 1 .. maxx - 2 loop
156             Move_Cursor (win, y, x);
157             Add (win, num);
158          end loop;
159       end loop;
160    end fill_panel;
161
162    modstr : constant array (0 .. 5) of String (1 .. 5) :=
163      ("test ",
164       "TEST ",
165       "(**) ",
166       "*()* ",
167       "<--> ",
168       "LAST "
169       );
170
171    package p is new ncurses2.genericPuts (1024);
172    use p;
173    use p.BS;
174    --  the C version said register int y, x;
175    tmpb : BS.Bounded_String;
176
177 begin
178    Refresh;
179
180    for y in 0 .. Integer (Lines - 2) loop
181       for x in 0 .. Integer (Columns - 1) loop
182          myPut (tmpb, (y + x) mod 10);
183          myAdd (Str => tmpb);
184       end loop;
185    end loop;
186    for y in 0 .. 4 loop
187       declare
188          p1, p2, p3, p4, p5 : Panel;
189          U1 : constant User_Data_Access := new User_Data'("p1");
190          U2 : constant User_Data_Access := new User_Data'("p2");
191          U3 : constant User_Data_Access := new User_Data'("p3");
192          U4 : constant User_Data_Access := new User_Data'("p4");
193          U5 : constant User_Data_Access := new User_Data'("p5");
194
195       begin
196          p1 := mkpanel (Red, Lines / 2 - 2, Columns / 8 + 1, 0, 0);
197          Set_User_Data (p1, U1);
198          p2 := mkpanel (Green, Lines / 2 + 1, Columns / 7, Lines / 4,
199                         Columns / 10);
200          Set_User_Data (p2, U2);
201          p3 := mkpanel (Yellow, Lines / 4, Columns / 10, Lines / 2,
202                         Columns / 9);
203          Set_User_Data (p3, U3);
204          p4 := mkpanel (Blue, Lines / 2 - 2, Columns / 8,  Lines / 2 - 2,
205                         Columns / 3);
206          Set_User_Data (p4, U4);
207          p5 := mkpanel (Magenta, Lines / 2 - 2, Columns / 8,  Lines / 2,
208                         Columns / 2 - 2);
209          Set_User_Data (p5, U5);
210
211          fill_panel (p1);
212          fill_panel (p2);
213          fill_panel (p3);
214          fill_panel (p4);
215          fill_panel (p5);
216          Hide (p4);
217          Hide (p5);
218          pflush;
219          saywhat ("press any key to continue");
220          wait_a_while (nap_msec);
221
222          saywhat ("h3 s1 s2 s4 s5; press any key to continue");
223          Move (p1, 0, 0);
224          Hide (p3);
225          Show (p1);
226          Show (p2);
227          Show (p4);
228          Show (p5);
229          pflush;
230          wait_a_while (nap_msec);
231
232          saywhat ("s1; press any key to continue");
233          Show (p1);
234          pflush;
235          wait_a_while (nap_msec);
236
237          saywhat ("s2; press any key to continue");
238          Show (p2);
239          pflush;
240          wait_a_while (nap_msec);
241
242          saywhat ("m2; press any key to continue");
243          Move (p2, Lines / 3 + 1, Columns / 8);
244          pflush;
245          wait_a_while (nap_msec);
246
247          saywhat ("s3;");
248          Show (p3);
249          pflush;
250          wait_a_while (nap_msec);
251
252          saywhat ("m3; press any key to continue");
253          Move (p3, Lines / 4 + 1, Columns / 15);
254          pflush;
255          wait_a_while (nap_msec);
256
257          saywhat ("b3; press any key to continue");
258          Bottom (p3);
259          pflush;
260          wait_a_while (nap_msec);
261
262          saywhat ("s4; press any key to continue");
263          Show (p4);
264          pflush;
265          wait_a_while (nap_msec);
266
267          saywhat ("s5; press any key to continue");
268          Show (p5);
269          pflush;
270          wait_a_while (nap_msec);
271
272          saywhat ("t3; press any key to continue");
273          Top (p3);
274          pflush;
275          wait_a_while (nap_msec);
276
277          saywhat ("t1; press any key to continue");
278          Top (p1);
279          pflush;
280          wait_a_while (nap_msec);
281
282          saywhat ("t2; press any key to continue");
283          Top (p2);
284          pflush;
285          wait_a_while (nap_msec);
286
287          saywhat ("t3; press any key to continue");
288          Top (p3);
289          pflush;
290          wait_a_while (nap_msec);
291
292          saywhat ("t4; press any key to continue");
293          Top (p4);
294          pflush;
295          wait_a_while (nap_msec);
296
297          for itmp in  0 ..  5 loop
298             declare
299                w4 : constant Window := Panel_Window (p4);
300                w5 : constant Window := Panel_Window (p5);
301             begin
302
303                saywhat ("m4; press any key to continue");
304                Move_Cursor (w4, Lines / 8, 1);
305                Add (w4, modstr (itmp));
306                Move (p4, Lines / 6, Column_Position (itmp) * (Columns / 8));
307                Move_Cursor (w5, Lines / 6, 1);
308                Add (w5, modstr (itmp));
309                pflush;
310                wait_a_while (nap_msec);
311
312                saywhat ("m5; press any key to continue");
313                Move_Cursor (w4, Lines / 6, 1);
314                Add (w4, modstr (itmp));
315                Move (p5, Lines / 3 - 1, (Column_Position (itmp) * 10) + 6);
316                Move_Cursor (w5, Lines / 8, 1);
317                Add (w5, modstr (itmp));
318                pflush;
319                wait_a_while (nap_msec);
320             end;
321          end loop;
322
323          saywhat ("m4; press any key to continue");
324          Move (p4, Lines / 6, 6 * (Columns / 8));
325          --  Move(p4, Lines / 6, itmp * (Columns / 8));
326          pflush;
327          wait_a_while (nap_msec);
328
329          saywhat ("t5; press any key to continue");
330          Top (p5);
331          pflush;
332          wait_a_while (nap_msec);
333
334          saywhat ("t2; press any key to continue");
335          Top (p2);
336          pflush;
337          wait_a_while (nap_msec);
338
339          saywhat ("t1; press any key to continue");
340          Top (p1);
341          pflush;
342          wait_a_while (nap_msec);
343
344          saywhat ("d2; press any key to continue");
345          rmpanel (p2);
346          pflush;
347          wait_a_while (nap_msec);
348
349          saywhat ("h3; press any key to continue");
350          Hide (p3);
351          pflush;
352          wait_a_while (nap_msec);
353
354          saywhat ("d1; press any key to continue");
355          rmpanel (p1);
356          pflush;
357          wait_a_while (nap_msec);
358
359          saywhat ("d4; press any key to continue");
360          rmpanel (p4);
361          pflush;
362          wait_a_while (nap_msec);
363
364          saywhat ("d5; press any key to continue");
365          rmpanel (p5);
366          pflush;
367          wait_a_while (nap_msec);
368          if nap_msec = 1 then
369             exit;
370          else
371             nap_msec := 100;
372          end if;
373
374       end;
375    end loop;
376
377    Erase;
378    End_Windows;
379
380 end ncurses2.demo_panels;