examples/form.rb (2406B) - raw
1 #!/usr/bin/ruby
2 # Copyright (c) 2004 by Simon Kaczor <skaczor@cox.net>
3 # Example from the NCurses Programming HOWTO
4 # This example uses module functions that are documented in the ncurses man page.
5 # For a more rubyish approach that uses Ruby objects see form2.rb
6 #
7 # The original example contained the following copyright:
8 # Copyright (c) 2001 by Pradeep Padala. This document may be distributed
9 # under the terms set forth in the LDP license at linuxdoc.org/COPYRIGHT.html.
10
11 require 'ncursesw.rb'
12
13 begin
14 scr = Ncurses.initscr()
15 Ncurses.cbreak()
16 Ncurses.noecho()
17 Ncurses.keypad(scr, true)
18
19 #create some fields
20 fields = Array.new
21 fields.push(Ncurses::Form.new_field(1,10,4,18,0,0))
22 fields.push(Ncurses::Form.new_field(1,10,6,18,0,0))
23
24 # set field options
25 Ncurses::Form.set_field_back(fields[0], Ncurses::A_UNDERLINE)
26 Ncurses::Form.field_opts_off(fields[0], Ncurses::Form::O_AUTOSKIP)
27
28 Ncurses::Form.set_field_back(fields[1], Ncurses::A_UNDERLINE)
29 Ncurses::Form.field_opts_off(fields[1], Ncurses::Form::O_AUTOSKIP)
30
31
32 # create a form
33 form = Ncurses::Form.new_form(fields)
34
35 # post the form and refresh the screen
36 Ncurses::Form.post_form(form)
37 scr.refresh()
38
39 Ncurses.mvprintw(4, 10, "Value 1:")
40 Ncurses.mvprintw(6, 10, "Value 2:")
41 scr.refresh()
42
43 # Loop through to get user requests
44 while((ch = scr.getch()) != Ncurses::KEY_F1) do
45 case(ch)
46 when Ncurses::KEY_DOWN
47 # Go to next field
48 Ncurses::Form.form_driver(form, Ncurses::Form::REQ_NEXT_FIELD)
49 # Go to the end of the present buffer
50 # Leaves nicely at the last character
51 Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE)
52
53 when Ncurses::KEY_UP
54 #Go to previous field
55 Ncurses::Form.form_driver(form, Ncurses::Form::REQ_PREV_FIELD)
56 Ncurses::Form.form_driver(form, Ncurses::Form::REQ_END_LINE);
57 else
58 # If this is a normal character, it gets Printed
59 Ncurses::Form.form_driver(form, ch)
60 end
61 end
62
63 # unpost and free form
64 Ncurses::Form.unpost_form(form);
65 Ncurses::Form.free_form(form)
66 Ncurses::Form.free_field(fields[0]);
67 Ncurses::Form.free_field(fields[1]);
68
69
70 #using class methods this time
71 # form = Ncurses::Form::FORM.new(fields)
72 # puts "Created form: #{form.inspect}\n"
73 # form.free()
74
75 ensure
76 # put the screen back in its normal state
77 Ncurses.echo()
78 Ncurses.nocbreak()
79 Ncurses.nl()
80 Ncurses.endwin()
81 end
82