Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

PrintControl.h

Go to the documentation of this file.
00001 //     $Id: PrintControl.h,v 1.1.1.1 2001/01/04 01:01:11 burnett Exp $
00002 //  Author: Toby Burnett
00003 //
00004 // Define the singleton PrintControl class 
00005 //
00006 #ifndef PRINTCONTROL_H
00007 #define PRINTCONTROL_H
00008 
00009 #include "gui/Menu.h"
00010 
00011 #include <vector>
00012 #include <iostream>
00013 
00014 namespace gui {
00015 
00016 class PrintCommand; // defer to define in .cxx since private
00017 
00018 class PrintControl  {
00019     // When this class has been set up, new print commands can be added to the menu
00020     // by:
00021     //      
00022     //      PrintControl::instance()->addPrinter("my printout", myCommand);
00023     //
00024     // where myCommand is an instance of a subclass of Command that
00025     // will output to the std::ostream* PrintControl::instance()->out();
00026 public:
00027     PrintControl(Menu& menu, std::ostream* out);
00028     // constructor--specify a stream to print to
00029 
00030     virtual ~PrintControl(); 
00031     // dtor 
00032 
00033     void addPrinter(const std::string& label, Command * cmd, bool enabled=0);
00034     void add(Command * cmd, const std::string& label,  bool enabled=0);
00035     // adds a menu item, associated with any command, presumably a printer
00036 
00037     void printAll();
00038     // print if enabled -- intended for after event
00039 
00040     void enable();
00041 
00042     void disable();
00043     
00044     void printNow();
00045     // print anyway
00046 
00047     void clear();
00048     // clear the display device (may not be implemented)
00049 
00050     void flush();
00051     // pass on to implementor
00052 
00053     std::ostream* out(){return m_print;}
00054     // get at the ostream
00055 
00056     void setOut(std::ostream* o){m_print = o;}
00057     // replace the ostream object
00058 
00059     // these implement the MenuClient interface
00060     void finishSetup();    // called by the Menu object after user menu commands executed
00061     void quit(){};  // dummy for now
00062 
00063     const char* nameOf()const {return "PrintControl";}
00064     static PrintControl* instance();
00065     
00066 private:
00067     void setup();
00068     
00069     std::ostream* m_print;
00070     // the stream to print to
00071         
00072     Menu& m_menu;
00073     // reference to our menu
00074     
00075     SubMenu* m_sub_menu;
00076     // pointer to print sub menu
00077 
00078     bool m_enabled;
00079     // determines if printing occurs after event
00080 
00081     typedef std::vector< PrintCommand* > PrintList;
00082 
00083     PrintList m_print_commands;
00084 
00085     static PrintControl* s_instance;
00086 
00087 
00088 };
00089 
00096 template<class T>
00097 class Printer_T : public Command {
00098 public:
00099     Printer_T(const T* t):m_t(t),m_out(PrintControl::instance()->out()){}
00100     void execute(){m_t->printOn(*m_out);}
00101 private:
00102     const T* m_t;
00103     std::ostream* m_out;
00104 };
00105 
00106 
00107 class PrintCommand : public Command {
00108     // private class for PrintControl--needs to be here
00109     friend class PrintControl;
00110     PrintCommand( Command* cmd, PrintControl* printer, bool enabled )
00111         :m_cmd(cmd), m_printer(printer), m_enabled(enabled){}
00112     ~PrintCommand() ;
00113     void execute() {
00114         if(m_enabled){ 
00115             m_cmd->execute();   
00116             m_printer->flush();
00117         }
00118     }
00119     void enable(){m_enabled=true;}
00120     void disable(){m_enabled=false;}
00121     Command* m_cmd;
00122     PrintControl* m_printer;
00123     bool m_enabled;
00124 };
00125 
00126 // here for temporary 
00127 inline void PrintControl::addPrinter(const std::string& label, Command * cmd, bool enabled)
00128 {
00129     add(cmd, label, enabled);
00130 }
00131 
00132 } // namespace gui
00133 #endif
00134 

Generated at Wed Nov 21 12:20:55 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000