Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelipe Heidrich2006-07-20 19:43:40 +0000
committerFelipe Heidrich2006-07-20 19:43:40 +0000
commit90e799a2f3caddd9fbb9bb038bc1d613744694a8 (patch)
tree7e95da72371a576b7883f1146c131fbf3e409f30 /bundles
parenta85637b0f04c02a186f7eb1cbfb42a7898cff198 (diff)
downloadeclipse.platform.swt-90e799a2f3caddd9fbb9bb038bc1d613744694a8.tar.gz
eclipse.platform.swt-90e799a2f3caddd9fbb9bb038bc1d613744694a8.tar.xz
eclipse.platform.swt-90e799a2f3caddd9fbb9bb038bc1d613744694a8.zip
Bug 19602 [Printing] Line numbers aren't printed
Diffstat (limited to 'bundles')
-rwxr-xr-xbundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java80
-rw-r--r--bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextPrintOptions.java9
2 files changed, 61 insertions, 28 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
index 3a7ac7354e..9b08902a36 100755
--- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledText.java
@@ -191,6 +191,7 @@ public class StyledText extends Canvas {
Point selection = null; // selected text
boolean mirrored; // indicates the printing gc should be mirrored
int lineSpacing;
+ int printMargin;
/**
* Creates an instance of <class>Printing</class>.
@@ -223,7 +224,6 @@ public class StyledText extends Canvas {
printerRenderer = new StyledTextRenderer(printer, null);
printerRenderer.setContent(copyContent(styledText.getContent()));
cacheLineData(styledText);
-
}
/**
* Caches all line data that needs to be requested from a listener.
@@ -335,6 +335,9 @@ public class StyledText extends Canvas {
}
}
lineSpacing = styledText.lineSpacing * printerDPI.y / screenDPI.y;
+ if (printOptions.printLineNumbers) {
+ printMargin = 3 * printerDPI.x / screenDPI.x;
+ }
}
/**
* Copies the text of the specified <class>StyledTextContent</class>.
@@ -432,22 +435,36 @@ public class StyledText extends Canvas {
Color foreground = gc.getForeground();
int paintY = clientArea.y;
int paintX = clientArea.x;
+ int width = clientArea.width;
int page = startPage;
int pageBottom = clientArea.y + clientArea.height;
int orientation = gc.getStyle() & (SWT.RIGHT_TO_LEFT | SWT.LEFT_TO_RIGHT);
-
-
+ TextLayout printLayout = null;
+ if (printOptions.printLineNumbers || printOptions.header != null || printOptions.footer != null) {
+ printLayout = new TextLayout(printer);
+ printLayout.setFont(printerFont);
+ }
+ if (printOptions.printLineNumbers) {
+ int count = endLine - startLine + 1;
+ StringBuffer buffer = new StringBuffer("0");
+ while ((count /= 10) > 0) buffer.append("0");
+ printLayout.setText(buffer.toString());
+ int numberingWidth = printLayout.getBounds().width + printMargin;
+ if (numberingWidth > width) numberingWidth = width;
+ paintX += numberingWidth;
+ width -= numberingWidth;
+ }
for (int i = startLine; i <= endLine && page <= endPage; i++) {
if (paintY == clientArea.y) {
printer.startPage();
- printDecoration(page, true);
+ printDecoration(page, true, printLayout);
}
- TextLayout layout = printerRenderer.getTextLayout(i, orientation, clientArea.width, lineSpacing);
+ TextLayout layout = printerRenderer.getTextLayout(i, orientation, width, lineSpacing);
Color lineBackground = printerRenderer.getLineBackground(i, background);
int paragraphBottom = paintY + layout.getBounds().height;
if (paragraphBottom <= pageBottom) {
//normal case, the whole paragraph fits in the current page
- printLine(paintX, paintY, gc, foreground, lineBackground, layout);
+ printLine(paintX, paintY, gc, foreground, lineBackground, layout, printLayout, i);
paintY = paragraphBottom;
} else {
int lineCount = layout.getLineCount();
@@ -457,31 +474,31 @@ public class StyledText extends Canvas {
}
if (lineCount == 0) {
//the whole paragraph goes to the next page
- printDecoration(page, false);
+ printDecoration(page, false, printLayout);
printer.endPage();
page++;
if (page <= endPage) {
printer.startPage();
- printDecoration(page, true);
+ printDecoration(page, true, printLayout);
paintY = clientArea.y;
- printLine(paintX, paintY, gc, foreground, lineBackground, layout);
+ printLine(paintX, paintY, gc, foreground, lineBackground, layout, printLayout, i);
paintY += layout.getBounds().height;
}
} else {
//draw paragraph top in the current page and paragraph bottom in the next
int height = paragraphBottom - paintY;
- gc.setClipping(paintX, paintY, clientArea.width, height);
- printLine(paintX, paintY, gc, foreground, lineBackground, layout);
- printDecoration(page, false);
+ gc.setClipping(paintX, paintY, width, height);
+ printLine(paintX, paintY, gc, foreground, lineBackground, layout, printLayout, i);
+ printDecoration(page, false, printLayout);
printer.endPage();
page++;
if (page <= endPage) {
printer.startPage();
- printDecoration(page, true);
+ printDecoration(page, true, printLayout);
paintY = clientArea.y - height;
int layoutHeight = layout.getBounds().height;
- gc.setClipping(paintX, clientArea.y, clientArea.width, layoutHeight - height);
- printLine(paintX, paintY, gc, foreground, lineBackground, layout);
+ gc.setClipping(paintX, clientArea.y, width, layoutHeight - height);
+ printLine(paintX, paintY, gc, foreground, lineBackground, layout, printLayout, i);
paintY += layoutHeight;
}
gc.setClipping((Rectangle)null);
@@ -491,9 +508,10 @@ public class StyledText extends Canvas {
}
if (paintY > clientArea.y) {
// close partial page
- printDecoration(page, false);
+ printDecoration(page, false, printLayout);
printer.endPage();
}
+ if (printLayout != null) printLayout.dispose();
}
/**
* Print header or footer decorations.
@@ -501,21 +519,20 @@ public class StyledText extends Canvas {
* @param page page number to print, if specified in the StyledTextPrintOptions header or footer.
* @param header true = print the header, false = print the footer
*/
- void printDecoration(int page, boolean header) {
+ void printDecoration(int page, boolean header, TextLayout layout) {
String text = header ? printOptions.header : printOptions.footer;
if (text == null) return;
int lastSegmentIndex = 0;
- int segmentCount = 3;
- for (int i = 0; i < segmentCount; i++) {
+ for (int i = 0; i < 3; i++) {
int segmentIndex = text.indexOf(StyledTextPrintOptions.SEPARATOR, lastSegmentIndex);
String segment;
if (segmentIndex == -1) {
segment = text.substring(lastSegmentIndex);
- printDecorationSegment(segment, i, page, header);
+ printDecorationSegment(segment, i, page, header, layout);
break;
} else {
segment = text.substring(lastSegmentIndex, segmentIndex);
- printDecorationSegment(segment, i, page, header);
+ printDecorationSegment(segment, i, page, header, layout);
lastSegmentIndex = segmentIndex + StyledTextPrintOptions.SEPARATOR.length();
}
}
@@ -530,7 +547,7 @@ public class StyledText extends Canvas {
* @param page page number to print, if specified in the decoration segment.
* @param header true = print the header, false = print the footer
*/
- void printDecorationSegment(String segment, int alignment, int page, boolean header) {
+ void printDecorationSegment(String segment, int alignment, int page, boolean header, TextLayout layout) {
int pageIndex = segment.indexOf(StyledTextPrintOptions.PAGE_TAG);
if (pageIndex != -1) {
int pageTagLength = StyledTextPrintOptions.PAGE_TAG.length();
@@ -540,9 +557,7 @@ public class StyledText extends Canvas {
segment = buffer.toString();
}
if (segment.length() > 0) {
- TextLayout layout = new TextLayout(printer);
layout.setText(segment);
- layout.setFont(printerFont);
int segmentWidth = layout.getBounds().width;
int segmentHeight = printerRenderer.getLineHeight();
int drawX = 0, drawY;
@@ -559,14 +574,13 @@ public class StyledText extends Canvas {
drawY = clientArea.y + clientArea.height + segmentHeight;
}
layout.draw(gc, drawX, drawY);
- layout.dispose();
}
}
- void printLine(int paintX, int paintY, GC gc, Color foreground, Color background, TextLayout layout) {
+ void printLine(int x, int y, GC gc, Color foreground, Color background, TextLayout layout, TextLayout printLayout, int index) {
if (background != null) {
Rectangle rect = layout.getBounds();
gc.setBackground(background);
- gc.fillRectangle(paintX, paintY, rect.width, rect.height);
+ gc.fillRectangle(x, y, rect.width, rect.height);
// int lineCount = layout.getLineCount();
// for (int i = 0; i < lineCount; i++) {
@@ -577,8 +591,18 @@ public class StyledText extends Canvas {
// gc.fillRectangle(rect);
// }
}
+ if (printOptions.printLineNumbers) {
+ FontMetrics metrics = layout.getLineMetrics(0);
+ printLayout.setAscent(metrics.getAscent() + metrics.getDescent());
+ printLayout.setDescent(metrics.getDescent());
+ printLayout.setText(String.valueOf(index));
+ int paintX = x - printMargin - printLayout.getBounds().width;
+ printLayout.draw(gc, paintX, y);
+ printLayout.setAscent(-1);
+ printLayout.setDescent(-1);
+ }
gc.setForeground(foreground);
- layout.draw(gc, paintX, paintY);
+ layout.draw(gc, x, y);
}
/**
* Starts a print job and prints the pages specified in the constructor.
diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextPrintOptions.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextPrintOptions.java
index 794cf0c33b..398b06ed49 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextPrintOptions.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/StyledTextPrintOptions.java
@@ -83,4 +83,13 @@ public class StyledTextPrintOptions {
* Print the line background color. Default value is <code>false</code>.
*/
public boolean printLineBackground = false;
+
+ /**
+ * Print line numbers. Default value is <code>false</code>.
+ *
+ * @since 3.3
+ */
+ public boolean printLineNumbers = false;
+
+
}

Back to the top