• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.9.5/pyobjc-42/2.5/pyobjc/pyobjc-framework-Cocoa/Examples/AppKit/CocoaBindings/ControlledPreferences/
1#
2#  FontSampleDisplayView.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
11from Foundation import *
12from AppKit import *
13from PyObjCTools.KeyValueCoding import *
14
15class FontSampleDisplayView(NSView):
16    """
17    Display WordOfTheDay with the preferred font and color
18    """
19    def drawRect_(self, rect):
20        defaults = NSUserDefaultsController.sharedUserDefaultsController().values()
21        favoriteColor = NSUnarchiver.unarchiveObjectWithData_(getKey(defaults, u'FavoriteColor'))
22        fontName = getKey(defaults, u'FontName')
23        fontSize = getKey(defaults, u'FontSize')
24        favoriteFont = NSFont.fontWithName_size_(fontName, fontSize)
25        wordOfTheDay = getKey(defaults, u'WordOfTheDay')
26
27        # Do the actual drawing
28        myBounds = self.bounds() # = (x, y), (bw, bh)
29        NSDrawLightBezel(myBounds, myBounds)
30        favoriteColor.set()
31        NSRectFill(NSInsetRect(myBounds, 2, 2))
32        attrsDictionary = {NSFontAttributeName: favoriteFont}
33        attrString = NSAttributedString.alloc().initWithString_attributes_(wordOfTheDay, attrsDictionary)
34        attrSize = attrString.size() # = (bw, bh)
35        attrString.drawAtPoint_(
36            NSMakePoint(
37                (attrSize.width / -2) + (myBounds.size.width / 2),
38                (attrSize.height / -2) + (myBounds.size.height / 2),
39            ),
40        )
41
42    def initWithFrame_(self, frameRect):
43        self = super(FontSampleDisplayView, self).initWithFrame_(frameRect)
44        dnc = NSNotificationCenter.defaultCenter()
45        dnc.addObserver_selector_name_object_(self, "redisplay:", NSUserDefaultsDidChangeNotification, None)
46        return self
47
48    def redisplay_(self, sender):
49        self.setNeedsDisplay_(True)
50