Enhancing the DocumentTemplate
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
registered 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
DocumentTemplate
objects (e.g., the Python editor template) may be asked to handle it,
rather than your ScribbleTemplate. This
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
MatchDocType()
. 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)[1]) if ext =='.psd': return win32ui.CDocTemplate_Confidence_yesAttemptNative return win32ui.CDocTemplate_Confidence_noAttempt ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access