@Override public Point computeSize(int wHint, int hHint, boolean changed) { checkWidget(); if (wHint != SWT.DEFAULT && wHint < 0) wHint = 0; if (hHint != SWT.DEFAULT && hHint < 0) hHint = 0; int width, height; int layoutWidth = layout.getWidth(); // TEMPORARY CODE if (wHint == 0) { layout.setWidth(1); Rectangle rect = layout.getBounds(); width = 0; height = rect.height; } else { layout.setWidth(wHint); Rectangle rect = layout.getBounds(); width = rect.width; height = rect.height; } layout.setWidth(layoutWidth); if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; int border = getBorderWidth(); width += border * 2; height += border * 2; return new Point(width, height); }
private void layoutControls() { Rectangle r = canvas.getBounds(); int hw = Math.max(0, (r.width - MARGIN_H) / 2 - MARGIN_H); nameLayout.setWidth(Math.max(hw, 1)); valueLayout.setWidth(Math.max(hw, 1)); if (editor != null) { Control ec = editor.getControl(); int eh = r.height - MARGIN_V - MARGIN_V; ec.setBounds(r.width - MARGIN_H - hw, (r.height - eh) / 2, hw, eh); } // if (border != null) { // border.dispose(); // border = null; // } // border = new Path(Display.getCurrent()); // float left = 0, top = 0, right = r.width, bottom = r.height; // border.moveTo(right - RADIUS, top); // border.cubicTo(right - RADIUS_BEND, top, right, top + RADIUS_BEND, // right, top + RADIUS); // border.lineTo(right, bottom - RADIUS); // border.cubicTo(right, bottom - RADIUS_BEND, right - RADIUS_BEND, // bottom, right - RADIUS, bottom); // border.lineTo(left + RADIUS, bottom); // border.cubicTo(left + RADIUS_BEND, bottom, left, bottom - RADIUS_BEND, // left, bottom - RADIUS); // border.lineTo(left, top + RADIUS); // border.cubicTo(left, top + RADIUS_BEND, left + RADIUS_BEND, top, left // + RADIUS, top); // border.close(); }
public Point computeSize(int wHint, int hHint, boolean changed) { checkWidget(); if (wHint != SWT.DEFAULT && wHint < 0) wHint = 0; if (hHint != SWT.DEFAULT && hHint < 0) hHint = 0; int width, height; if (OS.COMCTL32_MAJOR >= 6) { int /*long*/ hDC = OS.GetDC(handle); int /*long*/ newFont = OS.SendMessage(handle, OS.WM_GETFONT, 0, 0); int /*long*/ oldFont = OS.SelectObject(hDC, newFont); if (text.length() > 0) { TCHAR buffer = new TCHAR(getCodePage(), parse(text), false); RECT rect = new RECT(); int flags = OS.DT_CALCRECT | OS.DT_NOPREFIX; if (wHint != SWT.DEFAULT) { flags |= OS.DT_WORDBREAK; rect.right = wHint; } OS.DrawText(hDC, buffer, buffer.length(), rect, flags); width = rect.right - rect.left; height = rect.bottom; } else { TEXTMETRIC lptm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW() : new TEXTMETRICA(); OS.GetTextMetrics(hDC, lptm); width = 0; height = lptm.tmHeight; } if (newFont != 0) OS.SelectObject(hDC, oldFont); OS.ReleaseDC(handle, hDC); } else { int layoutWidth = layout.getWidth(); // TEMPORARY CODE if (wHint == 0) { layout.setWidth(1); Rectangle rect = layout.getBounds(); width = 0; height = rect.height; } else { layout.setWidth(wHint); Rectangle rect = layout.getBounds(); width = rect.width; height = rect.height; } layout.setWidth(layoutWidth); } if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; int border = getBorderWidth(); width += border * 2; height += border * 2; return new Point(width, height); }
@Test public void test_getLineBounds() { if (SwtTestUtil.isCocoa) { // TODO Fix Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Excluded test_getLineBonds(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)."); } return; } TextLayout layout = new TextLayout(display); String text = "0123456\n890123\n"; layout.setText(text); Rectangle rect0 = layout.getLineBounds(0); Rectangle rect1 = layout.getLineBounds(1); Rectangle rect2 = layout.getLineBounds(2); assertTrue(rect0.width > rect1.width); assertEquals(0, rect2.width); assertEquals(0, rect0.x); assertEquals(0, rect1.x); assertEquals(0, rect2.x); Rectangle bounds = layout.getBounds(); assertEquals(bounds.width, rect0.width); layout.setWidth(bounds.width); layout.setAlignment(SWT.CENTER); assertEquals(bounds, layout.getBounds()); layout.setWidth(bounds.width + 100); Rectangle newRect1 = layout.getLineBounds(0); assertEquals(50, newRect1.x); layout.setAlignment(SWT.RIGHT); newRect1 = layout.getLineBounds(0); assertEquals(100, newRect1.x); assertEquals(bounds.height, rect0.height + rect1.height + rect2.height); try { layout.getLineBounds(-1); fail("invalid range expection expected"); } catch (Exception e) { } try { layout.getLineBounds(3); fail("invalid range expection expected"); } catch (Exception e) { } layout.dispose(); }
@Override int setBounds(int x, int y, int width, int height, boolean move, boolean resize) { int result = super.setBounds(x, y, width, height, move, resize); if ((result & RESIZED) != 0) { layout.setWidth(width > 0 ? width : -1); redraw(); } return result; }
LRESULT WM_SIZE(int /*long*/ wParam, int /*long*/ lParam) { LRESULT result = super.WM_SIZE(wParam, lParam); if (OS.COMCTL32_MAJOR < 6) { RECT rect = new RECT(); OS.GetClientRect(handle, rect); layout.setWidth(rect.right > 0 ? rect.right : -1); redraw(); } return result; }
@Test public void test_getLineOffsets() { TextLayout layout = new TextLayout(display); String text = "0123456\n890123\n"; layout.setText(text); int[] offsets = layout.getLineOffsets(); int[] expected = new int[] {0, 8, 15, 15}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.setText(""); offsets = layout.getLineOffsets(); expected = new int[] {0, 0}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.setText("\n"); offsets = layout.getLineOffsets(); expected = new int[] {0, 1, 1}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.setText("WMWM"); int width = layout.getBounds().width; layout.setWidth(width / 4 + 1); offsets = layout.getLineOffsets(); expected = new int[] {0, 1, 2, 3, 4}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.setWidth(width / 2 + 1); offsets = layout.getLineOffsets(); expected = new int[] {0, 2, 4}; for (int i = 0; i < offsets.length; i++) { assertEquals(expected[i], offsets[i]); } layout.dispose(); }
private void decideShellSize(Shell newShell) { GC gc = new GC(newShell); Rectangle imgRect = getImage().getBounds(); Point extent = gc.textExtent(getTitle()); int titleWidth = imgRect.width + extent.x + 27; TextLayout layout = new TextLayout(newShell.getDisplay()); layout.setWidth(215); layout.setText(getTip()); shellWidth = Math.max(titleWidth, 215 + getLeftMargin() + getRightMargin() + 6); shellHeight = layout.getBounds().height + getTopMargin() + getBottomMargin() + Math.max(18, extent.y + 6) + 13 + btnDefault.getSize().y; gc.dispose(); layout.dispose(); }
@Test public void test_getOffset() { boolean isCocoa = SwtTestUtil.isCocoa; if (isCocoa) { // TODO Fix Cocoa failure. if (SwtTestUtil.verbose) { System.out.println( "Partially excluded test_getOffset(org.eclipse.swt.tests.junit.Test_org_eclipse_swt_graphics_TextLayout)."); } } TextLayout layout = new TextLayout(display); String text = "AB \u05E9\u05E0 CD\nHello"; layout.setText(text); int[] trailing = new int[1]; assertEquals(0, layout.getOffset(0, 0, trailing)); assertEquals(0, trailing[0]); Point point = layout.getLocation(0, true); assertEquals(1, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(1, false); assertEquals(0, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(2, true); assertEquals(4, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, true); assertEquals(2, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, false); assertEquals(3, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); Rectangle bounds = layout.getBounds(); layout.setWidth(bounds.width + 100); layout.setAlignment(SWT.CENTER); assertEquals(0, layout.getOffset(0, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(0, true); assertEquals(1, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(1, false); assertEquals(0, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(2, true); assertEquals(4, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, true); assertEquals(2, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, false); assertEquals(3, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); layout.setAlignment(SWT.RIGHT); assertEquals(0, layout.getOffset(0, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(0, true); assertEquals(1, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(0, trailing[0]); point = layout.getLocation(1, false); assertEquals(0, layout.getOffset(point.x - 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(2, true); assertEquals(4, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); point = layout.getLocation(4, true); assertEquals(2, layout.getOffset(point.x - 1, 0, trailing)); if (!isCocoa) assertEquals(1, trailing[0]); point = layout.getLocation(4, false); assertEquals(3, layout.getOffset(point.x + 1, 0, trailing)); assertEquals(1, trailing[0]); text = "Text"; layout.setText(text); int width = layout.getBounds().width; layout.setAlignment(SWT.LEFT); assertEquals(0, layout.getOffset(1, 0, null)); assertEquals(text.length() - 1, layout.getOffset(width - 1, 0, null)); layout.setWidth(width + 100); layout.setAlignment(SWT.CENTER); assertEquals(0, layout.getOffset(1 + 50, 0, null)); assertEquals(text.length() - 1, layout.getOffset(width - 1 + 50, 0, null)); layout.setAlignment(SWT.RIGHT); assertEquals(0, layout.getOffset(1 + 100, 0, null)); assertEquals(text.length() - 1, layout.getOffset(width - 1 + 100, 0, null)); layout.dispose(); }
@Test public void test_getSegments() { TextLayout layout = new TextLayout(display); layout.setText("AB"); String[] messages = { "no segments", "segments", "segments (duplicate at 0)", "segments (duplicate at 1)", "segments (duplicate at 2)" }; int[][] segments = {null, {0, 1, 2}, {0, 0, 1, 2}, {0, 1, 1, 2}, {0, 1, 2, 2}}; for (int i = 0; i < segments.length; i++) { String m = messages[i]; layout.setSegments(segments[i]); assertEquals(m, 1, layout.getNextOffset(0, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 2, layout.getNextOffset(1, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 2, layout.getNextOffset(2, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 1, layout.getPreviousOffset(2, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 0, layout.getPreviousOffset(1, SWT.MOVEMENT_CLUSTER)); assertEquals(m, 0, layout.getPreviousOffset(0, SWT.MOVEMENT_CLUSTER)); } // Bug 295513 layout.setText("Line"); layout.setAscent(20); layout.setDescent(6); layout.setSegments(new int[] {0, layout.getText().length()}); layout.getBounds(); layout.dispose(); layout = new TextLayout(display); // Bug 241482 comment 74 layout.setText("word word word"); layout.setSegments(new int[] {0, 5, 10}); int offset = 0; offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(5, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(10, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(14, offset); layout.setWidth(layout.getBounds().width / 2); layout.setAscent(20); layout.setDescent(6); offset = 0; offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(5, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(10, offset); offset = layout.getNextOffset(offset, SWT.MOVEMENT_WORD_START); assertEquals(14, offset); layout.dispose(); layout = new TextLayout(display); // Bug 241482 comment 64 layout.setText("\nAB"); layout.setSegments(new int[] {0, 1, 3}); int[] expected = new int[] {0, 1, 3}; int[] offsets = layout.getLineOffsets(); for (int i = 0; i < offsets.length; i++) { assertEquals(" i = " + i, expected[i], offsets[i]); } layout.dispose(); layout = new TextLayout(display); // Bug 241482 comment 80 layout.setText("MNMNMN"); layout.setSegments(new int[] {0, 1, 6}); layout.getBounds(); assertEquals( layout.getText().length() - 1, layout.getOffset(layout.getBounds().width, 0, null)); layout.setAscent(9); assertEquals( layout.getText().length() - 1, layout.getOffset(layout.getBounds().width, 0, null)); layout.dispose(); layout = new TextLayout(display); // Bug 486600 comment 2 layout.setText("Abcdef\nGhij\nKl"); Rectangle bounds1 = layout.getBounds(0, 6 - 1); int[] trailing = new int[1]; offset = layout.getOffset(bounds1.width, bounds1.height + 2, trailing); assertEquals(10, offset); assertEquals(1, trailing[0]); layout.dispose(); layout = new TextLayout(display); layout.setText("AbcdefGhijKl"); layout.setSegments(new int[] {6, 10}); layout.setSegmentsChars(new char[] {'\n', '\n'}); bounds1 = layout.getBounds(0, 6 - 1); trailing = new int[1]; offset = layout.getOffset(bounds1.width, bounds1.height + 2, trailing); assertEquals(9, offset); assertEquals(1, trailing[0]); layout.dispose(); layout = new TextLayout(display); // Lina's bug (bug 241482, comment 37) boolean doit = false; // known to be broken if (doit) { int length = layout.getText().length(); layout.setSegments(new int[] {length}); trailing = new int[1]; int width = layout.getBounds().width + 20; assertEquals("hit test to the left", 0, layout.getOffset(0, 0, trailing)); assertEquals("hit test to the left (trailing)", 0, trailing[0]); assertEquals("hit test to the right", length - 1, layout.getOffset(width, 0, trailing)); assertEquals("hit test to the right (trailing)", 1, trailing[0]); layout.setSegmentsChars(new char[] {'*'}); assertEquals("hit test to the left", 0, layout.getOffset(0, 0, trailing)); assertEquals("hit test to the left (trailing)", 0, trailing[0]); assertEquals("hit test to the right", length - 1, layout.getOffset(width, 0, trailing)); assertEquals("hit test to the right (trailing)", 1, trailing[0]); } /* wrong: internal testing */ // String text = "AB"; // int textLength = text.length(); // layout.setText(text); // messages = new String [] { // "no segments", // "segments", // "segments (duplicate at 0)", // "segments (duplicate at 1)", // "segments (duplicate at 2)" }; // segments = new int [][] { // null, // { 0, 1, 2 }, // { 0, 0, 1, 2 }, // { 0, 1, 1, 2 }, // { 0, 1, 2, 2 } }; // int[][] translatedOffsets = { // { 0, 1, 2 }, // { 1, 3, 5 }, // { 2, 4, 6 }, // { 1, 4, 6 }, // { 1, 3, 6 } }; // int[][] untranslatedOffsets = { // { 0, 1, 2 }, // { 0, 0, 1, 1, 2, 2 }, // { 0, 0, 0, 1, 1, 2, 2 }, // { 0, 0, 1, 1, 1, 2, 2 }, // { 0, 0, 1, 1, 2, 2, 2 } }; // for (int i = 0; i < segments.length; i++) { // layout.setSegments(segments[i]); // layout.getBounds(); // for (int j = 0; j <= textLength; j++) { // assertEquals(messages[i] + " j = " + j, translatedOffsets[i][j], // layout.translateOffset(j)); // } // for (int j = 0, n = layout.getSegments() == null ? 0 : textLength + // layout.getSegments().length; j < n; j++) { // assertEquals(messages[i] + " j = " + j, untranslatedOffsets[i][j], // layout.untranslateOffset(j)); // } // } layout.dispose(); }