Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | ------------------------------------------------------------------------------ |
| 2 | -- -- |
| 3 | -- GNAT ncurses Binding Samples -- |
| 4 | -- -- |
| 5 | -- ncurses -- |
| 6 | -- -- |
| 7 | -- B O D Y -- |
| 8 | -- -- |
| 9 | ------------------------------------------------------------------------------ |
| 10 | -- Copyright (c) 2000-2004,2006 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.7 $ |
| 39 | -- $Date: 2006/06/25 14:24:40 $ |
| 40 | -- Binding Version 01.00 |
| 41 | ------------------------------------------------------------------------------ |
| 42 | with ncurses2.util; use ncurses2.util; |
| 43 | with Terminal_Interface.Curses; use Terminal_Interface.Curses; |
| 44 | |
| 45 | with Ada.Strings.Unbounded; |
| 46 | with Interfaces.C; |
| 47 | with Terminal_Interface.Curses.Aux; |
| 48 | |
| 49 | procedure ncurses2.slk_test is |
| 50 | procedure myGet (Win : in Window := Standard_Window; |
| 51 | Str : out Ada.Strings.Unbounded.Unbounded_String; |
| 52 | Len : in Integer := -1); |
| 53 | |
| 54 | procedure myGet (Win : in Window := Standard_Window; |
| 55 | Str : out Ada.Strings.Unbounded.Unbounded_String; |
| 56 | Len : in Integer := -1) |
| 57 | is |
| 58 | use Ada.Strings.Unbounded; |
| 59 | use Interfaces.C; |
| 60 | use Terminal_Interface.Curses.Aux; |
| 61 | |
| 62 | function Wgetnstr (Win : Window; |
| 63 | Str : char_array; |
| 64 | Len : int) return int; |
| 65 | pragma Import (C, Wgetnstr, "wgetnstr"); |
| 66 | |
| 67 | -- FIXME: how to construct "(Len > 0) ? Len : 80"? |
| 68 | Ask : constant Interfaces.C.size_t := Interfaces.C.size_t'Val (Len + 80); |
| 69 | Txt : char_array (0 .. Ask); |
| 70 | |
| 71 | begin |
| 72 | Txt (0) := Interfaces.C.char'First; |
| 73 | if Wgetnstr (Win, Txt, Txt'Length) = Curses_Err then |
| 74 | raise Curses_Exception; |
| 75 | end if; |
| 76 | Str := To_Unbounded_String (To_Ada (Txt, True)); |
| 77 | end myGet; |
| 78 | |
| 79 | use Int_IO; |
| 80 | |
| 81 | use Ada.Strings.Unbounded; |
| 82 | |
| 83 | c : Key_Code; |
| 84 | buf : Unbounded_String; |
| 85 | c2 : Character; |
| 86 | fmt : Label_Justification := Centered; |
| 87 | tmp : Integer; |
| 88 | |
| 89 | begin |
| 90 | c := CTRL ('l'); |
| 91 | loop |
| 92 | Move_Cursor (Line => 0, Column => 0); |
| 93 | c2 := Code_To_Char (c); |
| 94 | case c2 is |
| 95 | when Character'Val (Character'Pos ('l') mod 16#20#) => -- CTRL('l') |
| 96 | Erase; |
| 97 | Switch_Character_Attribute (Attr => (Bold_Character => True, |
| 98 | others => False)); |
| 99 | Add (Line => 0, Column => 20, |
| 100 | Str => "Soft Key Exerciser"); |
| 101 | Switch_Character_Attribute (On => False, |
| 102 | Attr => (Bold_Character => True, |
| 103 | others => False)); |
| 104 | |
| 105 | Move_Cursor (Line => 2, Column => 0); |
| 106 | P ("Available commands are:"); |
| 107 | P (""); |
| 108 | P ("^L -- refresh screen"); |
| 109 | P ("a -- activate or restore soft keys"); |
| 110 | P ("d -- disable soft keys"); |
| 111 | P ("c -- set centered format for labels"); |
| 112 | P ("l -- set left-justified format for labels"); |
| 113 | P ("r -- set right-justified format for labels"); |
| 114 | P ("[12345678] -- set label; labels are numbered 1 through 8"); |
| 115 | P ("e -- erase stdscr (should not erase labels)"); |
| 116 | P ("s -- test scrolling of shortened screen"); |
| 117 | P ("x, q -- return to main menu"); |
| 118 | P (""); |
| 119 | P ("Note: if activating the soft keys causes your terminal to"); |
| 120 | P ("scroll up one line, your terminal auto-scrolls when anything"); |
| 121 | P ("is written to the last screen position. The ncurses code"); |
| 122 | P ("does not yet handle this gracefully."); |
| 123 | Refresh; |
| 124 | Restore_Soft_Label_Keys; |
| 125 | |
| 126 | when 'a' => |
| 127 | Restore_Soft_Label_Keys; |
| 128 | when 'e' => |
| 129 | Clear; |
| 130 | when 's' => |
| 131 | Add (Line => 20, Column => 0, |
| 132 | Str => "Press Q to stop the scrolling-test: "); |
| 133 | loop |
| 134 | c := Getchar; |
| 135 | c2 := Code_To_Char (c); |
| 136 | exit when c2 = 'Q'; |
| 137 | -- c = ERR? |
| 138 | -- TODO when c is not a character (arrow key) |
| 139 | -- the behavior is different from the C version. |
| 140 | Add (Ch => c2); |
| 141 | end loop; |
| 142 | when 'd' => |
| 143 | Clear_Soft_Label_Keys; |
| 144 | when 'l' => |
| 145 | fmt := Left; |
| 146 | when 'c' => |
| 147 | fmt := Centered; |
| 148 | when 'r' => |
| 149 | fmt := Right; |
| 150 | when '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' => |
| 151 | Add (Line => 20, Column => 0, |
| 152 | Str => "Please enter the label value: "); |
| 153 | Set_Echo_Mode (SwitchOn => True); |
| 154 | myGet (Str => buf); |
| 155 | Set_Echo_Mode (SwitchOn => False); |
| 156 | tmp := ctoi (c2); |
| 157 | Set_Soft_Label_Key (Label_Number (tmp), To_String (buf), fmt); |
| 158 | Refresh_Soft_Label_Keys; |
| 159 | Move_Cursor (Line => 20, Column => 0); |
| 160 | Clear_To_End_Of_Line; |
| 161 | when 'x' | 'q' => |
| 162 | exit; |
| 163 | -- the C version needed a goto, ha ha |
| 164 | -- breaks exit the case not the loop because fall-throuh |
| 165 | -- happens in C! |
| 166 | when others => |
| 167 | Beep; |
| 168 | end case; |
| 169 | c := Getchar; |
| 170 | -- TODO exit when c = EOF |
| 171 | end loop; |
| 172 | Erase; |
| 173 | End_Windows; |
| 174 | end ncurses2.slk_test; |