2001-08-02 01:24:49 +04:00
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// "$Id$"
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2010-11-29 00:06:39 +03:00
|
|
|
// Copyright 2001-2010 by Bill Spitzak and others.
|
2002-01-01 18:11:33 +03:00
|
|
|
// Original code Copyright Mark Edel. Permission to distribute under
|
2010-12-02 03:43:07 +03:00
|
|
|
// the LGPL for the FLTK library granted by Mark Edel.
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// This library is free software. Distribution and use rights are outlined in
|
|
|
|
// the file "COPYING" which should have been included with this file. If this
|
|
|
|
// file is missing or damaged, see the license at:
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2011-07-19 08:49:30 +04:00
|
|
|
// http://www.fltk.org/COPYING.php
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
2005-04-16 04:13:17 +04:00
|
|
|
// Please report all bugs and problems on the following page:
|
|
|
|
//
|
|
|
|
// http://www.fltk.org/str.php
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|
|
|
|
|
2002-10-23 16:23:40 +04:00
|
|
|
#include <stdio.h>
|
2001-08-02 01:24:49 +04:00
|
|
|
#include <stdlib.h>
|
2002-04-11 15:52:43 +04:00
|
|
|
#include "flstring.h"
|
2001-08-02 01:24:49 +04:00
|
|
|
#include <ctype.h>
|
2002-10-23 16:23:40 +04:00
|
|
|
#include <FL/Fl.H>
|
2006-10-11 05:01:02 +04:00
|
|
|
#include <FL/Fl_Window.H>
|
2002-10-23 16:23:40 +04:00
|
|
|
#include <FL/Fl_Text_Editor.H>
|
2002-11-08 18:57:10 +03:00
|
|
|
#include <FL/fl_ask.H>
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
|
2009-04-13 22:53:24 +04:00
|
|
|
/* Keyboard Control Matrix
|
|
|
|
|
|
|
|
key\modifier plain Ctrl Alt Meta
|
|
|
|
left 1/1 13/9 0/13 0/9
|
|
|
|
right 2/2 14/10 0/14 0/10
|
|
|
|
up 3/19 21/7 0/15 0/17
|
|
|
|
down 4/20 22/8 0/16 0/18
|
|
|
|
home 9/5 17/0 0/0 0/0
|
|
|
|
end 10/6 18/0 0/0 0/0
|
|
|
|
page up 11/7 23/0 0/11 0/0
|
|
|
|
page down 12/8 24/0 0/12 0/0
|
|
|
|
(FLTK action / OS X action)
|
|
|
|
(adding the shift key extends the selection, all other combinations are no-op)
|
|
|
|
|
|
|
|
0: no-op
|
|
|
|
1: move cursor to the left, at line beginning wrap to end of prev line, at doc start no-op
|
|
|
|
2: move cursor to the right, at line end move to beginning of the next line, at doc end no-op
|
|
|
|
3: move cursor up, at doc top no-op
|
|
|
|
4: move cursor down, at doc bottom no-op
|
|
|
|
5: scroll display to top of text (cursor unchanged)
|
|
|
|
6: scroll display to end of text (cursor unchanged)
|
|
|
|
7: scroll text down one page (cursor unchanged)
|
|
|
|
8: scroll text up one page (cursor unchanged)
|
|
|
|
9: move cursor to beginning of line
|
|
|
|
10: move cursor to end of line
|
|
|
|
11: move cursor up one page and scroll down
|
|
|
|
12: move cursor down one page and scroll up
|
|
|
|
13: move to the beginning of the word or the previous word
|
|
|
|
14: move to the end of the word or the next word
|
|
|
|
15: if start of line: start of prev line, else start of this line
|
|
|
|
16: if end of line: end of next line, else end of this line
|
|
|
|
17: move cursor to the beginning of the document
|
|
|
|
18: move cursor to the end of the document
|
|
|
|
19: move cursor up, at doc top: home, at doc start: no-op)
|
|
|
|
20: move cursor down, at doc bot: end, at doc end: no-op)
|
|
|
|
21: scroll text down one line (cursor unchanged)
|
|
|
|
22: scroll text up one line (cursor unchanged)
|
|
|
|
23: move cursor to the beginning of the top of the screen
|
|
|
|
24: move cursor to the beginning of the bottom of the window
|
|
|
|
*/
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** The constructor creates a new text editor widget.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
Fl_Text_Editor::Fl_Text_Editor(int X, int Y, int W, int H, const char* l)
|
|
|
|
: Fl_Text_Display(X, Y, W, H, l) {
|
|
|
|
mCursorOn = 1;
|
|
|
|
insert_mode_ = 1;
|
|
|
|
key_bindings = 0;
|
2013-01-13 19:25:37 +04:00
|
|
|
set_flag(MAC_USE_ACCENTS_MENU);
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
// handle the default key bindings
|
|
|
|
add_default_key_bindings(&key_bindings);
|
|
|
|
|
|
|
|
// handle everything else
|
|
|
|
default_key_function(kf_default);
|
|
|
|
}
|
|
|
|
|
2008-10-03 11:48:38 +04:00
|
|
|
#ifndef FL_DOXYGEN
|
2001-08-02 01:24:49 +04:00
|
|
|
Fl_Text_Editor::Key_Binding* Fl_Text_Editor::global_key_bindings = 0;
|
2008-10-03 11:48:38 +04:00
|
|
|
#endif
|
2001-08-02 01:24:49 +04:00
|
|
|
|
|
|
|
// These are the default key bindings every widget should start with
|
|
|
|
static struct {
|
|
|
|
int key;
|
|
|
|
int state;
|
|
|
|
Fl_Text_Editor::Key_Func func;
|
|
|
|
} default_key_bindings[] = {
|
|
|
|
{ FL_Escape, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor::kf_ignore },
|
|
|
|
{ FL_Enter, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor::kf_enter },
|
|
|
|
{ FL_KP_Enter, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor::kf_enter },
|
|
|
|
{ FL_BackSpace, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor::kf_backspace },
|
|
|
|
{ FL_Insert, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor::kf_insert },
|
|
|
|
{ FL_Delete, FL_TEXT_EDITOR_ANY_STATE, Fl_Text_Editor::kf_delete },
|
|
|
|
{ FL_Home, 0, Fl_Text_Editor::kf_move },
|
|
|
|
{ FL_End, 0, Fl_Text_Editor::kf_move },
|
|
|
|
{ FL_Left, 0, Fl_Text_Editor::kf_move },
|
|
|
|
{ FL_Up, 0, Fl_Text_Editor::kf_move },
|
|
|
|
{ FL_Right, 0, Fl_Text_Editor::kf_move },
|
|
|
|
{ FL_Down, 0, Fl_Text_Editor::kf_move },
|
|
|
|
{ FL_Page_Up, 0, Fl_Text_Editor::kf_move },
|
|
|
|
{ FL_Page_Down, 0, Fl_Text_Editor::kf_move },
|
|
|
|
{ FL_Home, FL_SHIFT, Fl_Text_Editor::kf_shift_move },
|
|
|
|
{ FL_End, FL_SHIFT, Fl_Text_Editor::kf_shift_move },
|
|
|
|
{ FL_Left, FL_SHIFT, Fl_Text_Editor::kf_shift_move },
|
|
|
|
{ FL_Up, FL_SHIFT, Fl_Text_Editor::kf_shift_move },
|
|
|
|
{ FL_Right, FL_SHIFT, Fl_Text_Editor::kf_shift_move },
|
|
|
|
{ FL_Down, FL_SHIFT, Fl_Text_Editor::kf_shift_move },
|
|
|
|
{ FL_Page_Up, FL_SHIFT, Fl_Text_Editor::kf_shift_move },
|
|
|
|
{ FL_Page_Down, FL_SHIFT, Fl_Text_Editor::kf_shift_move },
|
|
|
|
{ FL_Home, FL_CTRL, Fl_Text_Editor::kf_ctrl_move },
|
|
|
|
{ FL_End, FL_CTRL, Fl_Text_Editor::kf_ctrl_move },
|
|
|
|
{ FL_Left, FL_CTRL, Fl_Text_Editor::kf_ctrl_move },
|
|
|
|
{ FL_Up, FL_CTRL, Fl_Text_Editor::kf_ctrl_move },
|
|
|
|
{ FL_Right, FL_CTRL, Fl_Text_Editor::kf_ctrl_move },
|
|
|
|
{ FL_Down, FL_CTRL, Fl_Text_Editor::kf_ctrl_move },
|
|
|
|
{ FL_Page_Up, FL_CTRL, Fl_Text_Editor::kf_ctrl_move },
|
|
|
|
{ FL_Page_Down, FL_CTRL, Fl_Text_Editor::kf_ctrl_move },
|
|
|
|
{ FL_Home, FL_CTRL|FL_SHIFT, Fl_Text_Editor::kf_c_s_move },
|
|
|
|
{ FL_End, FL_CTRL|FL_SHIFT, Fl_Text_Editor::kf_c_s_move },
|
|
|
|
{ FL_Left, FL_CTRL|FL_SHIFT, Fl_Text_Editor::kf_c_s_move },
|
|
|
|
{ FL_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor::kf_c_s_move },
|
|
|
|
{ FL_Right, FL_CTRL|FL_SHIFT, Fl_Text_Editor::kf_c_s_move },
|
|
|
|
{ FL_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor::kf_c_s_move },
|
|
|
|
{ FL_Page_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor::kf_c_s_move },
|
|
|
|
{ FL_Page_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor::kf_c_s_move },
|
2010-11-06 18:23:50 +03:00
|
|
|
//{ FL_Clear, 0, Fl_Text_Editor::delete_to_eol },
|
2002-11-05 22:53:50 +03:00
|
|
|
{ 'z', FL_CTRL, Fl_Text_Editor::kf_undo },
|
|
|
|
{ '/', FL_CTRL, Fl_Text_Editor::kf_undo },
|
2001-08-02 01:24:49 +04:00
|
|
|
{ 'x', FL_CTRL, Fl_Text_Editor::kf_cut },
|
2002-10-29 20:34:47 +03:00
|
|
|
{ FL_Delete, FL_SHIFT, Fl_Text_Editor::kf_cut },
|
2001-08-02 01:24:49 +04:00
|
|
|
{ 'c', FL_CTRL, Fl_Text_Editor::kf_copy },
|
2002-10-29 20:34:47 +03:00
|
|
|
{ FL_Insert, FL_CTRL, Fl_Text_Editor::kf_copy },
|
2001-08-02 01:24:49 +04:00
|
|
|
{ 'v', FL_CTRL, Fl_Text_Editor::kf_paste },
|
2002-10-29 20:34:47 +03:00
|
|
|
{ FL_Insert, FL_SHIFT, Fl_Text_Editor::kf_paste },
|
2004-07-27 00:52:52 +04:00
|
|
|
{ 'a', FL_CTRL, Fl_Text_Editor::kf_select_all },
|
2002-10-30 17:23:16 +03:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
// Define CMD+key accelerators...
|
2002-11-05 22:53:50 +03:00
|
|
|
{ 'z', FL_COMMAND, Fl_Text_Editor::kf_undo },
|
2002-10-30 17:23:16 +03:00
|
|
|
{ 'x', FL_COMMAND, Fl_Text_Editor::kf_cut },
|
|
|
|
{ 'c', FL_COMMAND, Fl_Text_Editor::kf_copy },
|
|
|
|
{ 'v', FL_COMMAND, Fl_Text_Editor::kf_paste },
|
2004-07-27 00:52:52 +04:00
|
|
|
{ 'a', FL_COMMAND, Fl_Text_Editor::kf_select_all },
|
2009-09-20 23:24:24 +04:00
|
|
|
{ FL_Left, FL_COMMAND, Fl_Text_Editor::kf_meta_move },
|
|
|
|
{ FL_Right, FL_COMMAND, Fl_Text_Editor::kf_meta_move },
|
|
|
|
{ FL_Up, FL_COMMAND, Fl_Text_Editor::kf_meta_move },
|
|
|
|
{ FL_Down, FL_COMMAND, Fl_Text_Editor::kf_meta_move },
|
|
|
|
{ FL_Left, FL_COMMAND|FL_SHIFT, Fl_Text_Editor::kf_m_s_move },
|
|
|
|
{ FL_Right, FL_COMMAND|FL_SHIFT, Fl_Text_Editor::kf_m_s_move },
|
|
|
|
{ FL_Up, FL_COMMAND|FL_SHIFT, Fl_Text_Editor::kf_m_s_move },
|
|
|
|
{ FL_Down, FL_COMMAND|FL_SHIFT, Fl_Text_Editor::kf_m_s_move },
|
2002-10-30 17:23:16 +03:00
|
|
|
#endif // __APPLE__
|
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
{ 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Adds all of the default editor key bindings to the specified key binding list.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
void Fl_Text_Editor::add_default_key_bindings(Key_Binding** list) {
|
|
|
|
for (int i = 0; default_key_bindings[i].key; i++) {
|
|
|
|
add_key_binding(default_key_bindings[i].key,
|
|
|
|
default_key_bindings[i].state,
|
|
|
|
default_key_bindings[i].func,
|
|
|
|
list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Returns the function associated with a key binding.*/
|
|
|
|
Fl_Text_Editor::Key_Func Fl_Text_Editor::bound_key_function(int key, int state, Key_Binding* list) {
|
2002-08-09 07:17:30 +04:00
|
|
|
Key_Binding* cur;
|
|
|
|
for (cur = list; cur; cur = cur->next)
|
|
|
|
if (cur->key == key)
|
|
|
|
if (cur->state == FL_TEXT_EDITOR_ANY_STATE || cur->state == state)
|
2001-08-02 01:24:49 +04:00
|
|
|
break;
|
2002-08-09 07:17:30 +04:00
|
|
|
if (!cur) return 0;
|
|
|
|
return cur->function;
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Removes all of the key bindings associated with the text editor or list.*/
|
|
|
|
void Fl_Text_Editor::remove_all_key_bindings(Key_Binding** list) {
|
2002-08-09 07:17:30 +04:00
|
|
|
Key_Binding *cur, *next;
|
|
|
|
for (cur = *list; cur; cur = next) {
|
|
|
|
next = cur->next;
|
|
|
|
delete cur;
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
*list = 0;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Removes the key binding associated with the key "key" of state "state" */
|
|
|
|
void Fl_Text_Editor::remove_key_binding(int key, int state, Key_Binding** list) {
|
2002-08-09 07:17:30 +04:00
|
|
|
Key_Binding *cur, *last = 0;
|
|
|
|
for (cur = *list; cur; last = cur, cur = cur->next)
|
|
|
|
if (cur->key == key && cur->state == state) break;
|
|
|
|
if (!cur) return;
|
|
|
|
if (last) last->next = cur->next;
|
|
|
|
else *list = cur->next;
|
|
|
|
delete cur;
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Adds a key of state "state" with the function "function" */
|
|
|
|
void Fl_Text_Editor::add_key_binding(int key, int state, Key_Func function,
|
2001-08-02 01:24:49 +04:00
|
|
|
Key_Binding** list) {
|
|
|
|
Key_Binding* kb = new Key_Binding;
|
|
|
|
kb->key = key;
|
|
|
|
kb->state = state;
|
|
|
|
kb->function = function;
|
|
|
|
kb->next = *list;
|
|
|
|
*list = kb;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static void kill_selection(Fl_Text_Editor* e) {
|
|
|
|
if (e->buffer()->selected()) {
|
|
|
|
e->insert_position(e->buffer()->primary_selection()->start());
|
|
|
|
e->buffer()->remove_selection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Inserts the text associated with the key */
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_default(int c, Fl_Text_Editor* e) {
|
2010-11-06 20:51:00 +03:00
|
|
|
// FIXME: this function is a mess! Fix this!
|
2001-08-02 01:24:49 +04:00
|
|
|
if (!c || (!isprint(c) && c != '\t')) return 0;
|
|
|
|
char s[2] = "\0";
|
|
|
|
s[0] = (char)c;
|
|
|
|
kill_selection(e);
|
|
|
|
if (e->insert_mode()) e->insert(s);
|
|
|
|
else e->overstrike(s);
|
|
|
|
e->show_insert_position();
|
2004-07-27 20:02:21 +04:00
|
|
|
e->set_changed();
|
2003-05-28 00:20:20 +04:00
|
|
|
if (e->when()&FL_WHEN_CHANGED) e->do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Ignores the keypress */
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_ignore(int, Fl_Text_Editor*) {
|
|
|
|
return 0; // don't handle
|
|
|
|
}
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Does a backspace in the current buffer.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_backspace(int, Fl_Text_Editor* e) {
|
2008-09-11 03:56:49 +04:00
|
|
|
if (!e->buffer()->selected() && e->move_left()) {
|
2010-11-06 17:29:12 +03:00
|
|
|
int p1 = e->insert_position();
|
|
|
|
int p2 = e->buffer()->next_char(p1);
|
|
|
|
e->buffer()->select(p1, p2);
|
2008-09-11 03:56:49 +04:00
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
kill_selection(e);
|
|
|
|
e->show_insert_position();
|
2004-07-27 20:02:21 +04:00
|
|
|
e->set_changed();
|
|
|
|
if (e->when()&FL_WHEN_CHANGED) e->do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Inserts a newline at the current cursor position */
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_enter(int, Fl_Text_Editor* e) {
|
|
|
|
kill_selection(e);
|
|
|
|
e->insert("\n");
|
|
|
|
e->show_insert_position();
|
2004-07-27 20:02:21 +04:00
|
|
|
e->set_changed();
|
|
|
|
if (e->when()&FL_WHEN_CHANGED) e->do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void fl_text_drag_me(int pos, Fl_Text_Display* d);
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor in the direction indicated by key c.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_move(int c, Fl_Text_Editor* e) {
|
|
|
|
int i;
|
|
|
|
int selected = e->buffer()->selected();
|
|
|
|
if (!selected)
|
|
|
|
e->dragPos = e->insert_position();
|
|
|
|
e->buffer()->unselect();
|
2010-12-14 20:07:45 +03:00
|
|
|
Fl::copy("", 0, 0);
|
2001-08-02 01:24:49 +04:00
|
|
|
switch (c) {
|
|
|
|
case FL_Home:
|
|
|
|
e->insert_position(e->buffer()->line_start(e->insert_position()));
|
|
|
|
break;
|
|
|
|
case FL_End:
|
|
|
|
e->insert_position(e->buffer()->line_end(e->insert_position()));
|
|
|
|
break;
|
|
|
|
case FL_Left:
|
|
|
|
e->move_left();
|
|
|
|
break;
|
|
|
|
case FL_Right:
|
|
|
|
e->move_right();
|
|
|
|
break;
|
|
|
|
case FL_Up:
|
|
|
|
e->move_up();
|
|
|
|
break;
|
|
|
|
case FL_Down:
|
|
|
|
e->move_down();
|
|
|
|
break;
|
|
|
|
case FL_Page_Up:
|
|
|
|
for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_up();
|
|
|
|
break;
|
|
|
|
case FL_Page_Down:
|
|
|
|
for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_down();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
e->show_insert_position();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Extends the current selection in the direction of key c.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_shift_move(int c, Fl_Text_Editor* e) {
|
|
|
|
kf_move(c, e);
|
|
|
|
fl_text_drag_me(e->insert_position(), e);
|
2010-12-13 15:30:49 +03:00
|
|
|
char *copy = e->buffer()->selection_text();
|
|
|
|
if (copy) {
|
2012-04-05 09:12:30 +04:00
|
|
|
Fl::copy(copy, (int) strlen(copy), 0);
|
2010-12-13 15:30:49 +03:00
|
|
|
free(copy);
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the current text cursor in the direction indicated by control key */
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_ctrl_move(int c, Fl_Text_Editor* e) {
|
|
|
|
if (!e->buffer()->selected())
|
|
|
|
e->dragPos = e->insert_position();
|
|
|
|
if (c != FL_Up && c != FL_Down) {
|
|
|
|
e->buffer()->unselect();
|
2010-12-14 20:07:45 +03:00
|
|
|
Fl::copy("", 0, 0);
|
2001-08-02 01:24:49 +04:00
|
|
|
e->show_insert_position();
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case FL_Home:
|
|
|
|
e->insert_position(0);
|
2003-04-02 00:14:16 +04:00
|
|
|
e->scroll(0, 0);
|
2001-08-02 01:24:49 +04:00
|
|
|
break;
|
|
|
|
case FL_End:
|
|
|
|
e->insert_position(e->buffer()->length());
|
2003-04-02 00:14:16 +04:00
|
|
|
e->scroll(e->count_lines(0, e->buffer()->length(), 1), 0);
|
2001-08-02 01:24:49 +04:00
|
|
|
break;
|
|
|
|
case FL_Left:
|
|
|
|
e->previous_word();
|
|
|
|
break;
|
|
|
|
case FL_Right:
|
|
|
|
e->next_word();
|
|
|
|
break;
|
|
|
|
case FL_Up:
|
|
|
|
e->scroll(e->mTopLineNum-1, e->mHorizOffset);
|
|
|
|
break;
|
|
|
|
case FL_Down:
|
|
|
|
e->scroll(e->mTopLineNum+1, e->mHorizOffset);
|
|
|
|
break;
|
|
|
|
case FL_Page_Up:
|
|
|
|
e->insert_position(e->mLineStarts[0]);
|
|
|
|
break;
|
|
|
|
case FL_Page_Down:
|
|
|
|
e->insert_position(e->mLineStarts[e->mNVisibleLines-2]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-09-20 23:24:24 +04:00
|
|
|
/** Moves the current text cursor in the direction indicated by meta key */
|
|
|
|
int Fl_Text_Editor::kf_meta_move(int c, Fl_Text_Editor* e) {
|
|
|
|
if (!e->buffer()->selected())
|
|
|
|
e->dragPos = e->insert_position();
|
|
|
|
if (c != FL_Up && c != FL_Down) {
|
|
|
|
e->buffer()->unselect();
|
2010-12-14 20:07:45 +03:00
|
|
|
Fl::copy("", 0, 0);
|
2009-09-20 23:24:24 +04:00
|
|
|
e->show_insert_position();
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case FL_Up: // top of buffer
|
|
|
|
e->insert_position(0);
|
|
|
|
e->scroll(0, 0);
|
|
|
|
break;
|
|
|
|
case FL_Down: // end of buffer
|
|
|
|
e->insert_position(e->buffer()->length());
|
|
|
|
e->scroll(e->count_lines(0, e->buffer()->length(), 1), 0);
|
|
|
|
break;
|
|
|
|
case FL_Left: // beginning of line
|
2009-09-21 01:55:21 +04:00
|
|
|
kf_move(FL_Home, e);
|
2009-09-20 23:24:24 +04:00
|
|
|
break;
|
|
|
|
case FL_Right: // end of line
|
2009-09-21 01:55:21 +04:00
|
|
|
kf_move(FL_End, e);
|
2009-09-20 23:24:24 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Extends the current selection in the direction indicated by meta key c. */
|
|
|
|
int Fl_Text_Editor::kf_m_s_move(int c, Fl_Text_Editor* e) {
|
|
|
|
kf_meta_move(c, e);
|
|
|
|
fl_text_drag_me(e->insert_position(), e);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Extends the current selection in the direction indicated by control key c. */
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_c_s_move(int c, Fl_Text_Editor* e) {
|
|
|
|
kf_ctrl_move(c, e);
|
|
|
|
fl_text_drag_me(e->insert_position(), e);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor to the beginning of the current line.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_home(int, Fl_Text_Editor* e) {
|
|
|
|
return kf_move(FL_Home, e);
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor to the end of the current line.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_end(int, Fl_Text_Editor* e) {
|
|
|
|
return kf_move(FL_End, e);
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor one character to the left.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_left(int, Fl_Text_Editor* e) {
|
|
|
|
return kf_move(FL_Left, e);
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor one line up.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_up(int, Fl_Text_Editor* e) {
|
|
|
|
return kf_move(FL_Up, e);
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor one character to the right.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_right(int, Fl_Text_Editor* e) {
|
|
|
|
return kf_move(FL_Right, e);
|
|
|
|
}
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor one line down.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_down(int, Fl_Text_Editor* e) {
|
|
|
|
return kf_move(FL_Down, e);
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor up one page.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_page_up(int, Fl_Text_Editor* e) {
|
|
|
|
return kf_move(FL_Page_Up, e);
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Moves the text cursor down one page.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_page_down(int, Fl_Text_Editor* e) {
|
|
|
|
return kf_move(FL_Page_Down, e);
|
|
|
|
}
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Toggles the insert mode in the text editor.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_insert(int, Fl_Text_Editor* e) {
|
|
|
|
e->insert_mode(e->insert_mode() ? 0 : 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Does a delete of selected text or the current character in the current buffer.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_delete(int, Fl_Text_Editor* e) {
|
2008-09-11 03:56:49 +04:00
|
|
|
if (!e->buffer()->selected()) {
|
2010-11-06 17:29:12 +03:00
|
|
|
int p1 = e->insert_position();
|
|
|
|
int p2 = e->buffer()->next_char(p1);
|
|
|
|
e->buffer()->select(p1, p2);
|
2008-09-11 03:56:49 +04:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
kill_selection(e);
|
|
|
|
e->show_insert_position();
|
2004-07-27 20:02:21 +04:00
|
|
|
e->set_changed();
|
|
|
|
if (e->when()&FL_WHEN_CHANGED) e->do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Does a copy of selected text or the current character in the current buffer.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_copy(int, Fl_Text_Editor* e) {
|
|
|
|
if (!e->buffer()->selected()) return 1;
|
|
|
|
const char *copy = e->buffer()->selection_text();
|
2012-04-05 09:12:30 +04:00
|
|
|
if (*copy) Fl::copy(copy, (int) strlen(copy), 1);
|
2001-08-02 01:24:49 +04:00
|
|
|
free((void*)copy);
|
|
|
|
e->show_insert_position();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Does a cut of selected text in the current buffer.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_cut(int c, Fl_Text_Editor* e) {
|
|
|
|
kf_copy(c, e);
|
|
|
|
kill_selection(e);
|
2004-07-27 20:02:21 +04:00
|
|
|
e->set_changed();
|
|
|
|
if (e->when()&FL_WHEN_CHANGED) e->do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Does a paste of selected text in the current buffer.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_paste(int, Fl_Text_Editor* e) {
|
|
|
|
kill_selection(e);
|
2002-03-07 22:22:58 +03:00
|
|
|
Fl::paste(*e, 1);
|
2001-08-02 01:24:49 +04:00
|
|
|
e->show_insert_position();
|
2004-07-27 20:02:21 +04:00
|
|
|
e->set_changed();
|
|
|
|
if (e->when()&FL_WHEN_CHANGED) e->do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Selects all text in the current buffer.*/
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::kf_select_all(int, Fl_Text_Editor* e) {
|
|
|
|
e->buffer()->select(0, e->buffer()->length());
|
2010-12-14 20:07:45 +03:00
|
|
|
const char *copy = e->buffer()->selection_text();
|
2012-04-05 09:12:30 +04:00
|
|
|
if (*copy) Fl::copy(copy, (int) strlen(copy), 0);
|
2010-12-14 20:07:45 +03:00
|
|
|
free((void*)copy);
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Undo last edit in the current buffer. Also deselect previous selection. */
|
2002-11-05 22:53:50 +03:00
|
|
|
int Fl_Text_Editor::kf_undo(int , Fl_Text_Editor* e) {
|
|
|
|
e->buffer()->unselect();
|
2010-12-14 20:07:45 +03:00
|
|
|
Fl::copy("", 0, 0);
|
2002-11-05 22:53:50 +03:00
|
|
|
int crsr;
|
|
|
|
int ret = e->buffer()->undo(&crsr);
|
|
|
|
e->insert_position(crsr);
|
|
|
|
e->show_insert_position();
|
2004-07-27 20:02:21 +04:00
|
|
|
e->set_changed();
|
|
|
|
if (e->when()&FL_WHEN_CHANGED) e->do_callback();
|
2002-11-05 22:53:50 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** Handles a key press in the editor */
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::handle_key() {
|
2003-05-28 00:20:20 +04:00
|
|
|
// Call FLTK's rules to try to turn this into a printing character.
|
2001-08-02 01:24:49 +04:00
|
|
|
// This uses the right-hand ctrl key as a "compose prefix" and returns
|
|
|
|
// the changes that should be made to the text, as a number of
|
|
|
|
// bytes to delete and a string to insert:
|
2008-09-11 03:56:49 +04:00
|
|
|
int del = 0;
|
2001-08-02 01:24:49 +04:00
|
|
|
if (Fl::compose(del)) {
|
2010-11-06 17:29:12 +03:00
|
|
|
if (del) {
|
2012-12-16 02:03:55 +04:00
|
|
|
// del is a number of bytes
|
|
|
|
int dp = insert_position() - del;
|
|
|
|
if ( dp < 0 ) dp = 0;
|
2010-11-06 17:29:12 +03:00
|
|
|
buffer()->select(dp, insert_position());
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
kill_selection(this);
|
|
|
|
if (Fl::event_length()) {
|
|
|
|
if (insert_mode()) insert(Fl::event_text());
|
|
|
|
else overstrike(Fl::event_text());
|
|
|
|
}
|
2012-12-24 15:45:07 +04:00
|
|
|
#ifdef __APPLE__
|
2013-02-08 15:58:19 +04:00
|
|
|
if (Fl::compose_state) {
|
2012-12-24 15:45:07 +04:00
|
|
|
int pos = this->insert_position();
|
2013-02-08 15:58:19 +04:00
|
|
|
this->buffer()->select(pos - Fl::compose_state, pos);
|
2012-12-24 15:45:07 +04:00
|
|
|
}
|
|
|
|
#endif
|
2001-08-02 01:24:49 +04:00
|
|
|
show_insert_position();
|
2004-07-27 20:02:21 +04:00
|
|
|
set_changed();
|
2003-05-28 00:20:20 +04:00
|
|
|
if (when()&FL_WHEN_CHANGED) do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int key = Fl::event_key(), state = Fl::event_state(), c = Fl::event_text()[0];
|
|
|
|
state &= FL_SHIFT|FL_CTRL|FL_ALT|FL_META; // only care about these states
|
|
|
|
Key_Func f;
|
|
|
|
f = bound_key_function(key, state, global_key_bindings);
|
|
|
|
if (!f) f = bound_key_function(key, state, key_bindings);
|
|
|
|
if (f) return f(key, this);
|
|
|
|
if (default_key_function_ && !state) return default_key_function_(c, this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-15 20:39:05 +04:00
|
|
|
/** does or does not a callback according to changed() and when() settings */
|
2002-11-05 09:45:41 +03:00
|
|
|
void Fl_Text_Editor::maybe_do_callback() {
|
2003-05-28 00:20:20 +04:00
|
|
|
// printf("Fl_Text_Editor::maybe_do_callback()\n");
|
|
|
|
// printf("changed()=%d, when()=%x\n", changed(), when());
|
2004-07-27 20:02:21 +04:00
|
|
|
if (changed() || (when()&FL_WHEN_NOT_CHANGED)) do_callback();
|
2002-11-05 09:45:41 +03:00
|
|
|
}
|
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
int Fl_Text_Editor::handle(int event) {
|
2010-11-11 22:08:52 +03:00
|
|
|
static int dndCursorPos;
|
|
|
|
|
2001-08-02 01:24:49 +04:00
|
|
|
if (!buffer()) return 0;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case FL_FOCUS:
|
|
|
|
show_cursor(mCursorOn); // redraws the cursor
|
2004-05-26 06:42:10 +04:00
|
|
|
if (buffer()->selected()) redraw(); // Redraw selections...
|
2001-12-03 21:29:49 +03:00
|
|
|
Fl::focus(this);
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case FL_UNFOCUS:
|
|
|
|
show_cursor(mCursorOn); // redraws the cursor
|
2012-12-24 15:45:07 +04:00
|
|
|
#ifdef __APPLE__
|
2013-02-08 15:58:19 +04:00
|
|
|
if (buffer()->selected() && Fl::compose_state) {
|
2012-12-24 15:45:07 +04:00
|
|
|
int pos = insert_position();
|
|
|
|
buffer()->select(pos, pos);
|
|
|
|
Fl::reset_marked_text();
|
|
|
|
}
|
|
|
|
#endif
|
2004-05-26 06:42:10 +04:00
|
|
|
if (buffer()->selected()) redraw(); // Redraw selections...
|
2002-11-05 09:45:41 +03:00
|
|
|
case FL_HIDE:
|
|
|
|
if (when() & FL_WHEN_RELEASE) maybe_do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
case FL_KEYBOARD:
|
2007-02-08 10:58:47 +03:00
|
|
|
if (active_r() && window() && this == Fl::belowmouse())
|
|
|
|
window()->cursor(FL_CURSOR_NONE);
|
2001-08-02 01:24:49 +04:00
|
|
|
return handle_key();
|
|
|
|
|
|
|
|
case FL_PASTE:
|
2002-11-08 18:57:10 +03:00
|
|
|
if (!Fl::event_text()) {
|
|
|
|
fl_beep();
|
|
|
|
return 1;
|
|
|
|
}
|
2001-08-02 01:24:49 +04:00
|
|
|
buffer()->remove_selection();
|
|
|
|
if (insert_mode()) insert(Fl::event_text());
|
|
|
|
else overstrike(Fl::event_text());
|
|
|
|
show_insert_position();
|
2004-07-27 20:02:21 +04:00
|
|
|
set_changed();
|
|
|
|
if (when()&FL_WHEN_CHANGED) do_callback();
|
2001-08-02 01:24:49 +04:00
|
|
|
return 1;
|
|
|
|
|
2003-04-02 00:14:16 +04:00
|
|
|
case FL_ENTER:
|
|
|
|
// MRS: WIN32 only? Need to test!
|
2001-08-02 01:24:49 +04:00
|
|
|
// case FL_MOVE:
|
2003-04-02 00:14:16 +04:00
|
|
|
show_cursor(mCursorOn);
|
|
|
|
return 1;
|
2006-08-17 15:10:54 +04:00
|
|
|
|
|
|
|
case FL_PUSH:
|
|
|
|
if (Fl::event_button() == 2) {
|
|
|
|
// don't let the text_display see this event
|
|
|
|
if (Fl_Group::handle(event)) return 1;
|
2010-11-11 22:08:52 +03:00
|
|
|
dragType = DRAG_NONE;
|
2010-12-14 20:07:45 +03:00
|
|
|
if(buffer()->selected()) {
|
|
|
|
buffer()->unselect();
|
|
|
|
}
|
|
|
|
int pos = xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS);
|
|
|
|
insert_position(pos);
|
2006-08-17 15:10:54 +04:00
|
|
|
Fl::paste(*this, 0);
|
|
|
|
Fl::focus(this);
|
|
|
|
set_changed();
|
|
|
|
if (when()&FL_WHEN_CHANGED) do_callback();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2008-04-23 18:19:58 +04:00
|
|
|
|
|
|
|
case FL_SHORTCUT:
|
|
|
|
if (!(shortcut() ? Fl::test_shortcut(shortcut()) : test_shortcut()))
|
|
|
|
return 0;
|
2008-04-24 00:08:39 +04:00
|
|
|
if (Fl::visible_focus() && handle(FL_FOCUS)) {
|
2008-04-23 18:19:58 +04:00
|
|
|
Fl::focus(this);
|
2008-04-24 00:08:39 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
2010-11-11 22:08:52 +03:00
|
|
|
|
|
|
|
// Handle drag'n'drop attempt by the user. This is a simplified
|
|
|
|
// implementation which allows dnd operations onto the scroll bars.
|
|
|
|
case FL_DND_ENTER: // save the current cursor position
|
|
|
|
if (Fl::visible_focus() && handle(FL_FOCUS))
|
|
|
|
Fl::focus(this);
|
|
|
|
show_cursor(mCursorOn);
|
|
|
|
dndCursorPos = insert_position();
|
|
|
|
/* fall through */
|
|
|
|
case FL_DND_DRAG: // show a temporary insertion cursor
|
|
|
|
insert_position(xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS));
|
|
|
|
return 1;
|
|
|
|
case FL_DND_LEAVE: // restore original cursor
|
|
|
|
insert_position(dndCursorPos);
|
|
|
|
return 1;
|
|
|
|
case FL_DND_RELEASE: // keep insertion cursor and wait for the FL_PASTE event
|
2013-12-13 20:28:38 +04:00
|
|
|
if (!dragging) buffer()->unselect(); // FL_PASTE must not destroy current selection if drag comes from outside
|
2010-11-11 22:08:52 +03:00
|
|
|
return 1;
|
2001-08-02 01:24:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Fl_Text_Display::handle(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2005-02-25 00:55:12 +03:00
|
|
|
// End of "$Id$".
|
2001-08-02 01:24:49 +04:00
|
|
|
//
|