From d2df10a5fcf8dfce2a40fb14f52d447fdd078df2 Mon Sep 17 00:00:00 2001 From: Stelios Bounanos Date: Fri, 5 Mar 2010 12:12:55 +0000 Subject: [PATCH] Modify FText add() methods Use efficient versions of add() for all FText derived classes except FTextRX (which receives text one character at a time anyway). This greatly reduces the Event log window's draw overhead. --- src/include/FTextRXTX.h | 7 +++ src/include/FTextView.h | 13 +---- src/widgets/FTextRXTX.cxx | 47 ++++++++++++++++ src/widgets/FTextView.cxx | 109 +++++++++++--------------------------- 4 files changed, 87 insertions(+), 89 deletions(-) diff --git a/src/include/FTextRXTX.h b/src/include/FTextRXTX.h index bd281049..8fded5a2 100644 --- a/src/include/FTextRXTX.h +++ b/src/include/FTextRXTX.h @@ -36,6 +36,13 @@ public: virtual int handle(int event); + virtual void add(unsigned char c, int attr = RECV); + virtual void add(const char *s, int attr = RECV) + { + while (*s) + add(*s++, attr); + } + void set_quick_entry(bool b); bool get_quick_entry(void) { return quick_entry; } void mark(FTextBase::TEXT_ATTR attr = CLICK_START); diff --git a/src/include/FTextView.h b/src/include/FTextView.h index 3e729c1e..152801b3 100644 --- a/src/include/FTextView.h +++ b/src/include/FTextView.h @@ -48,8 +48,8 @@ public: FTextBase(int x, int y, int w, int h, const char *l = 0); virtual ~FTextBase() { delete tbuf; delete sbuf; } - virtual void add(const char *text, int attr = RECV) = 0; - virtual void add(unsigned char c, int attr = RECV) = 0; + virtual void add(const char *text, int attr = RECV); + virtual void add(unsigned char c, int attr = RECV); void addstr(const char *text, int attr = RECV) { add(text, attr); } void addchr(unsigned char c, int attr = RECV) { add(c, attr); } @@ -117,12 +117,6 @@ public: ~FTextView() { } virtual int handle(int event); - virtual void add(unsigned char c, int attr = RECV); - virtual void add(const char *s, int attr = RECV) - { - while (*s) - add(*s++, attr); - } protected: enum { @@ -156,9 +150,6 @@ public: virtual int handle(int event); - virtual void add(const char *s, int attr = RECV); - virtual void add(unsigned char c, int attr = RECV); - protected: enum { EDIT_MENU_CUT, EDIT_MENU_COPY, EDIT_MENU_PASTE, EDIT_MENU_CLEAR, diff --git a/src/widgets/FTextRXTX.cxx b/src/widgets/FTextRXTX.cxx index a722c9e6..04834f7d 100644 --- a/src/widgets/FTextRXTX.cxx +++ b/src/widgets/FTextRXTX.cxx @@ -234,6 +234,53 @@ out: return FTextView::handle(event); } +/// Adds a char to the buffer +/// +/// @param c The character +/// @param attr The attribute (@see enum text_attr_e); RECV if omitted. +/// +void FTextRX::add(unsigned char c, int attr) +{ + if (c == '\r') + return; + + // The user may have moved the cursor by selecting text or + // scrolling. Place it at the end of the buffer. + if (mCursorPos != tbuf->length()) + insert_position(tbuf->length()); + + switch (c) { + case '\b': + // we don't call kf_backspace because it kills selected text + tbuf->remove(tbuf->length() - 1, tbuf->length()); + sbuf->remove(sbuf->length() - 1, sbuf->length()); + break; + case '\n': + // maintain the scrollback limit, if we have one + if (max_lines > 0 && tbuf->count_lines(0, tbuf->length()) >= max_lines) { + int le = tbuf->line_end(0) + 1; // plus 1 for the newline + tbuf->remove(0, le); + sbuf->remove(0, le); + } + // fall-through + default: + char s[] = { '\0', '\0', FTEXT_DEF + attr, '\0' }; + const char *cp; + + if ((c < ' ' || c == 127) && attr != CTRL) // look it up + cp = ascii[(unsigned char)c]; + else { // insert verbatim + s[0] = c; + cp = &s[0]; + } + + for (int i = 0; cp[i]; ++i) + sbuf->append(s + 2); + insert(cp); + break; + } +} + void FTextRX::set_quick_entry(bool b) { if ((quick_entry = b)) diff --git a/src/widgets/FTextView.cxx b/src/widgets/FTextView.cxx index e63b8c18..f07913bf 100644 --- a/src/widgets/FTextView.cxx +++ b/src/widgets/FTextView.cxx @@ -97,6 +97,37 @@ int FTextBase::handle(int event) return Fl_Text_Editor_mod::handle(event); } +/// @see FTextRX::add +/// +/// @param s +/// @param attr +/// +void FTextBase::add(const char *s, int attr) +{ + // handle the text attribute first + int n = strlen(s); + char a[n + 1]; + memset(a, FTEXT_DEF + attr, n); + a[n] = '\0'; + sbuf->replace(insert_position(), insert_position() + n, a); + + insert(s); +} + +/// @see FTextBase::add +/// +/// @param s +/// @param attr +/// +void FTextBase::add(unsigned char c, int attr) +{ + char s[] = { FTEXT_DEF + attr, '\0' }; + sbuf->replace(insert_position(), insert_position() + 1, s); + + s[0] = c; + insert(s); +} + void FTextBase::set_word_wrap(bool b) { wrap_mode((wrap = b), wrap_col); @@ -452,53 +483,6 @@ FTextView::FTextView(int x, int y, int w, int h, const char *l) init_context_menu(); } -/// Adds a char to the buffer -/// -/// @param c The character -/// @param attr The attribute (@see enum text_attr_e); RECV if omitted. -/// -void FTextView::add(unsigned char c, int attr) -{ - if (c == '\r') - return; - - // The user may have moved the cursor by selecting text or - // scrolling. Place it at the end of the buffer. - if (mCursorPos != tbuf->length()) - insert_position(tbuf->length()); - - switch (c) { - case '\b': - // we don't call kf_backspace because it kills selected text - tbuf->remove(tbuf->length() - 1, tbuf->length()); - sbuf->remove(sbuf->length() - 1, sbuf->length()); - break; - case '\n': - // maintain the scrollback limit, if we have one - if (max_lines > 0 && tbuf->count_lines(0, tbuf->length()) >= max_lines) { - int le = tbuf->line_end(0) + 1; // plus 1 for the newline - tbuf->remove(0, le); - sbuf->remove(0, le); - } - // fall-through - default: - char s[] = { '\0', '\0', FTEXT_DEF + attr, '\0' }; - const char *cp; - - if ((c < ' ' || c == 127) && attr != CTRL) // look it up - cp = ascii[(unsigned char)c]; - else { // insert verbatim - s[0] = c; - cp = &s[0]; - } - - for (int i = 0; cp[i]; ++i) - sbuf->append(s + 2); - insert(cp); - break; - } -} - /// Handles fltk events for this widget. /// We only care about mouse presses (to display the popup menu and prevent @@ -700,37 +684,6 @@ int FTextEdit::handle(int event) return FTextBase::handle(event); } -/// @see FTextView::add -/// -/// @param s -/// @param attr -/// -void FTextEdit::add(const char *s, int attr) -{ - // handle the text attribute first - int n = strlen(s); - char a[n + 1]; - memset(a, FTEXT_DEF + attr, n); - a[n] = '\0'; - sbuf->replace(insert_position(), insert_position() + n, a); - - insert(s); -} - -/// @see FTextEdit::add -/// -/// @param s -/// @param attr -/// -void FTextEdit::add(unsigned char c, int attr) -{ - char s[] = { FTEXT_DEF + attr, '\0' }; - sbuf->replace(insert_position(), insert_position() + 1, s); - - s[0] = c; - insert(s); -} - /// Handles keyboard events to override Fl_Text_Editor_mod's handling of some /// keystrokes. ///