docview.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # document and view classes for MFC.
  2. import win32ui
  3. import win32con
  4. from . import object
  5. from . import window
  6. class View(window.Wnd):
  7. def __init__(self, initobj):
  8. window.Wnd.__init__(self, initobj)
  9. def OnInitialUpdate(self):
  10. pass
  11. # Simple control based views.
  12. class CtrlView(View):
  13. def __init__(self, doc, wndclass, style=0):
  14. View.__init__(self, win32ui.CreateCtrlView(doc, wndclass, style))
  15. class EditView(CtrlView):
  16. def __init__(self, doc):
  17. View.__init__(self, win32ui.CreateEditView(doc))
  18. class RichEditView(CtrlView):
  19. def __init__(self, doc):
  20. View.__init__(self, win32ui.CreateRichEditView(doc))
  21. class ListView(CtrlView):
  22. def __init__(self, doc):
  23. View.__init__(self, win32ui.CreateListView(doc))
  24. class TreeView(CtrlView):
  25. def __init__(self, doc):
  26. View.__init__(self, win32ui.CreateTreeView(doc))
  27. # Other more advanced views.
  28. class ScrollView(View):
  29. def __init__(self, doc):
  30. View.__init__(self, win32ui.CreateView(doc))
  31. class FormView(View):
  32. def __init__(self, doc, id):
  33. View.__init__(self, win32ui.CreateFormView(doc, id))
  34. class Document(object.CmdTarget):
  35. def __init__(self, template, docobj=None):
  36. if docobj is None:
  37. docobj = template.DoCreateDoc()
  38. object.CmdTarget.__init__(self, docobj)
  39. class RichEditDoc(object.CmdTarget):
  40. def __init__(self, template):
  41. object.CmdTarget.__init__(self, template.DoCreateRichEditDoc())
  42. class CreateContext:
  43. "A transient base class used as a CreateContext"
  44. def __init__(self, template, doc = None):
  45. self.template = template
  46. self.doc = doc
  47. def __del__(self):
  48. self.close()
  49. def close(self):
  50. self.doc = None
  51. self.template = None
  52. class DocTemplate(object.CmdTarget):
  53. def __init__(self, resourceId=None, MakeDocument=None, MakeFrame=None, MakeView=None):
  54. if resourceId is None: resourceId = win32ui.IDR_PYTHONTYPE
  55. object.CmdTarget.__init__(self, self._CreateDocTemplate(resourceId))
  56. self.MakeDocument=MakeDocument
  57. self.MakeFrame=MakeFrame
  58. self.MakeView=MakeView
  59. self._SetupSharedMenu_()
  60. def _SetupSharedMenu_(self):
  61. pass # to be overridden by each "app"
  62. def _CreateDocTemplate(self, resourceId):
  63. return win32ui.CreateDocTemplate(resourceId)
  64. def __del__(self):
  65. object.CmdTarget.__del__(self)
  66. def CreateCreateContext(self, doc=None):
  67. return CreateContext(self, doc)
  68. def CreateNewFrame(self, doc):
  69. makeFrame = self.MakeFrame
  70. if makeFrame is None: makeFrame = window.MDIChildWnd
  71. wnd = makeFrame()
  72. context = self.CreateCreateContext(doc)
  73. wnd.LoadFrame(self.GetResourceID(), -1, None, context) # triggers OnCreateClient...
  74. return wnd
  75. def CreateNewDocument(self):
  76. makeDocument = self.MakeDocument
  77. if makeDocument is None:
  78. makeDocument = Document
  79. return makeDocument(self)
  80. def CreateView(self, frame, context):
  81. makeView = self.MakeView
  82. if makeView is None: makeView = EditView
  83. view = makeView(context.doc)
  84. view.CreateWindow(frame)
  85. class RichEditDocTemplate(DocTemplate):
  86. def __init__(self, resourceId=None, MakeDocument=None, MakeFrame=None, MakeView=None):
  87. if MakeView is None: MakeView = RichEditView
  88. if MakeDocument is None: MakeDocument = RichEditDoc
  89. DocTemplate.__init__(self, resourceId, MakeDocument, MakeFrame, MakeView)
  90. def _CreateDocTemplate(self, resourceId):
  91. return win32ui.CreateRichEditDocTemplate(resourceId)
  92. def t():
  93. class FormTemplate(DocTemplate):
  94. def CreateView(self, frame, context):
  95. makeView = self.MakeView
  96. # view = FormView(context.doc, win32ui.IDD_PROPDEMO1)
  97. view = ListView(context.doc)
  98. view.CreateWindow(frame)
  99. t=FormTemplate()
  100. return t.OpenDocumentFile(None)