Although MFC and PythonWin support multiple document templates,
there’s a slight complication that isn’t immediately
obvious. When MFC is asked to open a document file, it asks each
DocumentTemplate in turn if it can
handle this document type. The default implementation for
DocumentTemplates is to report that it “can
possibly open this document.” Thus, when you’re asked to
open a Scribble document, one of the other
objects (e.g., the Python editor template) may be asked to handle it,
rather than your
wouldn’t be a problem if this application handled only one
document template, but since PythonWin already has some of its own,
it could be a problem.
Therefore, it’s necessary to modify the
DocumentTemplate so that when asked, it answers
“I can definitely open this document.” MFC then directs
the open request to the template.
You provide this functionality by overriding the MFC method
. It’s necessary for this function
to first check if a document of that name is already open; this
prevents users from opening the document multiple times. The document
template code now looks like:
class ScribbleTemplate(pywin.mfc.docview.DocTemplate): def MatchDocType(self, fileName, fileType): doc = self.FindOpenDocument(fileName) if doc: return doc ext = string.lower(os.path.splitext(fileName)) if ext =='.psd': return win32ui.CDocTemplate_Confidence_yesAttemptNative return win32ui.CDocTemplate_Confidence_noAttempt ...