Here’s how you might create a messagebox (image in previous post):

@mb = RubyCurses::MessageBox.new do title "Color selector" message "Choose a color" type :custom buttons %w[red green blue yellow] underlines [0,0,0,0] end

If you want one with an input box:

message "Enter your name" type :input default_value "rahul"

This will give an OK and Cancel button. There are other types for OK, Yes/No, Yes/No/Cancel etc.

The “underline” property will underline the given index in the name. Since ncurses underline does not work on my terminal (Highline does), I’ve changed the color. The normal key as well as Alt/Meta key is used as a hotkey. For input dialog boxes, the normal key will be processed by the edit box so the meta key was required.

Creating a form and fields:

@form = Form.new @win r = 1; c = 22; %w[ name age company].each do |w| field = Field.new @form do name w row r col c display_length 30 set_buffer "abcd #{w}" set_label Label.new @form, {'text' => w} end r += 1 end

A form takes a window parameter, and manages painting and navigation of its focusable fields. We’ve also tied in a label with the field, so it can put a default location for it.

All all properties of a field can be modified at any time. The given name parameter can be used to recall them.

@form.by_name["age"].display_length = 3 @form.by_name["age"].maxlen = 3 @form.by_name["age"].set_buffer "24" @form.by_name["age"].chars_allowed = /\d/ @form.by_name["name"].set_buffer "Not focusable" @form.by_name["name"].set_focusable(false) @form.by_name["company"].type(:ALPHA)

Defining variable fields and tying them to widgets

$results = Variable.new $results.value = "A variable" var = RubyCurses::Label.new @form, {'text_variable' => $results, "row" => r, "col" => 22}

This label changes whenever the value of result is changed.

Events

The form and fields have events such as ENTER and LEAVE.

@form.bind(:ENTER) { |f| f.label.bgcolor = $promptcolor } @form.bind(:LEAVE) { |f| f.label.bgcolor = $datacolor }

Buttons

Notable is the text and command.

ok_button = Button.new @form do text "OK" name "OK" row 18 col 22 end ok_button.command { |form| $results.value = "OK PRESS:";form.printstr(@window, 23,45, "OK CALLED") }

The name property of widgets is only for recalling them by name.

Radio Buttons

Here we create a label (using a hash, instead of the DSL way for a change).

Radio buttons are tied together by a Variable. On selection, the variable contains the value.

Label.new @form, {'text' => "Select a language:", "row" => 20, "col" => 22} $radio = Variable.new radio1 = RadioButton.new @form do text_variable $radio text "ruby" value "ruby" row 21 col 22 end radio2 = RadioButton.new @form do text_variable $radio text "java" value "java" row 22 col 22 end

Listboxes

Don’t flee out of boredom. Just a minute more!

I create an array, and then set it in the listbox call. At a later point, i insert some more data into offset 5. Data may be deleted or inserted externally.

mylist = [] 0.upto(100) { |v| mylist << "#{v} scrollable data" } field = Listbox.new @form do name "mylist" row r col 1 width 40 height 10 list mylist end field.insert 5, "hello ruby", "so long python", "farewell java", "RIP .Net"

Checkboxes

I have lazily tied the box to the same Variable, you needn’t.

checkbutton = CheckBox.new @form do text_variable $results #value = true onvalue "selected cb" offvalue "UNselected cb" text "Please click me" row 17 col 22 end

Now those configuration screens should be easier to build.

Multi-line fields

texta = TextArea.new @form do name "mytext" row 1 col 52 width 40 height 20 end texta << "hello there" << "we are testing deletes in this application" texta << "HELLO there" << "WE ARE testing deletes in this application"

Menubars, menuitems

Since my pickaxe had no samples of Tk Menu’s, I pulled out my old Java Swing book from my attic, and implemented menus the way those fellers do.

@mb = RubyCurses::MenuBar.new filemenu = RubyCurses::Menu.new "File" filemenu.add(item = RubyCurses::MenuItem.new("Open",'O')) item.command(@form) {|form| form.printstr(@window, 23,45, "Open CALLED"); } filemenu.insert_separator 1 item.accelerator = "Ctrl-X"

Now to post pictures of the screen. Its not well presented, since i’ve been busy creating widgets, but still…