void drawWidget(GC gc, RECT rect) { drawBackground(gc.handle, rect); int selStart = selection.x; int selEnd = selection.y; if (selStart > selEnd) { selStart = selection.y; selEnd = selection.x; } // temporary code to disable text selection selStart = selEnd = -1; if (!OS.IsWindowEnabled(handle)) gc.setForeground(disabledColor); layout.draw(gc, 0, 0, selStart, selEnd, null, null); if (hasFocus() && focusIndex != -1) { Rectangle[] rects = getRectangles(focusIndex); for (int i = 0; i < rects.length; i++) { Rectangle rectangle = rects[i]; gc.drawFocus(rectangle.x, rectangle.y, rectangle.width, rectangle.height); } } if (hooks(SWT.Paint) || filters(SWT.Paint)) { Event event = new Event(); event.gc = gc; event.x = rect.left; event.y = rect.top; event.width = rect.right - rect.left; event.height = rect.bottom - rect.top; sendEvent(SWT.Paint, event); event.gc = null; } }
/** * Shorten the given text <code>t</code> so that its length doesn't exceed the given width. The * default implementation replaces characters in the center of the original string with an * ellipsis ("..."). Override if you need a different strategy. * * @param gc the gc to use for text measurement * @param t the text to shorten * @param width the width to shorten the text to, in pixels * @return the shortened text */ protected String shortenText(GC gc, String t, int width) { if (t == null) return null; int w = gc.textExtent(ELLIPSIS, DRAW_FLAGS).x; if (width <= w) return t; int l = t.length(); int max = l / 2; int min = 0; int mid = (max + min) / 2 - 1; if (mid <= 0) return t; TextLayout layout = new TextLayout(getDisplay()); layout.setText(t); mid = validateOffset(layout, mid); while (min < mid && mid < max) { String s1 = t.substring(0, mid); String s2 = t.substring(validateOffset(layout, l - mid), l); int l1 = gc.textExtent(s1, DRAW_FLAGS).x; int l2 = gc.textExtent(s2, DRAW_FLAGS).x; if (l1 + w + l2 > width) { max = mid; mid = validateOffset(layout, (max + min) / 2); } else if (l1 + w + l2 < width) { min = mid; mid = validateOffset(layout, (max + min) / 2); } else { min = max; } } String result = mid == 0 ? t : t.substring(0, mid) + ELLIPSIS + t.substring(validateOffset(layout, l - mid), l); layout.dispose(); return result; }
/** Draw a rectangle in the given colors. */ private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright) { gc.setForeground(bottomright); gc.drawLine(x + w, y, x + w, y + h); gc.drawLine(x, y + h, x + w, y + h); gc.setForeground(topleft); gc.drawLine(x, y, x + w - 1, y); gc.drawLine(x, y, x, y + h - 1); }
LRESULT WM_PRINTCLIENT(int /*long*/ wParam, int /*long*/ lParam) { LRESULT result = super.WM_PRINTCLIENT(wParam, lParam); if (OS.COMCTL32_MAJOR < 6) { RECT rect = new RECT(); OS.GetClientRect(handle, rect); GCData data = new GCData(); data.device = display; data.foreground = getForegroundPixel(); GC gc = GC.win32_new(wParam, data); drawWidget(gc, rect); gc.dispose(); } return result; }
@Override void drawWidget(GC gc) { int selStart = selection.x; int selEnd = selection.y; if (selStart > selEnd) { selStart = selection.y; selEnd = selection.x; } // temporary code to disable text selection selStart = selEnd = -1; if ((state & DISABLED) != 0) gc.setForeground(disabledColor); layout.draw(gc, 0, 0, selStart, selEnd, null, null); if (hasFocus() && focusIndex != -1) { Rectangle[] rects = getRectangles(focusIndex); for (int i = 0; i < rects.length; i++) { Rectangle rect = rects[i]; gc.drawFocus(rect.x, rect.y, rect.width, rect.height); } } }
LRESULT WM_PAINT(int /*long*/ wParam, int /*long*/ lParam) { if (OS.COMCTL32_MAJOR >= 6) { return super.WM_PAINT(wParam, lParam); } PAINTSTRUCT ps = new PAINTSTRUCT(); GCData data = new GCData(); data.ps = ps; data.hwnd = handle; GC gc = new_GC(data); if (gc != null) { int width = ps.right - ps.left; int height = ps.bottom - ps.top; if (width != 0 && height != 0) { RECT rect = new RECT(); OS.SetRect(rect, ps.left, ps.top, ps.right, ps.bottom); drawWidget(gc, rect); } gc.dispose(); } return LRESULT.ZERO; }
/** 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; }
/** Paint the Label's border. */ private void paintBorder(GC gc, Rectangle r) { Display disp = getDisplay(); Color c1 = null; Color c2 = null; int style = getStyle(); if ((style & SWT.SHADOW_IN) != 0) { c1 = disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW); c2 = disp.getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW); } if ((style & SWT.SHADOW_OUT) != 0) { c1 = disp.getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW); c2 = disp.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW); } if (c1 != null && c2 != null) { gc.setLineWidth(1); drawBevelRect(gc, r.x, r.y, r.width - 1, r.height - 1, c1, c2); } }
void paint(Event event) { if (row == null) return; int columnIndex = column == null ? 0 : table.indexOf(column); GC gc = event.gc; gc.setBackground(getBackground()); gc.setForeground(getForeground()); gc.fillRectangle(event.x, event.y, event.width, event.height); int x = 0; Point size = getSize(); Image image = row.getImage(columnIndex); if (image != null) { Rectangle imageSize = image.getBounds(); int imageY = (size.y - imageSize.height) / 2; gc.drawImage(image, x, imageY); x += imageSize.width; } String text = row.getText(columnIndex); if (text.length() > 0) { Rectangle bounds = row.getBounds(columnIndex); Point extent = gc.stringExtent(text); // Temporary code - need a better way to determine table trim String platform = SWT.getPlatform(); if ("win32".equals(platform)) { // $NON-NLS-1$ if (table.getColumnCount() == 0 || columnIndex == 0) { x += 2; } else { int alignmnent = column.getAlignment(); switch (alignmnent) { case SWT.LEFT: x += 6; break; case SWT.RIGHT: x = bounds.width - extent.x - 6; break; case SWT.CENTER: x += (bounds.width - x - extent.x) / 2; break; } } } else { if (table.getColumnCount() == 0) { x += 5; } else { int alignmnent = column.getAlignment(); switch (alignmnent) { case SWT.LEFT: x += 5; break; case SWT.RIGHT: x = bounds.width - extent.x - 2; break; case SWT.CENTER: x += (bounds.width - x - extent.x) / 2 + 2; break; } } } int textY = (size.y - extent.y) / 2; gc.drawString(text, x, textY); } if (isFocusControl()) { Display display = getDisplay(); gc.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.drawFocus(0, 0, size.x, size.y); } }
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; } } }