object.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # MFC base classes.
  2. import sys
  3. import win32ui
  4. class Object:
  5. def __init__(self, initObj = None):
  6. self.__dict__['_obj_'] = initObj
  7. # self._obj_ = initObj
  8. if initObj is not None: initObj.AttachObject(self)
  9. def __del__(self):
  10. self.close()
  11. def __getattr__(self, attr): # Make this object look like the underlying win32ui one.
  12. # During cleanup __dict__ is not available, causing recursive death.
  13. if not attr.startswith('__'):
  14. try:
  15. o = self.__dict__['_obj_']
  16. if o is not None:
  17. return getattr(o, attr)
  18. # Only raise this error for non "internal" names -
  19. # Python may be calling __len__, __nonzero__, etc, so
  20. # we dont want this exception
  21. if attr[0]!= '_' and attr[-1] != '_':
  22. raise win32ui.error("The MFC object has died.")
  23. except KeyError:
  24. # No _obj_ at all - dont report MFC object died when there isnt one!
  25. pass
  26. raise AttributeError(attr)
  27. def OnAttachedObjectDeath(self):
  28. # print "object", self.__class__.__name__, "dieing"
  29. self._obj_ = None
  30. def close(self):
  31. if '_obj_' in self.__dict__:
  32. if self._obj_ is not None:
  33. self._obj_.AttachObject(None)
  34. self._obj_ = None
  35. class CmdTarget(Object):
  36. def __init__(self, initObj):
  37. Object.__init__(self, initObj)
  38. def HookNotifyRange(self, handler, firstID, lastID):
  39. oldhandlers = []
  40. for i in range(firstID, lastID + 1):
  41. oldhandlers.append(self.HookNotify(handler, i))
  42. return oldhandlers
  43. def HookCommandRange(self, handler, firstID, lastID):
  44. oldhandlers = []
  45. for i in range(firstID, lastID + 1):
  46. oldhandlers.append(self.HookCommand(handler, i))
  47. return oldhandlers
  48. def HookCommandUpdateRange(self, handler, firstID, lastID):
  49. oldhandlers = []
  50. for i in range(firstID, lastID + 1):
  51. oldhandlers.append(self.HookCommandUpdate(handler, i))
  52. return oldhandlers