/usr/share/doc/libncursesada-doc/examples/ncurses2-demo_forms.adb is in libncursesada-doc 6.0.20170708-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | ------------------------------------------------------------------------------
-- --
-- GNAT ncurses Binding Samples --
-- --
-- ncurses --
-- --
-- B O D Y --
-- --
------------------------------------------------------------------------------
-- Copyright (c) 2000-2011,2014 Free Software Foundation, Inc. --
-- --
-- Permission is hereby granted, free of charge, to any person obtaining a --
-- copy of this software and associated documentation files (the --
-- "Software"), to deal in the Software without restriction, including --
-- without limitation the rights to use, copy, modify, merge, publish, --
-- distribute, distribute with modifications, sublicense, and/or sell --
-- copies of the Software, and to permit persons to whom the Software is --
-- furnished to do so, subject to the following conditions: --
-- --
-- The above copyright notice and this permission notice shall be included --
-- in all copies or substantial portions of the Software. --
-- --
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS --
-- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF --
-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. --
-- IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, --
-- DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR --
-- OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR --
-- THE USE OR OTHER DEALINGS IN THE SOFTWARE. --
-- --
-- Except as contained in this notice, the name(s) of the above copyright --
-- holders shall not be used in advertising or otherwise to promote the --
-- sale, use or other dealings in this Software without prior written --
-- authorization. --
------------------------------------------------------------------------------
-- Author: Eugene V. Melaragno <aldomel@ix.netcom.com> 2000
-- Version Control
-- $Revision: 1.7 $
-- $Date: 2014/09/13 19:10:18 $
-- Binding Version 01.00
------------------------------------------------------------------------------
with ncurses2.util; use ncurses2.util;
with Terminal_Interface.Curses; use Terminal_Interface.Curses;
with Terminal_Interface.Curses.Forms; use Terminal_Interface.Curses.Forms;
with Terminal_Interface.Curses.Forms.Field_User_Data;
with Ada.Characters.Handling;
with Ada.Strings;
with Ada.Strings.Bounded;
procedure ncurses2.demo_forms is
package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (80);
type myptr is access Integer;
-- The C version stores a pointer in the userptr and
-- converts it into a long integer.
-- The correct, but inconvenient way to do it is to use a
-- pointer to long and keep the pointer constant.
-- It just adds one memory piece to allocate and deallocate (not done here)
package StringData is new
Terminal_Interface.Curses.Forms.Field_User_Data (Integer, myptr);
function edit_secure (me : Field; c_in : Key_Code) return Key_Code;
function form_virtualize (f : Form; w : Window) return Key_Code;
function my_form_driver (f : Form; c : Key_Code) return Boolean;
function make_label (frow : Line_Position;
fcol : Column_Position;
label : String) return Field;
function make_field (frow : Line_Position;
fcol : Column_Position;
rows : Line_Count;
cols : Column_Count;
secure : Boolean) return Field;
procedure display_form (f : Form);
procedure erase_form (f : Form);
-- prints '*' instead of characters.
-- Not that this keeps a bug from the C version:
-- type in the psasword field then move off and back.
-- the cursor is at position one, but
-- this assumes it as at the end so text gets appended instead
-- of overwtitting.
function edit_secure (me : Field; c_in : Key_Code) return Key_Code is
rows, frow : Line_Position;
nrow : Natural;
cols, fcol : Column_Position;
nbuf : Buffer_Number;
c : Key_Code := c_in;
c2 : Character;
use StringData;
begin
Info (me, rows, cols, frow, fcol, nrow, nbuf);
-- TODO if result = Form_Ok and nbuf > 0 then
-- C version checked the return value
-- of Info, the Ada binding throws an exception I think.
if nbuf > 0 then
declare
temp : BS.Bounded_String;
temps : String (1 .. 10);
-- TODO Get_Buffer povides no information on the field length?
len : myptr;
begin
Get_Buffer (me, 1, Str => temps);
-- strcpy(temp, field_buffer(me, 1));
Get_User_Data (me, len);
temp := BS.To_Bounded_String (temps (1 .. len.all));
if c <= Key_Max then
c2 := Code_To_Char (c);
if Ada.Characters.Handling.Is_Graphic (c2) then
BS.Append (temp, c2);
len.all := len.all + 1;
Set_Buffer (me, 1, BS.To_String (temp));
c := Character'Pos ('*');
else
c := 0;
end if;
else
case c is
when REQ_BEG_FIELD |
REQ_CLR_EOF |
REQ_CLR_EOL |
REQ_DEL_LINE |
REQ_DEL_WORD |
REQ_DOWN_CHAR |
REQ_END_FIELD |
REQ_INS_CHAR |
REQ_INS_LINE |
REQ_LEFT_CHAR |
REQ_NEW_LINE |
REQ_NEXT_WORD |
REQ_PREV_WORD |
REQ_RIGHT_CHAR |
REQ_UP_CHAR =>
c := 0; -- we don't want to do inline editing
when REQ_CLR_FIELD =>
if len.all /= 0 then
temp := BS.To_Bounded_String ("");
Set_Buffer (me, 1, BS.To_String (temp));
len.all := 0;
end if;
when REQ_DEL_CHAR |
REQ_DEL_PREV =>
if len.all /= 0 then
BS.Delete (temp, BS.Length (temp), BS.Length (temp));
Set_Buffer (me, 1, BS.To_String (temp));
len.all := len.all - 1;
end if;
when others => null;
end case;
end if;
end;
end if;
return c;
end edit_secure;
mode : Key_Code := REQ_INS_MODE;
function form_virtualize (f : Form; w : Window) return Key_Code is
type lookup_t is record
code : Key_Code;
result : Key_Code;
-- should be Form_Request_Code, but we need MAX_COMMAND + 1
end record;
lookup : constant array (Positive range <>) of lookup_t :=
(
(
Character'Pos ('A') mod 16#20#, REQ_NEXT_CHOICE
),
(
Character'Pos ('B') mod 16#20#, REQ_PREV_WORD
),
(
Character'Pos ('C') mod 16#20#, REQ_CLR_EOL
),
(
Character'Pos ('D') mod 16#20#, REQ_DOWN_FIELD
),
(
Character'Pos ('E') mod 16#20#, REQ_END_FIELD
),
(
Character'Pos ('F') mod 16#20#, REQ_NEXT_PAGE
),
(
Character'Pos ('G') mod 16#20#, REQ_DEL_WORD
),
(
Character'Pos ('H') mod 16#20#, REQ_DEL_PREV
),
(
Character'Pos ('I') mod 16#20#, REQ_INS_CHAR
),
(
Character'Pos ('K') mod 16#20#, REQ_CLR_EOF
),
(
Character'Pos ('L') mod 16#20#, REQ_LEFT_FIELD
),
(
Character'Pos ('M') mod 16#20#, REQ_NEW_LINE
),
(
Character'Pos ('N') mod 16#20#, REQ_NEXT_FIELD
),
(
Character'Pos ('O') mod 16#20#, REQ_INS_LINE
),
(
Character'Pos ('P') mod 16#20#, REQ_PREV_FIELD
),
(
Character'Pos ('R') mod 16#20#, REQ_RIGHT_FIELD
),
(
Character'Pos ('S') mod 16#20#, REQ_BEG_FIELD
),
(
Character'Pos ('U') mod 16#20#, REQ_UP_FIELD
),
(
Character'Pos ('V') mod 16#20#, REQ_DEL_CHAR
),
(
Character'Pos ('W') mod 16#20#, REQ_NEXT_WORD
),
(
Character'Pos ('X') mod 16#20#, REQ_CLR_FIELD
),
(
Character'Pos ('Y') mod 16#20#, REQ_DEL_LINE
),
(
Character'Pos ('Z') mod 16#20#, REQ_PREV_CHOICE
),
(
Character'Pos ('[') mod 16#20#, -- ESCAPE
Form_Request_Code'Last + 1
),
(
Key_Backspace, REQ_DEL_PREV
),
(
KEY_DOWN, REQ_DOWN_CHAR
),
(
Key_End, REQ_LAST_FIELD
),
(
Key_Home, REQ_FIRST_FIELD
),
(
KEY_LEFT, REQ_LEFT_CHAR
),
(
KEY_LL, REQ_LAST_FIELD
),
(
Key_Next, REQ_NEXT_FIELD
),
(
KEY_NPAGE, REQ_NEXT_PAGE
),
(
KEY_PPAGE, REQ_PREV_PAGE
),
(
Key_Previous, REQ_PREV_FIELD
),
(
KEY_RIGHT, REQ_RIGHT_CHAR
),
(
KEY_UP, REQ_UP_CHAR
),
(
Character'Pos ('Q') mod 16#20#, -- QUIT
Form_Request_Code'Last + 1 -- TODO MAX_FORM_COMMAND + 1
)
);
c : Key_Code := Getchar (w);
me : constant Field := Current (f);
begin
if c = Character'Pos (']') mod 16#20# then
if mode = REQ_INS_MODE then
mode := REQ_OVL_MODE;
else
mode := REQ_INS_MODE;
end if;
c := mode;
else
for n in lookup'Range loop
if lookup (n).code = c then
c := lookup (n).result;
exit;
end if;
end loop;
end if;
-- Force the field that the user is typing into to be in reverse video,
-- while the other fields are shown underlined.
if c <= Key_Max then
c := edit_secure (me, c);
Set_Background (me, (Reverse_Video => True, others => False));
elsif c <= Form_Request_Code'Last then
c := edit_secure (me, c);
Set_Background (me, (Under_Line => True, others => False));
end if;
return c;
end form_virtualize;
function my_form_driver (f : Form; c : Key_Code) return Boolean is
flag : constant Driver_Result := Driver (f, F_Validate_Field);
begin
if c = Form_Request_Code'Last + 1 and
flag = Form_Ok
then
return True;
else
Beep;
return False;
end if;
end my_form_driver;
function make_label (frow : Line_Position;
fcol : Column_Position;
label : String) return Field is
f : constant Field := Create (1, label'Length, frow, fcol, 0, 0);
o : Field_Option_Set := Get_Options (f);
begin
if f /= Null_Field then
Set_Buffer (f, 0, label);
o.Active := False;
Set_Options (f, o);
end if;
return f;
end make_label;
function make_field (frow : Line_Position;
fcol : Column_Position;
rows : Line_Count;
cols : Column_Count;
secure : Boolean) return Field is
f : Field;
use StringData;
len : myptr;
begin
if secure then
f := Create (rows, cols, frow, fcol, 0, 1);
else
f := Create (rows, cols, frow, fcol, 0, 0);
end if;
if f /= Null_Field then
Set_Background (f, (Under_Line => True, others => False));
len := new Integer;
len.all := 0;
Set_User_Data (f, len);
end if;
return f;
end make_field;
procedure display_form (f : Form) is
w : Window;
rows : Line_Count;
cols : Column_Count;
begin
Scale (f, rows, cols);
w := New_Window (rows + 2, cols + 4, 0, 0);
if w /= Null_Window then
Set_Window (f, w);
Set_Sub_Window (f, Derived_Window (w, rows, cols, 1, 2));
Box (w); -- 0,0
Set_KeyPad_Mode (w, True);
end if;
-- TODO if Post(f) /= Form_Ok then it's a procedure
declare
begin
Post (f);
exception
when
Eti_System_Error |
Eti_Bad_Argument |
Eti_Posted |
Eti_Connected |
Eti_Bad_State |
Eti_No_Room |
Eti_Not_Posted |
Eti_Unknown_Command |
Eti_No_Match |
Eti_Not_Selectable |
Eti_Not_Connected |
Eti_Request_Denied |
Eti_Invalid_Field |
Eti_Current =>
Refresh (w);
end;
-- end if;
end display_form;
procedure erase_form (f : Form) is
w : Window := Get_Window (f);
s : Window := Get_Sub_Window (f);
begin
Post (f, False);
Erase (w);
Refresh (w);
Delete (s);
Delete (w);
end erase_form;
finished : Boolean := False;
f : constant Field_Array_Access := new Field_Array (1 .. 12);
secure : Field;
myform : Form;
w : Window;
c : Key_Code;
result : Driver_Result;
begin
Move_Cursor (Line => 18, Column => 0);
Add (Str => "Defined form-traversal keys: ^Q/ESC- exit form");
Add (Ch => newl);
Add (Str => "^N -- go to next field ^P -- go to previous field");
Add (Ch => newl);
Add (Str => "Home -- go to first field End -- go to last field");
Add (Ch => newl);
Add (Str => "^L -- go to field to left ^R -- go to field to right");
Add (Ch => newl);
Add (Str => "^U -- move upward to field ^D -- move downward to field");
Add (Ch => newl);
Add (Str => "^W -- go to next word ^B -- go to previous word");
Add (Ch => newl);
Add (Str => "^S -- go to start of field ^E -- go to end of field");
Add (Ch => newl);
Add (Str => "^H -- delete previous char ^Y -- delete line");
Add (Ch => newl);
Add (Str => "^G -- delete current word ^C -- clear to end of line");
Add (Ch => newl);
Add (Str => "^K -- clear to end of field ^X -- clear field");
Add (Ch => newl);
Add (Str => "Arrow keys move within a field as you would expect.");
Add (Line => 4, Column => 57, Str => "Forms Entry Test");
Refresh;
-- describe the form
f.all (1) := make_label (0, 15, "Sample Form");
f.all (2) := make_label (2, 0, "Last Name");
f.all (3) := make_field (3, 0, 1, 18, False);
f.all (4) := make_label (2, 20, "First Name");
f.all (5) := make_field (3, 20, 1, 12, False);
f.all (6) := make_label (2, 34, "Middle Name");
f.all (7) := make_field (3, 34, 1, 12, False);
f.all (8) := make_label (5, 0, "Comments");
f.all (9) := make_field (6, 0, 4, 46, False);
f.all (10) := make_label (5, 20, "Password:");
f.all (11) := make_field (5, 30, 1, 9, True);
secure := f.all (11);
f.all (12) := Null_Field;
myform := New_Form (f);
display_form (myform);
w := Get_Window (myform);
Set_Raw_Mode (SwitchOn => True);
Set_NL_Mode (SwitchOn => True); -- lets us read ^M's
while not finished loop
c := form_virtualize (myform, w);
result := Driver (myform, c);
case result is
when Form_Ok =>
Add (Line => 5, Column => 57, Str => Get_Buffer (secure, 1));
Clear_To_End_Of_Line;
Refresh;
when Unknown_Request =>
finished := my_form_driver (myform, c);
when others =>
Beep;
end case;
end loop;
erase_form (myform);
-- TODO Free_Form(myform);
-- for (c = 0; f[c] != 0; c++) free_field(f[c]);
Set_Raw_Mode (SwitchOn => False);
Set_NL_Mode (SwitchOn => True);
end ncurses2.demo_forms;
|