diff --git a/FL/Fl_Input.H b/FL/Fl_Input.H index 10b0882df..9e5550cbb 100644 --- a/FL/Fl_Input.H +++ b/FL/Fl_Input.H @@ -44,57 +44,225 @@ Characters can be input using the keyboard or the character palette/map. Character composition is done using dead keys and/or a compose key as defined by the operating system. -

- -
- - - - - - - - - - - - - - - - - - - - - - - -
Mouse button 1Moves 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 2Insert 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 3Currently acts like button 1.
BackspaceDeletes one character to the left, or - deletes the selected region.
EnterMay cause the callback, see when().
^A or HomeGo to start of line.
^B or LeftMove left
^CCopy the selection to the clipboard
^D or DeleteDeletes one character to the right - or deletes the selected region.
^E or EndGo to the end of line.
^F or RightMove right
^KDelete to the end of line (next \\n character) - or deletes a single \\n character. These deletions are all concatenated - into the clipboard.
^N or DownMove down (for Fl_Multiline_Input - only, otherwise it moves to the next input field).
^P or UpMove up (for Fl_Multiline_Input only, - otherwise it moves to the previous input field).
^UDelete everything.
^V or ^YPaste the clipboard
^X or ^WCopy 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+moveMove the cursor but also extend the - selection.
+

+ + +
Fl_Input keyboard and mouse bindings.
+ 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. +
+ Delete + + Deletes one character to the right, or deletes the selected region. + Combine with Shift for equivalent of ^X (copy+cut). +
+ Enter + + May cause the callback, see when(). +
+ +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Fl_Input platform specific keyboard bindings.
Windows/Linux Mac Function
^A Command-A + Selects all text in the widget. + +
^C Command-C + Copy the current selection to the clipboard. + +
^I ^I + Insert a tab. + +
^J ^J + Insert a Line Feed.
+ (Similar to literal 'Enter' character) + +
^L ^L + Insert a Form Feed. + +
^M ^M + Insert a Carriage Return. + +
^V,
Shift-Insert
Command-V + Paste the clipboard.
+ (Macs keyboards don't have "Insert" keys, + but if they did, Shift-Insert would work) + +
^X,
Shift-Delete
Command-X,
Shift-Delete
+ Cut.
+ Copy the selection to the clipboard and delete it. + (If there's no selection, Shift-Delete acts like Delete) + +
^Z Command-Z + 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-^Z Shift-Command-Z + Redo.
+ Currently same behavior as ^Z. + Reserved for future multilevel undo/redo. + +
Arrow Keys Arrow Keys + Standard cursor movement.
+ Can be combined with Shift to extend selection. + +
Home Command-Up,
Command-Left
+ Move to start of line.
+ Can be combined with Shift to extend selection. + +
End Command-Down,
Command-Right
+ Move to end of line.
+ Can be combined with Shift to extend selection. + +
Ctrl-HomeCommand-Up,
Command-PgUp,
Ctrl-Left
+ Move to top of document/field.
+ In single line input, moves to start of line. + In multiline input, moves to start of top line. + Can be combined with Shift to extend selection. + +
Ctrl-End Command-End,
Command-PgDn,
Ctrl-Right
+ Move to bottom of document/field.
+ In single line input, moves to end of line. + In multiline input, moves to end of last line. + Can be combined with Shift to extend selection. + +
Ctrl-Left Alt-Left + Word left.
+ Can be combined with Shift to extend selection. + +
Ctrl-Right Alt-Right + Word right.
+ Can be combined with Shift to extend selection. + +
Ctrl-Backspace Alt-Delete, Alt-Backspace + Delete word left. + +
Ctrl-Delete Alt-Delete + Delete word right. + +
*/ class FL_EXPORT Fl_Input : public Fl_Input_ { int handle_key(); int shift_position(int p); int shift_up_down_position(int p); void handle_mouse(int keepmark=0); + + // Private keyboard functions + int kf_lines_up(int repeat_num); + int kf_lines_down(int repeat_num); + int kf_page_up(); + int kf_page_down(); + int kf_insert_toggle(); + int kf_delete_word_right(); + int kf_delete_word_left(); + int kf_delete_sol(); + int kf_delete_eol(); + int kf_delete_char_right(); + int kf_delete_char_left(); + int kf_move_sol(); + int kf_move_eol(); + int kf_clear_eol(); + int kf_move_char_left(); + int kf_move_char_right(); + int kf_move_word_left(); + int kf_move_word_right(); + int kf_move_up_and_sol(); + int kf_move_down_and_eol(); + int kf_top(); + int kf_bottom(); + int kf_select_all(); + int kf_undo(); + int kf_redo(); + int kf_copy(); + int kf_paste(); + int kf_copy_cut(); + protected: void draw(); public: diff --git a/src/Fl_Input.cxx b/src/Fl_Input.cxx index aee5f0510..b0d656e2c 100644 --- a/src/Fl_Input.cxx +++ b/src/Fl_Input.cxx @@ -90,12 +90,220 @@ static const char *legal_fp_chars = 0L; static const char *legal_fp_chars = ".eE+-"; #endif +// Move cursor up specified #lines +// If OPTION_ARROW_FOCUS is disabled, return 1 to prevent focus navigation. +// +int Fl_Input::kf_lines_up(int repeat_num) { + int i = position(); + if (!line_start(i)) { + //UNNEEDED if (input_type()==FL_MULTILINE_INPUT && !Fl::option(Fl::OPTION_ARROW_FOCUS)) return 1; + return NORMAL_INPUT_MOVE; + } + while(repeat_num--) { + i = line_start(i); + if (!i) break; + i--; + } + shift_up_down_position(line_start(i)); + return 1; +} + +// Move cursor down specified #lines +// If OPTION_ARROW_FOCUS is disabled, return 1 to prevent focus navigation. +// +int Fl_Input::kf_lines_down(int repeat_num) { + int i = position(); + if (line_end(i) >= size()) { + //UNNEEDED if (input_type()==FL_MULTILINE_INPUT && !Fl::option(Fl::OPTION_ARROW_FOCUS)) return 1; + return NORMAL_INPUT_MOVE; + } + while (repeat_num--) { + i = line_end(i); + if (i >= size()) break; + i++; + } + shift_up_down_position(i); + return 1; +} + +// Move up a page +int Fl_Input::kf_page_up() { + return kf_lines_up(linesPerPage()); +} + +// Move down a page +int Fl_Input::kf_page_down() { + return kf_lines_down(linesPerPage()); +} + +// Toggle insert mode +int Fl_Input::kf_insert_toggle() { + return 1; // \todo: needs insert mode +} + +// Delete word right +int Fl_Input::kf_delete_word_right() { + if (readonly()) { fl_beep(); return 1; } + if (mark() != position()) return cut(); + cut(position(), word_end(position())); + return 1; +} + +// Delete word left +int Fl_Input::kf_delete_word_left() { + if (readonly()) { fl_beep(); return 1; } + if (mark() != position()) return cut(); + cut(word_start(position()), position()); + return 1; +} + +// Delete to start of line +int Fl_Input::kf_delete_sol() { + if (mark() != position()) return cut(); + cut(line_start(position()), position()); + return 1; +} + +// Delete to end of line +int Fl_Input::kf_delete_eol() { + if (readonly()) { fl_beep(); return 1; } + if (mark() != position()) return cut(); + cut(position(), line_end(position())); + return 1; +} + +int Fl_Input::kf_delete_char_right() { + if (readonly()) { fl_beep(); return 1; } + if (mark() != position()) return cut(); + else return cut(1); +} + +int Fl_Input::kf_delete_char_left() { + if (readonly()) { fl_beep(); return 1; } + if (mark() != position()) cut(); + else cut(-1); + return 1; +} + +// Move cursor to start of line +int Fl_Input::kf_move_sol() { + return shift_position(line_start(position())) + NORMAL_INPUT_MOVE; +} + +// Move cursor to end of line +int Fl_Input::kf_move_eol() { + return shift_position(line_end(position())) + NORMAL_INPUT_MOVE; +} + +// Clear to end of line +int Fl_Input::kf_clear_eol() { + if (readonly()) { fl_beep(); return 1; } + if (position()>=size()) return 0; + int i = line_end(position()); + if (i == position() && i < size()) i++; + cut(position(), i); + return copy_cuts(); +} + +// Move cursor one character to the left +// If OPTION_ARROW_FOCUS is disabled, return 1 to prevent focus navigation. +// +int Fl_Input::kf_move_char_left() { + int i = shift_position(position()-1) + NORMAL_INPUT_MOVE; + return Fl::option(Fl::OPTION_ARROW_FOCUS) ? i : 1; +} + +// Move cursor one character to the right +// If OPTION_ARROW_FOCUS is disabled, return 1 to prevent focus navigation. +// +int Fl_Input::kf_move_char_right() { + int i = shift_position(position()+1) + NORMAL_INPUT_MOVE; + return Fl::option(Fl::OPTION_ARROW_FOCUS) ? i : 1; +} + +// Move cursor word-left +int Fl_Input::kf_move_word_left() { + shift_position(word_start(position())); + return 1; +} + +// Move cursor word-right +int Fl_Input::kf_move_word_right() { + shift_position(word_end(position())); + return 1; +} + +// Move cursor up one line and to the start of line (paragraph up) +int Fl_Input::kf_move_up_and_sol() { + if (line_start(position())==position() && position()>0) + return shift_position(line_start(position()-1)) + NORMAL_INPUT_MOVE; + else + return shift_position(line_start(position())) + NORMAL_INPUT_MOVE; +} + +// Move cursor down one line and to the end of line (paragraph down) +int Fl_Input::kf_move_down_and_eol() { + if (line_end(position())==position() && position() ^X - } else { - ascii = ctrl('D'); // Del -> ^D - } + int selected = (position() != mark()) ? 1 : 0; + if (mods==0 && shift && selected) + return kf_copy_cut(); // Shift-Delete with selection (WP,NP,WOW,GE,KE,OF) + if (mods==0 && shift && !selected) + return kf_delete_char_right(); // Shift-Delete no selection (WP,NP,WOW,GE,KE,!OF) + if (mods==0) return kf_delete_char_right(); // Delete (Standard) + if (mods==FL_CTRL) return kf_delete_word_right(); // Ctrl-Delete (WP,!NP,WOW,GE,KE,!OF) + return 0; // ignore other combos, pass to parent #endif - break; + } + case FL_Left: #ifdef __APPLE__ - if (mods==0) { // char left - ascii = ctrl('B'); - } else if (mods==FL_ALT) { // word left - shift_position(word_start(position())); - return 1; - } else if (mods==FL_CTRL || mods==FL_META) { // start of line - shift_position(line_start(position())); - return 1; - } else return 1; + if (mods==0) return kf_move_char_left(); // Left (OSX-HIG) + if (mods==FL_ALT) return kf_move_word_left(); // Alt-Left (OSX-HIG) + if (mods==FL_META) return kf_move_sol(); // Meta-Left (OSX-HIG) + if (mods==FL_CTRL) return kf_move_sol(); // Ctrl-Left (TE/SA) + return 1; // other combos absorb and ignore #else - if (mods==0) { // char left - ascii = ctrl('B'); - } else if (mods==FL_CTRL) { // word left - shift_position(word_start(position())); - return 1; - } else return 1; + if (mods==0) return kf_move_char_left(); // Left (WP,NP,WOW,GE,KE,OF) + if (mods==FL_CTRL) return kf_move_word_left(); // Ctrl-Left (WP,NP,WOW,GE,KE,!OF) + if (mods==FL_META) return kf_move_char_left(); // Meta-Left (WP,NP,?WOW,GE,KE) + return 1; // other combos absorb and ignore #endif - break; + case FL_Right: #ifdef __APPLE__ - if (mods==0) { // char right - ascii = ctrl('F'); - } else if (mods==FL_ALT) { // word right - shift_position(word_end(position())); - return 1; - } else if (mods==FL_CTRL || mods==FL_META) { // end of line - shift_position(line_end(position())); - return 1; - } else return 1; + if (mods==0) return kf_move_char_right(); // Right (OSX-HIG) + if (mods==FL_ALT) return kf_move_word_right(); // Alt-Right (OSX-HIG) + if (mods==FL_META) return kf_move_eol(); // Meta-Right (OSX-HIG) + if (mods==FL_CTRL) return kf_move_eol(); // Ctrl-Right (TE/SA) + return 1; // other combos absorb and ignore #else - if (mods==0) { // char right - ascii = ctrl('F'); - } else if (mods==FL_CTRL) { // word right - shift_position(word_end(position())); - return 1; - } else return 1; -#endif // __APPLE__ - break; - case FL_Page_Up: -#ifdef __APPLE__ - if (mods==0) { // scroll text one page - // OS X scrolls the view, but does not move the cursor - // Fl_Input has no scroll control, so instead we move the cursor by one page - repeat_num = linesPerPage(); - ascii = ctrl('P'); - } else if (mods==FL_ALT) { // move cursor one page - repeat_num = linesPerPage(); - ascii = ctrl('P'); - } else return 1; - break; -#else - repeat_num = linesPerPage(); - // fall through + if (mods==0) return kf_move_char_right(); // Right (WP,NP,WOW,GE,KE,OF) + if (mods==FL_CTRL) return kf_move_word_right(); // Ctrl-Right (WP,NP,WOW,GE,KE,!OF) + if (mods==FL_META) return kf_move_char_right(); // Meta-Right (WP,NP,?WOW,GE,KE,!OF) + return 1; // other combos absorb and ignore #endif + case FL_Up: #ifdef __APPLE__ - if (mods==0) { // line up - ascii = ctrl('P'); - } else if (mods==FL_CTRL) { // scroll text down one page - // OS X scrolls the view, but does not move the cursor - // Fl_Input has no scroll control, so instead we move the cursor by one page - repeat_num = linesPerPage(); - ascii = ctrl('P'); - } else if (mods==FL_ALT) { // line start and up - if (line_start(position())==position() && position()>0) - return shift_position(line_start(position()-1)) + NORMAL_INPUT_MOVE; - else - return shift_position(line_start(position())) + NORMAL_INPUT_MOVE; - } else if (mods==FL_META) { // start of document - shift_position(0); - return 1; - } else return 1; + if (mods==0) return kf_lines_up(1); // Up (OSX-HIG) + if (mods==FL_CTRL) return kf_page_up(); // Ctrl-Up (TE !HIG) + if (mods==FL_ALT) return kf_move_up_and_sol(); // Alt-Up (OSX-HIG) + if (mods==FL_META) return kf_top(); // Meta-Up (OSX-HIG) + return 1; // other combos absorb and ignore #else - if (mods==0) { // line up - ascii = ctrl('P'); - } else if (mods==FL_CTRL) { // scroll text down one line - // Fl_Input has no scroll control, so instead we move the cursor by one page - ascii = ctrl('P'); - } else return 1; -#endif - break; - case FL_Page_Down: -#ifdef __APPLE__ - if (mods==0) { // scroll text one page - // OS X scrolls the view, but does not move the cursor - // Fl_Input has no scroll control, so instead we move the cursor by one page - repeat_num = linesPerPage(); - ascii = ctrl('N'); - } else if (mods==FL_ALT) { // move cursor one page - repeat_num = linesPerPage(); - ascii = ctrl('N'); - } else return 1; - break; -#else - repeat_num = linesPerPage(); - // fall through + if (mods==0) return kf_lines_up(1); // Up (WP,NP,WOW,GE,KE,OF) + if (mods==FL_CTRL) return kf_move_up_and_sol(); // Ctrl-Up (WP,!NP,WOW,GE,!KE,OF) + return 1; // other combos absorb and ignore #endif + case FL_Down: #ifdef __APPLE__ - if (mods==0) { // line down - ascii = ctrl('N'); - } else if (mods==FL_CTRL) { - // OS X scrolls the view, but does not move the cursor - // Fl_Input has no scroll control, so instead we move the cursor by one page - repeat_num = linesPerPage(); - ascii = ctrl('N'); - } else if (mods==FL_ALT) { // line end and down - if (line_end(position())==position() && position()=size()) return 0; - i = line_end(position()); - if (i == position() && i < size()) i++; - cut(position(), i); - return copy_cuts(); - case ctrl('N'): // go down one line - i = position(); - if (line_end(i) >= size()) { - if (input_type()==FL_MULTILINE_INPUT && !Fl::option(Fl::OPTION_ARROW_FOCUS)) return 1; - return NORMAL_INPUT_MOVE; - } - while (repeat_num--) { - i = line_end(i); - if (i >= size()) break; - i++; - } - shift_up_down_position(i); - return 1; - case ctrl('P'): // go up one line - i = position(); - if (!line_start(i)) { - if (input_type()==FL_MULTILINE_INPUT && !Fl::option(Fl::OPTION_ARROW_FOCUS)) return 1; - return NORMAL_INPUT_MOVE; - } - while(repeat_num--) { - i = line_start(i); - if (!i) break; - i--; - } - shift_up_down_position(line_start(i)); - return 1; - case ctrl('U'): // clear the whole document? - if (readonly()) { - fl_beep(); - return 1; - } - return cut(0, size()); - case ctrl('V'): // paste text - case ctrl('Y'): - if (readonly()) { - fl_beep(); - return 1; - } - Fl::paste(*this, 1); - return 1; - case ctrl('X'): // cut the selected text - case ctrl('W'): - if (readonly()) { - fl_beep(); - return 1; - } - copy(1); - return cut(); - case ctrl('Z'): // undo - case ctrl('_'): - if (readonly()) { - fl_beep(); - return 1; - } - return undo(); - case ctrl('I'): // insert literal - case ctrl('J'): - case ctrl('L'): - case ctrl('M'): - if (readonly()) { - fl_beep(); - return 1; - } + case ctrl('H'): + return kf_delete_char_left(); // Ctrl-H (!WP,!NP,!WOW,!WOX,TE,SA,GE,KE,OF) + case ctrl('I'): // Ctrl-I (literal Tab) (!WP,NP,!WOW,!GE,KE,OF) + case ctrl('J'): // Ctrl-J (literal Line Feed/Enter) (Standard) + case ctrl('L'): // Ctrl-L (literal Form Feed) (Standard) + case ctrl('M'): // Ctrl-M (literal Cr) (Standard) + if (readonly()) { fl_beep(); return 1; } // insert a few selected control characters literally: if (input_type() != FL_FLOAT_INPUT && input_type() != FL_INT_INPUT) return replace(position(), mark(), &ascii, 1); break; } - return 0; + return 0; // ignored } int Fl_Input::handle(int event) { @@ -526,8 +608,16 @@ int Fl_Input::handle(int event) { break; case FL_KEYBOARD: - if (Fl::event_key() == FL_Tab && mark() != position()) { - // Set the current cursor position to the end of the selection... + // Handle special case for multiline input with 'old tab behavior' + // where tab is entered as a character: make sure user attempt to 'tab over' + // widget doesn't destroy the field, replacing it with a tab character. + // + if (Fl::event_key() == FL_Tab // Tab key? + && !Fl::event_state(FL_SHIFT) // no shift? + //// PROPOSED && !tab_nav() // with tab navigation disabled? + && input_type() == FL_MULTILINE_INPUT // with a multiline input? + && (mark()==0 && position()==size())) { // while entire field selected? + // Set cursor to the end of the selection... if (mark() > position()) position(mark()); else