/**************************************************************************** ** COPYRIGHT (C): 1997 Cay S. Horstmann. All Rights Reserved. ** PROJECT: Practical OO Development with C++ and Java ** FILE: grstream.cpp ** PURPOSE: graphics stream (chapter 13) ** VERSION 1.0 ** PROGRAMMERS: Cay Horstmann (CSH) ** RELEASE DATE: 3-15-97 (CSH) ** UPDATE HISTORY: ****************************************************************************/ /* Project: GUI Application Add files grstream.cpp \PracticalOOBook\cpplib\graphics.cpp Additional include directory: \PracticalOOBook\cpplib */ #include "setup.h" #include "graphics.h" class GraphicStreambuf : public streambuf { public: GraphicStreambuf(Graphics& g); protected: virtual int overflow(int c); virtual int underflow() { return 0; } private: Graphics& _g; FontMetrics _fm; int _xnext; int _ynext; }; class GraphicStream : public ostream { public: GraphicStream(Graphics&); private: GraphicStreambuf _buffer; }; /*-------------------------------------------------------------*/ GraphicStreambuf::GraphicStreambuf(Graphics& g) : _g(g), _xnext(0), _fm(g.getFontMetrics()) { _ynext = _fm.getAscent() + _fm.getDescent(); } /*.............................................................*/ int GraphicStreambuf::overflow(int c) { string s; switch(c) { case EOF: break; case '\n': _xnext = 0; _ynext += _fm.getAscent() + _fm.getDescent(); break; default: s = (char)c; _g.drawString(s, _xnext, _ynext); _xnext += _fm.stringWidth(s); break; } return 1; } /*-------------------------------------------------------------*/ GraphicStream::GraphicStream(Graphics& g) : _buffer(g), ostream(&_buffer) {} /*-------------------------------------------------------------*/ /* Test code: */ void paint(Graphics& g) { GraphicStream gs(g); gs << "Hello, World" << endl << 3.14; }