1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000,2004 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: 2004/08/21 21:37:00 $
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
60 Macro_Quit : constant Key_Code := Character'Pos ('Q') mod 16#20#;
61 Macro_Escape : constant Key_Code := Character'Pos ('[') mod 16#20#;
63 Quit : constant Key_Code := CTRL ('Q');
64 Escape : constant Key_Code := CTRL ('[');
67 Botlines : constant Line_Position := 4;
75 type FrameA is access Frame;
78 dumpfile : constant String := "screendump";
80 procedure Outerbox (ul, lr : pair; onoff : Boolean);
81 function HaveKeyPad (w : Window) return Boolean;
82 function HaveScroll (w : Window) return Boolean;
83 procedure newwin_legend (curpw : Window);
84 procedure transient (curpw : Window; msg : String);
85 procedure newwin_report (win : Window := Standard_Window);
86 procedure selectcell (uli : Line_Position;
87 ulj : Column_Position;
89 lrj : Column_Position;
92 function getwindow return Window;
93 procedure newwin_move (win : Window;
95 dx : Column_Position);
96 function delete_framed (fp : FrameA; showit : Boolean) return FrameA;
98 use Ada.Streams.Stream_IO;
102 -- I wish there was a standard library linked list. Oh well.
114 procedure Outerbox (ul, lr : pair; onoff : Boolean) is
117 -- Note the fix of an obscure bug
118 -- try making a 1x1 box then enlarging it, the is a blank
119 -- upper left corner!
120 Add (Line => ul.y - 1, Column => ul.x - 1,
121 Ch => ACS_Map (ACS_Upper_Left_Corner));
122 Add (Line => ul.y - 1, Column => lr.x + 1,
123 Ch => ACS_Map (ACS_Upper_Right_Corner));
124 Add (Line => lr.y + 1, Column => lr.x + 1,
125 Ch => ACS_Map (ACS_Lower_Right_Corner));
126 Add (Line => lr.y + 1, Column => ul.x - 1,
127 Ch => ACS_Map (ACS_Lower_Left_Corner));
129 Move_Cursor (Line => ul.y - 1, Column => ul.x);
130 Horizontal_Line (Line_Symbol => ACS_Map (ACS_Horizontal_Line),
131 Line_Size => Integer (lr.x - ul.x) + 1);
132 Move_Cursor (Line => ul.y, Column => ul.x - 1);
133 Vertical_Line (Line_Symbol => ACS_Map (ACS_Vertical_Line),
134 Line_Size => Integer (lr.y - ul.y) + 1);
135 Move_Cursor (Line => lr.y + 1, Column => ul.x);
136 Horizontal_Line (Line_Symbol => ACS_Map (ACS_Horizontal_Line),
137 Line_Size => Integer (lr.x - ul.x) + 1);
138 Move_Cursor (Line => ul.y, Column => lr.x + 1);
139 Vertical_Line (Line_Symbol => ACS_Map (ACS_Vertical_Line),
140 Line_Size => Integer (lr.y - ul.y) + 1);
142 Add (Line => ul.y - 1, Column => ul.x - 1, Ch => ' ');
143 Add (Line => ul.y - 1, Column => lr.x + 1, Ch => ' ');
144 Add (Line => lr.y + 1, Column => lr.x + 1, Ch => ' ');
145 Add (Line => lr.y + 1, Column => ul.x - 1, Ch => ' ');
147 Move_Cursor (Line => ul.y - 1, Column => ul.x);
148 Horizontal_Line (Line_Symbol => Blank2,
149 Line_Size => Integer (lr.x - ul.x) + 1);
150 Move_Cursor (Line => ul.y, Column => ul.x - 1);
151 Vertical_Line (Line_Symbol => Blank2,
152 Line_Size => Integer (lr.y - ul.y) + 1);
153 Move_Cursor (Line => lr.y + 1, Column => ul.x);
154 Horizontal_Line (Line_Symbol => Blank2,
155 Line_Size => Integer (lr.x - ul.x) + 1);
156 Move_Cursor (Line => ul.y, Column => lr.x + 1);
157 Vertical_Line (Line_Symbol => Blank2,
158 Line_Size => Integer (lr.y - ul.y) + 1);
162 function HaveKeyPad (w : Window) return Boolean is
164 return Get_KeyPad_Mode (w);
166 when Curses_Exception => return False;
169 function HaveScroll (w : Window) return Boolean is
171 return Scrolling_Allowed (w);
173 when Curses_Exception => return False;
177 procedure newwin_legend (curpw : Window) is
179 package p is new genericPuts (200);
183 type string_a is access String;
187 code : Integer range 0 .. 3;
190 legend : constant array (Positive range <>) of rrr :=
193 new String'("^C = create window"), 0
196 new String'("^N = next window"), 0
199 new String'("^P = previous window"), 0
202 new String'("^F = scroll forward"), 0
205 new String'("^B = scroll backward"), 0
208 new String'("^K = keypad(%s)"), 1
211 new String'("^S = scrollok(%s)"), 2
214 new String'("^W = save window to file"), 0
217 new String'("^R = restore window"), 0
220 new String'("^X = resize"), 0
223 new String'("^Q%s = exit"), 3
227 buf : Bounded_String;
228 do_keypad : constant Boolean := HaveKeyPad (curpw);
229 do_scroll : constant Boolean := HaveScroll (curpw);
235 use Ada.Strings.Fixed;
238 Move_Cursor (Line => Lines - 4, Column => 0);
239 for n in legend'Range loop
240 pos := Ada.Strings.Fixed.Index (Source => legend (n).msg.all,
242 -- buf := (others => ' ');
243 buf := To_Bounded_String (legend (n).msg.all);
244 case legend (n).code is
248 Replace_Slice (buf, pos, pos + 1, "yes");
250 Replace_Slice (buf, pos, pos + 1, "no");
254 Replace_Slice (buf, pos, pos + 1, "yes");
256 Replace_Slice (buf, pos, pos + 1, "no");
260 Replace_Slice (buf, pos, pos + 1, "/ESC");
262 Replace_Slice (buf, pos, pos + 1, "");
265 Get_Cursor_Position (Line => mypair.y, Column => mypair.x);
266 if Columns < mypair.x + 3 + Column_Position (Length (buf)) then
268 elsif n /= 1 then -- n /= legen'First
273 Clear_To_End_Of_Line;
277 procedure transient (curpw : Window; msg : String) is
279 newwin_legend (curpw);
281 Add (Line => Lines - 1, Column => 0, Str => msg);
283 Nap_Milli_Seconds (1000);
286 Move_Cursor (Line => Lines - 1, Column => 0);
288 if HaveKeyPad (curpw) then
289 Add (Str => "Non-arrow");
291 Add (Str => "All other");
293 Add (str => " characters are echoed, window should ");
294 if not HaveScroll (curpw) then
297 Add (str => "scroll");
299 Clear_To_End_Of_Line;
303 procedure newwin_report (win : Window := Standard_Window) is
307 tmp2a : String (1 .. 2);
308 tmp2b : String (1 .. 2);
310 if win /= Standard_Window then
313 Get_Cursor_Position (win, y, x);
314 Move_Cursor (Line => Lines - 1, Column => Columns - 17);
315 Put (tmp2a, Integer (y));
316 Put (tmp2b, Integer (x));
317 Add (Str => "Y = " & tmp2a & " X = " & tmp2b);
318 if win /= Standard_Window then
321 Move_Cursor (win, y, x);
325 procedure selectcell (uli : Line_Position;
326 ulj : Column_Position;
328 lrj : Column_Position;
333 i : Line_Position := 0;
334 j : Column_Position := 0;
335 si : constant Line_Position := lri - uli + 1;
336 sj : constant Column_Position := lrj - ulj + 1;
341 Move_Cursor (Line => uli + i, Column => ulj + j);
349 -- on the same line macro calls interfere due to the # comment
350 -- this is needed because keypad off affects all windows.
351 -- try removing the ESCAPE and see what happens.
356 -- same as i := i - 1 because of Modulus arithetic,
357 -- on Line_Position, which is a Natural
358 -- the C version uses this form too, interestingly.
370 Button : Mouse_Button;
371 State : Button_State;
375 Get_Event (Event => event,
380 if y > uli and x > ulj then
383 -- same as when others =>
406 function getwindow return Window is
411 Move_Cursor (Line => 0, Column => 0);
412 Clear_To_End_Of_Line;
413 Add (Str => "Use arrows to move cursor, anything else to mark corner 1");
415 selectcell (2, 1, Lines - Botlines - 2, Columns - 2, ul, result);
419 Add (Line => ul.y - 1, Column => ul.x - 1,
420 Ch => ACS_Map (ACS_Upper_Left_Corner));
421 Move_Cursor (Line => 0, Column => 0);
422 Clear_To_End_Of_Line;
423 Add (Str => "Use arrows to move cursor, anything else to mark corner 2");
425 selectcell (ul.y, ul.x, Lines - Botlines - 2, Columns - 2, lr, result);
430 rwindow := Sub_Window (Number_Of_Lines => lr.y - ul.y + 1,
431 Number_Of_Columns => lr.x - ul.x + 1,
432 First_Line_Position => ul.y,
433 First_Column_Position => ul.x);
435 Outerbox (ul, lr, True);
440 Move_Cursor (Line => 0, Column => 0);
441 Clear_To_End_Of_Line;
446 procedure newwin_move (win : Window;
448 dx : Column_Position) is
449 cur_y, max_y : Line_Position;
450 cur_x, max_x : Column_Position;
452 Get_Cursor_Position (win, cur_y, cur_x);
453 Get_Size (win, max_y, max_x);
454 cur_x := Column_Position'Min (Column_Position'Max (cur_x + dx, 0),
456 cur_y := Line_Position'Min (Line_Position'Max (cur_y + dy, 0),
459 Move_Cursor (win, Line => cur_y, Column => cur_x);
462 function delete_framed (fp : FrameA; showit : Boolean) return FrameA is
465 fp.last.next := fp.next;
466 fp.next.last := fp.last;
483 Mask : Event_Mask := No_Events;
490 Register_Reportable_Event (
494 Mask2 := Start_Mouse (Mask);
497 Set_Raw_Mode (SwitchOn => True);
499 transient (Standard_Window, "");
501 when Character'Pos ('c') mod 16#20# => -- Ctrl('c')
503 neww : FrameA := new Frame'(null, null, False, False,
506 neww.wind := getwindow;
507 if neww.wind = Null_Window then
509 -- was goto breakout; ha ha ha
512 if current = null then
516 neww.next := current.next;
517 neww.last := current;
518 neww.last.next := neww;
519 neww.next.last := neww;
523 Set_KeyPad_Mode (current.wind, True);
524 current.do_keypad := HaveKeyPad (current.wind);
525 current.do_scroll := HaveScroll (current.wind);
528 when Character'Pos ('N') mod 16#20# => -- Ctrl('N')
529 if current /= null then
530 current := current.next;
532 when Character'Pos ('P') mod 16#20# => -- Ctrl('P')
533 if current /= null then
534 current := current.last;
536 when Character'Pos ('F') mod 16#20# => -- Ctrl('F')
537 if current /= null and HaveScroll (current.wind) then
538 Scroll (current.wind, 1);
540 when Character'Pos ('B') mod 16#20# => -- Ctrl('B')
541 if current /= null and HaveScroll (current.wind) then
542 -- The C version of Scroll may return ERR which is ignored
543 -- we need to avoid the exception
544 -- with the 'and HaveScroll(current.wind)'
545 Scroll (current.wind, -1);
547 when Character'Pos ('K') mod 16#20# => -- Ctrl('K')
548 if current /= null then
549 current.do_keypad := not current.do_keypad;
550 Set_KeyPad_Mode (current.wind, current.do_keypad);
552 when Character'Pos ('S') mod 16#20# => -- Ctrl('S')
553 if current /= null then
554 current.do_scroll := not current.do_scroll;
555 Allow_Scrolling (current.wind, current.do_scroll);
557 when Character'Pos ('W') mod 16#20# => -- Ctrl('W')
558 if current /= current.next then
559 Create (f, Name => dumpfile); -- TODO error checking
560 if not Is_Open (f) then
561 raise Curses_Exception;
563 Put_Window (current.wind, f);
565 current := delete_framed (current, True);
567 when Character'Pos ('R') mod 16#20# => -- Ctrl('R')
569 neww : FrameA := new Frame'(null, null, False, False,
572 Open (f, Mode => In_File, Name => dumpfile);
573 neww := new Frame'(null, null, False, False, Null_Window);
575 neww.next := current.next;
576 neww.last := current;
577 neww.last.next := neww;
578 neww.next.last := neww;
580 neww.wind := Get_Window (f);
585 when Character'Pos ('X') mod 16#20# => -- Ctrl('X')
586 if current /= null then
589 mx : Column_Position;
593 Move_Cursor (Line => 0, Column => 0);
594 Clear_To_End_Of_Line;
595 Add (Str => "Use arrows to move cursor, anything else " &
596 "to mark new corner");
599 Get_Window_Position (current.wind, ul.y, ul.x);
601 selectcell (ul.y, ul.x, Lines - Botlines - 2, Columns - 2,
604 -- the C version had a goto. I refuse gotos.
607 Get_Size (current.wind, lr.y, lr.x);
608 lr.y := lr.y + ul.y - 1;
609 lr.x := lr.x + ul.x - 1;
610 Outerbox (ul, lr, False);
611 Refresh_Without_Update;
613 Get_Size (current.wind, my, mx);
614 if my > tmp.y - ul.y then
615 Get_Cursor_Position (current.wind, lr.y, lr.x);
616 Move_Cursor (current.wind, tmp.y - ul.y + 1, 0);
617 Clear_To_End_Of_Screen (current.wind);
618 Move_Cursor (current.wind, lr.y, lr.x);
620 if mx > tmp.x - ul.x then
621 for i in 0 .. my - 1 loop
622 Move_Cursor (current.wind, i, tmp.x - ul.x + 1);
623 Clear_To_End_Of_Line (current.wind);
626 Refresh_Without_Update (current.wind);
629 -- The C version passes invalid args to resize
630 -- which returns an ERR. For Ada we avoid the exception.
631 if lr.y /= ul.y and lr.x /= ul.x then
632 Resize (current.wind, lr.y - ul.y + 0,
636 Get_Window_Position (current.wind, ul.y, ul.x);
637 Get_Size (current.wind, lr.y, lr.x);
638 lr.y := lr.y + ul.y - 1;
639 lr.x := lr.x + ul.x - 1;
640 Outerbox (ul, lr, True);
641 Refresh_Without_Update;
643 Refresh_Without_Update (current.wind);
644 Move_Cursor (Line => 0, Column => 0);
645 Clear_To_End_Of_Line;
651 declare tmp : pair; tmpbool : Boolean;
653 -- undocumented --- use this to test area clears
654 selectcell (0, 0, Lines - 1, Columns - 1, tmp, tmpbool);
655 Clear_To_End_Of_Screen;
658 when Key_Cursor_Up =>
659 newwin_move (current.wind, -1, 0);
660 when Key_Cursor_Down =>
661 newwin_move (current.wind, 1, 0);
662 when Key_Cursor_Left =>
663 newwin_move (current.wind, 0, -1);
664 when Key_Cursor_Right =>
665 newwin_move (current.wind, 0, 1);
666 when Key_Backspace | Key_Delete_Char =>
672 Get_Cursor_Position (current.wind, y, x);
674 -- I got tricked by the -1 = Max_Natural - 1 result
676 if not (x = 0 and y = 0) then
679 Get_Size (current.wind, tmp, x);
682 Delete_Character (current.wind, y, x);
687 if current /= null then
690 Add (current.wind, Ch => Code_To_Char (c));
692 when Curses_Exception => null;
693 -- this happens if we are at the
694 -- lower right of a window and add a character.
700 newwin_report (current.wind);
701 if current /= null then
702 usescr := current.wind;
704 usescr := Standard_Window;
707 c := Getchar (usescr);
708 exit when c = Quit or (c = Escape and HaveKeyPad (usescr));
709 -- TODO when does c = ERR happen?
712 -- TODO while current /= null loop
713 -- current := delete_framed(current, False);
716 Allow_Scrolling (Mode => True);
719 Set_Raw_Mode (SwitchOn => True);
723 end ncurses2.acs_and_scroll;