use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.
the class DefaultCellRenderer method paint.
/**
* {@inheritDoc}
*/
public void paint(GC gc, Object value) {
GridItem item = (GridItem) value;
gc.setFont(item.getFont(getColumn()));
boolean drawAsSelected = isSelected();
boolean drawBackground = true;
if (isCellSelected()) {
//(!isCellFocus());
drawAsSelected = true;
}
if (drawAsSelected) {
gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
} else {
if (item.getParent().isEnabled()) {
Color back = item.getBackground(getColumn());
if (back != null) {
gc.setBackground(back);
} else {
drawBackground = false;
}
} else {
gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
}
gc.setForeground(item.getForeground(getColumn()));
}
if (drawBackground)
gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
int x = leftMargin;
if (isTree()) {
boolean renderBranches = item.getParent().getTreeLinesVisible();
if (renderBranches) {
branchRenderer.setBranches(getBranches(item));
branchRenderer.setIndent(treeIndent);
branchRenderer.setBounds(getBounds().x + x, getBounds().y, getToggleIndent(item), // Take into account border
getBounds().height + 1);
}
x += getToggleIndent(item);
toggleRenderer.setExpanded(item.isExpanded());
toggleRenderer.setHover(getHoverDetail().equals("toggle"));
toggleRenderer.setLocation(getBounds().x + x, (getBounds().height - toggleRenderer.getBounds().height) / 2 + getBounds().y);
if (item.hasChildren())
toggleRenderer.paint(gc, null);
if (renderBranches) {
branchRenderer.setToggleBounds(toggleRenderer.getBounds());
branchRenderer.paint(gc, null);
}
x += toggleRenderer.getBounds().width + insideMargin;
}
if (isCheck()) {
checkRenderer.setChecked(item.getChecked(getColumn()));
checkRenderer.setGrayed(item.getGrayed(getColumn()));
if (!item.getParent().isEnabled()) {
checkRenderer.setGrayed(true);
}
checkRenderer.setHover(getHoverDetail().equals("check"));
if (isCenteredCheckBoxOnly(item)) {
//Special logic if this column only has a checkbox and is centered
checkRenderer.setBounds(getBounds().x + ((getBounds().width - checkRenderer.getBounds().width) / 2), (getBounds().height - checkRenderer.getBounds().height) / 2 + getBounds().y, checkRenderer.getBounds().width, checkRenderer.getBounds().height);
} else {
checkRenderer.setBounds(getBounds().x + x, (getBounds().height - checkRenderer.getBounds().height) / 2 + getBounds().y, checkRenderer.getBounds().width, checkRenderer.getBounds().height);
x += checkRenderer.getBounds().width + insideMargin;
}
checkRenderer.paint(gc, null);
}
Image image = item.getImage(getColumn());
if (image != null) {
int y = getBounds().y;
y += (getBounds().height - image.getBounds().height) / 2;
gc.drawImage(image, getBounds().x + x, y);
x += image.getBounds().width + insideMargin;
}
int width = getBounds().width - x - rightMargin;
if (drawAsSelected) {
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
} else {
gc.setForeground(item.getForeground(getColumn()));
}
if (!isWordWrap()) {
String text = TextUtils.getShortString(gc, item.getText(getColumn()), width);
if (getAlignment() == SWT.RIGHT) {
int len = gc.stringExtent(text).x;
if (len < width) {
x += width - len;
}
} else if (getAlignment() == SWT.CENTER) {
int len = gc.stringExtent(text).x;
if (len < width) {
x += (width - len) / 2;
}
}
gc.drawString(text, getBounds().x + x, getBounds().y + textTopMargin + topMargin, true);
} else {
if (textLayout == null) {
textLayout = new TextLayout(gc.getDevice());
item.getParent().addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
textLayout.dispose();
}
});
}
textLayout.setFont(gc.getFont());
textLayout.setText(item.getText(getColumn()));
textLayout.setAlignment(getAlignment());
textLayout.setWidth(width < 1 ? 1 : width);
if (item.getParent().isAutoHeight()) {
// Look through all columns (except this one) to get the max height needed for this item
int columnCount = item.getParent().getColumnCount();
int maxHeight = textLayout.getBounds().height + textTopMargin + textBottomMargin;
for (int i = 0; i < columnCount; i++) {
GridColumn column = item.getParent().getColumn(i);
if (i != getColumn() && column.getWordWrap()) {
int height = column.getCellRenderer().computeSize(gc, column.getWidth(), SWT.DEFAULT, item).y;
maxHeight = Math.max(maxHeight, height);
}
}
// Also look at the row header if necessary
if (item.getParent().isWordWrapHeader()) {
int height = item.getParent().getRowHeaderRenderer().computeSize(gc, SWT.DEFAULT, SWT.DEFAULT, item).y;
maxHeight = Math.max(maxHeight, height);
}
if (maxHeight != item.getHeight()) {
item.setHeight(maxHeight);
}
}
textLayout.draw(gc, getBounds().x + x, getBounds().y + textTopMargin + topMargin);
}
if (item.getParent().getLinesVisible()) {
if (isCellSelected()) {
//XXX: should be user definable?
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
} else {
gc.setForeground(item.getParent().getLineColor());
}
gc.drawLine(getBounds().x, getBounds().y + getBounds().height, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x + getBounds().width - 1, getBounds().y + getBounds().height);
}
if (isCellFocus()) {
Rectangle focusRect = new Rectangle(getBounds().x, getBounds().y, getBounds().width - 1, getBounds().height);
gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
gc.drawRectangle(focusRect);
if (isFocus()) {
focusRect.x++;
focusRect.width -= 2;
focusRect.y++;
focusRect.height -= 2;
gc.drawRectangle(focusRect);
}
}
}
use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.
the class DefaultColumnFooterRenderer method getTextBounds.
/**
* {@inheritDoc}
*/
public Rectangle getTextBounds(Object value, boolean preferred) {
GridColumn column = (GridColumn) value;
int x = leftMargin;
if (column.getImage() != null) {
x += column.getImage().getBounds().width + imageSpacing;
}
GC gc = new GC(column.getParent());
gc.setFont(column.getFooterFont());
int y = getBounds().height - bottomMargin - gc.getFontMetrics().getHeight();
Rectangle bounds = new Rectangle(x, y, 0, 0);
Point p = gc.stringExtent(column.getText());
bounds.height = p.y;
if (preferred) {
bounds.width = p.x;
} else {
int width = getBounds().width - x;
if (column.getSort() == SWT.NONE) {
width -= rightMargin;
}
bounds.width = width;
}
gc.dispose();
return bounds;
}
use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.
the class BranchRenderer method paint.
/**
* {@inheritDoc}
*/
public void paint(GC gc, Object value) {
Rectangle bounds = getBounds();
int xLeft = bounds.x;
int yTop = bounds.y - 1;
int yBottom = yTop + bounds.height;
int yMiddle = toggleBounds.y + toggleBounds.height / 2;
int yToggleBottom = toggleBounds.y + toggleBounds.height - 1;
int yToggleTop = toggleBounds.y;
int oldStyle = gc.getLineStyle();
Color oldForeground = gc.getForeground();
int[] oldLineDash = gc.getLineDash();
gc.setForeground(getDisplay().getSystemColor(isSelected() ? SWT.COLOR_LIST_SELECTION_TEXT : SWT.COLOR_WIDGET_NORMAL_SHADOW));
int dy = bounds.y % 2;
// Set line style to dotted
gc.setLineDash(LINE_STYLE);
// Adjust line positions by a few pixels to create correct effect
yToggleTop--;
yTop++;
yToggleBottom++;
// If height is even, we shorten to an odd number of pixels, and start at the original y offset
if (bounds.height % 2 == 0) {
yBottom -= 1;
} else // If height is odd, we alternate based on the row offset
{
yTop += dy;
yBottom -= dy;
}
// Adjust ascender and descender
yToggleBottom += dy;
if ((yToggleTop - yTop + 1) % 2 == 0)
yToggleTop -= 1;
if ((yToggleBottom - yBottom + 1) % 2 == 0)
yToggleBottom += dy == 1 ? -1 : 1;
for (int i = 0; i < branches.length; i++) {
// Calculate offsets for this branch
int xRight = xLeft + indent;
int xMiddle = xLeft + toggleBounds.width / 2;
int xMiddleBranch = xMiddle;
int xToggleRight = xLeft + toggleBounds.width;
int dx = 0;
xRight--;
xMiddleBranch += 2;
xToggleRight--;
if (indent % 2 == 0) {
xRight -= 1;
} else {
dx = xLeft % 2;
xLeft += dx;
xRight -= dx;
}
// Render line segments
if ((branches[i] & H_FULL) == H_FULL)
gc.drawLine(xLeft, yMiddle, xRight, yMiddle);
if ((branches[i] & H_RIGHT) == H_RIGHT)
gc.drawLine(xMiddleBranch, yMiddle, xRight, yMiddle);
if ((branches[i] & H_CENTRE_TOGGLE) == H_CENTRE_TOGGLE)
gc.drawLine(xMiddleBranch, yMiddle, xToggleRight, yMiddle);
if ((branches[i] & H_LEFT_TOGGLE) == H_LEFT_TOGGLE)
gc.drawLine(xLeft, yMiddle, xToggleRight, yMiddle);
if ((branches[i] & V_FULL) == V_FULL)
gc.drawLine(xMiddle, yTop, xMiddle, yBottom);
if ((branches[i] & V_TOP) == V_TOP)
gc.drawLine(xMiddle, yTop, xMiddle, yMiddle);
if ((branches[i] & ASCENDER) == ASCENDER)
gc.drawLine(xMiddle, yTop, xMiddle, yToggleTop);
if ((branches[i] & DESCENDER) == DESCENDER)
gc.drawLine(xMiddle, yToggleBottom, xMiddle, yBottom);
xLeft += indent - dx;
}
gc.setLineDash(oldLineDash);
gc.setLineStyle(oldStyle);
gc.setForeground(oldForeground);
}
use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.
the class Grid method updateSelection.
/**
* Adds/removes items from the selected items list based on the
* selection/deselection of the given item.
*
* @param item item being selected/unselected
* @param stateMask key state during selection
*
* @return selection event that needs to be fired or null
*/
private Event updateSelection(GridItem item, int stateMask) {
if (!selectionEnabled) {
return null;
}
Event selectionEvent = null;
if (selectionType == SWT.SINGLE) {
if (selectedItems.contains(item)) {
// Deselect when pressing CTRL
if ((stateMask & SWT.MOD1) == SWT.MOD1) {
selectedItems.clear();
}
} else {
selectedItems.clear();
selectedItems.add(item);
}
Rectangle clientArea = getClientArea();
redraw(clientArea.x, clientArea.y, clientArea.width, clientArea.height, false);
selectionEvent = new Event();
selectionEvent.item = item;
} else if (selectionType == SWT.MULTI) {
boolean shift = false;
boolean ctrl = false;
if ((stateMask & SWT.MOD2) == SWT.MOD2) {
shift = true;
}
if ((stateMask & SWT.MOD1) == SWT.MOD1) {
ctrl = true;
}
if (!shift && !ctrl) {
if (selectedItems.size() == 1 && selectedItems.contains(item))
return null;
selectedItems.clear();
selectedItems.add(item);
Rectangle clientArea = getClientArea();
redraw(clientArea.x, clientArea.y, clientArea.width, clientArea.height, false);
shiftSelectionAnchorItem = null;
selectionEvent = new Event();
selectionEvent.item = item;
} else if (shift) {
if (shiftSelectionAnchorItem == null) {
shiftSelectionAnchorItem = focusItem;
}
// if (shiftSelectionAnchorItem == item)
// {
// return;
// }
boolean maintainAnchorSelection = false;
if (!ctrl) {
if (selectedItems.contains(shiftSelectionAnchorItem)) {
maintainAnchorSelection = true;
}
selectedItems.clear();
}
int anchorIndex = items.indexOf(shiftSelectionAnchorItem);
int itemIndex = items.indexOf(item);
int min = 0;
int max = 0;
if (anchorIndex < itemIndex) {
if (maintainAnchorSelection) {
min = anchorIndex;
} else {
min = anchorIndex + 1;
}
max = itemIndex;
} else {
if (maintainAnchorSelection) {
max = anchorIndex;
} else {
max = anchorIndex - 1;
}
min = itemIndex;
}
for (int i = min; i <= max; i++) {
if (!selectedItems.contains(items.get(i)) && ((GridItem) items.get(i)).isVisible()) {
selectedItems.add((GridItem) items.get(i));
}
}
Rectangle clientArea = getClientArea();
redraw(clientArea.x, clientArea.y, clientArea.width, clientArea.height, false);
selectionEvent = new Event();
} else if (ctrl) {
if (selectedItems.contains(item)) {
selectedItems.remove(item);
} else {
selectedItems.add(item);
}
Rectangle clientArea = getClientArea();
redraw(clientArea.x, clientArea.y, clientArea.width, clientArea.height, false);
shiftSelectionAnchorItem = null;
selectionEvent = new Event();
selectionEvent.item = item;
}
}
Rectangle clientArea = getClientArea();
redraw(clientArea.x, clientArea.y, clientArea.width, clientArea.height, false);
return selectionEvent;
}
use of org.eclipse.swt.graphics.Rectangle in project translationstudio8 by heartsome.
the class Grid method handleCellHover.
/**
* Sets the hovering variables (hoverItem,hoveringColumn) as well as
* hoverDetail by talking to the cell renderers. Triggers a redraw if
* necessary.
*
* @param x mouse x
* @param y mouse y
* @return true if a new section of the table is now being hovered
*/
private boolean handleCellHover(int x, int y) {
String detail = "";
boolean overText = false;
final GridColumn col = getColumn(new Point(x, y));
final GridItem item = getItem(new Point(x, y));
GridColumnGroup hoverColGroup = null;
GridColumn hoverColHeader = null;
if (col != null) {
if (item != null) {
if (y < getClientArea().height - footerHeight) {
col.getCellRenderer().setBounds(item.getBounds(columns.indexOf(col)));
if (col.getCellRenderer().notify(IInternalWidget.MouseMove, new Point(x, y), item)) {
detail = col.getCellRenderer().getHoverDetail();
}
Rectangle textBounds = col.getCellRenderer().getTextBounds(item, false);
if (textBounds != null) {
Point p = new Point(x - col.getCellRenderer().getBounds().x, y - col.getCellRenderer().getBounds().y);
overText = textBounds.contains(p);
}
}
} else {
if (y < headerHeight) {
if (columnGroups.length != 0 && y < groupHeaderHeight && col.getColumnGroup() != null) {
hoverColGroup = col.getColumnGroup();
hoverColGroup.getHeaderRenderer().setBounds(hoverColGroup.getBounds());
if (hoverColGroup.getHeaderRenderer().notify(IInternalWidget.MouseMove, new Point(x, y), hoverColGroup)) {
detail = hoverColGroup.getHeaderRenderer().getHoverDetail();
}
Rectangle textBounds = hoverColGroup.getHeaderRenderer().getTextBounds(hoverColGroup, false);
if (textBounds != null) {
Point p = new Point(x - hoverColGroup.getHeaderRenderer().getBounds().x, y - hoverColGroup.getHeaderRenderer().getBounds().y);
overText = textBounds.contains(p);
}
} else {
// on col header
hoverColHeader = col;
col.getHeaderRenderer().setBounds(col.getBounds());
if (col.getHeaderRenderer().notify(IInternalWidget.MouseMove, new Point(x, y), col)) {
detail = col.getHeaderRenderer().getHoverDetail();
}
Rectangle textBounds = col.getHeaderRenderer().getTextBounds(col, false);
if (textBounds != null) {
Point p = new Point(x - col.getHeaderRenderer().getBounds().x, y - col.getHeaderRenderer().getBounds().y);
overText = textBounds.contains(p);
}
}
}
}
}
boolean hoverChange = false;
if (hoveringItem != item || !hoveringDetail.equals(detail) || hoveringColumn != col || hoverColGroup != hoverColumnGroupHeader || hoverColHeader != hoveringColumnHeader) {
hoveringItem = item;
hoveringDetail = detail;
hoveringColumn = col;
hoveringColumnHeader = hoverColHeader;
hoverColumnGroupHeader = hoverColGroup;
Rectangle clientArea = getClientArea();
redraw(clientArea.x, clientArea.y, clientArea.width, clientArea.height, false);
hoverChange = true;
}
//do inplace toolTip stuff
if (hoverChange || hoveringOverText != overText) {
hoveringOverText = overText;
if (overText) {
Rectangle cellBounds = null;
Rectangle textBounds = null;
Rectangle preferredTextBounds = null;
if (//no inplace tooltips when regular tooltip
hoveringItem != null && hoveringItem.getToolTipText(indexOf(col)) == null && //dont show inplace tooltips for cells with wordwrap
!col.getWordWrap()) {
cellBounds = col.getCellRenderer().getBounds();
if (cellBounds.x + cellBounds.width > getSize().x) {
cellBounds.width = getSize().x - cellBounds.x;
}
textBounds = col.getCellRenderer().getTextBounds(item, false);
preferredTextBounds = col.getCellRenderer().getTextBounds(item, true);
} else if (//no inplace tooltips when regular tooltip
hoveringColumnHeader != null && hoveringColumnHeader.getHeaderTooltip() == null) {
cellBounds = hoveringColumnHeader.getHeaderRenderer().getBounds();
if (cellBounds.x + cellBounds.width > getSize().x) {
cellBounds.width = getSize().x - cellBounds.x;
}
textBounds = hoveringColumnHeader.getHeaderRenderer().getTextBounds(col, false);
preferredTextBounds = hoveringColumnHeader.getHeaderRenderer().getTextBounds(col, true);
} else if (hoverColumnGroupHeader != null) {
cellBounds = hoverColumnGroupHeader.getHeaderRenderer().getBounds();
if (cellBounds.x + cellBounds.width > getSize().x) {
cellBounds.width = getSize().x - cellBounds.x;
}
textBounds = hoverColumnGroupHeader.getHeaderRenderer().getTextBounds(hoverColumnGroupHeader, false);
preferredTextBounds = hoverColumnGroupHeader.getHeaderRenderer().getTextBounds(hoverColumnGroupHeader, true);
}
//if we are truncated
if (textBounds != null && textBounds.width < preferredTextBounds.width) {
showToolTip(item, col, hoverColumnGroupHeader, new Point(cellBounds.x + textBounds.x, cellBounds.y + textBounds.y));
//the following 2 lines are done here rather than in showToolTip to allow
//that method to be overridden yet still capture the mouse.
setCapture(true);
inplaceTooltipCapture = true;
}
} else {
hideToolTip();
}
}
//do normal cell specific tooltip stuff
if (hoverChange) {
String newTip = null;
if ((hoveringItem != null) && (hoveringColumn != null)) {
// get cell specific tooltip
newTip = hoveringItem.getToolTipText(indexOf(hoveringColumn));
} else if ((hoveringColumn != null) && (hoveringColumnHeader != null)) {
// get column header specific tooltip
newTip = hoveringColumn.getHeaderTooltip();
}
if (newTip == null) {
// no cell or column header specific tooltip then use base Grid tooltip
newTip = getToolTipText();
}
//Avoid unnecessarily resetting tooltip - this will cause the tooltip to jump around
if (newTip != null && !newTip.equals(displayedToolTipText)) {
updateToolTipText(newTip);
} else if (newTip == null && displayedToolTipText != null) {
updateToolTipText(null);
}
displayedToolTipText = newTip;
}
return hoverChange;
}
Aggregations