Code::Blocks  SVN r11506
scriptconsole.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3  * http://www.gnu.org/licenses/gpl-3.0.html
4  *
5  * $Revision: 10260 $
6  * $Id: scriptconsole.cpp 10260 2015-05-15 10:56:23Z jenslody $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/src/scriptconsole.cpp $
8  */
9 
10 #include <sdk.h>
11 #include <sqplus.h>
12 
13 #include "scriptconsole.h"
14 
15 #ifndef CB_PRECOMP
16  #include <globals.h>
17  #include <manager.h>
18  #include <configmanager.h>
19  #include <scriptingmanager.h>
20 #endif
21 
22 #include <wx/filedlg.h>
23 
24 //(*InternalHeaders(ScriptConsole)
25 #include <wx/settings.h>
26 #include <wx/string.h>
27 #include <wx/intl.h>
28 #include <wx/font.h>
29 #include <wx/bitmap.h>
30 #include <wx/image.h>
31 #include <wx/artprov.h>
32 //*)
33 
34 //(*IdInit(ScriptConsole)
41 const long ScriptConsole::ID_PANEL1 = wxNewId();
42 //*)
43 
44 static ScriptConsole* s_Console = nullptr;
45 static SQPRINTFUNCTION s_OldPrintFunc = nullptr;
46 
47 static void ScriptConsolePrintFunc(HSQUIRRELVM /*v*/, const SQChar * s, ...)
48 {
49  static SQChar temp[2048];
50  va_list vl;
51  va_start(vl,s);
52  scvsprintf( temp,s,vl);
53  wxString msg = cbC2U(temp);
54  va_end(vl);
55 
56  if (s_Console)
57  s_Console->Log(msg);
59 }
60 
61 BEGIN_EVENT_TABLE(ScriptConsole,wxPanel)
62  //(*EventTable(ScriptConsole)
63  //*)
64 END_EVENT_TABLE()
65 
67 {
68  //(*Initialize(ScriptConsole)
69  wxBoxSizer* BoxSizer2;
70  wxBoxSizer* BoxSizer1;
71 
72  Create(parent, id, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("id"));
73  BoxSizer1 = new wxBoxSizer(wxVERTICAL);
76  txtConsole->SetFont(txtConsoleFont);
77  BoxSizer1->Add(txtConsole, 1, wxALL|wxEXPAND, 0);
78  Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
79  Panel1->SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE));
80  BoxSizer2 = new wxBoxSizer(wxHORIZONTAL);
81  lblCommand = new wxStaticText(Panel1, ID_STATICTEXT1, _("Command:"), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
82  BoxSizer2->Add(lblCommand, 0, wxALL|wxALIGN_CENTER_VERTICAL, 5);
83  txtCommand = new wxComboBox(Panel1, ID_COMBOBOX1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, 0, wxCB_DROPDOWN|wxTE_PROCESS_ENTER, wxDefaultValidator, _T("ID_COMBOBOX1"));
85  txtCommand->SetFont(txtCommandFont);
86  BoxSizer2->Add(txtCommand, 1, wxALL|wxALIGN_CENTER_VERTICAL, 0);
87  btnExecute = new wxBitmapButton(Panel1, ID_BITMAPBUTTON1, wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_EXECUTABLE_FILE")),wxART_BUTTON), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON1"));
88  btnExecute->SetToolTip(_("Execute current command"));
89  BoxSizer2->Add(btnExecute, 0, wxALIGN_CENTER_VERTICAL, 5);
90  btnLoad = new wxBitmapButton(Panel1, ID_BITMAPBUTTON2, wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_FILE_OPEN")),wxART_BUTTON), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON2"));
91  btnLoad->SetDefault();
92  btnLoad->SetToolTip(_("Load from file"));
93  BoxSizer2->Add(btnLoad, 0, wxALL|wxALIGN_CENTER_VERTICAL, 0);
94  btnClear = new wxBitmapButton(Panel1, ID_BITMAPBUTTON3, wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(_T("wxART_DELETE")),wxART_BUTTON), wxDefaultPosition, wxDefaultSize, wxBU_AUTODRAW, wxDefaultValidator, _T("ID_BITMAPBUTTON3"));
95  btnClear->SetDefault();
96  btnClear->SetToolTip(_("Clear output window"));
97  BoxSizer2->Add(btnClear, 0, wxALL|wxALIGN_CENTER_VERTICAL, 0);
98  Panel1->SetSizer(BoxSizer2);
99  BoxSizer2->Fit(Panel1);
100  BoxSizer2->SetSizeHints(Panel1);
101  BoxSizer1->Add(Panel1, 0, wxALL|wxEXPAND, 0);
102  SetSizer(BoxSizer1);
103  BoxSizer1->Fit(this);
104  BoxSizer1->SetSizeHints(this);
105 
106  Connect(ID_COMBOBOX1,wxEVT_COMMAND_TEXT_ENTER,(wxObjectEventFunction)&ScriptConsole::OnbtnExecuteClick);
107  Connect(ID_BITMAPBUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&ScriptConsole::OnbtnExecuteClick);
108  Connect(ID_BITMAPBUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&ScriptConsole::OnbtnLoadClick);
109  Connect(ID_BITMAPBUTTON3,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&ScriptConsole::OnbtnClearClick);
110  //*)
111 
112  txtCommand->Append(wxEmptyString);
113  if (!s_Console)
114  {
115  s_Console = this;
116  s_OldPrintFunc = sq_getprintfunc(SquirrelVM::GetVMPtr());
117  sq_setprintfunc(SquirrelVM::GetVMPtr(), ScriptConsolePrintFunc);
118  }
119 
120  Log(_("Welcome to the script console!"));
121 }
122 
124 {
125  if (s_Console == this)
126  {
127  s_Console = nullptr;
128  if (SquirrelVM::GetVMPtr())
129  sq_setprintfunc(SquirrelVM::GetVMPtr(), s_OldPrintFunc);
130  }
131 
132  //(*Destroy(ScriptConsole)
133  //*)
134 }
135 
136 void ScriptConsole::Log(const wxString& msg)
137 {
138  txtConsole->AppendText(msg);
139  if (msg.Last() != _T('\n'))
140  txtConsole->AppendText(_T('\n'));
141 // txtConsole->ScrollLines(-1);
143 }
144 
146 {
147  wxString cmd = txtCommand->GetValue();
148  cmd.Trim(false);
149  cmd.Trim(true);
150  if (cmd.IsEmpty())
151  {
152  wxBell();
153  return;
154  }
155 
156  Log(_T("> ") + cmd);
157  if (Manager::Get()->GetScriptingManager()->LoadBuffer(cmd, _T("ScriptConsole")))
158  {
159  if (txtCommand->FindString(cmd) == wxNOT_FOUND)
160  txtCommand->Insert(cmd, 1); // right after the blank entry
161  txtCommand->SetValue(wxEmptyString);
162  }
163  else
164  txtConsole->AppendText(Manager::Get()->GetScriptingManager()->GetErrorString());
165  txtCommand->SetFocus();
166 }
167 
169 {
170  ConfigManager* mgr = Manager::Get()->GetConfigManager(_T("app"));
171  wxString path = mgr->Read(_T("/file_dialogs/file_run_script/directory"), wxEmptyString);
172  wxFileDialog dlg(this,
173  _("Load script"),
174  path,
176  _T("Script files (*.script)|*.script"),
177  wxFD_OPEN | compatibility::wxHideReadonly);
178  if (dlg.ShowModal() == wxID_OK)
179  {
180  mgr->Write(_T("/file_dialogs/file_run_script/directory"), dlg.GetDirectory());
182  Log(_("Script loaded successfully"));
183  else
184  {
185  Log(_("Loading script failed."));
186  txtConsole->AppendText(Manager::Get()->GetScriptingManager()->GetErrorString());
187  }
188  }
189  txtCommand->SetFocus();
190 }
191 
193 {
194  txtConsole->Clear();
195  txtCommand->SetFocus();
196 }
wxSize Fit(wxWindow *window)
static const long ID_PANEL1
Definition: scriptconsole.h:40
void OnbtnLoadClick(wxCommandEvent &event)
virtual int ShowModal()
int wxNewId()
static void ProcessPendingEvents()
Definition: manager.cpp:215
#define wxCB_DROPDOWN
void Log(const wxString &msg)
#define wxTE_READONLY
ConfigManager * GetConfigManager(const wxString &name_space) const
Definition: manager.cpp:474
static const long ID_BITMAPBUTTON1
Definition: scriptconsole.h:37
const wxValidator wxDefaultValidator
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
#define wxTE_MULTILINE
void OnbtnExecuteClick(wxCommandEvent &event)
static const long ID_COMBOBOX1
Definition: scriptconsole.h:36
static const long ID_STATICTEXT1
Definition: scriptconsole.h:35
#define wxTE_PROCESS_ENTER
#define _T(string)
void wxBell()
#define wxHSCROLL
#define wxNOT_FOUND
const long ID_TEXTCTRL1
bool LoadScript(const wxString &filename)
Loads a script.
#define wxBU_AUTODRAW
void Write(const wxString &name, const wxString &value, bool ignoreEmpty=false)
static void ScriptConsolePrintFunc(HSQUIRRELVM, const SQChar *s,...)
DLLIMPORT wxString cbC2U(const char *str)
Return str as a proper unicode-compatible string.
Definition: globals.cpp:733
#define wxTAB_TRAVERSAL
wxSizerItem * Add(wxWindow *window, const wxSizerFlags &flags)
static SQPRINTFUNCTION s_OldPrintFunc
const wxSize wxDefaultSize
SQPRINTFUNCTION sq_getprintfunc(HSQUIRRELVM v)
Definition: sqapi.cpp:1254
const wxPoint wxDefaultPosition
static wxColour GetColour(wxSystemColour index)
wxString Read(const wxString &key, const wxString &defaultVal=wxEmptyString)
wxTextCtrl * txtConsole
Definition: scriptconsole.h:56
wxArtClient wxART_BUTTON
wxComboBox * txtCommand
Definition: scriptconsole.h:53
wxString wxEmptyString
virtual wxString GetPath() const
void InjectScriptOutput(const wxString &output)
Injects script output.
const wxString & _(const wxString &string)
wxString & Trim(bool fromRight=true)
static const long ID_BITMAPBUTTON2
Definition: scriptconsole.h:38
static const long ID_TEXTCTRL1
Definition: scriptconsole.h:34
bool IsEmpty() const
virtual wxString GetDirectory() const
char SQChar
void sq_setprintfunc(HSQUIRRELVM v, SQPRINTFUNCTION printfunc)
Definition: sqapi.cpp:1249
void SetSizeHints(wxWindow *window)
virtual ~ScriptConsole()
static const long ID_BITMAPBUTTON3
Definition: scriptconsole.h:39
static wxBitmap GetBitmap(const wxArtID &id, const wxArtClient &client=wxART_OTHER, const wxSize &size=wxDefaultSize)
wxUniChar Last() const
ScriptingManager * GetScriptingManager() const
Definition: manager.cpp:469
int wxWindowID
void OnbtnClearClick(wxCommandEvent &event)