Search in sources :

Example 1 with Theme

use of com.codename1.ui.util.xml.Theme in project CodenameOne by codenameone.

the class Border method paintBorderBackground.

private void paintBorderBackground(Graphics g, final int xParameter, final int yParameter, final int widthParameter, final int heightParameter, Component c) {
    int originalColor = g.getColor();
    int x = xParameter;
    int y = yParameter;
    int width = widthParameter;
    int height = heightParameter;
    switch(type) {
        case TYPE_ROUNDED_PRESSED:
            x++;
            y++;
            width -= 2;
            height -= 2;
        case TYPE_ROUNDED:
            // Removing this due to issue 301, not sure regarding this...
            // width--;
            // height--;
            // rounded is also responsible for drawing the background
            Style s = c.getStyle();
            if ((s.getBgImage() != null && s.getBackgroundType() == Style.BACKGROUND_IMAGE_SCALED) || s.getBackgroundType() > 1) {
                Object w = s.roundRectCache;
                Image i = null;
                if (w != null) {
                    i = (Image) Display.getInstance().extractHardRef(w);
                }
                if (i != null && i.getWidth() == width && i.getHeight() == height) {
                    g.drawImage(i, x, y);
                } else {
                    // we need to draw a background image!
                    i = Image.createImage(width, height);
                    Graphics imageG = i.getGraphics();
                    imageG.setColor(0);
                    imageG.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);
                    int[] rgb = i.getRGBCached();
                    int transColor = rgb[0];
                    int[] imageRGB;
                    if (s.getBackgroundType() == Style.BACKGROUND_IMAGE_SCALED) {
                        imageRGB = s.getBgImage().scaled(width, height).getRGBCached();
                    } else {
                        Image bgPaint = Image.createImage(width, height);
                        Painter p = s.getBgPainter();
                        // might occur during temporary conditions in the theme switching
                        if (p == null) {
                            return;
                        }
                        p.paint(bgPaint.getGraphics(), new Rectangle(0, 0, width, height));
                        imageRGB = bgPaint.getRGB();
                    }
                    int rlen = rgb.length;
                    for (int iter = 0; iter < rlen; iter++) {
                        if (rgb[iter] == transColor) {
                            imageRGB[iter] = 0;
                        }
                    }
                    i = Image.createImage(imageRGB, width, height);
                    s.roundRectCache = Display.getInstance().createSoftWeakRef(i);
                    g.drawImage(i, x, y);
                }
            } else {
                int foreground = g.getColor();
                g.setColor(s.getBgColor());
                // Its opaque much easier job!
                if (s.getBgTransparency() == ((byte) 0xff)) {
                    g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
                } else {
                    if (g.isAlphaSupported()) {
                        int alpha = g.getAlpha();
                        g.setAlpha(s.getBgTransparency() & 0xff);
                        g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
                        g.setAlpha(alpha);
                    } else {
                        // translucent... well....
                        if (s.getBgTransparency() != 0) {
                            Image i = Image.createImage(width, height);
                            int[] imageRgb;
                            if (g.getColor() != 0xffffff) {
                                Graphics imageG = i.getGraphics();
                                imageG.setColor(g.getColor());
                                imageG.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);
                                imageRgb = i.getRGBCached();
                            } else {
                                // background color is white we need to remove a different color
                                // black is the only other "reliable" color on the device
                                Graphics imageG = i.getGraphics();
                                imageG.setColor(0);
                                imageG.fillRect(0, 0, width, height);
                                imageG.setColor(g.getColor());
                                imageG.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);
                                imageRgb = i.getRGBCached();
                            }
                            int removeColor = imageRgb[0];
                            int size = width * height;
                            int alphaInt = ((s.getBgTransparency() & 0xff) << 24) & 0xff000000;
                            for (int iter = 0; iter < size; iter++) {
                                if (removeColor == imageRgb[iter]) {
                                    imageRgb[iter] = 0;
                                    continue;
                                }
                                if ((imageRgb[iter] & 0xff000000) != 0) {
                                    imageRgb[iter] = (imageRgb[iter] & 0xffffff) | alphaInt;
                                }
                            }
                            g.drawImage(new RGBImage(imageRgb, width, height), x, y);
                        }
                    }
                }
                g.setColor(foreground);
            }
            break;
        case TYPE_IMAGE:
            {
                Image topLeft = images[4];
                Image topRight = images[5];
                Image bottomLeft = images[6];
                Image center = images[8];
                x += topLeft.getWidth();
                y += topLeft.getHeight();
                height -= (topLeft.getHeight() + bottomLeft.getHeight());
                width -= (topLeft.getWidth() + topRight.getWidth());
                if (center != null) {
                    g.tileImage(center, x, y, width, height);
                }
                Image top = images[0];
                Image bottom = images[1];
                Image left = images[2];
                Image right = images[3];
                Image bottomRight = images[7];
                x = xParameter;
                y = yParameter;
                width = widthParameter;
                height = heightParameter;
                g.drawImage(topLeft, x, y);
                g.drawImage(bottomLeft, x, y + height - bottomLeft.getHeight());
                g.drawImage(topRight, x + width - topRight.getWidth(), y);
                g.drawImage(bottomRight, x + width - bottomRight.getWidth(), y + height - bottomRight.getHeight());
                Image arrowDownImage = null;
                Image arrowUpImage = null;
                Image arrowLeftImage = null;
                Image arrowRightImage = null;
                int arrowPosition = 0;
                // we need to draw an arrow on one of the sides
                if (trackComponent != null) {
                    int cabsY = c.getAbsoluteY();
                    int trackY = trackComponent.getY();
                    int trackX = trackComponent.getX();
                    int cabsX = c.getAbsoluteX();
                    if (cabsY >= trackY + trackComponent.getHeight()) {
                        // we are below the component
                        arrowUpImage = specialTile[0];
                        arrowPosition = (trackX + trackComponent.getWidth() / 2) - cabsX - arrowUpImage.getWidth() / 2;
                    } else {
                        if (cabsY + c.getHeight() <= trackY) {
                            // we are above the component
                            arrowDownImage = specialTile[1];
                            arrowPosition = (trackX + trackComponent.getWidth() / 2) - cabsX - arrowDownImage.getWidth() / 2;
                        } else {
                            if (cabsX >= trackX + trackComponent.getWidth()) {
                                // we are to the right of the component
                                arrowLeftImage = specialTile[2];
                                arrowPosition = (trackY + trackComponent.getHeight() / 2) - cabsY - arrowLeftImage.getHeight() / 2;
                            } else {
                                if (cabsX + c.getWidth() <= trackX) {
                                    // we are to the left of the component
                                    arrowRightImage = specialTile[3];
                                    arrowPosition = (trackY + trackComponent.getHeight() / 2) - cabsY - arrowRightImage.getHeight() / 2;
                                }
                            }
                        }
                    }
                }
                drawImageBorderLine(g, topLeft, topRight, top, x, y, width, arrowUpImage, arrowPosition, false);
                drawImageBorderLine(g, bottomLeft, bottomRight, bottom, x, y + height - bottom.getHeight(), width, arrowDownImage, arrowPosition, true);
                drawImageBorderColumn(g, topLeft, bottomLeft, left, x, y, height, arrowLeftImage, arrowPosition, false);
                drawImageBorderColumn(g, topRight, bottomRight, right, x + width - right.getWidth(), y, height, arrowRightImage, arrowPosition, true);
                break;
            }
        case TYPE_IMAGE_SCALED:
            {
                int clipX = g.getClipX();
                int clipY = g.getClipY();
                int clipWidth = g.getClipWidth();
                int clipHeight = g.getClipHeight();
                // g.pushClip();
                Image topLeft = images[4];
                Image topRight = images[5];
                Image bottomLeft = images[6];
                Image center = images[8];
                x += topLeft.getWidth();
                y += topLeft.getHeight();
                height -= (topLeft.getHeight() + bottomLeft.getHeight());
                width -= (topLeft.getWidth() + topRight.getWidth());
                g.clipRect(x, y, width, height);
                if (center != null && width > 0 && height > 0) {
                    int centerWidth = center.getWidth();
                    int centerHeight = center.getHeight();
                    g.drawImage(center, x, y, width, height);
                }
                Image top = images[0];
                Image bottom = images[1];
                Image left = images[2];
                Image right = images[3];
                Image bottomRight = images[7];
                g.setClip(clipX, clipY, clipWidth, clipHeight);
                // g.popClip();
                // g.pushClip();
                x = xParameter;
                y = yParameter;
                width = widthParameter;
                height = heightParameter;
                g.drawImage(topLeft, x, y);
                g.drawImage(bottomLeft, x, y + height - bottomLeft.getHeight());
                g.drawImage(topRight, x + width - topRight.getWidth(), y);
                g.drawImage(bottomRight, x + width - bottomRight.getWidth(), y + height - bottomRight.getHeight());
                drawImageBorderLineScale(g, topLeft, topRight, top, x, y, width);
                drawImageBorderLineScale(g, bottomLeft, bottomRight, bottom, x, y + height - bottom.getHeight(), width);
                drawImageBorderColumnScale(g, topLeft, bottomLeft, left, x, y, height);
                drawImageBorderColumnScale(g, topRight, bottomRight, right, x + width - right.getWidth(), y, height);
                g.setClip(clipX, clipY, clipWidth, clipHeight);
                // g.popClip();
                break;
            }
        case TYPE_IMAGE_HORIZONTAL:
            {
                Image left = images[0];
                Image right = images[1];
                Image center = images[2];
                if (c.getUIManager().isThemeConstant("centerAlignHBorderBool", false)) {
                    y += Math.max(0, height / 2 - center.getHeight() / 2);
                }
                g.drawImage(left, x, y);
                g.drawImage(right, x + width - right.getWidth(), y);
                g.tileImage(center, x + left.getWidth(), y, width - left.getWidth() - right.getWidth(), center.getHeight());
                break;
            }
        case TYPE_IMAGE_VERTICAL:
            {
                Image top = images[0];
                Image bottom = images[1];
                Image center = images[2];
                g.drawImage(top, x, y);
                g.drawImage(bottom, x, y + height - bottom.getHeight());
                g.tileImage(center, x, y + top.getHeight(), center.getWidth(), height - top.getHeight() - bottom.getHeight());
                break;
            }
    }
    g.setColor(originalColor);
}
Also used : Graphics(com.codename1.ui.Graphics) Painter(com.codename1.ui.Painter) Rectangle(com.codename1.ui.geom.Rectangle) RGBImage(com.codename1.ui.RGBImage) Image(com.codename1.ui.Image) RGBImage(com.codename1.ui.RGBImage)

Example 2 with Theme

use of com.codename1.ui.util.xml.Theme in project CodenameOne by codenameone.

the class Resources method openFileImpl.

private void openFileImpl(InputStream input) throws IOException {
    this.input = new DataInputStream(input);
    int resourceCount = this.input.readShort();
    if (resourceCount < 0) {
        throw new IOException("Invalid resource file!");
    }
    boolean password = false;
    keyOffset = 0;
    for (int iter = 0; iter < resourceCount; iter++) {
        byte magic = this.input.readByte();
        String id = this.input.readUTF();
        if (password) {
            magic = (byte) decode(magic & 0xff);
            char[] chars = id.toCharArray();
            int clen = chars.length;
            for (int i = 0; i < clen; i++) {
                chars[i] = (char) decode(chars[i] & 0xffff);
            }
            id = new String(chars);
        }
        startingEntry(id, magic);
        switch(magic) {
            case MAGIC_PASSWORD:
                checkKey(id);
                password = true;
                continue;
            case MAGIC_HEADER:
                readHeader();
                continue;
            case MAGIC_THEME:
                setResource(id, MAGIC_THEME, loadTheme(id, magic == MAGIC_THEME));
                continue;
            case MAGIC_IMAGE:
                Image img = createImage();
                img.setImageName(id);
                setResource(id, magic, img);
                continue;
            case MAGIC_FONT:
                setResource(id, magic, loadFont(this.input, id, false));
                continue;
            case MAGIC_DATA:
                setResource(id, magic, createData());
                continue;
            case MAGIC_UI:
                setResource(id, magic, createData());
                continue;
            case MAGIC_L10N:
                setResource(id, magic, loadL10N());
                continue;
            // legacy file support to be removed
            case MAGIC_IMAGE_LEGACY:
                setResource(id, MAGIC_IMAGE, createImage());
                continue;
            case MAGIC_INDEXED_IMAGE_LEGACY:
                setResource(id, MAGIC_IMAGE, createPackedImage8());
                continue;
            case MAGIC_THEME_LEGACY:
                setResource(id, MAGIC_THEME, loadTheme(id, magic == MAGIC_THEME));
                continue;
            case MAGIC_FONT_LEGACY:
                setResource(id, MAGIC_FONT, loadFont(this.input, id, false));
                continue;
            case MAGIC_INDEXED_FONT_LEGACY:
                setResource(id, MAGIC_FONT, loadFont(this.input, id, true));
                continue;
            default:
                throw new IOException("Corrupt theme file unrecognized magic number: " + Integer.toHexString(magic & 0xff));
        }
    }
}
Also used : IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage)

Example 3 with Theme

use of com.codename1.ui.util.xml.Theme in project CodenameOne by codenameone.

the class UIManager method buildTheme.

private void buildTheme(Hashtable themeProps) {
    String con = (String) themeProps.get("@includeNativeBool");
    if (con != null && con.equalsIgnoreCase("true") && Display.getInstance().hasNativeTheme()) {
        boolean a = accessible;
        accessible = true;
        Display.getInstance().installNativeTheme();
        accessible = a;
    }
    Enumeration e = themeProps.keys();
    while (e.hasMoreElements()) {
        String key = (String) e.nextElement();
        // this is a constant not a theme entry
        if (key.startsWith("@")) {
            themeConstants.put(key.substring(1, key.length()), themeProps.get(key));
            continue;
        }
        this.themeProps.put(key, themeProps.get(key));
    }
    // necessary to clear up the style so we don't get resedue from the previous UI
    defaultStyle = new Style();
    // create's the default style
    defaultStyle = createStyle("", "", false);
    defaultSelectedStyle = new Style(defaultStyle);
    defaultSelectedStyle = createStyle("", "sel#", true);
    String overlayThemes = (String) themeProps.get("@OverlayThemes");
    if (overlayThemes != null) {
        java.util.List<String> overlayThemesArr = StringUtil.tokenize(overlayThemes, ',');
        for (String th : overlayThemesArr) {
            th = th.trim();
            if (th.length() == 0) {
                continue;
            }
            try {
                Resources res = Resources.openLayered("/" + th);
                boolean a = accessible;
                accessible = true;
                addThemeProps(res.getTheme(res.getThemeResourceNames()[0]));
                accessible = a;
            } catch (Exception ex) {
                System.err.println("Failed to load overlay theme file specified by @overlayThemes theme constant: " + th);
                Log.e(ex);
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Example 4 with Theme

use of com.codename1.ui.util.xml.Theme in project CodenameOne by codenameone.

the class UIManager method initFirstTheme.

/**
 * This is a shorthand notation for boilerplate code for initializing the first theme in the given resource file
 * and catching/doing nothing with the IOException since this would be invoked too early in the program
 * where we would be out of options if something like that happens. Effectively this is the same as writing:
 * <pre>
 *        try {
 *            theme = Resources.openLayered(resourceFile);
 *            UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
 *        } catch(IOException e){
 *            e.printStackTrace();
 *        }
 * </pre>
 * @param resourceFile the name of the resource file starting with / and without the res extension
 * @return the resource file or null in case of a failure
 */
public static Resources initFirstTheme(String resourceFile) {
    try {
        Resources theme = Resources.openLayered(resourceFile);
        UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
        Resources.setGlobalResources(theme);
        return theme;
    } catch (IOException e) {
        Log.e(e);
    }
    return null;
}
Also used : Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Example 5 with Theme

use of com.codename1.ui.util.xml.Theme in project CodenameOne by codenameone.

the class UIManager method initNamedTheme.

/**
 * Same as the initFirstTheme method, but unlike that method this allows specifying the theme resource name
 * @param resourceFile the name of the resource file starting with / and without the res extension
 * @param resName the name of the theme to use from the file if it contains more than one theme
 * @return the resource file or null in case of a failure
 */
public static Resources initNamedTheme(String resourceFile, String resName) {
    try {
        Resources theme = Resources.openLayered(resourceFile);
        UIManager.getInstance().setThemeProps(theme.getTheme(resName));
        Resources.setGlobalResources(theme);
        return theme;
    } catch (IOException e) {
        Log.e(e);
    }
    return null;
}
Also used : Resources(com.codename1.ui.util.Resources) IOException(java.io.IOException)

Aggregations

Hashtable (java.util.Hashtable)18 AnimationObject (com.codename1.ui.animations.AnimationObject)16 IOException (java.io.IOException)16 EncodedImage (com.codename1.ui.EncodedImage)12 Border (com.codename1.ui.plaf.Border)12 Image (com.codename1.ui.Image)10 RoundBorder (com.codename1.ui.plaf.RoundBorder)10 RoundRectBorder (com.codename1.ui.plaf.RoundRectBorder)10 BufferedImage (java.awt.image.BufferedImage)8 FileInputStream (java.io.FileInputStream)8 Resources (com.codename1.ui.util.Resources)7 File (java.io.File)7 EditorTTFFont (com.codename1.ui.EditorTTFFont)6 EditableResources (com.codename1.ui.util.EditableResources)6 InputStream (java.io.InputStream)6 EditorFont (com.codename1.ui.EditorFont)5 DataInputStream (java.io.DataInputStream)5 Container (com.codename1.ui.Container)4 Form (com.codename1.ui.Form)4 Label (com.codename1.ui.Label)4