Changeset 151

Show
Ignore:
Timestamp:
03/08/08 01:08:32 (10 months ago)
Author:
tal
Message:
Draw a nice drag image.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • packages/defconAppKit/trunk/Lib/defconAppKit/views/glyphCellView.py

    r108 r151  
    99 
    1010gridColor = backgroundColor = NSColor.colorWithCalibratedWhite_alpha_(.6, 1.0) 
    11 selectionColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.87, .87, .9, 1.0) 
     11selectionColor = NSColor.colorWithCalibratedRed_green_blue_alpha_(.82, .82, .9, 1.0) 
    1212 
    1313 
    1414DefconAppKitGlyphPboardType = "DefconAppKitGlyphPboardType" 
     15 
     16 
     17def _makeGlyphCellDragIcon(glyphs): 
     18    font = None 
     19    glyphRepresentation = None 
     20    glyphWidth = None 
     21    for glyph in glyphs: 
     22        rep = glyph.getRepresentation("NSBezierPath") 
     23        if not rep.isEmpty(): 
     24            font = glyph.getParent() 
     25            glyphRepresentation = rep 
     26            glyphWidth = glyph.width 
     27            break 
     28    # set up the image 
     29    iconImage = NSImage.alloc().initWithSize_((40, 40)) 
     30    iconImage.lockFocus() 
     31    context = NSGraphicsContext.currentContext() 
     32    # draw the page base 
     33    pageSize = 35 
     34    pagePath = NSBezierPath.bezierPath() 
     35    pagePath.moveToPoint_((.5, 5.5)) 
     36    pagePath.lineToPoint_((.5, 39.5)) 
     37    pagePath.lineToPoint_((34.5, 39.5)) 
     38    pagePath.lineToPoint_((34.5, 5.5)) 
     39    pagePath.lineToPoint_((.5, 5.5)) 
     40    # set the shadow 
     41    context.saveGraphicsState() 
     42    shadow = NSShadow.alloc().init() 
     43    shadow.setShadowOffset_((1, -1)) 
     44    shadow.setShadowColor_(NSColor.blackColor()) 
     45    shadow.setShadowBlurRadius_(2.0) 
     46    shadow.set() 
     47    # fill the page 
     48    NSColor.whiteColor().set() 
     49    pagePath.fill() 
     50    try: 
     51        color1 = NSColor.colorWithCalibratedWhite_alpha_(.95, 1) 
     52        color2 = NSColor.colorWithCalibratedWhite_alpha_(.85, 1) 
     53        gradient = NSGradient.alloc().initWithColors_([color1, color2]) 
     54        gradient.drawInBezierPath_angle_(pagePath, -90) 
     55    except NameError: 
     56        pass 
     57    # remove the shadow 
     58    context.restoreGraphicsState() 
     59    # draw the glyph 
     60    if glyphRepresentation is not None: 
     61        context.saveGraphicsState() 
     62        buffer = pageSize * .125 
     63        upm = font.info.unitsPerEm 
     64        scale = (pageSize - buffer - buffer) / float(upm) 
     65        xOffset = ((pageSize * (1.0 / scale)) - glyphWidth) / 2 
     66        transform = NSAffineTransform.transform() 
     67        transform.translateXBy_yBy_(0, 5 + buffer) 
     68        transform.scaleBy_(scale) 
     69        transform.translateXBy_yBy_(xOffset, -font.info.descender) 
     70        transform.concat() 
     71        NSColor.colorWithCalibratedWhite_alpha_(0, .8).set() 
     72        # XXX should clip the glyph path here to prevent overflow 
     73        glyphRepresentation.fill() 
     74        context.restoreGraphicsState() 
     75    # draw the page border 
     76    NSColor.grayColor().set() 
     77    pagePath.stroke() 
     78    # done 
     79    iconImage.unlockFocus() 
     80    # add the count badge 
     81    return addCountBadgeToIcon(len(glyphs), iconImage) 
    1582 
    1683 
     
    570637    def _beginDrag(self, event): 
    571638        s = " ".join([str(i) for i in sorted(self._selection)]) 
    572         image = addCountBadgeToIcon(len(self._selection)
     639        image = _makeGlyphCellDragIcon([self._glyphs[i] for i in self._selection]
    573640 
    574641        eventLocation = event.locationInWindow()