1 11 package org.eclipse.swt.custom; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.graphics.*; 15 16 20 class DisplayRenderer extends StyledTextRenderer { 21 private StyledText parent; private int topIndex = -1; 23 private TextLayout[] layouts; 24 25 33 DisplayRenderer(Device device, Font regularFont, StyledText parent, int tabLength) { 34 super(device, regularFont); 35 this.parent = parent; 36 calculateLineHeight(); 37 setTabLength(tabLength); 38 } 39 void dispose() { 40 super.dispose(); 41 if (layouts != null) { 42 for (int i = 0; i < layouts.length; i++) { 43 TextLayout layout = layouts[i]; 44 if (layout != null) super.disposeTextLayout(layout); 45 } 46 topIndex = -1; 47 layouts = null; 48 } 49 } 50 55 protected void disposeGC(GC gc) { 56 gc.dispose(); 57 } 58 69 protected void drawLineBreakSelection(String line, int lineOffset, int paintX, int paintY, GC gc) { 70 Point selection = parent.internalGetSelection(); 71 int lineLength = line.length(); 72 int selectionStart = Math.max(0, selection.x - lineOffset); 73 int selectionEnd = selection.y - lineOffset; 74 int lineEndSpaceWidth = getLineEndSpaceWidth(); 75 int lineHeight = getLineHeight(); 76 77 if (selectionEnd == selectionStart || selectionEnd < 0 || selectionStart > lineLength || selectionEnd <= lineLength) { 78 return; 79 } 80 81 gc.setBackground(parent.getSelectionBackground()); 82 gc.setForeground(parent.getSelectionForeground()); 83 if ((parent.getStyle() & SWT.FULL_SELECTION) != 0) { 84 Rectangle rect = getClientArea(); 85 gc.fillRectangle(paintX, paintY, rect.width - paintX, lineHeight); 86 } else { 87 boolean isWrappedLine = false; 88 if (parent.internalGetWordWrap()) { 89 StyledTextContent content = getContent(); 90 int lineEnd = lineOffset + lineLength; 91 int lineIndex = content.getLineAtOffset(lineEnd); 92 93 if (lineIndex < content.getLineCount() - 1 && 96 content.getOffsetAtLine(lineIndex + 1) == lineEnd) { 97 isWrappedLine = true; 98 } 99 } 100 if (isWrappedLine == false) { 101 gc.fillRectangle(paintX, paintY, lineEndSpaceWidth, lineHeight); 103 } 104 } 105 } 106 124 protected int[] getBidiSegments(int lineOffset, String lineText) { 125 if (!parent.isBidi()) return null; 126 return parent.getBidiSegments(lineOffset, lineText); 127 } 128 133 protected Rectangle getClientArea() { 134 return parent.getClientArea(); 135 } 136 143 protected StyledTextContent getContent() { 144 return parent.internalGetContent(); 145 } 146 154 protected GC getGC() { 155 return new GC(parent); 156 } 157 162 protected int getHorizontalPixel() { 163 return parent.internalGetHorizontalPixel(); 164 } 165 protected int getLeftMargin() { 166 return parent.leftMargin; 167 } 168 171 protected StyledTextEvent getLineBackgroundData(int lineOffset, String line) { 172 return parent.getLineBackgroundData(lineOffset, line); 173 } 174 177 protected StyledTextEvent getLineStyleData(int lineOffset, String line) { 178 StyledTextEvent logicalLineEvent = parent.getLineStyleData(lineOffset, line); 179 if (logicalLineEvent != null) { 180 logicalLineEvent = getLineStyleData(logicalLineEvent, lineOffset, line); 181 } 182 return logicalLineEvent; 183 } 184 protected int getOrientation () { 185 return parent.getOrientation(); 186 } 187 protected int getRightMargin() { 188 return parent.rightMargin; 189 } 190 protected Color getSelectionBackground() { 191 return parent.getSelectionBackground(); 192 } 193 protected Color getSelectionForeground() { 194 return parent.getSelectionForeground(); 195 } 196 199 protected Point getSelection() { 200 return parent.internalGetSelection(); 201 } 202 205 protected boolean getWordWrap() { 206 return parent.getWordWrap(); 207 } 208 211 protected boolean isFullLineSelection() { 212 return (parent.getStyle() & SWT.FULL_SELECTION) != 0; 213 } 214 TextLayout createTextLayout(int lineOffset) { 215 if (!parent.internalGetWordWrap()) { 216 int lineIndex = getContent().getLineAtOffset(lineOffset); 217 updateTopIndex(); 218 if (layouts != null) { 219 int layoutIndex = lineIndex - topIndex; 220 if (0 <= layoutIndex && layoutIndex < layouts.length) { 221 TextLayout layout = layouts[layoutIndex]; 222 if (layout != null) return layout; 223 return layouts[layoutIndex] = super.createTextLayout(lineIndex); 224 } 225 } 226 } 227 return super.createTextLayout(lineOffset); 228 } 229 void disposeTextLayout (TextLayout layout) { 230 if (layouts != null) { 231 for (int i=0; i<layouts.length; i++) { 232 if (layouts[i] == layout) return; 233 } 234 } 235 super.disposeTextLayout(layout); 236 } 237 void updateTopIndex() { 238 int verticalIncrement = parent.getVerticalIncrement(); 239 int topIndex = verticalIncrement == 0 ? 0 : parent.verticalScrollOffset / verticalIncrement; 240 int newLength = Math.max(1 , parent.getPartialBottomIndex() - topIndex + 1); 241 if (layouts == null || topIndex != this.topIndex || newLength != layouts.length) { 242 TextLayout[] newLayouts = new TextLayout[newLength]; 243 if (layouts != null) { 244 for(int i=0; i<layouts.length; i++) { 245 TextLayout layout = layouts[i]; 246 if (layout != null) { 247 int layoutIndex = (i + this.topIndex) - topIndex; 248 if (0 <= layoutIndex && layoutIndex < newLayouts.length) { 249 newLayouts[layoutIndex] = layout; 250 } else { 251 super.disposeTextLayout(layout); 252 } 253 } 254 } 255 } 256 this.topIndex = topIndex; 257 layouts = newLayouts; 258 } 259 } 260 } 261 | Popular Tags |