summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--FL/Fl_Text_Buffer.H4
-rw-r--r--src/Fl_Text_Buffer.cxx44
2 files changed, 48 insertions, 0 deletions
diff --git a/FL/Fl_Text_Buffer.H b/FL/Fl_Text_Buffer.H
index 64f6f1e45..8fba33f16 100644
--- a/FL/Fl_Text_Buffer.H
+++ b/FL/Fl_Text_Buffer.H
@@ -22,6 +22,7 @@
#ifndef FL_TEXT_BUFFER_H
#define FL_TEXT_BUFFER_H
+#include <stdarg.h> /* va_start/end */
#undef ASSERT_UTF8
@@ -287,6 +288,9 @@ public:
*/
void append(const char* t) { insert(length(), t); }
+ void vprintf(const char *fmt, va_list ap);
+ void printf(const char* fmt, ...);
+
/**
Deletes a range of characters in the buffer.
\param start byte offset to first character to be removed
diff --git a/src/Fl_Text_Buffer.cxx b/src/Fl_Text_Buffer.cxx
index d63da183f..6b46dd497 100644
--- a/src/Fl_Text_Buffer.cxx
+++ b/src/Fl_Text_Buffer.cxx
@@ -292,6 +292,50 @@ void Fl_Text_Buffer::insert(int pos, const char *text)
}
+/**
+ Can be used by subclasses that need their own printf() style functionality.
+ e.g. Fl_Simple_Terminal::printf() would wrap around this method.
+ \note The expanded string is currently limited to 1024 characters.
+ \param[in] fmt is a printf format string for the message text.
+ \param[in] ap is a va_list created by va_start() and closed with va_end(),
+ which the caller is responsible for handling.
+*/
+void Fl_Text_Buffer::vprintf(const char *fmt, va_list ap) {
+ char buffer[1024]; // XXX: 1024 should be user configurable
+ ::vsnprintf(buffer, 1024, fmt, ap);
+ buffer[1024-1] = 0; // XXX: MICROSOFT
+ append(buffer);
+}
+
+
+/**
+ Appends printf formatted messages to the end of the buffer.
+ Example:
+ \code
+ #include <FL/Fl_Text_Display.H>
+ int main(..) {
+ :
+ // Create a text display widget and assign it a text buffer
+ Fl_Text_Display *tdsp = new Fl_Text_Display(..);
+ Fl_Text_Buffer *tbuf = new Fl_Text_Buffer();
+ tdsp->buffer(tbuf);
+ :
+ // Append three lines of formatted text to the buffer
+ tbuf->printf("The current date is: %s.\nThe time is: %s\n", date_str, time_str);
+ tbuf->printf("The current PID is %ld.\n", (long)getpid());
+ :
+ \endcode
+ \note The expanded string is currently limited to 1024 characters.
+ \param[in] fmt is a printf format string for the message text.
+*/
+void Fl_Text_Buffer::printf(const char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ Fl_Text_Buffer::vprintf(fmt,ap);
+ va_end(ap);
+}
+
+
/*
Replace a range of text with new text.
Start and end must be at a character boundary.