📄 wrappedcontent.java
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.widgets.rich;
import org.eclipse.swt.graphics.GC;
/**
* 包含一个rich content,使其具有wrap功能
*
* @author luma
*/
public class WrappedContent implements IWrappedRichContent {
private static final int LINE_OFFSET = 0;
private static final int LINE_LENGTH = 1;
private RichBox richbox;
private IRichContent unwrappedContent;
private ContentHelper unwrappedHelper;
private WrappedLineStyler styler;
private ContentHelper helper;
// 存放硬行wrap之后各个硬行的软行偏移
private int[] wrapped;
// 软行总数
private int softLineCount;
// 软行偏移和长度数组
private int[][] softLines;
/**
* 创建content
*/
public WrappedContent(RichBox box, IRichContent content, ImageResolver resolver) {
richbox = box;
unwrappedContent = content;
unwrappedHelper = content.getContentHelper();
styler = new WrappedLineStyler(this, content.getLineStyler());
helper = new ContentHelper(this, resolver);
int lineCount = content.getLineCount();
wrapped = new int[lineCount];
softLines = new int[10][2];
}
/**
* 删除wrap偏移数据
*
* @param start
* @param length
*/
private void removeWrapped(int start, int length) {
if(start + length < wrapped.length)
System.arraycopy(wrapped, start + length, wrapped, start, wrapped.length - start - length);
}
/**
* 插入空间到wrap数组
*
* @param startIndex
* @param numLines
* @param oldLineCount
*/
private void insertWrapped(int startIndex, int numLines, int oldLineCount) {
while(oldLineCount + numLines > wrapped.length)
expandWrappedDouble();
System.arraycopy(wrapped, startIndex, wrapped, startIndex + numLines, oldLineCount - startIndex);
}
/**
* 插入空间到行偏移长度数组
*
* @param startIndex
* @param numLines
* @param oldLineCount
*/
private void insertSoftLines(int startIndex, int numLines, int oldLineCount) {
while(oldLineCount + numLines > softLines.length)
expandLineDouble();
System.arraycopy(softLines, startIndex, softLines, startIndex + numLines, oldLineCount - startIndex);
}
/**
* 删除行
*
* @param start
* @param length
*/
private void removeSoftLines(int start, int length) {
if(start + length < softLines.length)
System.arraycopy(softLines, start + length, softLines, start, softLines.length - start - length);
}
/**
* 扩展wrap数组
*/
private void expandWrappedDouble() {
int[] newArray = new int[wrapped.length << 1];
System.arraycopy(wrapped, 0, newArray, 0, wrapped.length);
wrapped = newArray;
}
/**
* 保证wrap数组能满足一个容量的需求
*
* @param minimum
*/
private void ensureWrappedSize(int minimum) {
if(minimum > wrapped.length) {
int[] newArray = new int[minimum + 10];
System.arraycopy(wrapped, 0, newArray, 0, wrapped.length);
wrapped = newArray;
}
}
/**
* 成倍增加行数
*/
private void expandLineDouble() {
int[][] newLines = new int[softLines.length << 1][];
System.arraycopy(softLines, 0, newLines, 0, softLines.length);
softLines = newLines;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#getLineCount()
*/
public int getLineCount() {
return softLineCount;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#getLine(int)
*/
public String getLine(int lineIndex) {
int start = softLines[lineIndex][0];
int length = softLines[lineIndex][1];
while (length - 1 >= 0 && isDelimit(start + length - 1) )
length--;
return unwrappedContent.getText(start, length);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#getLineAtOffset(int)
*/
public int getLineAtOffset(int offset) {
int low = 0;
int high = softLineCount - 1;
int middle = (low + high) >>> 1;
for(; low < high; middle = (low + high) >>> 1) {
if(offset >= softLines[middle][0])
low = (middle == low) ? (low + 1) : middle;
else
high = (middle == high) ? (high - 1) : middle;
}
if(offset >= softLines[low][0])
return low;
else
return low - 1;
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#getLineStartOffset(int)
*/
public int getLineStartOffset(int lineIndex) {
if(lineIndex < softLineCount)
return softLines[lineIndex][0];
else
return getCharCount();
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#getText(int, int)
*/
public String getText(int start, int length) {
return unwrappedContent.getText(start, length);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#getCharCount()
*/
public int getCharCount() {
return unwrappedContent.getCharCount();
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#setText(java.lang.String)
*/
public void setText(String text) {
unwrappedContent.setText(text);
wrap(richbox.getClientArea().width - richbox.getLeftMargin() - richbox.getRightMargin());
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#isDelimit(int)
*/
public boolean isDelimit(int i) {
return unwrappedContent.isDelimit(i);
}
/* (non-Javadoc)
* @see edu.tsinghua.lumaqq.widgets.IRichContent#isImageTag(int)
*/
public boolean isImageTag(int i) {
return unwrappedContent.isImageTag(i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -