more changes to the demo

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@7187 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn
2000-04-17 04:25:48 +00:00
parent 833a6790ad
commit 9bf69b7bb5
7 changed files with 300 additions and 89 deletions

View File

@@ -2,7 +2,7 @@
# Name: wxPython.lib.activexwrapper
# Purpose: a wxWindow derived class that can hold an ActiveX control
#
# Author: Mike Fletcher, Robin Dunn
# Author: Robin Dunn
#
# RCS-ID: $Id$
# Copyright: (c) 2000 by Total Control Software
@@ -13,70 +13,127 @@ from wxPython.wx import *
try:
import win32ui
import pywin.mfc.activex
import win32com.client
except ImportError:
raise ImportError( "Cannot find win32ui module, wxWindows ActiveXControl module cannot be loaded")
raise ImportError( "ActiveXWrapper requires PythonWin. Please install the win32all-xxx.exe package.")
### Following makes it possible to include this module without
# needing to include the (rather large) win32con module...
##from win32con import WS_TABSTOP, WS_VISIBLE
WS_TABSTOP = 0x10000
WS_TABSTOP = 0x00010000
WS_VISIBLE = 0x10000000
class ActiveXWrapper( wxWindow ):
#----------------------------------------------------------------------
def MakeActiveXClass(CoClass, eventClass=None, eventObj=None):
"""
Class providing easy integration of ActiveXControls into
wxPython GUI environments. Pass an instantiated control class (one
which inherits from pywin.mfc.activex.Control and a particular control
class generated by gencache) in the argument "control". Alternatively,
pass the instantiated control to the SetControl method to allow for
specifying the win32 style, id, etceteras
Dynamically construct a new class that derives from wxWindow, the
ActiveX control and the appropriate COM classes. This new class
can be used just like the wxWindow class, but will also respond
appropriately to the methods and properties of the COM object. If
this class, a derived class or a mix-in class has method names
that match the COM objects event names, they will be called
automatically.
CoClass -- A COM control class from a module generated by
makepy.py from a COM TypeLibrary. Can also accept a
CLSID.
eventClass -- If given, this class will be added to the set of
base classes that the new class is drived from. It is
good for mix-in classes for catching events.
eventObj -- If given, this object will be searched for attributes
by the new class's __getattr__ method, (like a mix-in
object.) This is useful if you want to catch COM
callbacks in an existing object, (such as the parent
window.)
"""
def __init__( self, parent, ID=-1, pos = wxDefaultPosition, size = wxDefaultSize,
style = 0,
name = "ActiveXControlWindow",
control = None,
controlName = "Control",
eraseBackground = 1,
):
wxWindow.__init__( self, parent, ID, pos, size, style, name )
win32ui.EnableControlContainer()
self.windowPointer = win32ui.CreateWindowFromHandle(self.GetHandle())
if control:
self.SetControl( control, name )
EVT_SIZE( self, self.OnSize)
if not eraseBackground:
def nullfunction( event ): pass
EVT_ERASE_BACKGROUND( self, nullfunction )
def SetControl( self, controlObject, name="Control",
style=WS_TABSTOP | WS_VISIBLE, ID=None):
"""
Pass a pre-built control object, calls the control's
CreateControl method with the proper arguments to make it a
child of this window
"""
self.control = controlObject
w,h = self.GetSizeTuple()
if not ID:
ID = wxNewId()
self.control.CreateControl( name, style, (0,0,w,h),
self.windowPointer,
ID)
if type(CoClass) == type(""):
# use the CLSID to get the real class
CoClass = win32com.client.CLSIDToClass(CoClass)
# determine the base classes
axEventClass = CoClass.default_source
baseClasses = [wxWindow, pywin.mfc.activex.Control, CoClass, axEventClass]
if eventClass:
baseClasses.append(eventClass)
baseClasses = tuple(baseClasses)
# define the class attributes
className = 'AXControl_'+CoClass.__name__
classDict = { '__init__' : axw__init__,
'__getattr__' : axw__getattr__,
'axw_OnSize' : axw_OnSize,
'axw_OEB' : axw_OEB,
'_name' : className,
'_eventBase' : axEventClass,
'_eventObj' : eventObj,
'Cleanup' : axw_Cleanup,
}
# make a new class object
import new
classObj = new.classobj(className, baseClasses, classDict)
return classObj
def Cleanup( self ):
"""Delete the reference to the control, to ensure that it is dereferenced."""
self.control = None
self.windowPointer = None
def OnSize( self, event=None ):
"""wxPython resize event, call MoveWindow on the control"""
size = self.GetClientSize()
self.control.MoveWindow( (0,0,size.width,size.height),1)
# These functions will be used as methods in the new class
def axw__init__(self, parent, ID, pos=wxDefaultPosition, size=wxDefaultSize, style=0):
# init base classes
pywin.mfc.activex.Control.__init__(self)
wxWindow.__init__(self, parent, -1, pos, size, style)
win32ui.EnableControlContainer()
self._eventObj = self._eventObj # move from class to instance
# create a pythonwin wrapper around this wxWindow
handle = self.GetHandle()
self._wnd = win32ui.CreateWindowFromHandle(handle)
# create the control
sz = self.GetSize()
self.CreateControl(self._name, WS_TABSTOP | WS_VISIBLE,
(0, 0, sz.width, sz.height), self._wnd, ID)
# init the ax events part of the object
self._eventBase.__init__(self, self._dispobj_)
# hook some wx events
EVT_SIZE(self, self.axw_OnSize)
EVT_ERASE_BACKGROUND(self, self.axw_OEB)
def axw__getattr__(self, attr):
try:
return pywin.mfc.activex.Control.__getattr__(self, attr)
except AttributeError:
try:
eo = self.__dict__['_eventObj']
return getattr(eo, attr)
except AttributeError:
raise AttributeError('Attribute not found: %s' % attr)
def axw_OnSize(self, event):
sz = self.GetClientSize() # get wxWindow size
self.MoveWindow((0, 0, sz.width, sz.height), 1) # move the AXControl
def axw_OEB(self, event):
pass
def axw_Cleanup(self):
#del self._wnd
self.close()
## anything else???