use of com.codename1.ui.html.HTMLElement in project CodenameOne by codenameone.
the class HTMLComponent method addQuote.
/**
* Adds a quote according to the current quote count
*
* @param quoteElement The quote element (TAG_Q)
* @param curAlign The current horizontal alignment
*/
private void addQuote(HTMLElement quoteElement, int curAlign, boolean isStartTag) {
String quote = null;
// 0 is the start tag of primary tag and 1 its closing tag. 2 is the start of secondary tag and 3 the closing tag (Used for CSS_QUOTES)
int quoteNum = isStartTag ? 0 : 1;
if (quoteTagCount == 0) {
quote = "\"";
} else {
quote = "'";
quoteNum += 2;
}
if ((FIXED_WIDTH) && (width - x < font.stringWidth(quote))) {
newLine(curAlign);
}
Label quoteLabel = addString(quote, curAlign);
quoteLabel.putClientProperty(CLIENT_PROPERTY_QUOTE, new Integer(quoteNum));
if (loadCSS) {
quoteElement.addAssociatedComponent(quoteLabel);
}
}
use of com.codename1.ui.html.HTMLElement in project CodenameOne by codenameone.
the class HTMLComponent method pushContainer.
private void pushContainer(HTMLElement element) {
if (loadCSS) {
Container cont = new Container(new BoxLayout(BoxLayout.Y_AXIS));
cont.setScrollableX(false);
// cont.getStyle().setBgColor(Element.COLOR_VALS[contCount]);
// contCount=(contCount+1)%Element.COLOR_VALS.length; // debug for CSS
// cont.getStyle().setBgTransparency(128);
cont.getStyle().setBgTransparency(0);
element.setAssociatedComponents(cont);
curContainer.addComponent(cont);
containers.addElement(curContainer);
curContainer = cont;
}
}
use of com.codename1.ui.html.HTMLElement in project CodenameOne by codenameone.
the class HTMLComponent method handleImageMapArea.
/**
* Handles an area definition for an image map
*
* @param areaTag The AREA tag
*/
private void handleImageMapArea(HTMLElement areaTag) {
if (curImageMap != null) {
String shape = areaTag.getAttributeById(HTMLElement.ATTR_SHAPE);
boolean supportedShape = false;
if (shape != null) {
String hrefStr = areaTag.getAttributeById(HTMLElement.ATTR_HREF);
if (shape.equalsIgnoreCase("default")) {
supportedShape = true;
curImageMap.setDefaultLink(hrefStr);
} else if ((shape.equalsIgnoreCase("rect")) || (shape.equalsIgnoreCase("circle"))) {
supportedShape = true;
String coordsStr = areaTag.getAttributeById(HTMLElement.ATTR_COORDS);
if ((coordsStr != null) && (hrefStr != null)) {
String curValStr = "";
int[] coords = new int[4];
int curCoord = 0;
boolean error = true;
try {
for (int c = 0; c < coordsStr.length(); c++) {
char ch = coordsStr.charAt(c);
if (ch != ',') {
curValStr += ch;
} else {
coords[curCoord] = Integer.parseInt(curValStr);
curCoord++;
curValStr = "";
}
}
if (curValStr.length() > 0) {
coords[curCoord] = Integer.parseInt(curValStr);
curCoord++;
}
if (shape.equalsIgnoreCase("rect")) {
if (curCoord == 4) {
curImageMap.addRectArea(new Rectangle(coords[0], coords[1], coords[2] - coords[0], coords[3] - coords[1]), hrefStr);
error = false;
}
} else if (curCoord == 3) {
// circle
// coords[2] is the radius, and 0,1 are the center x,y
curImageMap.addRectArea(new Rectangle(coords[0] - coords[2], coords[1] - coords[2], coords[2] * 2 + 1, coords[2] * 2 + 1), hrefStr);
// Note: The 'circle' SHAPE in AREA tag is implemented as a rectangle to avoid complication of circle pixel collision
error = false;
}
} catch (Exception e) {
// Can be number format exception or index out of bounds
// do nothing - error will stay true
}
if (error) {
notifyImageMapError("AREA tag 'coords' property value is invalid (should be exactly 3 comma seperated numbers for circle, 4 for rectangle): " + coordsStr, HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, HTMLElement.ATTR_COORDS, coordsStr);
}
}
}
}
if (!supportedShape) {
notifyImageMapError("Unsupported or missing AREA tag 'shape' property: " + shape, HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, HTMLElement.ATTR_SHAPE, shape);
}
} else {
notifyImageMapError("AREA tag is defined without a parent MAP tag - ignoring", HTMLCallback.ERROR_INVALID_TAG_HIERARCHY, -1, null);
}
}
use of com.codename1.ui.html.HTMLElement in project CodenameOne by codenameone.
the class HTMLComponent method handleImage.
/**
* Handles the IMG tag. This includes calculating its size (if available), applying any links/accesskeys and adding it to the download queue
*
* @param imgElement the IMG element
* @param align th current alignment
* @param cmd The submit command of a form, used only for INPUT type="image"
*/
private void handleImage(HTMLElement imgElement, int align, Command cmd) {
String imageUrl = imgElement.getAttributeById(HTMLElement.ATTR_SRC);
Label imgLabel = null;
if (imageUrl != null) {
String alignStr = imgElement.getAttributeById(HTMLElement.ATTR_ALIGN);
// Image width and height
int iWidth = calcSize(getWidth(), imgElement.getAttributeById(HTMLElement.ATTR_WIDTH), 0, false);
int iHeight = calcSize(getHeight(), imgElement.getAttributeById(HTMLElement.ATTR_HEIGHT), 0, false);
// Whitespace on the image sides (i.e. Margins)
int hspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_HSPACE));
int vspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_VSPACE));
int totalWidth = iWidth + hspace * 2;
if ((FIXED_WIDTH) && (x + totalWidth >= width)) {
newLine(align);
}
// Alternative image text, shown until image is loaded.
String altText = imgElement.getAttributeById(HTMLElement.ATTR_ALT);
String imageMap = imgElement.getAttributeById(HTMLElement.ATTR_USEMAP);
if (link != null) {
// This image is inside an A tag with HREF attribute
imgLabel = new HTMLLink(altText, link, this, mainLink, false);
if (mainLink == null) {
mainLink = (HTMLLink) imgLabel;
}
if (accesskey != '\0') {
// accessKeys.put(new Integer(accesskey), imgLabel);
addAccessKey(accesskey, imgLabel, false);
}
if (!PROCESS_HTML_MP1_ONLY) {
((HTMLLink) imgLabel).isMap = (imgElement.getAttributeById(HTMLElement.ATTR_ISMAP) != null);
}
} else if (cmd != null) {
// Special case of an image submit button
imgLabel = new Button(cmd);
if ((altText != null) && (!altText.equals(""))) {
imgLabel.setText(altText);
}
if (firstFocusable == null) {
firstFocusable = imgLabel;
}
} else if (imageMap != null) {
// Image Map
imgLabel = new HTMLImageMap(this);
if (imageMapComponents == null) {
imageMapComponents = new Hashtable();
}
if (imageMap.startsWith("#")) {
// Image map are denoted by # and then the map name (But we also tolerate if map is specified without #)
imageMap = imageMap.substring(1);
}
imageMapComponents.put(imageMap, imgLabel);
if ((imageMapData != null) && (imageMapData.containsKey(imageMap))) {
ImageMapData data = (ImageMapData) imageMapData.get(imageMap);
((HTMLImageMap) imgLabel).mapData = data;
}
} else {
imgLabel = new Label(altText);
}
if ((iWidth != 0) || (iHeight != 0)) {
// reserve space while loading image if either width or height are specified, otherwise we don't know how much to reserve
iWidth += imgLabel.getStyle().getPadding(Component.LEFT) + imgLabel.getStyle().getPadding(Component.RIGHT);
iHeight += imgLabel.getStyle().getPadding(Component.TOP) + imgLabel.getStyle().getPadding(Component.BOTTOM);
imgLabel.setPreferredSize(new Dimension(iWidth, iHeight));
} else {
// If no space is reserved, make a minimal text, otherwise Codename One won't calculate the size right after the image loads
if ((imgLabel.getText() == null) || (imgLabel.getText().equals(""))) {
imgLabel.setText(" ");
}
}
// It is important that the padding of the image component itself will be all 0
// This is because when the image is loaded, its preferred size is checked to see if its width/height were preset by the width/height attribute
imgLabel.getSelectedStyle().setPadding(0, 0, 0, 0);
imgLabel.getUnselectedStyle().setPadding(0, 0, 0, 0);
imgLabel.getSelectedStyle().setFont(font.getFont());
imgLabel.getUnselectedStyle().setFont(font.getFont());
int borderSize = getInt(imgElement.getAttributeById(HTMLElement.ATTR_BORDER));
if (borderSize != 0) {
imgLabel.putClientProperty(CLIENT_PROPERTY_IMG_BORDER, new Integer(borderSize));
} else {
borderSize = 1;
}
imgLabel.getUnselectedStyle().setBorder(Border.createLineBorder(borderSize));
imgLabel.getSelectedStyle().setBorder(Border.createLineBorder(borderSize));
imgLabel.getUnselectedStyle().setBgTransparency(0);
imgLabel.getSelectedStyle().setBgTransparency(0);
Container imgCont = new Container(new BorderLayout());
imgCont.addComponent(BorderLayout.CENTER, imgLabel);
imgCont.getSelectedStyle().setMargin(vspace, vspace, hspace, hspace);
imgCont.getUnselectedStyle().setMargin(vspace, vspace, hspace, hspace);
curLine.addComponent(imgCont);
x += totalWidth;
// Alignment
imgLabel.setAlignment(getHorizAlign(alignStr, align, false));
imgLabel.setVerticalAlignment(getVertAlign(alignStr, Component.CENTER));
if (showImages) {
if (docInfo != null) {
imageUrl = docInfo.convertURL(imageUrl);
threadQueue.add(imgLabel, imageUrl);
} else {
if (DocumentInfo.isAbsoluteURL(imageUrl)) {
threadQueue.add(imgLabel, imageUrl);
} else {
if (htmlCallback != null) {
htmlCallback.parsingError(HTMLCallback.ERROR_NO_BASE_URL, imgElement.getTagName(), imgElement.getAttributeName(new Integer(HTMLElement.ATTR_SRC)), imageUrl, "Ignoring Image file referred in an IMG tag (" + imageUrl + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
}
}
}
}
if (loadCSS) {
imgElement.setAssociatedComponents(imgCont);
}
}
}
use of com.codename1.ui.html.HTMLElement in project CodenameOne by codenameone.
the class CSSEngine method setWrapRecursive.
/**
* Sets this element and all children to have wrapped text.
* In cases where text is already wrapped no change will be made.
* This will work only in FIXED_WIDTH mode (Checked before called)
* Technically all this logic can be found in HTMLComponent.showText, but since we don't want to get into
* the context of this element (i.e. what was the indentation, alignment etc.), we use this algorithm.
*
* @param element The element to apply text wrapping on
* @param htmlC The HTMLComponent
*/
private void setWrapRecursive(HTMLElement element, HTMLComponent htmlC) {
if (element.isTextElement()) {
String text = element.getText();
final Vector ui = element.getUi();
if ((text != null) && (ui != null) && (ui.size() == 1)) {
// If it's already wrapped, no need to process
final Vector words = htmlC.getWords(text, Component.LEFT, false);
final Label label = (Label) ui.elementAt(0);
setWrapText(label, words, element, htmlC);
}
}
for (int i = 0; i < element.getNumChildren(); i++) {
setWrapRecursive((HTMLElement) element.getChildAt(i), htmlC);
}
}
Aggregations