#include #include "wx/wxprec.h" #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #include "wx/wx.h" #endif #include "wx/colordlg.h" #include "wx/filedlg.h" #include "wx/splitter.h" #include "wx/treectrl.h" #include "wx/grid.h" #include "wx/tokenzr.h" #include "wx/fontdlg.h" #include "wx/image.h" #include "wx/dnd.h" #include "api.h" enum { WINDOW_TYPE='WX', TOPWIN_TYPE, SIZER_TYPE, SPLITTER_TYPE, GRID_TYPE, TREE_TYPE, NOTEBOOK_TYPE }; #pragma warning(disable : 4100) // Set one event handle for an widget to call Z-Script to handle events class myEvtHandler : public wxEvtHandler { void *_ctx, *_func, *_obj; public: myEvtHandler(void *ctx, void *func, void *obj) : _ctx(ctx), _func(func), _obj(obj) { } ~myEvtHandler() { } void OnCommand(wxCommandEvent& event); void OnKey(wxKeyEvent& event); void OnMouse(wxMouseEvent& event); }; // file drop // main frame class myFrame : public wxFrame { void *_ctx, *_func; std::vector _evth; wxTimer _timer; public: myFrame() : wxFrame(NULL, wxID_ANY, _T("http://www.zegraph.com/z-script/")), _ctx(0), _func(0) { _timer.SetOwner(this, wxID_ANY); DragAcceptFiles(true); } virtual ~myFrame() { if (_ctx && _func) { void *p[1]; p[0] = api_create_string(_ctx, "exit"); api_call_func(_ctx, _func, 1, p); } for (int i = 0; i < _evth.size(); i++) { delete _evth[i]; } } void OnSize(wxSizeEvent& event); void OnTimer(wxTimerEvent& event); void OnPaint(wxPaintEvent& event); void OnDropFile(wxDropFilesEvent& event); void OnDrag(wxCommandEvent& event); wxTimer &timer() { return _timer; } void evth(myEvtHandler *h) { _evth.push_back(h); } void callback(void *ctx, void *func) { _ctx = ctx; _func = func; } void *ctx() { return _ctx; } void *func() { return _func; } private: DECLARE_EVENT_TABLE() }; BEGIN_EVENT_TABLE(myFrame, wxFrame) EVT_SIZE(myFrame::OnSize) EVT_TIMER(wxID_ANY, myFrame::OnTimer) EVT_PAINT(myFrame::OnPaint) EVT_DROP_FILES(myFrame::OnDropFile) END_EVENT_TABLE() // Application startup class myApp : public wxApp { void *_module; public: myApp() : _module(0) { } ~myApp() { if (_module) api_delete_module(_module); } virtual bool OnInit(); virtual int OnExit() { return 0; } }; IMPLEMENT_APP(myApp) bool myApp::OnInit() { myFrame *frame = new myFrame; frame->SetIcon(wxICON(sample)); SetTopWindow(frame); // call Z-Script to create widgets char *s = wxApp::argv[0]; if (argc > 1) s = wxApp::argv[1]; int i, n = strlen(s), k1 = -1, k2 = n; for (i = 0; i < n; i++) { // wxString find('.', true) does not work! if (s[i] == '\\') k1 = i; if (s[i] == '.') k2 = i; } wxString fname(s); fname = fname.SubString(k1+1, k2-1); char err[256]; err[0] = 0; _module = api_parse_file(fname.c_str(), err); if (!_module) { wxMessageBox(_T(err), "Z-Script", wxOK | wxICON_ERROR); return false; } if (api_exec_module(_module, err) <= 0) { wxMessageBox(_T(err), "Z-Script", wxOK | wxICON_ERROR); return false; } frame->Show(true); return true; } class myTreeCtrl : public wxTreeCtrl { void *_ctx, *_func; wxImageList _img; public: myTreeCtrl(wxWindow* parent, void *ctx, void *func) : wxTreeCtrl(parent, wxID_ANY), _ctx(ctx), _func(func) { SetImageList(&_img); } virtual ~myTreeCtrl() { } void OnActivated(wxTreeEvent& event); void OnRightClick(wxTreeEvent& event); void OnBeginDrag(wxTreeEvent& event); void OnEndDrag(wxTreeEvent& event); int addImage(const char *fname); private: DECLARE_EVENT_TABLE() }; BEGIN_EVENT_TABLE(myTreeCtrl, wxTreeCtrl) EVT_TREE_ITEM_ACTIVATED(wxID_ANY, myTreeCtrl::OnActivated) EVT_TREE_ITEM_RIGHT_CLICK(wxID_ANY, myTreeCtrl::OnRightClick) EVT_TREE_BEGIN_DRAG(wxID_ANY, myTreeCtrl::OnBeginDrag) EVT_TREE_END_DRAG(wxID_ANY, myTreeCtrl::OnEndDrag) END_EVENT_TABLE() int myTreeCtrl::addImage(const char *fname) { wxImage img; if (img.LoadFile(fname, wxBITMAP_TYPE_ANY)) { wxBitmap bmp(img); return _img.Add(bmp); } return -1; } //////// Z-Script /////////////////////////////////////////////////// struct zsOptions { const char *name; long code; }; // utility functions long get_options(void *ctx, const char *str, struct zsOptions ops[]) { long flag = 0; int k1 = 0, k2, n = strlen(str); char buf[128]; for (k2 = 1; k2 <= n; k2++) { if (str[k2] == '|' || k2 == n) { strncpy(buf, str+k1, k2-k1); buf[k2-k1] = 0; int i = 0; while (ops[i].name != 0) { if (strcmp(buf, ops[i].name) == 0) { flag |= ops[i].code; break; } i++; } if (ops[i].name == 0) api_runtime_error(ctx, "incorrect option string"); k2++; k1 = k2; } } return flag; } // none-type functions void* wx_message(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxMessageBox(_T(api_get_string(ctx, args[0])), "Z-Script", wxOK, wxGetApp().GetTopWindow()); return 0; } void* wx_frame(void *ctx, int nargs, void** args) { myFrame *frame = (myFrame*)wxGetApp().GetTopWindow(); return api_create_user(ctx, frame, 0, 0, TOPWIN_TYPE); } void* wx_callback(void *ctx, int nargs, void** args) { myFrame *frame = (myFrame*)wxGetApp().GetTopWindow(); frame->callback(ctx, api_get_func(ctx, api_get_string(ctx, args[0]))); return 0; } void* wx_cast(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); return api_create_user(ctx, api_get_ptr(ctx, args[0]), 0, 0, WINDOW_TYPE); } void* wx_timer(void *ctx, int nargs, void** args) { myApp& app = wxGetApp(); myFrame *frame = (myFrame*)app.GetTopWindow(); int ms = api_get_integer(ctx, args[0]); if (ms <= 0) frame->timer().Stop(); else frame->timer().Start(ms); return 0; } void* wx_dirdialog(void *ctx, int nargs, void** args) { const char *path = ""; if (nargs > 0) path = api_get_string(ctx, args[0]); myApp& app = wxGetApp(); wxDirDialog dialog(app.GetTopWindow(), "Z-Script", path); if (dialog.ShowModal() == wxID_OK) { wxString s = dialog.GetPath(); return api_create_string(ctx, s.c_str()); } return 0; } void* wx_filedialog(void *ctx, int nargs, void** args) { const char *path = ""; if (nargs > 0) path = api_get_string(ctx, args[0]); int flag = 0; if (nargs > 1) flag = api_get_integer(ctx, args[1]); myApp& app = wxGetApp(); if (flag != 0) { wxFileDialog dialog(app.GetTopWindow(), "Z-Script", path, "", "*.*", wxSAVE | wxOVERWRITE_PROMPT); if (dialog.ShowModal() == wxID_OK) { wxString s = dialog.GetPath(); return api_create_string(ctx, s.c_str()); } } else { wxFileDialog dialog(app.GetTopWindow(), "Z-Script", path, "", "*.*", wxOPEN | wxMULTIPLE); if (dialog.ShowModal() == wxID_OK) { wxArrayString ss; dialog.GetPaths(ss); if (ss.Count() == 1) { wxString s = ss.Item(0); return api_create_string(ctx, s.c_str()); } void *arr = api_create_array(ctx, ss.Count()); char key[64]; for (unsigned i = 0; i < ss.Count(); i++) { sprintf(key, "%d", i); wxString s = ss.Item(i); api_set_array_object(ctx, arr, key, api_create_string(0, s.c_str())); } return arr; } } return 0; } void* wx_colordialog(void *ctx, int nargs, void** args) { myApp& app = wxGetApp(); wxColourDialog dialog(app.GetTopWindow(), NULL); if (dialog.ShowModal() == wxID_OK) { wxColour color = dialog.GetColourData().GetColour(); void *arr = api_create_array(ctx, 3); api_set_array_object(ctx, arr, "0", api_create_integer(0, color.Red())); api_set_array_object(ctx, arr, "1", api_create_integer(0, color.Green())); api_set_array_object(ctx, arr, "2", api_create_integer(0, color.Blue())); return arr; } return 0; } void* wx_fontdialog(void *ctx, int nargs, void** args) { myApp& app = wxGetApp(); wxFontDialog dialog(app.GetTopWindow()); if (dialog.ShowModal() == wxID_OK) { wxFont font = dialog.GetFontData().GetChosenFont(); void *arr = api_create_array(ctx, 7); api_set_array_object(ctx, arr, "encoding", api_create_integer(0, font.GetDefaultEncoding())); api_set_array_object(ctx, arr, "family", api_create_integer(0, font.GetFamily())); api_set_array_object(ctx, arr, "face", api_create_string(0, font.GetFaceName().c_str())); api_set_array_object(ctx, arr, "size", api_create_integer(0, font.GetPointSize())); api_set_array_object(ctx, arr, "style", api_create_integer(0, font.GetStyle())); api_set_array_object(ctx, arr, "underlined", api_create_integer(0, font.GetUnderlined())); api_set_array_object(ctx, arr, "weight", api_create_integer(0, font.GetWeight())); return arr; } return 0; } void* wx_active(void *ctx, int nargs, void** args) { return api_create_integer(ctx, wxGetApp().IsActive()); } // window function void* win_id(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); return api_create_integer(ctx, w->GetId()); } void* win_close(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); w->Close(true); return 0; } void* win_hwnd(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); return api_create_user(ctx, w->GetHandle(), 0, 0, 0); } void* win_size(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *win = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); if (nargs == 1) { void *arr = api_create_array(ctx, 3); int w, h; win->GetClientSize(&w, &h); api_set_array_object(ctx, arr, "0", api_create_integer(0, w)); api_set_array_object(ctx, arr, "1", api_create_integer(0, h)); return arr; } else { if (nargs < 3) api_input_error(ctx); win->SetClientSize(api_get_integer(ctx, args[1]), api_get_integer(ctx, args[2])); } return 0; } void* win_get(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *win = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); if (win->IsKindOf(CLASSINFO(wxSlider))) { return api_create_integer(ctx, ((wxSlider*)win)->GetValue()); } if (win->IsKindOf(CLASSINFO(wxRadioButton))) { return api_create_integer(ctx, ((wxRadioButton*)win)->GetValue()); } if (win->IsKindOf(CLASSINFO(wxCheckBox))) { return api_create_integer(ctx, ((wxCheckBox*)win)->GetValue()); } if (win->IsKindOf(CLASSINFO(wxRadioBox))) { return api_create_string(ctx, ((wxRadioBox*)win)->GetStringSelection().c_str()); } return api_create_string(ctx, win->GetLabel().c_str()); } void* win_set(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *win = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); if (win->IsKindOf(CLASSINFO(wxSlider))) { ((wxSlider*)win)->SetValue(api_get_integer(ctx, args[1])); return 0; } if (win->IsKindOf(CLASSINFO(wxRadioButton))) { ((wxRadioButton*)win)->SetValue(api_get_integer(ctx, args[1]) != 0); return 0; } if (win->IsKindOf(CLASSINFO(wxCheckBox))) { ((wxCheckBox*)win)->SetValue(api_get_integer(ctx, args[1]) != 0); return 0; } win->SetLabel(api_get_string(ctx, args[1])); return 0; } void* win_dialog(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); long style = wxDEFAULT_DIALOG_STYLE; if (nargs > 1) { zsOptions ops[] = { { "caption", wxCAPTION }, { "resize", wxRESIZE_BORDER }, { "close", wxCLOSE_BOX }, { "minimize", wxMINIMIZE_BOX }, { "maximize", wxMAXIMIZE_BOX }, { "thick", wxTHICK_FRAME }, { "top", wxSTAY_ON_TOP }, { "no3d", wxNO_3D }, { 0, 0 } }; style = get_options(ctx, api_get_string(ctx, args[1]), ops); } wxDialog *dlg = new wxDialog(parent, wxID_ANY, "Z-Script", wxDefaultPosition, wxDefaultSize, style); return api_create_user(ctx, dlg, 0, 0, TOPWIN_TYPE); } void* win_bgcolor(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); if (nargs == 1) { wxColour color = w->GetBackgroundColour(); void *arr = api_create_array(ctx, 3); api_set_array_object(ctx, arr, "0", api_create_integer(0, color.Red())); api_set_array_object(ctx, arr, "1", api_create_integer(0, color.Green())); api_set_array_object(ctx, arr, "2", api_create_integer(0, color.Blue())); return arr; } if (nargs < 4) api_input_error(ctx); int r = api_get_integer(ctx, args[1]); int g = api_get_integer(ctx, args[2]); int b = api_get_integer(ctx, args[3]); wxColour color(r, g, b); w->SetBackgroundColour(color); return 0; } void* win_fgcolor(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); if (nargs == 1) { wxColour color = w->GetForegroundColour(); void *arr = api_create_array(ctx, 3); api_set_array_object(ctx, arr, "0", api_create_integer(0, color.Red())); api_set_array_object(ctx, arr, "1", api_create_integer(0, color.Green())); api_set_array_object(ctx, arr, "2", api_create_integer(0, color.Blue())); return arr; } if (nargs < 4) api_input_error(ctx); int r = api_get_integer(ctx, args[1]); int g = api_get_integer(ctx, args[2]); int b = api_get_integer(ctx, args[3]); wxColour color(r, g, b); w->SetForegroundColour(color); return 0; } void* win_font(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxFont font; if (api_is_integer(args[1])) { wxFont font = w->GetFont(); font.SetPointSize(api_get_integer(ctx, args[1])); w->SetFont(font); return 0; } void *o = api_get_array_object(ctx, args[1], "encoding"); font.SetDefaultEncoding((wxFontEncoding)api_get_integer(ctx, o)); o = api_get_array_object(ctx, args[1], "family"); font.SetFamily(api_get_integer(ctx, o)); o = api_get_array_object(ctx, args[1], "face"); font.SetFaceName(api_get_string(ctx, o)); o = api_get_array_object(ctx, args[1], "size"); font.SetPointSize(api_get_integer(ctx, o)); o = api_get_array_object(ctx, args[1], "style"); font.SetStyle(api_get_integer(ctx, o)); o = api_get_array_object(ctx, args[1], "underlined"); font.SetUnderlined(api_get_integer(ctx, o) != 0); o = api_get_array_object(ctx, args[1], "weight"); font.SetWeight(api_get_integer(ctx, o)); w->SetFont(font); return 0; } void* win_panel(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxPanel *p = new wxPanel(w); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_sizer(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); const char *type = api_get_string(ctx, args[1]); if (strcmp(type, "herbox") == 0) { wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); w->SetSizer(sizer); sizer->SetSizeHints(w); return api_create_user(ctx, sizer, 0, 0, SIZER_TYPE); } else if (strcmp(type, "verbox") == 0) { wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL); w->SetSizer(sizer); sizer->SetSizeHints(w); return api_create_user(ctx, sizer, 0, 0, SIZER_TYPE); } else if (strcmp(type, "herstatic") == 0) { if (nargs < 5) api_input_error(ctx); wxStaticBoxSizer *sizer = new wxStaticBoxSizer(wxHORIZONTAL, w, api_get_string(ctx, args[2])); sizer->SetMinSize(api_get_integer(ctx, args[3]), api_get_integer(ctx, args[4])); w->SetSizer(sizer); sizer->SetSizeHints(w); return api_create_user(ctx, sizer, 0, 0, SIZER_TYPE); } else if (strcmp(type, "verstatic") == 0) { if (nargs < 5) api_input_error(ctx); wxStaticBoxSizer *sizer = new wxStaticBoxSizer(wxVERTICAL, w, api_get_string(ctx, args[2])); sizer->SetMinSize(api_get_integer(ctx, args[3]), api_get_integer(ctx, args[4])); w->SetSizer(sizer); sizer->SetSizeHints(w); return api_create_user(ctx, sizer, 0, 0, SIZER_TYPE); } else if (strcmp(type, "flexgrid") == 0) { if (nargs < 6) api_input_error(ctx); int rows = api_get_integer(ctx, args[2]); int vgap = api_get_integer(ctx, args[3]); int cols = api_get_integer(ctx, args[4]); int hgap = api_get_integer(ctx, args[5]); wxFlexGridSizer *sizer = new wxFlexGridSizer(rows, cols, vgap, hgap); w->SetSizer(sizer); sizer->SetSizeHints(w); return api_create_user(ctx, sizer, 0, 0, SIZER_TYPE); } api_runtime_error(ctx, "unsupported sizer type"); return 0; } void* win_style(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); zsOptions ops[] = { { "simple", wxSIMPLE_BORDER }, { "double", wxDOUBLE_BORDER }, { "sunken", wxSUNKEN_BORDER }, { "raised", wxRAISED_BORDER }, { "static", wxSTATIC_BORDER }, { "none", wxNO_BORDER }, { "vscroll", wxVSCROLL }, { "hscroll", wxHSCROLL }, { "clip", wxCLIP_CHILDREN }, { "transparent", wxTRANSPARENT_WINDOW }, { 0, 0 } }; w->SetWindowStyle(get_options(ctx, api_get_string(ctx, args[1]), ops)); return 0; } void* win_button(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); const char *s = api_get_string(ctx, args[1]); wxButton *p = new wxButton(w, wxID_ANY, _T(s)); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_checkbox(void *ctx, int nargs, void** args) { if (nargs < 3) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); const char *s = api_get_string(ctx, args[1]); int flag = api_get_integer(ctx, args[2]); wxCheckBox *p = new wxCheckBox(w, wxID_ANY, _T(s)); if (flag != 0) p->SetValue(true); else p->SetValue(false); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_combobox(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxComboBox *p = new wxComboBox(parent, wxID_ANY); int n = api_get_array_size(ctx, args[1]); for (int i = 0; i < n; i++) { const char *s = api_get_string(ctx, api_get_array_object2(ctx, args[1], i)); p->Append(wxString(_T(s))); } int sel = 0; if (nargs > 2) sel = api_get_integer(ctx, args[2]); p->Select(sel); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_radiobox(void *ctx, int nargs, void** args) { if (nargs < 5) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); const char *title = api_get_string(ctx, args[1]); long style = wxRA_SPECIFY_COLS; const char *flag = api_get_string(ctx, args[2]); if (flag[0] == 'r' || flag[0] == 'R') style = wxRA_SPECIFY_ROWS; wxArrayString choices; for (int i = 3; i < nargs; i++) choices.Add(_T(api_get_string(ctx, args[i]))); wxRadioBox *p = new wxRadioBox(parent, wxID_ANY, _T(title), wxDefaultPosition, wxDefaultSize, choices, 0, style); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_radiobutton(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); const char *caption = api_get_string(ctx, args[1]); long style = wxRB_SINGLE; if (nargs > 2 && api_get_integer(ctx, args[2]) != 0) style = wxRB_GROUP; wxRadioButton *p = new wxRadioButton(parent, wxID_ANY, _T(caption), wxDefaultPosition, wxDefaultSize, style); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_slider(void *ctx, int nargs, void** args) { if (nargs < 5) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); const char *s = api_get_string(ctx, args[1]); long style = wxSL_LABELS|wxSL_AUTOTICKS; if (strcmp(s, "left") == 0) style |= wxSL_LEFT|wxSL_INVERSE; else if (strcmp(s, "right") == 0) style |= wxSL_RIGHT|wxSL_INVERSE; else if (strcmp(s, "top") == 0) style |= wxSL_TOP; else style |= wxSL_BOTTOM; int vmin = api_get_integer(ctx, args[2]); int vmax = api_get_integer(ctx, args[3]); int value = api_get_integer(ctx, args[4]); int freq = (vmax-vmin) / 10; if (nargs > 5) freq = api_get_integer(ctx, args[5]); wxSlider *p = new wxSlider(parent, wxID_ANY, value, vmin, vmax, wxDefaultPosition, wxDefaultSize, style); p->SetTickFreq(freq, 0); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_statictext(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); const char *s = api_get_string(ctx, args[1]); long style = wxALIGN_LEFT; if (nargs > 2) { int flag = api_get_integer(ctx, args[2]); if (flag > 0) style = wxALIGN_RIGHT; if (flag == 0) style = wxALIGN_CENTRE; } wxStaticText *p = new wxStaticText(parent, wxID_ANY, _T(s), wxDefaultPosition, wxDefaultSize, style); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_textbox(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxString value(_T(api_get_string(ctx, args[1]))); long style = 0; wxSize size = wxDefaultSize; if (nargs > 2) { if (api_is_string(args[2])) { zsOptions ops[] = { { "multiline", wxTE_MULTILINE }, { "password", wxTE_PASSWORD }, { "readonly", wxTE_READONLY }, { "left", wxTE_LEFT }, { "center", wxTE_CENTRE }, { "right", wxTE_RIGHT }, { 0, 0 } }; style = get_options(ctx, api_get_string(ctx, args[2]), ops); if (nargs > 3) { size.SetWidth(api_get_integer(ctx, args[3])); if (nargs > 4) size.SetHeight(api_get_integer(ctx, args[4])); } } else { size.SetWidth(api_get_integer(ctx, args[2])); if (nargs > 3) size.SetHeight(api_get_integer(ctx, args[3])); } } wxTextCtrl *p = new wxTextCtrl(parent, wxID_ANY, value, wxDefaultPosition, size, style); return api_create_user(ctx, p, 0, 0, WINDOW_TYPE); } void* win_hsplitter(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxSplitterWindow *p = new wxSplitterWindow(parent, wxID_ANY); wxPanel *w1 = new wxPanel(p, wxID_ANY); wxPanel *w2 = new wxPanel(p, wxID_ANY); int pos = 0; if (nargs > 1) pos = api_get_integer(ctx, args[1]); p->SplitHorizontally(w1, w2, pos); return api_create_user(ctx, p, 0, 0, SPLITTER_TYPE); } void* win_vsplitter(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxSplitterWindow *p = new wxSplitterWindow(parent, wxID_ANY); wxPanel *w1 = new wxPanel(p, wxID_ANY); wxPanel *w2 = new wxPanel(p, wxID_ANY); int pos = 0; if (nargs > 1) pos = api_get_integer(ctx, args[1]); p->SplitVertically(w1, w2, pos); return api_create_user(ctx, p, 0, 0, SPLITTER_TYPE); } void* win_grid(void *ctx, int nargs, void** args) { if (nargs < 3) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxGrid *p = new wxGrid(parent, wxID_ANY); p->CreateGrid(api_get_integer(ctx, args[1]), api_get_integer(ctx, args[2])); return api_create_user(ctx, p, 0, 0, GRID_TYPE); } void* win_treeview(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); void *func = 0; if (nargs > 1) func = api_get_func(ctx, api_get_string(ctx, args[1])); return api_create_user(ctx, new myTreeCtrl(parent, ctx, func), 0, 0, TREE_TYPE); } void* win_notebook(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *parent = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); zsOptions ops[] = { { "top", wxNB_TOP }, { "left", wxNB_LEFT }, { "rigth", wxNB_RIGHT }, { "bottom", wxNB_BOTTOM }, { 0, 0 } }; long style = wxNB_TOP; if (nargs > 1) style = get_options(ctx, api_get_string(ctx, args[1]), ops); wxNotebook *p = new wxNotebook(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, style); return api_create_user(ctx, p, 0, 0, NOTEBOOK_TYPE); } void* win_bitmap(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *win = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxImage img; if (!img.LoadFile(api_get_string(ctx, args[1]), wxBITMAP_TYPE_ANY)) api_runtime_error(ctx, "image type unsupported"); wxClientDC dc(win); int logicalFunc = wxAND; if (nargs > 2 && api_get_integer(ctx, args[2]) == 0) logicalFunc = wxCOPY; wxCoord xdest=0, ydest=0, width, height; if (nargs > 4) { xdest = api_get_integer(ctx, args[2]); ydest = api_get_integer(ctx, args[3]); width = img.GetWidth(); height = img.GetHeight(); } else { dc.GetSize(&width, &height); img.Rescale(width, height); } wxBitmap bmp(img); wxMemoryDC mdc; mdc.SelectObject(bmp); dc.BeginDrawing(); dc.Blit(xdest, ydest, width, height, &mdc, 0, 0, logicalFunc, true); dc.EndDrawing(); return 0; } void* win_label(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *win = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); wxString s(_T(api_get_string(ctx, args[1]))); win->SetLabel(s); return 0; } void* win_user(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *win = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); if (nargs > 1) { win->SetClientData(args[1]); } else { if (win->GetClientData() != NULL) return api_create_user(ctx, win->GetClientData(), 0, 0, 0); } return 0; } void* win_cursor(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxWindow *win = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); const char *flag = api_get_string(ctx, args[1]); if (strcmp(flag, "arrow") == 0) { wxCursor cursor(wxCURSOR_ARROW); win->SetCursor(cursor); } else if (strcmp(flag, "qarrow") == 0) { wxCursor cursor(wxCURSOR_QUESTION_ARROW); win->SetCursor(cursor); } else if (strcmp(flag, "hand") == 0) { wxCursor cursor(wxCURSOR_HAND); win->SetCursor(cursor); } else if (strcmp(flag, "ibeam") == 0) { wxCursor cursor(wxCURSOR_IBEAM); win->SetCursor(cursor); } else if (strcmp(flag, "noentry") == 0) { wxCursor cursor(wxCURSOR_NO_ENTRY); win->SetCursor(cursor); } else if (strcmp(flag, "wait") == 0) { wxCursor cursor(wxCURSOR_WAIT); win->SetCursor(cursor); } else { wxImage img(flag); wxCursor cursor(img); win->SetCursor(cursor); } return 0; } // frame/dialog functions void* topwin_title(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxTopLevelWindow *frame = (wxTopLevelWindow*)api_get_user(ctx, args[0], TOPWIN_TYPE); frame->SetTitle(_T(api_get_string(ctx, args[1]))); return 0; } void* topwin_show(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxTopLevelWindow *top = (wxDialog*)api_get_user(ctx, args[0], TOPWIN_TYPE); wxDialog dialog; if (!top->IsKindOf(dialog.GetClassInfo())) api_runtime_error(ctx, "not a diaolog window object"); wxDialog *w = (wxDialog*)top; if (api_get_integer(ctx, args[1]) != 0) w->ShowModal(); else w->Show(); return 0; } void* topwin_icon(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxTopLevelWindow *frame = (wxTopLevelWindow*)api_get_user(ctx, args[0], TOPWIN_TYPE); wxString fname(_T(api_get_string(ctx, args[1]))); wxString ext = fname.Right(4); ext = ext.Upper(); wxIcon ico; bool ok = false; if (ext.Cmp(".ICO") == 0) { ok = ico.LoadFile(fname, wxBITMAP_TYPE_ICO); } else if (ext.Cmp(".GIF") == 0) { ok = ico.LoadFile(fname, wxBITMAP_TYPE_GIF); } else if (ext.Cmp(".XPM") == 0) { ok = ico.LoadFile(fname, wxBITMAP_TYPE_XPM); } else if (ext.Cmp(".XBM") == 0) { ok = ico.LoadFile(fname, wxBITMAP_TYPE_XBM); } else { api_runtime_error(ctx, "unsupported icon type"); } if (ok) frame->SetIcon(ico); return 0; } void* topwin_fullscreen(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxTopLevelWindow *w = (wxTopLevelWindow*)api_get_user(ctx, args[0], TOPWIN_TYPE); if (api_get_integer(ctx, args[1]) != 0) w->ShowFullScreen(true); else w->ShowFullScreen(false); return 0; } void* topwin_menubar(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxFrame *frame = (wxFrame*)api_get_user(ctx, args[0], TOPWIN_TYPE); wxMenuBar *menuBar = frame->GetMenuBar(); if (!menuBar) { menuBar = new wxMenuBar(); frame->SetMenuBar(menuBar); } wxMenu *menu = new wxMenu; menuBar->Append(menu, _T(api_get_string(ctx, args[1]))); void *ret = api_create_array(ctx, nargs-2); wxMenu *subm = 0; for (int i = 2, j = 0; i < nargs; i++) { const char* s = api_get_string(ctx, args[i]); if (s[0] == '>') { if (i == nargs-1 || subm) api_input_error(ctx); subm = new wxMenu; menu->Append(wxID_ANY, _T(api_get_string(ctx, args[i+1])), subm); i++; continue; } if (s[0] == '<') { if (!subm) api_input_error(ctx); subm = 0; continue; } if (s[0] == '_') { if (subm) subm->AppendSeparator(); else menu->AppendSeparator(); continue; } long id = wxNewId(); api_set_array_object2(ctx, ret, j++, api_create_integer(0, id)); wxRegisterId(id); if (subm) subm->Append(id, _T(s)); else menu->Append(id, _T(s)); } return ret; } void* topwin_toolbar(void *ctx, int nargs, void** args) { if (nargs < 3) api_input_error(ctx); wxFrame *frame = (wxFrame*)api_get_user(ctx, args[0], TOPWIN_TYPE); frame->SetToolBar(NULL); wxToolBarBase *toolBar = frame->CreateToolBar(); void *arr = api_create_array(ctx, nargs/2); for (int i = 2, j = 0; i < nargs; i += 2) { wxString fname (_T(api_get_string(ctx, args[i-1]))); const char* tip = api_get_string(ctx, args[i ]); if (fname.Cmp("|") == 0) { toolBar->AddSeparator(); continue; } wxImage img; if (!img.LoadFile(api_get_string(ctx, args[i-1]), wxBITMAP_TYPE_ANY)) api_runtime_error(ctx, "unsupported image type"); wxBitmap bmp(img); long id = wxNewId(); api_set_array_object2(ctx, arr, j++, api_create_integer(0, id)); wxRegisterId(id); toolBar->AddTool(id, "", bmp, tip); } toolBar->Realize(); return arr; } void* topwin_statusbar(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxFrame *frame = (wxFrame*)api_get_user(ctx, args[0], TOPWIN_TYPE); int n = api_get_integer(ctx, args[1]); if (nargs == 2) { frame->SetStatusBar(NULL); frame->CreateStatusBar(n); } else { frame->SetStatusText(_T(api_get_string(ctx, args[2])), n); } return 0; } void* topwin_transparent(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxWindow *win = (wxWindow*)api_get_user(ctx, args[0], TOPWIN_TYPE); int alpha = 0, R = 255, G = 255, B = 255; if (nargs > 1) alpha = api_get_integer(ctx, args[1]); if (nargs > 4) { R = api_get_integer(ctx, args[2]); G = api_get_integer(ctx, args[3]); B = api_get_integer(ctx, args[4]); } // OS dependent HWND hwnd = (HWND)win->GetHandle(); //win->GetHWND(); // #define WS_EX_LAYERED 0x80000 #define LWA_COLORKEY 1 #define LWA_ALPHA 2 SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE)|WS_EX_LAYERED); typedef DWORD (WINAPI *PSLWA)(HWND, DWORD, BYTE, DWORD); HMODULE hDLL = LoadLibrary ("user32"); PSLWA pSetLayeredWindowAttributes = (PSLWA)GetProcAddress(hDLL,"SetLayeredWindowAttributes"); if (pSetLayeredWindowAttributes != NULL) { pSetLayeredWindowAttributes(hwnd, RGB(R, G, B), alpha, LWA_COLORKEY|LWA_ALPHA); } return 0; } // splitter void* splitter_panel1(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxSplitterWindow *s = (wxSplitterWindow*)api_get_user(ctx, args[0], SPLITTER_TYPE); return api_create_user(ctx, s->GetWindow1(), 0, 0, WINDOW_TYPE); } void* splitter_panel2(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxSplitterWindow *s = (wxSplitterWindow*)api_get_user(ctx, args[0], SPLITTER_TYPE); return api_create_user(ctx, s->GetWindow2(), 0, 0, WINDOW_TYPE); } void* splitter_replace1(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxSplitterWindow *s = (wxSplitterWindow*)api_get_user(ctx, args[0], SPLITTER_TYPE); wxWindow *w = (wxWindow*)api_get_user(ctx, args[1], WINDOW_TYPE); s->ReplaceWindow(s->GetWindow1(), w); return 0; } void* splitter_replace2(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxSplitterWindow *s = (wxSplitterWindow*)api_get_user(ctx, args[0], SPLITTER_TYPE); wxWindow *w = (wxWindow*)api_get_user(ctx, args[1], WINDOW_TYPE); s->ReplaceWindow(s->GetWindow2(), w); return 0; } void* splitter_min(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxSplitterWindow *s = (wxSplitterWindow*)api_get_user(ctx, args[0], SPLITTER_TYPE); int size = api_get_integer(ctx, args[1]); s->SetMinimumPaneSize(size); return 0; } // grid control void* grid_get(void *ctx, int nargs, void** args) { if (nargs < 3) api_input_error(ctx); wxGrid *w = (wxGrid*)api_get_user(ctx, args[0], GRID_TYPE); wxString s = w->GetCellValue(api_get_integer(ctx, args[1]), api_get_integer(ctx, args[2])); return api_create_string(ctx, s.c_str()); } void* grid_set(void *ctx, int nargs, void** args) { if (nargs < 4) api_input_error(ctx); wxGrid *w = (wxGrid*)api_get_user(ctx, args[0], GRID_TYPE); w->SetCellValue(api_get_integer(ctx, args[1]), api_get_integer(ctx, args[2]), api_get_string(ctx, args[3])); return 0; } void* grid_label(void *ctx, int nargs, void** args) { if (nargs < 3) api_input_error(ctx); wxGrid *w = (wxGrid*)api_get_user(ctx, args[0], GRID_TYPE); w->SetColLabelValue(api_get_integer(ctx, args[1]), api_get_string(ctx, args[2])); return 0; } // tree control void* tree_root(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); myTreeCtrl *w = (myTreeCtrl*)api_get_user(ctx, args[0], TREE_TYPE); int idx1 = -1; int idx2 = -1; if (nargs > 2) idx1 = w->addImage(api_get_string(ctx, args[2])); if (nargs > 3) idx2 = w->addImage(api_get_string(ctx, args[3])); w->AddRoot(api_get_string(ctx, args[1]), idx1, idx2); return 0; } void* tree_append(void *ctx, int nargs, void** args) { if (nargs < 3) api_input_error(ctx); myTreeCtrl *w = (myTreeCtrl*)api_get_user(ctx, args[0], TREE_TYPE); wxStringTokenizer tkz(_T(api_get_string(ctx, args[1])), _T("/")); wxTreeItemId id = w->GetRootItem(); while ( tkz.HasMoreTokens()) { wxString token = tkz.GetNextToken(); if (token.IsEmpty()) { id = w->GetRootItem(); } else { bool flag = false; wxTreeItemIdValue cookie; wxTreeItemId id2 = w->GetFirstChild(id, cookie); while (id2.IsOk()) { wxString s = w->GetItemText(id2); if (s.Cmp(token) == 0) { flag = true; break; } id2 = w->GetNextChild(id, cookie); } if (!flag) { wxMessageBox(_T("failed to find tree item"), "Z-Script", wxOK | wxICON_ERROR); return 0; } id = id2; } } int idx1 = -1; int idx2 = -1; if (nargs > 3) idx1 = w->addImage(api_get_string(ctx, args[3])); if (nargs > 4) idx2 = w->addImage(api_get_string(ctx, args[4])); w->AppendItem(id, _T(api_get_string(ctx, args[2])), idx1, idx2); return 0; } void* tree_get(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); myTreeCtrl *w = (myTreeCtrl*)api_get_user(ctx, args[0], TREE_TYPE); wxTreeItemId id = w->GetSelection(); if (id.IsOk()) api_create_string(ctx, w->GetItemText(id).c_str()); return 0; } // notebook control void* notebook_add(void *ctx, int nargs, void** args) { if (nargs < 3) api_input_error(ctx); wxNotebook *book = (wxNotebook*)api_get_user(ctx, args[0], NOTEBOOK_TYPE); book->InsertPage(0, (wxWindow*)api_get_ptr(ctx, args[1]), _T(api_get_string(ctx, args[2])), true); return 0; } // sizer long sizer_flags(void *ctx, void* arg) { zsOptions ops[] = { { "top", wxTOP }, { "bottom", wxBOTTOM }, { "left", wxLEFT }, { "right", wxRIGHT }, { "all", wxALL }, { "expand", wxEXPAND }, { "shaped", wxSHAPED }, { "minsize", wxFIXED_MINSIZE }, { "aligncenter", wxALIGN_CENTER }, { "alignvertical", wxALIGN_CENTER_VERTICAL }, { "alignhorizontal", wxALIGN_CENTER_HORIZONTAL }, { "alignleft", wxALIGN_LEFT }, { "alignright", wxALIGN_RIGHT }, { "aligntop", wxALIGN_TOP }, { "alignbottom", wxALIGN_BOTTOM }, { 0, 0 } }; return get_options(ctx, api_get_string(ctx, arg), ops); } void* sizer_add(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxSizer* sizer = (wxSizer*)api_get_user(ctx, args[0], SIZER_TYPE); int flag = 0; int prop = 0; if (api_is_integer(args[1])) { if (nargs < 3) api_input_error(ctx); int w = api_get_integer(ctx, args[1]); int h = api_get_integer(ctx, args[2]); if (nargs > 3) { flag = sizer_flags(ctx, args[3]); if (nargs > 4) prop = api_get_integer(ctx, args[4]); } sizer->Add(w, h, prop, flag); sizer->Layout(); return 0; } if (nargs > 2) { flag = sizer_flags(ctx, args[2]); if (nargs > 3) prop = api_get_integer(ctx, args[3]); } if (api_get_type(args[1]) == SIZER_TYPE) { wxSizer *w = (wxSizer*)api_get_ptr(ctx, args[1]); sizer->Add(w, prop, flag); sizer->Layout(); } else if (api_get_type(args[1]) == WINDOW_TYPE) { wxWindow *w = (wxWindow*)api_get_ptr(ctx, args[1]); sizer->Add(w, prop, flag); sizer->Layout(); } else { api_input_error(ctx); } return 0; } void* sizer_sizer(void *ctx, int nargs, void** args) { if (nargs < 2) api_input_error(ctx); wxSizer *topsizer = (wxSizer*)api_get_user(ctx, args[0], SIZER_TYPE); const char *type = api_get_string(ctx, args[1]); int flag = 0; int prop = 0; if (strcmp(type, "herbox") == 0) { wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL); if (nargs > 2) { flag = sizer_flags(ctx, args[2]); if (nargs > 3) prop = api_get_integer(ctx, args[3]); } topsizer->Add(sizer, prop, flag); topsizer->Layout(); return api_create_user(ctx, sizer, 0, 0, SIZER_TYPE); } else if (strcmp(type, "verbox") == 0) { wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL); if (nargs > 2) { flag = sizer_flags(ctx, args[2]); if (nargs > 3) prop = api_get_integer(ctx, args[3]); } topsizer->Add(sizer, prop, flag); topsizer->Layout(); return api_create_user(ctx, sizer, 0, 0, SIZER_TYPE); } else if (strcmp(type, "flexgrid") == 0) { if (nargs < 6) api_input_error(ctx); int rows = api_get_integer(ctx, args[2]); int vgap = api_get_integer(ctx, args[3]); int cols = api_get_integer(ctx, args[4]); int hgap = api_get_integer(ctx, args[5]); wxFlexGridSizer *sizer = new wxFlexGridSizer(rows, cols, vgap, hgap); if (nargs > 6) { flag = sizer_flags(ctx, args[6]); if (nargs > 7) prop = api_get_integer(ctx, args[7]); } topsizer->Add(sizer, prop, flag); topsizer->Layout(); return api_create_user(ctx, sizer, 0, 0, SIZER_TYPE); } api_runtime_error(ctx, "unsupported sizer type"); return 0; } void* sizer_update(void *ctx, int nargs, void** args) { if (nargs < 1) api_input_error(ctx); wxSizer* sizer = (wxSizer*)api_get_user(ctx, args[0], SIZER_TYPE); return 0; } // the master event handler void* win_push(void *ctx, int nargs, void** args) { if (nargs < 3) api_input_error(ctx); wxWindow *w = (wxWindow*)api_get_user(ctx, args[0], WINDOW_TYPE); void *func = api_get_func(ctx, api_get_string(ctx, args[1])); myEvtHandler* h = new myEvtHandler(ctx, func, args[0]); myFrame *frame = (myFrame*)wxGetApp().GetTopWindow(); frame->evth(h); w->PushEventHandler(h); const char* ev = api_get_string(ctx, args[2]); if (strcmp(ev, "key") == 0) { w->Connect(wxEVT_KEY_UP, (wxObjectEventFunction)(&myEvtHandler::OnKey), NULL, h); return 0; } if (strcmp(ev, "mouse") == 0) { w->Connect(wxEVT_LEFT_UP, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_LEFT_DOWN, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_LEFT_DCLICK, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_RIGHT_UP, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_RIGHT_DOWN, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_RIGHT_DCLICK, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_MIDDLE_UP, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_MIDDLE_DOWN, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_MIDDLE_DCLICK, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_MOTION, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_ENTER_WINDOW, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_LEAVE_WINDOW, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); w->Connect(wxEVT_MOUSEWHEEL, (wxObjectEventFunction)(&myEvtHandler::OnMouse), NULL, h); return 0; } if (nargs < 4) api_input_error(ctx); if (strcmp(ev, "menu") == 0) { if (api_is_integer(args[3])) { w->Connect(api_get_integer(ctx, args[3]), wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)(&myEvtHandler::OnCommand), NULL, h); } else { int i, n = api_get_array_size(ctx, args[3]); for (i = 0; i < n; i++) { void *value = api_get_array_object2(ctx, args[3], i); w->Connect(api_get_integer(ctx, value), wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)(&myEvtHandler::OnCommand), NULL, h); } } return 0; } if (strcmp(ev, "tool") == 0) { if (api_is_integer(args[3])) { w->Connect(api_get_integer(ctx, args[3]), wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)(&myEvtHandler::OnCommand), NULL, h); } else { int i, n = api_get_array_size(ctx, args[3]); for (i = 0; i < n; i++) { void *value = api_get_array_object2(ctx, args[3], i); w->Connect(api_get_integer(ctx, value), wxEVT_COMMAND_TOOL_CLICKED, (wxObjectEventFunction)(&myEvtHandler::OnCommand), NULL, h); } } return 0; } return 0; } // event functions void myEvtHandler::OnCommand(wxCommandEvent& event) { if (_func) { void *p[2] = { api_create_integer(_ctx, event.GetId()), _obj }; api_call_func(_ctx, _func, 2, p); } event.Skip(); } void myEvtHandler::OnKey(wxKeyEvent& event) { if (_func) { void *p[5] = { api_create_integer(_ctx, event.GetKeyCode()), api_create_integer(_ctx, event.ControlDown()), api_create_integer(_ctx, event.AltDown()), api_create_integer(_ctx, event.ShiftDown()), _obj }; api_call_func(_ctx, _func, 5, p); } // event.Skip(); } void myEvtHandler::OnMouse(wxMouseEvent& event) { if (_func) { void *p[7]; WXTYPE type = event.GetEventType(); if (type == wxEVT_LEFT_DOWN) p[0] = api_create_integer(_ctx, 1); else if (type == wxEVT_LEFT_UP) p[0] = api_create_integer(_ctx, 2); else if (type == wxEVT_LEFT_DCLICK) p[0] = api_create_integer(_ctx, 3); else if (type == wxEVT_MIDDLE_DOWN) p[0] = api_create_integer(_ctx, 4); else if (type == wxEVT_MIDDLE_UP) p[0] = api_create_integer(_ctx, 5); else if (type == wxEVT_MIDDLE_DCLICK) p[0] = api_create_integer(_ctx, 6); else if (type == wxEVT_RIGHT_DOWN) p[0] = api_create_integer(_ctx, 7); else if (type == wxEVT_RIGHT_UP) p[0] = api_create_integer(_ctx, 8); else if (type == wxEVT_RIGHT_DCLICK) p[0] = api_create_integer(_ctx, 9); else if (type == wxEVT_MOTION) p[0] = api_create_integer(_ctx, 10); else p[0] = api_create_integer(_ctx, 0); p[1] = api_create_integer(_ctx, event.GetX()); p[2] = api_create_integer(_ctx, event.GetY()); p[3] = api_create_integer(_ctx, event.ControlDown()); p[4] = api_create_integer(_ctx, event.AltDown()); p[5] = api_create_integer(_ctx, event.ShiftDown()); p[6] = _obj; api_call_func(_ctx, _func, 7, p); } // event.Skip(); } void myTreeCtrl::OnActivated(wxTreeEvent& event) { if (_func) { wxTreeItemId id = event.GetItem(); wxString s = GetItemText(id); id = GetItemParent(id); while (id.IsOk()) { s = GetItemText(id) + "/" + s; id = GetItemParent(id); } void *p[2] = { api_create_string(_ctx, "ACTIVATE"), api_create_string(_ctx, s.c_str()) }; api_call_func(_ctx, _func, 2, p); } event.Skip(); } void myTreeCtrl::OnRightClick(wxTreeEvent& event) { if (_func) { wxTreeItemId id = event.GetItem(); wxString s = GetItemText(id); id = GetItemParent(id); while (id.IsOk()) { s = GetItemText(id) + "/" + s; id = GetItemParent(id); } void *p[2] = { api_create_string(_ctx, "RIGHTCLICK"), api_create_string(_ctx, s.c_str()) }; api_call_func(_ctx, _func, 2, p); } event.Skip(); } void myTreeCtrl::OnBeginDrag(wxTreeEvent& event) { if (_func) { wxTreeItemId id = event.GetItem(); wxString s = GetItemText(id); id = GetItemParent(id); while (id.IsOk()) { s = GetItemText(id) + "/" + s; id = GetItemParent(id); } void *p[2] = { api_create_string(_ctx, "RIGHTCLICK"), api_create_string(_ctx, s.c_str()) }; api_call_func(_ctx, _func, 2, p); } event.Skip(); } void myTreeCtrl::OnEndDrag(wxTreeEvent& event) { if (_func) { wxTreeItemId id = event.GetItem(); wxString s = GetItemText(id); id = GetItemParent(id); while (id.IsOk()) { s = GetItemText(id) + "/" + s; id = GetItemParent(id); } void *p[2] = { api_create_string(_ctx, "RIGHTCLICK"), api_create_string(_ctx, s.c_str()) }; api_call_func(_ctx, _func, 2, p); } event.Skip(); } void myFrame::OnSize(wxSizeEvent& event) { if (_func) { int w, h; GetSize(&w, &h); void *p[3] = { api_create_string(_ctx, "size"), api_create_integer(_ctx, w), api_create_integer(_ctx, h) }; api_call_func(_ctx, _func, 3, p); } event.Skip(); } void myFrame::OnTimer(wxTimerEvent& event) { if (_func) { void *p[2] = { api_create_string(_ctx, "timer"), api_create_integer(_ctx, timer().GetInterval()) }; api_call_func(_ctx, _func, 2, p); } event.Skip(); } void myFrame::OnDropFile(wxDropFilesEvent& event) { if (_func) { void *p[3] = { api_create_string(_ctx, "dropfile"), api_create_string(_ctx, event.GetFiles()->c_str()), api_create_integer(_ctx, event.GetNumberOfFiles()) }; api_call_func(_ctx, _func, 3, p); } event.Skip(); } void myFrame::OnPaint(wxPaintEvent& event) { if (_func) { wxPaintDC dc(this); void *p[2] = { api_create_string(_ctx, "paint"), api_create_user(_ctx, GetHandle(), 0, 0, 0) }; api_call_func(_ctx, _func, 2, p); } event.Skip(); } ///////////////////////////////////////////////////////////////////// void* shell_exec(void* ctx, int nargs, void **args) { if (nargs < 1) api_input_error(ctx); SHELLEXECUTEINFO shellInfo; ZeroMemory(&shellInfo, sizeof(shellInfo)); shellInfo.cbSize = sizeof(shellInfo); shellInfo.fMask = SEE_MASK_FLAG_NO_UI | SEE_MASK_NOCLOSEPROCESS; shellInfo.nShow = TRUE; shellInfo.lpFile = api_get_string(ctx, args[0]); if (nargs > 1) shellInfo.lpParameters = api_get_string(ctx, args[1]); ShellExecuteEx(&shellInfo); return 0; } class zsRegPrimitive { public: zsRegPrimitive() { api_add_primitive("wxmsg", 0, wx_message); api_add_primitive("wxframe", 0, wx_frame); api_add_primitive("wxcast", 0, wx_cast); api_add_primitive("wxtimer", 0, wx_timer); api_add_primitive("wxexec", 0, shell_exec); api_add_primitive("wxdir", 0, wx_dirdialog); api_add_primitive("wxfile", 0, wx_filedialog); api_add_primitive("wxcolor", 0, wx_colordialog); api_add_primitive("wxfont", 0, wx_fontdialog); api_add_primitive("wxfunc", 0, wx_callback); api_add_primitive("wxactive", 0, wx_active); api_add_primitive("id", WINDOW_TYPE, win_id); api_add_primitive("close", WINDOW_TYPE, win_close); api_add_primitive("hwnd", WINDOW_TYPE, win_hwnd); api_add_primitive("size", WINDOW_TYPE, win_size); api_add_primitive("get", WINDOW_TYPE, win_get); api_add_primitive("set", WINDOW_TYPE, win_set); api_add_primitive("bgcolor", WINDOW_TYPE, win_bgcolor); api_add_primitive("font", WINDOW_TYPE, win_font); api_add_primitive("panel", WINDOW_TYPE, win_panel); api_add_primitive("sizer", WINDOW_TYPE, win_sizer); api_add_primitive("style", WINDOW_TYPE, win_style); api_add_primitive("button", WINDOW_TYPE, win_button); api_add_primitive("checkbox", WINDOW_TYPE, win_checkbox); api_add_primitive("combobox", WINDOW_TYPE, win_combobox); api_add_primitive("radiobox", WINDOW_TYPE, win_radiobox); api_add_primitive("radiobutton",WINDOW_TYPE, win_radiobutton); api_add_primitive("slider", WINDOW_TYPE, win_slider); api_add_primitive("statictext", WINDOW_TYPE, win_statictext); api_add_primitive("textbox", WINDOW_TYPE, win_textbox); api_add_primitive("hsplitter", WINDOW_TYPE, win_hsplitter); api_add_primitive("vsplitter", WINDOW_TYPE, win_vsplitter); api_add_primitive("grid", WINDOW_TYPE, win_grid); api_add_primitive("treeview", WINDOW_TYPE, win_treeview); api_add_primitive("notebook", WINDOW_TYPE, win_notebook); api_add_primitive("bitmap", WINDOW_TYPE, win_bitmap); api_add_primitive("label", WINDOW_TYPE, win_label); api_add_primitive("push", WINDOW_TYPE, win_push); api_add_primitive("user", WINDOW_TYPE, win_user); api_add_primitive("cursor", WINDOW_TYPE, win_cursor); api_add_primitive("title", TOPWIN_TYPE, topwin_title); api_add_primitive("show", TOPWIN_TYPE, topwin_show); api_add_primitive("icon", TOPWIN_TYPE, topwin_icon); api_add_primitive("fullscreen", TOPWIN_TYPE, topwin_fullscreen); api_add_primitive("menubar", TOPWIN_TYPE, topwin_menubar); api_add_primitive("toolbar", TOPWIN_TYPE, topwin_toolbar); api_add_primitive("statusbar", TOPWIN_TYPE, topwin_statusbar); api_add_primitive("transparent",TOPWIN_TYPE, topwin_transparent); api_add_primitive("panel1", SPLITTER_TYPE, splitter_panel1); api_add_primitive("panel2", SPLITTER_TYPE, splitter_panel2); api_add_primitive("replace1", SPLITTER_TYPE, splitter_replace1); api_add_primitive("replace2", SPLITTER_TYPE, splitter_replace2); api_add_primitive("min", SPLITTER_TYPE, splitter_min); api_add_primitive("__get", GRID_TYPE, grid_get); api_add_primitive("__set", GRID_TYPE, grid_set); api_add_primitive("label", GRID_TYPE, grid_label); api_add_primitive("root", TREE_TYPE, tree_root); api_add_primitive("append", TREE_TYPE, tree_append); api_add_primitive("get", TREE_TYPE, tree_get); api_add_primitive("add", NOTEBOOK_TYPE, notebook_add); api_add_primitive("add", SIZER_TYPE, sizer_add); api_add_primitive("sizer", SIZER_TYPE, sizer_sizer); api_add_primitive("update", SIZER_TYPE, sizer_update); } ~zsRegPrimitive() { } }; zsRegPrimitive wx_register;