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