Code::Blocks  SVN r11506
sc_dialog.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: 11399 $
6  * $Id: sc_dialog.cpp 11399 2018-05-08 21:54:03Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/scripting/bindings/sc_dialog.cpp $
8  */
9 
10 #include <sdk_precomp.h>
11 
12 #ifndef CB_PRECOMP
13  #include <globals.h>
14  #include <cbexception.h>
15  #include <manager.h>
16  #include <configmanager.h>
17  #include <logmanager.h>
18  #include <wx/string.h>
19  #include "scrollingdialog.h"
20 #endif
21 
22 #include <wx/xrc/xmlres.h>
23 
24 #include "sc_base_types.h"
25 
26 namespace ScriptBindings
27 {
29  {
30  DECLARE_EVENT_TABLE()
32  public:
33  XrcDialog(wxWindow* parent, const wxString& dlgName, const wxString& callback)
34  : m_CallBack(callback)
35  {
36  // first try to load dlgName as wxDialog, if that does not work, try to load it as wxScrollingDialog
37  // if both does not work, throw an exception
38  if ( !wxXmlResource::Get()->LoadDialog(this, parent, dlgName)
39  && !wxXmlResource::Get()->LoadObject(this, parent, dlgName,_T("wxScrollingDialog")) )
40  {
42  }
43  }
44  ~XrcDialog() override{ }
45  void OnButton(wxCommandEvent& event);
46  };
47 
48  XrcDialog* s_ActiveDialog = nullptr;
49 
50  BEGIN_EVENT_TABLE(XrcDialog, wxScrollingDialog)
51  EVT_CHOICE(-1, XrcDialog::OnButton)
52  EVT_COMBOBOX(-1, XrcDialog::OnButton)
53  EVT_CHECKBOX(-1, XrcDialog::OnButton)
54  EVT_LISTBOX(-1, XrcDialog::OnButton)
55  EVT_RADIOBOX(-1, XrcDialog::OnButton)
56  EVT_BUTTON(-1, XrcDialog::OnButton)
57  END_EVENT_TABLE()
58 
60  {
61  // VERY important, or else the dialog will not be dismissed with
62  // standard event IDs like wxID_OK/wxID_CANCEL/etc.
63  event.Skip(true);
64 
65  try
66  {
67 // Manager::Get()->GetLogManager()->DebugLog(F(_T("Script dialog event: %d"), event.GetId()));
68  SqPlus::SquirrelFunction<void> cb(cbU2C(m_CallBack));
69  if (cb.func.IsNull())
70  return;
71  cb(event.GetId());
72  }
73  catch (SquirrelError& e)
74  {
76  }
77  }
78 
79  int ShowDialog(const wxString& xrc, const wxString& dlgName, const wxString& callback)
80  {
82 // Manager::Get()->GetLogManager()->DebugLog(F(_T("Original parameter is: ") + xrc));
83  Manager::Get()->GetLogManager()->DebugLog(_T("Loading XRC: ") + actual);
84  if (wxXmlResource::Get()->Load(actual))
85  {
87  try
88  {
89  s_ActiveDialog = new XrcDialog(nullptr, dlgName, callback);
90  int ret = s_ActiveDialog->ShowModal();
91  delete s_ActiveDialog;
92  s_ActiveDialog = old;
93  #if wxABI_VERSION > 20601
94  wxXmlResource::Get()->Unload(actual);
95  #endif
96  return ret;
97  }
98  catch (cbException& e)
99  {
100  cbMessageBox(wxString::Format(_("Dialog \"%s\" not found...\n\nActual resource: \"%s\"\nOriginal resource: \"%s\""),
101  dlgName.c_str(),
102  actual.c_str(),
103  xrc.c_str()),
104  _("Error"), wxICON_ERROR);
105  }
106  }
107  else
108  Manager::Get()->GetLogManager()->DebugLog(_T("Loading XRC: '") + actual + _T("' FAILED!"));
109  return -1;
110  }
111 
112  void EndModal(int retCode)
113  {
114  // valid only while in a ShowDialog() call
115  if (s_ActiveDialog)
116  {
117  s_ActiveDialog->EndModal(retCode);
118  return;
119  }
120  cbMessageBox(_("EndModal() only valid while inside a ShowDialog() call..."), _("Error"), wxICON_ERROR);
121  }
122 
123  SQInteger XrcId(HSQUIRRELVM v)
124  {
125  // here we simulate XRCID by using wxWindow::FindWindowByName().
126  // XRCID() doesn't seem to work on its own inside here...
127  StackHandler sa(v);
128  if (!s_ActiveDialog)
129  {
130  cbMessageBox(_("XRCID() only valid while inside a ShowDialog() call..."), _("Error"), wxICON_ERROR);
131  return sa.Return((SQInteger)-1);
132  }
133 
134  wxWindow* win = nullptr;
135  if (sa.GetType(2) == OT_STRING)
136  win = wxWindow::FindWindowByName(cbC2U(sa.GetString(2)), s_ActiveDialog);
137  else
138  win = wxWindow::FindWindowByName(*SqPlus::GetInstance<wxString,false>(v, 2), s_ActiveDialog);
139  return sa.Return((SQInteger)(win ? win->GetId() : -1));
140  }
141 
143  {
144  SqPlus::RegisterGlobal(ShowDialog, "ShowDialog");
145  SqPlus::RegisterGlobal(EndModal, "EndModal");
146  SquirrelVM::CreateFunctionGlobal(XrcId, "XRCID", "*");
147  }
148 } // namespace ScriptBindings
void EndModal(int retCode)
Definition: sc_dialog.cpp:112
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 wxICON_ERROR
Scripts folder in base dir.
Definition: configmanager.h:80
Scripts folder in user&#39;s dir.
Definition: configmanager.h:74
void OnButton(wxCommandEvent &event)
Definition: sc_dialog.cpp:59
wxCStrData c_str() const
#define _T(string)
void Register_Dialog()
Definition: sc_dialog.cpp:142
static wxString LocateDataFile(const wxString &filename, int search_dirs=sdAllKnown)
Locate a file in an installation- and platform-independent way.
DLLIMPORT const wxWX2MBbuf cbU2C(const wxString &str)
Return multibyte (C string) representation of the string.
Definition: globals.cpp:743
DLLIMPORT wxString cbC2U(const char *str)
Return str as a proper unicode-compatible string.
Definition: globals.cpp:733
int ShowDialog(const wxString &xrc, const wxString &dlgName, const wxString &callback)
Definition: sc_dialog.cpp:79
LogManager * GetLogManager() const
Definition: manager.cpp:439
void DisplayErrors(SquirrelError *exception=nullptr, bool clearErrors=true)
Display error dialog.
virtual int ShowModal()
wxString wxEmptyString
const wxString & _(const wxString &string)
XrcDialog * s_ActiveDialog
Definition: sc_dialog.cpp:48
#define cbThrow(message)
Definition: cbexception.h:42
SQInteger XrcId(HSQUIRRELVM v)
Definition: sc_dialog.cpp:123
void DebugLog(const wxString &msg, Logger::level lv=Logger::info)
Definition: logmanager.h:146
bool Unload(const wxString &filename)
static wxXmlResource * Get()
ScriptingManager * GetScriptingManager() const
Definition: manager.cpp:469
XrcDialog(wxWindow *parent, const wxString &dlgName, const wxString &callback)
Definition: sc_dialog.cpp:33
Code::Blocks error handling unit.
Definition: cbexception.h:23
static wxString Format(const wxString &format,...)
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