t1form.pp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. program form_basic;
  2. {
  3. Example 25. Forms Basics
  4. from ncurses howto
  5. Possible bug: moving cursors before first char doesn't seem to work.
  6. }
  7. {$MODE OBJFPC}
  8. uses
  9. ncurses, form;
  10. var
  11. field: array[0..2] of PFIELD;
  12. my_form: PFORM;
  13. ch: Longint;
  14. begin
  15. try
  16. (* Initialize curses *)
  17. initscr();
  18. cbreak();
  19. noecho();
  20. keypad(stdscr, TRUE);
  21. (* Initialize the fields *)
  22. field[0] := new_field(1, 10, 4, 18, 0, 0);
  23. field[1] := new_field(1, 10, 6, 18, 0, 0);
  24. field[2] := nil;
  25. (* Set field options *)
  26. set_field_back(field[0], A_UNDERLINE); { Print a line for the option }
  27. field_opts_off(field[0], O_AUTOSKIP); { Don't go to next field when this }
  28. { Field is filled up }
  29. set_field_back(field[1], A_UNDERLINE);
  30. field_opts_off(field[1], O_AUTOSKIP);
  31. (* Create the form and post it *)
  32. my_form := new_form(field);
  33. post_form(my_form);
  34. refresh();
  35. mvprintw(2, 10, 'Cursor up/down to move, F1 to Exit');
  36. mvprintw(4, 10, 'Value 1:');
  37. mvprintw(6, 10, 'Value 2:');
  38. refresh();
  39. (* Loop through to get user requests *)
  40. ch := getch();
  41. while ch <> KEY_F(1) do
  42. begin
  43. case ch of
  44. KEY_DOWN:
  45. (* Go to next field *)
  46. begin
  47. form_driver(my_form, REQ_NEXT_FIELD);
  48. { Go to the end of the present buffer
  49. Leaves nicely at the last character }
  50. form_driver(my_form, REQ_END_LINE);
  51. end;
  52. KEY_UP:
  53. (* Go to previous field *)
  54. begin
  55. form_driver(my_form, REQ_PREV_FIELD);
  56. form_driver(my_form, REQ_END_LINE);
  57. end;
  58. else
  59. { If this is a normal character, it gets
  60. Printed }
  61. form_driver(my_form, ch);
  62. end;
  63. ch := getch();
  64. end
  65. finally
  66. (* Un post form and free the memory *)
  67. unpost_form(my_form);
  68. free_form(my_form);
  69. free_field(field[0]);
  70. free_field(field[1]);
  71. endwin();
  72. end;
  73. end.