use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class DefaultLookAndFeel method calculateTextAreaSpan.
@Override
public Spans calculateTextAreaSpan(TextSelection sel, TextArea ta) {
Spans out = sel.newSpans();
// setFG(g, ta);
// Span out = sel.newSpan(ta);
int line = ta.getLines();
// int oX = g.getClipX();
// int oY = g.getClipY();
// int oWidth = g.getClipWidth();
// int oHeight = g.getClipHeight();
Font f = ta.getStyle().getFont();
int fontHeight = f.getHeight();
int align = reverseAlignForBidi(ta);
int leftPadding = ta.getStyle().getPaddingLeft(ta.isRTL());
int rightPadding = ta.getStyle().getPaddingRight(ta.isRTL());
int topPadding = ta.getStyle().getPaddingTop();
switch(ta.getVerticalAlignment()) {
case Component.CENTER:
topPadding += Math.max(0, (ta.getInnerHeight() - (ta.getRowsGap() + fontHeight) * line) / 2);
break;
case Component.BOTTOM:
topPadding += Math.max(0, (ta.getInnerHeight() - (ta.getRowsGap() + fontHeight) * line));
}
// boolean shouldBreak = false;
int posOffset = 0;
int lastRowBottom = 0;
for (int i = 0; i < line; i++) {
Span rowSpan = sel.newSpan(ta);
int x = ta.getX() + leftPadding;
int y = ta.getY() + topPadding + (ta.getRowsGap() + fontHeight) * i;
int adjustedY = Math.max(y, lastRowBottom);
int yDiff = adjustedY - y;
y = adjustedY;
// if(Rectangle.intersects(x, y, ta.getWidth(), fontHeight, oX, oY, oWidth, oHeight)) {
String rowText = (String) ta.getTextAt(i);
// display ******** if it is a password field
String displayText = "";
if ((ta.getConstraint() & TextArea.PASSWORD) != 0) {
int rlen = rowText.length();
for (int j = 0; j < rlen; j++) {
displayText += passwordChar;
}
} else {
displayText = rowText;
}
posOffset = ta.getText().indexOf(rowText, posOffset);
switch(align) {
case Component.RIGHT:
x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
break;
case Component.CENTER:
x += (ta.getWidth() - leftPadding - rightPadding - f.stringWidth(displayText)) / 2;
break;
}
// add "..." at the last row
if (ta.isEndsWith3Points() && ta.getGrowLimit() == (i + 1) && ta.getGrowLimit() != line) {
if (displayText.length() > 3) {
displayText = displayText.substring(0, displayText.length() - 3);
}
// g.drawString(displayText + "...", x, y ,ta.getStyle().getTextDecoration());
append(sel, ta, rowSpan, displayText + "...", f, posOffset, x, y, getSelectionHeight(f) - yDiff);
lastRowBottom = rowSpan.getBounds().getY() + rowSpan.getBounds().getHeight();
rowSpan = rowSpan.translate(ta.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - ta.getX(), ta.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - ta.getY());
out.add(rowSpan);
return out;
} else {
// g.drawString(displayText, x, y ,ta.getStyle().getTextDecoration());
append(sel, ta, rowSpan, displayText, f, posOffset, x, y, getSelectionHeight(f) - yDiff);
lastRowBottom = rowSpan.getBounds().getY() + rowSpan.getBounds().getHeight();
rowSpan = rowSpan.translate(ta.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - ta.getX(), ta.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - ta.getY());
out.add(rowSpan);
}
posOffset += displayText.length();
// shouldBreak = true;
// }else{
// if(shouldBreak){
// break;
// }
// }
}
return out;
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class DefaultLookAndFeel method calculateSpanForLabelStringValign.
private Span calculateSpanForLabelStringValign(TextSelection sel, Label l, String str, int x, int y, int iconStringHGap, int iconHeight, int textSpaceW, int fontHeight) {
switch(l.getVerticalAlignment()) {
case Component.TOP:
return calculateSpanForLabelString(sel, l, str, x, y, textSpaceW);
case Component.CENTER:
return calculateSpanForLabelString(sel, l, str, x, y + iconHeight / 2 - fontHeight / 2, textSpaceW);
case Component.BASELINE:
Font iconFont = l.getIconStyleComponent().getStyle().getFont();
Font textFont = l.getStyle().getFont();
if (iconFont == null) {
iconFont = Font.getDefaultFont();
}
if (textFont == null) {
textFont = Font.getDefaultFont();
}
int ascentDiff = iconFont.getAscent() - textFont.getAscent();
return calculateSpanForLabelString(sel, l, str, x, y + ascentDiff, textSpaceW);
default:
return calculateSpanForLabelString(sel, l, str, x, y + iconStringHGap, textSpaceW);
}
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class CSSEngine method createBorder.
/**
* Returns a border for a specific side of the component
*
* @param styleAttributes The style attributes element containing the border directives
* @param ui The component we want to set the border on
* @param location One of Component.TOP/BOTTOM/LEFT/RIGHT
* @return
*/
Border createBorder(CSSElement styleAttributes, Component ui, int location, int styles, int type) {
int borderStyle = styleAttributes.getAttrVal(BORDER_OUTLINE_PROPERTIES[type][STYLE] + location);
if ((borderStyle == -1) || (borderStyle == BORDER_STYLE_NONE)) {
return null;
}
int borderColor = styleAttributes.getAttrVal(BORDER_OUTLINE_PROPERTIES[type][COLOR] + location);
int borderWidth = styleAttributes.getAttrLengthVal(BORDER_OUTLINE_PROPERTIES[type][WIDTH] + location, ui, 0);
if (borderWidth == -1) {
// Default value
borderWidth = CSSElement.BORDER_DEFAULT_WIDTH;
}
if (type == OUTLINE) {
// all
location = -1;
}
if ((styles & STYLE_SELECTED) != 0) {
incPadding(ui.getSelectedStyle(), location, borderWidth);
}
if ((styles & STYLE_UNSELECTED) != 0) {
incPadding(ui.getUnselectedStyle(), location, borderWidth);
}
if ((styles & STYLE_PRESSED) != 0) {
incPadding(((HTMLLink) ui).getPressedStyle(), location, borderWidth);
}
Border border = null;
if ((borderColor == -1) && (borderStyle >= BORDER_STYLE_GROOVE)) {
borderColor = DEFAULT_3D_BORDER_COLOR;
}
switch(borderStyle) {
case BORDER_STYLE_SOLID:
if (borderColor == -1) {
border = Border.createLineBorder(borderWidth);
} else {
border = Border.createLineBorder(borderWidth, borderColor);
}
break;
case BORDER_STYLE_DOUBLE:
if (borderColor == -1) {
border = Border.createDoubleBorder(borderWidth);
} else {
border = Border.createDoubleBorder(borderWidth, borderColor);
}
break;
case BORDER_STYLE_GROOVE:
border = Border.createGrooveBorder(borderWidth, borderColor);
break;
case BORDER_STYLE_RIDGE:
border = Border.createRidgeBorder(borderWidth, borderColor);
break;
case BORDER_STYLE_INSET:
border = Border.createInsetBorder(borderWidth, borderColor);
break;
case BORDER_STYLE_OUTSET:
border = Border.createOutsetBorder(borderWidth, borderColor);
break;
case BORDER_STYLE_DOTTED:
if (borderColor == -1) {
border = Border.createDottedBorder(borderWidth);
} else {
border = Border.createDottedBorder(borderWidth, borderColor);
}
break;
case BORDER_STYLE_DASHED:
if (borderColor == -1) {
border = Border.createDashedBorder(borderWidth);
} else {
border = Border.createDashedBorder(borderWidth, borderColor);
}
break;
}
return border;
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class CSSEngine method setMatchingFont.
/**
* Sets the font of the component to the closest font that can be found according to the specified properties
* Note that system fonts will be matched only with system fonts and same goes for bitmap fonts
*
* @param htmlC The HTMLComponent this component belongs to (For the available bitmap fonts table)
* @param cmp The component to work on
* @param fontFamily The font family
* @param fontSize The font size in pixels
* @param fontStyle The font style - either Font.STYLE_PLAIN or Font.STYLE_ITALIC
* @param fontWeight The font weight - either Font.STYLE_PLAIN ot Font.STYLE_BOLD
*/
private void setMatchingFont(HTMLComponent htmlC, Component cmp, String fontFamily, int fontSize, int fontStyle, int fontWeight, CSSElement selector) {
int styles = getApplicableStyles(cmp, selector);
Font curFont = cmp.getUnselectedStyle().getFont();
if (((styles & STYLE_SELECTED) != 0) && ((styles & STYLE_UNSELECTED) == 0)) {
// Focus
curFont = cmp.getSelectedStyle().getFont();
}
if ((styles & STYLE_PRESSED) != 0) {
// Active
curFont = ((HTMLLink) cmp).getPressedStyle().getFont();
}
int curSize = 0;
boolean isBold = false;
boolean isItalic = false;
String curFamily = null;
if (curFont.getCharset() == null) {
// The family string in system fonts is just used to index the font in the matchingFonts cache hashtable
switch(curFont.getFace()) {
case Font.FACE_SYSTEM:
curFamily = "system";
break;
case Font.FACE_PROPORTIONAL:
curFamily = "proportional";
break;
default:
curFamily = "monospace";
}
// Font height is roughly 2-3 pixels above the font size, and is the best indicator we have to what the system font size is
curSize = curFont.getHeight() - 2;
isBold = ((curFont.getStyle() & Font.STYLE_BOLD) != 0);
isItalic = ((curFont.getStyle() & Font.STYLE_ITALIC) != 0);
} else {
// bitmap font
HTMLFont hFont = htmlC.getHTMLFont(curFont);
if (hFont != null) {
curSize = hFont.getSize();
isBold = hFont.isBold();
isItalic = hFont.isItalic();
curFamily = hFont.getFamily();
}
}
if (((fontFamily != null) && (curFamily != null) && (!fontFamily.equalsIgnoreCase(curFamily))) || (fontSize != curSize) || ((isBold) != (fontWeight == Font.STYLE_BOLD)) || ((isItalic) != (fontWeight == Font.STYLE_ITALIC))) {
// Set the unspecified attributes of the requested font to match those of the current one
if ((fontFamily == null) && (curFamily != null)) {
fontFamily = curFamily.toLowerCase();
}
if (fontSize == -1) {
fontSize = curSize;
}
if (fontStyle == -1) {
if (isItalic) {
fontStyle = Font.STYLE_ITALIC;
} else {
fontStyle = 0;
}
}
if (fontWeight == -1) {
if (isBold) {
fontWeight = Font.STYLE_BOLD;
} else {
fontWeight = 0;
}
}
String fontKey = fontFamily + "." + fontSize + "." + fontStyle + "." + fontWeight;
Object obj = matchingFonts.get(fontKey);
if (obj != null) {
Font font = (Font) obj;
setFontForStyles(styles, cmp, font);
return;
}
Font font = null;
if (curFont.getCharset() == null) {
// system font
int systemFontSize = curFont.getSize();
if (fontSize > curSize) {
// bigger font
if (systemFontSize == Font.SIZE_SMALL) {
systemFontSize = Font.SIZE_MEDIUM;
} else if (systemFontSize == Font.SIZE_MEDIUM) {
systemFontSize = Font.SIZE_LARGE;
}
} else if (fontSize < curSize) {
// smaller font
if (systemFontSize == Font.SIZE_LARGE) {
systemFontSize = Font.SIZE_MEDIUM;
} else if (systemFontSize == Font.SIZE_MEDIUM) {
systemFontSize = Font.SIZE_SMALL;
}
}
font = Font.createSystemFont(curFont.getFace(), fontStyle + fontWeight, systemFontSize);
} else {
font = htmlC.getClosestFont(fontFamily, fontSize, fontStyle, fontWeight);
}
if (font != null) {
matchingFonts.put(fontKey, font);
setFontForStyles(styles, cmp, font);
}
}
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class FlowLayout method moveComponents.
private void moveComponents(Container target, int x, int y, int width, int height, int rowStart, int rowEnd, int baseline) {
switch(orientation) {
case Component.CENTER:
// this will remove half of last gap
if (target.isRTL()) {
x = -(width) / 2;
} else {
x = (width) / 2;
}
break;
case Component.RIGHT:
if (target.isRTL()) {
// this will remove the last gap
x = -width;
} else {
x = width;
}
break;
}
Style parentStyle = target.getStyle();
int parentPadding = parentStyle.getHorizontalPadding();
for (int i = rowStart; i < rowEnd; i++) {
Component m = target.getComponentAt(i);
Style style = m.getStyle();
int marginX = style.getMarginLeftNoRTL() + style.getMarginRightNoRTL();
if (m.getWidth() + marginX < target.getWidth() - parentPadding) {
m.setX(m.getX() + x);
}
int marginTop = style.getMarginTop();
switch(valign) {
case Component.BOTTOM:
if (vAlignByRow) {
m.setY(y + Math.max(marginTop, height - m.getHeight() - style.getMarginBottom()));
} else {
// m.setY(y + Math.max(marginTop, target.getHeight() - m.getHeight()) - style.getMarginBottom());
}
break;
case Component.CENTER:
if (vAlignByRow) {
m.setY(y + Math.max(marginTop, (height - m.getOuterHeight()) / 2 + style.getMarginTop()));
} else {
// m.setY(y + Math.max(marginTop, (target.getHeight() - m.getHeight()) / 2));
}
break;
case Component.BASELINE:
m.setY(y + Math.max(marginTop, baseline - m.getBaseline(m.getWidth(), m.getHeight())));
break;
default:
m.setY(y + marginTop);
break;
}
}
}
Aggregations