1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000-2004,2006 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: 2006/06/25 14:24:40 $
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;
96 use Ada.Streams.Stream_IO;
99 -- I wish there was a standard library linked list. Oh well.
111 procedure Outerbox (ul, lr : pair; onoff : Boolean) is
114 -- Note the fix of an obscure bug
115 -- try making a 1x1 box then enlarging it, the is a blank
116 -- upper left corner!
117 Add (Line => ul.y - 1, Column => ul.x - 1,
118 Ch => ACS_Map (ACS_Upper_Left_Corner));
119 Add (Line => ul.y - 1, Column => lr.x + 1,
120 Ch => ACS_Map (ACS_Upper_Right_Corner));
121 Add (Line => lr.y + 1, Column => lr.x + 1,
122 Ch => ACS_Map (ACS_Lower_Right_Corner));
123 Add (Line => lr.y + 1, Column => ul.x - 1,
124 Ch => ACS_Map (ACS_Lower_Left_Corner));
126 Move_Cursor (Line => ul.y - 1, Column => ul.x);
127 Horizontal_Line (Line_Symbol => ACS_Map (ACS_Horizontal_Line),
128 Line_Size => Integer (lr.x - ul.x) + 1);
129 Move_Cursor (Line => ul.y, Column => ul.x - 1);
130 Vertical_Line (Line_Symbol => ACS_Map (ACS_Vertical_Line),
131 Line_Size => Integer (lr.y - ul.y) + 1);
132 Move_Cursor (Line => lr.y + 1, Column => ul.x);
133 Horizontal_Line (Line_Symbol => ACS_Map (ACS_Horizontal_Line),
134 Line_Size => Integer (lr.x - ul.x) + 1);
135 Move_Cursor (Line => ul.y, Column => lr.x + 1);
136 Vertical_Line (Line_Symbol => ACS_Map (ACS_Vertical_Line),
137 Line_Size => Integer (lr.y - ul.y) + 1);
139 Add (Line => ul.y - 1, Column => ul.x - 1, Ch => ' ');
140 Add (Line => ul.y - 1, Column => lr.x + 1, Ch => ' ');
141 Add (Line => lr.y + 1, Column => lr.x + 1, Ch => ' ');
142 Add (Line => lr.y + 1, Column => ul.x - 1, Ch => ' ');
144 Move_Cursor (Line => ul.y - 1, Column => ul.x);
145 Horizontal_Line (Line_Symbol => Blank2,
146 Line_Size => Integer (lr.x - ul.x) + 1);
147 Move_Cursor (Line => ul.y, Column => ul.x - 1);
148 Vertical_Line (Line_Symbol => Blank2,
149 Line_Size => Integer (lr.y - ul.y) + 1);
150 Move_Cursor (Line => lr.y + 1, Column => ul.x);
151 Horizontal_Line (Line_Symbol => Blank2,
152 Line_Size => Integer (lr.x - ul.x) + 1);
153 Move_Cursor (Line => ul.y, Column => lr.x + 1);
154 Vertical_Line (Line_Symbol => Blank2,
155 Line_Size => Integer (lr.y - ul.y) + 1);
159 function HaveKeyPad (w : Window) return Boolean is
161 return Get_KeyPad_Mode (w);
163 when Curses_Exception => return False;
166 function HaveScroll (w : Window) return Boolean is
168 return Scrolling_Allowed (w);
170 when Curses_Exception => return False;
173 procedure newwin_legend (curpw : Window) is
175 package p is new genericPuts (200);
179 type string_a is access String;
183 code : Integer range 0 .. 3;
186 legend : constant array (Positive range <>) of rrr :=
189 new String'("^C = create window"), 0
192 new String'("^N = next window"), 0
195 new String'("^P = previous window"), 0
198 new String'("^F = scroll forward"), 0
201 new String'("^B = scroll backward"), 0
204 new String'("^K = keypad(%s)"), 1
207 new String'("^S = scrollok(%s)"), 2
210 new String'("^W = save window to file"), 0
213 new String'("^R = restore window"), 0
216 new String'("^X = resize"), 0
219 new String'("^Q%s = exit"), 3
223 buf : Bounded_String;
224 do_keypad : constant Boolean := HaveKeyPad (curpw);
225 do_scroll : constant Boolean := HaveScroll (curpw);
231 use Ada.Strings.Fixed;
234 Move_Cursor (Line => Lines - 4, Column => 0);
235 for n in legend'Range loop
236 pos := Ada.Strings.Fixed.Index (Source => legend (n).msg.all,
238 -- buf := (others => ' ');
239 buf := To_Bounded_String (legend (n).msg.all);
240 case legend (n).code is
244 Replace_Slice (buf, pos, pos + 1, "yes");
246 Replace_Slice (buf, pos, pos + 1, "no");
250 Replace_Slice (buf, pos, pos + 1, "yes");
252 Replace_Slice (buf, pos, pos + 1, "no");
256 Replace_Slice (buf, pos, pos + 1, "/ESC");
258 Replace_Slice (buf, pos, pos + 1, "");
261 Get_Cursor_Position (Line => mypair.y, Column => mypair.x);
262 if Columns < mypair.x + 3 + Column_Position (Length (buf)) then
264 elsif n /= 1 then -- n /= legen'First
269 Clear_To_End_Of_Line;
272 procedure transient (curpw : Window; msg : String) is
274 newwin_legend (curpw);
276 Add (Line => Lines - 1, Column => 0, Str => msg);
278 Nap_Milli_Seconds (1000);
281 Move_Cursor (Line => Lines - 1, Column => 0);
283 if HaveKeyPad (curpw) then
284 Add (Str => "Non-arrow");
286 Add (Str => "All other");
288 Add (str => " characters are echoed, window should ");
289 if not HaveScroll (curpw) then
292 Add (str => "scroll");
294 Clear_To_End_Of_Line;
297 procedure newwin_report (win : Window := Standard_Window) is
301 tmp2a : String (1 .. 2);
302 tmp2b : String (1 .. 2);
304 if win /= Standard_Window then
307 Get_Cursor_Position (win, y, x);
308 Move_Cursor (Line => Lines - 1, Column => Columns - 17);
309 Put (tmp2a, Integer (y));
310 Put (tmp2b, Integer (x));
311 Add (Str => "Y = " & tmp2a & " X = " & tmp2b);
312 if win /= Standard_Window then
315 Move_Cursor (win, y, x);
319 procedure selectcell (uli : Line_Position;
320 ulj : Column_Position;
322 lrj : Column_Position;
327 i : Line_Position := 0;
328 j : Column_Position := 0;
329 si : constant Line_Position := lri - uli + 1;
330 sj : constant Column_Position := lrj - ulj + 1;
335 Move_Cursor (Line => uli + i, Column => ulj + j);
343 -- on the same line macro calls interfere due to the # comment
344 -- this is needed because keypad off affects all windows.
345 -- try removing the ESCAPE and see what happens.
350 -- same as i := i - 1 because of Modulus arithetic,
351 -- on Line_Position, which is a Natural
352 -- the C version uses this form too, interestingly.
364 Button : Mouse_Button;
365 State : Button_State;
369 Get_Event (Event => event,
374 if y > uli and x > ulj then
377 -- same as when others =>
399 function getwindow return Window is
404 Move_Cursor (Line => 0, Column => 0);
405 Clear_To_End_Of_Line;
406 Add (Str => "Use arrows to move cursor, anything else to mark corner 1");
408 selectcell (2, 1, Lines - Botlines - 2, Columns - 2, ul, result);
412 Add (Line => ul.y - 1, Column => ul.x - 1,
413 Ch => ACS_Map (ACS_Upper_Left_Corner));
414 Move_Cursor (Line => 0, Column => 0);
415 Clear_To_End_Of_Line;
416 Add (Str => "Use arrows to move cursor, anything else to mark corner 2");
418 selectcell (ul.y, ul.x, Lines - Botlines - 2, Columns - 2, lr, result);
423 rwindow := Sub_Window (Number_Of_Lines => lr.y - ul.y + 1,
424 Number_Of_Columns => lr.x - ul.x + 1,
425 First_Line_Position => ul.y,
426 First_Column_Position => ul.x);
428 Outerbox (ul, lr, True);
433 Move_Cursor (Line => 0, Column => 0);
434 Clear_To_End_Of_Line;
438 procedure newwin_move (win : Window;
440 dx : Column_Position) is
441 cur_y, max_y : Line_Position;
442 cur_x, max_x : Column_Position;
444 Get_Cursor_Position (win, cur_y, cur_x);
445 Get_Size (win, max_y, max_x);
446 cur_x := Column_Position'Min (Column_Position'Max (cur_x + dx, 0),
448 cur_y := Line_Position'Min (Line_Position'Max (cur_y + dy, 0),
451 Move_Cursor (win, Line => cur_y, Column => cur_x);
454 function delete_framed (fp : FrameA; showit : Boolean) return FrameA is
457 fp.last.next := fp.next;
458 fp.next.last := fp.last;
475 Mask : Event_Mask := No_Events;
482 Register_Reportable_Event (
486 Mask2 := Start_Mouse (Mask);
489 Set_Raw_Mode (SwitchOn => True);
491 transient (Standard_Window, "");
493 when Character'Pos ('c') mod 16#20# => -- Ctrl('c')
495 neww : constant FrameA := new Frame'(null, null,
499 neww.wind := getwindow;
500 if neww.wind = Null_Window then
502 -- was goto breakout; ha ha ha
505 if current = null then
509 neww.next := current.next;
510 neww.last := current;
511 neww.last.next := neww;
512 neww.next.last := neww;
516 Set_KeyPad_Mode (current.wind, True);
517 current.do_keypad := HaveKeyPad (current.wind);
518 current.do_scroll := HaveScroll (current.wind);
521 when Character'Pos ('N') mod 16#20# => -- Ctrl('N')
522 if current /= null then
523 current := current.next;
525 when Character'Pos ('P') mod 16#20# => -- Ctrl('P')
526 if current /= null then
527 current := current.last;
529 when Character'Pos ('F') mod 16#20# => -- Ctrl('F')
530 if current /= null and HaveScroll (current.wind) then
531 Scroll (current.wind, 1);
533 when Character'Pos ('B') mod 16#20# => -- Ctrl('B')
534 if current /= null and HaveScroll (current.wind) then
535 -- The C version of Scroll may return ERR which is ignored
536 -- we need to avoid the exception
537 -- with the 'and HaveScroll(current.wind)'
538 Scroll (current.wind, -1);
540 when Character'Pos ('K') mod 16#20# => -- Ctrl('K')
541 if current /= null then
542 current.do_keypad := not current.do_keypad;
543 Set_KeyPad_Mode (current.wind, current.do_keypad);
545 when Character'Pos ('S') mod 16#20# => -- Ctrl('S')
546 if current /= null then
547 current.do_scroll := not current.do_scroll;
548 Allow_Scrolling (current.wind, current.do_scroll);
550 when Character'Pos ('W') mod 16#20# => -- Ctrl('W')
551 if current /= current.next then
552 Create (f, Name => dumpfile); -- TODO error checking
553 if not Is_Open (f) then
554 raise Curses_Exception;
556 Put_Window (current.wind, f);
558 current := delete_framed (current, True);
560 when Character'Pos ('R') mod 16#20# => -- Ctrl('R')
562 neww : FrameA := new Frame'(null, null, False, False,
565 Open (f, Mode => In_File, Name => dumpfile);
566 neww := new Frame'(null, null, False, False, Null_Window);
568 neww.next := current.next;
569 neww.last := current;
570 neww.last.next := neww;
571 neww.next.last := neww;
573 neww.wind := Get_Window (f);
578 when Character'Pos ('X') mod 16#20# => -- Ctrl('X')
579 if current /= null then
582 mx : Column_Position;
586 Move_Cursor (Line => 0, Column => 0);
587 Clear_To_End_Of_Line;
588 Add (Str => "Use arrows to move cursor, anything else " &
589 "to mark new corner");
592 Get_Window_Position (current.wind, ul.y, ul.x);
594 selectcell (ul.y, ul.x, Lines - Botlines - 2, Columns - 2,
597 -- the C version had a goto. I refuse gotos.
600 Get_Size (current.wind, lr.y, lr.x);
601 lr.y := lr.y + ul.y - 1;
602 lr.x := lr.x + ul.x - 1;
603 Outerbox (ul, lr, False);
604 Refresh_Without_Update;
606 Get_Size (current.wind, my, mx);
607 if my > tmp.y - ul.y then
608 Get_Cursor_Position (current.wind, lr.y, lr.x);
609 Move_Cursor (current.wind, tmp.y - ul.y + 1, 0);
610 Clear_To_End_Of_Screen (current.wind);
611 Move_Cursor (current.wind, lr.y, lr.x);
613 if mx > tmp.x - ul.x then
614 for i in 0 .. my - 1 loop
615 Move_Cursor (current.wind, i, tmp.x - ul.x + 1);
616 Clear_To_End_Of_Line (current.wind);
619 Refresh_Without_Update (current.wind);
622 -- The C version passes invalid args to resize
623 -- which returns an ERR. For Ada we avoid the exception.
624 if lr.y /= ul.y and lr.x /= ul.x then
625 Resize (current.wind, lr.y - ul.y + 0,
629 Get_Window_Position (current.wind, ul.y, ul.x);
630 Get_Size (current.wind, lr.y, lr.x);
631 lr.y := lr.y + ul.y - 1;
632 lr.x := lr.x + ul.x - 1;
633 Outerbox (ul, lr, True);
634 Refresh_Without_Update;
636 Refresh_Without_Update (current.wind);
637 Move_Cursor (Line => 0, Column => 0);
638 Clear_To_End_Of_Line;
644 declare tmp : pair; tmpbool : Boolean;
646 -- undocumented --- use this to test area clears
647 selectcell (0, 0, Lines - 1, Columns - 1, tmp, tmpbool);
648 Clear_To_End_Of_Screen;
651 when Key_Cursor_Up =>
652 newwin_move (current.wind, -1, 0);
653 when Key_Cursor_Down =>
654 newwin_move (current.wind, 1, 0);
655 when Key_Cursor_Left =>
656 newwin_move (current.wind, 0, -1);
657 when Key_Cursor_Right =>
658 newwin_move (current.wind, 0, 1);
659 when Key_Backspace | Key_Delete_Char =>
665 Get_Cursor_Position (current.wind, y, x);
667 -- I got tricked by the -1 = Max_Natural - 1 result
669 if not (x = 0 and y = 0) then
672 Get_Size (current.wind, tmp, x);
675 Delete_Character (current.wind, y, x);
680 if current /= null then
683 Add (current.wind, Ch => Code_To_Char (c));
685 when Curses_Exception => null;
686 -- this happens if we are at the
687 -- lower right of a window and add a character.
693 newwin_report (current.wind);
694 if current /= null then
695 usescr := current.wind;
697 usescr := Standard_Window;
700 c := Getchar (usescr);
701 exit when c = Quit or (c = Escape and HaveKeyPad (usescr));
702 -- TODO when does c = ERR happen?
705 -- TODO while current /= null loop
706 -- current := delete_framed(current, False);
709 Allow_Scrolling (Mode => True);
712 Set_Raw_Mode (SwitchOn => True);
716 end ncurses2.acs_and_scroll;