public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER); styledText.setText(text); FontData data = display.getSystemFont().getFontData()[0]; Font font = new Font(display, data.getName(), 16, SWT.BOLD); styledText.setFont(font); styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE)); styledText.addListener( SWT.Resize, new Listener() { public void handleEvent(Event event) { Rectangle rect = styledText.getClientArea(); Image newImage = new Image(display, 1, Math.max(1, rect.height)); GC gc = new GC(newImage); gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE)); gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW)); gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true); gc.dispose(); styledText.setBackgroundImage(newImage); if (oldImage != null) oldImage.dispose(); oldImage = newImage; } }); shell.setSize(700, 400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } if (oldImage != null) oldImage.dispose(); font.dispose(); display.dispose(); }
/** * Sets the font that the receiver will use to paint textual information for the specified cell in * this item to the font specified by the argument, or to the default font for that kind of * control if the argument is null. * * @param index the column index * @param font the new font (or null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed * </ul> * * @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.0 */ public void setFont(int index, Font font) { checkWidget(); if (font != null && font.isDisposed()) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } int count = Math.max(1, parent.getColumnCount()); if (0 > index || index > count - 1) return; if (cellFont == null) { if (font == null) return; cellFont = new Font[count]; } Font oldFont = cellFont[index]; if (oldFont == font) return; cellFont[index] = font; if (oldFont != null && oldFont.equals(font)) return; int modelIndex = parent.columnCount == 0 ? Table.FIRST_COLUMN : parent.columns[index].modelIndex; int /*long*/ fontHandle = font != null ? font.handle : 0; OS.gtk_list_store_set(parent.modelHandle, handle, modelIndex + Table.CELL_FONT, fontHandle, -1); /* * Bug in GTK. When using fixed-height-mode, * row changes do not cause the row to be repainted. The fix is to * invalidate the row when it is cleared. */ if ((parent.style & SWT.VIRTUAL) != 0) { if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2) && OS.GTK_VERSION < OS.VERSION(2, 6, 3)) { redraw(); } } cached = true; if (font != null) { boolean customDraw = (parent.columnCount == 0) ? parent.firstCustomDraw : parent.columns[index].customDraw; if (!customDraw) { if ((parent.style & SWT.VIRTUAL) == 0) { int /*long*/ parentHandle = parent.handle; int /*long*/ column = 0; if (parent.columnCount > 0) { column = parent.columns[index].handle; } else { column = OS.gtk_tree_view_get_column(parentHandle, index); } if (column == 0) return; int /*long*/ textRenderer = parent.getTextRenderer(column); int /*long*/ imageRenderer = parent.getPixbufRenderer(column); OS.gtk_tree_view_column_set_cell_data_func( column, textRenderer, display.cellDataProc, parentHandle, 0); OS.gtk_tree_view_column_set_cell_data_func( column, imageRenderer, display.cellDataProc, parentHandle, 0); } if (parent.columnCount == 0) { parent.firstCustomDraw = true; } else { parent.columns[index].customDraw = true; } } } }
LRESULT WM_SETFONT(int /*long*/ wParam, int /*long*/ lParam) { if (OS.COMCTL32_MAJOR < 6) { layout.setFont(Font.win32_new(display, wParam)); } if (lParam != 0) OS.InvalidateRect(handle, null, true); return super.WM_SETFONT(font = wParam, lParam); }
/** * Sets the font that the receiver will use to paint textual information to the font specified by * the argument, or to the default font for that kind of control if the argument is null. * * @param font the new font (or null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the font has been disposed * </ul> * * @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> */ public void setFont(Font font) { checkWidget(); if (font != null && font.isDisposed()) { error(SWT.ERROR_INVALID_ARGUMENT); } this.font = font; }
/** * Initializes any internal resources needed by the device. * * <p>This method is called after <code>create</code>. * * <p>If subclasses reimplement this method, they must call the <code>super</code> implementation. * * @see #create */ protected void init() { /* Create the standard colors */ colors = new Color[SWT.COLOR_DARK_GRAY + 1]; colors[SWT.COLOR_BLACK] = colors[0] = Color.wpf_new(this, OS.Colors_Black()); colors[SWT.COLOR_DARK_RED] = Color.wpf_new(this, OS.Colors_Maroon()); colors[SWT.COLOR_DARK_GREEN] = Color.wpf_new(this, OS.Colors_Green()); colors[SWT.COLOR_DARK_YELLOW] = Color.wpf_new(this, OS.Colors_Olive()); colors[SWT.COLOR_DARK_BLUE] = Color.wpf_new(this, OS.Colors_Navy()); colors[SWT.COLOR_DARK_MAGENTA] = Color.wpf_new(this, OS.Colors_Purple()); colors[SWT.COLOR_DARK_CYAN] = Color.wpf_new(this, OS.Colors_Teal()); colors[SWT.COLOR_GRAY] = Color.wpf_new(this, OS.Colors_Silver()); colors[SWT.COLOR_DARK_GRAY] = Color.wpf_new(this, OS.Colors_Silver()); colors[SWT.COLOR_RED] = Color.wpf_new(this, OS.Colors_Red()); colors[SWT.COLOR_GREEN] = Color.wpf_new(this, OS.Colors_Lime()); colors[SWT.COLOR_YELLOW] = Color.wpf_new(this, OS.Colors_Yellow()); colors[SWT.COLOR_BLUE] = Color.wpf_new(this, OS.Colors_Blue()); colors[SWT.COLOR_MAGENTA] = Color.wpf_new(this, OS.Colors_Magenta()); colors[SWT.COLOR_CYAN] = Color.wpf_new(this, OS.Colors_Cyan()); colors[SWT.COLOR_WHITE] = Color.wpf_new(this, OS.Colors_White()); /* Initialize the system font slot */ int fontFamily = OS.SystemFonts_MessageFontFamily(); int style = OS.SystemFonts_MessageFontStyle(); int weight = OS.SystemFonts_MessageFontWeight(); double size = OS.SystemFonts_MessageFontSize(); int typeface = OS.gcnew_Typeface(fontFamily, style, weight, OS.FontStretches_Normal); OS.GCHandle_Free(fontFamily); OS.GCHandle_Free(style); OS.GCHandle_Free(weight); systemFont = Font.wpf_new(this, typeface, size); }
private static void paintImage2(GC gc, Point size, int f) { gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gc.fillRectangle(0, 0, size.x, size.y); // Scale line width, corner roundness, and font size. // Caveat: line width expands in all directions, so the origin also has to move. gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_LIST_SELECTION)); gc.fillRoundRectangle(f / 2, f / 2, size.x - f, size.y - f, 10 * f, 10 * f); gc.setBackground(gc.getDevice().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); gc.setLineWidth(f); gc.drawRoundRectangle(f / 2, f / 2, size.x - f, size.y - f, 10 * f, 10 * f); FontData fontData = gc.getFont().getFontData()[0]; fontData.setHeight(fontData.getHeight() * f); Font font = new Font(gc.getDevice(), fontData); try { gc.setFont(font); gc.drawText(fontData.toString(), 10 * f, 10 * f, true); } finally { font.dispose(); } }
/** * Sets the font that the receiver will use to paint textual information for this item to the font * specified by the argument, or to the default font for that kind of control if the argument is * null. * * @param font the new font (or null) * @exception IllegalArgumentException * <ul> * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed * </ul> * * @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.0 */ public void setFont(Font font) { checkWidget(); if (font != null && font.isDisposed()) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } Font oldFont = this.font; if (oldFont == font) return; this.font = font; if (oldFont != null && oldFont.equals(font)) return; int /*long*/ fontHandle = font != null ? font.handle : 0; OS.gtk_list_store_set(parent.modelHandle, handle, Table.FONT_COLUMN, fontHandle, -1); /* * Bug in GTK. When using fixed-height-mode, * row changes do not cause the row to be repainted. The fix is to * invalidate the row when it is cleared. */ if ((parent.style & SWT.VIRTUAL) != 0) { if (OS.GTK_VERSION >= OS.VERSION(2, 3, 2) && OS.GTK_VERSION < OS.VERSION(2, 6, 3)) { redraw(); } } cached = true; }
/** * Releases any internal resources back to the operating system and clears all fields except the * device handle. * * <p>When a device is destroyed, resources that were acquired on behalf of the programmer need to * be returned to the operating system. For example, if the device allocated a font to be used as * the system font, this font would be freed in <code>release</code>. Also,to assist the garbage * collector and minimize the amount of memory that is not reclaimed when the programmer keeps a * reference to a disposed device, all fields except the handle are zero'd. The handle is needed * by <code>destroy</code>. This method is called before <code>destroy</code>. * * <p>If subclasses reimplement this method, they must call the <code>super</code> implementation. * * @see #dispose * @see #destroy */ protected void release() { for (int i = 0; i < colors.length; i++) { if (colors[i] != null) colors[i].dispose(); } colors = null; if (systemFont != null) systemFont.dispose(); systemFont = null; if (tracking) { synchronized (trackingLock) { if (TRACK & objects != null) { for (int i = 0; i < objects.length; i++) { if (objects[i] != null) { System.err.println(objects[i]); errors[i].printStackTrace(); } } } } } }
/** * 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(); }
@Override void setFontDescription(long /*int*/ font) { super.setFontDescription(font); layout.setFont(Font.gtk_new(display, font)); }
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(); }