Code::Blocks  SVN r11506
Classes | Public Types | Public Member Functions | Private Types | Private Member Functions | Private Attributes | Friends | List of all members
ScriptingManager Class Reference

Provides scripting in Code::Blocks. More...

#include <scriptingmanager.h>

Inheritance diagram for ScriptingManager:
Collaboration diagram for ScriptingManager:

Classes

struct  MenuBoundScript
 
struct  TrustedScriptProps
 Script trusts container struct. More...
 

Public Types

typedef std::map< wxString, TrustedScriptPropsTrustedScripts
 Script trusts container struct. More...
 

Public Member Functions

bool LoadScript (const wxString &filename)
 Loads a script. More...
 
bool LoadBuffer (const wxString &buffer, const wxString &debugName=_T("CommandLine"))
 Loads a string buffer. More...
 
wxString LoadBufferRedirectOutput (const wxString &buffer)
 Loads a string buffer and captures its output. More...
 
wxString GetErrorString (SquirrelError *exception=nullptr, bool clearErrors=true)
 Returns an accumulated error string. More...
 
void DisplayErrors (SquirrelError *exception=nullptr, bool clearErrors=true)
 Display error dialog. More...
 
void InjectScriptOutput (const wxString &output)
 Injects script output. More...
 
int Configure ()
 Configure scripting in Code::Blocks. More...
 
bool RegisterScriptPlugin (const wxString &name, const wxArrayInt &ids)
 Registers a script plugin menu IDs with the callback function. More...
 
bool RegisterScriptMenu (const wxString &menuPath, const wxString &scriptOrFunc, bool isFunction)
 Script-bound function to register a script with a menu item. More...
 
bool UnRegisterScriptMenu (const wxString &menuPath)
 Script-bound function to unregister a script's menu item. More...
 
bool UnRegisterAllScriptMenus ()
 Unregister all scripts' menu items. More...
 
bool IsScriptTrusted (const wxString &script)
 Security function. More...
 
bool IsCurrentlyRunningScriptTrusted ()
 Security function. More...
 
void TrustScript (const wxString &script, bool permanently)
 Security function to trust a script. More...
 
void TrustCurrentlyRunningScript (bool permanently)
 Security function to trust a script. More...
 
bool RemoveTrust (const wxString &script)
 Remove a script trust. More...
 
void RefreshTrusts ()
 Force refresh of script trusts. More...
 
const TrustedScriptsGetTrustedScripts ()
 Access the script trusts container (const). More...
 
ScriptingManageroperator= (cb_unused const ScriptingManager &rhs)
 

Private Types

typedef std::map< int, MenuBoundScriptMenuIDToScript
 
typedef std::set< wxStringIncludeSet
 

Private Member Functions

 ScriptingManager (cb_unused const ScriptingManager &rhs)
 
void OnScriptMenu (wxCommandEvent &event)
 
void OnScriptPluginMenu (wxCommandEvent &event)
 
void RegisterScriptFunctions ()
 
 ScriptingManager ()
 
 ~ScriptingManager () override
 

Private Attributes

wxCriticalSection cs
 
TrustedScripts m_TrustedScripts
 
MenuIDToScript m_MenuIDToScript
 
bool m_AttachedToMainWindow
 
wxString m_CurrentlyRunningScriptFile
 
IncludeSet m_IncludeSet
 
MenuItemsManager m_MenuItemsManager
 

Friends

class Mgr< ScriptingManager >
 

Additional Inherited Members

- Static Public Member Functions inherited from Mgr< ScriptingManager >
static bool Valid ()
 
static ScriptingManagerGet ()
 
static void Free ()
 
- Protected Member Functions inherited from Mgr< ScriptingManager >
 Mgr ()
 
virtual ~Mgr ()
 

Detailed Description

Provides scripting in Code::Blocks.

The scripting engine used is Squirrel (http:://www.squirrel-lang.org).

Here's an example to load and execute a script:

And here's an example to call a script function:

// int return value
// C++ equivalent: int retValue = FunctionName("str_arg", 5, 1.0);
SqPlus::SquirrelFunction<int> myfunc("FunctionName");
int retValue = myfunc(_T("str_arg"), 5, 1.0);
// void return
// C++ equivalent: FunctionName("str_arg", 5, 1.0);
SqPlus::SquirrelFunction<void> myfunc("FunctionName");
myfunc(_T("str_arg"), 5, 1.0);

The templated type denotes the function's return type. Also note that the function name is not unicode (we 're not using Squirrel in unicode mode).

Definition at line 50 of file scriptingmanager.h.


Class Documentation

◆ ScriptingManager::MenuBoundScript

struct ScriptingManager::MenuBoundScript

Definition at line 239 of file scriptingmanager.h.

Collaboration diagram for ScriptingManager::MenuBoundScript:
Class Members
bool isFunc
wxString scriptOrFunc

◆ ScriptingManager::TrustedScriptProps

struct ScriptingManager::TrustedScriptProps

Script trusts container struct.

Definition at line 56 of file scriptingmanager.h.

Collaboration diagram for ScriptingManager::TrustedScriptProps:
Class Members
wxUint32 crc
bool permanent

Member Typedef Documentation

◆ IncludeSet

typedef std::set<wxString> ScriptingManager::IncludeSet
private

Definition at line 250 of file scriptingmanager.h.

◆ MenuIDToScript

typedef std::map<int, MenuBoundScript> ScriptingManager::MenuIDToScript
private

Definition at line 244 of file scriptingmanager.h.

◆ TrustedScripts

Script trusts container struct.

Definition at line 64 of file scriptingmanager.h.

Constructor & Destructor Documentation

◆ ScriptingManager() [1/2]

ScriptingManager::ScriptingManager ( cb_unused const ScriptingManager rhs)
private

◆ ScriptingManager() [2/2]

ScriptingManager::ScriptingManager ( )
private

◆ ~ScriptingManager()

ScriptingManager::~ScriptingManager ( )
overrideprivate

Member Function Documentation

◆ Configure()

int ScriptingManager::Configure ( )

Configure scripting in Code::Blocks.

Returns
0 on success or a negative value on error.

Definition at line 268 of file scriptingmanager.cpp.

◆ DisplayErrors()

void ScriptingManager::DisplayErrors ( SquirrelError *  exception = nullptr,
bool  clearErrors = true 
)

Display error dialog.

Displays an error dialog containing exception info and any other script errors. Calls GetErrorString() internally. You should normally call this function inside your catch handler for SquirrelFunction<>() calls.

Parameters
exceptionA pointer to the exception object containing the error. Can be NULL (default).
clearErrorsIf true (default), when this function returns all accumulated error messages are cleared.

Definition at line 245 of file scriptingmanager.cpp.

References _(), cbMessageBox(), Manager::Get(), GetErrorString(), wxString::IsEmpty(), wxDialog::ShowModal(), wxICON_ERROR, wxID_YES, wxNO_DEFAULT, and wxYES_NO.

Referenced by CompilerCommandGenerator::DoBuildScripts(), WizPageBase::GetNext(), WizPageBase::GetPrev(), ProjectOptionsDlg::IsScriptValid(), Wiz::Launch(), Wiz::OnAttach(), ScriptBindings::XrcDialog::OnButton(), WizPage::OnButton(), CodeBlocksApp::OnInit(), WizPageBase::OnPageChanged(), WizPageBase::OnPageChanging(), CodeBlocksApp::OnRun(), OnScriptMenu(), Wiz::RunCustomWizard(), Wiz::RunFilesWizard(), Wiz::RunProjectWizard(), MainFrame::RunStartupScripts(), and Wiz::RunTargetWizard().

◆ GetErrorString()

wxString ScriptingManager::GetErrorString ( SquirrelError *  exception = nullptr,
bool  clearErrors = true 
)

Returns an accumulated error string.

Returns an error string for the passed exception (if any) plus any accumulated script engine errors (e.g. from failed function calls).

Parameters
exceptionA pointer to the exception object containing the error. Can be NULL (default).
clearErrorsIf true (default), when this function returns all accumulated error messages are cleared.
Returns
The error string. If empty, it means "no errors".

Definition at line 232 of file scriptingmanager.cpp.

References cbC2U(), wxString::Clear(), and s_ScriptErrors.

Referenced by DisplayErrors().

◆ GetTrustedScripts()

const ScriptingManager::TrustedScripts & ScriptingManager::GetTrustedScripts ( )

Access the script trusts container (const).

Returns
The script trusts container.

Definition at line 419 of file scriptingmanager.cpp.

References m_TrustedScripts.

Referenced by ScriptingSettingsDlg::FillTrusts().

◆ InjectScriptOutput()

void ScriptingManager::InjectScriptOutput ( const wxString output)

Injects script output.

This function is for advanced uses. It's used when some code sets a different print function for the scripting engine. When this happens, ScriptingManager no longer receives engine output. If you do something like that, use this function to "forward" all script output to ScriptingManager.

Parameters
outputThe engine's output to inject.

Definition at line 263 of file scriptingmanager.cpp.

Referenced by ScriptConsolePrintFunc().

◆ IsCurrentlyRunningScriptTrusted()

bool ScriptingManager::IsCurrentlyRunningScriptTrusted ( )

Security function.

Returns
True if the script is trusted, false if not.
See also
TrustScript(), TrustCurrentlyRunningScript(), IsScriptTrusted().

Definition at line 358 of file scriptingmanager.cpp.

References IsScriptTrusted(), and m_CurrentlyRunningScriptFile.

◆ IsScriptTrusted()

bool ScriptingManager::IsScriptTrusted ( const wxString script)

Security function.

Parameters
scriptThe script's full filename.
Returns
True if the script is trusted, false if not.
See also
TrustScript(), TrustCurrentlyRunningScript(), IsCurrentlyRunningScriptTrusted().

Definition at line 342 of file scriptingmanager.cpp.

References _(), _T, cbMessageBox(), wxCrc32::FromFile(), m_TrustedScripts, and wxICON_WARNING.

Referenced by IsCurrentlyRunningScriptTrusted().

◆ LoadBuffer()

bool ScriptingManager::LoadBuffer ( const wxString buffer,
const wxString debugName = _T("CommandLine") 
)

Loads a string buffer.

Parameters
bufferThe script buffer to compile and run.
debugNameA debug name. This will appear in any errors displayed.
Returns
True if the script compiled, false if not.

Definition at line 174 of file scriptingmanager.cpp.

References _(), _T, wxString::c_str(), cbC2U(), cbMessageBox(), cbU2C(), wxString::Clear(), F(), wxString::Format(), Manager::Get(), Manager::GetLogManager(), LogManager::LogWarning(), m_IncludeSet, UnixFilename(), wxString::wx_str(), and wxICON_ERROR.

Referenced by CompilerCommandGenerator::DoBuildScripts(), ProjectOptionsDlg::IsScriptValid(), Wiz::Launch(), LoadBufferRedirectOutput(), LoadScript(), and CodeBlocksApp::OnInit().

◆ LoadBufferRedirectOutput()

wxString ScriptingManager::LoadBufferRedirectOutput ( const wxString buffer)

Loads a string buffer and captures its output.

Parameters
bufferThe script buffer to compile and run.
Returns
The script's output (if any).

Definition at line 218 of file scriptingmanager.cpp.

References capture, CaptureScriptOutput(), wxString::Clear(), LoadBuffer(), ScriptsPrintFunc(), sq_setprintfunc(), and wxEmptyString.

Referenced by MacrosManager::ReplaceMacros().

◆ LoadScript()

bool ScriptingManager::LoadScript ( const wxString filename)

◆ OnScriptMenu()

void ScriptingManager::OnScriptMenu ( wxCommandEvent event)
private

◆ OnScriptPluginMenu()

void ScriptingManager::OnScriptPluginMenu ( wxCommandEvent event)
private

◆ operator=()

ScriptingManager& ScriptingManager::operator= ( cb_unused const ScriptingManager rhs)
inline

◆ RefreshTrusts()

void ScriptingManager::RefreshTrusts ( )

◆ RegisterScriptFunctions()

void ScriptingManager::RegisterScriptFunctions ( )
private

Definition at line 129 of file scriptingmanager.cpp.

◆ RegisterScriptMenu()

bool ScriptingManager::RegisterScriptMenu ( const wxString menuPath,
const wxString scriptOrFunc,
bool  isFunction 
)

Script-bound function to register a script with a menu item.

Parameters
menuPathThe full menu path. This can be separated by slashes (/) to create submenus (e.g. "MyScripts/ASubMenu/MyItem"). If the last part of the string ("MyItem" in the example) starts with a dash (-) (e.g. "-MyItem") then a menu separator is prepended before the actual menu item.
scriptOrFuncThe script's filename or a script's function name.
isFunctionIf true, the scriptOrFunc parameter is considered to be a script filename. If false, it is considered to be a script's function name.
Returns
True on success, false on failure.

Definition at line 291 of file scriptingmanager.cpp.

References _(), wxString::c_str(), MenuItemsManager::CreateFromString(), F(), Manager::Get(), Manager::GetAppFrame(), Manager::GetAppWindow(), Manager::GetLogManager(), ScriptingManager::MenuBoundScript::isFunc, LogManager::Log(), m_AttachedToMainWindow, m_MenuIDToScript, m_MenuItemsManager, OnScriptMenu(), ScriptingManager::MenuBoundScript::scriptOrFunc, wxMenuItem::SetHelp(), wxString::wx_str(), and wxNewId().

Referenced by ScriptBindings::RegisterBindings(), and MainFrame::RunStartupScripts().

◆ RegisterScriptPlugin()

bool ScriptingManager::RegisterScriptPlugin ( const wxString name,
const wxArrayInt ids 
)

Registers a script plugin menu IDs with the callback function.

Parameters
nameThe script plugin's name.
idsThe menu IDs to bind.
Returns
True on success, false on failure.

Definition at line 273 of file scriptingmanager.cpp.

References Manager::Get(), Manager::GetAppWindow(), m_AttachedToMainWindow, and OnScriptPluginMenu().

Referenced by ScriptBindings::ScriptPluginWrapper::RegisterPlugin().

◆ RemoveTrust()

bool ScriptingManager::RemoveTrust ( const wxString script)

Remove a script trust.

Returns
True if the trust existed and was removed, false if not.

Definition at line 386 of file scriptingmanager.cpp.

References m_TrustedScripts.

Referenced by ScriptingSettingsDlg::OnDeleteTrust().

◆ TrustCurrentlyRunningScript()

void ScriptingManager::TrustCurrentlyRunningScript ( bool  permanently)

Security function to trust a script.

Parameters
permanentlyIf true, this script will be trusted on a permanent basis. If false, it will be trusted for this session only.
See also
TrustScript()

Definition at line 381 of file scriptingmanager.cpp.

References m_CurrentlyRunningScriptFile, and TrustScript().

Referenced by ScriptBindings::IOLib::SecurityAllows().

◆ TrustScript()

void ScriptingManager::TrustScript ( const wxString script,
bool  permanently 
)

Security function to trust a script.

Parameters
scriptThe script's full filename.
permanentlyIf true, this script will be trusted on a permanent basis. If false, it will be trusted for this session only.
Note
When a script is marked as trusted, a CRC key is generated from its contents and stored for reference. When the IsScriptTrusted() function is called later on, besides checking the script's filename against the trusted scripts database, the CRC is checked too. If the CRC doesn't match, the script is not trusted anymore (the user is notified too).
See also
TrustCurrentlyRunningScript()

Definition at line 363 of file scriptingmanager.cpp.

References ScriptingManager::TrustedScriptProps::crc, wxCrc32::FromFile(), m_TrustedScripts, and ScriptingManager::TrustedScriptProps::permanent.

Referenced by TrustCurrentlyRunningScript().

◆ UnRegisterAllScriptMenus()

bool ScriptingManager::UnRegisterAllScriptMenus ( )

Unregister all scripts' menu items.

Returns
True on success, false on failure.

Definition at line 336 of file scriptingmanager.cpp.

References MenuItemsManager::Clear(), and m_MenuItemsManager.

◆ UnRegisterScriptMenu()

bool ScriptingManager::UnRegisterScriptMenu ( const wxString menuPath)

Script-bound function to unregister a script's menu item.

Parameters
menuPathThe full menu path to unregister.
Returns
True on success, false on failure.

Definition at line 329 of file scriptingmanager.cpp.

References _T, LogManager::DebugLog(), Manager::Get(), and Manager::GetLogManager().

Friends And Related Function Documentation

◆ Mgr< ScriptingManager >

friend class Mgr< ScriptingManager >
friend

Definition at line 52 of file scriptingmanager.h.

Member Data Documentation

◆ cs

wxCriticalSection ScriptingManager::cs
private

Definition at line 53 of file scriptingmanager.h.

◆ m_AttachedToMainWindow

bool ScriptingManager::m_AttachedToMainWindow
private

Definition at line 247 of file scriptingmanager.h.

Referenced by RegisterScriptMenu(), and RegisterScriptPlugin().

◆ m_CurrentlyRunningScriptFile

wxString ScriptingManager::m_CurrentlyRunningScriptFile
private

◆ m_IncludeSet

IncludeSet ScriptingManager::m_IncludeSet
private

Definition at line 251 of file scriptingmanager.h.

Referenced by LoadBuffer().

◆ m_MenuIDToScript

MenuIDToScript ScriptingManager::m_MenuIDToScript
private

Definition at line 245 of file scriptingmanager.h.

Referenced by OnScriptMenu(), and RegisterScriptMenu().

◆ m_MenuItemsManager

MenuItemsManager ScriptingManager::m_MenuItemsManager
private

Definition at line 253 of file scriptingmanager.h.

Referenced by RegisterScriptMenu(), and UnRegisterAllScriptMenus().

◆ m_TrustedScripts

TrustedScripts ScriptingManager::m_TrustedScripts
private

The documentation for this class was generated from the following files: