| 1 | import objc |
|---|
| 2 | from AppKit import * |
|---|
| 3 | from defconAppKit.windows.progressWindow import ProgressWindow |
|---|
| 4 | from defcon import Font |
|---|
| 5 | from area51Lib.interface.documentWindow import Area51DocumentWindowController |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | class Area51Document(NSDocument): |
|---|
| 9 | |
|---|
| 10 | def readFromURL_ofType_error_(self, url, tp): |
|---|
| 11 | """ |
|---|
| 12 | NSDocument method that is called when |
|---|
| 13 | a new document is being read from a path. |
|---|
| 14 | In this case, the _loadFont method is |
|---|
| 15 | called and the result is returned. |
|---|
| 16 | """ |
|---|
| 17 | # read a font |
|---|
| 18 | result = self._loadFont(url) |
|---|
| 19 | return (result, None) |
|---|
| 20 | |
|---|
| 21 | def _loadFont(self, url): |
|---|
| 22 | """ |
|---|
| 23 | Private method that hadles loading the |
|---|
| 24 | UFO at path. This returns a boolean |
|---|
| 25 | indicating if it was successful or not. |
|---|
| 26 | """ |
|---|
| 27 | path = url.path() |
|---|
| 28 | # start a progress window |
|---|
| 29 | self.loadingProgress = ProgressWindow("Opening...") |
|---|
| 30 | # try to open |
|---|
| 31 | try: |
|---|
| 32 | self.font = Font(path) |
|---|
| 33 | # if there is a problem, close the progress |
|---|
| 34 | # window and return False |
|---|
| 35 | except: |
|---|
| 36 | self.loadingProgress.close() |
|---|
| 37 | del self.loadingProgress |
|---|
| 38 | return False |
|---|
| 39 | # success. return True. |
|---|
| 40 | return True |
|---|
| 41 | |
|---|
| 42 | def writeSafelyToURL_ofType_forSaveOperation_error_(self, url, tp, op): |
|---|
| 43 | """ |
|---|
| 44 | NSDocument that is called when the user |
|---|
| 45 | wants to save the font. This lets the |
|---|
| 46 | font object do the work. |
|---|
| 47 | """ |
|---|
| 48 | path = url.path() |
|---|
| 49 | progress = None |
|---|
| 50 | # save as. toss up progress. |
|---|
| 51 | if op == NSSaveAsOperation: |
|---|
| 52 | progress = self.vanillaWindowController.startProgress("Saving...") |
|---|
| 53 | # if the font path is not the same as the url |
|---|
| 54 | # and this is not a save as operation, then the |
|---|
| 55 | # user has moved the file in the finder. handle |
|---|
| 56 | # this by setting the path in the font. |
|---|
| 57 | else: |
|---|
| 58 | if self.font.path != path: |
|---|
| 59 | self.font.path = path |
|---|
| 60 | # now save |
|---|
| 61 | self.font.save(path) |
|---|
| 62 | # close progress |
|---|
| 63 | if progress: |
|---|
| 64 | progress.close() |
|---|
| 65 | return (True, None) |
|---|
| 66 | |
|---|
| 67 | def makeWindowControllers(self): |
|---|
| 68 | """ |
|---|
| 69 | NSDocument method that will be called when it |
|---|
| 70 | is time to make the interface for the document. |
|---|
| 71 | """ |
|---|
| 72 | window = self.vanillaWindowController = Area51DocumentWindowController(self.font) |
|---|
| 73 | self.loadingProgress.close() |
|---|
| 74 | del self.loadingProgress |
|---|
| 75 | windowController = window.w.getNSWindowController() |
|---|
| 76 | self.addWindowController_(windowController) |
|---|
| 77 | |
|---|
| 78 | def setFileURL_(self, url): |
|---|
| 79 | """ |
|---|
| 80 | NSDocument method that will be called when |
|---|
| 81 | the user changes the file name outside |
|---|
| 82 | of this application, ie in the Finder. |
|---|
| 83 | """ |
|---|
| 84 | super(Area51Document, self).setFileURL_(url) |
|---|
| 85 | if hasattr(self, "vanillaWindowController"): |
|---|
| 86 | self.vanillaWindowController.filePathChangedByUser() |
|---|
| 87 | |
|---|
| 88 | def validateUserInterfaceItem_(self, item): |
|---|
| 89 | """ |
|---|
| 90 | This method validates menu items. We only care |
|---|
| 91 | about the "generate font" menu item, so this |
|---|
| 92 | only deals with that. |
|---|
| 93 | """ |
|---|
| 94 | if item.action() != "compileOTF:": |
|---|
| 95 | return super(Area51Document, self).validateUserInterfaceItem_(item) |
|---|
| 96 | # only enable the item if the FDK has been found |
|---|
| 97 | return NSApp().delegate().haveFDK() |
|---|
| 98 | |
|---|
| 99 | # ----------------- |
|---|
| 100 | # Inspector Support |
|---|
| 101 | # ----------------- |
|---|
| 102 | |
|---|
| 103 | def getSelectedGlyph(self): |
|---|
| 104 | return self.vanillaWindowController.getSelectedGlyph() |
|---|
| 105 | |
|---|
| 106 | # ----------------- |
|---|
| 107 | # Menu Item Methods |
|---|
| 108 | # ----------------- |
|---|
| 109 | |
|---|
| 110 | def compileOTF_(self, sender): |
|---|
| 111 | """ |
|---|
| 112 | This method will be called when the user selects the |
|---|
| 113 | "Generate Font..." menu item. |
|---|
| 114 | """ |
|---|
| 115 | self.vanillaWindowController.compileOTF() |
|---|
| 116 | |
|---|