| 1 |
// |
| 2 |
// "$Id: Fl_Text_Editor_mod.cxx 8034 2010-12-15 12:21:55Z AlbrechtS $" |
| 3 |
// |
| 4 |
// Copyright 2001-2010 by Bill Spitzak and others. |
| 5 |
// Original code Copyright Mark Edel. Permission to distribute under |
| 6 |
// the LGPL for the FLTK library granted by Mark Edel. |
| 7 |
// |
| 8 |
// This library is free software; you can redistribute it and/or |
| 9 |
// modify it under the terms of the GNU Library General Public |
| 10 |
// License as published by the Free Software Foundation; either |
| 11 |
// version 2 of the License, or (at your option) any later version. |
| 12 |
// |
| 13 |
// This library is distributed in the hope that it will be useful, |
| 14 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
// Library General Public License for more details. |
| 17 |
// |
| 18 |
// You should have received a copy of the GNU Library General Public |
| 19 |
// License along with this library; if not, write to the |
| 20 |
// |
| 21 |
// Free Software Foundation, Inc. |
| 22 |
// 51 Franklin Street, Fifth Floor |
| 23 |
// Boston, MA 02110-1301 USA. |
| 24 |
// |
| 25 |
// Please report all bugs and problems on the following page: |
| 26 |
// |
| 27 |
// http://www.fltk.org/str.php |
| 28 |
// |
| 29 |
|
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include "missing_strings.h" |
| 33 |
#include <ctype.h> |
| 34 |
#include <FL/Fl.H> |
| 35 |
#include <FL/Fl_Window.H> |
| 36 |
#include <FL/fl_ask.H> |
| 37 |
#include "Fl_Text_Editor_mod.H" |
| 38 |
|
| 39 |
#include <config.h> |
| 40 |
#include "debug.h" |
| 41 |
|
| 42 |
#if FLCLUSTER_FLTK_API_MAJOR == 1 && FLCLUSTER_FLTK_API_MINOR < 3 |
| 43 |
# error "FLTK versions less then 1.3 are no longer supported" |
| 44 |
#endif |
| 45 |
|
| 46 |
/* Keyboard Control Matrix |
| 47 |
|
| 48 |
key\modifier plain Ctrl Alt Meta |
| 49 |
--------------------------------------- |
| 50 |
left 1/1 13/9 0/13 0/9 |
| 51 |
right 2/2 14/10 0/14 0/10 |
| 52 |
up 3/19 21/7 0/15 0/17 |
| 53 |
down 4/20 22/8 0/16 0/18 |
| 54 |
home 9/5 17/0 0/0 0/0 |
| 55 |
end 10/6 18/0 0/0 0/0 |
| 56 |
page up 11/7 23/0 0/11 0/0 |
| 57 |
page down 12/8 24/0 0/12 0/0 |
| 58 |
|
| 59 |
(FLTK action / OS X action) |
| 60 |
(adding the shift key extends the selection, all other combinations are no-op) |
| 61 |
|
| 62 |
0: no-op |
| 63 |
1: move cursor to the left, at line beginning wrap to end of prev line, at doc start no-op |
| 64 |
2: move cursor to the right, at line end move to beginning of the next line, at doc end no-op |
| 65 |
3: move cursor up, at doc top no-op |
| 66 |
4: move cursor down, at doc bottom no-op |
| 67 |
5: scroll display to top of text (cursor unchanged) |
| 68 |
6: scroll display to end of text (cursor unchanged) |
| 69 |
7: scroll text down one page (cursor unchanged) |
| 70 |
8: scroll text up one page (cursor unchanged) |
| 71 |
9: move cursor to beginning of line |
| 72 |
10: move cursor to end of line |
| 73 |
11: move cursor up one page and scroll down |
| 74 |
12: move cursor down one page and scroll up |
| 75 |
13: move to the beginning of the word or the previous word |
| 76 |
14: move to the end of the word or the next word |
| 77 |
15: if start of line: start of prev line, else start of this line |
| 78 |
16: if end of line: end of next line, else end of this line |
| 79 |
17: move cursor to the beginning of the document |
| 80 |
18: move cursor to the end of the document |
| 81 |
19: move cursor up, at doc top: home, at doc start: no-op) |
| 82 |
20: move cursor down, at doc bot: end, at doc end: no-op) |
| 83 |
21: scroll text down one line (cursor unchanged) |
| 84 |
22: scroll text up one line (cursor unchanged) |
| 85 |
23: move cursor to the beginning of the top of the screen |
| 86 |
24: move cursor to the beginning of the bottom of the window |
| 87 |
*/ |
| 88 |
|
| 89 |
/** The constructor creates a new text editor widget.*/ |
| 90 |
Fl_Text_Editor_mod::Fl_Text_Editor_mod(int X, int Y, int W, int H, const char* l) |
| 91 |
: Fl_Text_Display_mod(X, Y, W, H, l) { |
| 92 |
mCursorOn = 1; |
| 93 |
insert_mode_ = 1; |
| 94 |
key_bindings = 0; |
| 95 |
|
| 96 |
// handle the default key bindings |
| 97 |
add_default_key_bindings(&key_bindings); |
| 98 |
|
| 99 |
// handle everything else |
| 100 |
default_key_function(kf_default); |
| 101 |
} |
| 102 |
|
| 103 |
#ifndef FL_DOXYGEN |
| 104 |
Fl_Text_Editor_mod::Key_Binding* Fl_Text_Editor_mod::global_key_bindings = 0; |
| 105 |
#endif |
| 106 |
|
| 107 |
// These are the default key bindings every widget should start with |
| 108 |
static struct { |
| 109 |
int key; |
| 110 |
int state; |
| 111 |
Fl_Text_Editor_mod::Key_Func func; |
| 112 |
} default_key_bindings[] = { |
| 113 |
{ FL_Escape, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_ignore }, |
| 114 |
{ FL_Enter, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_enter }, |
| 115 |
{ FL_KP_Enter, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_enter }, |
| 116 |
{ FL_BackSpace, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_backspace }, |
| 117 |
{ FL_Insert, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_insert }, |
| 118 |
{ FL_Delete, Fl_Text_Editor_mod_ANY_STATE, Fl_Text_Editor_mod::kf_delete }, |
| 119 |
{ FL_Home, 0, Fl_Text_Editor_mod::kf_move }, |
| 120 |
{ FL_End, 0, Fl_Text_Editor_mod::kf_move }, |
| 121 |
{ FL_Left, 0, Fl_Text_Editor_mod::kf_move }, |
| 122 |
{ FL_Up, 0, Fl_Text_Editor_mod::kf_move }, |
| 123 |
{ FL_Right, 0, Fl_Text_Editor_mod::kf_move }, |
| 124 |
{ FL_Down, 0, Fl_Text_Editor_mod::kf_move }, |
| 125 |
{ FL_Page_Up, 0, Fl_Text_Editor_mod::kf_move }, |
| 126 |
{ FL_Page_Down, 0, Fl_Text_Editor_mod::kf_move }, |
| 127 |
{ FL_Home, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, |
| 128 |
{ FL_End, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, |
| 129 |
{ FL_Left, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, |
| 130 |
{ FL_Up, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, |
| 131 |
{ FL_Right, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, |
| 132 |
{ FL_Down, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, |
| 133 |
{ FL_Page_Up, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, |
| 134 |
{ FL_Page_Down, FL_SHIFT, Fl_Text_Editor_mod::kf_shift_move }, |
| 135 |
{ FL_Home, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, |
| 136 |
{ FL_End, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, |
| 137 |
{ FL_Left, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, |
| 138 |
{ FL_Up, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, |
| 139 |
{ FL_Right, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, |
| 140 |
{ FL_Down, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, |
| 141 |
{ FL_Page_Up, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, |
| 142 |
{ FL_Page_Down, FL_CTRL, Fl_Text_Editor_mod::kf_ctrl_move }, |
| 143 |
{ FL_Home, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, |
| 144 |
{ FL_End, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, |
| 145 |
{ FL_Left, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, |
| 146 |
{ FL_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, |
| 147 |
{ FL_Right, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, |
| 148 |
{ FL_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, |
| 149 |
{ FL_Page_Up, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, |
| 150 |
{ FL_Page_Down, FL_CTRL|FL_SHIFT, Fl_Text_Editor_mod::kf_c_s_move }, |
| 151 |
//{ FL_Clear, 0, Fl_Text_Editor_mod::delete_to_eol }, |
| 152 |
{ 'z', FL_CTRL, Fl_Text_Editor_mod::kf_undo }, |
| 153 |
{ '/', FL_CTRL, Fl_Text_Editor_mod::kf_undo }, |
| 154 |
{ FL_Home, FL_SHIFT, Fl_Text_Editor_mod::kf_undo }, |
| 155 |
{ 'x', FL_CTRL, Fl_Text_Editor_mod::kf_cut }, |
| 156 |
{ FL_Delete, FL_SHIFT, Fl_Text_Editor_mod::kf_cut }, |
| 157 |
{ 'c', FL_CTRL, Fl_Text_Editor_mod::kf_copy }, |
| 158 |
{ FL_Insert, FL_CTRL, Fl_Text_Editor_mod::kf_copy }, |
| 159 |
{ 'v', FL_CTRL, Fl_Text_Editor_mod::kf_paste }, |
| 160 |
{ FL_Insert, FL_SHIFT, Fl_Text_Editor_mod::kf_paste }, |
| 161 |
{ 'a', FL_CTRL, Fl_Text_Editor_mod::kf_select_all }, |
| 162 |
|
| 163 |
#ifdef __APPLE__ |
| 164 |
// Define CMD+key accelerators... |
| 165 |
{ 'z', FL_COMMAND, Fl_Text_Editor_mod::kf_undo }, |
| 166 |
{ 'x', FL_COMMAND, Fl_Text_Editor_mod::kf_cut }, |
| 167 |
{ 'c', FL_COMMAND, Fl_Text_Editor_mod::kf_copy }, |
| 168 |
{ 'v', FL_COMMAND, Fl_Text_Editor_mod::kf_paste }, |
| 169 |
{ 'a', FL_COMMAND, Fl_Text_Editor_mod::kf_select_all }, |
| 170 |
{ FL_Left, FL_COMMAND, Fl_Text_Editor_mod::kf_meta_move }, |
| 171 |
{ FL_Right, FL_COMMAND, Fl_Text_Editor_mod::kf_meta_move }, |
| 172 |
{ FL_Up, FL_COMMAND, Fl_Text_Editor_mod::kf_meta_move }, |
| 173 |
{ FL_Down, FL_COMMAND, Fl_Text_Editor_mod::kf_meta_move }, |
| 174 |
{ FL_Left, FL_COMMAND|FL_SHIFT, Fl_Text_Editor_mod::kf_m_s_move }, |
| 175 |
{ FL_Right, FL_COMMAND|FL_SHIFT, Fl_Text_Editor_mod::kf_m_s_move }, |
| 176 |
{ FL_Up, FL_COMMAND|FL_SHIFT, Fl_Text_Editor_mod::kf_m_s_move }, |
| 177 |
{ FL_Down, FL_COMMAND|FL_SHIFT, Fl_Text_Editor_mod::kf_m_s_move }, |
| 178 |
#endif // __APPLE__ |
| 179 |
|
| 180 |
{ 0, 0, 0 } |
| 181 |
}; |
| 182 |
|
| 183 |
/** Adds all of the default editor key bindings to the specified key binding list.*/ |
| 184 |
void Fl_Text_Editor_mod::add_default_key_bindings(Key_Binding** list) { |
| 185 |
for (int i = 0; default_key_bindings[i].key; i++) { |
| 186 |
add_key_binding(default_key_bindings[i].key, |
| 187 |
default_key_bindings[i].state, |
| 188 |
default_key_bindings[i].func, |
| 189 |
list); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** Returns the function associated with a key binding.*/ |
| 194 |
Fl_Text_Editor_mod::Key_Func Fl_Text_Editor_mod::bound_key_function(int key, int state, Key_Binding* list) { |
| 195 |
Key_Binding* cur; |
| 196 |
if (!list) return 0; |
| 197 |
for (cur = list; cur; cur = cur->next) |
| 198 |
if (cur->key == key) |
| 199 |
if (cur->state == Fl_Text_Editor_mod_ANY_STATE || cur->state == state) |
| 200 |
break; |
| 201 |
if (!cur) return 0; |
| 202 |
return cur->function; |
| 203 |
} |
| 204 |
|
| 205 |
/** Removes all of the key bindings associated with the text editor or list.*/ |
| 206 |
void Fl_Text_Editor_mod::remove_all_key_bindings(Key_Binding** list) { |
| 207 |
Key_Binding *cur, *next; |
| 208 |
for (cur = *list; cur; cur = next) { |
| 209 |
next = cur->next; |
| 210 |
delete cur; |
| 211 |
} |
| 212 |
*list = 0; |
| 213 |
} |
| 214 |
|
| 215 |
/** Removes the key binding associated with the key "key" of state "state" */ |
| 216 |
void Fl_Text_Editor_mod::remove_key_binding(int key, int state, Key_Binding** list) { |
| 217 |
Key_Binding *cur, *last = 0; |
| 218 |
for (cur = *list; cur; last = cur, cur = cur->next) |
| 219 |
if (cur->key == key && cur->state == state) break; |
| 220 |
if (!cur) return; |
| 221 |
if (last) last->next = cur->next; |
| 222 |
else *list = cur->next; |
| 223 |
delete cur; |
| 224 |
} |
| 225 |
/** Adds a key of state "state" with the function "function" */ |
| 226 |
void Fl_Text_Editor_mod::add_key_binding(int key, int state, Key_Func function, |
| 227 |
Key_Binding** list) { |
| 228 |
Key_Binding* kb = new Key_Binding; |
| 229 |
kb->key = key; |
| 230 |
kb->state = state; |
| 231 |
kb->function = function; |
| 232 |
kb->next = *list; |
| 233 |
*list = kb; |
| 234 |
} |
| 235 |
|
| 236 |
//////////////////////////////////////////////////////////////// |
| 237 |
|
| 238 |
static void kill_selection(Fl_Text_Editor_mod* e) { |
| 239 |
if (e->buffer()->selected()) { |
| 240 |
e->insert_position(e->buffer()->primary_selection()->start()); |
| 241 |
e->buffer()->remove_selection(); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/** Inserts the text associated with the key */ |
| 246 |
int Fl_Text_Editor_mod::kf_default(int c, Fl_Text_Editor_mod* e) { |
| 247 |
// FIXME: this function is a mess! Fix this! |
| 248 |
if (!c || (!isprint(c) && c != '\t')) return 0; |
| 249 |
char s[2] = "\0"; |
| 250 |
s[0] = (char)c; |
| 251 |
kill_selection(e); |
| 252 |
if (e->insert_mode()) e->insert(s); |
| 253 |
else e->overstrike(s); |
| 254 |
e->show_insert_position(); |
| 255 |
e->set_changed(); |
| 256 |
if (e->when()&FL_WHEN_CHANGED) e->do_callback(); |
| 257 |
return 1; |
| 258 |
} |
| 259 |
|
| 260 |
/** Ignores the keypress */ |
| 261 |
int Fl_Text_Editor_mod::kf_ignore(int, Fl_Text_Editor_mod*) { |
| 262 |
return 0; // don't handle |
| 263 |
} |
| 264 |
/** Does a backspace in the current buffer.*/ |
| 265 |
int Fl_Text_Editor_mod::kf_backspace(int, Fl_Text_Editor_mod* e) { |
| 266 |
if (!e->buffer()->selected() && e->move_left()) { |
| 267 |
int p1 = e->insert_position(); |
| 268 |
int p2 = e->buffer()->next_char(p1); |
| 269 |
e->buffer()->select(p1, p2); |
| 270 |
} |
| 271 |
kill_selection(e); |
| 272 |
e->show_insert_position(); |
| 273 |
e->set_changed(); |
| 274 |
if (e->when()&FL_WHEN_CHANGED) e->do_callback(); |
| 275 |
return 1; |
| 276 |
} |
| 277 |
|
| 278 |
/** Inserts a newline at the current cursor position */ |
| 279 |
int Fl_Text_Editor_mod::kf_enter(int, Fl_Text_Editor_mod* e) { |
| 280 |
kill_selection(e); |
| 281 |
e->insert("\n"); |
| 282 |
e->show_insert_position(); |
| 283 |
e->set_changed(); |
| 284 |
if (e->when()&FL_WHEN_CHANGED) e->do_callback(); |
| 285 |
return 1; |
| 286 |
} |
| 287 |
|
| 288 |
extern void fl_text_drag_me(int pos, Fl_Text_Display_mod* d); |
| 289 |
/** Moves the text cursor in the direction indicated by key c.*/ |
| 290 |
int Fl_Text_Editor_mod::kf_move(int c, Fl_Text_Editor_mod* e) { |
| 291 |
int i; |
| 292 |
int selected = e->buffer()->selected(); |
| 293 |
if (!selected) |
| 294 |
e->dragPos = e->insert_position(); |
| 295 |
e->buffer()->unselect(); |
| 296 |
Fl::copy("", 0, 0); |
| 297 |
switch (c) { |
| 298 |
case FL_Home: |
| 299 |
e->insert_position(e->buffer()->line_start(e->insert_position())); |
| 300 |
break; |
| 301 |
case FL_End: |
| 302 |
e->insert_position(e->buffer()->line_end(e->insert_position())); |
| 303 |
break; |
| 304 |
case FL_Left: |
| 305 |
e->move_left(); |
| 306 |
break; |
| 307 |
case FL_Right: |
| 308 |
e->move_right(); |
| 309 |
break; |
| 310 |
case FL_Up: |
| 311 |
e->move_up(); |
| 312 |
break; |
| 313 |
case FL_Down: |
| 314 |
e->move_down(); |
| 315 |
break; |
| 316 |
case FL_Page_Up: |
| 317 |
for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_up(); |
| 318 |
break; |
| 319 |
case FL_Page_Down: |
| 320 |
for (i = 0; i < e->mNVisibleLines - 1; i++) e->move_down(); |
| 321 |
break; |
| 322 |
} |
| 323 |
e->show_insert_position(); |
| 324 |
return 1; |
| 325 |
} |
| 326 |
|
| 327 |
/** Extends the current selection in the direction of key c.*/ |
| 328 |
int Fl_Text_Editor_mod::kf_shift_move(int c, Fl_Text_Editor_mod* e) { |
| 329 |
kf_move(c, e); |
| 330 |
fl_text_drag_me(e->insert_position(), e); |
| 331 |
char *copy = e->buffer()->selection_text(); |
| 332 |
if (copy) { |
| 333 |
Fl::copy(copy, strlen(copy), 0); |
| 334 |
free(copy); |
| 335 |
} |
| 336 |
return 1; |
| 337 |
} |
| 338 |
/** Moves the current text cursor in the direction indicated by control key */ |
| 339 |
int Fl_Text_Editor_mod::kf_ctrl_move(int c, Fl_Text_Editor_mod* e) { |
| 340 |
if (!e->buffer()->selected()) |
| 341 |
e->dragPos = e->insert_position(); |
| 342 |
if (c != FL_Up && c != FL_Down) { |
| 343 |
e->buffer()->unselect(); |
| 344 |
Fl::copy("", 0, 0); |
| 345 |
e->show_insert_position(); |
| 346 |
} |
| 347 |
switch (c) { |
| 348 |
case FL_Home: |
| 349 |
e->insert_position(0); |
| 350 |
e->scroll(0, 0); |
| 351 |
break; |
| 352 |
case FL_End: |
| 353 |
e->insert_position(e->buffer()->length()); |
| 354 |
e->scroll(e->count_lines(0, e->buffer()->length(), 1), 0); |
| 355 |
break; |
| 356 |
case FL_Left: |
| 357 |
e->previous_word(); |
| 358 |
break; |
| 359 |
case FL_Right: |
| 360 |
e->next_word(); |
| 361 |
break; |
| 362 |
case FL_Up: |
| 363 |
e->scroll(e->mTopLineNum-1, e->mHorizOffset); |
| 364 |
break; |
| 365 |
case FL_Down: |
| 366 |
e->scroll(e->mTopLineNum+1, e->mHorizOffset); |
| 367 |
break; |
| 368 |
case FL_Page_Up: |
| 369 |
e->insert_position(e->mLineStarts[0]); |
| 370 |
break; |
| 371 |
case FL_Page_Down: |
| 372 |
e->insert_position(e->mLineStarts[e->mNVisibleLines-2]); |
| 373 |
break; |
| 374 |
} |
| 375 |
return 1; |
| 376 |
} |
| 377 |
|
| 378 |
/** Moves the current text cursor in the direction indicated by meta key */ |
| 379 |
int Fl_Text_Editor_mod::kf_meta_move(int c, Fl_Text_Editor_mod* e) { |
| 380 |
if (!e->buffer()->selected()) |
| 381 |
e->dragPos = e->insert_position(); |
| 382 |
if (c != FL_Up && c != FL_Down) { |
| 383 |
e->buffer()->unselect(); |
| 384 |
Fl::copy("", 0, 0); |
| 385 |
e->show_insert_position(); |
| 386 |
} |
| 387 |
switch (c) { |
| 388 |
case FL_Up: // top of buffer |
| 389 |
e->insert_position(0); |
| 390 |
e->scroll(0, 0); |
| 391 |
break; |
| 392 |
case FL_Down: // end of buffer |
| 393 |
e->insert_position(e->buffer()->length()); |
| 394 |
e->scroll(e->count_lines(0, e->buffer()->length(), 1), 0); |
| 395 |
break; |
| 396 |
case FL_Left: // beginning of line |
| 397 |
kf_move(FL_Home, e); |
| 398 |
break; |
| 399 |
case FL_Right: // end of line |
| 400 |
kf_move(FL_End, e); |
| 401 |
break; |
| 402 |
} |
| 403 |
return 1; |
| 404 |
} |
| 405 |
|
| 406 |
/** Extends the current selection in the direction indicated by meta key c. */ |
| 407 |
int Fl_Text_Editor_mod::kf_m_s_move(int c, Fl_Text_Editor_mod* e) { |
| 408 |
kf_meta_move(c, e); |
| 409 |
fl_text_drag_me(e->insert_position(), e); |
| 410 |
return 1; |
| 411 |
} |
| 412 |
|
| 413 |
/** Extends the current selection in the direction indicated by control key c. */ |
| 414 |
int Fl_Text_Editor_mod::kf_c_s_move(int c, Fl_Text_Editor_mod* e) { |
| 415 |
kf_ctrl_move(c, e); |
| 416 |
fl_text_drag_me(e->insert_position(), e); |
| 417 |
return 1; |
| 418 |
} |
| 419 |
|
| 420 |
/** Moves the text cursor to the beginning of the current line.*/ |
| 421 |
int Fl_Text_Editor_mod::kf_home(int, Fl_Text_Editor_mod* e) { |
| 422 |
return kf_move(FL_Home, e); |
| 423 |
} |
| 424 |
|
| 425 |
/** Moves the text cursor to the end of the current line.*/ |
| 426 |
int Fl_Text_Editor_mod::kf_end(int, Fl_Text_Editor_mod* e) { |
| 427 |
return kf_move(FL_End, e); |
| 428 |
} |
| 429 |
|
| 430 |
/** Moves the text cursor one character to the left.*/ |
| 431 |
int Fl_Text_Editor_mod::kf_left(int, Fl_Text_Editor_mod* e) { |
| 432 |
return kf_move(FL_Left, e); |
| 433 |
} |
| 434 |
|
| 435 |
/** Moves the text cursor one line up.*/ |
| 436 |
int Fl_Text_Editor_mod::kf_up(int, Fl_Text_Editor_mod* e) { |
| 437 |
return kf_move(FL_Up, e); |
| 438 |
} |
| 439 |
|
| 440 |
/** Moves the text cursor one character to the right.*/ |
| 441 |
int Fl_Text_Editor_mod::kf_right(int, Fl_Text_Editor_mod* e) { |
| 442 |
return kf_move(FL_Right, e); |
| 443 |
} |
| 444 |
/** Moves the text cursor one line down.*/ |
| 445 |
int Fl_Text_Editor_mod::kf_down(int, Fl_Text_Editor_mod* e) { |
| 446 |
return kf_move(FL_Down, e); |
| 447 |
} |
| 448 |
|
| 449 |
/** Moves the text cursor up one page.*/ |
| 450 |
int Fl_Text_Editor_mod::kf_page_up(int, Fl_Text_Editor_mod* e) { |
| 451 |
return kf_move(FL_Page_Up, e); |
| 452 |
} |
| 453 |
|
| 454 |
/** Moves the text cursor down one page.*/ |
| 455 |
int Fl_Text_Editor_mod::kf_page_down(int, Fl_Text_Editor_mod* e) { |
| 456 |
return kf_move(FL_Page_Down, e); |
| 457 |
} |
| 458 |
/** Toggles the insert mode in the text editor.*/ |
| 459 |
int Fl_Text_Editor_mod::kf_insert(int, Fl_Text_Editor_mod* e) { |
| 460 |
e->insert_mode(e->insert_mode() ? 0 : 1); |
| 461 |
return 1; |
| 462 |
} |
| 463 |
|
| 464 |
/** Does a delete of selected text or the current character in the current buffer.*/ |
| 465 |
int Fl_Text_Editor_mod::kf_delete(int, Fl_Text_Editor_mod* e) { |
| 466 |
if (!e->buffer()->selected()) { |
| 467 |
int p1 = e->insert_position(); |
| 468 |
int p2 = e->buffer()->next_char(p1); |
| 469 |
e->buffer()->select(p1, p2); |
| 470 |
} |
| 471 |
|
| 472 |
kill_selection(e); |
| 473 |
e->show_insert_position(); |
| 474 |
e->set_changed(); |
| 475 |
if (e->when()&FL_WHEN_CHANGED) e->do_callback(); |
| 476 |
return 1; |
| 477 |
} |
| 478 |
|
| 479 |
/** Does a copy of selected text or the current character in the current buffer.*/ |
| 480 |
int Fl_Text_Editor_mod::kf_copy(int, Fl_Text_Editor_mod* e) { |
| 481 |
if (!e->buffer()->selected()) return 1; |
| 482 |
const char *copy = e->buffer()->selection_text(); |
| 483 |
if (*copy) Fl::copy(copy, strlen(copy), 1); |
| 484 |
free((void*)copy); |
| 485 |
e->show_insert_position(); |
| 486 |
return 1; |
| 487 |
} |
| 488 |
|
| 489 |
/** Does a cut of selected text in the current buffer.*/ |
| 490 |
int Fl_Text_Editor_mod::kf_cut(int c, Fl_Text_Editor_mod* e) { |
| 491 |
kf_copy(c, e); |
| 492 |
kill_selection(e); |
| 493 |
e->set_changed(); |
| 494 |
if (e->when()&FL_WHEN_CHANGED) e->do_callback(); |
| 495 |
return 1; |
| 496 |
} |
| 497 |
|
| 498 |
/** Does a paste of selected text in the current buffer.*/ |
| 499 |
int Fl_Text_Editor_mod::kf_paste(int, Fl_Text_Editor_mod* e) { |
| 500 |
kill_selection(e); |
| 501 |
Fl::paste(*e, 1); |
| 502 |
e->show_insert_position(); |
| 503 |
e->set_changed(); |
| 504 |
if (e->when()&FL_WHEN_CHANGED) e->do_callback(); |
| 505 |
return 1; |
| 506 |
} |
| 507 |
|
| 508 |
/** Selects all text in the current buffer.*/ |
| 509 |
int Fl_Text_Editor_mod::kf_select_all(int, Fl_Text_Editor_mod* e) { |
| 510 |
e->buffer()->select(0, e->buffer()->length()); |
| 511 |
const char *copy = e->buffer()->selection_text(); |
| 512 |
if (*copy) Fl::copy(copy, strlen(copy), 0); |
| 513 |
free((void*)copy); |
| 514 |
return 1; |
| 515 |
} |
| 516 |
/** Undo last edit in the current buffer. Also deselect previous selection. */ |
| 517 |
int Fl_Text_Editor_mod::kf_undo(int , Fl_Text_Editor_mod* e) { |
| 518 |
e->buffer()->unselect(); |
| 519 |
Fl::copy("", 0, 0); |
| 520 |
int crsr; |
| 521 |
int ret = e->buffer()->undo(&crsr); |
| 522 |
e->insert_position(crsr); |
| 523 |
e->show_insert_position(); |
| 524 |
e->set_changed(); |
| 525 |
if (e->when()&FL_WHEN_CHANGED) e->do_callback(); |
| 526 |
return ret; |
| 527 |
} |
| 528 |
|
| 529 |
/** Handles a key press in the editor */ |
| 530 |
int Fl_Text_Editor_mod::handle_key() { |
| 531 |
// Call FLTK's rules to try to turn this into a printing character. |
| 532 |
// This uses the right-hand ctrl key as a "compose prefix" and returns |
| 533 |
// the changes that should be made to the text, as a number of |
| 534 |
// bytes to delete and a string to insert: |
| 535 |
|
| 536 |
int del = 0; |
| 537 |
int key = Fl::event_key(), state = Fl::event_state();//, c = Fl::event_text()[0]; |
| 538 |
if (key == FL_Caps_Lock) return 0; |
| 539 |
|
| 540 |
if (Fl::compose(del)) { |
| 541 |
if (del) { |
| 542 |
int dp = insert_position(), di = del; |
| 543 |
while (di--) dp = buffer()->prev_char_clipped(dp); |
| 544 |
buffer()->select(dp, insert_position()); |
| 545 |
} |
| 546 |
kill_selection(this); |
| 547 |
if (Fl::event_length()) { |
| 548 |
if (insert_mode()) insert(Fl::event_text()); |
| 549 |
else overstrike(Fl::event_text()); |
| 550 |
} |
| 551 |
show_insert_position(); |
| 552 |
set_changed(); |
| 553 |
if (when()&FL_WHEN_CHANGED) do_callback(); |
| 554 |
return 1; |
| 555 |
} |
| 556 |
|
| 557 |
state &= FL_SHIFT|FL_CTRL|FL_ALT|FL_META; // only care about these states |
| 558 |
Key_Func f; |
| 559 |
f = bound_key_function(key, state, global_key_bindings); |
| 560 |
|
| 561 |
if (!f && key_bindings) f = bound_key_function(key, state, key_bindings); |
| 562 |
|
| 563 |
if (f) return f(key, this); |
| 564 |
if (default_key_function_ && !state) return default_key_function_(key, this); |
| 565 |
return 0; |
| 566 |
} |
| 567 |
|
| 568 |
/** does or does not a callback according to changed() and when() settings */ |
| 569 |
void Fl_Text_Editor_mod::maybe_do_callback() { |
| 570 |
if (changed() || (when()&FL_WHEN_NOT_CHANGED)) do_callback(); |
| 571 |
} |
| 572 |
|
| 573 |
int Fl_Text_Editor_mod::handle(int event) { |
| 574 |
static int dndCursorPos; |
| 575 |
|
| 576 |
if (!buffer()) return 0; |
| 577 |
|
| 578 |
switch (event) { |
| 579 |
case FL_FOCUS: |
| 580 |
show_cursor(mCursorOn); // redraws the cursor |
| 581 |
if (buffer()->selected()) redraw(); // Redraw selections... |
| 582 |
Fl::focus(this); |
| 583 |
return 1; |
| 584 |
|
| 585 |
case FL_UNFOCUS: |
| 586 |
show_cursor(mCursorOn); // redraws the cursor |
| 587 |
if (buffer()->selected()) redraw(); // Redraw selections... |
| 588 |
case FL_HIDE: |
| 589 |
if (when() & FL_WHEN_RELEASE) maybe_do_callback(); |
| 590 |
return 1; |
| 591 |
|
| 592 |
case FL_KEYBOARD: |
| 593 |
if (active_r() && window() && this == Fl::belowmouse()) |
| 594 |
window()->cursor(FL_CURSOR_NONE); |
| 595 |
return handle_key(); |
| 596 |
|
| 597 |
case FL_PASTE: |
| 598 |
if (!Fl::event_text()) { |
| 599 |
fl_beep(); |
| 600 |
return 1; |
| 601 |
} |
| 602 |
buffer()->remove_selection(); |
| 603 |
if (insert_mode()) insert(Fl::event_text()); |
| 604 |
else overstrike(Fl::event_text()); |
| 605 |
show_insert_position(); |
| 606 |
set_changed(); |
| 607 |
if (when()&FL_WHEN_CHANGED) do_callback(); |
| 608 |
return 1; |
| 609 |
|
| 610 |
case FL_ENTER: |
| 611 |
// MRS: WIN32 only? Need to test! |
| 612 |
// case FL_MOVE: |
| 613 |
show_cursor(mCursorOn); |
| 614 |
return 1; |
| 615 |
|
| 616 |
case FL_PUSH: |
| 617 |
if (Fl::event_button() == 2) { |
| 618 |
// don't let the text_display see this event |
| 619 |
if (Fl_Group::handle(event)) return 1; |
| 620 |
dragType = DRAG_NONE; |
| 621 |
if(buffer()->selected()) { |
| 622 |
buffer()->unselect(); |
| 623 |
} |
| 624 |
int pos = xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS); |
| 625 |
insert_position(pos); |
| 626 |
Fl::paste(*this, 0); |
| 627 |
Fl::focus(this); |
| 628 |
set_changed(); |
| 629 |
if (when()&FL_WHEN_CHANGED) do_callback(); |
| 630 |
return 1; |
| 631 |
} |
| 632 |
break; |
| 633 |
|
| 634 |
case FL_SHORTCUT: |
| 635 |
if (!(shortcut() ? Fl::test_shortcut(shortcut()) : test_shortcut())) |
| 636 |
return 0; |
| 637 |
if (Fl::visible_focus() && handle(FL_FOCUS)) { |
| 638 |
Fl::focus(this); |
| 639 |
return 1; |
| 640 |
} |
| 641 |
break; |
| 642 |
|
| 643 |
// Handle drag'n'drop attempt by the user. This is a simplified |
| 644 |
// implementation which allows dnd operations onto the scroll bars. |
| 645 |
case FL_DND_ENTER: // save the current cursor position |
| 646 |
if (Fl::visible_focus() && handle(FL_FOCUS)) |
| 647 |
Fl::focus(this); |
| 648 |
show_cursor(mCursorOn); |
| 649 |
dndCursorPos = insert_position(); |
| 650 |
/* fall through */ |
| 651 |
case FL_DND_DRAG: // show a temporary insertion cursor |
| 652 |
insert_position(xy_to_position(Fl::event_x(), Fl::event_y(), CURSOR_POS)); |
| 653 |
return 1; |
| 654 |
case FL_DND_LEAVE: // restore original cursor |
| 655 |
insert_position(dndCursorPos); |
| 656 |
return 1; |
| 657 |
case FL_DND_RELEASE: // keep insertion cursor and wait for the FL_PASTE event |
| 658 |
buffer()->unselect(); // FL_PASTE must not destroy current selection! |
| 659 |
return 1; |
| 660 |
} |
| 661 |
|
| 662 |
return Fl_Text_Display_mod::handle(event); |
| 663 |
} |
| 664 |
|
| 665 |
// |
| 666 |
// End of "$Id: Fl_Text_Editor_mod.cxx 8034 2010-12-15 12:21:55Z AlbrechtS $". |
| 667 |
// |