NAME
FLTK::Input - One-line text input field
Description
This is the FLTK text input widget. It displays a single line of text and lets the user edit it. The text may contain any bytes (even \0
). The bytes 0..31
are displayed in ^X
notation, the rest are interpreted as UTF-8 (see utf8decode()
).
The default when()
is WHEN_RELEASE
. This is fine for a popup control panel where nothing happens until the panel is closed. But for most other uses of the input field you want to change it. Useful values are:
WHEN_NEVER
-
The callback is not done, but
changed()
is turned on. WHEN_CHANGED
-
The callback is done each time the text is changed by the user.
WHEN_ENTER_KEY
-
Hitting the enter key after changing the text will cause the callback.
WHEN_ENTER_KEY_ALWAYS
-
The Enter key will do the callback even if the text has not changed. Useful for command fields. Also you need to do this if you want both the enter key and either
WHEN_CHANGED
orWHEN_RELEASE
, in this case you can tell if Enter was typed by testingevent_key() == FLTK::EnterKey
.
If you wish to restrict the text the user can type (such as limiting it to numbers, a particular length, etc), you should subclass this and override the replace()
function with a version that rejects changes you don't want to allow.
If you don't like the keybindings you can override handle()
to change them.
All arguments that are lengths or offsets into the strings are in bytes, not the UTF-8 characters they represent.
Functions
at
my $chr = $input->at( $index );
-
Same as
text()[$index]
, but may be faster in plausible implementations. No bounds checking is done.
copy
my $okay = $input->copy( $to_clipboard );
-
Put the current selection between
mark()
andposition()
into the selection or clipboard by callingcopy()
. Ifposition()
andmark()
are equal this does nothing (ie it does not clear the clipboard).If
to_clipboard
is true, the text is put into the user-visible cut & paste clipboard (this is probably what you want). Ifto_clipboard
is 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
paste(1)
and fltk will send the widget aPASTE
event with the text, which will cause it to be inserted.
cut
my $okay = $input->cut( );
-
Wrapper around
replace()
, this deletes the region between the point and the mark. It does nothing if they are equal. my $okay = $input->cut( $length );
-
Wrapper around
replace()
this deletes up tolength
characters after the point, or before the point iflength
is negative.length
is bounds checked. my $okay = $input->cut( $begin, $end );
-
Wrapper around
replace()
this deletes the characters betweenbegin
andend
. The values are clamped to the ends of the string, andend
can be less thanbegin
.
handle_key
my $handled = $input->handle_key( );
-
Handle
KEY
events. The defaulthandle()
method calls this. This provides an Emacs and Windows style of editing. Most Emacs commands are first run throughtry_shortcut()
to test if they are menu items for the program.Shift
: do not move the mark when moving the pointLeftKey
,Ctrl+B
: move left one characterCtrl+LeftKey
,Alt+B
: move left one wordRightKey
,Ctrl+F
: move right one characterCtrl+RightKey
,Alt+F
: move right one wordCtrl+A
: go to start of line, if already there select all textHomeKey
: go to start of lineEndKey
, Ctrl+E>: go to end of lineCtrl+Insert
: copyShift+Insert
: pasteShift+Delete
: cutDelete
,Ctrl+D
: delete region or one characterCtrl+Delete
,Alt+D
: delete region or one wordBackSpace
,Ctrl+H
: delete region or left one characterCtrl+BackSpace
,Alt+H
: delete region or left one wordReturn
,KeypadEnter
: ifwhen() & WHEN_ENTER_KEY
, and no shift keys held down, this selects all and does the callback. Otherwise key is ignored.Ctrl+K
: cuts from the position to the end of lineCtrl+C
: copyCtrl+T
: swap the two characters around point. If point is at end swap the last two characters.Ctrl+U
: delete all the textCtrl+V
: pasteCtrl+X
,Ctrl+W
: cutCtrl+Y
: redoCtrl+Z
,Ctrl+/
: undo- All printing characters are run through
compose()
and the result used to insert text.
For Input widgets in
WORDWRAP
mode, you can also do these:UpKey
,Ctrl+P
: move up one lineDownKey
,Ctrl+N
: move down one linePageUpKey
: move up 1 line less than the vertical widget sizePageDownKey
: move down 1 line less than the vertical widget sizeCtrl+HomeKey
,Alt+A
: got to start of textCtrl+EndKey
,Alt+E
: go to end of textReturn
,KeypadEnter
: inserts a newlineCtrl+O
: insert a newline and leave the cursor before it.
This method may be overridden for subclassing.
insert
my $okay = $input->insert( $text );
-
Wrapper around
replace()
. This inserts the string at the point and leaves the point after it. my $okay = $input->insert( $text, $length );
-
Wrapper around
replace()
. This insertslength
characters from thetext
(including\0
characters!) at the point and leaves the point after it.
line_end
my $index = $input->line_end( $position );
-
Returns the location of the next newline or wordwrap space at or after
position
.
line_start
my $index = $input->line_start( $position );
-
Returns the location of the start of the line containing the
position
.
mark
mouse_position
my $index = $input->mouse_position( $rectangle );
-
Figure out what character the most recent mouse event would be pointing to, assumming you drew the text by calling
draw()
with the samerectangle
. Returns0
if the mouse is before the first character, andsize()
if it is after the last one.
new
position
my $pos = $input->position( );
-
Returns the current location of the cursor.
$input->position( $new_position );
$input->position( $new_position, $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 X selection, call
copy()
if you want that.Changing these values causes a
redraw()
. The new values are bounds checked and limited to the size of the string.It is up to the caller to make sure the position and mark are at the borders of UTF-8 characters!
replace
my $return = $input->replace( $begin, $end, $text, $length );
-
This call does all editing of the text. It deletes the region between
begin
andend
(either one may be less or equal to the other), and then insertslength
(which may be zero) characters from the stringtext
at that point and leaves themark()
andposition()
after the insertion. If the text is changed the callback is done if thewhen()
flags indicate it should be done.begin
andend
are bounds checked so don't worry about sending values outside the length of the string.
reserve
$input->reserve( $newsize );
-
Reserve the interal private buffer of at least
newsize
bytes, even if the currenttext()
is not that long. Can be used to avoid unnecessary memory reallocations if you know you will be replacing thetext()
with a longer one later.
size
my $length = $input->size( );
-
Returns the number of characters in
text()
. This may be greater thanlength($input-
text())> if there areNULL
characters in it.
static_text
my $ret = $input->static_text( $string, $length );
-
Same as
text($string, $length)
, except it does not copy the string, instead it makestext()
return a pointer to$string
(unless$length
is 0, in which case it makes it point to a zero-length string).$string
must point to static memory that will not be altered until at least the Input widget is destroyed or thetext()
is changed again. If the user attempts to edit the string it is then copied to the internal buffer and the editing done there. This can save a lot of time and memory if your program is changing the string to various constants a lot but the user rarely edits it. my $ret = $input->static_text( $string );
-
Same as
$input->static_text($string, $string ? length($string) : 0)
.
text
my $different = $input->text( $string, $length );
-
Change the
text()
to return the firstlength
bytes ofstring
andsize()
to returnlength
, and set theposition()
tolength
and themark()
to zero (thus highlighting the entire value).Returns true if the bytes in the new string are different than the old string.
my $different = $input->text( $string );
-
Same as
$input->text($string, $string ? length($string) : 0)
. my $string = $input->text( );
-
The current string, as edited by the user.
size()
returns how many bytes are in the string.
undo
my $okay = $input->undo( );
-
If this is the most recent widget on which
replace()
was done on, this will undo thatreplace()
and probably several others (ie if the user has typed a lot of text it will undo all of it even though that was probably many calls toreplace()
). Returns true if any change was made.
up_down_position
$input->up_down_position( $position, $keepmark );
-
Does the correct thing for arrow keys.
position
must be the location of the start of a line. Sets the position (and mark ifkeepmark
is false) to somewhere afterposition
, such that pressing the arrows repeatedly will cause the point to move up and down.
word_end
my $index = $input->word_end( $position );
-
Returns the location of the next word boundary at or after
position
.
word_start
my $index = $input->word_start( $position );
-
Returns the location of the first word boundary at or before
position
.
xscroll
yscroll
Values for type
NORMAL
FLOAT_INPUT
INT_INPUT
SECRET
WORDWRAP
Subclassing FLTK::Input
The following methods may be overridden in subclasses of FLTK::Input:
handle
You may override handle
which accepts an event
and a rectangle
. The passed rectangle is the area the text is to be drawn into. This method is provided so a subclass can place the text into a subrectangle.
The default handle...
Handles
FOCUS
,UNFOCUS
May do callback on
HIDE
Any keystrokes call
handle_key()
Handles
PUSH
,DRAG
,RELEASE
to select regions of text, move the cursor, and start drag & drop. Double click selects words, triple click selects lines (triple click is broken on Windows).Receives drag&drop and accepts.
Handles
PASTE
events caused by accepting the drag&drop or by callingpaste()
(whichhandle_key()
does for^V
)
handle_key
Handle KEY
events. The default handle()
method calls this. This provides an Emacs and Windows style of editing. Most Emacs commands are first run through try_shortcut()
to test if they are menu items for the program.
Shift
: do not move the mark when moving the pointLeftKey
,Ctrl+B
: move left one characterCtrl+LeftKey
,Alt+B
: move left one wordRightKey
,Ctrl+F
: move right one characterCtrl+RightKey
,Alt+F
: move right one wordCtrl+A
: go to start of line, if already there select all textHomeKey
: go to start of lineEndKey
, Ctrl+E>: go to end of lineCtrl+Insert
: copyShift+Insert
: pasteShift+Delete
: cutDelete
,Ctrl+D
: delete region or one characterCtrl+Delete
,Alt+D
: delete region or one wordBackSpace
,Ctrl+H
: delete region or left one characterCtrl+BackSpace
,Alt+H
: delete region or left one wordReturn
,KeypadEnter
: ifwhen() & WHEN_ENTER_KEY
, and no shift keys held down, this selects all and does the callback. Otherwise key is ignored.Ctrl+K
: cuts from the position to the end of lineCtrl+C
: copyCtrl+T
: swap the two characters around point. If point is at end swap the last two characters.Ctrl+U
: delete all the textCtrl+V
: pasteCtrl+X
,Ctrl+W
: cutCtrl+Y
: redoCtrl+Z
,Ctrl+/
: undo- All printing characters are run through
compose()
and the result used to insert text.
For Input widgets in WORDWRAP
mode, you can also do these:
UpKey
,Ctrl+P
: move up one lineDownKey
,Ctrl+N
: move down one linePageUpKey
: move up 1 line less than the vertical widget sizePageDownKey
: move down 1 line less than the vertical widget sizeCtrl+HomeKey
,Alt+A
: got to start of textCtrl+EndKey
,Alt+E
: go to end of textReturn
,KeypadEnter
: inserts a newlineCtrl+O
: insert a newline and leave the cursor before it.
Author
Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
License and Legal
Copyright (C) 2008-2010 by Sanko Robinson <sanko@cpan.org>
This program is free software; you can redistribute it and/or modify it under the terms of The Artistic License 2.0. See the LICENSE file included with this distribution or notes on the Artistic License 2.0 for clarification.
When separated from the distribution, all original POD documentation is covered by the Creative Commons Attribution-Share Alike 3.0 License. See the clarification of the CCA-SA3.0.