⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 activex.i

📁 Wxpython Implemented on Windows CE, Source code
💻 I
📖 第 1 页 / 共 3 页
字号:
        }
    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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -