use of com.codename1.ui.TextSelection in project CodenameOne by codenameone.
the class TextSelection method selectAll.
/**
* Selects all of the selectable text in the TextSelection (generally on the current form).
*/
public void selectAll() {
if (selectionMask == null) {
selectionMask = new SelectionMask();
getLayeredPane().add(selectionMask);
}
selectionRoot = root;
selectedBounds.setBounds(0, 0, selectionRoot.getWidth(), selectionRoot.getHeight());
update();
selectionRoot.getComponentForm().revalidateLater();
textSelectionListeners.fireActionEvent(new ActionEvent(TextSelection.this, Type.Change));
}
use of com.codename1.ui.TextSelection in project CodenameOne by codenameone.
the class DefaultLookAndFeel method append.
private void append(TextSelection sel, Component l, Span span, String text, Font f, int posOffset, int x, int y, int h) {
int len = text.length();
int xPos = 0;
int curPos = 1;
// int ty = l.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - l.getY();
while (curPos <= len) {
int newXpos = f.stringWidth(text.substring(0, curPos));
Char next = sel.newChar(posOffset + curPos - 1, x + xPos, y, newXpos - xPos, h);
span.add(next);
xPos = newXpos;
curPos++;
}
}
use of com.codename1.ui.TextSelection in project CodenameOne by codenameone.
the class DefaultLookAndFeel method calculateTextFieldSpan.
public Spans calculateTextFieldSpan(TextSelection sel, TextArea ta) {
// setFG(g, ta);
Span out = sel.newSpan(ta);
// display ******** if it is a password field
String displayText = getTextFieldString(ta);
Style style = ta.getStyle();
int x = 0;
// ta.getCursorX();
int cursorCharPosition = ta.hasFocus() ? ta.getCursorPosition() : 0;
Font f = style.getFont();
int cursorX = 0;
int xPos = 0;
int align = reverseAlignForBidi(ta);
int displayX = 0;
String inputMode = ta.getInputMode();
int inputModeWidth = f.stringWidth(inputMode);
// QWERTY devices don't quite have an input mode hide it also when we have a VK
if (!Display.getInstance().platformUsesInputMode() || ta.isQwertyInput() || Display.getInstance().isVirtualKeyboardShowing()) {
inputMode = "";
inputModeWidth = 0;
}
if (ta.isSingleLineTextArea()) {
// there is currently no support for CENTER aligned text fields
if (align == Component.LEFT) {
if (cursorCharPosition > 0) {
cursorCharPosition = Math.min(displayText.length(), cursorCharPosition);
xPos = f.stringWidth(displayText.substring(0, cursorCharPosition));
cursorX = ta.getX() + style.getPaddingLeft(ta.isRTL()) + xPos;
// no point in showing the input mode when there is only one input mode...
if (inputModeWidth > 0 && ta.getInputModeOrder() != null && ta.getInputModeOrder().length == 1) {
inputModeWidth = 0;
}
if (ta.isEnableInputScroll()) {
if (ta.getWidth() > (f.getHeight() * 2) && cursorX >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL())) {
if (x + xPos >= ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL()) * 2) {
x = ta.getWidth() - inputModeWidth - style.getPaddingLeft(ta.isRTL()) * 2 - xPos - 1;
}
}
}
}
displayX = ta.getX() + x + style.getPaddingLeft(ta.isRTL());
} else {
x = 0;
cursorX = getTextFieldCursorX(ta);
int baseX = ta.getX() + style.getPaddingLeftNoRTL() + inputModeWidth;
int endX = ta.getX() + ta.getWidth() - style.getPaddingRightNoRTL();
if (cursorX < baseX) {
x = baseX - cursorX;
} else {
if (cursorX > endX) {
x = endX - cursorX;
}
}
displayX = ta.getX() + ta.getWidth() - style.getPaddingRightNoRTL() - style.getPaddingLeftNoRTL() - f.stringWidth(displayText) + x;
}
// int cx = g.getClipX();
// int cy = g.getClipY();
// int cw = g.getClipWidth();
// int ch = g.getClipHeight();
// int clipx = ta.getX() + style.getPaddingLeft(ta.isRTL());
// int clipw = ta.getWidth() - style.getPaddingLeft(ta.isRTL()) - style.getPaddingRight(ta.isRTL());
// g.pushClip();
// g.clipRect(clipx, cy, clipw, ch);
// int xOffset = 0;
// int len = displayText.length();
// int charH = f.getHeight();
int h = getSelectionHeight(f);
switch(ta.getVerticalAlignment()) {
case Component.BOTTOM:
// g.drawString(displayText, displayX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight(), style.getTextDecoration());
append(sel, ta, out, displayText, f, 0, displayX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - h, h);
// c = sel.newChar(i, charX, ta.getY() + ta.getHeight() - style.getPaddingBottom() - f.getHeight(), charW , charH);
break;
case Component.CENTER:
// g.drawString(displayText, displayX, ta.getY() + ta.getHeight() / 2 - f.getHeight() / 2, style.getTextDecoration());
// c = sel.newChar(i, charX, ta.getY() + ta.getHeight() / 2 - f.getHeight() / 2, charW, charH);
append(sel, ta, out, displayText, f, 0, displayX, ta.getY() + ta.getHeight() / 2 - h / 2, h);
break;
default:
// g.drawString(displayText, displayX, ta.getY() + style.getPaddingTop(), style.getTextDecoration());
// c = sel.newChar(i, charX, ta.getY() + style.getPaddingTop(), charW, charH);
append(sel, ta, out, displayText, f, 0, displayX, ta.getY() + style.getPaddingTop(), h);
break;
}
// g.setClip(cx, cy, cw, ch);
// g.popClip();
out = out.translate(ta.getAbsoluteX() - sel.getSelectionRoot().getAbsoluteX() - ta.getX(), ta.getAbsoluteY() - sel.getSelectionRoot().getAbsoluteY() - ta.getY());
Spans spansOut = sel.newSpans();
spansOut.add(out);
return spansOut;
} else {
// drawTextArea(g, ta);
return calculateTextAreaSpan(sel, ta);
}
}
use of com.codename1.ui.TextSelection in project CodenameOne by codenameone.
the class TextSelectionSample method start.
public void start() {
if (current != null) {
current.show();
return;
}
Form hi = new Form("Hi World", BoxLayout.y());
hi.setScrollableY(false);
TextSelection sel = hi.getTextSelection();
sel.addTextSelectionListener(e -> {
// System.out.println("Text selection has changed");
});
sel.setEnabled(true);
Label label = new Label("This label should be selectable");
label.setTextSelectionEnabled(true);
Label label2 = new Label("Some more text");
label2.setTextSelectionEnabled(true);
hi.add(label);
hi.add(new TextField("Hello Universe"));
hi.add(label2);
hi.add(new Label("Hi World"));
Container cnt = new Container(BoxLayout.x());
cnt.setScrollableX(true);
cnt.getStyle().setBorder(Border.createLineBorder(1, 0x0));
cnt.setPreferredH(CN.convertToPixels(5));
cnt.setPreferredW(CN.convertToPixels(20));
TextArea ta = new TextArea();
ta.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.");
ta.setEnabled(false);
ta.setRows(6);
hi.add(ta);
SpanLabel sl = new SpanLabel();
sl.setText(ta.getText());
sl.setTextSelectionEnabled(true);
hi.add(sl);
TextField tf = new TextField();
tf.setText("Hello World. This is a test field");
tf.setEnabled(false);
hi.add(tf);
Label l = new Label("This is a test with some long text to see if this works. It should just flow when it runs out of space");
l.setTextSelectionEnabled(true);
cnt.add(l);
Container cntY = new Container(BoxLayout.y());
cntY.setScrollableY(true);
cntY.getStyle().setBorder(Border.createLineBorder(1, 0x0));
for (int i = 0; i < 50; i++) {
Label li = new Label("List item " + i);
li.setTextSelectionEnabled(true);
cntY.add(li);
}
hi.add(cnt);
hi.add(cntY);
$(cnt, cntY).selectAllStyles().setMarginMillimeters(4);
hi.show();
}
use of com.codename1.ui.TextSelection 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