Example #1
0
 void drawRectangles(NSWindow window, Rectangle[] rects, boolean erase) {
   NSRect frame = window.frame();
   NSGraphicsContext context = window.graphicsContext();
   NSGraphicsContext.setCurrentContext(context);
   NSAffineTransform transform = NSAffineTransform.transform();
   context.saveGraphicsState();
   transform.scaleXBy(1, -1);
   transform.translateXBy(0, -frame.height);
   transform.concat();
   Point parentOrigin;
   if (parent != null) {
     parentOrigin = display.map(parent, null, 0, 0);
   } else {
     parentOrigin = new Point(0, 0);
   }
   context.setCompositingOperation(erase ? OS.NSCompositeClear : OS.NSCompositeSourceOver);
   for (int i = 0; i < rects.length; i++) {
     Rectangle rect = rects[i];
     frame.x = rect.x + parentOrigin.x;
     frame.y = rect.y + parentOrigin.y;
     frame.width = rect.width;
     frame.height = rect.height;
     if (erase) {
       frame.width++;
       frame.height++;
       NSBezierPath.fillRect(frame);
     } else {
       frame.x += 0.5f;
       frame.y += 0.5f;
       NSBezierPath.strokeRect(frame);
     }
   }
   context.flushGraphics();
   context.restoreGraphicsState();
 }
Example #2
0
 /**
  * Adds to the receiver a circular or elliptical arc that lies within the specified rectangular
  * area.
  *
  * <p>The resulting arc begins at <code>startAngle</code> and extends for <code>arcAngle</code>
  * degrees. Angles are interpreted such that 0 degrees is at the 3 o'clock position. A positive
  * value indicates a counter-clockwise rotation while a negative value indicates a clockwise
  * rotation.
  *
  * <p>The center of the arc is the center of the rectangle whose origin is (<code>x</code>, <code>
  * y</code>) and whose size is specified by the <code>width</code> and <code>height</code>
  * arguments.
  *
  * <p>The resulting arc covers an area <code>width + 1</code> pixels wide by <code>height + 1
  * </code> pixels tall.
  *
  * @param x the x coordinate of the upper-left corner of the arc
  * @param y the y coordinate of the upper-left corner of the arc
  * @param width the width of the arc
  * @param height the height of the arc
  * @param startAngle the beginning angle
  * @param arcAngle the angular extent of the arc, relative to the start angle
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  *     </ul>
  */
 public void addArc(
     float x, float y, float width, float height, float startAngle, float arcAngle) {
   if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
   NSAffineTransform transform = NSAffineTransform.transform();
   transform.translateXBy(x + width / 2f, y + height / 2f);
   transform.scaleXBy(width / 2f, height / 2f);
   NSBezierPath path = NSBezierPath.bezierPath();
   NSPoint center = new NSPoint();
   float sAngle = -startAngle;
   float eAngle = -(startAngle + arcAngle);
   path.appendBezierPathWithArcWithCenter_radius_startAngle_endAngle_clockwise_(
       center, 1, sAngle, eAngle, arcAngle > 0);
   path.transformUsingAffineTransform(transform);
   handle.appendBezierPath(path);
 }
Example #3
0
 /**
  * Adds to the receiver the pattern of glyphs generated by drawing the given string using the
  * given font starting at the point (x, y).
  *
  * @param string the text to use
  * @param x the x coordinate of the starting point
  * @param y the y coordinate of the starting point
  * @param font the font to use
  * @exception IllegalArgumentException
  *     <ul>
  *       <li>ERROR_NULL_ARGUMENT - if the font is null
  *       <li>ERROR_INVALID_ARGUMENT - if the font has been disposed
  *     </ul>
  *
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed
  *     </ul>
  */
 public void addString(String string, float x, float y, Font font) {
   if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
   if (font == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
   if (font.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
   NSString str = NSString.stringWith(string);
   NSTextStorage textStorage = ((NSTextStorage) new NSTextStorage().alloc());
   textStorage.initWithString_(str);
   NSLayoutManager layoutManager = (NSLayoutManager) new NSLayoutManager().alloc().init();
   NSTextContainer textContainer = (NSTextContainer) new NSTextContainer().alloc();
   NSSize size = new NSSize();
   size.width = Float.MAX_VALUE;
   size.height = Float.MAX_VALUE;
   textContainer.initWithContainerSize(size);
   textStorage.addLayoutManager(layoutManager);
   layoutManager.addTextContainer(textContainer);
   NSRange range = new NSRange();
   range.length = str.length();
   textStorage.beginEditing();
   textStorage.addAttribute(OS.NSFontAttributeName(), font.handle, range);
   textStorage.endEditing();
   range = layoutManager.glyphRangeForTextContainer(textContainer);
   if (range.length != 0) {
     int glyphs = OS.malloc(4 * range.length * 2);
     layoutManager.getGlyphs(glyphs, range);
     NSBezierPath path = NSBezierPath.bezierPath();
     NSPoint point = new NSPoint();
     point.x = x;
     point.y = y;
     path.moveToPoint(point);
     path.appendBezierPathWithGlyphs(glyphs, range.length, font.handle);
     NSAffineTransform transform = NSAffineTransform.transform();
     transform.scaleXBy(1, -1);
     transform.translateXBy(0, -((2 * y) + textStorage.size().height));
     path.transformUsingAffineTransform(transform);
     OS.free(glyphs);
     handle.appendBezierPath(path);
   }
   textContainer.release();
   layoutManager.release();
   textStorage.release();
 }
  void drawInteriorWithFrame_inView(
      long /*int*/ id, long /*int*/ sel, NSRect cellRect, long /*int*/ view) {
    /*
     * Feature in Cocoa.  When the last column in a tree does not reach the
     * rightmost edge of the tree view, the cell that draws the rightmost-
     * column's header is also invoked to draw the header space between its
     * right edge and the tree's right edge.  If this case is detected then
     * nothing should be drawn.
     */
    int columnIndex = parent.indexOf(nsColumn);
    NSRect headerRect = parent.headerView.headerRectOfColumn(columnIndex);
    if (headerRect.x != cellRect.x || headerRect.width != cellRect.width) return;

    NSGraphicsContext context = NSGraphicsContext.currentContext();
    context.saveGraphicsState();

    int contentWidth = 0;
    NSSize stringSize = null, imageSize = null;
    NSAttributedString attrString = null;
    NSTableHeaderCell headerCell = nsColumn.headerCell();
    if (displayText != null) {
      Font font = Font.cocoa_new(display, headerCell.font());
      attrString =
          parent.createString(
              displayText, font, null, SWT.LEFT, false, (parent.state & DISABLED) == 0, false);
      stringSize = attrString.size();
      contentWidth += Math.ceil(stringSize.width);
      if (image != null) contentWidth += MARGIN; /* space between image and text */
    }
    if (image != null) {
      imageSize = image.handle.size();
      contentWidth += Math.ceil(imageSize.width);
    }

    if (parent.sortColumn == this && parent.sortDirection != SWT.NONE) {
      boolean ascending = parent.sortDirection == SWT.UP;
      headerCell.drawSortIndicatorWithFrame(cellRect, new NSView(view), ascending, 0);
      /* remove the arrow's space from the available drawing width */
      NSRect sortRect = headerCell.sortIndicatorRectForBounds(cellRect);
      cellRect.width = Math.max(0, sortRect.x - cellRect.x);
    }

    int drawX = 0;
    if ((style & SWT.CENTER) != 0) {
      drawX = (int) (cellRect.x + Math.max(MARGIN, ((cellRect.width - contentWidth) / 2)));
    } else if ((style & SWT.RIGHT) != 0) {
      drawX = (int) (cellRect.x + Math.max(MARGIN, cellRect.width - contentWidth - MARGIN));
    } else {
      drawX = (int) cellRect.x + MARGIN;
    }

    if (image != null) {
      NSRect destRect = new NSRect();
      destRect.x = drawX;
      destRect.y = cellRect.y;
      destRect.width = Math.min(imageSize.width, cellRect.width - 2 * MARGIN);
      destRect.height = Math.min(imageSize.height, cellRect.height);
      boolean isFlipped = new NSView(view).isFlipped();
      if (isFlipped) {
        context.saveGraphicsState();
        NSAffineTransform transform = NSAffineTransform.transform();
        transform.scaleXBy(1, -1);
        transform.translateXBy(0, -(destRect.height + 2 * destRect.y));
        transform.concat();
      }
      NSRect sourceRect = new NSRect();
      sourceRect.width = destRect.width;
      sourceRect.height = destRect.height;
      image.handle.drawInRect(destRect, sourceRect, OS.NSCompositeSourceOver, 1f);
      if (isFlipped) context.restoreGraphicsState();
      drawX += destRect.width;
    }

    if (displayText != null && displayText.length() > 0) {
      if (image != null) drawX += MARGIN; /* space between image and text */
      NSRect destRect = new NSRect();
      destRect.x = drawX;
      destRect.y = cellRect.y;
      destRect.width = Math.min(stringSize.width, cellRect.x + cellRect.width - MARGIN - drawX);
      destRect.height = Math.min(stringSize.height, cellRect.height);
      attrString.drawInRect(destRect);
    }
    if (attrString != null) attrString.release();

    context.restoreGraphicsState();
  }