Search in sources :

Example 11 with HTMLComponent

use of com.codename1.ui.html.HTMLComponent 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);
    }
}
Also used : Label(com.codename1.ui.Label) Vector(java.util.Vector)

Example 12 with HTMLComponent

use of com.codename1.ui.html.HTMLComponent in project CodenameOne by codenameone.

the class CSSEngine method setWrapText.

/**
 * Replaces an unwrapped text with a wrapped version, while copying the style of the original text.
 *
 * @param label The current label that contains the unwrapped text
 * @param words A vector containing one word of the text (without white spaces) in each element
 * @param element The text element
 */
private void setWrapText(Label label, Vector words, HTMLElement element, HTMLComponent htmlC) {
    Style selectedStyle = label.getSelectedStyle();
    Style unselectedStyle = label.getUnselectedStyle();
    Vector ui = new Vector();
    label.setText((String) words.elementAt(0) + ' ');
    HTMLLink link = null;
    if (label instanceof HTMLLink) {
        link = (HTMLLink) label;
    }
    ui.addElement(label);
    for (int i = 1; i < words.size(); i++) {
        Label word = null;
        if (link != null) {
            word = new HTMLLink((String) words.elementAt(i) + ' ', link.link, htmlC, link, link.linkVisited);
        } else {
            word = new Label((String) words.elementAt(i) + ' ');
        }
        word.setSelectedStyle(selectedStyle);
        word.setUnselectedStyle(unselectedStyle);
        label.getParent().addComponent(word);
        ui.addElement(word);
    }
    element.setAssociatedComponents(ui);
    label.getParent().revalidate();
}
Also used : Label(com.codename1.ui.Label) Style(com.codename1.ui.plaf.Style) Vector(java.util.Vector)

Example 13 with HTMLComponent

use of com.codename1.ui.html.HTMLComponent in project CodenameOne by codenameone.

the class CSSEngine method setNowrapRecursive.

/**
 * Sets this element and all children to have unwrapped text.
 * In cases where text is already unwrapped no change will be made.
 * This will work only in FIXED_WIDTH mode (Checked before called)
 * Technically a lot of this logic can be found in HTMLComponent, 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
 */
private void setNowrapRecursive(final HTMLElement element) {
    // if (element.getId()==HTMLElement.TAG_TEXT) {
    if (element.isTextElement()) {
        // String text=element.getAttributeById(HTMLElement.ATTR_TITLE);
        String text = element.getText();
        final Vector ui = element.getUi();
        if ((text != null) && (ui != null) && (ui.size() > 1)) {
            // If it's just one word or already no-wrapped, no need to process
            String word = "";
            String newText = "";
            for (int c = 0; c < text.length(); c++) {
                char ch = text.charAt(c);
                if ((ch == ' ') || (ch == 10) || (ch == 13) || (ch == '\t') || (ch == '\n')) {
                    if (!word.equals("")) {
                        newText += word + " ";
                        word = "";
                    }
                } else {
                    word += ch;
                }
            }
            if (!word.equals("")) {
                newText += word + " ";
            }
            final Label label = (Label) ui.elementAt(0);
            setNowrapText(label, ui, newText, element);
        }
    }
    for (int i = 0; i < element.getNumChildren(); i++) {
        setNowrapRecursive((HTMLElement) element.getChildAt(i));
    }
    // If children elements' UI was changed, we need to recalc the UI of the parent
    element.recalcUi();
}
Also used : Label(com.codename1.ui.Label) Vector(java.util.Vector)

Example 14 with HTMLComponent

use of com.codename1.ui.html.HTMLComponent in project CodenameOne by codenameone.

the class CSSEngine method applyStyle.

/**
 * Applies the given style attributes to the HTML DOM entry
 *
 * @param element The element to apply the style to
 * @param selector The selector containing the style directives
 * @param htmlC The HTMLComponent
 */
private void applyStyle(HTMLElement element, CSSElement selector, HTMLComponent htmlC) {
    if ((element.getUi() != null) && (element.getUi().size() > 0)) {
        if (!HTMLComponent.PROCESS_HTML_MP1_ONLY) {
            String reset = selector.getAttributeById(CSSElement.CSS_COUNTER_RESET);
            if (reset != null) {
                htmlC.incCounter(reset, true);
            }
            String inc = selector.getAttributeById(CSSElement.CSS_COUNTER_INCREMENT);
            if (inc != null) {
                htmlC.incCounter(inc, false);
            }
            if ((selector.getSelectorPseudoClass() & (CSSElement.PC_BEFORE | CSSElement.PC_AFTER)) != 0) {
                handleContentProperty(element, selector, htmlC);
                return;
            }
        }
        for (int iter = 0; iter < element.getUi().size(); iter++) {
            Object o = element.getUi().elementAt(iter);
            if (o != null && o instanceof Component) {
                final Component cmp = (Component) o;
                applyStyleToUIElement(cmp, selector, element, htmlC);
            }
        }
    }
}
Also used : Component(com.codename1.ui.Component)

Example 15 with HTMLComponent

use of com.codename1.ui.html.HTMLComponent 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);
        }
    }
}
Also used : Font(com.codename1.ui.Font)

Aggregations

Vector (java.util.Vector)9 Label (com.codename1.ui.Label)6 Component (com.codename1.ui.Component)5 Container (com.codename1.ui.Container)4 Enumeration (java.util.Enumeration)3 Hashtable (java.util.Hashtable)3 TextArea (com.codename1.ui.TextArea)2 HTMLComponent (com.codename1.ui.html.HTMLComponent)2 Style (com.codename1.ui.plaf.Style)2 ConnectionRequest (com.codename1.io.ConnectionRequest)1 Button (com.codename1.ui.Button)1 ComboBox (com.codename1.ui.ComboBox)1 Font (com.codename1.ui.Font)1 Form (com.codename1.ui.Form)1 RadioButton (com.codename1.ui.RadioButton)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 AsyncDocumentRequestHandlerImpl (com.codename1.ui.html.AsyncDocumentRequestHandlerImpl)1 DocumentInfo (com.codename1.ui.html.DocumentInfo)1 HTMLElement (com.codename1.ui.html.HTMLElement)1