#include <ivplus/iohandler.h>#include <ivplus/iocallback.h>
For convenience, InterViews Plus provides a template for deriving a generic class which will do nothing more than call an arbitrary object’s member function. A set of macros is defined in iocallback.h to allow a user to declare and implement a class derived directly from IOHandler. The mechanism used is described in The C++ Programming Language, by Stroustrup. See EXAMPLES for sample usage.
IOHandler is an abstract base class.
// IOHandler/IOCallback Example #include <ivplus/font.h> #include <ivplus/frame.h> #include <ivplus/painter.h> #include <ivplus/shape.h> #include <ivplus/world.h> #include <ivplus/iohandler.h> #include <ivplus/iocallback.h> #include <ivplus/dispatcher.h> class Blinker : public Interactor { public: Blinker(const char* target); void Blink(long, long); void ActivateBlink(long s, long u); protected: virtual void Redraw(IntCoord, IntCoord, IntCoord, IntCoord); virtual void Reconfig(); private: int width; int height; const char* text; long sec; long usec; // This object will talk to the dispatcher for us. IOHandler* blinker; }; // Invoke the macro which declares a class derived from IOHandler. // This must occur after class Blinker has been fully declared. declare(IOCallback,Blinker) // Invoke the macro which generates the implementation code. implement(IOCallback,Blinker) Blinker::Blinker(const char* msg) { text = msg; } // Redraw() calculates where to draw text so it is centered. void Blinker::Redraw(IntCoord l, IntCoord b, IntCoord r, IntCoord t) { output->ClearRect(canvas, l, b, r, t); int tx = (xmax - output->GetFont()->Width(text))/2; int ty = (ymax - output->GetFont()->Height())/2; output->Text(canvas, text, tx, ty); } void Blinker::Reconfig() { shape->width = output->GetFont()->Width(text); shape->height = output->GetFont()->Height(); shape->Rigid(0, hfil, 0, vfil); } // ActivateBlink() instantiates the IOHandler that will call Blinker::Blink() // and tells the dispatcher we want a timer started. void Blinker::ActivateBlink(long s, long u) { sec = s; usec = u; blinker = new IOCallback(Blinker)(this, Blinker::Blink); Dispatcher::instance().startTimer(sec, usec, blinker); } // Blink() ignores its arguments, since we always want to // use the args from ActivateBlink(). Swaps the foreground and // background colors, then forces a Redraw() by calling Draw() // Resets the timer. void Blinker::Blink(long, long) { output->SetColors(output->GetBgColor(), output->GetFgColor()); Draw(); Dispatcher::instance().stopTimer(blinker); Dispatcher::instance().startTimer(sec, usec, blinker); } int main(int argc, char **argv) { // Create the world to get the connection with the display. World* world = new World("MyApp", argc, argv); // Create the blinking thing. Blinker* blink = new Blinker("Blink, blink!"); // Map the window. world->InsertApplication(new Frame(blink, 2, BevelOut)); // Turn the blinking thing on. Blink at 1 second intervals. blink->ActivateBlink(1,0); // Enter the event loop. world->Run(); return 0; }
generic.h iocallback.h iohandler.h