예제 #1
0
 /**
  * Set the label's margins, in pixels.
  *
  * @param leftMargin the left margin.
  * @param topMargin the top margin.
  * @param rightMargin the right margin.
  * @param bottomMargin the bottom margin.
  * @exception SWTException
  *     <ul>
  *       <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  *       <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
  *     </ul>
  *
  * @since 3.6
  */
 public void setMargins(int leftMargin, int topMargin, int rightMargin, int bottomMargin) {
   checkWidget();
   this.leftMargin = Math.max(0, leftMargin);
   this.topMargin = Math.max(0, topMargin);
   this.rightMargin = Math.max(0, rightMargin);
   this.bottomMargin = Math.max(0, bottomMargin);
   redraw();
 }
예제 #2
0
  /** Compute the minimum size. */
  private Point getTotalSize(Image image, String text) {
    Point size = new Point(0, 0);

    if (image != null) {
      Rectangle r = image.getBounds();
      size.x += r.width;
      size.y += r.height;
    }

    GC gc = new GC(this);
    if (text != null && text.length() > 0) {
      Point e = gc.textExtent(text, DRAW_FLAGS);
      size.x += e.x;
      size.y = Math.max(size.y, e.y);
      if (image != null) size.x += GAP;
    } else {
      size.y = Math.max(size.y, gc.getFontMetrics().getHeight());
    }
    gc.dispose();

    return size;
  }
예제 #3
0
 String parse(String string) {
   int length = string.length();
   offsets = new Point[length / 4];
   ids = new String[length / 4];
   mnemonics = new int[length / 4 + 1];
   StringBuffer result = new StringBuffer();
   char[] buffer = new char[length];
   string.getChars(0, string.length(), buffer, 0);
   int index = 0, state = 0, linkIndex = 0;
   int start = 0, tagStart = 0, linkStart = 0, endtagStart = 0, refStart = 0;
   while (index < length) {
     char c = Character.toLowerCase(buffer[index]);
     switch (state) {
       case 0:
         if (c == '<') {
           tagStart = index;
           state++;
         }
         break;
       case 1:
         if (c == 'a') state++;
         break;
       case 2:
         switch (c) {
           case 'h':
             state = 7;
             break;
           case '>':
             linkStart = index + 1;
             state++;
             break;
           default:
             if (Character.isWhitespace(c)) break;
             else state = 13;
         }
         break;
       case 3:
         if (c == '<') {
           endtagStart = index;
           state++;
         }
         break;
       case 4:
         state = c == '/' ? state + 1 : 3;
         break;
       case 5:
         state = c == 'a' ? state + 1 : 3;
         break;
       case 6:
         if (c == '>') {
           mnemonics[linkIndex] = parseMnemonics(buffer, start, tagStart, result);
           int offset = result.length();
           parseMnemonics(buffer, linkStart, endtagStart, result);
           offsets[linkIndex] = new Point(offset, result.length() - 1);
           if (ids[linkIndex] == null) {
             ids[linkIndex] = new String(buffer, linkStart, endtagStart - linkStart);
           }
           linkIndex++;
           start = tagStart = linkStart = endtagStart = refStart = index + 1;
           state = 0;
         } else {
           state = 3;
         }
         break;
       case 7:
         state = c == 'r' ? state + 1 : 0;
         break;
       case 8:
         state = c == 'e' ? state + 1 : 0;
         break;
       case 9:
         state = c == 'f' ? state + 1 : 0;
         break;
       case 10:
         state = c == '=' ? state + 1 : 0;
         break;
       case 11:
         if (c == '"') {
           state++;
           refStart = index + 1;
         } else {
           state = 0;
         }
         break;
       case 12:
         if (c == '"') {
           ids[linkIndex] = new String(buffer, refStart, index - refStart);
           state = 2;
         }
         break;
       case 13:
         if (Character.isWhitespace(c)) {
           state = 0;
         } else if (c == '=') {
           state++;
         }
         break;
       case 14:
         state = c == '"' ? state + 1 : 0;
         break;
       case 15:
         if (c == '"') state = 2;
         break;
       default:
         state = 0;
         break;
     }
     index++;
   }
   if (start < length) {
     int tmp = parseMnemonics(buffer, start, tagStart, result);
     int mnemonic = parseMnemonics(buffer, Math.max(tagStart, linkStart), length, result);
     if (mnemonic == -1) mnemonic = tmp;
     mnemonics[linkIndex] = mnemonic;
   } else {
     mnemonics[linkIndex] = -1;
   }
   if (offsets.length != linkIndex) {
     Point[] newOffsets = new Point[linkIndex];
     System.arraycopy(offsets, 0, newOffsets, 0, linkIndex);
     offsets = newOffsets;
     String[] newIDs = new String[linkIndex];
     System.arraycopy(ids, 0, newIDs, 0, linkIndex);
     ids = newIDs;
     int[] newMnemonics = new int[linkIndex + 1];
     System.arraycopy(mnemonics, 0, newMnemonics, 0, linkIndex + 1);
     mnemonics = newMnemonics;
   }
   return result.toString();
 }
예제 #4
0
 void keyDown(Event event) {
   if (row == null) return;
   switch (event.character) {
     case SWT.CR:
       notifyListeners(SWT.DefaultSelection, new Event());
       return;
   }
   int rowIndex = table.indexOf(row);
   int columnIndex = column == null ? 0 : table.indexOf(column);
   switch (event.keyCode) {
     case SWT.ARROW_UP:
       setRowColumn(Math.max(0, rowIndex - 1), columnIndex, true);
       break;
     case SWT.ARROW_DOWN:
       setRowColumn(Math.min(rowIndex + 1, table.getItemCount() - 1), columnIndex, true);
       break;
     case SWT.ARROW_LEFT:
     case SWT.ARROW_RIGHT:
       {
         int columnCount = table.getColumnCount();
         if (columnCount == 0) break;
         int[] order = table.getColumnOrder();
         int index = 0;
         while (index < order.length) {
           if (order[index] == columnIndex) break;
           index++;
         }
         if (index == order.length) index = 0;
         int leadKey = (getStyle() & SWT.RIGHT_TO_LEFT) != 0 ? SWT.ARROW_RIGHT : SWT.ARROW_LEFT;
         if (event.keyCode == leadKey) {
           setRowColumn(rowIndex, order[Math.max(0, index - 1)], true);
         } else {
           setRowColumn(rowIndex, order[Math.min(columnCount - 1, index + 1)], true);
         }
         break;
       }
     case SWT.HOME:
       setRowColumn(0, columnIndex, true);
       break;
     case SWT.END:
       {
         int i = table.getItemCount() - 1;
         setRowColumn(i, columnIndex, true);
         break;
       }
     case SWT.PAGE_UP:
       {
         int index = table.getTopIndex();
         if (index == rowIndex) {
           Rectangle rect = table.getClientArea();
           TableItem item = table.getItem(index);
           Rectangle itemRect = item.getBounds(0);
           rect.height -= itemRect.y;
           int height = table.getItemHeight();
           int page = Math.max(1, rect.height / height);
           index = Math.max(0, index - page + 1);
         }
         setRowColumn(index, columnIndex, true);
         break;
       }
     case SWT.PAGE_DOWN:
       {
         int index = table.getTopIndex();
         Rectangle rect = table.getClientArea();
         TableItem item = table.getItem(index);
         Rectangle itemRect = item.getBounds(0);
         rect.height -= itemRect.y;
         int height = table.getItemHeight();
         int page = Math.max(1, rect.height / height);
         int end = table.getItemCount() - 1;
         index = Math.min(end, index + page - 1);
         if (index == rowIndex) {
           index = Math.min(end, index + page - 1);
         }
         setRowColumn(index, columnIndex, true);
         break;
       }
   }
 }
예제 #5
0
  void onPaint(PaintEvent event) {
    Rectangle rect = getClientArea();
    if (rect.width == 0 || rect.height == 0) return;

    boolean shortenText = false;
    String t = text;
    Image img = image;
    int availableWidth = Math.max(0, rect.width - (leftMargin + rightMargin));
    Point extent = getTotalSize(img, t);
    if (extent.x > availableWidth) {
      img = null;
      extent = getTotalSize(img, t);
      if (extent.x > availableWidth) {
        shortenText = true;
      }
    }

    GC gc = event.gc;
    String[] lines = text == null ? null : splitString(text);

    // shorten the text
    if (shortenText) {
      extent.x = 0;
      for (int i = 0; i < lines.length; i++) {
        Point e = gc.textExtent(lines[i], DRAW_FLAGS);
        if (e.x > availableWidth) {
          lines[i] = shortenText(gc, lines[i], availableWidth);
          extent.x = Math.max(extent.x, getTotalSize(null, lines[i]).x);
        } else {
          extent.x = Math.max(extent.x, e.x);
        }
      }
      if (appToolTipText == null) {
        super.setToolTipText(text);
      }
    } else {
      super.setToolTipText(appToolTipText);
    }

    // determine horizontal position
    int x = rect.x + leftMargin;
    if (align == SWT.CENTER) {
      x = (rect.width - extent.x) / 2;
    }
    if (align == SWT.RIGHT) {
      x = rect.width - rightMargin - extent.x;
    }

    // draw a background image behind the text
    try {
      if (backgroundImage != null) {
        // draw a background image behind the text
        Rectangle imageRect = backgroundImage.getBounds();
        // tile image to fill space
        gc.setBackground(getBackground());
        gc.fillRectangle(rect);
        int xPos = 0;
        while (xPos < rect.width) {
          int yPos = 0;
          while (yPos < rect.height) {
            gc.drawImage(backgroundImage, xPos, yPos);
            yPos += imageRect.height;
          }
          xPos += imageRect.width;
        }
      } else if (gradientColors != null) {
        // draw a gradient behind the text
        final Color oldBackground = gc.getBackground();
        if (gradientColors.length == 1) {
          if (gradientColors[0] != null) gc.setBackground(gradientColors[0]);
          gc.fillRectangle(0, 0, rect.width, rect.height);
        } else {
          final Color oldForeground = gc.getForeground();
          Color lastColor = gradientColors[0];
          if (lastColor == null) lastColor = oldBackground;
          int pos = 0;
          for (int i = 0; i < gradientPercents.length; ++i) {
            gc.setForeground(lastColor);
            lastColor = gradientColors[i + 1];
            if (lastColor == null) lastColor = oldBackground;
            gc.setBackground(lastColor);
            if (gradientVertical) {
              final int gradientHeight = (gradientPercents[i] * rect.height / 100) - pos;
              gc.fillGradientRectangle(0, pos, rect.width, gradientHeight, true);
              pos += gradientHeight;
            } else {
              final int gradientWidth = (gradientPercents[i] * rect.width / 100) - pos;
              gc.fillGradientRectangle(pos, 0, gradientWidth, rect.height, false);
              pos += gradientWidth;
            }
          }
          if (gradientVertical && pos < rect.height) {
            gc.setBackground(getBackground());
            gc.fillRectangle(0, pos, rect.width, rect.height - pos);
          }
          if (!gradientVertical && pos < rect.width) {
            gc.setBackground(getBackground());
            gc.fillRectangle(pos, 0, rect.width - pos, rect.height);
          }
          gc.setForeground(oldForeground);
        }
        gc.setBackground(oldBackground);
      } else {
        if (background != null || (getStyle() & SWT.DOUBLE_BUFFERED) == 0) {
          gc.setBackground(getBackground());
          gc.fillRectangle(rect);
        }
      }
    } catch (SWTException e) {
      if ((getStyle() & SWT.DOUBLE_BUFFERED) == 0) {
        gc.setBackground(getBackground());
        gc.fillRectangle(rect);
      }
    }

    // draw border
    int style = getStyle();
    if ((style & SWT.SHADOW_IN) != 0 || (style & SWT.SHADOW_OUT) != 0) {
      paintBorder(gc, rect);
    }

    /*
     * Compute text height and image height. If image height is more than
     * the text height, draw image starting from top margin. Else draw text
     * starting from top margin.
     */
    Rectangle imageRect = null;
    int lineHeight = 0, textHeight = 0, imageHeight = 0;

    if (img != null) {
      imageRect = img.getBounds();
      imageHeight = imageRect.height;
    }
    if (lines != null) {
      lineHeight = gc.getFontMetrics().getHeight();
      textHeight = lines.length * lineHeight;
    }

    int imageY = 0, midPoint = 0, lineY = 0;
    if (imageHeight > textHeight) {
      if (topMargin == DEFAULT_MARGIN && bottomMargin == DEFAULT_MARGIN)
        imageY = rect.y + (rect.height - imageHeight) / 2;
      else imageY = topMargin;
      midPoint = imageY + imageHeight / 2;
      lineY = midPoint - textHeight / 2;
    } else {
      if (topMargin == DEFAULT_MARGIN && bottomMargin == DEFAULT_MARGIN)
        lineY = rect.y + (rect.height - textHeight) / 2;
      else lineY = topMargin;
      midPoint = lineY + textHeight / 2;
      imageY = midPoint - imageHeight / 2;
    }

    // draw the image
    if (img != null) {
      gc.drawImage(
          img, 0, 0, imageRect.width, imageHeight, x, imageY, imageRect.width, imageHeight);
      x += imageRect.width + GAP;
      extent.x -= imageRect.width + GAP;
    }

    // draw the text
    if (lines != null) {
      gc.setForeground(getForeground());
      for (int i = 0; i < lines.length; i++) {
        int lineX = x;
        if (lines.length > 1) {
          if (align == SWT.CENTER) {
            int lineWidth = gc.textExtent(lines[i], DRAW_FLAGS).x;
            lineX = x + Math.max(0, (extent.x - lineWidth) / 2);
          }
          if (align == SWT.RIGHT) {
            int lineWidth = gc.textExtent(lines[i], DRAW_FLAGS).x;
            lineX = Math.max(x, rect.x + rect.width - rightMargin - lineWidth);
          }
        }
        gc.drawText(lines[i], lineX, lineY, DRAW_FLAGS);
        lineY += lineHeight;
      }
    }
  }