Code::Blocks  SVN r11506
sc_globals.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 10769 $
6  * $Id: sc_globals.cpp 10769 2016-02-06 14:26:58Z mortenmacfly $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/scripting/bindings/sc_globals.cpp $
8  */
9 
10 #include <sdk_precomp.h>
11 #ifndef CB_PRECOMP
12  #include <wx/string.h>
13  #include <wx/textdlg.h>
14  #include <globals.h>
15  #include <settings.h>
16  #include <manager.h>
17  #include <logmanager.h>
18  #include <configmanager.h>
19  #include <editormanager.h>
20  #include <projectmanager.h>
21  #include <pluginmanager.h>
22 #endif
23 
24 #include "sc_base_types.h"
25 
26 #include <wx/colordlg.h>
27 #include <wx/numdlg.h>
28 #include <infowindow.h>
29 
30 namespace ScriptBindings
31 {
32  // global funcs
33  void gDebugLog(const wxString& msg){ Manager::Get()->GetLogManager()->DebugLog(msg); }
34  void gErrorLog(const wxString& msg){ Manager::Get()->GetLogManager()->LogError(msg); }
35  void gWarningLog(const wxString& msg){ Manager::Get()->GetLogManager()->LogWarning(msg); }
36  void gLog(const wxString& msg){ Manager::Get()->GetLogManager()->Log(msg); }
37  int gMessage(const wxString& msg, const wxString& caption, int buttons){ return cbMessageBox(msg, caption, buttons); }
38  void gShowMessage(const wxString& msg){ cbMessageBox(msg, _("Script message"), wxICON_INFORMATION | wxOK); }
39  void gShowMessageWarn(const wxString& msg){ cbMessageBox(msg, _("Script warning"), wxICON_WARNING | wxOK); }
40  void gShowMessageError(const wxString& msg){ cbMessageBox(msg, _("Script error"), wxICON_ERROR | wxOK); }
41  void gShowMessageInfo(const wxString& msg){ cbMessageBox(msg, _("Script information"), wxICON_INFORMATION | wxOK); }
43 
44  SQInteger IsNull(HSQUIRRELVM v)
45  {
46  StackHandler sa(v);
47  SQUserPointer up = nullptr;
48  sq_getinstanceup(v, 2, &up, nullptr);
49  return sa.Return(up == nullptr);
50  }
51 
53  {
54  return Manager::Get()->GetProjectManager();
55  }
57  {
58  return Manager::Get()->GetEditorManager();
59  }
61  {
62  return Manager::Get()->GetConfigManager(_T("scripts"));
63  }
65  {
66  static CompilerFactory cf; // all its members are static functions anyway
67  return &cf;
68  }
70  {
72  }
74  {
76  }
77  bool InstallPlugin(const wxString& pluginName, bool allUsers, bool confirm)
78  {
79  if (cbMessageBox(_("A script is trying to install a Code::Blocks plugin.\n"
80  "Do you wish to allow this?\n\n") + pluginName,
81  _("Security warning"), wxICON_WARNING | wxYES_NO) == wxID_NO)
82  {
83  return false;
84  }
85  return Manager::Get()->GetPluginManager()->InstallPlugin(pluginName, allUsers, confirm);
86  }
87  int ExecutePlugin(const wxString& pluginName)
88  {
89  return Manager::Get()->GetPluginManager()->ExecutePlugin(pluginName);
90  }
91  int ConfigurePlugin(const wxString& pluginName)
92  {
93  return 0; /* leaving script binding intact for compatibility, but this is factually not implemented at all */
94  }
95  // locate and call a menu from string (e.g. "/Valgrind/Run Valgrind::MemCheck")
96  void CallMenu(const wxString& menuPath)
97  {
98  // this code is partially based on MenuItemsManager::CreateFromString()
99  wxMenuBar* mbar = Manager::Get()->GetAppFrame()->GetMenuBar();
100  wxMenu* menu = nullptr;
101  size_t pos = 0;
102  while (true)
103  {
104  // ignore consecutive slashes
105  while (pos < menuPath.Length() && menuPath.GetChar(pos) == _T('/'))
106  ++pos;
107 
108  // find next slash
109  size_t nextPos = pos;
110  while (nextPos < menuPath.Length() && menuPath.GetChar(++nextPos) != _T('/'))
111  ;
112 
113  wxString current = menuPath.Mid(pos, nextPos - pos);
114  if (current.IsEmpty())
115  break;
116  bool isLast = nextPos >= menuPath.Length();
117  // current holds the current search string
118 
119  if (!menu) // no menu yet? look in menubar
120  {
121  int menuPos = mbar->FindMenu(current);
122  if (menuPos == wxNOT_FOUND)
123  break; // failed
124  else
125  menu = mbar->GetMenu(menuPos);
126  }
127  else
128  {
129  if (isLast)
130  {
131  int id = menu->FindItem(current);
132  if (id != wxNOT_FOUND)
133  {
134  wxCommandEvent evt(wxEVT_COMMAND_MENU_SELECTED, id);
135  #if wxCHECK_VERSION(3, 0, 0)
136  mbar->GetEventHandler()->ProcessEvent(evt);
137  #else
138  if ( !mbar->ProcessEvent(evt) )
139  {
140  wxString msg; msg.Printf(_("Calling the menu '%s' with ID %d failed."), menuPath.wx_str(), id);
141  cbMessageBox(msg, _("Script error"), wxICON_WARNING);
142  }
143  #endif
144  // done
145  }
146  break;
147  }
148  int existing = menu->FindItem(current);
149  if (existing != wxNOT_FOUND)
150  menu = menu->GetMenuItems()[existing]->GetSubMenu();
151  else
152  break; // failed
153  }
154  pos = nextPos; // prepare for next loop
155  }
156  }
157  void Include(const wxString& filename)
158  {
159  getSM()->LoadScript(filename);
160  }
161  SQInteger Require(HSQUIRRELVM v)
162  {
163  StackHandler sa(v);
164  const wxString& filename = *SqPlus::GetInstance<wxString,false>(v, 2);
165  if (!getSM()->LoadScript(filename))
166  {
167  wxString msg = wxString::Format(_("Failed to load required script: %s"), filename.c_str());
168  return sa.ThrowError(cbU2C(msg));
169  }
170  return sa.Return(static_cast<SQInteger>(0));
171  }
172  SQInteger wx_GetColourFromUser(HSQUIRRELVM v)
173  {
174  StackHandler sa(v);
175  const wxColour& c = sa.GetParamCount() == 2 ? *SqPlus::GetInstance<wxColour,false>(v, 2) : *wxBLACK;
176  return SqPlus::ReturnCopy(v, wxGetColourFromUser(Manager::Get()->GetAppWindow(), c));
177  }
178  long wx_GetNumberFromUser(const wxString& message, const wxString& prompt, const wxString& caption, long value)
179  {
180  return wxGetNumberFromUser(message, prompt, caption, value);
181  }
182  wxString wx_GetPasswordFromUser(const wxString& message, const wxString& caption, const wxString& default_value)
183  {
184  return wxGetPasswordFromUser(message, caption, default_value);
185  }
186  wxString wx_GetTextFromUser(const wxString& message, const wxString& caption, const wxString& default_value)
187  {
188  return cbGetTextFromUser(message, caption, default_value);
189  }
190 
191  long wxString_ToLong(wxString const &str)
192  {
193  long value;
194  if(!str.ToLong(&value))
195  return -1;
196  return value;
197  }
198 
199 
201  {
202  // global funcs
203  SqPlus::RegisterGlobal(gLog, "Log");
204  SqPlus::RegisterGlobal(gDebugLog, "LogDebug");
205  SqPlus::RegisterGlobal(gWarningLog, "LogWarning");
206  SqPlus::RegisterGlobal(gErrorLog, "LogError");
207 
208  SqPlus::RegisterGlobal(gMessage, "Message");
209  SqPlus::RegisterGlobal(gShowMessage, "ShowMessage");
210  SqPlus::RegisterGlobal(gShowMessageWarn, "ShowWarning");
211  SqPlus::RegisterGlobal(gShowMessageError, "ShowError");
212  SqPlus::RegisterGlobal(gShowMessageInfo, "ShowInfo");
213  SqPlus::RegisterGlobal(gReplaceMacros, "ReplaceMacros");
214 
215  SqPlus::RegisterGlobal(getPM, "GetProjectManager");
216  SqPlus::RegisterGlobal(getEM, "GetEditorManager");
217  SqPlus::RegisterGlobal(getCM, "GetConfigManager");
218  SqPlus::RegisterGlobal(getUVM, "GetUserVariableManager");
219  SqPlus::RegisterGlobal(getSM, "GetScriptingManager");
220  SqPlus::RegisterGlobal(getCF, "GetCompilerFactory");
221 
222  // from globals.h
223  SqPlus::RegisterGlobal(GetArrayFromString, "GetArrayFromString");
224  SqPlus::RegisterGlobal(GetStringFromArray, "GetStringFromArray");
225  SqPlus::RegisterGlobal(EscapeSpaces, "EscapeSpaces");
226  SqPlus::RegisterGlobal(UnixFilename, "UnixFilename");
227  SqPlus::RegisterGlobal(FileTypeOf, "FileTypeOf");
228  SqPlus::RegisterGlobal(URLEncode, "URLEncode");
229  SqPlus::RegisterGlobal(NotifyMissingFile, "NotifyMissingFile");
230  SqPlus::RegisterGlobal(GetPlatformsFromString, "GetPlatformsFromString");
231  SqPlus::RegisterGlobal(GetStringFromPlatforms, "GetStringFromPlatforms");
232 
233  SqPlus::RegisterGlobal(ConfigManager::GetFolder, "GetFolder");
234  SqPlus::RegisterGlobal(ConfigManager::LocateDataFile, "LocateDataFile");
235 
236  SqPlus::RegisterGlobal(ExecutePlugin, "ExecuteToolPlugin");
237  SqPlus::RegisterGlobal(ConfigurePlugin, "ConfigureToolPlugin");
238  SqPlus::RegisterGlobal(InstallPlugin, "InstallPlugin");
239 
240  SqPlus::RegisterGlobal(CallMenu, "CallMenu");
241 
242  SqPlus::RegisterGlobal(Include, "Include");
243  SquirrelVM::CreateFunctionGlobal(Require, "Require", "*");
244 
245  SqPlus::RegisterGlobal(InfoWindow::Display, "InfoWindow");
246 
247  SquirrelVM::CreateFunctionGlobal(IsNull, "IsNull", "*");
248 
249  // now for some wx globals (utility) functions
250  SqPlus::RegisterGlobal(wxLaunchDefaultBrowser, "wxLaunchDefaultBrowser");
251  SquirrelVM::CreateFunctionGlobal(wx_GetColourFromUser, "wxGetColourFromUser", "*");
252  SqPlus::RegisterGlobal(wx_GetNumberFromUser, "wxGetNumberFromUser");
253  SqPlus::RegisterGlobal(wx_GetPasswordFromUser, "wxGetPasswordFromUser");
254  SqPlus::RegisterGlobal(wx_GetTextFromUser, "wxGetTextFromUser");
255 
256  SqPlus::RegisterGlobal(wxString_ToLong, "wxString_ToLong");
257  }
258 }
DLLIMPORT wxArrayString GetArrayFromString(const wxString &text, const wxString &separator=DEFAULT_ARRAY_SEP, bool trimSpaces=true)
Definition: globals.cpp:134
void gShowMessageWarn(const wxString &msg)
Definition: sc_globals.cpp:39
static void Display(const wxString &title, const wxString &message, unsigned int delay=5000, unsigned int hysteresis=1)
Definition: infowindow.cpp:294
wxString wxGetPasswordFromUser(const wxString &message, const wxString &caption=wxGetPasswordFromUserPromptStr, const wxString &default_value=wxEmptyString, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord, bool centre=true)
wxColour * wxBLACK
static wxString GetFolder(SearchDirs dir)
Access one of Code::Blocks&#39; folders.
PluginManager * GetPluginManager() const
Definition: manager.cpp:444
SQRESULT sq_getinstanceup(HSQUIRRELVM v, SQInteger idx, SQUserPointer *p, SQUserPointer typetag)
Definition: sqapi.cpp:647
#define wxICON_WARNING
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
UserVariableManager * GetUserVariableManager() const
Definition: manager.cpp:464
void gShowMessageInfo(const wxString &msg)
Definition: sc_globals.cpp:41
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
void gShowMessageError(const wxString &msg)
Definition: sc_globals.cpp:40
ScriptingManager * getSM()
Definition: sc_globals.cpp:73
DLLIMPORT wxString GetStringFromPlatforms(int platforms, bool forceSeparate=false)
Return a string representation of a platform / multiple platforms.
Definition: globals.cpp:90
#define wxICON_ERROR
void LogWarning(const wxString &msg, int i=app_log)
Definition: logmanager.h:141
DLLIMPORT wxString cbGetTextFromUser(const wxString &message, const wxString &caption=cbGetTextFromUserPromptStr, const wxString &default_value=wxEmptyString, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord, bool centre=true)
Definition: globals.cpp:1465
SQInteger IsNull(HSQUIRRELVM v)
Definition: sc_globals.cpp:44
CompilerFactory * getCF()
Definition: sc_globals.cpp:64
size_t Length() const
wxCStrData c_str() const
long wxGetNumberFromUser(const wxString &message, const wxString &prompt, const wxString &caption, long value, long min=0, long max=100, wxWindow *parent=NULL, const wxPoint &pos=wxDefaultPosition)
#define _T(string)
int ExecutePlugin(const wxString &pluginName)
int ConfigurePlugin(const wxString &pluginName)
Definition: sc_globals.cpp:91
void gShowMessage(const wxString &msg)
Definition: sc_globals.cpp:38
#define wxYES_NO
DLLIMPORT wxString URLEncode(const wxString &str)
Definition: globals.cpp:832
EditorManager * getEM()
Definition: sc_globals.cpp:56
long wx_GetNumberFromUser(const wxString &message, const wxString &prompt, const wxString &caption, long value)
Definition: sc_globals.cpp:178
DLLIMPORT wxString GetStringFromArray(const wxArrayString &array, const wxString &separator=DEFAULT_ARRAY_SEP, bool SeparatorAtEnd=true)
Definition: globals.cpp:122
DLLIMPORT FileType FileTypeOf(const wxString &filename)
Definition: globals.cpp:285
bool InstallPlugin(const wxString &pluginName, bool allUsers, bool confirm)
Definition: sc_globals.cpp:77
#define wxICON_INFORMATION
wxString wx_GetPasswordFromUser(const wxString &message, const wxString &caption, const wxString &default_value)
Definition: sc_globals.cpp:182
void NotifyMissingFile(const wxString &name)
Definition: globals.h:370
void gDebugLog(const wxString &msg)
Definition: sc_globals.cpp:33
ProjectManager * getPM()
Definition: sc_globals.cpp:52
#define wxNOT_FOUND
wxString wx_GetTextFromUser(const wxString &message, const wxString &caption, const wxString &default_value)
Definition: sc_globals.cpp:186
static wxString LocateDataFile(const wxString &filename, int search_dirs=sdAllKnown)
Locate a file in an installation- and platform-independent way.
void gErrorLog(const wxString &msg)
Definition: sc_globals.cpp:34
EditorManager * GetEditorManager() const
Definition: manager.cpp:434
DLLIMPORT const wxWX2MBbuf cbU2C(const wxString &str)
Return multibyte (C string) representation of the string.
Definition: globals.cpp:743
void LogError(const wxString &msg, int i=app_log)
Definition: logmanager.h:142
ProjectManager * GetProjectManager() const
Functions returning pointers to the respective sub-manager instances.
Definition: manager.cpp:429
SQInteger Require(HSQUIRRELVM v)
Definition: sc_globals.cpp:161
bool LoadScript(const wxString &filename)
Loads a script.
DLLIMPORT wxString UnixFilename(const wxString &filename, wxPathFormat format=wxPATH_NATIVE)
Definition: globals.cpp:228
UserVariableManager * getUVM()
Definition: sc_globals.cpp:69
wxFrame * GetAppFrame() const
Definition: manager.cpp:419
void gWarningLog(const wxString &msg)
Definition: sc_globals.cpp:35
void gLog(const wxString &msg)
Definition: sc_globals.cpp:36
LogManager * GetLogManager() const
Definition: manager.cpp:439
const wxStringCharType * wx_str() const
wxMenu * GetMenu(size_t menuIndex) const
#define wxOK
MacrosManager * GetMacrosManager() const
Definition: manager.cpp:454
const wxString & _(const wxString &string)
int FindMenu(const wxString &title) const
void ReplaceMacros(wxString &buffer, ProjectBuildTarget *target=nullptr, bool subrequest=false)
bool ToLong(long *val, int base=10) const
int gMessage(const wxString &msg, const wxString &caption, int buttons)
Definition: sc_globals.cpp:37
virtual int FindItem(const wxString &itemString) const
wxString gReplaceMacros(const wxString &buffer)
Definition: sc_globals.cpp:42
bool IsEmpty() const
The entry point singleton for working with projects.
void CallMenu(const wxString &menuPath)
Definition: sc_globals.cpp:96
int ExecutePlugin(const wxString &pluginName)
Definition: sc_globals.cpp:87
Provides scripting in Code::Blocks.
void Log(const wxString &msg, int i=app_log, Logger::level lv=Logger::info)
Definition: logmanager.h:140
void DebugLog(const wxString &msg, Logger::level lv=Logger::info)
Definition: logmanager.h:146
ConfigManager * getCM()
Definition: sc_globals.cpp:60
wxColour wxGetColourFromUser(wxWindow *parent, const wxColour &colInit, const wxString &caption=wxEmptyString, wxColourData *data=NULL)
SQInteger wx_GetColourFromUser(HSQUIRRELVM v)
Definition: sc_globals.cpp:172
void Include(const wxString &filename)
Definition: sc_globals.cpp:157
bool InstallPlugin(const wxString &pluginName, bool forAllUsers=true, bool askForConfirmation=true)
DLLIMPORT int GetPlatformsFromString(const wxString &platforms)
Return an integer representation of a platform string.
Definition: globals.cpp:73
wxUniChar GetChar(size_t n) const
bool wxLaunchDefaultBrowser(const wxString &url, int flags=0)
ScriptingManager * GetScriptingManager() const
Definition: manager.cpp:469
DLLIMPORT wxString EscapeSpaces(const wxString &str)
Escapes spaces and tabs (NOT quoting the string)
Definition: globals.cpp:273
int Printf(const wxString &pszFormat,...)
wxMenuItemList & GetMenuItems()
static wxString Format(const wxString &format,...)
wxString Mid(size_t first, size_t nCount=wxString::npos) const
void Register_Globals()
Definition: sc_globals.cpp:200
long wxString_ToLong(wxString const &str)
Definition: sc_globals.cpp:191
DLLIMPORT int cbMessageBox(const wxString &message, const wxString &caption=wxEmptyString, int style=wxOK, wxWindow *parent=NULL, int x=-1, int y=-1)
wxMessageBox wrapper.
Definition: globals.cpp:1395