FLTK 1.4.0
Loading...
Searching...
No Matches
Fl_Terminal.H
Go to the documentation of this file.
1//
2// Fl_Terminal.cxx - A terminal widget for Fast Light Tool Kit (FLTK).
3//
4// Copyright 2022 by Greg Ercolano.
5// Copyright 2023 by Bill Spitzak and others.
6//
7// This library is free software. Distribution and use rights are outlined in
8// the file "COPYING" which should have been included with this file. If this
9// file is missing or damaged, see the license at:
10//
11// https://www.fltk.org/COPYING.php
12//
13// Please see the following page on how to report bugs and issues:
14//
15// https://www.fltk.org/bugs.php
16//
17
22#ifndef Fl_Terminal_H
23#define Fl_Terminal_H
24
25#include <FL/Fl.H>
26#include <FL/Fl_Window.H>
27#include <FL/Fl_Group.H>
28#include <FL/Fl_Scrollbar.H>
29#include <FL/Fl_Rect.H>
30
31#include <stdarg.h> // va_list (MinGW)
32
311class FL_EXPORT Fl_Terminal : public Fl_Group {
315public:
324 NO_REDRAW=0,
326 PER_WRITE
327 };
328
337 enum Attrib {
338 NORMAL = 0x00,
339 BOLD = 0x01,
340 DIM = 0x02,
341 ITALIC = 0x04,
342 UNDERLINE = 0x08,
343 _RESERVED_1 = 0x10,
344 INVERSE = 0x20,
345 _RESERVED_2 = 0x40,
346 STRIKEOUT = 0x80
347 };
348
354 FG_XTERM = 0x01,
355 BG_XTERM = 0x02,
356 EOL = 0x04,
357 RESV_A = 0x08,
358 RESV_B = 0x10,
359 RESV_C = 0x20,
360 RESV_D = 0x40,
361 RESV_E = 0x80,
362 COLORMASK = (FG_XTERM | BG_XTERM)
363 };
364
369 enum OutFlags {
370 OFF = 0x00,
371 CR_TO_LF = 0x01,
372 LF_TO_CR = 0x02,
373 LF_TO_CRLF = 0x04
374 };
375
381protected:
382 // Margin Class ////////////////////////////////////////////
383 //
384 // Class to manage the terminal's margins
385 //
386 class FL_EXPORT Margin {
387 int left_, right_, top_, bottom_;
388 public:
389 Margin(void) { left_ = right_ = top_ = bottom_ = 3; }
390 int left(void) const { return left_; }
391 int right(void) const { return right_; }
392 int top(void) const { return top_; }
393 int bottom(void) const { return bottom_; }
394 void left(int val) { left_ = val; }
395 void right(int val) { right_ = val; }
396 void top(int val) { top_ = val; }
397 void bottom(int val) { bottom_ = val; }
398 };
399
400 // CharStyle Class ////////////////////////////////////////////
401 //
402 // Class to manage the terminal's character style
403 // This includes the font, color, and some cached internal
404 // info for optimized drawing speed.
405 //
406 class FL_EXPORT CharStyle {
407 uchar attrib_; // bold, underline..
408 uchar charflags_; // CharFlags (xterm color management)
409 Fl_Color fgcolor_; // foreground color for text
410 Fl_Color bgcolor_; // background color for text
411 Fl_Color defaultfgcolor_; // default fg color used by ESC[0m
412 Fl_Color defaultbgcolor_; // default bg color used by ESC[0m
413 Fl_Font fontface_; // font face
414 Fl_Fontsize fontsize_; // font size
415 int fontheight_; // font height (in pixels)
416 int fontdescent_; // font descent (pixels below font baseline)
417 int charwidth_; // width of a fixed width ASCII character
418 public:
419 CharStyle(bool fontsize_defer);
420 uchar attrib(void) const { return attrib_; }
421 uchar charflags(void) const { return charflags_; }
422 Fl_Color fgcolor(void) const;
423 Fl_Color bgcolor(void) const;
424 Fl_Color defaultfgcolor(void) const { return defaultfgcolor_; }
425 Fl_Color defaultbgcolor(void) const { return defaultbgcolor_; }
426 Fl_Font fontface(void) const { return fontface_; }
427 Fl_Fontsize fontsize(void) const { return fontsize_; }
428 int fontheight(void) const { return fontheight_; }
429 int fontdescent(void) const { return fontdescent_; }
430 int charwidth(void) const { return charwidth_; }
431 uchar colorbits_only(uchar inflags) const;
432 void attrib(uchar val) { attrib_ = val; }
433 void charflags(uchar val) { charflags_ = val; }
434 void set_charflag(uchar val) { charflags_ |= val; }
435 void clr_charflag(uchar val) { charflags_ &= ~val; }
436 void fgcolor_uchar(uchar val);
437 void bgcolor_uchar(uchar val);
438 void fgcolor(int r,int g,int b) { fgcolor_ = (r<<24) | (g<<16) | (b<<8); clr_charflag(FG_XTERM); }
439 void bgcolor(int r,int g,int b) { bgcolor_ = (r<<24) | (g<<16) | (b<<8); clr_charflag(BG_XTERM); }
440 void fgcolor(Fl_Color val) { fgcolor_ = val; clr_charflag(FG_XTERM); }
441 void bgcolor(Fl_Color val) { bgcolor_ = val; clr_charflag(BG_XTERM); }
442 void defaultfgcolor(Fl_Color val) { defaultfgcolor_ = val; }
443 void defaultbgcolor(Fl_Color val) { defaultbgcolor_ = val; }
444 void fontface(Fl_Font val) { fontface_ = val; update(); }
445 void fontsize(Fl_Fontsize val) { fontsize_ = val; update(); }
446 void update(void);
447 void update_fake(void);
448 // SGR MODES: Set Graphics Rendition
449 void sgr_reset(void) { // e.g. ESC[0m
450 attrib(Fl_Terminal::NORMAL);
451 fgcolor(defaultfgcolor_);
452 bgcolor(defaultbgcolor_);
453 }
454 int onoff(bool flag, Attrib a) { return (flag ? (attrib_ | a) : (attrib_ & ~a)); }
455 void sgr_bold(bool val) { attrib_ = onoff(val, Fl_Terminal::BOLD); } // e.g. ESC[1m
456 void sgr_dim(bool val) { attrib_ = onoff(val, Fl_Terminal::DIM); } // e.g. ESC[2m
457 void sgr_italic(bool val) { attrib_ = onoff(val, Fl_Terminal::ITALIC); } // e.g. ESC[3m
458 void sgr_underline(bool val) { attrib_ = onoff(val, Fl_Terminal::UNDERLINE); } // e.g. ESC[3m
459 void sgr_dbl_under(bool val) { attrib_ = onoff(val, Fl_Terminal::UNDERLINE); } // e.g. ESC[21m (TODO!)
460 void sgr_blink(bool val) { (void)val; /* NOT IMPLEMENTED */ } // e.g. ESC[5m
461 void sgr_inverse(bool val) { attrib_ = onoff(val, Fl_Terminal::INVERSE); } // e.g. ESC[7m
462 void sgr_strike(bool val) { attrib_ = onoff(val, Fl_Terminal::STRIKEOUT); } // e.g. ESC[9m
463 };
464
465protected:
466 // Cursor Class ///////////////////////////////////////////////////////////
467 //
468 // Class to manage the terminal's cursor position, color, etc.
469 //
470 class FL_EXPORT Cursor {
471 int col_; // cursor's current col (x) position on display
472 int row_; // cursor's current row (y) position on display
473 int h_; // cursor's height (affected by font size)
474 Fl_Color fgcolor_; // cursor's fg color (color of text, if any)
475 Fl_Color bgcolor_; // cursor's bg color
476 public:
477 Cursor(void) {
478 col_ = 0;
479 row_ = 0;
480 h_ = 10;
481 fgcolor_ = 0xfffff000; // wht
482 bgcolor_ = 0x00d00000; // grn
483 }
484 int col(void) const { return col_; }
485 int row(void) const { return row_; }
486 int h(void) const { return h_; }
487 Fl_Color fgcolor(void) const { return fgcolor_; }
488 Fl_Color bgcolor(void) const { return bgcolor_; }
489 void col(int val) { col_ = val >= 0 ? val : 0; }
490 void row(int val) { row_ = val >= 0 ? val : 0; }
491 void h(int val) { h_ = val; }
492 void fgcolor(Fl_Color val) { fgcolor_ = val; }
493 void bgcolor(Fl_Color val) { bgcolor_ = val; }
494 int left(void) { col_ = (col_>0) ? (col_-1) : 0; return col_; }
495 int right(void) { return ++col_; }
496 int up(void) { row_ = (row_>0) ? (row_-1) : 0; return row_; }
497 int down(void) { return ++row_; }
498 bool is_rowcol(int drow,int dcol) const;
499 void scroll(int nrows);
500 void home(void) { row_ = 0; col_ = 0; }
501 };
502
503 // Utf8Char Class ///////////////////////////////////////////////////////////
504 //
505 // Class to manage the terminal's individual UTF-8 characters.
506 // Includes fg/bg color, attributes (BOLD, UNDERLINE..)
507 //
508 class FL_EXPORT Utf8Char {
509 static const int max_utf8_ = 4; // RFC 3629 paraphrased: In UTF-8, chars are encoded with 1 to 4 octets
510 char text_[max_utf8_]; // memory for actual ASCII or UTF-8 byte contents
511 uchar len_; // length of bytes in text_[] buffer; 1 for ASCII, >1 for UTF-8
512 uchar attrib_; // attribute bits for this char (bold, underline..)
513 uchar charflags_; // CharFlags (xterm colors management)
514 Fl_Color fgcolor_; // fltk fg color (supports 8color or 24bit color set w/ESC[37;<r>;<g>;<b>m)
515 Fl_Color bgcolor_; // fltk bg color (supports 8color or 24bit color set w/ESC[47;<r>;<g>;<b>m)
516 // Private methods
517 void text_utf8_(const char *text, int len);
518 public:
519 // Public methods
520 Utf8Char(void); // ctor
521 Utf8Char(const Utf8Char& o); // copy ctor
522 ~Utf8Char(void); // dtor
523 Utf8Char& operator=(const Utf8Char& o); // assignment
524 inline int max_utf8() const { return max_utf8_; }
525 void text_utf8(const char *text, int len, const CharStyle& style);
526 void text_ascii(char c, const CharStyle& style);
527 void fl_font_set(const CharStyle& style) const;
528
529 // Return the UTF-8 text string for this character.
530 // Use length() to get number of bytes in string, which will be 1 for ASCII chars.
531 //
532 const char* text_utf8(void) const { return text_; }
533 // Return the attribute for this char
534 uchar attrib(void) const { return attrib_; }
535 uchar charflags(void) const { return charflags_; }
536 Fl_Color fgcolor(void) const;
537 Fl_Color bgcolor(void) const;
538 // Return the length of this character in bytes (UTF-8 can be multibyte..)
539 int length(void) const { return int(len_); }
540 double pwidth(void) const;
541 int pwidth_int(void) const;
542 // Clear the character to a 'space'
543 void clear(const CharStyle& style) { text_ascii(' ', style); }
544 bool is_char(char c) const { return *text_ == c; }
545 void show_char(void) const { ::printf("%.*s", len_, text_); }
546 void show_char_info(void) const { ::fprintf(stderr, "UTF-8('%.*s', len=%d)\n", len_, text_, len_); }
547
548 Fl_Color attr_color(Fl_Color col, const Fl_Widget *grp) const;
549 Fl_Color attr_fg_color(const Fl_Widget *grp) const;
550 Fl_Color attr_bg_color(const Fl_Widget *grp) const;
551 };
552
553 // RingBuffer Class ///////////////////////////////////////////////////
554 //
555 // Manages ring with indexed row/col and "history" vs. "display" concepts.
556 //
557 class FL_EXPORT RingBuffer {
558 Utf8Char *ring_chars_; // the ring UTF-8 char buffer
559 int ring_rows_; // #rows in ring total
560 int ring_cols_; // #columns in ring/hist/disp
561 int nchars_; // #chars in ring (ring_rows*ring_cols)
562 int hist_rows_; // #rows in history
563 int hist_use_; // #rows in use by history
564 int disp_rows_; // #rows in display
565 int offset_; // index offset (used for 'scrolling')
566
567private:
568 void new_copy(int drows, int dcols, int hrows, const CharStyle& style);
569 //DEBUG void write_row(FILE *fp, Utf8Char *u8c, int cols) const {
570 //DEBUG cols = (cols != 0) ? cols : ring_cols();
571 //DEBUG for ( int col=0; col<cols; col++, u8c++ ) {
572 //DEBUG ::fprintf(fp, "%.*s", u8c->length(), u8c->text_utf8());
573 //DEBUG }
574 //DEBUG }
575 public:
576 void clear(void);
577 void clear_hist(void);
578 RingBuffer(void);
579 RingBuffer(int drows, int dcols, int hrows);
580 ~RingBuffer(void);
581
582 // Methods to access ring
583 //
584 // The 'offset' concept allows the 'history' and 'display'
585 // to be scrolled indefinitely. The 'offset' is applied
586 // to all the row accesses, and are clamped to within their bounds.
587 //
588 // For 'raw' access to the ring (without the offset concept),
589 // use the ring_chars() method, and walk from 0 - ring_rows().
590 //
591 // _____________
592 // | | <- hist_srow() <- ring_srow()
593 // | H i s t |
594 // | |
595 // |_____________| <- hist_erow()
596 // | | <- disp_srow()
597 // | D i s p |
598 // | |
599 // |_____________| <- disp_erow() <- ring_erow()
600 //
601 // \___________/
602 // ring_cols()
603 // hist_cols()
604 // disp_cols()
605 //
606 inline int ring_rows(void) const { return ring_rows_; }
607 inline int ring_cols(void) const { return ring_cols_; }
608 inline int ring_srow(void) const { return(0); }
609 inline int ring_erow(void) const { return(ring_rows_ - 1); }
610 inline int hist_rows(void) const { return hist_rows_; }
611 inline int hist_cols(void) const { return ring_cols_; }
612 inline int hist_srow(void) const { return((offset_ + 0) % ring_rows_); }
613 inline int hist_erow(void) const { return((offset_ + hist_rows_ - 1) % ring_rows_); }
614 inline int disp_rows(void) const { return disp_rows_; }
615 inline int disp_cols(void) const { return ring_cols_; }
616 inline int disp_srow(void) const { return((offset_ + hist_rows_) % ring_rows_); }
617 inline int disp_erow(void) const { return((offset_ + hist_rows_ + disp_rows_ - 1) % ring_rows_); }
618 inline int offset(void) const { return offset_; }
619 void offset_adjust(int rows);
620 void hist_rows(int val) { hist_rows_ = val; }
621 void disp_rows(int val) { disp_rows_ = val; }
622
623 // History use
624 inline int hist_use(void) const { return hist_use_; }
625 inline void hist_use(int val) { hist_use_ = val; }
626 inline int hist_use_srow(void) const { return((offset_ + hist_rows_ - hist_use_) % ring_rows_); }
627 inline Utf8Char *ring_chars(void) { return ring_chars_; } // access ring buffer directly
628 inline Utf8Char *ring_chars(void) const { return ring_chars_; } // access ring buffer directly
629
630 bool is_hist_ring_row(int grow) const;
631 bool is_disp_ring_row(int grow) const;
632 //DEBUG void show_ring_info(void) const;
633 void move_disp_row(int src_row, int dst_row);
634 void clear_disp_rows(int sdrow, int edrow, const CharStyle& style);
635 void scroll(int rows, const CharStyle& style);
636
637 const Utf8Char* u8c_ring_row(int row) const;
638 const Utf8Char* u8c_hist_row(int hrow) const;
639 const Utf8Char* u8c_hist_use_row(int hurow) const;
640 const Utf8Char* u8c_disp_row(int drow) const;
641 // Non-const versions of the above methods
642 // Using "Effective C++" ugly-as-hell syntax technique.
643 //
644 Utf8Char* u8c_ring_row(int row);
645 Utf8Char* u8c_hist_row(int hrow);
646 Utf8Char* u8c_hist_use_row(int hurow);
647 Utf8Char* u8c_disp_row(int drow);
648
649 void create(int drows, int dcols, int hrows);
650 void resize(int drows, int dcols, int hrows, const CharStyle& style);
651
652 void change_disp_rows(int drows, const CharStyle& style);
653 void change_disp_cols(int dcols, const CharStyle& style);
654 };
655
656 // Selection Class ///////////////////////////////////////////////////
657 //
658 // Class to manage mouse selection
659 //
660 class FL_EXPORT Selection {
661 int srow_, scol_, erow_, ecol_; // selection start/end. NOTE: start *might* be > end
662 int push_row_, push_col_; // global row/col for last FL_PUSH
663 Fl_Color selectionbgcolor_;
664 Fl_Color selectionfgcolor_;
665 int state_ ; // 0=none, 1=started, 2=extended, 3=done
666 bool is_selection_; // false: no selection
667 public:
668 Selection(void);
669 int srow(void) const { return srow_; }
670 int scol(void) const { return scol_; }
671 int erow(void) const { return erow_; }
672 int ecol(void) const { return ecol_; }
673 void push_clear() { push_row_ = push_col_ = -1; }
674 void push_rowcol(int row,int col) { push_row_ = row; push_col_ = col; }
675 void start_push() { start(push_row_, push_col_); }
676 bool dragged_off(int row,int col) { return (push_row_ != row) || (push_col_ != col); }
677 void selectionfgcolor(Fl_Color val) { selectionfgcolor_ = val; }
678 void selectionbgcolor(Fl_Color val) { selectionbgcolor_ = val; }
679 Fl_Color selectionfgcolor(void) const { return selectionfgcolor_; }
680 Fl_Color selectionbgcolor(void) const { return selectionbgcolor_; }
681 bool is_selection(void) const { return is_selection_; }
682 bool get_selection(int &srow,int &scol,int &erow,int &ecol) const; // guarantees return (start < end)
683 bool start(int row, int col);
684 bool extend(int row, int col);
685 void end(void);
686 void select(int srow, int scol, int erow, int ecol);
687 bool clear(void);
688 int state(void) const { return state_; }
689 void scroll(int nrows);
690 };
691
692 // EscapeSeq Class ///////////////////////////////////////////////////
693 //
694 // Class to handle parsing ESC sequences
695 //
696 // Holds all state information for parsing esc sequences,
697 // so sequences can span multiple block read(2) operations, etc.
698 // Handling of parsed sequences is NOT handled in this class,
699 // just the parsing of the sequences and managing generic integers.
700 //
701 class FL_EXPORT EscapeSeq {
702 public:
703 // EscapeSeq Constants
704 // Maximums
705 static const int maxbuff = 80; // character buffer
706 static const int maxvals = 20; // integer value buffer
707 // Return codes
708 static const int success = 0; // operation succeeded
709 static const int fail = -1; // operation failed
710 static const int completed = 1; // multi-step operation completed successfully
711 private:
712 char esc_mode_; // escape parsing mode state
713 char csi_; // This is an ESC[.. sequence (Ctrl Seq Introducer)
714 char buff_[maxbuff]; // escape sequence being parsed
715 char *buffp_; // parsing ptr into buff[]
716 char *buffendp_; // end of buff[] (ptr to last valid buff char)
717 char *valbuffp_; // pointer to first char in buff of integer being parsed
718 int vals_[maxvals]; // value array for parsing #'s in ESC[#;#;#..
719 int vali_; // parsing index into vals_[], 0 if none
720 int save_row_, save_col_; // used by ESC[s/u for save/restore
721
722 int append_buff(char c);
723 int append_val(void);
724
725 public:
726 EscapeSeq(void);
727 void reset(void);
728 char esc_mode(void) const;
729 void esc_mode(char val);
730 int total_vals(void) const;
731 int val(int i) const;
732 int defvalmax(int dval, int max) const;
733 bool parse_in_progress(void) const;
734 bool is_csi(void) const;
735 int parse(char c);
736 void save_cursor(int row, int col);
737 void restore_cursor(int &row, int &col);
738 };
739
740 // Partial UTF-8 Buffer Class ////////////////////////////////////////////
741 //
742 // Class to manage buffering partial UTF-8 characters between write calls.
743 //
744 class FL_EXPORT PartialUtf8Buf {
745 char buf_[10]; // buffer partial UTF-8 encoded char
746 int buflen_; // length of buffered UTF-8 encoded char
747 int clen_; // final byte length of a UTF-8 char
748 public:
749 void clear(void) { buflen_ = clen_ = 0; } // clear the buffer
750 PartialUtf8Buf(void) { clear(); } // Ctor
751 // Is byte 'c' in the middle of a UTF-8 encoded byte sequence?
752 bool is_continuation(char c) {
753 // Byte 1 Byte 2 Byte 3 ..etc..
754 // ASCII: 0xxxxxxx
755 // UTF8(2): 110xxxxx 10xxxxxx
756 // UTF8(3): 1110xxxx 10xxxxxx 10xxxxxx
757 // UTF8(4): 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
758 // UTF8(5): 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
759 // UTF8(6): 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
760 // \______/ \______________________________________________/
761 // Start byte Continuation bytes
762 // (c & 0xc0) == 0x80
763 return ((c & 0xc0) == 0x80);
764 }
765 // Access buffer
766 const char* buf(void) const { return buf_; }
767 // Access buffer length
768 int buflen(void) const { return buflen_; }
769 // Append bytes of a partial UTF-8 string to the buffer.
770 //
771 // Returns:
772 // - true if done OK. Use is_complete() to see if a complete char received.
773 // - false if buffer overrun occurred, class is clear()ed.
774 //
775 // An appropriate response to 'false' would be to print the
776 // "unknown character" and skip all subsequent UTF-8 continuation chars.
777 //
778 bool append(const char* p, int len) {
779 if (len <= 0) return true; // ignore silly requests: say we did but dont
780 if (buflen_ + len >= (int)sizeof(buf_)) // overrun check
781 { clear(); return false; } // clear self, return false
782 if (!buflen_) clen_ = fl_utf8len(*p); // first byte? save char len for later
783 while (len>0) { buf_[buflen_++] = *p++; len--; } // append byte to buffer
784 return true;
785 }
786 bool is_complete(void) const { return (buflen_ && (buflen_ == clen_)); }
787 };
788
794public:
799 Fl_Scrollbar *scrollbar; // vertical scrollbar (value: rows above disp_chars[])
800private:
801 bool fontsize_defer_; // flag defers font calcs until first draw() (issue 837)
802 int scrollbar_size_; // local preference for scrollbar size
803 CharStyle *current_style_; // current font, attrib, color..
804 OutFlags oflags_; // output translation flags (CR_TO_LF, LF_TO_CR, LF_TO_CRLF)
805
806 // A ring buffer is used for the terminal's history (hist) and display (disp) buffer.
807 // See README-Fl_Terminal.txt, section "RING BUFFER DESCRIPTION" for diagrams/info.
808 //
809 // Ring buffer
810 RingBuffer ring_; // terminal history/display ring buffer
811 Cursor cursor_; // terminal cursor (position, color, etc)
812 Margin margin_; // terminal margins (top,left,bottom,right)
813 Selection select_; // mouse selection
814 EscapeSeq escseq; // Escape sequence parsing (ESC[ xterm/vt100)
815 bool show_unknown_; // if true, show unknown chars as '¿' (default off)
816 bool ansi_; // if true, parse ansi codes (default on)
817 char *tabstops_; // array of tab stops (0|1) \__ TODO: This should probably
818 int tabstops_size_; // size of tabstops[] array / be a class "TabStops".
819 Fl_Rect scrn_; // terminal screen xywh inside box(), margins, and scrollbar
820 int autoscroll_dir_; // 0=autoscroll timer off, 3=scrolling up, 4=scrolling down
821 int autoscroll_amt_; // #pixels above or below edge, used for autoscroll speed
822 RedrawStyle redraw_style_; // NO_REDRAW, RATE_LIMITED, PER_WRITE
823 float redraw_rate_; // maximum redraw rate in seconds, default=0.10
824 bool redraw_modified_; // display modified; used by update_cb() to rate limit redraws
825 bool redraw_timer_; // if true, redraw timer is running
826 PartialUtf8Buf pub_; // handles Partial Utf8 Buffer (pub)
827
828protected:
829 // Ring buffer management
830 const Utf8Char* u8c_ring_row(int grow) const;
831 const Utf8Char* u8c_hist_row(int hrow) const;
832 const Utf8Char* u8c_hist_use_row(int hrow) const;
833 const Utf8Char* u8c_disp_row(int drow) const;
834 // Non-const versions of the above.
835 // "Effective C++" says: implement non-const method to cast away const
836 //
837 Utf8Char* u8c_ring_row(int grow);
838 Utf8Char* u8c_hist_row(int hrow);
839 Utf8Char* u8c_hist_use_row(int hurow);
840 Utf8Char* u8c_disp_row(int drow);
841 Utf8Char* u8c_cursor(void);
842private:
843 void create_ring(int drows, int dcols, int hrows);
844 void init_(int X,int Y,int W,int H,const char*L,int rows,int cols,int hist,bool fontsize_defer);
845protected:
846 int scrollbar_width(void) const;
847private:
848 // Tabstops
849 void init_tabstops(int newsize);
850 void default_tabstops(void);
851 void clear_all_tabstops(void);
852 void set_tabstop(void);
853 void clear_tabstop(void);
854 // Updates
855 void update_screen_xywh(void);
856 void update_screen(bool font_changed);
857 void update_scrollbar(void);
858 // Resize
859 void resize_display_rows(int drows);
860 void resize_display_columns(int dcols);
861 void refit_disp_to_screen(void);
862 // Callbacks
863 static void scrollbar_cb(Fl_Widget*, void*); // scrollbar manipulation
864 static void autoscroll_timer_cb(void*); // mouse drag autoscroll
865 void autoscroll_timer_cb2(void);
866 static void redraw_timer_cb(void*); // redraw rate limiting timer
867 void redraw_timer_cb2(void);
868
869 // Screen management
870protected:
871 const CharStyle& current_style(void) const;
872 void current_style(const CharStyle& sty);
873private:
874 int x_to_glob_col(int X, int grow, int &gcol) const;
875 int xy_to_glob_rowcol(int X, int Y, int &grow, int &gcol) const;
876protected:
877 int w_to_col(int W) const;
878 int h_to_row(int H) const;
879 // API: Display clear operations
880 void clear_sod(void);
881 void clear_eod(void);
882 void clear_eol(void);
883 void clear_sol(void);
884 void clear_line(int row);
885 void clear_line(void);
886 const Utf8Char* walk_selection(const Utf8Char *u8c, int &row, int &col) const;
887 bool get_selection(int &srow,int &scol,int &erow,int &ecol) const;
888 bool is_selection(void) const;
889 bool is_inside_selection(int row,int col) const;
890private:
891 bool is_hist_ring_row(int grow) const;
892 bool is_disp_ring_row(int grow) const;
893protected:
894 int selection_text_len(void) const;
895 const char* selection_text(void) const;
896 void clear_mouse_selection(void);
897 bool selection_extend(int X,int Y);
898 void scroll(int rows);
899 void insert_rows(int count);
900 void delete_rows(int count);
901 void insert_char_eol(char c, int drow, int dcol, int rep);
902 void insert_char(char c, int rep);
903 void delete_chars(int drow, int dcol, int rep);
904 void delete_chars(int rep);
905public:
906 // API: Terminal operations
907 void clear(void);
908 void clear(Fl_Color val);
909 void clear_screen(bool scroll_to_hist=true); // ESC [ 2 J
910 void clear_screen_home(bool scroll_to_hist=true); // ESC [ H ESC [ 2 J
911 void clear_history(void); // ESC [ 3 J
912 void reset_terminal(void); // ESC c
913 void cursor_home(void); // ESC [ 0 H
914 // API: Cursor
915 void cursorfgcolor(Fl_Color val);
916 void cursorbgcolor(Fl_Color val);
917 Fl_Color cursorfgcolor(void) const;
918 Fl_Color cursorbgcolor(void) const;
919protected:
920 void cursor_row(int row);
921 void cursor_col(int col);
922public:
923 int cursor_row(void) const;
924 int cursor_col(void) const;
925protected:
926 void cursor_up(int count=1, bool do_scroll=false);
927 void cursor_down(int count=1, bool do_scroll=false);
928 void cursor_left(int count=1);
929 void cursor_right(int count=1, bool do_scroll=false);
930 void cursor_eol(void);
931 void cursor_sol(void);
932 void cursor_cr(void);
933 void cursor_crlf(int count=1);
934 void cursor_tab_right(int count=1);
935 void cursor_tab_left(int count=1);
936 void save_cursor(void);
937 void restore_cursor(void);
938 // Output translation
939public:
940 void output_translate(Fl_Terminal::OutFlags val);
941 Fl_Terminal::OutFlags output_translate(void) const;
942private:
943 void handle_lf(void);
944 void handle_cr(void);
945 // Printing
946 void handle_ctrl(char c);
947 bool is_printable(char c);
948 bool is_ctrl(char c);
949 void handle_SGR(void);
950 void handle_DECRARA(void);
951 void handle_escseq(char c);
952 // --
953 void display_modified(void);
954 void display_modified_clear(void);
955 void clear_char_at_disp(int drow, int dcol);
956 const Utf8Char* utf8_char_at_disp(int drow, int dcol) const;
957 const Utf8Char* utf8_char_at_glob(int grow, int gcol) const;
958 void repeat_char(char c, int rep);
959 void utf8_cache_clear(void);
960 void utf8_cache_flush(void);
961 // API: Character display output
962public:
963 void putchar(const char *text, int len, int drow, int dcol);
964 void putchar(char c, int drow, int dcol);
965 void print_char(const char *text, int len=-1);
966 void print_char(char c);
967 // API: String display output
968 void append_utf8(const char *buf, int len=-1);
969 void append_ascii(const char *s);
970 void append(const char *s, int len=-1);
971protected:
972 int handle_unknown_char(void);
973 // Drawing
974 void draw_row_bg(int grow, int X, int Y) const;
975 void draw_row(int grow, int Y) const;
976 void draw_buff(int Y) const;
977private:
978 void handle_selection_autoscroll(void);
979 int handle_selection(int e);
980public:
981 // FLTK: draw(), resize(), handle()
982 void draw(void) FL_OVERRIDE;
983 void resize(int X,int Y,int W,int H) FL_OVERRIDE;
984 int handle(int e) FL_OVERRIDE;
985
986protected:
987 // Internal short names
988 // Don't make these public, but allow internals and
989 // derived classes to maintain brevity.
990 //
992 inline int ring_rows(void) const { return ring_.ring_rows(); }
994 inline int ring_cols(void) const { return ring_.ring_cols(); }
996 inline int ring_srow(void) const { return ring_.ring_srow(); }
998 inline int ring_erow(void) const { return ring_.ring_erow(); }
1000 inline int hist_rows(void) const { return ring_.hist_rows(); }
1002 inline int hist_cols(void) const { return ring_.hist_cols(); }
1004 inline int hist_srow(void) const { return ring_.hist_srow(); }
1006 inline int hist_erow(void) const { return ring_.hist_erow(); }
1008 inline int hist_use(void) const { return ring_.hist_use(); }
1010 inline int hist_use_srow(void) const { return ring_.hist_use_srow(); }
1012 inline int disp_rows(void) const { return ring_.disp_rows(); }
1014 inline int disp_cols(void) const { return ring_.disp_cols(); }
1016 inline int disp_srow(void) const { return ring_.disp_srow(); }
1018 inline int disp_erow(void) const { return ring_.disp_erow(); }
1020 inline int offset(void) const { return ring_.offset(); }
1021
1022 // TODO: CLEAN UP WHAT'S PUBLIC, AND WHAT SHOULD BE 'PROTECTED' AND 'PRIVATE'
1023 // Some of the public stuff should, quite simply, "not be".
1024
1025 // API: Terminal features
1026public:
1027 // API: Scrollbar
1028 int scrollbar_size(void) const;
1029 void scrollbar_size(int val);
1030 int scrollbar_actual_size(void) const;
1031 // API: History
1032 int history_rows(void) const;
1033 void history_rows(int val);
1034 int history_use(void) const;
1035 // API: Display
1036 int display_rows(void) const;
1037 void display_rows(int val);
1038 int display_columns(void) const;
1039 void display_columns(int val);
1040 // API: Box
1048 void box(Fl_Boxtype val) { Fl_Group::box(val); update_screen(false); }
1050 Fl_Boxtype box(void) const { return Fl_Group::box(); }
1051 // API: Margins
1053 int margin_left(void) const { return margin_.left(); }
1055 int margin_right(void) const { return margin_.right(); }
1057 int margin_top(void) const { return margin_.top(); }
1059 int margin_bottom(void) const { return margin_.bottom(); }
1060 void margin_left(int val);
1061 void margin_right(int val);
1062 void margin_top(int val);
1063 void margin_bottom(int val);
1064 // API: Text font/size/color
1065 void textfont(Fl_Font val);
1066 void textsize(Fl_Fontsize val);
1067 void textcolor(Fl_Color val);
1068 void color(Fl_Color val);
1069 void textfgcolor(Fl_Color val);
1070 void textbgcolor(Fl_Color val);
1071 void textfgcolor_default(Fl_Color val);
1072 void textbgcolor_default(Fl_Color val);
1074 Fl_Font textfont(void) const { return current_style_->fontface(); }
1076 Fl_Fontsize textsize(void) const { return current_style_->fontsize(); }
1078 Fl_Color color(void) const { return Fl_Group::color(); }
1080 Fl_Color textcolor(void) const { return textfgcolor_default(); }
1082 Fl_Color textfgcolor(void) const { return current_style_->fgcolor(); }
1084 Fl_Color textbgcolor(void) const { return current_style_->bgcolor(); }
1086 Fl_Color textfgcolor_default(void) const { return current_style_->defaultfgcolor(); }
1088 Fl_Color textbgcolor_default(void) const { return current_style_->defaultbgcolor(); }
1089 void textfgcolor_xterm(uchar val);
1090 void textbgcolor_xterm(uchar val);
1092 void selectionfgcolor(Fl_Color val) { select_.selectionfgcolor(val); }
1094 void selectionbgcolor(Fl_Color val) { select_.selectionbgcolor(val); }
1096 Fl_Color selectionfgcolor(void) const { return select_.selectionfgcolor(); }
1098 Fl_Color selectionbgcolor(void) const { return select_.selectionbgcolor(); }
1099 // API: Text attrib
1100 void textattrib(uchar val);
1101 uchar textattrib() const;
1102 // API: Redraw style/rate
1103 RedrawStyle redraw_style(void) const;
1104 void redraw_style(RedrawStyle val);
1105private:
1106 bool is_redraw_style(RedrawStyle val) { return redraw_style_ == val; }
1107public:
1108 float redraw_rate(void) const;
1109 void redraw_rate(float val);
1110 // API: Show unknown/unprintable chars
1111 bool show_unknown(void) const;
1112 void show_unknown(bool val);
1113 // API: ANSI sequences
1114 bool ansi(void) const;
1115 void ansi(bool val);
1116 // Fl_Simple_Terminal API compatibility
1117 int history_lines(void) const;
1118 void history_lines(int val);
1119 // API: printf()
1120 void printf(const char *fmt, ...);
1121 void vprintf(const char *fmt, va_list ap);
1122 // Ctor
1123 Fl_Terminal(int X,int Y,int W,int H,const char*L=0);
1124 Fl_Terminal(int X,int Y,int W,int H,const char*L,int rows,int cols,int hist);
1125 // Dtor
1126 ~Fl_Terminal(void);
1127 // Debugging features
1128//DEBUG void show_ring_info() const { ring_.show_ring_info(); }
1129//DEBUG void write_row(FILE *fp, Utf8Char *u8c, int cols) const;
1130//DEBUG void show_buffers(RingBuffer *a, RingBuffer *b=0) const;
1131};
1132#endif
int Fl_Font
A font number is an index into the internal font table.
Definition Enumerations.H:1044
unsigned int Fl_Color
An FLTK color value; see also Colors
Definition Enumerations.H:1101
int Fl_Fontsize
Size of a font in pixels.
Definition Enumerations.H:1073
Fl_Boxtype
FLTK standard box types.
Definition Enumerations.H:626
Fl static class.
Fl_Group and Fl_End classes.
Fl_Window widget .
The Fl_Group class is the FLTK container widget.
Definition Fl_Group.H:56
void end()
Exactly the same as current(this->parent()).
Definition Fl_Group.cxx:73
int handle(int) FL_OVERRIDE
Handles the specified event.
Definition Fl_Group.cxx:145
void resize(int, int, int, int) FL_OVERRIDE
Resizes the Fl_Group widget and all of its children.
Definition Fl_Group.cxx:823
void draw() FL_OVERRIDE
Draws the widget.
Definition Fl_Group.cxx:926
void clear()
Deletes all child widgets from memory recursively.
Definition Fl_Group.cxx:379
Rectangle with standard FLTK coordinates (X, Y, W, H).
Definition Fl_Rect.H:30
The Fl_Scrollbar widget displays a slider with arrow buttons at the ends of the scrollbar.
Definition Fl_Scrollbar.H:41
Definition Fl_Terminal.H:406
Definition Fl_Terminal.H:470
Definition Fl_Terminal.H:701
Definition Fl_Terminal.H:386
Definition Fl_Terminal.H:744
Definition Fl_Terminal.H:557
Definition Fl_Terminal.H:660
Definition Fl_Terminal.H:508
Terminal widget supporting Unicode/utf-8, ANSI/xterm escape codes with full RGB color control.
Definition Fl_Terminal.H:311
void selectionbgcolor(Fl_Color val)
Set mouse selection background color.
Definition Fl_Terminal.H:1094
int disp_srow(void) const
Return the starting row# in the display area.
Definition Fl_Terminal.H:1016
Fl_Color color(void) const
Return base widget Fl_Group's box() color()
Definition Fl_Terminal.H:1078
int hist_rows(void) const
Return the number of rows in the scrollback history.
Definition Fl_Terminal.H:1000
Fl_Color textbgcolor_default(void) const
Return text's default background color.
Definition Fl_Terminal.H:1088
int margin_left(void) const
Return the left margin; see Margins.
Definition Fl_Terminal.H:1053
Fl_Color selectionfgcolor(void) const
Get mouse selection foreground color.
Definition Fl_Terminal.H:1096
Attrib
Bits for the per-character attributes, which control text features such as italic,...
Definition Fl_Terminal.H:337
@ NORMAL
all attributes off
Definition Fl_Terminal.H:338
@ ITALIC
italic font text
Definition Fl_Terminal.H:341
@ DIM
dim text; color slightly darker than normal
Definition Fl_Terminal.H:340
@ STRIKEOUT
strikeout text
Definition Fl_Terminal.H:346
@ UNDERLINE
underlined text
Definition Fl_Terminal.H:342
@ BOLD
bold text: uses bold font, color brighter than normal
Definition Fl_Terminal.H:339
@ INVERSE
inverse text; fg/bg color are swapped
Definition Fl_Terminal.H:344
Fl_Color selectionbgcolor(void) const
Get mouse selection background color.
Definition Fl_Terminal.H:1098
int offset(void) const
Returns the current offset into the ring buffer.
Definition Fl_Terminal.H:1020
int margin_bottom(void) const
Return the bottom margin; see Margins.
Definition Fl_Terminal.H:1059
Fl_Scrollbar * scrollbar
Vertical scrollbar.
Definition Fl_Terminal.H:799
int margin_top(void) const
Return the top margin; see Margins.
Definition Fl_Terminal.H:1057
int hist_srow(void) const
Return the starting row# of the scrollback history.
Definition Fl_Terminal.H:1004
Fl_Font textfont(void) const
Return text font used to draw all text in the terminal.
Definition Fl_Terminal.H:1074
int disp_erow(void) const
Return the ending row# in the display area.
Definition Fl_Terminal.H:1018
Fl_Color textfgcolor(void) const
Return text's current foreground color.
Definition Fl_Terminal.H:1082
Fl_Fontsize textsize(void) const
Return text font size used to draw all text in the terminal.
Definition Fl_Terminal.H:1076
int ring_cols(void) const
Return the number of columns in the ring buffer.
Definition Fl_Terminal.H:994
CharFlags
Per-character 8 bit flags (uchar) used to manage special states for characters.
Definition Fl_Terminal.H:353
int hist_cols(void) const
Return the number of columns in the scrollback history.
Definition Fl_Terminal.H:1002
Fl_Color textbgcolor(void) const
Return text's current background color.
Definition Fl_Terminal.H:1084
Fl_Color textfgcolor_default(void) const
Return text's default foreground color.
Definition Fl_Terminal.H:1086
RedrawStyle
Determines when Fl_Terminal calls redraw() if new text is added.
Definition Fl_Terminal.H:323
@ RATE_LIMITED
timer controlled redraws. (DEFAULT)
Definition Fl_Terminal.H:325
int margin_right(void) const
Return the right margin; see Margins.
Definition Fl_Terminal.H:1055
int ring_rows(void) const
Return the number of rows in the ring buffer.
Definition Fl_Terminal.H:992
int hist_use(void) const
Return number of rows in use by the scrollback history.
Definition Fl_Terminal.H:1008
int disp_rows(void) const
Return the number of rows in the display area.
Definition Fl_Terminal.H:1012
int hist_use_srow(void) const
Return the starting row of the "in use" scrollback history.
Definition Fl_Terminal.H:1010
int disp_cols(void) const
Return the number of columns in the display area.
Definition Fl_Terminal.H:1014
int ring_srow(void) const
Return the starting row# in the ring buffer. (Always 0)
Definition Fl_Terminal.H:996
OutFlags
Output translation flags for special control character translations.
Definition Fl_Terminal.H:369
int ring_erow(void) const
Return the ending row# in the ring buffer (Always ring_rows()-1)
Definition Fl_Terminal.H:998
Fl_Boxtype box(void) const
Returns the current box type.
Definition Fl_Terminal.H:1050
void box(Fl_Boxtype val)
Sets the box type, updates terminal margins et al.
Definition Fl_Terminal.H:1048
void selectionfgcolor(Fl_Color val)
Set mouse selection foreground color.
Definition Fl_Terminal.H:1092
Fl_Color textcolor(void) const
Return textcolor(). This is a convenience method that returns textfgcolor_default()
Definition Fl_Terminal.H:1080
int hist_erow(void) const
Return the ending row# of the scrollback history.
Definition Fl_Terminal.H:1006
Fl_Widget is the base class for all widgets in FLTK.
Definition Fl_Widget.H:104
Fl_Color color() const
Gets the background color of the widget.
Definition Fl_Widget.H:447
int h() const
Gets the widget height.
Definition Fl_Widget.H:368
Fl_Boxtype box() const
Gets the box type of the widget.
Definition Fl_Widget.H:432
#define FL_OVERRIDE
This macro makes it safe to use the C++11 keyword override with older compilers.
Definition fl_attr.h:46
unsigned char uchar
unsigned char
Definition fl_types.h:30
int fl_utf8len(char c)
Returns the byte length of the UTF-8 sequence with first byte c, or -1 if c is not valid.
Definition fl_utf8.cxx:69