You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

653 lines
23KB

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