FLTK 1.4.0
Loading...
Searching...
No Matches
Fl_Simple_Terminal.H
1//
2// A simple terminal widget for Fast Light Tool Kit (FLTK).
3//
4// Copyright 1998-2011 by Bill Spitzak and others.
5// Copyright 2017 by Greg Ercolano.
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
18/* \file
19 Fl_Simple_Terminal widget . */
20
21#ifndef Fl_Simple_Terminal_H
22#define Fl_Simple_Terminal_H
23
24#include "Fl_Export.H"
25#include <FL/Fl_Text_Display.H>
26
120class FL_EXPORT Fl_Simple_Terminal : public Fl_Text_Display {
121protected:
122 Fl_Text_Buffer *buf; // text buffer
123 Fl_Text_Buffer *sbuf; // style buffer
124
125private:
126 // Private class to handle parsing ESC sequences
127 // Holds all state information for parsing esc sequences,
128 // so sequences can span multiple block read(2) operations, etc.
129 //
130 class FL_EXPORT Fl_Escape_Seq {
131 public:
132 static const int maxbuf = 80;
133 static const int maxvals = 10;
134 // Return codes
135 static const int success = 0; // operation succeeded
136 static const int fail = -1; // operation failed
137 static const int completed = 1; // multi-step operation completed successfully
138 private:
139 char esc_mode_; // escape parsing mode state
140 char buf_[maxbuf]; // escape sequence being parsed
141 char *bufp_; // parsing ptr into buf[]
142 char *bufendp_; // end of buf[] (ptr to last valid buf char)
143 char *valbufp_; // pointer to first char in buf of integer being parsed
144 int vals_[maxvals]; // value array for parsing #'s in ESC[#;#;#..
145 int vali_; // parsing index into vals_[], 0 if none
146
147 int append_buf(char c);
148 int append_val();
149
150 public:
151 Fl_Escape_Seq();
152 void reset();
153 char esc_mode() const;
154 void esc_mode(char val);
155 int total_vals() const;
156 int val(int i) const;
157 bool parse_in_progress() const;
158 int parse(char c);
159 };
160
161private:
162 int history_lines_; // max lines allowed in screen history
163 bool stay_at_bottom_; // lets scroller chase last line in buffer
164 // scroll management
165 int lines_; // #lines in buffer (optimization: Fl_Text_Buffer slow to calc this)
166 bool scrollaway_; // true when user changed vscroll away from bottom
167 bool scrolling_; // true while scroll callback active
168 // Fl_Text_Display vscrollbar's callback+data
169 Fl_Callback *orig_vscroll_cb_;
170 void *orig_vscroll_data_;
171 // Style table
172 const Fl_Text_Display::Style_Table_Entry *stable_; // the active style table
173 int stable_size_; // active style table size (in bytes)
174 int normal_style_index_; // "normal" style used by "\033[0m" reset sequence
175 int current_style_index_; // current style used for drawing text
176 char current_style_; // current 'style char' (e.g. 'A' = first style entry)
177 // ANSI escape seq
178 Fl_Escape_Seq escseq; // escape sequence state handler
179 bool ansi_; // enables ANSI sequences
180 bool ansi_show_unknown_; // show '¿' for unknown ESC sequences (default: off)
181 // String parsing vars initialized/used by append(), used by handle_backspace() etc.
182 char *ntm_; // new text memory (ntm) - malloc()ed by append() for output text
183 char *ntp_; // new text ptr (ntp) - points into ntm buffer
184 char *nsm_; // new style memory (nsm) - malloc()ed by append() for output style
185 char *nsp_; // new style ptr (nsp) - points into nsm buffer
186
187public:
188 Fl_Simple_Terminal(int X,int Y,int W,int H,const char *l=0);
190
191 // Terminal options
192 void stay_at_bottom(bool);
193 bool stay_at_bottom() const;
194 void history_lines(int);
195 int history_lines() const;
196 void ansi(bool val);
197 bool ansi() const;
198 void ansi_show_unknown(bool val);
199 bool ansi_show_unknown() const;
200 void style_table(Fl_Text_Display::Style_Table_Entry *stable, int stable_size, int normal_style_index=0);
201 const Fl_Text_Display::Style_Table_Entry *style_table() const;
202 int style_table_size() const;
203 void normal_style_index(int);
204 int normal_style_index() const;
205 void current_style_index(int);
206 int current_style_index() const;
207 int current_style() const;
208
209 // Terminal text management
210 void append(const char *s, int len=-1);
211 void text(const char *s, int len=-1);
212 const char* text() const;
213 void printf(const char *fmt, ...);
214 void vprintf(const char *fmt, va_list ap);
215 void clear();
216 void remove_lines(int start, int count);
217
218private:
219 // Methods blocking public access to the subclass
220 // These are subclass methods that would give unexpected
221 // results if used. By making them private, we effectively
222 // "block" them.
223 //
224 // TODO: There are probably other Fl_Text_Display methods that
225 // need to be blocked.
226 //
227 void insert(const char*) { }
228
229protected:
230 // Fltk
231 void draw() FL_OVERRIDE;
232
233 // Internal methods
234 void enforce_stay_at_bottom();
235 void enforce_history_lines();
236 void vscroll_cb2(Fl_Widget*, void*);
237 static void vscroll_cb(Fl_Widget*, void*);
238 void backspace_buffer(unsigned int count);
239 void handle_backspace();
240 void append_ansi(const char *s, int len);
241 void unknown_escape();
242};
243
244#endif
void() Fl_Callback(Fl_Widget *, void *)
Default callback type definition for all fltk widgets (by far the most used)
Definition Fl_Widget.H:33
This is a continuous text scroll widget for logging and debugging output, much like a terminal.
Definition Fl_Simple_Terminal.H:120
This class manages Unicode text displayed in one or more Fl_Text_Display widgets.
Definition Fl_Text_Buffer.H:201
Rich text display widget.
Definition Fl_Text_Display.H:78
Fl_Widget is the base class for all widgets in FLTK.
Definition Fl_Widget.H:104
#define FL_OVERRIDE
This macro makes it safe to use the C++11 keyword override with older compilers.
Definition fl_attr.h:46
This structure associates the color, font, and font size of a string to draw with an attribute mask m...
Definition Fl_Text_Display.H:145