tahoma2d/toonz/sources/include/tw/action.h

187 lines
4.5 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#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;
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI TGenericCommandAction {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TGenericCommandAction() {}
virtual ~TGenericCommandAction() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual void sendCommand() = 0;
virtual TGenericCommandAction *clone() const = 0;
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
template <class T>
2016-06-15 18:43:10 +12:00
class TCommandAction : public TGenericCommandAction {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
typedef void (T::*CommandMethod)();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
T *m_target;
CommandMethod m_method;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TCommandAction<T>(T *target, CommandMethod method)
: m_target(target), m_method(method){};
void sendCommand() { (m_target->*m_method)(); };
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TGenericCommandAction *clone() const {
return new TCommandAction<T>(m_target, m_method);
}
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
template <class T, class Arg>
2016-06-15 18:43:10 +12:00
class TCommandAction1 : public TGenericCommandAction {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
typedef void (T::*CommandMethod)(Arg arg);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
T *m_target;
CommandMethod m_method;
Arg m_arg;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TCommandAction1<T, Arg>(T *target, CommandMethod method, Arg arg)
: m_target(target), m_method(method), m_arg(arg){};
void sendCommand() { (m_target->*m_method)(m_arg); };
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TGenericCommandAction *clone() const {
return new TCommandAction1<T, Arg>(m_target, m_method, m_arg);
}
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI TCommandSource {
vector<TGenericCommandAction *> *m_actions;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TCommandSource();
virtual ~TCommandSource();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void addAction(TGenericCommandAction *action);
void sendCommand();
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// not implemented
TCommandSource(const TCommandSource &);
TCommandSource &operator=(const TCommandSource &);
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
template <class T>
2016-06-15 18:43:10 +12:00
inline void tconnect(TCommandSource &src, T *target, void (T::*method)()) {
src.addAction(new TCommandAction<T>(target, method));
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------
template <class T, class Arg>
2016-06-15 18:43:10 +12:00
inline void tconnect(TCommandSource &src, T *target, void (T::*method)(Arg arg),
Arg arg) {
src.addAction(new TCommandAction1<T, Arg>(target, method, arg));
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI TGuiCommand {
class Imp;
Imp *m_imp;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TGuiCommand(string cmdName = "none");
virtual ~TGuiCommand();
TGuiCommand(const TGuiCommand &);
TGuiCommand &operator=(const TGuiCommand &);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// void setHelp(string longHelp, string shortHelp);
// void setHelp(string help) {setHelp(help, help);}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// void setTitle(string title);
// string getTitle();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool isToggle() const;
void setIsToggle(bool on);
void setStatus(bool on);
bool getStatus() const;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void enable();
void disable();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void add(TButton *button);
void setAction(TGenericCommandAction *action);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// debug!!
void sendCommand();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
static void execute(string cmdName);
static void getNames(std::vector<string> &cmdNames);
2016-03-19 06:57:51 +13:00
private:
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class DVAPI TGuiCommandExecutor : public TGuiCommand {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TGuiCommandExecutor(string cmdName) : TGuiCommand(cmdName) {
setAction(new TCommandAction<TGuiCommandExecutor>(
this, &TGuiCommandExecutor::onCommand));
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual ~TGuiCommandExecutor() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
virtual void onCommand() = 0;
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
class TGuiCommandGenericTarget {
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
TGuiCommand m_command;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TGuiCommandGenericTarget(string cmdName) : m_command(cmdName) {}
virtual ~TGuiCommandGenericTarget() {}
virtual void activate() = 0;
virtual void deactivate() { m_command.setAction(0); }
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
template <class T>
2016-06-15 18:43:10 +12:00
class TGuiCommandTarget : public TGuiCommandGenericTarget {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
typedef void (T::*Method)();
T *m_target;
Method m_method;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TGuiCommandTarget(string cmdName, T *target, Method method)
: TGuiCommandGenericTarget(cmdName), m_target(target), m_method(method) {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void activate() {
m_command.setAction(new TCommandAction<T>(m_target, m_method));
}
2016-03-19 06:57:51 +13:00
};
#endif