## class fltk::Input

### Class Hierarchy

    fltk::Widget
       |
       +----fltk::Input
               |
               +----fltk::FloatInput, fltk::IntInput,
                    fltk::MultilineInput, fltk::SecretInput, fltk::Output

### Include Files

    #include <fltk/Input.h>

### Description

This is the FLTK text input widget. It displays a single line of text and lets the user edit it. Normally it is drawn with an inset box and a white background. The text may contain any characters (even 0). The unprintable control characters are displayed with ^X notation. The appearance of other characters will depend on your operating system.

Mouse button 1 | Moves the cursor to this point. Drag selects characters. Double click selects words. Triple click selects all text. Shift+click extends the selection. When you select text it is automatically copied to the clipboard.
Mouse button 2 | Insert the clipboard at the point clicked. You can also select a region and replace it with the clipboard by selecting the region with mouse button 2.
Mouse button 3 | Currently acts like button 1.
Backspace | Deletes one character to the left, or deletes the selected region.
Enter | May cause the callback, see when().
^A or Home | Go to start of line.
^B or Left | Move left
^C | Copy the selection to the clipboard
^D or Delete | Deletes one character to the right or deletes the selected region.
^E or End | Go to the end of line.
^F or Right | Move right
^K | Delete to the end of line (next \n character) or deletes a single \n character. These deletions are all concatenated into the clipboard.
^N or Down | Move down (for fltk::MultilineInput only, otherwise it moves to the next input field).
^O | Insert a newline and put the cursor before it.
^P or Up | Move up (for fltk::MultilineInput only, otherwise it moves to the previous input field).
^T | Swap the two characters around the cursor, or the two characters before it if at the end of line.
^U | Delete everything.
^V or ^Y | Paste the clipboard
^X or ^W | Copy the region to the clipboard and delete it.
^Z or ^_ | Undo. This is a single-level undo mechanism, but all adjacent deletions and insertions are concatenated into a single "undo". Often this will undo a lot more than you expected.
Shift+move | Move the cursor but also extend the selection.

RightCtrl or
Compose

Start a compose-character sequence. The next one or two keys typed define the character to insert:

Keys | Char | Keys | Char | Keys | Char | Keys | Char | Keys | Char | Keys | Char
--- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---
space | nbsp | `*` | ° | ``A` | À | `D-` | Ð | ``a` | à | `d-` | ð
`!` | ¡ | `+-` | ± | `'A` | Á | `~N` | Ñ | `'a` | á | `~n` | ñ
`c|` | ¢ | `2` | ² | `A^` | Â | ``O` | Ò | `^a` | â | ``o` | ò
`L-` | £ | `3` | ³ | `~A` | Ã | `'O` | Ó | `~a` | ã | `'o` | ó
`ox` | ¤ | `'` | ´ | `:A` | Ä | `^O` | Ô | `:a` | ä | `^o` | ô
`y=` | ¥ | `u` | µ | `*A` | Å | `~O` | Õ | `*a` | å | `~o` | õ
`|` | ¦ | `p` | ¶ | `AE` | Æ | `:O` | Ö | `ae` | æ | `:o` | ö
`&` | § | `.` | · | `,C` | Ç | `x` | × | `,c` | ç | `-:` | ÷
`:` | ¨ | `,` | ¸ | `E`` | È | `O/` | Ø | ``e` | è | `o/` | ø
`c` | © | `1` | ¹ | `'E` | É | ``U` | Ù | `'e` | é | ``u` | ù
`a` | ª | `o` | º | `^E` | Ê | `'U` | Ú | `^e` | ê | `'u` | ú
`<<` | « | `>>` | » | `:E` | Ë | `^U` | Û | `:e` | ë | `^u` | û
`~` | ¬ | `14` | 1/4 | ``I` | Ì | `:U` | Ü | ``i` | ì | `:u` | ü
`-` |  | `12` | 1/2 | `'I` | Í | `'Y` | Ý | `'i` | í | `'y` | ý
`r` | ® | `34` | 3/4 | `^I` | Î | `TH` | Þ | `^i` | î | `th` | þ
`_` | ¯ | `?` | ¿ | `:I` | Ï | `ss` | ß | `:i` | ï | `:y` | ÿ

For instance, to type "á" type [compose][a]['] or [compose]['][a].

The character "nbsp" (non-breaking space) is typed by using [compose][space].

The single-character sequences may be followed by a space if necessary to remove ambiguity. For instance, if you really want to type "ª~" rather than "ã" you must type [compose][a][space][~].

The same key may be used to "quote" control characters into the text. If you need a `^Q` character you can get one by typing [compose][Control+Q].

X may have a key on the keyboard defined as `XK_Multikey`. If so this key may be used as well as the right-hand control key. You can set this up with the program `xmodmap`.

If your keyboard is set to support a foreign language you should also be able to type "dead key" prefix characters. On X you will actually be able to see what dead key you typed, and if you then move the cursor without completing the sequence the accent will remain inserted.

### Methods

- fltk::Input
- ~Input
- index
- static_value
- textcolor

- textfont
- textsize
- copy
- cut
- drawtext

- handletext
- insert
- line_start
- line_end
- mark

- maybe_do_callback
- position
- replace
- size
- undo

- up_down_position
- value
- when
- word_start
- word_end

#### fltk::Input::Input(int x, int y, int w, int h, const char *label = 0)

Creates a new `fltk::Input` widget using the given position, size, and label string. The default boxtype is `fltk::DOWN_BOX`.

#### virtual fltk::Input::~Input()

The destructor removes the widget and any value associated with it.

#### int fltk::Input::word_start(int position) const

Returns the location of the first word boundary at or before position.

#### int fltk::Input::word_end(int position) const

Returns the location of the next word boundary at or after position.

#### int fltk::Input::line_start(int position) const

Returns the location of the start of the line containing the position.

#### int fltk::Input::line_end(int position) const

Returns the location of the next newline or wordwrap space at or after position.

#### void fltk::Input::drawtext(int,int,int,int)

Draw the text in the passed bounding box. If `damage() fltk::DAMAGE_ALL` is true, this assummes the area has already been erased to `color()`. Otherwise it does minimal update and erases the area itself.

#### void fltk::Input::handletext(int e,int,int,int,int)

Default handler for all event types. Your `handle()` method should call this for all events that it does not handle completely. You must pass it the same bounding box as passed to `draw()`. Handles `fltk::PUSH`, `fltk::DRAG`, `fltk::RELEASE` to select text, handles `fltk::FOCUS` and `fltk::UNFOCUS` to show and hide the cursor.

#### int fltk::Input::up_down_position(int i, int keepmark=0)

Do the correct thing for arrow keys. `i` must be the location of the start of a line. Sets the position (and mark if keepmark is zero) to somewhere after i, such that pressing the arrows repeatedly will cause the point to move up and down.

#### void fltk::Input::maybe_do_callback()

Does the callback if `changed()` is true or if `when() fltk::WHEN_NOT_CHANGED` is non-zero. You should call this at any point you think you should generate a callback.

#### int fltk::Input::position() const
int fltk::Input::position(int new_position, int new_mark)
int fltk::Input::position(int new_position_and_new_mark)

The input widget maintains two pointers into the string. The "position" is where the cursor is. The "mark" is the other end of the selected text. If they are equal then there is no selection. Changing this does not affect the clipboard (use `copy()` to do that).

Changing these values causes a `redraw()`. The new values are bounds checked. The return value is non-zero if the new position is different than the old one. `position(n)` is the same as `position(n,n)`. `mark(n)` is the same as `position(position(),n)`.

#### int fltk::Input::mark() const
int fltk::Input::mark(int new_mark)

Gets or sets the current selection mark. `mark(n)` is the same as `position(position(),n)`.

#### virtual int fltk::Input::replace(int a, int b, const char* insert, int length)

This call does all editing of the text. It deletes the region between `a` and `b` (either one may be less or equal to the other), and then inserts `length` (which may be zero) characters from the string `insert` at that point and leaves the `mark()` and `position()` after the insertion. Does the callback if `when()&fltk::WHEN_CHANGED` and there is a change.

Subclasses of fltk::Input can override this function to control what characters can be inserted into the text. A typical implementation will check the characters in the insertion for legality and then call `fltk::Input::replace` only if they are all ok.

Subclasses should return true if the keystroke that produced this call should be "eaten". If false is returned the keystroke is allowed to be tested as a shortcut for other widgets. In our experience it is best to return true even if you don't make changes. The base class version returns true always.

#### int fltk::Input::cut()
int fltk::Input::cut(int n)
int fltk::Input::cut(int a, int b);

These are wrappers around replace(). `fltk::Input::cut()` deletes the area between `mark()` and `position()`. `cut(n)` deletes `n` characters after the `position()`. `cut(-n)` deletes `n` characters before the `position()` . `cut(a,b)` deletes the characters between offsets `a` and `b`. `A`, `b`, and `n` are all clamped to the size of the string. The mark and point are left where the deleted text was.

If you want the data to go into the clipboard, do `fltk::Input::copy()` before calling `fltk::Input::cut()`.

#### int fltk::Input::insert(const char *t,int l=0)

Insert the string `t` at the current position, and leave the mark and position after it. If `l` is not zero then it is assummed to be `strlen(t)`.

#### int fltk::Input::copy(bool clipboard=true)

Put the current selection between `mark()` and `position()` into the clipboard. Does not replace the old clipboard contents if `position()` and `mark()` are equal. This calls `fltk::copy()`.

If clipboard is true the text is put into the user-visible cut & paste clipboard (this is probably what you want). If false it is put into the less-visible selection buffer that is used to do middle-mouse paste and drag & drop.

To paste the clipboard, call `fltk::paste(true)` and fltk will send the widget a `fltk::PASTE` event with the text, which will cause it to be inserted.

#### int fltk::Input::undo()

Does undo of several previous calls to `replace()`. Returns non-zero if any change was made.

#### const char *fltk::Input::value() const
int fltk::Input::value(const char*)
int fltk::Input::value(const char*, int)

The first form returns the current value, which is a pointer to the internal buffer and is valid only until the next event is handled.

The second two forms change the text and set the mark and the point to the end of it. The string is copied to the internal buffer. Passing `NULL` is the same as "". This returns non-zero if the new value is different than the current one. You can use the second version to directly set the length if you know it already or want to put nul's in the text.

#### int fltk::Input::static_value(const char*)
int fltk::Input::static_value(const char*, int)

Change the text and set the mark and the point to the end of it. The string is not copied. If the user edits the string it is copied to the internal buffer then. This can save a great deal of time and memory if your program is rapidly changing the values of text fields, but this will only work if the passed string remains unchanged until either the `fltk::Input` is destroyed or `value()` is called again.

#### int fltk::Input::size() const

Returns the number of characters in `value()`. This may be greater than `strlen(value())` if there are nul characters in it.

#### char fltk::Input::index(int) const

Same as `value()[n]`, but may be faster in plausible implementations. No bounds checking is done.

#### fltk::When fltk::Widget::when() const
void fltk::Widget::when(fltk::When)

Controls when callbacks are done. The following values are useful, the default value is `fltk::WHEN_RELEASE`:

- `0`: The callback is not done, but `changed()` is turned on.
- `fltk::WHEN_CHANGED`: The callback is done each time the text is changed by the user.
- `fltk::WHEN_RELEASE`: The callback will be done when this widget loses the focus, including when the window is unmapped. This is a useful value for text fields in a panel where doing the callback on every change is wasteful. However the callback will also happen if the mouse is moved out of the window, which means it should not do anything visible (like pop up an error message). You might do better setting this to zero, and scanning all the items for `changed()` when the OK button on a panel is pressed.
- `fltk::WHEN_ENTER_KEY`: If the user types the Enter key, the entire text is selected, and the callback is done if the text has changed. Normally the Enter key will navigate to the next field (or insert a newline for a `fltk::Mulitline_Input`), this changes the behavior.
- `fltk::WHEN_ENTER_KEY|fltk::WHEN_NOT_CHANGED`: The Enter key will do the callback even if the text has not changed. Useful for command fields.

#### fltk::Color fltk::Input::textcolor() const
void fltk::Input::textcolor(fltk::Color)

Gets or sets the color of the text in the input field.

#### fltk::Font fltk::Input::textfont() const
void fltk::Input::textfont(fltk::Font)

Gets or sets the font of the text in the input field.

#### uchar fltk::Input::textsize() const
void fltk::Input::textsize(uchar)

Gets or sets the size of the text in the input field.

#### fltk::Color fltk::Input::cursor_color() const
void fltk::Input::cursor_color(fltk::Color)

Get or set the color of the cursor. This is black by default.