Update of /cvsroot/audacity/audacity-src/src/commands
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv31379/src/commands
Added Files:
AppCommandEvent.cpp AppCommandEvent.h BatchEvalCommand.h
Command.h CommandBuilder.cpp CommandBuilder.h
CommandHandler.cpp CommandHandler.h DebugPrintCommand.h
ScriptCommandRelay.cpp ScriptCommandRelay.h
Log Message:
Several new files for dealing with passing around and applying 'commands' in a
more abstract way, and for allowing them to be sent to the main thread from the
script thread.
--- NEW FILE: ScriptCommandRelay.cpp ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file ScriptCommandRelay.cpp
\brief Contains definitions for ScriptCommandRelay
*//****************************************************************//**
\class ScriptCommandRelay
\brief ScriptCommandRelay is just a way to move some of the scripting-specific
code out of LoadModules.
*//*******************************************************************/
#include "ScriptCommandRelay.h"
#include "../BatchCommands.h"
#include "../Audacity.h"
#include "Command.h"
#include "CommandBuilder.h"
#include "AppCommandEvent.h"
#include "CommandHandler.h"
#include <wx/wx.h>
#include "../Project.h"
// Declare static class members
CommandHandler *ScriptCommandRelay::sCmdHandler;
tpRegScriptServerFunc ScriptCommandRelay::sScriptFn;
tpScriptServerResponseFunc ScriptCommandRelay::sScriptOutFn;
void ScriptCommandRelay::SetRegScriptServerFunc(tpRegScriptServerFunc scriptFn)
{ sScriptFn = scriptFn; }
void ScriptCommandRelay::SetScriptServerResponseFunc(tpScriptServerResponseFunc
scriptOutFn)
{ sScriptOutFn = scriptOutFn; }
void ScriptCommandRelay::SetCommandHandler(CommandHandler &ch)
{ sCmdHandler = &ch; }
void ScriptCommandRelay::Run()
{
wxASSERT( sScriptFn != NULL );
while( true )
sScriptFn(&ScriptCommandRelay::ExecCommand);
}
/// This is the function which actually obeys one command. Rather than applying
/// the command directly, an event containing a reference to the command is sent
/// to the main (GUI) thread. This is because having more than one thread access
/// the GUI at a time causes problems with wxwidgets.
int ScriptCommandRelay::ExecCommand(wxString *pIn)
{
CommandBuilder builder(*pIn);
if (builder.WasValid())
{
Command *cmd = builder.GetCommand();
AppCommandEvent ev;
ev.SetCommand(cmd);
GetActiveProject()->AddPendingEvent(ev);
} else
{
wxMessageOutputDebug().Printf(wxT("Syntax error!\n"));
// TODO: Send message back to script
}
return 0;
}
void ScriptCommandRelay::SendResponse(wxString &pOut)
{
sScriptOutFn(&pOut);
}
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: AppCommandEvent.cpp ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file AppCommandEvent.cpp
\brief Implements AppCommandEvent.
*//****************************************************************//**
\class AppCommandEvent
\brief An event 'envelope' for sending Command objects through the wxwidgets
event loop.
This allows commands to be communicated from the script thread to the main
thread.
*//*******************************************************************/
#include "AppCommandEvent.h"
DEFINE_EVENT_TYPE(wxEVT_APP_COMMAND_RECEIVED)
IMPLEMENT_DYNAMIC_CLASS(AppCommandEvent, wxEvent)
AppCommandEvent::AppCommandEvent(wxEventType commandType, int id)
: wxCommandEvent(commandType, id), mCommand(NULL)
{ }
// Copy constructor
AppCommandEvent::AppCommandEvent(const AppCommandEvent &event) :
wxCommandEvent(event)
{
this->mCommand = event.mCommand;
}
AppCommandEvent::~AppCommandEvent()
{
}
// Clone is required by wxwidgets; implemented via copy constructor
wxEvent *AppCommandEvent::Clone() const
{
return new AppCommandEvent(*this);
}
/// Store a pointer to a command object
void AppCommandEvent::SetCommand(Command *cmd)
{
wxASSERT(NULL == mCommand);
mCommand = cmd;
}
// When the command pointer is retrieved, the caller is responsible for
// deletion.
Command *AppCommandEvent::GetCommand()
{
Command *tmp = mCommand;
mCommand = NULL;
return tmp;
}
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: Command.h ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file Command.h
\brief Contains declaration of Command base class.
*//*******************************************************************/
#ifndef __COMMAND__
#define __COMMAND__
#include "../WrappedType.h"
#include <wx/msgout.h>
#include <map>
class AudacityApp;
class AudacityProject;
class wxString;
class CommandExecutionContext {
public:
AudacityApp *app;
AudacityProject *proj;
CommandExecutionContext(AudacityApp *app, AudacityProject *proj)
: app(app), proj(proj) {}
};
// Map from parameter name to the value of the parameter
typedef std::map<wxString, WrappedType> tParamMap ;
class Command
{
private:
wxString mName;
tParamMap mParams; // NB. This is not really used yet! (30 May 2009)
public:
Command(const wxString &name) : mName(name) {}
virtual ~Command() {};
void SetParameter(const wxString ¶mName, const WrappedType
¶mValue)
{
mParams[paramName] = paramValue;
}
// Subclasses should override this.
virtual bool Apply(CommandExecutionContext context)
{
return true;
}
};
#endif /* End of include guard: __COMMAND__ */
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: BatchEvalCommand.h ---
/**********************************************************************
Audacity: A Digital Audio Editor
Audacity(R) is copyright (c) 1999-2009 Audacity Team.
License: GPL v2. See License.txt.
BatchEvalCommand.h
Dan Horgan
******************************************************************//**
\class BatchEvalCommand
\brief Given a string representing a command, pass it to the BatchCommands
system.
The eventual aim is to move the code from BatchCommands out into separate
command classes, but until this happens, BatchEvalCommand can act as a 'bridge'
to that system.
*//*******************************************************************/
#ifndef __BATCHEVALCOMMAND__
#define __BATCHEVALCOMMAND__
#include "Command.h"
#include "../BatchCommands.h"
class BatchEvalCommand : public Command
{
private:
wxString mCmdName;
wxString mCmdParams;
public:
BatchEvalCommand(const wxString &cmdName, const wxString &cmdParams) :
Command(wxT("BatchEvalCommand")), mCmdName(cmdName), mCmdParams(cmdParams) {}
~BatchEvalCommand() {}
virtual bool Apply(CommandExecutionContext context)
{
// Create a Batch that will have just one command in it...
BatchCommands Batch;
return Batch.ApplyCommand(mCmdName, mCmdParams);
}
};
#endif /* End of include guard: __BATCHEVALCOMMAND__ */
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: CommandBuilder.h ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file CommandBuilder.h
\brief Contains declaration of CommandBuilder class.
*//*******************************************************************/
#include <wx/string.h>
class Command;
// CommandBuilder has the task of validating and interpreting a command string.
// If the string represents a valid command, it builds the command object.
class CommandBuilder
{
private:
bool mValid;
Command *mCommand;
wxString mError;
void BuildCommand(const wxString &cmdName, const wxString &cmdParams);
void BuildCommand(const wxString &cmdString);
public:
CommandBuilder(const wxString &cmdString);
CommandBuilder(const wxString &cmdName, const wxString &cmdParams);
~CommandBuilder();
bool WasValid();
Command *GetCommand();
const wxString &GetErrorMessage();
};
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: CommandBuilder.cpp ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file CommandBuilder.cpp
\brief Contains definitions for CommandBuilder class.
*//****************************************************************//**
\class CommandBuilder
\brief A type of factory for Commands of various sorts.
CommandBuilder has the task of deciding what command is meant by a given
command string, and producing a suitable command object. For now, it doesn't
actually do any processing - it just passes everything on to the BatchCommand
system by constructing BatchCommandEval objects.
*//*******************************************************************/
#include "CommandBuilder.h"
#include <wx/log.h>
#include <wx/wx.h>
#include "DebugPrintCommand.h"
#include "BatchEvalCommand.h"
CommandBuilder::CommandBuilder(const wxString &cmdString)
: mValid(false), mCommand(NULL)
{
BuildCommand(cmdString);
}
CommandBuilder::CommandBuilder(const wxString &cmdName, const wxString ¶ms)
: mValid(false), mCommand(NULL)
{
BuildCommand(cmdName, params);
}
CommandBuilder::~CommandBuilder() { }
bool CommandBuilder::WasValid() { return mValid; }
const wxString &CommandBuilder::GetErrorMessage() { return mError; }
Command *CommandBuilder::GetCommand()
{
wxASSERT(mValid);
wxASSERT(NULL != mCommand);
return mCommand;
}
void CommandBuilder::BuildCommand(const wxString &cmdName, const wxString
&cmdParams)
{
/*
wxMessageOutputDebug().Printf(cmdName);
// Temp
mValid = true;
// Split up parameters and add them to the map
// The checking below should be replaced by a lookup + polymorphism
// eventually
// See if the name refers to a 'special command'
// See if the name refers to an effect
//Effect *f = EffectManager::Get().GetEffectByIdentifier( cmdName );
//if( f!=NULL )
//{
// ApplyEffectCommand( f, command, params );
// // TODO store command somewhere else...
// mCommand = new EffectCommand(f);
//}
*/
// See if the name refers to a menu command
mCommand = new BatchEvalCommand(cmdName, cmdParams);
mValid = true;
}
void CommandBuilder::BuildCommand(const wxString &cmdString)
{
// Find the command name terminator... ignore line if not found
int splitAt = cmdString.Find(wxT(':'));
if (splitAt < 0) {
mError = wxT("BAD - Missing ':'?");
// TODO
}
wxString cmdName = cmdString.Left(splitAt).Strip(wxString::both);
wxString cmdParams = cmdString.Mid(splitAt+1).Strip(wxString::both);
BuildCommand(cmdName, cmdParams);
// Temp
//mCommand = new DebugPrintCommand(cmdString);
}
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: CommandHandler.cpp ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file CommandHandler.cpp
\brief Contains definitions for the CommandHandler class.
*//****************************************************************//**
\class CommandHandler
\brief Contains methods for applying commands that are passed to it.
May possibly inherit from wxEvtHandler in the future...
*//*******************************************************************/
#include <wx/event.h>
#include "CommandHandler.h"
#include "../Project.h"
#include "Command.h"
#include "AppCommandEvent.h"
#include "ScriptCommandRelay.h"
CommandHandler::CommandHandler(AudacityApp &app)
//: wxEvtHandler(),
: mCurrentContext(new CommandExecutionContext(&app, GetActiveProject()))
{
//app.SetNextHandler(this);
}
CommandHandler::~CommandHandler()
{
delete mCurrentContext;
}
void CommandHandler::SetProject(AudacityProject *proj)
{
mCurrentContext->proj = proj;
}
void CommandHandler::OnReceiveCommand(AppCommandEvent &event)
{
// First retrieve the actual command from the event 'envelope'.
Command *cmd = event.GetCommand();
// Then apply it to current application & project. Note that the
// command may change the context - for example, switching to a
// different project.
bool rc = cmd->Apply(*mCurrentContext);
// Rudimentary error handling
wxString msgOut;
if (rc)
msgOut = wxT("OK");
else
msgOut = wxT("Command FAILED!");
ScriptCommandRelay::SendResponse(msgOut);
// Done with the command so delete it.
delete cmd;
}
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: AppCommandEvent.h ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file AppCommandEvent.h
\brief Headers and event table macros for AppCommandEvent
*//*******************************************************************/
#ifndef __APPCOMMANDEVENT__
#define __APPCOMMANDEVENT__
#include <wx/event.h>
#include "../Audacity.h"
DECLARE_EXPORTED_EVENT_TYPE(AUDACITY_DLL_API, wxEVT_APP_COMMAND_RECEIVED, -1);
class Command;
class AppCommandEvent : public wxCommandEvent
{
private:
Command *mCommand;
public:
AppCommandEvent(wxEventType commandType = wxEVT_APP_COMMAND_RECEIVED, int id
= 0);
AppCommandEvent(const AppCommandEvent &event);
~AppCommandEvent();
virtual wxEvent *Clone() const;
void SetCommand(Command *cmd);
Command *GetCommand();
private:
DECLARE_DYNAMIC_CLASS(AppCommandEvent)
};
typedef void (wxEvtHandler::*wxAppCommandEventFunction)(AppCommandEvent&);
#define wxAppCommandEventHandler(func) \
(wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxAppCommandEventFunction,
&func)
#define EVT_APP_COMMAND(winid, fn) DECLARE_EVENT_TABLE_ENTRY(
wxEVT_APP_COMMAND_RECEIVED, winid, wxID_ANY, wxAppCommandEventHandler(fn),
(wxObject *) NULL ),
#endif /* End of include guard: __APPCOMMANDEVENT__ */
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: DebugPrintCommand.h ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file DebugPrintCommand.h
\brief Contains definition of DebugPrintCommand class.
*//***************************************************************//***
\class DebugPrintCommand
\brief Command to tell Audacity to print out a message.
Intended for testing and debugging.
*//*******************************************************************/
#ifndef __DEBUGPRINTCOMMAND__
#define __DEBUGPRINTCOMMAND__
#include "Command.h"
class DebugPrintCommand : public Command
{
private:
wxString mMessage;
public:
DebugPrintCommand(const wxString &message) :
Command(wxT("DebugPrintCommand")), mMessage(message) {}
~DebugPrintCommand() {}
virtual bool Apply(CommandExecutionContext context)
{
wxMessageOutputDebug().Printf(mMessage);
return true;
}
};
#endif /* End of include guard: __DEBUGPRINTCOMMAND__ */
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: ScriptCommandRelay.h ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file ScriptCommandRelay.h
\brief Contains declarations for ScriptCommandRelay
*//*******************************************************************/
#ifndef __SCRIPTCOMMANDRELAY__
#define __SCRIPTCOMMANDRELAY__
#include "../Audacity.h"
#include <wx/string.h>
class CommandHandler;
extern "C" {
typedef int (*tpExecScriptServerFunc)( wxString * pIn);
typedef int (*tpRegScriptServerFunc)(tpExecScriptServerFunc pFn);
typedef int (*tpScriptServerResponseFunc)( wxString * pOut);
class ScriptCommandRelay
{
private:
static CommandHandler *sCmdHandler;
static tpRegScriptServerFunc sScriptFn;
static tpScriptServerResponseFunc sScriptOutFn;
public:
static void SetRegScriptServerFunc(tpRegScriptServerFunc scriptFn);
static void SetScriptServerResponseFunc(tpScriptServerResponseFunc
scriptOutFn);
static void SetCommandHandler(CommandHandler &ch);
static void Run();
AUDACITY_DLL_API static int ExecCommand( wxString * pIn );
static void SendResponse(wxString &pOut);
};
} // End 'extern C'
#endif /* End of include guard: __SCRIPTCOMMANDRELAY__ */
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
--- NEW FILE: CommandHandler.h ---
/**********************************************************************
Audacity - A Digital Audio Editor
Copyright 1999-2009 Audacity Team
License: GPL v2 - see LICENSE.txt
Dan Horgan
******************************************************************//**
\file CommandHandler.h
\brief Contains declarations for the CommandHandler class.
*//******************************************************************/
#ifndef __COMMANDHANDLER__
#define __COMMANDHANDLER__
//#include <wx/event.h>
#include "../AudacityApp.h"
class AudacityProject;
class AppCommandEvent;
class Command;
class CommandExecutionContext;
//class CommandHandler : public wxEvtHandler
class CommandHandler
{
private:
CommandExecutionContext *mCurrentContext;
public:
CommandHandler(AudacityApp &app);
~CommandHandler();
// This should only be used during initialization
void SetProject(AudacityProject *proj);
// Whenever a command is recieved, process it.
void OnReceiveCommand(AppCommandEvent &event);
/*
protected:
DECLARE_EVENT_TABLE()
*/
};
#endif /* End of include guard: __COMMANDHANDLER__ */
// Indentation settings for Vim and Emacs and unique identifier for Arch, a
// version control system. Please do not modify past this point.
//
// Local Variables:
// c-basic-offset: 3
// indent-tabs-mode: nil
// End:
//
// vim: et sts=3 sw=3
// arch-tag: TBD
------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, &
iPhoneDevCamp as they present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://p.sf.net/sfu/creativitycat-com
_______________________________________________
Audacity-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/audacity-cvs