1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
|
/////////////////////////////////////////////////////////////////////////////
// Name: activex.i
// Purpose: ActiveX controls (such as Internet Explorer) in a wxWindow
//
// Author: Robin Dunn
//
// Created: 18-Mar-2004
// RCS-ID: $Id: activex.i 39219 2006-05-19 01:10:46Z RD $
// Copyright: (c) 2004 by Total Control Software
// Licence: wxWindows license
/////////////////////////////////////////////////////////////////////////////
%module(package="wx") activex
%{
#include "wx/wxPython/wxPython.h"
#include "wx/wxPython/pyclasses.h"
#include "wx/wxPython/pyistream.h"
#include "wxactivex.h"
%}
//---------------------------------------------------------------------------
%import core.i
%pythoncode { wx = _core }
%pythoncode { __docfilter__ = wx.__DocFilter(globals()) }
MAKE_CONST_WXSTRING_NOSWIG(PanelNameStr);
//---------------------------------------------------------------------------
typedef unsigned short USHORT;
typedef long DISPID;
typedef long MEMBERID;
typedef unsigned short VARTYPE;
%{
// Since SWIG doesn't support nested classes, we need to fool it a bit
// and make them look like global classes. These defines make the C++ code
// know what we are doing.
#define wxParamX wxActiveX::ParamX
#define wxFuncX wxActiveX::FuncX
#define wxPropX wxActiveX::PropX
#define wxParamXArray wxActiveX::ParamXArray
#define wxFuncXArray wxActiveX::FuncXArray
#define wxPropXArray wxActiveX::PropXArray
%}
%{
// Some conversion helpers
static wxVariant _PyObj2Variant(PyObject* value);
static bool _PyObj2Variant(PyObject* value, wxVariant& wv);
static PyObject* _Variant2PyObj(wxVariant& value, bool useNone=false);
static wxString _VARTYPEname(VARTYPE vt);
// Check if an exception has been raised (blocking threads)
inline bool wxPyErr_Occurred()
{
bool rval;
wxPyBlock_t blocked = wxPyBeginBlockThreads();
rval = PyErr_Occurred() != NULL;
wxPyEndBlockThreads(blocked);
return rval;
}
%}
//---------------------------------------------------------------------------
%newgroup
DocStr(CLSID,
"This class wraps the Windows CLSID structure and is used to
specify the class of the ActiveX object that is to be created. A
CLSID can be constructed from either a ProgID string, (such as
'WordPad.Document.1') or a classID string, (such as
'{CA8A9783-280D-11CF-A24D-444553540000}').", "");
class CLSID {
public:
%extend {
CLSID(const wxString& id)
{
int result;
CLSID* self = new CLSID;
memset(self, 0, sizeof(CLSID));
if (id[0] == _T('{')) {
// Looks like a classID string
result =
CLSIDFromString(
(LPOLESTR)(const wchar_t *)id.wc_str(wxConvUTF8),
self);
} else {
// Try a progID
result =
CLSIDFromProgID(
(LPOLESTR)(const wchar_t *)id.wc_str(wxConvUTF8),
self);
}
if (result != NOERROR) {
wxPyErr_SetString(PyExc_ValueError, "Not a recognized classID or progID");
delete self;
return NULL;
}
return self;
}
~CLSID() { delete self; }
wxString GetCLSIDString()
{
LPOLESTR s;
wxString str;
if (StringFromCLSID(*self, &s) == S_OK) {
str = s;
CoTaskMemFree(s);
}
else {
str = _T("Error!"); // TODO: raise exception?
}
return str;
}
wxString GetProgIDString()
{
LPOLESTR s;
wxString str;
if (ProgIDFromCLSID(*self, &s) == S_OK) {
str = s;
CoTaskMemFree(s);
}
else {
str = _T("Error!"); // TODO: raise exception?
}
return str;
}
}
%pythoncode { def __str__(self): return self.GetCLSIDString() }
};
//---------------------------------------------------------------------------
%newgroup
%define MAKE_ARRAY_WRAPPER(basetype, arrayname)
class arrayname
{
public:
%extend {
bool __nonzero__() { return self->size() > 0; }
int __len__() { return self->size(); }
const basetype& __getitem__(int idx) {
if ( idx >= 0 && idx < self->size() )
return (*self)[idx];
else {
static basetype BadVal;
wxPyErr_SetString(PyExc_IndexError, "Index out of range");
return BadVal;
}
}
// TODO __iter__??
}
};
%enddef
//---------------------------------------------------------------------------
%immutable;
class wxParamX
{
public:
USHORT flags;
bool isPtr;
bool isSafeArray;
bool isOptional;
VARTYPE vt;
wxString name;
%feature("shadow") vt_type_get "vt_type = property(_activex.ParamX_vt_type_get)";
%extend { wxString vt_type_get() { return _VARTYPEname(self->vt); } }
%feature("shadow") IsIn "isIn = property(_activex.ParamX_IsIn)";
%feature("shadow") IsOut "isOut = property(_activex.ParamX_IsOut)";
%feature("shadow") IsRetVal "isRetVal = property(_activex.ParamX_IsRetVal)";
bool IsIn() const;
bool IsOut() const;
bool IsRetVal() const;
};
class wxFuncX
{
public:
wxString name;
MEMBERID memid;
bool hasOut;
wxParamX retType;
wxParamXArray params;
};
class wxPropX
{
public:
wxString name;
MEMBERID memid;
wxParamX type;
wxParamX arg;
bool putByRef;
%feature("shadow") CanGet "canGet = property(_activex.PropX_CanGet)";
%feature("shadow") CanSet "canSet = property(_activex.PropX_CanSet)";
bool CanGet() const;
bool CanSet() const;
};
%mutable;
MAKE_ARRAY_WRAPPER(wxParamX, wxParamXArray);
MAKE_ARRAY_WRAPPER(wxFuncX, wxFuncXArray);
MAKE_ARRAY_WRAPPER(wxPropX, wxPropXArray);
//---------------------------------------------------------------------------
%newgroup
%{
// C++ version of a Python-aware wxActiveX
class wxActiveXWindow : public wxActiveX
{
private:
CLSID m_CLSID;
DECLARE_ABSTRACT_CLASS(wxActiveXWindow);
public:
wxActiveXWindow( wxWindow* parent, const CLSID& clsId, wxWindowID id = -1,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxPyPanelNameStr)
: wxActiveX(parent, clsId, id, pos, size, style, name)
{
m_CLSID = clsId;
}
const CLSID& GetCLSID() const { return m_CLSID; }
// Renamed versions of some base class methods that delegate
// to the base where appropriate, and raise Python exceptions
// when needed.
int GetAXEventCount() const { return wxActiveX::GetEventCount(); }
int GetAXPropCount() const { return wxActiveX::GetPropCount(); }
int GetAXMethodCount() const { return wxActiveX::GetMethodCount(); }
const wxFuncX& GetAXEventDesc(int idx) const
{
static wxFuncX BadVal;
if (idx < 0 || idx >= GetAXEventCount()) {
wxPyErr_SetString(PyExc_IndexError, "Index out of range");
return BadVal;
}
return m_events[idx];
}
const wxFuncX& GetAXMethodDesc(int idx) const
{
static wxFuncX BadVal;
if (idx < 0 || idx >= GetAXMethodCount()) {
wxPyErr_SetString(PyExc_IndexError, "Index out of range");
return BadVal;
}
return m_methods[idx];
}
const wxPropX& GetAXPropDesc(int idx) const
{
static wxPropX BadVal;
if (idx < 0 || idx >= GetAXPropCount()) {
wxPyErr_SetString(PyExc_IndexError, "Index out of range");
return BadVal;
}
return m_props[idx];
}
const wxFuncX& GetAXMethodDesc(const wxString& name) const
{
NameMap::const_iterator it = m_methodNames.find(name);
if (it == m_methodNames.end()) {
wxString msg;
msg << _T("method <") << name << _T("> not found");
wxPyErr_SetString(PyExc_KeyError, msg.mb_str());
static wxFuncX BadVal;
return BadVal;
};
return GetAXMethodDesc(it->second);
}
const wxPropX& GetAXPropDesc(const wxString& name) const
{
NameMap::const_iterator it = m_propNames.find(name);
if (it == m_propNames.end()) {
wxString msg;
msg << _T("property <") << name << _T("> not found");
wxPyErr_SetString(PyExc_KeyError, msg.mb_str());
static wxPropX BadVal;
return BadVal;
};
return GetAXPropDesc(it->second);
}
// Accessors for the internal vectors of events, methods and
// proprties. Can be used as sequence like objects from
// Python.
const wxFuncXArray& GetAXEvents() { return m_events; }
const wxFuncXArray& GetAXMethods() { return m_methods; }
const wxPropXArray& GetAXProperties() { return m_props; }
// Set a property from a Python object
void SetAXProp(const wxString& name, PyObject* value)
{
const wxPropX& prop = GetAXPropDesc(name);
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (! PyErr_Occurred() ) {
if (! prop.CanSet()) {
wxString msg;
msg << _T("property <") << name << _T("> is readonly");
PyErr_SetString(PyExc_TypeError, msg.mb_str());
goto done;
} else {
wxVariant wxV = _PyObj2Variant(value);
if (PyErr_Occurred())
goto done;
VARIANT v = {prop.arg.vt};
if (!VariantToMSWVariant(wxV, v) || PyErr_Occurred()) {
wxString msg;
msg << _T("Unable to convert value to expected type: (")
<< _VARTYPEname(prop.arg.vt) << _T(") for property <")
<< name << _T(">");
PyErr_SetString(PyExc_TypeError, msg.mb_str());
goto done;
}
PyThreadState* tstate = wxPyBeginAllowThreads();
SetProp(prop.memid, v);
VariantClear(&v);
wxPyEndAllowThreads(tstate);
}
}
done:
wxPyEndBlockThreads(blocked);
}
// Get a property and convert it to a Python object
PyObject* GetAXProp(const wxString& name)
{
PyObject* rval = NULL;
const wxPropX& prop = GetAXPropDesc(name);
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (! PyErr_Occurred() ) {
if (! prop.CanGet()) {
wxString msg;
msg << _T("property <") << name << _T("> is writeonly");
PyErr_SetString(PyExc_TypeError, msg.mb_str());
goto done;
} else {
PyThreadState* tstate = wxPyBeginAllowThreads();
VARIANT v = GetPropAsVariant(prop.memid);
wxPyEndAllowThreads(tstate);
wxVariant wv;
if (!MSWVariantToVariant(v, wv) || PyErr_Occurred()) {
wxString msg;
msg << _T("Unable to convert value to expected type: (")
<< _VARTYPEname(prop.arg.vt) << _T(") for property <")
<< name << _T(">");
PyErr_SetString(PyExc_TypeError, msg.mb_str());
goto done;
}
rval = _Variant2PyObj(wv);
VariantClear(&v);
}
}
done:
wxPyEndBlockThreads(blocked);
return rval;
}
// If both IsIn and isOut are false, assume it is actually an
// input param
bool paramIsIn(const wxParamX& p)
{
return p.IsIn() || (!p.IsIn() && !p.IsOut());
}
// Call a method of the ActiveX object
PyObject* _CallAXMethod(const wxString& name, PyObject* args)
{
VARIANTARG *vargs = NULL;
int nargs = 0;
PyObject* rval = NULL;
const wxFuncX& func = GetAXMethodDesc(name);
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (! PyErr_Occurred() ) {
nargs = func.params.size();
if (nargs > 0)
vargs = new VARIANTARG[nargs];
if (vargs) {
// init type of vargs, in reverse order
int i;
for (i = 0; i < nargs; i++)
vargs[nargs - i - 1].vt = func.params[i].vt;
// Map the args coming from Python to the input parameters in vargs
int pi = 0;
i = 0;
while ( i<nargs && pi<PyTuple_Size(args) ) {
// Move to the next input param.
if (! paramIsIn(func.params[i])) {
i += 1;
continue;
}
// convert the python object
PyObject* obj = PyTuple_GetItem(args, pi);
if (obj == Py_None) // special-case None?
vargs[nargs - i - 1].vt = VT_EMPTY;
else {
wxVariant wxV = _PyObj2Variant(obj);
if (PyErr_Occurred())
goto done;
if (!VariantToMSWVariant(wxV, vargs[nargs - i - 1]) || PyErr_Occurred()) {
wxString msg;
msg << _T("Unable to convert value to expected type: (")
<< _VARTYPEname(vargs[nargs - i - 1].vt)
<< _T(") for parameter ") << i;
PyErr_SetString(PyExc_TypeError, msg.mb_str());
goto done;
}
}
i += 1;
pi += 1;
}
}
// call the method
PyThreadState* tstate = wxPyBeginAllowThreads();
VARIANT rv = CallMethod(func.memid, vargs, nargs);
wxPyEndAllowThreads(tstate);
// Convert the return value and any out-params, ignoring
// conversion errors for now
wxVariant wv;
MSWVariantToVariant(rv, wv);
rval = _Variant2PyObj(wv, true);
VariantClear(&rv);
if (func.hasOut) {
// make a list and put the rval in it if it is not None
PyObject* lst = PyList_New(0);
if (rval != Py_None)
PyList_Append(lst, rval);
else
Py_DECREF(rval);
// find the out params and convert them
for (int i = 0; i < nargs; i++) {
VARIANTARG& va = vargs[nargs - i - 1];
const wxParamX &px = func.params[i];
if (px.IsOut()) {
MSWVariantToVariant(va, wv);
PyObject* obj = _Variant2PyObj(wv, true);
PyList_Append(lst, obj);
}
}
rval = PyList_AsTuple(lst);
Py_DECREF(lst);
}
if (PyErr_Occurred())
PyErr_Clear();
}
done:
wxPyEndBlockThreads(blocked);
if (vargs) {
for (int i = 0; i < nargs; i++)
VariantClear(&vargs[i]);
delete [] vargs;
}
return rval;
}
};
IMPLEMENT_ABSTRACT_CLASS( wxActiveXWindow, wxWindow );
%}
// Now tell SWIG about this new class that is implemented above.
DocStr(wxActiveXWindow,
"ActiveXWindow derives from wxWindow and the constructor accepts a
CLSID for the ActiveX Control that should be created. The
ActiveXWindow class simply adds methods that allow you to query
some of the TypeInfo exposed by the ActiveX object, and also to
get/set properties or call methods by name. The Python
implementation automatically handles converting parameters and
return values to/from the types expected by the ActiveX code as
specified by the TypeInfo.
", "");
MustHaveApp(wxActiveXWindow);
class wxActiveXWindow : public wxWindow
{
public:
%pythonAppend wxActiveXWindow "self._setOORInfo(self)"
DocCtorStr(
wxActiveXWindow( wxWindow* parent, const CLSID& clsId, wxWindowID id = -1,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxPyPanelNameStr),
"Creates an ActiveX control from the clsID given and makes it act
as much like a regular wx.Window as possible.", "");
DocDeclStr(
const CLSID& , GetCLSID() const,
"Return the CLSID used to construct this ActiveX window", "");
DocDeclStr(
int , GetAXEventCount() const,
"Number of events defined for this control", "");
DocDeclStr(
const wxFuncX& , GetAXEventDesc(int idx) const,
"Returns event description by index", "");
DocDeclStr(
int , GetAXPropCount() const,
"Number of properties defined for this control", "");
%nokwargs GetAXPropDesc;
DocStr(GetPropDesc, "Returns property description by index or by name", "");
const wxPropX& GetAXPropDesc(int idx) const;
const wxPropX& GetAXPropDesc(const wxString& name) const;
DocDeclStr(
int , GetAXMethodCount() const,
"Number of methods defined for this control", "");
%nokwargs GetAXMethodDesc;
DocStr(GetMethodDesc, "Returns method description by index or name", "");
const wxFuncX& GetAXMethodDesc(int idx) const;
const wxFuncX& GetAXMethodDesc(const wxString& name) const;
DocDeclStr(
const wxFuncXArray& , GetAXEvents(),
"Returns a sequence of FuncX objects describing the events
available for this ActiveX object.", "");
DocDeclStr(
const wxFuncXArray& , GetAXMethods(),
"Returns a sequence of FuncX objects describing the methods
available for this ActiveX object.", "");
DocDeclStr(
const wxPropXArray& , GetAXProperties(),
"Returns a sequence of PropX objects describing the properties
available for this ActiveX object.", "");
DocDeclStr(
void , SetAXProp(const wxString& name, PyObject* value),
"Set a property of the ActiveX object by name.", "");
DocDeclStr(
PyObject* , GetAXProp(const wxString& name),
"Get the value of an ActiveX property by name.", "");
%nokwargs _CallAXMethod;
DocDeclStr(
PyObject* , _CallAXMethod(const wxString& name, PyObject* args),
"The implementation for CallMethod. Calls an ActiveX method, by
name passing the parameters given in args.", "");
%pythoncode {
def CallAXMethod(self, name, *args):
"""
Front-end for _CallMethod. Simply passes all positional args
after the name as a single tuple to _CallMethod.
"""
return self._CallAXMethod(name, args)
}
};
//---------------------------------------------------------------------------
%newgroup
DocDeclStr(
wxEventType , RegisterActiveXEvent(const wxString& eventName),
"Creates a standard wx event ID for the given eventName.", "");
DocStr(wxActiveXEvent,
"An instance of ActiveXEvent is sent to the handler for all bound
ActiveX events. Any event parameters from the ActiveX cntrol are
turned into attributes of the Python proxy for this event object.
Additionally, there is a property called eventName that will
return (surprisingly <wink>) the name of the ActiveX event.", "");
class wxActiveXEvent : public wxCommandEvent
{
public:
%feature("shadow") EventName "eventName = property(_activex.ActiveXEvent_EventName)";
wxString EventName();
%extend {
// This is called by the EventThunker before calling the
// handler. We'll convert and load the ActiveX event parameters into
// attributes of the Python event object.
void _preCallInit(PyObject* pyself) {
wxPyBlock_t blocked = wxPyBeginBlockThreads();
PyObject* pList = PyList_New(0);
PyObject_SetAttrString(pyself, "paramList", pList);
Py_DECREF(pList);
for (int i=0; i<self->ParamCount(); i+=1) {
PyObject* name = PyString_FromString((char*)(const char*)self->ParamName(i).mb_str());
PyObject* val = _Variant2PyObj((*self)[i], true);
PyObject_SetAttr(pyself, name, val);
PyList_Append(pList, name);
Py_DECREF(val);
Py_DECREF(name);
}
wxPyEndBlockThreads(blocked);
}
// This one is called by the EventThunker after calling the
// handler. It reloads any "out" parameters from the python attributes
// back into the wxVariant they came from.
void _postCallCleanup(PyObject* pyself) {
wxPyBlock_t blocked = wxPyBeginBlockThreads();
for (int i=0; i<self->ParamCount(); i+=1) {
PyObject* val = PyObject_GetAttrString(
pyself, (char*)(const char*)self->ParamName(i).mb_str());
_PyObj2Variant(val, (*self)[i]);
Py_DECREF(val);
}
wxPyEndBlockThreads(blocked);
}
}
};
//---------------------------------------------------------------------------
%{
// Caller should already have the GIL!
wxVariant _PyObj2Variant(PyObject* value)
{
wxVariant rval;
if (value == Py_None)
return rval;
#if PYTHON_API_VERSION >= 1012 // Python 2.3+
else if (PyBool_Check(value))
rval = (value == Py_True) ? true : false;
#endif
else if (PyInt_Check(value))
rval = PyInt_AS_LONG(value);
else if (PyFloat_Check(value))
rval = PyFloat_AS_DOUBLE(value);
else if (PyString_Check(value) || PyUnicode_Check(value))
rval = Py2wxString(value);
// TODO: PyList of strings --> wxArrayString
// wxDateTime
// list of objects
// etc.
else {
PyErr_SetString(PyExc_TypeError, "Unsupported object type in _PyObj2Variant");
rval = (long)0;
}
return rval;
}
// This one uses the type of the variant to try and force the conversion
bool _PyObj2Variant(PyObject* value, wxVariant& wv)
{
wxString type = wv.GetType();
if ( type == _T("long") || type == _T("bool") || type == _T("char") )
wv = PyInt_AsLong(value);
else if ( type == _T("string") )
wv = Py2wxString(value);
else if ( type == _T("double") )
wv = PyFloat_AsDouble(value);
else {
// it's some other type that we dont' handle yet. Log it?
return false;
}
return true;
}
// Caller should already have the GIL!
PyObject* _Variant2PyObj(wxVariant& value, bool useNone)
{
PyObject* rval = NULL;
if (value.IsNull()) {
rval = Py_None;
Py_INCREF(rval);
}
// should "char" be treated as an int or as a string?
else if (value.IsType(_T("char")) || value.IsType(_T("long")))
rval = PyInt_FromLong(value);
else if (value.IsType(_T("double")))
rval = PyFloat_FromDouble(value);
else if (value.IsType(_T("bool"))) {
rval = (bool)value ? Py_True : Py_False;
Py_INCREF(rval);
}
else if (value.IsType(_T("string")))
rval = wx2PyString(value);
else {
if (useNone) {
rval = Py_None;
Py_INCREF(rval);
}
else {
PyErr_SetString(PyExc_TypeError, "Unsupported object type in _Variant2PyObj");
}
}
return rval;
}
wxString _VARTYPEname(VARTYPE vt)
{
if (vt & VT_BYREF)
vt &= ~(VT_BYREF);
switch(vt) {
case VT_VARIANT:
return _T("VT_VARIANT");
// 1 byte chars
case VT_I1:
case VT_UI1:
// 2 byte shorts
case VT_I2:
case VT_UI2:
// 4 bytes longs
case VT_I4:
case VT_UI4:
case VT_INT:
case VT_UINT:
case VT_ERROR:
return _T("int");
// 4 byte floats
case VT_R4:
// 8 byte doubles
case VT_R8:
// decimals are converted from doubles too
case VT_DECIMAL:
return _T("double");
case VT_BOOL:
return _T("bool");
case VT_DATE:
return _T("wx.DateTime");
case VT_BSTR:
return _T("string");
case VT_UNKNOWN:
return _T("VT_UNKNOWN");
case VT_DISPATCH:
return _T("VT_DISPATCH");
case VT_EMPTY:
return _T("VT_EMPTY");
case VT_NULL:
return _T("VT_NULL");
case VT_VOID:
return _T("VT_VOID");
default:
wxString msg;
msg << _T("unsupported type ") << vt;
return msg;
}
}
%}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
%newgroup
%{
// A class derived from our wxActiveXWindow for the IE WebBrowser
// control that will serve as a base class for a Python
// implementation. This is done so we can "eat our own dog food"
// and use a class at least mostly generated by genaxmodule, but
// also get some of the extra stuff like loading a document from
// a string or a stream, getting text contents, etc. that
// Lindsay's version gives us.
//
#include <wx/mstream.h>
#include <oleidl.h>
#include <winerror.h>
#include <exdispid.h>
#include <exdisp.h>
#include <olectl.h>
#include <Mshtml.h>
#include <sstream>
#include "IEHtmlStream.h"
class wxIEHtmlWindowBase : public wxActiveXWindow {
private:
wxAutoOleInterface<IWebBrowser2> m_webBrowser;
DECLARE_ABSTRACT_CLASS(wxIEHtmlWindowBase);
public:
wxIEHtmlWindowBase ( wxWindow* parent, const CLSID& clsId, wxWindowID id = -1,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxPyPanelNameStr)
: wxActiveXWindow(parent, clsId, id, pos, size, style, name)
{
HRESULT hret;
// Get IWebBrowser2 Interface
hret = m_webBrowser.QueryInterface(IID_IWebBrowser2, m_ActiveX);
wxASSERT(SUCCEEDED(hret));
// web browser setup
m_webBrowser->put_MenuBar(VARIANT_FALSE);
m_webBrowser->put_AddressBar(VARIANT_FALSE);
m_webBrowser->put_StatusBar(VARIANT_FALSE);
m_webBrowser->put_ToolBar(VARIANT_FALSE);
m_webBrowser->put_RegisterAsBrowser(VARIANT_TRUE);
m_webBrowser->put_RegisterAsDropTarget(VARIANT_TRUE);
m_webBrowser->Navigate( L"about:blank", NULL, NULL, NULL, NULL );
}
void SetCharset(const wxString& charset)
{
HRESULT hret;
// HTML Document ?
IDispatch *pDisp = NULL;
hret = m_webBrowser->get_Document(&pDisp);
wxAutoOleInterface<IDispatch> disp(pDisp);
if (disp.Ok())
{
wxAutoOleInterface<IHTMLDocument2> doc(IID_IHTMLDocument2, disp);
if (doc.Ok())
doc->put_charset((BSTR) (const wchar_t *) charset.wc_str(wxConvUTF8));
//doc->put_charset((BSTR) wxConvUTF8.cMB2WC(charset).data());
}
}
bool LoadString(const wxString& html)
{
char *data = NULL;
size_t len = html.length();
len *= sizeof(wxChar);
data = (char *) malloc(len);
memcpy(data, html.c_str(), len);
return LoadStream(new wxOwnedMemInputStream(data, len));
}
bool LoadStream(IStreamAdaptorBase *pstrm)
{
// need to prepend this as poxy MSHTML will not recognise a HTML comment
// as starting a html document and treats it as plain text
// Does nayone know how to force it to html mode ?
#if wxUSE_UNICODE
// TODO: What to do in this case???
#else
pstrm->prepend = _T("<html>");
#endif
// strip leading whitespace as it can confuse MSHTML
wxAutoOleInterface<IStream> strm(pstrm);
// Document Interface
IDispatch *pDisp = NULL;
HRESULT hret = m_webBrowser->get_Document(&pDisp);
if (! pDisp)
return false;
wxAutoOleInterface<IDispatch> disp(pDisp);
// get IPersistStreamInit
wxAutoOleInterface<IPersistStreamInit>
pPersistStreamInit(IID_IPersistStreamInit, disp);
if (pPersistStreamInit.Ok())
{
HRESULT hr = pPersistStreamInit->InitNew();
if (SUCCEEDED(hr))
hr = pPersistStreamInit->Load(strm);
return SUCCEEDED(hr);
}
else
return false;
}
bool LoadStream(wxInputStream *is)
{
// wrap reference around stream
IwxStreamAdaptor *pstrm = new IwxStreamAdaptor(is);
pstrm->AddRef();
return LoadStream(pstrm);
}
wxString GetStringSelection(bool asHTML)
{
wxAutoOleInterface<IHTMLTxtRange> tr(wxieGetSelRange(m_oleObject));
if (! tr)
return wxEmptyString;
BSTR text = NULL;
HRESULT hr = E_FAIL;
if (asHTML)
hr = tr->get_htmlText(&text);
else
hr = tr->get_text(&text);
if (hr != S_OK)
return wxEmptyString;
wxString s = text;
SysFreeString(text);
return s;
};
wxString GetText(bool asHTML)
{
if (! m_webBrowser.Ok())
return wxEmptyString;
// get document dispatch interface
IDispatch *iDisp = NULL;
HRESULT hr = m_webBrowser->get_Document(&iDisp);
if (hr != S_OK)
return wxEmptyString;
// Query for Document Interface
wxAutoOleInterface<IHTMLDocument2> hd(IID_IHTMLDocument2, iDisp);
iDisp->Release();
if (! hd.Ok())
return wxEmptyString;
// get body element
IHTMLElement *_body = NULL;
hd->get_body(&_body);
if (! _body)
return wxEmptyString;
wxAutoOleInterface<IHTMLElement> body(_body);
// get inner text
BSTR text = NULL;
hr = E_FAIL;
if (asHTML)
hr = body->get_innerHTML(&text);
else
hr = body->get_innerText(&text);
if (hr != S_OK)
return wxEmptyString;
wxString s = text;
SysFreeString(text);
return s;
}
// void wxIEHtmlWin::SetEditMode(bool seton)
// {
// m_bAmbientUserMode = ! seton;
// AmbientPropertyChanged(DISPID_AMBIENT_USERMODE);
// };
// bool wxIEHtmlWin::GetEditMode()
// {
// return ! m_bAmbientUserMode;
// };
};
IMPLEMENT_ABSTRACT_CLASS( wxIEHtmlWindowBase, wxActiveXWindow );
%}
// we'll document it in the derived Python class
%feature("noautodoc") wxIEHtmlWindowBase;
%feature("noautodoc") wxIEHtmlWindowBase::SetCharset;
%feature("noautodoc") wxIEHtmlWindowBase::LoadString;
%feature("noautodoc") wxIEHtmlWindowBase::LoadStream;
%feature("noautodoc") wxIEHtmlWindowBase::GetStringSelection;
%feature("noautodoc") wxIEHtmlWindowBase::GetText;
MustHaveApp(wxIEHtmlWindowBase);
class wxIEHtmlWindowBase : public wxActiveXWindow {
public:
%pythonAppend wxIEHtmlWindowBase "self._setOORInfo(self)"
wxIEHtmlWindowBase ( wxWindow* parent, const CLSID& clsId, wxWindowID id = -1,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxPyPanelNameStr);
void SetCharset(const wxString& charset);
bool LoadString(const wxString& html);
bool LoadStream(wxInputStream *is);
wxString GetStringSelection(bool asHTML);
wxString GetText(bool asHTML);
};
#if 0
enum wxIEHtmlRefreshLevel
{
wxIEHTML_REFRESH_NORMAL = 0,
wxIEHTML_REFRESH_IFEXPIRED = 1,
wxIEHTML_REFRESH_CONTINUE = 2,
wxIEHTML_REFRESH_COMPLETELY = 3
};
DocStr(wxIEHtmlWin,
"");
class wxIEHtmlWin : public wxWindow
{
public:
%pythonAppend wxIEHtmlWin "self._setOORInfo(self)"
wxIEHtmlWin(wxWindow * parent, wxWindowID id = -1,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxPyPanelNameStr);
void LoadUrl(const wxString& url);
bool LoadString(wxString html);
bool LoadStream(wxInputStream *is);
%pythoncode { Navigate = LoadUrl }
void SetCharset(wxString charset);
void SetEditMode(bool seton);
bool GetEditMode();
wxString GetStringSelection(bool asHTML = false);
wxString GetText(bool asHTML = false);
bool GoBack();
bool GoForward();
bool GoHome();
bool GoSearch();
%name(RefreshPage)bool Refresh(wxIEHtmlRefreshLevel level);
bool Stop();
};
%pythoncode {
wxEVT_COMMAND_MSHTML_BEFORENAVIGATE2 = RegisterActiveXEvent('BeforeNavigate2')
wxEVT_COMMAND_MSHTML_NEWWINDOW2 = RegisterActiveXEvent('NewWindow2')
wxEVT_COMMAND_MSHTML_DOCUMENTCOMPLETE = RegisterActiveXEvent('DocumentComplete')
wxEVT_COMMAND_MSHTML_PROGRESSCHANGE = RegisterActiveXEvent('ProgressChange')
wxEVT_COMMAND_MSHTML_STATUSTEXTCHANGE = RegisterActiveXEvent('StatusTextChange')
wxEVT_COMMAND_MSHTML_TITLECHANGE = RegisterActiveXEvent('TitleChange')
EVT_MSHTML_BEFORENAVIGATE2 = wx.PyEventBinder(wxEVT_COMMAND_MSHTML_BEFORENAVIGATE2, 1)
EVT_MSHTML_NEWWINDOW2 = wx.PyEventBinder(wxEVT_COMMAND_MSHTML_NEWWINDOW2, 1)
EVT_MSHTML_DOCUMENTCOMPLETE = wx.PyEventBinder(wxEVT_COMMAND_MSHTML_DOCUMENTCOMPLETE, 1)
EVT_MSHTML_PROGRESSCHANGE = wx.PyEventBinder(wxEVT_COMMAND_MSHTML_PROGRESSCHANGE, 1)
EVT_MSHTML_STATUSTEXTCHANGE = wx.PyEventBinder(wxEVT_COMMAND_MSHTML_STATUSTEXTCHANGE, 1)
EVT_MSHTML_TITLECHANGE = wx.PyEventBinder(wxEVT_COMMAND_MSHTML_TITLECHANGE, 1)
}
#endif
//---------------------------------------------------------------------------
// Include some extra Python code into the proxy module
%pythoncode "_activex_ex.py"
//---------------------------------------------------------------------------
|