use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class BlackBerryTouchSupport method touchEvent.
protected boolean touchEvent(TouchEvent e) {
super.touchEvent(e);
lastNavigationCharged = false;
int x1 = e.getGlobalX(1);
int y1 = e.getGlobalY(1);
int x2 = e.getGlobalX(2);
int y2 = e.getGlobalY(2);
int evt = e.getEvent();
Form f = Display.getInstance().getCurrent();
if (f != null) {
Component cmp = f.getComponentAt(x1, y1);
if (cmp != null) {
if (cmp instanceof PeerComponent) {
return false;
}
if (cmp instanceof TextArea && cmp.hasFocus() && impl.nativeEdit != null) {
return false;
}
if (impl.nativeEdit != null) {
impl.finishEdit(true);
}
}
}
if (!isClickTouchScreen()) {
if (evt == TouchEvent.DOWN) {
evt = TouchEvent.CLICK;
} else if (evt == TouchEvent.UP) {
evt = TouchEvent.UNCLICK;
}
}
switch(evt) {
case TouchEvent.UNCLICK:
clicked = false;
drawHover = true;
if (x1 < 0) {
impl.pointerReleased(x2, y2);
} else {
if (x2 < 0) {
impl.pointerReleased(x1, y1);
} else {
impl.pointerReleased(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
break;
case TouchEvent.CLICK:
clicked = true;
drawHover = false;
if (x1 < 0) {
impl.pointerPressed(x2, y2);
} else {
if (x2 < 0) {
impl.pointerPressed(x1, y1);
} else {
impl.pointerPressed(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
break;
case TouchEvent.MOVE:
if (clicked) {
drawHover = false;
if (x1 < 0) {
impl.pointerDragged(x2, y2);
} else {
if (x2 < 0) {
impl.pointerDragged(x1, y1);
} else {
impl.pointerDragged(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
} else {
drawHover = true;
if (x1 < 0) {
impl.pointerHover(x2, y2);
} else {
if (x2 < 0) {
impl.pointerHover(x1, y1);
} else {
impl.pointerHover(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
}
break;
case TouchEvent.DOWN:
if (!clicked) {
drawHover = true;
if (x1 < 0) {
impl.pointerHoverPressed(x2, y2);
} else {
if (x2 < 0) {
impl.pointerHoverPressed(x1, y1);
} else {
impl.pointerHoverPressed(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
}
break;
case TouchEvent.UP:
if (!clicked) {
drawHover = false;
if (x1 < 0) {
impl.pointerHoverReleased(x2, y2);
} else {
if (x2 < 0) {
impl.pointerHoverReleased(x1, y1);
} else {
impl.pointerHoverReleased(new int[] { x1, x2 }, new int[] { y1, y2 });
}
}
}
break;
}
hoverY = y1;
hoverX = x1;
return true;
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawLabelStringValign.
/**
* Implements the drawString for the text component and adjust the valign
* assuming the icon is in one of the sides
*/
private int drawLabelStringValign(Graphics g, Label l, String str, int x, int y, int iconStringHGap, int iconHeight, int textSpaceX, int textSpaceW, int fontHeight) {
if (str.length() == 0) {
return 0;
}
switch(l.getVerticalAlignment()) {
case Component.TOP:
return drawLabelString(g, l, str, x, y, textSpaceX, textSpaceW);
case Component.CENTER:
return drawLabelString(g, l, str, x, y + iconHeight / 2 - fontHeight / 2, textSpaceX, 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 drawLabelString(g, l, str, x, y + ascentDiff, textSpaceX, textSpaceW);
default:
return drawLabelString(g, l, str, x, y + iconStringHGap, textSpaceX, textSpaceW);
}
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawLabelImageValign.
private void drawLabelImageValign(Graphics g, Label l, Image icon, int x, int y, int fontHeight, int iconHeight) {
int iconStringHGap = (fontHeight - iconHeight) / 2;
// int strWidth = drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
switch(l.getVerticalAlignment()) {
case Component.TOP:
g.drawImage(icon, x, y + iconStringHGap);
break;
case Component.BOTTOM:
g.drawImage(icon, x, y + fontHeight - iconHeight);
break;
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();
}
iconStringHGap = textFont.getAscent() - iconFont.getAscent();
g.drawImage(icon, x, y + iconStringHGap);
break;
default:
g.drawImage(icon, x, y + iconStringHGap);
}
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class DefaultLookAndFeel method getPreferredSize.
/**
* {@inheritDoc}
*/
private Dimension getPreferredSize(Label l, Image[] icons, Image stateImage) {
int prefW = 0;
int prefH = 0;
Style style = l.getStyle();
int gap = l.getGap();
int ilen = icons.length;
for (int i = 0; i < ilen; i++) {
Image icon = icons[i];
if (icon != null) {
prefW = Math.max(prefW, icon.getWidth());
prefH = Math.max(prefH, icon.getHeight());
}
}
String text = l.getText();
Font font = style.getFont();
if (font == null) {
System.out.println("Missing font for " + l);
font = Font.getDefaultFont();
}
if (text != null && text.length() > 0) {
// add the text size
switch(l.getTextPosition()) {
case Label.LEFT:
case Label.RIGHT:
prefW += font.stringWidth(text);
prefH = Math.max(prefH, font.getHeight());
break;
case Label.BOTTOM:
case Label.TOP:
prefW = Math.max(prefW, font.stringWidth(text));
prefH += font.getHeight();
break;
}
}
// add the state image(relevant for CheckBox\RadioButton)
if (stateImage != null) {
prefW += (stateImage.getWidth() + gap);
prefH = Math.max(prefH, stateImage.getHeight());
}
if (icons[0] != null && text != null && text.length() > 0) {
switch(l.getTextPosition()) {
case Label.LEFT:
case Label.RIGHT:
prefW += gap;
break;
case Label.BOTTOM:
case Label.TOP:
prefH += gap;
break;
}
}
if (l.isShowEvenIfBlank()) {
prefH += style.getVerticalPadding();
prefW += style.getHorizontalPadding();
} else {
if (prefH != 0) {
prefH += style.getVerticalPadding();
}
if (prefW != 0) {
prefW += style.getHorizontalPadding();
}
}
if (style.getBorder() != null) {
prefW = Math.max(style.getBorder().getMinimumWidth(), prefW);
prefH = Math.max(style.getBorder().getMinimumHeight(), prefH);
}
if (isBackgroundImageDetermineSize() && style.getBgImage() != null) {
prefW = Math.max(style.getBgImage().getWidth(), prefW);
prefH = Math.max(style.getBgImage().getHeight(), prefH);
}
return new Dimension(prefW, prefH);
}
use of com.codename1.components.Switch in project CodenameOne by codenameone.
the class DefaultLookAndFeel method calculateLabelSpan.
public Span calculateLabelSpan(TextSelection sel, Label l) {
Image icon = l.getMaskedIcon();
Image stateIcon = null;
int preserveSpaceForState = 0;
// setFG(g, l);
int gap = l.getGap();
int stateIconSize = 0;
int stateIconYPosition = 0;
String text = l.getText();
Style style = l.getStyle();
int cmpX = l.getX();
int cmpY = l.getY();
int cmpHeight = l.getHeight();
int cmpWidth = l.getWidth();
boolean rtl = l.isRTL();
int leftPadding = style.getPaddingLeft(rtl);
int rightPadding = style.getPaddingRight(rtl);
int topPadding = style.getPaddingTop();
int bottomPadding = style.getPaddingBottom();
Font font = style.getFont();
int fontHeight = 0;
if (text == null) {
text = "";
}
if (text.length() > 0) {
fontHeight = font.getHeight();
}
int x = cmpX + leftPadding;
int y = cmpY + topPadding;
boolean opposite = false;
if (stateIcon != null) {
// square image width == height
stateIconSize = stateIcon.getWidth();
preserveSpaceForState = stateIconSize + gap;
stateIconYPosition = cmpY + topPadding + (cmpHeight - topPadding - bottomPadding) / 2 - stateIconSize / 2;
int tX = cmpX;
if (((Button) l).isOppositeSide()) {
if (rtl) {
tX += leftPadding;
} else {
tX = tX + cmpWidth - leftPadding - stateIconSize;
}
cmpWidth -= leftPadding - stateIconSize;
preserveSpaceForState = 0;
opposite = true;
} else {
x = cmpX + leftPadding + preserveSpaceForState;
if (rtl) {
tX = tX + cmpWidth - leftPadding - stateIconSize;
} else {
tX += leftPadding;
}
}
// g.drawImage(stateIcon, tX, stateIconYPosition);
}
// default for bottom left alignment
int align = reverseAlignForBidi(l, style.getAlignment());
int textPos = reverseAlignForBidi(l, l.getTextPosition());
// set initial x,y position according to the alignment and textPosition
if (align == Component.LEFT) {
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
break;
}
} else if (align == Component.CENTER) {
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + ((icon != null) ? icon.getWidth() + l.getGap() : 0) + l.getStringWidth(font))) / 2;
if (!opposite) {
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
} else {
x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
}
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + Math.max(((icon != null) ? icon.getWidth() : 0), l.getStringWidth(font)))) / 2;
if (!opposite) {
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
} else {
x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
}
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
break;
}
} else if (align == Component.RIGHT) {
switch(textPos) {
case Label.LEFT:
case Label.RIGHT:
x = cmpX + cmpWidth - rightPadding - (((icon != null) ? (icon.getWidth() + gap) : 0) + l.getStringWidth(font));
if (l.isRTL()) {
if (!opposite) {
x = Math.max(x - preserveSpaceForState, cmpX + leftPadding);
} else {
x = Math.min(x - preserveSpaceForState, cmpX + leftPadding);
}
} else {
if (!opposite) {
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
} else {
x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
}
}
y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
break;
case Label.BOTTOM:
case Label.TOP:
x = cmpX + cmpWidth - rightPadding - (Math.max(((icon != null) ? (icon.getWidth()) : 0), l.getStringWidth(font)));
if (!opposite) {
x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
} else {
x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
}
y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
break;
}
}
int textSpaceW = cmpWidth - rightPadding - leftPadding;
if (icon != null && (textPos == Label.RIGHT || textPos == Label.LEFT)) {
textSpaceW = textSpaceW - icon.getWidth();
}
if (stateIcon != null) {
textSpaceW = textSpaceW - stateIconSize;
} else {
textSpaceW = textSpaceW - preserveSpaceForState;
}
if (icon == null) {
// no icon only string
return calculateSpanForLabelString(sel, l, text, x, y, textSpaceW);
} else {
int strWidth = l.getStringWidth(font);
int iconWidth = icon.getWidth();
int iconHeight = icon.getHeight();
int iconStringWGap;
int iconStringHGap;
switch(textPos) {
case Label.LEFT:
if (iconHeight > fontHeight) {
iconStringHGap = (iconHeight - fontHeight) / 2;
return calculateSpanForLabelStringValign(sel, l, text, x, y, iconStringHGap, iconHeight, textSpaceW, fontHeight);
} else {
iconStringHGap = (fontHeight - iconHeight) / 2;
// strWidth = drawLabelString(l, text, x, y, textSpaceW);
return calculateSpanForLabelString(sel, l, text, x, y, textSpaceW);
// g.drawImage(icon, x + strWidth + gap, y + iconStringHGap);
}
case Label.RIGHT:
if (iconHeight > fontHeight) {
iconStringHGap = (iconHeight - fontHeight) / 2;
// g.drawImage(icon, x, y);
return calculateSpanForLabelStringValign(sel, l, text, x + iconWidth + gap, y, iconStringHGap, iconHeight, textSpaceW, fontHeight);
} else {
iconStringHGap = (fontHeight - iconHeight) / 2;
// g.drawImage(icon, x, y + iconStringHGap);
return calculateSpanForLabelString(sel, l, text, x + iconWidth + gap, y, textSpaceW);
}
case Label.BOTTOM:
if (iconWidth > strWidth) {
// center align the smaller
iconStringWGap = (iconWidth - strWidth) / 2;
// g.drawImage(icon, x, y);
return calculateSpanForLabelString(sel, l, text, x + iconStringWGap, y + iconHeight + gap, textSpaceW);
} else {
iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
return calculateSpanForLabelString(sel, l, text, x, y + iconHeight + gap, textSpaceW);
}
case Label.TOP:
if (iconWidth > strWidth) {
// center align the smaller
iconStringWGap = (iconWidth - strWidth) / 2;
return calculateSpanForLabelString(sel, l, text, x + iconStringWGap, y, textSpaceW);
// g.drawImage(icon, x, y + fontHeight + gap);
} else {
iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
return calculateSpanForLabelString(sel, l, text, x, y, textSpaceW);
// g.drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
}
}
}
return sel.newSpan(l);
}
Aggregations