00001
00002
00003
00004
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;
00017
00018 class PrintControl {
00019
00020
00021
00022
00023
00024
00025
00026 public:
00027 PrintControl(Menu& menu, std::ostream* out);
00028
00029
00030 virtual ~PrintControl();
00031
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
00036
00037 void printAll();
00038
00039
00040 void enable();
00041
00042 void disable();
00043
00044 void printNow();
00045
00046
00047 void clear();
00048
00049
00050 void flush();
00051
00052
00053 std::ostream* out(){return m_print;}
00054
00055
00056 void setOut(std::ostream* o){m_print = o;}
00057
00058
00059
00060 void finishSetup();
00061 void quit(){};
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
00071
00072 Menu& m_menu;
00073
00074
00075 SubMenu* m_sub_menu;
00076
00077
00078 bool m_enabled;
00079
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
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
00127 inline void PrintControl::addPrinter(const std::string& label, Command * cmd, bool enabled)
00128 {
00129 add(cmd, label, enabled);
00130 }
00131
00132 }
00133 #endif
00134