Esempio n. 1
0
 def update(self, s):
     retval = None
     # find format
     fmt = 'string'
     if isinstance(s, NSAttributedString):
         fmt = 'NSAttributedString'
     elif isinstance(s, NSData):
         fmt = 'rtf'
     # we only do highlighting if string is supplied
     if fmt == 'string':
         # format the output
         try:
             lexer = guess_lexer(
                 s).__class__ if self.lexer is True else self.lexer
         except pygments.util.ClassNotFound:
             lexer = None
         if lexer is not None:
             rtf = highlight(
                 s,
                 lexer(),
                 RtfFormatter(
                     style=self.style,
                     fontface=self.font,
                     fontsize=self.fontsize *
                     2  # *2 as it wants half points
                 )).encode('utf-8')
             data = NSData.dataWithBytes_length_(rtf, len(rtf))
             retval = self.update(data)
         else:
             # fallback - the string unformatted
             retval = NSAttributedString.alloc().initWithString_(s)
             if self.sel is not None:
                 self.sel(retval)
     elif fmt == 'rtf':
         a = NSAttributedString.alloc().initWithRTF_documentAttributes_(
             s, None)
         retval = a[0]
         if self.sel is not None:
             self.sel(a[0])
     elif fmt == 'NSAttributedString':
         retval = s
         if self.sel is not None:
             self.sel(s)
     else:
         raise ValueError('unsupported format; %s' % fmt)
     return retval
Esempio n. 2
0
 def _formatString_forOutput_(self, s, name):
     return NSAttributedString.alloc().initWithString_attributes_(
         s,
         {
             NSFontAttributeName:self.font(),
             NSForegroundColorAttributeName:getattr(self, name+'Color')(),
         },
     )
Esempio n. 3
0
 def formatString_forOutput_(self, s, name):
     return NSAttributedString.alloc().initWithString_attributes_(
         s,
         {
             NSFontAttributeName: self.font(),
             NSForegroundColorAttributeName: self.colorForName_(name),
         },
     )
Esempio n. 4
0
 def _formatString_forOutput_(self, s, name):
     return NSAttributedString.alloc().initWithString_attributes_(
         s,
         {
             NSFontAttributeName: self.font(),
             NSForegroundColorAttributeName: getattr(self,
                                                     name + "Color")(),
         },
     )
Esempio n. 5
0
    def drawRect_(self, rect):
        defaults = NSUserDefaultsController.sharedUserDefaultsController().values()
        favoriteColor = NSUnarchiver.unarchiveObjectWithData_(getKey(defaults, 'FavoriteColor'))
        fontName = getKey(defaults, 'FontName')
        fontSize = getKey(defaults, 'FontSize')
        favoriteFont = NSFont.fontWithName_size_(fontName, fontSize)
        wordOfTheDay = getKey(defaults, 'WordOfTheDay')

        # Do the actual drawing
        myBounds = self.bounds() # = (x, y), (bw, bh)
        NSDrawLightBezel(myBounds, myBounds)
        favoriteColor.set()
        NSRectFill(NSInsetRect(myBounds, 2, 2))
        attrsDictionary = {NSFontAttributeName: favoriteFont}
        attrString = NSAttributedString.alloc().initWithString_attributes_(wordOfTheDay, attrsDictionary)
        attrSize = attrString.size() # = (bw, bh)
        attrString.drawAtPoint_(
            NSMakePoint(
                (attrSize.width / -2) + (myBounds.size.width / 2),
                (attrSize.height / -2) + (myBounds.size.height / 2),
            ),
        )