O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required