use of com.codename1.ui.Button in project CodenameOne by codenameone.
the class GenericListCellRenderer method extractLastClickedComponent.
/**
* Allows partitioning the renderer into "areas" that can be clicked. When
* receiving an action event in the list this method allows a developer to
* query the renderer to "see" whether a button within the component was "touched"
* by the user on a touch screen device.
* This method will reset the value to null after returning a none-null value!
*
* @return a button or null
*/
public Button extractLastClickedComponent() {
Button c = lastClickedComponent;
lastClickedComponent = null;
return c;
}
use of com.codename1.ui.Button in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawComponent.
private void drawComponent(Graphics g, Label l, Image icon, Image stateIcon, int preserveSpaceForState) {
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() + 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 + ((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
drawLabelString(g, 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;
strWidth = drawLabelStringValign(g, l, text, x, y, iconStringHGap, iconHeight, textSpaceW, fontHeight);
g.drawImage(icon, x + strWidth + gap, y);
} else {
iconStringHGap = (fontHeight - iconHeight) / 2;
strWidth = drawLabelString(g, l, text, x, y, textSpaceW);
g.drawImage(icon, x + strWidth + gap, y + iconStringHGap);
}
break;
case Label.RIGHT:
if (iconHeight > fontHeight) {
iconStringHGap = (iconHeight - fontHeight) / 2;
g.drawImage(icon, x, y);
drawLabelStringValign(g, l, text, x + iconWidth + gap, y, iconStringHGap, iconHeight, textSpaceW, fontHeight);
} else {
iconStringHGap = (fontHeight - iconHeight) / 2;
g.drawImage(icon, x, y + iconStringHGap);
drawLabelString(g, l, text, x + iconWidth + gap, y, textSpaceW);
}
break;
case Label.BOTTOM:
if (iconWidth > strWidth) {
// center align the smaller
iconStringWGap = (iconWidth - strWidth) / 2;
g.drawImage(icon, x, y);
drawLabelString(g, l, text, x + iconStringWGap, y + iconHeight + gap, textSpaceW);
} else {
iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
g.drawImage(icon, x + iconStringWGap, y);
drawLabelString(g, l, text, x, y + iconHeight + gap, textSpaceW);
}
break;
case Label.TOP:
if (iconWidth > strWidth) {
// center align the smaller
iconStringWGap = (iconWidth - strWidth) / 2;
drawLabelString(g, l, text, x + iconStringWGap, y, textSpaceW);
g.drawImage(icon, x, y + fontHeight + gap);
} else {
iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
drawLabelString(g, l, text, x, y, textSpaceW);
g.drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
}
break;
}
}
}
use of com.codename1.ui.Button in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawRadioButton.
/**
* {@inheritDoc}
*/
public void drawRadioButton(Graphics g, Button rb) {
if (rButtonImages != null) {
Image x;
if (rButtonImagesFocus != null && rButtonImagesFocus[0] != null && rb.hasFocus() && Display.getInstance().shouldRenderSelection(rb)) {
if (rb.isEnabled()) {
x = rButtonImagesFocus[rb.isSelected() ? 1 : 0];
} else {
x = rButtonImagesFocus[rb.isSelected() ? 3 : 2];
}
} else {
if (rb.isEnabled()) {
x = rButtonImages[rb.isSelected() ? 1 : 0];
} else {
x = rButtonImages[rb.isSelected() ? 3 : 2];
}
}
drawComponent(g, rb, rb.getIconFromState(), x, 0);
} else {
Style style = rb.getStyle();
// radio button radius needs to be of the size of the font height even
// when no text is in the radio button this is a good indication of phone DPI
int height = rb.getStyle().getFont().getHeight();
drawComponent(g, rb, rb.getIconFromState(), null, height + rb.getGap());
g.setColor(style.getFgColor());
int x = rb.getX();
if (rb.isRTL()) {
x = x + rb.getWidth() - style.getPaddingLeft(rb.isRTL()) - height;
} else {
x += style.getPaddingLeft(rb.isRTL());
}
int y = rb.getY();
// center the RadioButton
y += Math.max(0, rb.getHeight() / 2 - height / 2);
g.drawArc(x, y, height, height, 0, 360);
if (rb.isSelected()) {
int color = g.getColor();
int destColor = findDestColor(color);
g.fillRadialGradient(color, destColor, x + 3, y + 3, height - 5, height - 5);
}
}
}
use of com.codename1.ui.Button in project CodenameOne by codenameone.
the class DefaultLookAndFeel method drawCheckBox.
/**
* {@inheritDoc}
*/
public void drawCheckBox(Graphics g, Button cb) {
if (chkBoxImages != null) {
Image x;
if (chkBoxImagesFocus != null && chkBoxImagesFocus[0] != null && cb.hasFocus() && Display.getInstance().shouldRenderSelection(cb)) {
if (cb.isEnabled()) {
x = chkBoxImagesFocus[cb.isSelected() ? 1 : 0];
} else {
x = chkBoxImagesFocus[cb.isSelected() ? 3 : 2];
}
} else {
if (cb.isEnabled()) {
x = chkBoxImages[cb.isSelected() ? 1 : 0];
} else {
x = chkBoxImages[cb.isSelected() ? 3 : 2];
}
}
drawComponent(g, cb, cb.getIconFromState(), x, 0);
} else {
Style style = cb.getStyle();
// checkbox square needs to be the height and width of the font height even
// when no text is in the check box this is a good indication of phone DPI
int height = cb.getStyle().getFont().getHeight();
drawComponent(g, cb, cb.getIconFromState(), null, height);
int gradientColor;
g.setColor(style.getFgColor());
gradientColor = style.getBgColor();
int width = height;
int rectWidth = scaleCoordinate(12f, 16, width);
int tX = cb.getX();
if (cb.isRTL()) {
tX = tX + cb.getWidth() - style.getPaddingLeft(cb.isRTL()) - rectWidth;
} else {
tX += style.getPaddingLeft(cb.isRTL());
}
int tY = cb.getY() + style.getPaddingTop() + (cb.getHeight() - style.getPaddingTop() - style.getPaddingBottom()) / 2 - height / 2;
g.translate(tX, tY);
int x = scaleCoordinate(1.04f, 16, width);
int y = scaleCoordinate(4.0f, 16, height);
int rectHeight = scaleCoordinate(12f, 16, height);
// brighten or darken the color slightly
int destColor = findDestColor(gradientColor);
g.fillLinearGradient(gradientColor, destColor, x + 1, y + 1, rectWidth - 2, rectHeight - 1, false);
g.drawRoundRect(x, y, rectWidth, rectHeight, 5, 5);
if (cb.isSelected()) {
int color = g.getColor();
g.setColor(0x111111);
g.translate(0, 1);
fillCheckbox(g, width, height);
g.setColor(color);
g.translate(0, -1);
fillCheckbox(g, width, height);
}
g.translate(-tX, -tY);
}
}
use of com.codename1.ui.Button in project CodenameOne by codenameone.
the class DefaultLookAndFeel method getRadioButtonPreferredSize.
/**
* {@inheritDoc}
*/
public Dimension getRadioButtonPreferredSize(Button rb) {
if (rb.isToggle()) {
return getButtonPreferredSize(rb);
}
threeImageCache[0] = rb.getMaskedIcon();
threeImageCache[1] = rb.getRolloverIcon();
threeImageCache[2] = rb.getPressedIcon();
if (rButtonImages != null) {
return getPreferredSize(rb, threeImageCache, rButtonImages[0]);
}
Dimension d = getPreferredSize(rb, threeImageCache, null);
// radio button radius needs to be of the size of the font height even
// when no text is in the radio button this is a good indication of phone DPI
int height = rb.getStyle().getFont().getHeight();
// allow for radio buttons without a string within them
d.setHeight(Math.max(height, d.getHeight()));
if (rButtonImages != null && rButtonImages.length > 0) {
d.setWidth(rButtonImages[0].getWidth() + d.getWidth() + rb.getGap());
} else {
d.setWidth(d.getWidth() + height + rb.getGap());
}
return d;
}
Aggregations