objdoc.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # This is a sample file, and shows the basic framework for using an "Object" based
  2. # document, rather than a "filename" based document.
  3. # This is referenced by the Pythonwin .html documentation.
  4. # In the example below, the OpenObject() method is used instead of OpenDocumentFile,
  5. # and all the core MFC document open functionality is retained.
  6. import win32ui
  7. from pywin.mfc import docview
  8. class object_template (docview.DocTemplate):
  9. def __init__(self):
  10. docview.DocTemplate.__init__(self, None, None, None, object_view)
  11. def OpenObject(self, object): # Use this instead of OpenDocumentFile.
  12. # Look for existing open document
  13. for doc in self.GetDocumentList():
  14. print("document is ", doc)
  15. if doc.object is object:
  16. doc.GetFirstView().ActivateFrame()
  17. return doc
  18. # not found - new one.
  19. doc = object_document(self, object)
  20. frame = self.CreateNewFrame(doc)
  21. doc.OnNewDocument()
  22. doc.SetTitle(str(object))
  23. self.InitialUpdateFrame(frame, doc)
  24. return doc
  25. class object_document (docview.Document):
  26. def __init__(self, template, object):
  27. docview.Document.__init__(self, template)
  28. self.object = object
  29. def OnOpenDocument (self, name):
  30. raise RuntimeError("Should not be called if template strings set up correctly")
  31. return 0
  32. class object_view (docview.EditView):
  33. def OnInitialUpdate (self):
  34. self.ReplaceSel("Object is %s" % repr(self.GetDocument().object))
  35. def demo ():
  36. t = object_template()
  37. d = t.OpenObject(win32ui)
  38. return (t, d)
  39. if __name__=='__main__':
  40. import demoutils
  41. if demoutils.NeedGoodGUI():
  42. demo()