1# 2# PreferencesPanelController.py 3# ControlledPreferences 4# 5# Converted by u.fiedler on 04.02.05. 6# with great help from Bob Ippolito - Thank you Bob! 7# 8# The original version was written in Objective-C by Malcolm Crawford 9# at http://homepage.mac.com/mmalc/CocoaExamples/controllers.html 10 11 12from FontNameToDisplayNameTransformer import FontNameToDisplayNameTransformer 13from Foundation import * 14from AppKit import * 15from PyObjCTools.KeyValueCoding import * 16 17class PreferencesPanelController (NSWindowController): 18 19 @objc.IBAction 20 def changeTextFont_(self, sender): 21 "The user changed the current font selection, so update the default font" 22 23 # Get font name and size from user defaults 24 defaults = NSUserDefaultsController.sharedUserDefaultsController().values() 25 fontName = getKey(defaults, u'FontName') 26 fontSize = getKey(defaults, u'FontSize') 27 28 # Create font from name and size; initialize font panel 29 font = NSFont.fontWithName_size_(fontName, fontSize) 30 if font is None: 31 font = NSFont.systemFontOfSize_(NSFont.systemFontSize()) 32 NSFontManager.sharedFontManager().setSelectedFont_isMultiple_(font, False) 33 NSFontManager.sharedFontManager().orderFrontFontPanel_(self) 34 35 # Set window as firstResponder so we get changeFont: messages 36 self.window().makeFirstResponder_(self.window()) 37 38 39 @objc.IBAction 40 def changeFont_(self, sender): 41 "This is the message the font panel sends when a new font is selected" 42 # Get selected font 43 fontManager = NSFontManager.sharedFontManager() 44 selectedFont = fontManager.selectedFont() 45 if selectedFont is None: 46 selectedFont = NSFont.systemFontOfSize_(NSFont.systemFontSize()) 47 panelFont = fontManager.convertFont_(selectedFont) 48 49 # Get and store details of selected font 50 # Note: use fontName, not displayName. The font name identifies the font to 51 # the system, we use a value transformer to show the user the display name 52 fontSize = panelFont.pointSize() 53 54 defaults = NSUserDefaultsController.sharedUserDefaultsController().values() 55 defaults.setValue_forKey_(panelFont.fontName(), u"FontName") 56 defaults.setValue_forKey_(fontSize, u"FontSize") 57 58 59""" 60Set up initial values for defaults: 61Create dictionary with keys and values for WordOfTheDay, FontName, 62FontSize, and FavoriteColor. Mostly straightforward, but: 63 64Store the fontName of the font as the default; the textfield displays 65the font's displayName using a value transformer. 66 67The color must be archived -- you can't store NSColors directly in NSUserDefaults. 68""" 69 70dictionary = {} 71dictionary[u'WordOfTheDay'] = u'Today' 72systemFont = NSFont.systemFontOfSize_(NSFont.systemFontSize()) 73dictionary[u"FontName"] = systemFont.fontName() 74dictionary[u"FontSize"] = systemFont.pointSize() 75archivedColor = NSArchiver.archivedDataWithRootObject_(NSColor.greenColor()) 76dictionary[u'FavoriteColor'] = archivedColor 77NSUserDefaultsController.sharedUserDefaultsController().setInitialValues_(dictionary) 78 79# Create and register font name value transformer 80transformer = FontNameToDisplayNameTransformer.alloc().init() 81NSValueTransformer.setValueTransformer_forName_(transformer, u'FontNameToDisplayNameTransformer') 82