1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000-2011,2018 Free Software Foundation, Inc. --
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: --
20 -- The above copyright notice and this permission notice shall be included --
21 -- in all copies or substantial portions of the Software. --
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. --
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 --
35 ------------------------------------------------------------------------------
36 -- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
39 -- $Date: 2018/07/07 23:31:55 $
40 -- Binding Version 01.00
41 ------------------------------------------------------------------------------
42 -- Windows and scrolling tester.
43 -- Demonstrate windows
45 with Ada.Strings.Fixed;
48 with ncurses2.util; use ncurses2.util;
49 with ncurses2.genericPuts;
50 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
51 with Terminal_Interface.Curses.Mouse; use Terminal_Interface.Curses.Mouse;
52 with Terminal_Interface.Curses.PutWin; use Terminal_Interface.Curses.PutWin;
54 with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
55 with Ada.Streams; use Ada.Streams;
57 procedure ncurses2.acs_and_scroll is
59 Macro_Quit : constant Key_Code := Character'Pos ('Q') mod 16#20#;
60 Macro_Escape : constant Key_Code := Character'Pos ('[') mod 16#20#;
62 Quit : constant Key_Code := CTRL ('Q');
63 Escape : constant Key_Code := CTRL ('[');
65 Botlines : constant Line_Position := 4;
73 type FrameA is access Frame;
76 dumpfile : constant String := "screendump";
78 procedure Outerbox (ul, lr : pair; onoff : Boolean);
79 function HaveKeyPad (w : Window) return Boolean;
80 function HaveScroll (w : Window) return Boolean;
81 procedure newwin_legend (curpw : Window);
82 procedure transient (curpw : Window; msg : String);
83 procedure newwin_report (win : Window := Standard_Window);
84 procedure selectcell (uli : Line_Position;
85 ulj : Column_Position;
87 lrj : Column_Position;
90 function getwindow return Window;
91 procedure newwin_move (win : Window;
93 dx : Column_Position);
94 function delete_framed (fp : FrameA; showit : Boolean) return FrameA;
97 -- I wish there was a standard library linked list. Oh well.
109 procedure Outerbox (ul, lr : pair; onoff : Boolean) is
112 -- Note the fix of an obscure bug
113 -- try making a 1x1 box then enlarging it, the is a blank
114 -- upper left corner!
115 Add (Line => ul.y - 1, Column => ul.x - 1,
116 Ch => ACS_Map (ACS_Upper_Left_Corner));
117 Add (Line => ul.y - 1, Column => lr.x + 1,
118 Ch => ACS_Map (ACS_Upper_Right_Corner));
119 Add (Line => lr.y + 1, Column => lr.x + 1,
120 Ch => ACS_Map (ACS_Lower_Right_Corner));
121 Add (Line => lr.y + 1, Column => ul.x - 1,
122 Ch => ACS_Map (ACS_Lower_Left_Corner));
124 Move_Cursor (Line => ul.y - 1, Column => ul.x);
125 Horizontal_Line (Line_Symbol => ACS_Map (ACS_Horizontal_Line),
126 Line_Size => Integer (lr.x - ul.x) + 1);
127 Move_Cursor (Line => ul.y, Column => ul.x - 1);
128 Vertical_Line (Line_Symbol => ACS_Map (ACS_Vertical_Line),
129 Line_Size => Integer (lr.y - ul.y) + 1);
130 Move_Cursor (Line => lr.y + 1, Column => ul.x);
131 Horizontal_Line (Line_Symbol => ACS_Map (ACS_Horizontal_Line),
132 Line_Size => Integer (lr.x - ul.x) + 1);
133 Move_Cursor (Line => ul.y, Column => lr.x + 1);
134 Vertical_Line (Line_Symbol => ACS_Map (ACS_Vertical_Line),
135 Line_Size => Integer (lr.y - ul.y) + 1);
137 Add (Line => ul.y - 1, Column => ul.x - 1, Ch => ' ');
138 Add (Line => ul.y - 1, Column => lr.x + 1, Ch => ' ');
139 Add (Line => lr.y + 1, Column => lr.x + 1, Ch => ' ');
140 Add (Line => lr.y + 1, Column => ul.x - 1, Ch => ' ');
142 Move_Cursor (Line => ul.y - 1, Column => ul.x);
143 Horizontal_Line (Line_Symbol => Blank2,
144 Line_Size => Integer (lr.x - ul.x) + 1);
145 Move_Cursor (Line => ul.y, Column => ul.x - 1);
146 Vertical_Line (Line_Symbol => Blank2,
147 Line_Size => Integer (lr.y - ul.y) + 1);
148 Move_Cursor (Line => lr.y + 1, Column => ul.x);
149 Horizontal_Line (Line_Symbol => Blank2,
150 Line_Size => Integer (lr.x - ul.x) + 1);
151 Move_Cursor (Line => ul.y, Column => lr.x + 1);
152 Vertical_Line (Line_Symbol => Blank2,
153 Line_Size => Integer (lr.y - ul.y) + 1);
157 function HaveKeyPad (w : Window) return Boolean is
159 return Get_KeyPad_Mode (w);
161 when Curses_Exception => return False;
164 function HaveScroll (w : Window) return Boolean is
166 return Scrolling_Allowed (w);
168 when Curses_Exception => return False;
171 procedure newwin_legend (curpw : Window) is
173 package p is new genericPuts (200);
177 type string_a is access String;
181 code : Integer range 0 .. 3;
184 legend : constant array (Positive range <>) of rrr :=
187 new String'("^C = create window"), 0
190 new String'("^N = next window"), 0
193 new String'("^P = previous window"), 0
196 new String'("^F = scroll forward"), 0
199 new String'("^B = scroll backward"), 0
202 new String'("^K = keypad(%s)"), 1
205 new String'("^S = scrollok(%s)"), 2
208 new String'("^W = save window to file"), 0
211 new String'("^R = restore window"), 0
214 new String'("^X = resize"), 0
217 new String'("^Q%s = exit"), 3
221 buf : Bounded_String;
222 do_keypad : constant Boolean := HaveKeyPad (curpw);
223 do_scroll : constant Boolean := HaveScroll (curpw);
230 Move_Cursor (Line => Lines - 4, Column => 0);
231 for n in legend'Range loop
232 pos := Ada.Strings.Fixed.Index (Source => legend (n).msg.all,
234 buf := To_Bounded_String (legend (n).msg.all);
235 case legend (n).code is
239 Replace_Slice (buf, pos, pos + 1, "yes");
241 Replace_Slice (buf, pos, pos + 1, "no");
245 Replace_Slice (buf, pos, pos + 1, "yes");
247 Replace_Slice (buf, pos, pos + 1, "no");
251 Replace_Slice (buf, pos, pos + 1, "/ESC");
253 Replace_Slice (buf, pos, pos + 1, "");
256 Get_Cursor_Position (Line => mypair.y, Column => mypair.x);
257 if Columns < mypair.x + 3 + Column_Position (Length (buf)) then
259 elsif n /= 1 then -- n /= legen'First
264 Clear_To_End_Of_Line;
267 procedure transient (curpw : Window; msg : String) is
269 newwin_legend (curpw);
271 Add (Line => Lines - 1, Column => 0, Str => msg);
273 Nap_Milli_Seconds (1000);
276 Move_Cursor (Line => Lines - 1, Column => 0);
278 if HaveKeyPad (curpw) then
279 Add (Str => "Non-arrow");
281 Add (Str => "All other");
283 Add (Str => " characters are echoed, window should ");
284 if not HaveScroll (curpw) then
287 Add (Str => "scroll");
289 Clear_To_End_Of_Line;
292 procedure newwin_report (win : Window := Standard_Window) is
296 tmp2a : String (1 .. 2);
297 tmp2b : String (1 .. 2);
299 if win /= Standard_Window then
302 Get_Cursor_Position (win, y, x);
303 Move_Cursor (Line => Lines - 1, Column => Columns - 17);
304 Put (tmp2a, Integer (y));
305 Put (tmp2b, Integer (x));
306 Add (Str => "Y = " & tmp2a & " X = " & tmp2b);
307 if win /= Standard_Window then
310 Move_Cursor (win, y, x);
314 procedure selectcell (uli : Line_Position;
315 ulj : Column_Position;
317 lrj : Column_Position;
322 i : Line_Position := 0;
323 j : Column_Position := 0;
324 si : constant Line_Position := lri - uli + 1;
325 sj : constant Column_Position := lrj - ulj + 1;
330 Move_Cursor (Line => uli + i, Column => ulj + j);
338 -- on the same line macro calls interfere due to the # comment
339 -- this is needed because keypad off affects all windows.
340 -- try removing the ESCAPE and see what happens.
345 -- same as i := i - 1 because of Modulus arithmetic,
346 -- on Line_Position, which is a Natural
347 -- the C version uses this form too, interestingly.
359 Button : Mouse_Button;
360 State : Button_State;
364 Get_Event (Event => event,
369 if y > uli and x > ulj then
372 -- same as when others =>
394 function getwindow return Window is
399 Move_Cursor (Line => 0, Column => 0);
400 Clear_To_End_Of_Line;
401 Add (Str => "Use arrows to move cursor, anything else to mark corner 1");
403 selectcell (2, 1, Lines - Botlines - 2, Columns - 2, ul, result);
407 Add (Line => ul.y - 1, Column => ul.x - 1,
408 Ch => ACS_Map (ACS_Upper_Left_Corner));
409 Move_Cursor (Line => 0, Column => 0);
410 Clear_To_End_Of_Line;
411 Add (Str => "Use arrows to move cursor, anything else to mark corner 2");
413 selectcell (ul.y, ul.x, Lines - Botlines - 2, Columns - 2, lr, result);
418 rwindow := Sub_Window (Number_Of_Lines => lr.y - ul.y + 1,
419 Number_Of_Columns => lr.x - ul.x + 1,
420 First_Line_Position => ul.y,
421 First_Column_Position => ul.x);
423 Outerbox (ul, lr, True);
428 Move_Cursor (Line => 0, Column => 0);
429 Clear_To_End_Of_Line;
433 procedure newwin_move (win : Window;
435 dx : Column_Position) is
436 cur_y, max_y : Line_Position;
437 cur_x, max_x : Column_Position;
439 Get_Cursor_Position (win, cur_y, cur_x);
440 Get_Size (win, max_y, max_x);
441 cur_x := Column_Position'Min (Column_Position'Max (cur_x + dx, 0),
443 cur_y := Line_Position'Min (Line_Position'Max (cur_y + dy, 0),
446 Move_Cursor (win, Line => cur_y, Column => cur_x);
449 function delete_framed (fp : FrameA; showit : Boolean) return FrameA is
452 fp.all.last.all.next := fp.all.next;
453 fp.all.next.all.last := fp.all.last;
457 Refresh (fp.all.wind);
459 Delete (fp.all.wind);
461 if fp = fp.all.next then
470 Mask : Event_Mask := No_Events;
477 Register_Reportable_Event (
481 Mask2 := Start_Mouse (Mask);
484 Set_Raw_Mode (SwitchOn => True);
486 transient (Standard_Window, "");
488 when Character'Pos ('c') mod 16#20# => -- Ctrl('c')
490 neww : constant FrameA := new Frame'(null, null,
494 neww.all.wind := getwindow;
495 if neww.all.wind = Null_Window then
497 -- was goto breakout; ha ha ha
500 if current = null then
501 neww.all.next := neww;
502 neww.all.last := neww;
504 neww.all.next := current.all.next;
505 neww.all.last := current;
506 neww.all.last.all.next := neww;
507 neww.all.next.all.last := neww;
511 Set_KeyPad_Mode (current.all.wind, True);
512 current.all.do_keypad := HaveKeyPad (current.all.wind);
513 current.all.do_scroll := HaveScroll (current.all.wind);
516 when Character'Pos ('N') mod 16#20# => -- Ctrl('N')
517 if current /= null then
518 current := current.all.next;
520 when Character'Pos ('P') mod 16#20# => -- Ctrl('P')
521 if current /= null then
522 current := current.all.last;
524 when Character'Pos ('F') mod 16#20# => -- Ctrl('F')
525 if current /= null and then HaveScroll (current.all.wind) then
526 Scroll (current.all.wind, 1);
528 when Character'Pos ('B') mod 16#20# => -- Ctrl('B')
529 if current /= null and then HaveScroll (current.all.wind) then
530 -- The C version of Scroll may return ERR which is ignored
531 -- we need to avoid the exception
532 -- with the 'and HaveScroll(current.wind)'
533 Scroll (current.all.wind, -1);
535 when Character'Pos ('K') mod 16#20# => -- Ctrl('K')
536 if current /= null then
537 current.all.do_keypad := not current.all.do_keypad;
538 Set_KeyPad_Mode (current.all.wind, current.all.do_keypad);
540 when Character'Pos ('S') mod 16#20# => -- Ctrl('S')
541 if current /= null then
542 current.all.do_scroll := not current.all.do_scroll;
543 Allow_Scrolling (current.all.wind, current.all.do_scroll);
545 when Character'Pos ('W') mod 16#20# => -- Ctrl('W')
546 if current /= current.all.next then
547 Create (f, Name => dumpfile); -- TODO error checking
548 if not Is_Open (f) then
549 raise Curses_Exception;
551 Put_Window (current.all.wind, f);
553 current := delete_framed (current, True);
555 when Character'Pos ('R') mod 16#20# => -- Ctrl('R')
557 neww : FrameA := new Frame'(null, null, False, False,
560 Open (f, Mode => In_File, Name => dumpfile);
561 neww := new Frame'(null, null, False, False, Null_Window);
563 neww.all.next := current.all.next;
564 neww.all.last := current;
565 neww.all.last.all.next := neww;
566 neww.all.next.all.last := neww;
568 neww.all.wind := Get_Window (f);
571 Refresh (neww.all.wind);
573 when Character'Pos ('X') mod 16#20# => -- Ctrl('X')
574 if current /= null then
577 mx : Column_Position;
581 Move_Cursor (Line => 0, Column => 0);
582 Clear_To_End_Of_Line;
583 Add (Str => "Use arrows to move cursor, anything else " &
584 "to mark new corner");
587 Get_Window_Position (current.all.wind, ul.y, ul.x);
589 selectcell (ul.y, ul.x, Lines - Botlines - 2, Columns - 2,
592 -- the C version had a goto. I refuse gotos.
595 Get_Size (current.all.wind, lr.y, lr.x);
596 lr.y := lr.y + ul.y - 1;
597 lr.x := lr.x + ul.x - 1;
598 Outerbox (ul, lr, False);
599 Refresh_Without_Update;
601 Get_Size (current.all.wind, my, mx);
602 if my > tmp.y - ul.y then
603 Get_Cursor_Position (current.all.wind, lr.y, lr.x);
604 Move_Cursor (current.all.wind, tmp.y - ul.y + 1, 0);
605 Clear_To_End_Of_Screen (current.all.wind);
606 Move_Cursor (current.all.wind, lr.y, lr.x);
608 if mx > tmp.x - ul.x then
609 for i in 0 .. my - 1 loop
610 Move_Cursor (current.all.wind, i, tmp.x - ul.x + 1);
611 Clear_To_End_Of_Line (current.all.wind);
614 Refresh_Without_Update (current.all.wind);
617 -- The C version passes invalid args to resize
618 -- which returns an ERR. For Ada we avoid the exception.
619 if lr.y /= ul.y and lr.x /= ul.x then
620 Resize (current.all.wind, lr.y - ul.y + 0,
624 Get_Window_Position (current.all.wind, ul.y, ul.x);
625 Get_Size (current.all.wind, lr.y, lr.x);
626 lr.y := lr.y + ul.y - 1;
627 lr.x := lr.x + ul.x - 1;
628 Outerbox (ul, lr, True);
629 Refresh_Without_Update;
631 Refresh_Without_Update (current.all.wind);
632 Move_Cursor (Line => 0, Column => 0);
633 Clear_To_End_Of_Line;
639 declare tmp : pair; tmpbool : Boolean;
641 -- undocumented --- use this to test area clears
642 selectcell (0, 0, Lines - 1, Columns - 1, tmp, tmpbool);
643 Clear_To_End_Of_Screen;
646 when Key_Cursor_Up =>
647 newwin_move (current.all.wind, -1, 0);
648 when Key_Cursor_Down =>
649 newwin_move (current.all.wind, 1, 0);
650 when Key_Cursor_Left =>
651 newwin_move (current.all.wind, 0, -1);
652 when Key_Cursor_Right =>
653 newwin_move (current.all.wind, 0, 1);
654 when Key_Backspace | Key_Delete_Char =>
660 Get_Cursor_Position (current.all.wind, y, x);
662 -- I got tricked by the -1 = Max_Natural - 1 result
664 if not (x = 0 and y = 0) then
667 Get_Size (current.all.wind, tmp, x);
670 Delete_Character (current.all.wind, y, x);
675 if current /= null then
678 Add (current.all.wind, Ch => Code_To_Char (c));
680 when Curses_Exception => null;
681 -- this happens if we are at the
682 -- lower right of a window and add a character.
688 newwin_report (current.all.wind);
689 if current /= null then
690 usescr := current.all.wind;
692 usescr := Standard_Window;
695 c := Getchar (usescr);
696 exit when c = Quit or (c = Escape and HaveKeyPad (usescr));
697 -- TODO when does c = ERR happen?
700 -- TODO while current /= null loop
701 -- current := delete_framed(current, False);
704 Allow_Scrolling (Mode => True);
707 Set_Raw_Mode (SwitchOn => True);
711 end ncurses2.acs_and_scroll;