#pragma once #ifndef TNZ_ACTION_INCLUDED #define TNZ_ACTION_INCLUDED #include "tw/tw.h" #undef DVAPI #undef DVVAR #ifdef TWIN_EXPORTS #define DVAPI DV_EXPORT_API #define DVVAR DV_EXPORT_VAR #else #define DVAPI DV_IMPORT_API #define DVVAR DV_IMPORT_VAR #endif class TButton; //------------------------------------------------------------------- class DVAPI TGenericCommandAction { public: TGenericCommandAction() {} virtual ~TGenericCommandAction() {} virtual void sendCommand() = 0; virtual TGenericCommandAction *clone() const = 0; }; //------------------------------------------------------------------- template class TCommandAction : public TGenericCommandAction { public: typedef void (T::*CommandMethod)(); T *m_target; CommandMethod m_method; TCommandAction(T *target, CommandMethod method) : m_target(target), m_method(method){}; void sendCommand() { (m_target->*m_method)(); }; TGenericCommandAction *clone() const { return new TCommandAction(m_target, m_method); } }; //------------------------------------------------------------------- template class TCommandAction1 : public TGenericCommandAction { public: typedef void (T::*CommandMethod)(Arg arg); T *m_target; CommandMethod m_method; Arg m_arg; TCommandAction1(T *target, CommandMethod method, Arg arg) : m_target(target), m_method(method), m_arg(arg){}; void sendCommand() { (m_target->*m_method)(m_arg); }; TGenericCommandAction *clone() const { return new TCommandAction1(m_target, m_method, m_arg); } }; //------------------------------------------------------------------- class DVAPI TCommandSource { vector *m_actions; public: TCommandSource(); virtual ~TCommandSource(); void addAction(TGenericCommandAction *action); void sendCommand(); private: // not implemented TCommandSource(const TCommandSource &); TCommandSource &operator=(const TCommandSource &); }; //------------------------------------------------------------------- template inline void tconnect(TCommandSource &src, T *target, void (T::*method)()) { src.addAction(new TCommandAction(target, method)); } //------------------------------------------------------------------- template inline void tconnect(TCommandSource &src, T *target, void (T::*method)(Arg arg), Arg arg) { src.addAction(new TCommandAction1(target, method, arg)); } //------------------------------------------------------------------- class DVAPI TGuiCommand { class Imp; Imp *m_imp; public: TGuiCommand(string cmdName = "none"); virtual ~TGuiCommand(); TGuiCommand(const TGuiCommand &); TGuiCommand &operator=(const TGuiCommand &); // void setHelp(string longHelp, string shortHelp); // void setHelp(string help) {setHelp(help, help);} // void setTitle(string title); // string getTitle(); bool isToggle() const; void setIsToggle(bool on); void setStatus(bool on); bool getStatus() const; void enable(); void disable(); void add(TButton *button); void setAction(TGenericCommandAction *action); // debug!! void sendCommand(); static void execute(string cmdName); static void getNames(std::vector &cmdNames); private: }; //------------------------------------------------------------------- class DVAPI TGuiCommandExecutor : public TGuiCommand { public: TGuiCommandExecutor(string cmdName) : TGuiCommand(cmdName) { setAction(new TCommandAction( this, &TGuiCommandExecutor::onCommand)); } virtual ~TGuiCommandExecutor() {} virtual void onCommand() = 0; }; //------------------------------------------------------------------- class TGuiCommandGenericTarget { protected: TGuiCommand m_command; public: TGuiCommandGenericTarget(string cmdName) : m_command(cmdName) {} virtual ~TGuiCommandGenericTarget() {} virtual void activate() = 0; virtual void deactivate() { m_command.setAction(0); } }; //------------------------------------------------------------------- template class TGuiCommandTarget : public TGuiCommandGenericTarget { public: typedef void (T::*Method)(); T *m_target; Method m_method; public: TGuiCommandTarget(string cmdName, T *target, Method method) : TGuiCommandGenericTarget(cmdName), m_target(target), m_method(method) {} void activate() { m_command.setAction(new TCommandAction(m_target, m_method)); } }; #endif