1 ------------------------------------------------------------------------------
3 -- GNAT ncurses Binding Samples --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000 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 -- Binding Version 01.00
40 ------------------------------------------------------------------------------
41 with ncurses2.util; use ncurses2.util;
42 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
43 with Terminal_Interface.Curses.Forms; use Terminal_Interface.Curses.Forms;
44 with Terminal_Interface.Curses.Forms.Field_User_Data;
45 with Ada.Characters.Handling;
47 with Ada.Strings.Bounded;
49 procedure ncurses2.demo_forms is
50 package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (80);
52 type myptr is access Integer;
54 -- The C version stores a pointer in the userptr and
55 -- converts it into a long integer.
56 -- The correct, but inconvenient way to do it is to use a
57 -- pointer to long and keep the pointer constant.
58 -- It just adds one memory piece to allocate and deallocate (not done here)
60 package StringData is new
61 Terminal_Interface.Curses.Forms.Field_User_Data (Integer, myptr);
63 function edit_secure (me : Field; c_in : Key_Code) return Key_Code;
64 function form_virtualize (f : Form; w : Window) return Key_Code;
65 function my_form_driver (f : Form; c : Key_Code) return Boolean;
66 function make_label (frow : Line_Position;
67 fcol : Column_Position;
68 label : String) return Field;
69 function make_field (frow : Line_Position;
70 fcol : Column_Position;
73 secure : Boolean) return Field;
74 procedure display_form (f : Form);
75 procedure erase_form (f : Form);
77 -- prints '*' instead of characters.
78 -- Not that this keeps a bug from the C version:
79 -- type in the psasword field then move off and back.
80 -- the cursor is at position one, but
81 -- this assumes it as at the end so text gets appended instead
83 function edit_secure (me : Field; c_in : Key_Code) return Key_Code is
84 rows, frow : Line_Position;
86 cols, fcol : Column_Position;
93 Info (me, rows, cols, frow, fcol, nrow, nbuf);
94 -- TODO if result = Form_Ok and nbuf > 0 then
95 -- C version checked the return value
96 -- of Info, the Ada binding throws an exception I think.
99 temp : BS.Bounded_String;
100 temps : String (1 .. 10);
101 -- TODO Get_Buffer povides no information on the field length?
104 Get_Buffer (me, 1, Str => temps);
105 -- strcpy(temp, field_buffer(me, 1));
106 Get_User_Data (me, len);
107 temp := BS.To_Bounded_String (temps (1 .. len.all));
109 c2 := Code_To_Char (c);
110 if Ada.Characters.Handling.Is_Graphic (c2) then
111 BS.Append (temp, c2);
112 len.all := len.all + 1;
113 Set_Buffer (me, 1, BS.To_String (temp));
114 c := Character'Pos ('*');
135 c := 0; -- we don't want to do inline editing
136 when REQ_CLR_FIELD =>
138 temp := BS.To_Bounded_String ("");
139 Set_Buffer (me, 1, BS.To_String (temp));
146 BS.Delete (temp, BS.Length (temp), BS.Length (temp));
147 Set_Buffer (me, 1, BS.To_String (temp));
148 len.all := len.all - 1;
158 mode : Key_Code := REQ_INS_MODE;
160 function form_virtualize (f : Form; w : Window) return Key_Code is
161 type lookup_t is record
164 -- should be Form_Request_Code, but we need MAX_COMMAND + 1
167 lookup : constant array (Positive range <>) of lookup_t :=
170 Character'Pos ('A') mod 16#20#, REQ_NEXT_CHOICE
173 Character'Pos ('B') mod 16#20#, REQ_PREV_WORD
176 Character'Pos ('C') mod 16#20#, REQ_CLR_EOL
179 Character'Pos ('D') mod 16#20#, REQ_DOWN_FIELD
182 Character'Pos ('E') mod 16#20#, REQ_END_FIELD
185 Character'Pos ('F') mod 16#20#, REQ_NEXT_PAGE
188 Character'Pos ('G') mod 16#20#, REQ_DEL_WORD
191 Character'Pos ('H') mod 16#20#, REQ_DEL_PREV
194 Character'Pos ('I') mod 16#20#, REQ_INS_CHAR
197 Character'Pos ('K') mod 16#20#, REQ_CLR_EOF
200 Character'Pos ('L') mod 16#20#, REQ_LEFT_FIELD
203 Character'Pos ('M') mod 16#20#, REQ_NEW_LINE
206 Character'Pos ('N') mod 16#20#, REQ_NEXT_FIELD
209 Character'Pos ('O') mod 16#20#, REQ_INS_LINE
212 Character'Pos ('P') mod 16#20#, REQ_PREV_FIELD
215 Character'Pos ('R') mod 16#20#, REQ_RIGHT_FIELD
218 Character'Pos ('S') mod 16#20#, REQ_BEG_FIELD
221 Character'Pos ('U') mod 16#20#, REQ_UP_FIELD
224 Character'Pos ('V') mod 16#20#, REQ_DEL_CHAR
227 Character'Pos ('W') mod 16#20#, REQ_NEXT_WORD
230 Character'Pos ('X') mod 16#20#, REQ_CLR_FIELD
233 Character'Pos ('Y') mod 16#20#, REQ_DEL_LINE
236 Character'Pos ('Z') mod 16#20#, REQ_PREV_CHOICE
239 Character'Pos ('[') mod 16#20#, -- ESCAPE
240 Form_Request_Code'Last + 1
243 Key_Backspace, REQ_DEL_PREV
246 KEY_DOWN, REQ_DOWN_CHAR
249 Key_End, REQ_LAST_FIELD
252 Key_Home, REQ_FIRST_FIELD
255 KEY_LEFT, REQ_LEFT_CHAR
258 KEY_LL, REQ_LAST_FIELD
261 Key_Next, REQ_NEXT_FIELD
264 KEY_NPAGE, REQ_NEXT_PAGE
267 KEY_PPAGE, REQ_PREV_PAGE
270 Key_Previous, REQ_PREV_FIELD
273 KEY_RIGHT, REQ_RIGHT_CHAR
279 Character'Pos ('Q') mod 16#20#, -- QUIT
280 Form_Request_Code'Last + 1 -- TODO MAX_FORM_COMMAND + 1
284 c : Key_Code := Getchar (w);
285 me : Field := Current (f);
288 if c = Character'Pos (']') mod 16#20# then
289 if mode = REQ_INS_MODE then
290 mode := REQ_OVL_MODE;
292 mode := REQ_INS_MODE;
296 for n in lookup'Range loop
297 if lookup (n).code = c then
298 c := lookup (n).result;
304 -- Force the field that the user is typing into to be in reverse video,
305 -- while the other fields are shown underlined.
307 c := edit_secure (me, c);
308 Set_Background (me, (Reverse_Video => True, others => False));
309 elsif c <= Form_Request_Code'Last then
310 c := edit_secure (me, c);
311 Set_Background (me, (Under_Line => True, others => False));
316 function my_form_driver (f : Form; c : Key_Code) return Boolean is
317 flag : Driver_Result := Driver (f, F_Validate_Field);
319 if c = Form_Request_Code'Last + 1
320 and flag = Form_Ok then
328 function make_label (frow : Line_Position;
329 fcol : Column_Position;
330 label : String) return Field is
331 f : Field := Create (1, label'Length, frow, fcol, 0, 0);
332 o : Field_Option_Set := Get_Options (f);
334 if f /= Null_Field then
335 Set_Buffer (f, 0, label);
342 function make_field (frow : Line_Position;
343 fcol : Column_Position;
346 secure : Boolean) return Field is
352 f := Create (rows, cols, frow, fcol, 0, 1);
354 f := Create (rows, cols, frow, fcol, 0, 0);
357 if f /= Null_Field then
358 Set_Background (f, (Under_Line => True, others => False));
361 Set_User_Data (f, len);
366 procedure display_form (f : Form) is
371 Scale (f, rows, cols);
373 w := New_Window (rows + 2, cols + 4, 0, 0);
374 if w /= Null_Window then
376 Set_Sub_Window (f, Derived_Window (w, rows, cols, 1, 2));
378 Set_KeyPad_Mode (w, True);
381 -- TODO if Post(f) /= Form_Ok then it's a procedure
394 Eti_Unknown_Command |
406 procedure erase_form (f : Form) is
407 w : Window := Get_Window (f);
408 s : Window := Get_Sub_Window (f);
417 finished : Boolean := False;
418 f : Field_Array_Access := new Field_Array (1 .. 12);
423 result : Driver_Result;
425 Move_Cursor (Line => 18, Column => 0);
426 Add (Str => "Defined form-traversal keys: ^Q/ESC- exit form");
428 Add (Str => "^N -- go to next field ^P -- go to previous field");
430 Add (Str => "Home -- go to first field End -- go to last field");
432 Add (Str => "^L -- go to field to left ^R -- go to field to right");
434 Add (Str => "^U -- move upward to field ^D -- move downward to field");
436 Add (Str => "^W -- go to next word ^B -- go to previous word");
438 Add (Str => "^S -- go to start of field ^E -- go to end of field");
440 Add (Str => "^H -- delete previous char ^Y -- delete line");
442 Add (Str => "^G -- delete current word ^C -- clear to end of line");
444 Add (Str => "^K -- clear to end of field ^X -- clear field");
446 Add (Str => "Arrow keys move within a field as you would expect.");
448 Add (Line => 4, Column => 57, Str => "Forms Entry Test");
453 f (1) := make_label (0, 15, "Sample Form");
454 f (2) := make_label (2, 0, "Last Name");
455 f (3) := make_field (3, 0, 1, 18, False);
456 f (4) := make_label (2, 20, "First Name");
457 f (5) := make_field (3, 20, 1, 12, False);
458 f (6) := make_label (2, 34, "Middle Name");
459 f (7) := make_field (3, 34, 1, 12, False);
460 f (8) := make_label (5, 0, "Comments");
461 f (9) := make_field (6, 0, 4, 46, False);
462 f (10) := make_label (5, 20, "Password:");
463 f (11) := make_field (5, 30, 1, 9, True);
465 f (12) := Null_Field;
467 myform := New_Form (f);
469 display_form (myform);
471 w := Get_Window (myform);
472 Set_Raw_Mode (SwitchOn => True);
473 Set_NL_Mode (SwitchOn => True); -- lets us read ^M's
474 while not finished loop
475 c := form_virtualize (myform, w);
476 result := Driver (myform, c);
479 Add (Line => 5, Column => 57, Str => Get_Buffer (secure, 1));
480 Clear_To_End_Of_Line;
482 when Unknown_Request =>
483 finished := my_form_driver (myform, c);
491 -- TODO Free_Form(myform);
492 -- for (c = 0; f[c] != 0; c++) free_field(f[c]);
493 Set_Raw_Mode (SwitchOn => False);
494 Set_NL_Mode (SwitchOn => True);
496 end ncurses2.demo_forms;