missing ncurses sources
[tomato.git] / release / src / router / libncurses / Ada95 / samples / ncurses2-demo_forms.adb
blob1afbb88bf3ff6c151283ae7dec7a86db737e7acf
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT ncurses Binding Samples --
4 -- --
5 -- ncurses --
6 -- --
7 -- B O D Y --
8 -- --
9 ------------------------------------------------------------------------------
10 -- Copyright (c) 2000-2006,2011 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.6 $
39 -- $Date: 2011/03/23 00:44:12 $
40 -- Binding Version 01.00
41 ------------------------------------------------------------------------------
42 with ncurses2.util; use ncurses2.util;
43 with Terminal_Interface.Curses; use Terminal_Interface.Curses;
44 with Terminal_Interface.Curses.Forms; use Terminal_Interface.Curses.Forms;
45 with Terminal_Interface.Curses.Forms.Field_User_Data;
46 with Ada.Characters.Handling;
47 with Ada.Strings;
48 with Ada.Strings.Bounded;
50 procedure ncurses2.demo_forms is
51 package BS is new Ada.Strings.Bounded.Generic_Bounded_Length (80);
53 type myptr is access Integer;
55 -- The C version stores a pointer in the userptr and
56 -- converts it into a long integer.
57 -- The correct, but inconvenient way to do it is to use a
58 -- pointer to long and keep the pointer constant.
59 -- It just adds one memory piece to allocate and deallocate (not done here)
61 package StringData is new
62 Terminal_Interface.Curses.Forms.Field_User_Data (Integer, myptr);
64 function edit_secure (me : Field; c_in : Key_Code) return Key_Code;
65 function form_virtualize (f : Form; w : Window) return Key_Code;
66 function my_form_driver (f : Form; c : Key_Code) return Boolean;
67 function make_label (frow : Line_Position;
68 fcol : Column_Position;
69 label : String) return Field;
70 function make_field (frow : Line_Position;
71 fcol : Column_Position;
72 rows : Line_Count;
73 cols : Column_Count;
74 secure : Boolean) return Field;
75 procedure display_form (f : Form);
76 procedure erase_form (f : Form);
78 -- prints '*' instead of characters.
79 -- Not that this keeps a bug from the C version:
80 -- type in the psasword field then move off and back.
81 -- the cursor is at position one, but
82 -- this assumes it as at the end so text gets appended instead
83 -- of overwtitting.
84 function edit_secure (me : Field; c_in : Key_Code) return Key_Code is
85 rows, frow : Line_Position;
86 nrow : Natural;
87 cols, fcol : Column_Position;
88 nbuf : Buffer_Number;
89 c : Key_Code := c_in;
90 c2 : Character;
92 use StringData;
93 begin
94 Info (me, rows, cols, frow, fcol, nrow, nbuf);
95 -- TODO if result = Form_Ok and nbuf > 0 then
96 -- C version checked the return value
97 -- of Info, the Ada binding throws an exception I think.
98 if nbuf > 0 then
99 declare
100 temp : BS.Bounded_String;
101 temps : String (1 .. 10);
102 -- TODO Get_Buffer povides no information on the field length?
103 len : myptr;
104 begin
105 Get_Buffer (me, 1, Str => temps);
106 -- strcpy(temp, field_buffer(me, 1));
107 Get_User_Data (me, len);
108 temp := BS.To_Bounded_String (temps (1 .. len.all));
109 if c <= Key_Max then
110 c2 := Code_To_Char (c);
111 if Ada.Characters.Handling.Is_Graphic (c2) then
112 BS.Append (temp, c2);
113 len.all := len.all + 1;
114 Set_Buffer (me, 1, BS.To_String (temp));
115 c := Character'Pos ('*');
116 else
117 c := 0;
118 end if;
119 else
120 case c is
121 when REQ_BEG_FIELD |
122 REQ_CLR_EOF |
123 REQ_CLR_EOL |
124 REQ_DEL_LINE |
125 REQ_DEL_WORD |
126 REQ_DOWN_CHAR |
127 REQ_END_FIELD |
128 REQ_INS_CHAR |
129 REQ_INS_LINE |
130 REQ_LEFT_CHAR |
131 REQ_NEW_LINE |
132 REQ_NEXT_WORD |
133 REQ_PREV_WORD |
134 REQ_RIGHT_CHAR |
135 REQ_UP_CHAR =>
136 c := 0; -- we don't want to do inline editing
137 when REQ_CLR_FIELD =>
138 if len.all /= 0 then
139 temp := BS.To_Bounded_String ("");
140 Set_Buffer (me, 1, BS.To_String (temp));
141 len.all := 0;
142 end if;
144 when REQ_DEL_CHAR |
145 REQ_DEL_PREV =>
146 if len.all /= 0 then
147 BS.Delete (temp, BS.Length (temp), BS.Length (temp));
148 Set_Buffer (me, 1, BS.To_String (temp));
149 len.all := len.all - 1;
150 end if;
151 when others => null;
152 end case;
153 end if;
154 end;
155 end if;
156 return c;
157 end edit_secure;
159 mode : Key_Code := REQ_INS_MODE;
161 function form_virtualize (f : Form; w : Window) return Key_Code is
162 type lookup_t is record
163 code : Key_Code;
164 result : Key_Code;
165 -- should be Form_Request_Code, but we need MAX_COMMAND + 1
166 end record;
168 lookup : constant array (Positive range <>) of lookup_t :=
171 Character'Pos ('A') mod 16#20#, REQ_NEXT_CHOICE
174 Character'Pos ('B') mod 16#20#, REQ_PREV_WORD
177 Character'Pos ('C') mod 16#20#, REQ_CLR_EOL
180 Character'Pos ('D') mod 16#20#, REQ_DOWN_FIELD
183 Character'Pos ('E') mod 16#20#, REQ_END_FIELD
186 Character'Pos ('F') mod 16#20#, REQ_NEXT_PAGE
189 Character'Pos ('G') mod 16#20#, REQ_DEL_WORD
192 Character'Pos ('H') mod 16#20#, REQ_DEL_PREV
195 Character'Pos ('I') mod 16#20#, REQ_INS_CHAR
198 Character'Pos ('K') mod 16#20#, REQ_CLR_EOF
201 Character'Pos ('L') mod 16#20#, REQ_LEFT_FIELD
204 Character'Pos ('M') mod 16#20#, REQ_NEW_LINE
207 Character'Pos ('N') mod 16#20#, REQ_NEXT_FIELD
210 Character'Pos ('O') mod 16#20#, REQ_INS_LINE
213 Character'Pos ('P') mod 16#20#, REQ_PREV_FIELD
216 Character'Pos ('R') mod 16#20#, REQ_RIGHT_FIELD
219 Character'Pos ('S') mod 16#20#, REQ_BEG_FIELD
222 Character'Pos ('U') mod 16#20#, REQ_UP_FIELD
225 Character'Pos ('V') mod 16#20#, REQ_DEL_CHAR
228 Character'Pos ('W') mod 16#20#, REQ_NEXT_WORD
231 Character'Pos ('X') mod 16#20#, REQ_CLR_FIELD
234 Character'Pos ('Y') mod 16#20#, REQ_DEL_LINE
237 Character'Pos ('Z') mod 16#20#, REQ_PREV_CHOICE
240 Character'Pos ('[') mod 16#20#, -- ESCAPE
241 Form_Request_Code'Last + 1
244 Key_Backspace, REQ_DEL_PREV
247 KEY_DOWN, REQ_DOWN_CHAR
250 Key_End, REQ_LAST_FIELD
253 Key_Home, REQ_FIRST_FIELD
256 KEY_LEFT, REQ_LEFT_CHAR
259 KEY_LL, REQ_LAST_FIELD
262 Key_Next, REQ_NEXT_FIELD
265 KEY_NPAGE, REQ_NEXT_PAGE
268 KEY_PPAGE, REQ_PREV_PAGE
271 Key_Previous, REQ_PREV_FIELD
274 KEY_RIGHT, REQ_RIGHT_CHAR
277 KEY_UP, REQ_UP_CHAR
280 Character'Pos ('Q') mod 16#20#, -- QUIT
281 Form_Request_Code'Last + 1 -- TODO MAX_FORM_COMMAND + 1
285 c : Key_Code := Getchar (w);
286 me : constant Field := Current (f);
288 begin
289 if c = Character'Pos (']') mod 16#20# then
290 if mode = REQ_INS_MODE then
291 mode := REQ_OVL_MODE;
292 else
293 mode := REQ_INS_MODE;
294 end if;
295 c := mode;
296 else
297 for n in lookup'Range loop
298 if lookup (n).code = c then
299 c := lookup (n).result;
300 exit;
301 end if;
302 end loop;
303 end if;
305 -- Force the field that the user is typing into to be in reverse video,
306 -- while the other fields are shown underlined.
307 if c <= Key_Max then
308 c := edit_secure (me, c);
309 Set_Background (me, (Reverse_Video => True, others => False));
310 elsif c <= Form_Request_Code'Last then
311 c := edit_secure (me, c);
312 Set_Background (me, (Under_Line => True, others => False));
313 end if;
314 return c;
315 end form_virtualize;
317 function my_form_driver (f : Form; c : Key_Code) return Boolean is
318 flag : constant Driver_Result := Driver (f, F_Validate_Field);
319 begin
320 if c = Form_Request_Code'Last + 1
321 and flag = Form_Ok then
322 return True;
323 else
324 Beep;
325 return False;
326 end if;
327 end my_form_driver;
329 function make_label (frow : Line_Position;
330 fcol : Column_Position;
331 label : String) return Field is
332 f : constant Field := Create (1, label'Length, frow, fcol, 0, 0);
333 o : Field_Option_Set := Get_Options (f);
334 begin
335 if f /= Null_Field then
336 Set_Buffer (f, 0, label);
337 o.Active := False;
338 Set_Options (f, o);
339 end if;
340 return f;
341 end make_label;
343 function make_field (frow : Line_Position;
344 fcol : Column_Position;
345 rows : Line_Count;
346 cols : Column_Count;
347 secure : Boolean) return Field is
348 f : Field;
349 use StringData;
350 len : myptr;
351 begin
352 if secure then
353 f := Create (rows, cols, frow, fcol, 0, 1);
354 else
355 f := Create (rows, cols, frow, fcol, 0, 0);
356 end if;
358 if f /= Null_Field then
359 Set_Background (f, (Under_Line => True, others => False));
360 len := new Integer;
361 len.all := 0;
362 Set_User_Data (f, len);
363 end if;
364 return f;
365 end make_field;
367 procedure display_form (f : Form) is
368 w : Window;
369 rows : Line_Count;
370 cols : Column_Count;
371 begin
372 Scale (f, rows, cols);
374 w := New_Window (rows + 2, cols + 4, 0, 0);
375 if w /= Null_Window then
376 Set_Window (f, w);
377 Set_Sub_Window (f, Derived_Window (w, rows, cols, 1, 2));
378 Box (w); -- 0,0
379 Set_KeyPad_Mode (w, True);
380 end if;
382 -- TODO if Post(f) /= Form_Ok then it's a procedure
383 declare
384 begin
385 Post (f);
386 exception
387 when
388 Eti_System_Error |
389 Eti_Bad_Argument |
390 Eti_Posted |
391 Eti_Connected |
392 Eti_Bad_State |
393 Eti_No_Room |
394 Eti_Not_Posted |
395 Eti_Unknown_Command |
396 Eti_No_Match |
397 Eti_Not_Selectable |
398 Eti_Not_Connected |
399 Eti_Request_Denied |
400 Eti_Invalid_Field |
401 Eti_Current =>
402 Refresh (w);
403 end;
404 -- end if;
405 end display_form;
407 procedure erase_form (f : Form) is
408 w : Window := Get_Window (f);
409 s : Window := Get_Sub_Window (f);
410 begin
411 Post (f, False);
412 Erase (w);
413 Refresh (w);
414 Delete (s);
415 Delete (w);
416 end erase_form;
418 finished : Boolean := False;
419 f : constant Field_Array_Access := new Field_Array (1 .. 12);
420 secure : Field;
421 myform : Form;
422 w : Window;
423 c : Key_Code;
424 result : Driver_Result;
425 begin
426 Move_Cursor (Line => 18, Column => 0);
427 Add (Str => "Defined form-traversal keys: ^Q/ESC- exit form");
428 Add (Ch => newl);
429 Add (Str => "^N -- go to next field ^P -- go to previous field");
430 Add (Ch => newl);
431 Add (Str => "Home -- go to first field End -- go to last field");
432 Add (Ch => newl);
433 Add (Str => "^L -- go to field to left ^R -- go to field to right");
434 Add (Ch => newl);
435 Add (Str => "^U -- move upward to field ^D -- move downward to field");
436 Add (Ch => newl);
437 Add (Str => "^W -- go to next word ^B -- go to previous word");
438 Add (Ch => newl);
439 Add (Str => "^S -- go to start of field ^E -- go to end of field");
440 Add (Ch => newl);
441 Add (Str => "^H -- delete previous char ^Y -- delete line");
442 Add (Ch => newl);
443 Add (Str => "^G -- delete current word ^C -- clear to end of line");
444 Add (Ch => newl);
445 Add (Str => "^K -- clear to end of field ^X -- clear field");
446 Add (Ch => newl);
447 Add (Str => "Arrow keys move within a field as you would expect.");
449 Add (Line => 4, Column => 57, Str => "Forms Entry Test");
451 Refresh;
453 -- describe the form
454 f.all (1) := make_label (0, 15, "Sample Form");
455 f.all (2) := make_label (2, 0, "Last Name");
456 f.all (3) := make_field (3, 0, 1, 18, False);
457 f.all (4) := make_label (2, 20, "First Name");
458 f.all (5) := make_field (3, 20, 1, 12, False);
459 f.all (6) := make_label (2, 34, "Middle Name");
460 f.all (7) := make_field (3, 34, 1, 12, False);
461 f.all (8) := make_label (5, 0, "Comments");
462 f.all (9) := make_field (6, 0, 4, 46, False);
463 f.all (10) := make_label (5, 20, "Password:");
464 f.all (11) := make_field (5, 30, 1, 9, True);
465 secure := f.all (11);
466 f.all (12) := Null_Field;
468 myform := New_Form (f);
470 display_form (myform);
472 w := Get_Window (myform);
473 Set_Raw_Mode (SwitchOn => True);
474 Set_NL_Mode (SwitchOn => True); -- lets us read ^M's
475 while not finished loop
476 c := form_virtualize (myform, w);
477 result := Driver (myform, c);
478 case result is
479 when Form_Ok =>
480 Add (Line => 5, Column => 57, Str => Get_Buffer (secure, 1));
481 Clear_To_End_Of_Line;
482 Refresh;
483 when Unknown_Request =>
484 finished := my_form_driver (myform, c);
485 when others =>
486 Beep;
487 end case;
488 end loop;
490 erase_form (myform);
492 -- TODO Free_Form(myform);
493 -- for (c = 0; f[c] != 0; c++) free_field(f[c]);
494 Set_Raw_Mode (SwitchOn => False);
495 Set_NL_Mode (SwitchOn => True);
497 end ncurses2.demo_forms;