private AutoCloseable configGC(final GC gc) {
    final int lineStyle = gc.getLineStyle();
    final int alpha = gc.getAlpha();
    final int[] lineDash = gc.getLineDash();

    final Color foreground = gc.getForeground();
    final Color background = gc.getBackground();

    gc.setForeground(this.indentGuide.getColor(styledText));
    gc.setBackground(styledText.getBackground());
    gc.setAlpha(this.indentGuide.getTransparency());
    gc.setLineStyle(SWT.LINE_CUSTOM);
    gc.setLineDash(new int[] {1, 2});
    return new AutoCloseable() {

      @Override
      public void close() throws Exception {
        gc.setForeground(foreground);
        gc.setBackground(background);
        gc.setAlpha(alpha);
        gc.setLineStyle(lineStyle);
        gc.setLineDash(lineDash);
      }
    };
  }
  @Test
  public void testRestoresState() {
    dispatcher.dispatch();

    assertEquals(0, gc.getAlpha());
    assertEquals(0, gc.getLineWidth());
    assertNull(gc.getForeground());
  }
 @Override
 public void paint(GC gc) {
   CanvasViewInfo lastRoot = mViewHierarchy.getRoot();
   if (lastRoot != null) {
     gc.setForeground(mOutlineColor);
     gc.setLineStyle(SwtDrawingStyle.OUTLINE.getLineStyle());
     int oldAlpha = gc.getAlpha();
     gc.setAlpha(SwtDrawingStyle.OUTLINE.getStrokeAlpha());
     drawOutline(gc, lastRoot);
     gc.setAlpha(oldAlpha);
   }
 }
 /**
  * Draw characters in view range.
  *
  * @param gc
  * @param x
  * @param y
  * @param w
  * @param h
  */
 private void handleDrawRequest(GC gc, int x, int y, int w, int h) {
   int startLine = fTextWidget.getLineIndex(y);
   int endLine = fTextWidget.getLineIndex(y + h - 1);
   if (startLine <= endLine && startLine < fTextWidget.getLineCount()) {
     if (fIsAdvancedGraphicsPresent) {
       int alpha = gc.getAlpha();
       gc.setAlpha(100);
       drawLineRange(gc, startLine, endLine, x, w);
       gc.setAlpha(alpha);
     } else drawLineRange(gc, startLine, endLine, x, w);
   }
 }
Example #5
0
  @Override
  public void paint(GC gc) {
    if (mHoverRect != null) {
      // Translate the hover rectangle (in canvas coordinates) to control
      // coordinates
      int x = mHScale.translate(mHoverRect.x);
      int y = mVScale.translate(mHoverRect.y);
      int w = mHScale.scale(mHoverRect.width);
      int h = mVScale.scale(mHoverRect.height);

      boolean hoverIsSelected = false;
      List<SelectionItem> selections = mCanvas.getSelectionManager().getSelections();
      for (SelectionItem item : selections) {
        if (mHoverRect.equals(item.getViewInfo().getSelectionRect())) {
          hoverIsSelected = true;
          break;
        }
      }

      Color stroke = hoverIsSelected ? mHoverSelectStrokeColor : mHoverStrokeColor;
      Color fill = hoverIsSelected ? mHoverSelectFillColor : mHoverFillColor;

      if (stroke != null) {
        int oldAlpha = gc.getAlpha();
        gc.setForeground(stroke);
        gc.setLineStyle(hoverIsSelected ? HOVER_SELECTION.getLineStyle() : HOVER.getLineStyle());
        gc.setAlpha(hoverIsSelected ? HOVER_SELECTION.getStrokeAlpha() : HOVER.getStrokeAlpha());
        gc.drawRectangle(x, y, w, h);
        gc.setAlpha(oldAlpha);
      }

      if (fill != null) {
        int oldAlpha = gc.getAlpha();
        gc.setAlpha(hoverIsSelected ? HOVER_SELECTION.getFillAlpha() : HOVER.getFillAlpha());
        gc.setBackground(fill);
        gc.fillRectangle(x, y, w, h);
        gc.setAlpha(oldAlpha);
      }
    }
  }
  @Override
  public boolean paint(GC gc, int pass) {
    initColors();

    Map<Area, Double> fill;
    synchronized (this) {
      fill = new HashMap<>(trafficDensity.getRoadSegmentFill());
    }
    int oldAlpha = gc.getAlpha();
    gc.setAlpha(ALPHA);
    for (Map.Entry<Area, Double> entry : fill.entrySet()) {
      int colorIdx = Math.min((int) Math.round(entry.getValue() * COLOR_STEPS), COLOR_STEPS - 1);
      if (colorIdx > 0) {
        gc.setBackground(colors[colorIdx]);
        gc.fillRectangle(entry.getKey().getDrawArea());
      }
    }
    gc.setAlpha(oldAlpha);
    mustRedraw.set(false);
    return false;
  }
  private void setAlpha(GC gc, double fade) {

    if (gc.getAlpha() == fade) return;
    if (fade >= 255) gc.setAlpha(255);
    else gc.setAlpha((int) fade);
  }
Example #8
0
  private void paintCanvas(GC gc, int px, int py, int pw, int ph) {
    if (canvas == null || canvas.isDisposed()) return;

    Rectangle r = canvas.getBounds();
    Color b1 = canvas.getBackground();

    gc.setAntialias(SWT.ON);
    gc.setTextAntialias(SWT.ON);

    // Draw selection background:
    if (isSelected()) {
      gc.setBackground(selectedBackground);
      gc.fillRoundRectangle(0, 0, r.width, r.height, CORNER, CORNER);
    } else if (showingHover) {
      int oldAlpha = gc.getAlpha();
      gc.setAlpha(96 * oldAlpha / 255);
      gc.setBackground(selectedBackground);
      gc.fillRoundRectangle(0, 0, r.width, r.height, CORNER, CORNER);
      gc.setAlpha(oldAlpha);
    }

    // Draw edit indicator:
    if (showingEditorHover) {
      int oldAlpha = gc.getAlpha();
      gc.setAlpha(192 * oldAlpha / 255);
      gc.setBackground(b1);
      gc.fillRoundRectangle(
          r.width / 2 + HOVER_SHRINK_H,
          HOVER_SHRINK_V,
          r.width / 2 - HOVER_SHRINK_H - HOVER_SHRINK_H,
          r.height - HOVER_SHRINK_V - HOVER_SHRINK_V,
          HOVER_CORNER,
          HOVER_CORNER);
      gc.setAlpha(oldAlpha);
      //
      //            Control ec = editor == null ? null : editor.getControl();
      //            if (ec != null && !ec.isDisposed()) {
      //                Rectangle eb = ec.getBounds();
      //                gc.setAlpha(192);
      //                gc.setBackground(b1);
      //                gc.fillRoundRectangle(eb.x, eb.y, eb.width, eb.height, 8, 8);
      //                gc.setAlpha(255);
      //            }
    }

    // Draw property name label:
    gc.setForeground(canvas.getForeground());
    gc.setFont(canvas.getFont());
    if (nameLayout != null && !nameLayout.isDisposed()) {
      Rectangle nb = nameLayout.getBounds();
      nameLayout.draw(gc, MARGIN_H, (r.height - nb.height) / 2);
    }

    // Draw property value repsentating image:
    int right = r.width - MARGIN_H;
    if (valueImage != null && !valueImage.isDisposed()) {
      Rectangle ib = valueImage.getBounds();
      int iw, ih;
      if (ib.height > r.height) {
        ih = r.height;
        iw = ib.width * r.height / ib.height;
      } else {
        ih = ib.height;
        iw = ib.width;
      }
      gc.drawImage(valueImage, 0, 0, ib.width, ib.height, right - iw, (r.height - ih) / 2, iw, ih);
      right = right - iw - 3;
    }

    // Draw property value label:
    if (valueLayout != null && !valueLayout.isDisposed()) {
      Rectangle vb = valueLayout.getBounds();
      valueLayout.draw(gc, right - vb.width, (r.height - vb.height) / 2);
    }
  }
Example #9
0
  /**
   * Paints the preview at the given x/y position
   *
   * @param gc the graphics context to paint it into
   * @param x the x coordinate to paint the preview at
   * @param y the y coordinate to paint the preview at
   */
  void paint(GC gc, int x, int y) {
    mTitleHeight = paintTitle(gc, x, y, true /*showFile*/);
    y += mTitleHeight;
    y += 2;

    int width = getWidth();
    int height = getHeight();
    if (mThumbnail != null && mError == null) {
      gc.drawImage(mThumbnail, x, y);

      if (mActive) {
        int oldWidth = gc.getLineWidth();
        gc.setLineWidth(3);
        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION));
        gc.drawRectangle(x - 1, y - 1, width + 2, height + 2);
        gc.setLineWidth(oldWidth);
      }
    } else if (mError != null) {
      if (mThumbnail != null) {
        gc.drawImage(mThumbnail, x, y);
      } else {
        gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BORDER));
        gc.drawRectangle(x, y, width, height);
      }

      gc.setClipping(x, y, width, height);
      Image icon = IconFactory.getInstance().getIcon("renderError"); // $NON-NLS-1$
      ImageData data = icon.getImageData();
      int prevAlpha = gc.getAlpha();
      int alpha = 96;
      if (mThumbnail != null) {
        alpha -= 32;
      }
      gc.setAlpha(alpha);
      gc.drawImage(icon, x + (width - data.width) / 2, y + (height - data.height) / 2);

      String msg = mError;
      Density density = mConfiguration.getDensity();
      if (density == Density.TV || density == Density.LOW) {
        msg =
            "Broken rendering library; unsupported DPI. Try using the SDK manager "
                + "to get updated layout libraries.";
      }
      int charWidth = gc.getFontMetrics().getAverageCharWidth();
      int charsPerLine = (width - 10) / charWidth;
      msg = SdkUtils.wrap(msg, charsPerLine, null);
      gc.setAlpha(255);
      gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_BLACK));
      gc.drawText(msg, x + 5, y + HEADER_HEIGHT, true);
      gc.setAlpha(prevAlpha);
      gc.setClipping((Region) null);
    } else {
      gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BORDER));
      gc.drawRectangle(x, y, width, height);

      Image icon = IconFactory.getInstance().getIcon("refreshPreview"); // $NON-NLS-1$
      ImageData data = icon.getImageData();
      int prevAlpha = gc.getAlpha();
      gc.setAlpha(96);
      gc.drawImage(icon, x + (width - data.width) / 2, y + (height - data.height) / 2);
      gc.setAlpha(prevAlpha);
    }

    if (mActive) {
      int left = x;
      int prevAlpha = gc.getAlpha();
      gc.setAlpha(208);
      Color bg = mCanvas.getDisplay().getSystemColor(SWT.COLOR_WHITE);
      gc.setBackground(bg);
      gc.fillRectangle(left, y, x + width - left, HEADER_HEIGHT);
      gc.setAlpha(prevAlpha);

      y += 2;

      // Paint icons
      gc.drawImage(CLOSE_ICON, left, y);
      left += CLOSE_ICON_WIDTH;

      gc.drawImage(ZOOM_IN_ICON, left, y);
      left += ZOOM_IN_ICON_WIDTH;

      gc.drawImage(ZOOM_OUT_ICON, left, y);
      left += ZOOM_OUT_ICON_WIDTH;

      gc.drawImage(EDIT_ICON, left, y);
      left += EDIT_ICON_WIDTH;
    }
  }