Debian Codegraph @

src/widgets/FTextView.cxx

raw
1 // ----------------------------------------------------------------------------
2 // FTextView.cxx
3 //
4 // Copyright (C) 2007-2009
5 // Stelios Bounanos, M0GLD
6 //
7 // Copyright (C) 2008-2009
8 // Dave Freese, W1HKJ
9 //
10 // This file is part of FLAMP.
11 //
12 // This is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This software is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program. If not, see <http://www.gnu.org/licenses/>.
24 //
25 // ----------------------------------------------------------------------------
26
27 #include <config.h>
28
29 #include <cstring>
30 #include <cstdlib>
31 #include <cstdio>
32 #include <cmath>
33 #include <sys/stat.h>
34 #include <map>
35 #include <fstream>
36 #include <sstream>
37 #include <algorithm>
38 #include <iomanip>
39
40 #include <string>
41
42 #include <FL/Fl_Tooltip.H>
43
44 #include "flmisc.h"
45 #include "fileselect.h"
46 #include "font_browser.h"
47 #include "ascii.h"
48 #include "icons.h"
49 #include "gettext.h"
50
51 #include "FTextView.h"
52
53 #include "debug.h"
54
55
56
57 /// FTextBase constructor.
58 /// Word wrapping is enabled by default at column 80, but see \c reset_wrap_col.
59 /// @param x
60 /// @param y
61 /// @param w
62 /// @param h
63 /// @param l
64
65 FTextBase::FTextBase(int x, int y, int w, int h, const char *l)
66 : Fl_Text_Editor_mod(x, y, w, h, l),
67 wrap(WRAP_AT_BOUNDS), wrap_col(80), max_lines(0), scroll_hint(false)
68 {
69 oldw = oldh = olds = -1;
70 oldf = (Fl_Font)-1;
71 textfont(FL_COURIER);
72 textsize(FL_NORMAL_SIZE);
73 textcolor(FL_FOREGROUND_COLOR);
74
75 tbuf = new Fl_Text_Buffer_mod;
76 sbuf = new Fl_Text_Buffer_mod;
77
78 buffer(tbuf);
79 highlight_data(sbuf, styles, NATTR, FTEXT_DEF, 0, 0);
80 cursor_style(Fl_Text_Editor_mod::NORMAL_CURSOR);
81
82 restore_wrap = wrap;
83
84 // Do we want narrower scrollbars? The default width is 16.
85 scrollbar_width((int)floor(scrollbar_width() * 7.0/8.0));
86
87 reset_styles(SET_FONT | SET_SIZE | SET_COLOR);
88 read_cb = NULL;
89 }
90
91 void FTextBase::clear()
92 {
93 tbuf->text("");
94 sbuf->text("");
95 set_word_wrap(restore_wrap);
96 }
97
98 int FTextBase::handle(int event)
99 {
100 if (event == FL_MOUSEWHEEL && !Fl::event_inside(this))
101 return 1;
102
103 // Fl_Text_Editor::handle() calls window()->cursor(FL_CURSOR_DONE) when
104 // it receives an FL_KEYBOARD event, which crashes some buggy X drivers
105 // (e.g. Intel on the Asus Eee PC). Call handle_key directly to work
106 // around this problem.
107 if (event == FL_KEYBOARD)
108 return Fl_Text_Editor_mod::handle_key();
109 else
110 return Fl_Text_Editor_mod::handle(event);
111 }
112
113 /// @see FTextRX::add
114 ///
115 /// @param s
116 /// @param attr
117 ///
118 void FTextBase::add(const char *s, int attr)
119 {
120 // handle the text attribute first
121 int n = strlen(s);
122 char a[n + 1];
123 memset(a, FTEXT_DEF + attr, n);
124 a[n] = '\0';
125 sbuf->replace(insert_position(), insert_position() + n, a);
126
127 insert(s);
128 }
129
130 /// @see FTextBase::add
131 ///
132 /// @param s
133 /// @param attr
134 ///
135 void FTextBase::add(unsigned char c, int attr)
136 {
137 char s[] = { static_cast<char>(FTEXT_DEF + attr), '\0' };
138 sbuf->replace(insert_position(), insert_position() + 1, s);
139
140 s[0] = c;
141 insert(s);
142 }
143
144 void FTextBase::set_word_wrap(bool b)
145 {
146 if ((wrap = b)) wrap_mode(WRAP_AT_BOUNDS, 0);
147 else wrap_mode(WRAP_NONE, 0);
148 show_insert_position();
149 }
150
151 void FTextBase::setFont(Fl_Font f, int attr)
152 {
153 set_style(attr, f, textsize(), textcolor(), SET_FONT);
154 }
155
156 void FTextBase::setFontSize(int s, int attr)
157 {
158 set_style(attr, textfont(), s, textcolor(), SET_SIZE);
159 }
160
161 void FTextBase::setFontColor(Fl_Color c, int attr)
162 {
163 set_style(attr, textfont(), textsize(), c, SET_COLOR);
164 }
165
166 /// Resizes the text widget.
167 /// The real work is done by \c Fl_Text_Editor_mod::resize or, if \c HSCROLLBAR_KLUDGE
168 /// is defined, a version of that code modified so that no horizontal
169 /// scrollbars are displayed when word wrapping.
170 ///
171 /// @param X
172 /// @param Y
173 /// @param W
174 /// @param H
175 ///
176 void FTextBase::resize(int X, int Y, int W, int H)
177 {
178 bool need_wrap_reset = false;
179 bool need_margin_reset = false;
180
181 if (unlikely(text_area.w != oldw)) {
182 oldw = text_area.w;
183 need_wrap_reset = true;
184 }
185 if (unlikely(text_area.h != oldh)) {
186 oldh = text_area.h;
187 need_margin_reset = true;
188 }
189 if (unlikely(textfont() != oldf || textsize() != olds)) {
190 oldf = textfont();
191 olds = textsize();
192 need_wrap_reset = need_margin_reset = true;
193 }
194
195 if (need_wrap_reset)
196 reset_wrap_col();
197
198 TOP_MARGIN = DEFAULT_TOP_MARGIN;
199 int r = H - Fl::box_dh(box()) - TOP_MARGIN - BOTTOM_MARGIN;
200 if (mHScrollBar->visible())
201 r -= scrollbar_width();
202 int msize = mMaxsize ? mMaxsize : textsize();
203 if (!msize) msize = 1;
204 //printf("H %d, textsize %d, lines %d, extra %d\n", r, msize, r / msize, r % msize);
205 if (r %= msize)
206 TOP_MARGIN += r;
207
208 if (scroll_hint) {
209 mTopLineNumHint = mNBufferLines;
210 mHorizOffsetHint = 0;
211 // display_insert_position_hint = 1;
212 scroll_hint = false;
213 }
214
215 bool hscroll_visible = mHScrollBar->visible();
216 Fl_Text_Editor_mod::resize(X, Y, W, H);
217 if (hscroll_visible != mHScrollBar->visible())
218 oldh = 0; // reset margins next time
219 }
220
221 /// Checks the new widget height.
222 /// This is registered with Fl_Tile_check and then called with horizontal
223 /// and vertical size increments every time the Fl_Tile boundary is moved.
224 ///
225 /// @param arg The callback argument; should be a pointer to a FTextBase object
226 /// @param xd The horizontal increment (ignored)
227 /// @param yd The vertical increment
228 ///
229 /// @return True if the widget is visible, and the new text area height would be
230 /// a multiple of the font height.
231 ///
232 bool FTextBase::wheight_mult_tsize(void *arg, int, int yd)
233 {
234 FTextBase *v = reinterpret_cast<FTextBase *>(arg);
235 if (!v->visible())
236 return true;
237 return v->mMaxsize > 0 && (v->text_area.h + yd) % v->mMaxsize == 0;
238 }
239
240 /// Changes text style attributes
241 ///
242 /// @param attr The attribute name to change, or \c NATTR to change all styles.
243 /// @param f The new font
244 /// @param s The new font size
245 /// @param c The new font color
246 /// @param set One or more (OR'd together) SET operations; @see set_style_op_e
247 ///
248 void FTextBase::set_style(int attr, Fl_Font f, int s, Fl_Color c, int set)
249 {
250 int start, end;
251
252 if (attr == NATTR) { // update all styles
253 start = 0;
254 end = NATTR;
255 if (set & SET_FONT)
256 Fl_Text_Display_mod::textfont(f);
257 if (set & SET_SIZE)
258 textsize(s);
259 if (set & SET_COLOR)
260 textcolor(c);
261 }
262 else {
263 start = attr;
264 end = start + 1;
265 }
266 for (int i = start; i < end; i++) {
267 styles[i].attr = 0;
268 if (set & SET_FONT)
269 styles[i].font = f;
270 if (set & SET_SIZE)
271 styles[i].size = s;
272 if (set & SET_COLOR)
273 styles[i].color = c;
274 if (i == SKIP) // clickable styles always same as SKIP for now
275 for (int j = CLICK_START; j < NATTR; j++)
276 memcpy(&styles[j], &styles[i], sizeof(styles[j]));
277 }
278 if (set & SET_COLOR)
279 mCursor_color = styles[0].color;
280
281 resize(x(), y(), w(), h()); // to redraw and recalculate the wrap column
282 }
283
284 /// Reads a file and inserts its contents.
285 ///
286 /// @return 0 on success, -1 on error
287 int FTextBase::readFile(const char* fn)
288 {
289 set_word_wrap(restore_wrap);
290
291 if ( !(fn || (fn = FSEL::select(_("Insert text"), "Text\t*.txt"))) )
292 return -1;
293
294 int ret = 0, pos = insert_position();
295
296 #ifdef __WOE32__
297 FILE* tfile = fopen(fn, "rt");
298 #else
299 FILE* tfile = fopen(fn, "r");
300 #endif
301 if (!tfile)
302 return -1;
303 char buf[BUFSIZ+1];
304 std::string newbuf;
305 size_t p;
306 memset(buf, 0, BUFSIZ+1);
307 if (pos == tbuf->length()) { // optimise for append
308 while (fgets(buf, sizeof(buf), tfile)) {
309 newbuf = buf;
310 tbuf->append(newbuf.c_str());
311 memset(buf, 0, BUFSIZ+1);
312 }
313 if (ferror(tfile))
314 ret = -1;
315 pos = tbuf->length();
316 }
317 else {
318 while (fgets(buf, sizeof(buf), tfile)) {
319 newbuf = buf;
320 p = 0;
321 while ((p = newbuf.find('^',p)) != std::string::npos) {
322 newbuf.insert(p, "^");
323 p += 2;
324 }
325 tbuf->insert(pos, newbuf.c_str());
326 pos += strlen(buf);
327 memset(buf, 0, BUFSIZ+1);
328 }
329 if (ferror(tfile))
330 ret = -1;
331 }
332 fclose(tfile);
333
334 insert_position(pos);
335 show_insert_position();
336
337 if (read_cb) (read_cb)(fn);
338 return ret;
339 }
340
341 /// Writes all buffer text out to a file.
342 ///
343 ///
344 void FTextBase::saveFile(void)
345 {
346 const char *fn = FSEL::saveas(_("Save text as"), "Text\t*.txt");
347 if (fn) {
348 #ifdef __WOE32__
349 std::ofstream tfile(fn);
350 if (!tfile)
351 return;
352
353 char *p1, *p2, *text = tbuf->text();
354 for (p1 = p2 = text; *p1; p1 = p2) {
355 while (*p2 != '\0' && *p2 != '\n')
356 p2++;
357 if (*p2 == '\n') {
358 *p2 = '\0';
359 tfile << p1 << "\r\n";
360 p2++;
361
362 }
363 else
364 tfile << p1;
365 }
366 free(text);
367 #else
368 tbuf->outputfile(fn, 0, tbuf->length());
369 #endif
370 }
371 }
372
373 /// Returns a character std::string containing the selected word, if any,
374 /// or the word at (\a x, \a y) relative to the widget's \c x() and \c y().
375 /// If \a ontext is true, this function will return text only if the
376 /// mouse cursor position is inside the text range.
377 ///
378 /// @param x
379 /// @param y
380 ///
381 /// @return The selection, or the word text at (x,y). <b>Must be freed by the caller</b>.
382 ///
383 char* FTextBase::get_word(int x, int y, const char* nwchars, bool ontext)
384 {
385 int p = xy_to_position(x + this->x(), y + this->y(), Fl_Text_Display_mod::CURSOR_POS);
386 int start, end;
387
388 if (tbuf->selected()) {
389 if (ontext && tbuf->selection_position(&start, &end) && (p < start || p >= end))
390 return 0;
391 else
392 return tbuf->selection_text();
393 }
394
395 std::string nonword = nwchars;
396 nonword.append(" \t\n");
397 if (!tbuf->findchars_backward(p, nonword.c_str(), &start))
398 start = 0;
399 else
400 start++;
401 if (!tbuf->findchars_forward(p, nonword.c_str(), &end))
402 end = tbuf->length();
403 //#endif
404
405 if (ontext && (p < start || p >= end))
406 return 0;
407 else
408 return tbuf->text_range(start, end);
409 }
410
411 /// Initialised the menu pointed to by \c context_menu. The menu items' user_data
412 /// field is used to store the initialisation flag.
413 void FTextBase::init_context_menu(void)
414 {
415 for (int i = 0; i < context_menu->size() - 1; i++) {
416 if (context_menu[i].user_data() == 0 &&
417 context_menu[i].labeltype() == _FL_MULTI_LABEL) {
418 icons::set_icon_label(&context_menu[i]);
419 context_menu[i].user_data(this);
420 }
421 }
422 }
423
424 /// Displays the menu pointed to by \c context_menu and calls the menu function;
425 /// @see call_cb.
426 ///
427 void FTextBase::show_context_menu(void)
428 {
429 const Fl_Menu_Item *m;
430 int xpos = Fl::event_x();
431 int ypos = Fl::event_y();
432
433 popx = xpos - x();
434 popy = ypos - y();
435 window()->cursor(FL_CURSOR_DEFAULT);
436 m = context_menu->popup(xpos, ypos, 0, 0, 0);
437 if (m)
438 menu_cb(m - context_menu);
439 }
440
441 /// Recalculates the wrap margin when the font is changed or the widget resized.
442 /// Line wrapping works with proportional fonts but may be very slow.
443 ///
444 int FTextBase::reset_wrap_col(void)
445 {
446 if (!wrap || text_area.w == 0)
447 return wrap_col;
448
449 int old_wrap_col = wrap_col;
450 if (Font_Browser::fixed_width(textfont())) {
451 fl_font(textfont(), textsize());
452 wrap_col = (int)floorf(text_area.w / fl_width('X'));
453 }
454 else // use slower (but accurate) wrapping for variable width fonts
455 wrap_col = 0;
456 // wrap_mode triggers a resize; don't call it if wrap_col hasn't changed
457 if (old_wrap_col != wrap_col)
458 wrap_mode(wrap, wrap_col);
459
460 return old_wrap_col;
461 }
462
463 void FTextBase::reset_styles(int set)
464 {
465 set_style(NATTR, FL_COURIER, FL_NORMAL_SIZE, FL_FOREGROUND_COLOR, set);
466 set_style(XMIT, FL_COURIER, FL_NORMAL_SIZE, FL_RED, set);
467 set_style(CTRL, FL_COURIER, FL_NORMAL_SIZE, FL_DARK_GREEN, set);
468 set_style(SKIP, FL_COURIER, FL_NORMAL_SIZE, FL_BLUE, set);
469 set_style(ALTR, FL_COURIER, FL_NORMAL_SIZE, FL_DARK_MAGENTA, set);
470 }
471
472 // ----------------------------------------------------------------------------
473
474 Fl_Menu_Item FTextView::menu[] = {
475 { icons::make_icon_label(_("Copy"), edit_copy_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
476 { icons::make_icon_label(_("Clear"), edit_clear_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
477 { icons::make_icon_label(_("Select All"), edit_select_all_icon), 0, 0, 0, FL_MENU_DIVIDER, _FL_MULTI_LABEL },
478 { icons::make_icon_label(_("Save as..."), save_as_icon), 0, 0, 0, FL_MENU_DIVIDER, _FL_MULTI_LABEL },
479 { _("Word wrap"), 0, 0, 0, FL_MENU_TOGGLE, FL_NORMAL_LABEL },
480 { 0 }
481 };
482
483 /// FTextView constructor.
484 /// We remove \c Fl_Text_Display_mod::buffer_modified_cb from the list of callbacks
485 /// because we want to scroll depending on the visibility of the last line; @see
486 /// changed_cb.
487 /// @param x
488 /// @param y
489 /// @param w
490 /// @param h
491 /// @param l
492 FTextView::FTextView(int x, int y, int w, int h, const char *l)
493 : FTextBase(x, y, w, h, l), quick_entry(false)
494 {
495 tbuf->remove_modify_callback(buffer_modified_cb, this);
496 tbuf->add_modify_callback(changed_cb, this);
497 tbuf->canUndo(0);
498
499 // disable some keybindings that are not allowed in FTextView buffers
500 change_keybindings();
501
502 context_menu = menu;
503 init_context_menu();
504 }
505
506 /// Handles fltk events for this widget.
507
508 /// We only care about mouse presses (to display the popup menu and prevent
509 /// pasting) and keyboard events (to make sure no text can be inserted).
510 /// Everything else is passed to the base class handle().
511 ///
512 /// @param event
513 ///
514 /// @return
515 ///
516 int FTextView::handle(int event)
517 {
518 switch (event) {
519 case FL_PUSH:
520 if (!Fl::event_inside(this))
521 break;
522 if (Fl::event_button() == FL_RIGHT_MOUSE) {
523 handle_context_menu();
524 return 1;
525 }
526 break;
527 case FL_DRAG:
528 if (Fl::event_button() != FL_LEFT_MOUSE)
529 return 1;
530 break;
531 // catch some text-modifying events that are not handled by kf_* functions
532 case FL_KEYBOARD:
533 int k;
534 if (Fl::compose(k))
535 return 1;
536 k = Fl::event_key();
537 if (k == FL_BackSpace)
538 return 1;
539 else if (k == FL_Tab)
540 return Fl_Widget::handle(event);
541 }
542
543 return FTextBase::handle(event);
544 }
545
546 void FTextView::handle_context_menu(void)
547 {
548 icons::set_active(&menu[VIEW_MENU_COPY], tbuf->selected());
549 icons::set_active(&menu[VIEW_MENU_CLEAR], tbuf->length());
550 icons::set_active(&menu[VIEW_MENU_SELECT_ALL], tbuf->length());
551 icons::set_active(&menu[VIEW_MENU_SAVE], tbuf->length());
552 if (wrap)
553 menu[VIEW_MENU_WRAP].set();
554 else
555 menu[VIEW_MENU_WRAP].clear();
556
557 show_context_menu();
558 }
559
560 /// The context menu handler
561 ///
562 /// @param val
563 ///
564 void FTextView::menu_cb(size_t item)
565 {
566 switch (item) {
567 case VIEW_MENU_COPY:
568 kf_copy(Fl::event_key(), this);
569 break;
570 case VIEW_MENU_CLEAR:
571 clear();
572 break;
573 case VIEW_MENU_SELECT_ALL:
574 tbuf->select(0, tbuf->length());
575 break;
576 case VIEW_MENU_SAVE:
577 saveFile();
578 break;
579 case VIEW_MENU_WRAP:
580 set_word_wrap(!wrap);
581 restore_wrap = wrap;
582 break;
583 }
584 }
585
586 /// Scrolls down if the buffer has been modified and the last line is
587 /// visible. See Fl_Text_Buffer::add_modify_callback() for parameter details.
588 ///
589 /// @param pos
590 /// @param nins
591 /// @param ndel
592 /// @param nsty
593 /// @param dtext
594 /// @param arg
595 ///
596 inline
597 void FTextView::changed_cb(int pos, int nins, int ndel, int nsty, const char *dtext, void *arg)
598 {
599 FTextView *v = reinterpret_cast<FTextView *>(arg);
600
601 if (v->mTopLineNum + v->mNVisibleLines - 1 == v->mNBufferLines)
602 v->scroll_hint = true;
603
604 v->buffer_modified_cb(pos, nins, ndel, nsty, dtext, v);
605 }
606
607 /// Removes Fl_Text_Edit keybindings that would modify text and put it out of
608 /// sync with the style buffer. At some point we may decide that we want
609 /// FTextView to be editable (e.g., to insert comments about a QSO), in which
610 /// case we'll keep the keybindings and add some code to changed_cb to update
611 /// the style buffer.
612 ///
613 void FTextView::change_keybindings(void)
614 {
615 Fl_Text_Editor_mod::Key_Func fdelete[] = { Fl_Text_Editor_mod::kf_default,
616 Fl_Text_Editor_mod::kf_enter,
617 Fl_Text_Editor_mod::kf_delete,
618 Fl_Text_Editor_mod::kf_cut,
619 Fl_Text_Editor_mod::kf_paste };
620 int n = sizeof(fdelete) / sizeof(fdelete[0]);
621
622 // walk the keybindings linked list and delete items containing elements
623 // of fdelete
624 loop:
625 for (Fl_Text_Editor_mod::Key_Binding *k = key_bindings; k; k = k->next) {
626 for (int i = 0; i < n; i++) {
627 if (k->function == fdelete[i]) {
628 remove_key_binding(k->key, k->state);
629 goto loop;
630 }
631 }
632 }
633 }
634
635 // ----------------------------------------------------------------------------
636
637
638 Fl_Menu_Item FTextEdit::menu[] = {
639 { icons::make_icon_label(_("Cut"), edit_cut_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
640 { icons::make_icon_label(_("Copy"), edit_copy_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
641 { icons::make_icon_label(_("Paste"), edit_paste_icon), 0, 0, 0, 0, _FL_MULTI_LABEL },
642 { icons::make_icon_label(_("Clear"), edit_clear_icon), 0, 0, 0, FL_MENU_DIVIDER, _FL_MULTI_LABEL },
643 { icons::make_icon_label(_("Insert file..."), file_open_icon), 0, 0, 0, FL_MENU_DIVIDER, _FL_MULTI_LABEL },
644 { _("Word wrap"), 0, 0, 0, FL_MENU_TOGGLE, FL_NORMAL_LABEL } ,
645 { 0 }
646 };
647
648 FTextEdit::FTextEdit(int x, int y, int w, int h, const char *l)
649 : FTextBase(x, y, w, h, l)
650 {
651 tbuf->remove_modify_callback(buffer_modified_cb, this);
652 tbuf->add_modify_callback(changed_cb, this);
653
654 ascii_cnt = 0;
655 ascii_chr = 0;
656
657 context_menu = menu;
658 init_context_menu();
659
660 dnd_paste = false;
661 }
662
663 /// Handles fltk events for this widget.
664 /// We pass keyboard events to handle_key() and handle mouse3 presses to show
665 /// the popup menu. We also disallow mouse2 events in the transmitted text area.
666 /// Everything else is passed to the base class handle().
667 ///
668 /// @param event
669 ///
670 /// @return
671 ///
672 int FTextEdit::handle(int event)
673 {
674 if ( !(Fl::event_inside(this) || (event == FL_KEYBOARD && Fl::focus() == this)) )
675 return FTextBase::handle(event);
676
677 switch (event) {
678 case FL_KEYBOARD:
679 return handle_key(Fl::event_key()) ? 1 : FTextBase::handle(event);
680 case FL_DND_RELEASE:
681 dnd_paste = true;
682 // fall through
683 case FL_DND_ENTER: case FL_DND_LEAVE:
684 return 1;
685 case FL_DND_DRAG:
686 return handle_dnd_drag(xy_to_position(Fl::event_x(), Fl::event_y(), CHARACTER_POS));
687 case FL_PASTE:
688 {
689 int r = dnd_paste ? handle_dnd_drop() : FTextBase::handle(event);
690 dnd_paste = false;
691 return r;
692 }
693 case FL_PUSH:
694 {
695 int eb = Fl::event_button();
696 if (eb == FL_RIGHT_MOUSE) {
697 handle_context_menu();
698 return 1;
699 }
700 }
701 default:
702 break;
703 }
704
705 return FTextBase::handle(event);
706 }
707
708 /// Handles keyboard events to override Fl_Text_Editor_mod's handling of some
709 /// keystrokes.
710 ///
711 /// @param key
712 ///
713 /// @return
714 ///
715 int FTextEdit::handle_key(int key)
716 {
717 // read ctl-ddd, where d is a digit, as ascii characters (in base 10)
718 // and insert verbatim; e.g. ctl-001 inserts a <soh>
719 if (key == FL_Control_L || key == FL_Control_R) return 0;
720 bool t1 = isdigit(key);
721 bool t2 = false;
722 if (key >= FL_KP) t2 = isdigit(key - FL_KP + '0');
723 bool t3 = (Fl::event_state() & FL_CTRL) == FL_CTRL;
724 if (t3 && (t1 || t2))
725 return handle_key_ascii(key);
726 ascii_cnt = 0; // restart the numeric keypad entries.
727 ascii_chr = 0;
728 return Fl_Text_Editor_mod::handle(FL_KEYBOARD);
729 // return 0;
730 }
731
732 /// Composes ascii characters and adds them to the FTextEdit buffer.
733 /// Control characters are inserted with the CTRL style. Values larger than 127
734 /// (0x7f) are ignored. We cannot really add NULs for the time being.
735 ///
736 /// @param key A digit character
737 ///
738 /// @return 1
739 ///
740 int FTextEdit::handle_key_ascii(int key)
741 {
742 if (key >= FL_KP)
743 key -= FL_KP;
744 key -= '0';
745 ascii_cnt++;
746 for (int i = 0; i < 3 - ascii_cnt; i++)
747 key *= 10;
748 ascii_chr += key;
749 if (ascii_cnt == 3) {
750 if (ascii_chr < 0x100) //0x7F)
751 add(ascii_chr, (iscntrl(ascii_chr) ? CTRL : RECV));
752 ascii_cnt = ascii_chr = 0;
753 }
754
755 return 1;
756 }
757
758 /// Handles FL_DND_DRAG events by scrolling and moving the cursor
759 ///
760 /// @return 1
761 int FTextEdit::handle_dnd_drag(int pos)
762 {
763 // Scroll if the pointer is being dragged inside the scrollbars,
764 // otherwise obtain keyboard focus and set the insert position.
765 if (mVScrollBar->visible() && Fl::event_inside(mVScrollBar))
766 mVScrollBar->handle(FL_DRAG);
767 else if (mHScrollBar->visible() && Fl::event_inside(mHScrollBar))
768 mHScrollBar->handle(FL_DRAG);
769 else {
770 if (Fl::focus() != this)
771 take_focus();
772 insert_position(pos);
773 }
774
775 return 1;
776 }
777
778 /// Handles FL_PASTE events by inserting text
779 ///
780 /// @return 1 or FTextBase::handle(FL_PASTE)
781 int FTextEdit::handle_dnd_drop(void)
782 {
783 // paste verbatim if the shift key was held down during dnd
784 if (Fl::event_shift())
785 return FTextBase::handle(FL_PASTE);
786
787 std::string text;
788 std::string::size_type p, len;
789
790 text = Fl::event_text();
791
792 const char sep[] = "\n";
793 #if defined(__APPLE__) || defined(__WOE32__)
794 text += sep;
795 #endif
796
797 len = text.length();
798 while ((p = text.find(sep)) != std::string::npos) {
799 text[p] = '\0';
800 #if !defined(__APPLE__) && !defined(__WOE32__)
801 if (text.find("file://") == 0) {
802 text.erase(0, 7);
803 p -= 7;
804 len -= 7;
805 }
806 #endif
807 // paste everything verbatim if we cannot read the first file
808 if (readFile(text.c_str()) == -1 && len == text.length())
809 return FTextBase::handle(FL_PASTE);
810 text.erase(0, p + sizeof(sep) - 1);
811 }
812
813 return 1;
814 }
815
816 /// Handles mouse-3 clicks by displaying the context menu
817 ///
818 /// @param val
819 ///
820 void FTextEdit::handle_context_menu(void)
821 {
822 bool selected = tbuf->selected();
823 icons::set_active(&menu[EDIT_MENU_CUT], selected);
824 icons::set_active(&menu[EDIT_MENU_COPY], selected);
825 icons::set_active(&menu[EDIT_MENU_CLEAR], tbuf->length());
826
827 if (wrap)
828 menu[EDIT_MENU_WRAP].set();
829 else
830 menu[EDIT_MENU_WRAP].clear();
831
832 show_context_menu();
833 }
834
835 /// The context menu handler
836 ///
837 /// @param val
838 ///
839 void FTextEdit::menu_cb(size_t item)
840 {
841 switch (item) {
842 case EDIT_MENU_CLEAR:
843 clear();
844 break;
845 case EDIT_MENU_CUT:
846 kf_cut(0, this);
847 break;
848 case EDIT_MENU_COPY:
849 kf_copy(0, this);
850 break;
851 case EDIT_MENU_PASTE:
852 kf_paste(0, this);
853 break;
854 case EDIT_MENU_READ:
855 readFile();
856 break;
857 case EDIT_MENU_WRAP:
858 set_word_wrap(!wrap);
859 restore_wrap = wrap;
860 break;
861 }
862 }
863
864 /// This function is called by Fl_Text_Buffer when the buffer is modified, and
865 /// also by nextChar when a character has been passed up the transmit path. In
866 /// the first case either nins or ndel will be nonzero, and we change a
867 /// corresponding amount of text in the style buffer.
868 ///
869 /// In the latter case, nins, ndel, pos and nsty are all zero and we update the
870 /// style buffer to mark the last character in the buffer with the XMIT
871 /// attribute.
872 ///
873 /// @param pos
874 /// @param nins
875 /// @param ndel
876 /// @param nsty
877 /// @param dtext
878 /// @param arg
879 ///
880 void FTextEdit::changed_cb(int pos, int nins, int ndel, int nsty, const char *dtext, void *arg)
881 {
882 FTextEdit *e = reinterpret_cast<FTextEdit *>(arg);
883
884 if (nins == 0 && ndel == 0) {
885 if (nsty == -1) { // called by nextChar to update transmitted text style
886 char s[] = { FTEXT_DEF + XMIT, '\0' };
887 e->sbuf->replace(pos - 1, pos, s);
888 e->redisplay_range(pos - 1, pos);
889 }
890 else if (nsty > 0) // restyled, e.g. selected, text
891 return e->buffer_modified_cb(pos, nins, ndel, nsty, dtext, e);
892
893 // No changes, e.g., a paste with an empty clipboard.
894 return;
895 }
896 else if (nins > 0 && e->sbuf->length() < e->tbuf->length()) {
897 // New text not inserted by our add() methods, i.e., via a file
898 // read, mouse-2 paste or, most likely, direct keyboard entry.
899 int n = e->tbuf->length() - e->sbuf->length();
900 if (n == 1) {
901 char s[] = { FTEXT_DEF, '\0' };
902 e->sbuf->append(s);
903 }
904 else {
905 char *s = new char [n + 1];
906 memset(s, FTEXT_DEF, n);
907 s[n] = '\0';
908 e->sbuf->append(s);
909 delete [] s;
910 }
911 }
912 else if (ndel > 0)
913 e->sbuf->remove(pos, pos + ndel);
914
915 e->sbuf->select(pos, pos + nins - ndel);
916
917 e->buffer_modified_cb(pos, nins, ndel, nsty, dtext, e);
918 // We may need to scroll if the text was inserted by the
919 // add() methods, e.g. by a macro
920 if (e->mTopLineNum + e->mNVisibleLines - 1 <= e->mNBufferLines)
921 e->show_insert_position();
922 }