📄 wrappedcontent.java
字号:
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IWrappedContent#wrap(int)
*/
public void wrap(int width) {
reset();
if(width <= 0)
width = 100;
GC gc = richbox.getGC();
softLineCount = 0;
int hardLineCount = unwrappedContent.getLineCount();
ensureWrappedSize(hardLineCount);
for(int i = 0; i < hardLineCount; i++) {
wrapped[i] = softLineCount;
int[] offsets = unwrappedHelper.splitLine(gc, width, i);
softLineCount += offsets.length;
int m = 0;
for(int j = wrapped[i]; j < softLineCount - 1; j++, m++)
setSoftLine(j, offsets[m], offsets[m + 1] - offsets[m]);
setSoftLine(softLineCount - 1, offsets[m], unwrappedContent.getLineStartOffset(i + 1) - offsets[m]);
}
richbox.releaseGC();
}
/**
* 初始化各个变量的值
*/
private void reset() {
softLineCount = 1;
softLines[0][0] = 0;
softLines[0][1] = 0;
wrapped[0] = 0;
helper.clearLineHeightCache();
}
/**
* 保存软行的起始和长度
*
* @param index
* 行号
* @param start
* 起始偏移
* @param length
* 长度
*/
private void setSoftLine(int index, int start, int length) {
while(index >= softLines.length)
expandLineDouble();
softLines[index] = new int[2];
softLines[index][0] = start;
softLines[index][1] = length;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#getLineStyler()
*/
public ILineStyler getLineStyler() {
return styler;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IWrappedRichContent#getHardLineIndex(int)
*/
public int getHardLineIndex(int softLineIndex) {
int low = 0;
int high = getHardLineCount() - 1;
int middle = (low + high) >>> 1;
for(; low < high; middle = (low + high) >>> 1) {
if(softLineIndex >= wrapped[middle])
low = (middle == low) ? (low + 1) : middle;
else
high = (middle == high) ? (high - 1) : middle;
}
if(softLineIndex >= wrapped[low])
return low;
else
return low - 1;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#getContentHelper()
*/
public ContentHelper getContentHelper() {
return helper;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#replaceText(edu.tsinghua.lumaqq.widgets.TextEvent)
*/
public void replaceText(TextEvent event) {
int oldHardLineCount = unwrappedContent.getLineCount();
unwrappedContent.replaceText(event);
int width = richbox.getClientArea().width - richbox.getLeftMargin() - richbox.getRightMargin();
if(width <= 0)
width = 100;
// 调整空间
int startSoftLine = wrapped[event.startLine];
int removedSoftLineCount;
if(event.startLine + event.replaceLineCount + 1 >= oldHardLineCount)
removedSoftLineCount = softLineCount - wrapped[event.startLine];
else
removedSoftLineCount = wrapped[event.startLine + event.replaceLineCount + 1] - wrapped[event.startLine];
removeWrapped(event.startLine + 1, event.replaceLineCount);
oldHardLineCount -= event.replaceLineCount;
insertWrapped(event.startLine + 1, event.newLineCount, oldHardLineCount);
// 计算软行的增减
GC gc = richbox.getGC();
int addedSoftLineCount = 0;
for(int i = event.startLine; i <= event.startLine + event.newLineCount; i++) {
int[] offsets = unwrappedHelper.splitLine(gc, width, i);
addedSoftLineCount += offsets.length;
}
int softLineDelta = addedSoftLineCount - removedSoftLineCount;
// 调整软行的空间和其他空间
removeSoftLines(startSoftLine, removedSoftLineCount);
insertSoftLines(startSoftLine, addedSoftLineCount, softLineCount);
helper.removeLineHeight(startSoftLine, removedSoftLineCount);
helper.insertLineHeight(startSoftLine, addedSoftLineCount, softLineCount);
softLineCount += softLineDelta;
// 调整编辑范围之内的软行
for(int i = event.startLine; i <= event.startLine + event.newLineCount; i++) {
int[] offsets = unwrappedHelper.splitLine(gc, width, i);
wrapped[i] = startSoftLine;
startSoftLine += offsets.length;
int m = 0;
for(int j = wrapped[i]; j < startSoftLine - 1; j++, m++)
setSoftLine(j, offsets[m], offsets[m + 1] - offsets[m]);
setSoftLine(startSoftLine - 1, offsets[m], unwrappedContent.getLineStartOffset(i + 1) - offsets[m]);
}
// 调整编辑范围之外的硬行分行数
for(int i = event.startLine + event.newLineCount + 1; i < unwrappedContent.getLineCount(); i++)
wrapped[i] += softLineDelta;
// 调整编辑范围之外的软行
int charDelta = event.newCharCount - event.replaceCharCount;
for(int i = startSoftLine; i < softLineCount; i++)
softLines[i][0] += charDelta;
richbox.releaseGC();
richbox.redrawFromLine(wrapped[event.startLine]);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#setLineStyle(int, edu.tsinghua.lumaqq.widgets.LineStyle)
*/
public void setLineStyle(int lineIndex, LineStyle style) {
int hardLineIndex = lineIndex;
unwrappedContent.getLineStyler().addLineStyle(hardLineIndex, style);
unwrappedHelper.clearLineHeightCache(hardLineIndex);
int hardLineCount = unwrappedContent.getLineCount();
// 调整空间
int startSoftLine = wrapped[hardLineIndex];
int removedSoftLineCount;
if(hardLineIndex >= hardLineCount - 1)
removedSoftLineCount = softLineCount - startSoftLine;
else
removedSoftLineCount = wrapped[hardLineIndex + 1] - startSoftLine;
// 计算软行的增减
GC gc = richbox.getGC();
int width = richbox.getClientArea().width - richbox.getLeftMargin() - richbox.getRightMargin();
if(width <= 0)
width = 100;
int addedSoftLineCount = 0;
int[] offsets = unwrappedHelper.splitLine(gc, width, hardLineIndex);
addedSoftLineCount += offsets.length;
int softLineDelta = addedSoftLineCount - removedSoftLineCount;
// 调整软行的空间和其他空间
removeSoftLines(startSoftLine, removedSoftLineCount);
insertSoftLines(startSoftLine, addedSoftLineCount, softLineCount);
helper.removeLineHeight(startSoftLine, removedSoftLineCount);
helper.insertLineHeight(startSoftLine, addedSoftLineCount, softLineCount);
softLineCount += softLineDelta;
// 调整编辑范围之内的软行
startSoftLine += offsets.length;
int m = 0;
for(int j = wrapped[hardLineIndex]; j < startSoftLine - 1; j++, m++)
setSoftLine(j, offsets[m], offsets[m + 1] - offsets[m]);
setSoftLine(startSoftLine - 1, offsets[m], unwrappedContent.getLineStartOffset(hardLineIndex + 1) - offsets[m]);
// 调整编辑范围之外的硬行分行数
for(int i = hardLineIndex + 1; i < unwrappedContent.getLineCount(); i++)
wrapped[i] += softLineDelta;
richbox.releaseGC();
richbox.redrawFromLine(wrapped[hardLineIndex]);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IWrappedRichContent#getHardLineCount()
*/
public int getHardLineCount() {
return unwrappedContent.getLineCount();
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#setDefaultStyle(edu.tsinghua.lumaqq.widgets.LineStyle)
*/
public void setDefaultStyle(LineStyle style) {
unwrappedContent.getLineStyler().setDefaultStyle(style);
wrap(richbox.getClientArea().width - richbox.getLeftMargin() - richbox.getRightMargin());
richbox.calculateTopLine();
richbox.redraw();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -