mirror of
https://github.com/fltk/fltk.git
synced 2026-05-21 14:31:40 +08:00
Added printf() and vprintf() to Fl_Text_Buffer
git-svn-id: file:///fltk/svn/fltk/branches/branch-1.4@12483 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user