1 11 package org.eclipse.swt.dnd; 12 13 14 import org.eclipse.swt.custom.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.internal.win32.*; 17 18 class StyledTextDragAndDropEffect extends DragAndDropEffect { 19 StyledText text; 20 long scrollBeginTime; 21 int scrollX = -1, scrollY = -1; 22 23 static final int SCROLL_HYSTERESIS = 100; static final int SCROLL_TOLERANCE = 20; 26 StyledTextDragAndDropEffect(StyledText control) { 27 text = control; 28 } 29 void showDropTargetEffect(int effect, int x, int y) { 30 Point pt = text.getDisplay().map(null, text, x, y); 31 if ((effect & DND.FEEDBACK_SCROLL) == 0) { 32 scrollBeginTime = 0; 33 scrollX = scrollY = -1; 34 } else { 35 if (text.getCharCount() == 0) { 36 scrollBeginTime = 0; 37 scrollX = scrollY = -1; 38 } else { 39 if (scrollX != -1 && scrollY != -1 && scrollBeginTime != 0 && 40 (pt.x >= scrollX && pt.x <= (scrollX + SCROLL_TOLERANCE) || 41 pt.y >= scrollY && pt.y <= (scrollY + SCROLL_TOLERANCE))) { 42 if (System.currentTimeMillis() >= scrollBeginTime) { 43 Rectangle area = text.getClientArea(); 44 Rectangle bounds = text.getTextBounds(0, 0); 45 int charWidth = bounds.width; 46 if (pt.x < area.x + 2*charWidth) { 47 int leftPixel = text.getHorizontalPixel(); 48 text.setHorizontalPixel(leftPixel - charWidth); 49 if (text.getHorizontalPixel() != leftPixel) { 50 text.redraw(); 51 } 52 } 53 if (pt.x > area.width - 2*charWidth) { 54 int leftPixel = text.getHorizontalPixel(); 55 text.setHorizontalPixel(leftPixel + charWidth); 56 if (text.getHorizontalPixel() != leftPixel) { 57 text.redraw(); 58 } 59 } 60 int lineHeight = bounds.height; 61 if (pt.y < area.y + lineHeight) { 62 int topPixel = text.getTopPixel(); 63 text.setTopPixel(topPixel - lineHeight); 64 if (text.getTopPixel() != topPixel) { 65 text.redraw(); 66 } 67 } 68 if (pt.y > area.height - lineHeight) { 69 int topPixel = text.getTopPixel(); 70 text.setTopPixel(topPixel + lineHeight); 71 if (text.getTopPixel() != topPixel) { 72 text.redraw(); 73 } 74 } 75 scrollBeginTime = 0; 76 scrollX = scrollY = -1; 77 } 78 } else { 79 scrollBeginTime = System.currentTimeMillis() + SCROLL_HYSTERESIS; 80 scrollX = pt.x; 81 scrollY = pt.y; 82 } 83 } 84 } 85 86 if ((effect & DND.FEEDBACK_SELECT) != 0) { 87 StyledTextContent content = text.getContent(); 88 int oldOffset = text.getCaretOffset(); 89 int newOffset = -1; 90 try { 91 newOffset = text.getOffsetAtLocation(pt); 92 } catch (IllegalArgumentException ex1) { 93 int maxOffset = content.getCharCount(); 94 Point maxLocation = text.getLocationAtOffset(maxOffset); 95 if (pt.y >= maxLocation.y) { 96 try { 97 newOffset = text.getOffsetAtLocation(new Point(pt.x, maxLocation.y)); 98 } catch (IllegalArgumentException ex2) { 99 newOffset = maxOffset; 100 } 101 } else { 102 try { 103 int startOffset = text.getOffsetAtLocation(new Point(0, pt.y)); 104 int endOffset = maxOffset; 105 int line = content.getLineAtOffset(startOffset); 106 int lineCount = content.getLineCount(); 107 if (line + 1 < lineCount) { 108 endOffset = content.getOffsetAtLine(line + 1) - 1; 109 } 110 int lineHeight = text.getLineHeight(startOffset); 111 for (int i = endOffset; i >= startOffset; i--) { 112 Point p = text.getLocationAtOffset(i); 113 if (p.x < pt.x && p.y < pt.y && p.y + lineHeight > pt.y) { 114 newOffset = i; 115 break; 116 } 117 } 118 } catch (IllegalArgumentException ex2) { 119 newOffset = -1; 120 } 121 } 122 } 123 if (newOffset != -1 && newOffset != oldOffset) { 124 int line = content.getLineAtOffset(newOffset); 127 int lineOffset = content.getOffsetAtLine(line); 128 int offsetInLine = newOffset - lineOffset; 129 if (offsetInLine > content.getLine(line).length()) { 133 newOffset = Math.max(0, newOffset - 1); 134 } 135 text.setFocus(); 136 OS.ImageList_DragShowNolock(false); 137 text.setCaretOffset(newOffset); 138 OS.ImageList_DragShowNolock(true); 139 } 140 } 141 } 142 } 143 | Popular Tags |