We'll now set up a corresponding document class which is a simple text editor.

import W

import os
import macfs

class MyDocumentClass(W.Window):
	_untitledcounter = 1
	
        # Initialisation
        def __init__(self, path = ""):
                # get the title and data
                if not path:
                        self.title = "Untitled " + `MyDocumentClass._untitledcounter`
                        MyDocumentClass._untitledcounter = MyDocumentClass._untitledcounter + 1
                        text = ""
                elif os.path.exists(path):
                        path = macfs.ResolveAliasFile(path)[0].as_pathname()
                        dir, name = os.path.split(path)
                        self.title = name
                        f = open(path, "rb")
                        text = f.read()
                        f.close()
                        # Might read from resource fork here
                self.path = path

                # Set up the window and widgets
                W.Window.__init__(self, (600, 400), self.title, minsize = (330, 120), tabbable = 0)
                self.setupwidgets(text)
                self.editgroup.editor.changed = 0

        def setupwidgets(self, text):
                self.editgroup = W.Group((0, 0, 0, 0))
                editor = W.TextEditor((0, 0, -15,-15), text)

                self.editgroup._barx = W.Scrollbar((0, -15, -14, 16), editor.hscroll, max = 32767)
                self.editgroup._bary = W.Scrollbar((-15, 0, 16, -14), editor.vscroll, max = 32767)
                self.editgroup.editor = editor

        # Finalisation
        def close(self):
                # If things have changed, should we save before closing?
                if self.editgroup.editor.changed:
                        import EasyDialogs
                        import Qd
                        Qd.InitCursor() # XXX should be done by dialog
                        save = EasyDialogs.AskYesNoCancel('Save window Ò%sÓ before closing?' % self.title, 1)
                        if save > 0:
                                if self.domenu_save():
                                        return 1
                        elif save < 0:
                                return 1
                W.Window.close(self)
                
        # Document Menu Items
        def domenu_close(self, *args):
                return self.close()

        def domenu_save(self, *args):
                if not self.path:
                        # Will call us recursively
                        return self.domenu_save_as()
                data = self.editgroup.editor.get()
                f = open(self.path, 'wb')
                f.write(data)
                f.close()
                import macostools
                macostools.touched(self.path)
                self.editgroup.editor.changed = 0
        
        def can_save(self, menuitem):
                return self.editgroup.editor.changed or self.editgroup.editor.selchanged
                
        def domenu_save_as(self, *args):
                fss, ok = macfs.StandardPutFile('Save as:', self.title)
                if not ok: 
                        return 1
                self.path = fss.as_pathname()
                dir, name = os.path.split(self.path)
                self.title = name
                self.settitle(self.title)
                self.domenu_save()

It's worthwhile noting that, because we are only using standard menu items, and are using them in the same way as the Python IDE, you can import this and create your own instances of the document interactively.