Search in sources :

Example 46 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class JavaSEPort method init.

/**
 * @inheritDoc
 */
public void init(Object m) {
    inInit = true;
    File updater = new File(System.getProperty("user.home") + File.separator + ".codenameone" + File.separator + "UpdateCodenameOne.jar");
    if (!updater.exists()) {
        System.out.println("******************************************************************************");
        System.out.println("* It seems that you are using an old plugin version please upate to the latest plugin and invoke Codename One -> Codename One Settings -> Basic -> Update Client Libs");
        System.out.println("******************************************************************************");
    }
    Preferences pref = Preferences.userNodeForPackage(JavaSEPort.class);
    boolean desktopSkin = pref.getBoolean("desktopSkin", false);
    if (desktopSkin && m == null) {
        Toolkit tk = Toolkit.getDefaultToolkit();
        setDefaultPixelMilliRatio(tk.getScreenResolution() / 25.4 * getRetinaScale());
        pixelMilliRatio = getDefaultPixelMilliRatio();
        JPanel panel = new javax.swing.JPanel();
        panel.setLayout(new BorderLayout());
        JPanel bottom = new javax.swing.JPanel();
        panel.setOpaque(false);
        bottom.setLayout(new FlowLayout(FlowLayout.RIGHT));
        widthLabel = new JLabel("Width:   ");
        heightLabel = new JLabel(" Height:   ");
        bottom.add(widthLabel);
        bottom.add(heightLabel);
        panel.add(bottom, BorderLayout.SOUTH);
        JFrame frame = new JFrame();
        // frame.addWindowListener(new WindowListener() {
        // 
        // });
        frame.setLayout(new BorderLayout());
        frame.add(panel, BorderLayout.CENTER);
        frame.setSize(new Dimension(300, 400));
        m = panel;
        window = frame;
    }
    setInvokePointerHover(desktopSkin || invokePointerHover);
    // this is essential for push and other things to work in the simulator
    Preferences p = Preferences.userNodeForPackage(com.codename1.ui.Component.class);
    String user = p.get("user", null);
    if (user != null) {
        Display d = Display.getInstance();
        d.setProperty("built_by_user", user);
        String mainClass = System.getProperty("MainClass");
        if (mainClass != null) {
            mainClass = mainClass.substring(0, mainClass.lastIndexOf('.'));
            d.setProperty("package_name", mainClass);
        }
    }
    try {
        Class.forName("javafx.embed.swing.JFXPanel");
        Platform.setImplicitExit(false);
        fxExists = true;
    } catch (Throwable ex) {
    }
    String hide = System.getProperty("hideMenu", "false");
    if (hide != null && hide.equals("true")) {
        showMenu = false;
    }
    URLConnection.setDefaultAllowUserInteraction(true);
    HttpURLConnection.setFollowRedirects(false);
    if (!blockMonitors && pref.getBoolean("NetworkMonitor", false)) {
        showNetworkMonitor();
    }
    if (!blockMonitors && pref.getBoolean("PushSimulator", false)) {
        pushSimulation = new PushSimulator();
        pushSimulation.setVisible(true);
    }
    if (!blockMonitors && pref.getBoolean("PerformanceMonitor", false)) {
        showPerformanceMonitor();
    }
    if (defaultInitTarget != null && m == null) {
        m = defaultInitTarget;
    }
    if (canvas.getParent() != null) {
        canvas.getParent().remove(canvas);
    }
    if (m != null && m instanceof java.awt.Container) {
        java.awt.Container cnt = (java.awt.Container) m;
        if (cnt.getLayout() instanceof java.awt.BorderLayout) {
            cnt.add(java.awt.BorderLayout.CENTER, canvas);
        } else {
            cnt.add(canvas);
        }
    } else {
        window = new JFrame();
        window.setLayout(new java.awt.BorderLayout());
        hSelector = new JScrollBar(Scrollbar.HORIZONTAL);
        vSelector = new JScrollBar(Scrollbar.VERTICAL);
        hSelector.addAdjustmentListener(canvas);
        vSelector.addAdjustmentListener(canvas);
        scrollableSkin = pref.getBoolean("Scrollable", true);
        if (scrollableSkin) {
            window.add(java.awt.BorderLayout.SOUTH, hSelector);
            window.add(java.awt.BorderLayout.EAST, vSelector);
        }
        window.add(java.awt.BorderLayout.CENTER, canvas);
    }
    if (window != null) {
        java.awt.Image large = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/application64.png"));
        java.awt.Image small = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/application48.png"));
        try {
            // setIconImages is only available in JDK 1.6
            window.setIconImages(Arrays.asList(new java.awt.Image[] { large, small }));
        } catch (Throwable err) {
            window.setIconImage(small);
        }
        window.addWindowListener(new WindowListener() {

            public void windowOpened(WindowEvent e) {
            }

            public void windowClosing(WindowEvent e) {
                Display.getInstance().exitApplication();
            }

            public void windowClosed(WindowEvent e) {
            }

            public void windowIconified(WindowEvent e) {
            }

            public void windowDeiconified(WindowEvent e) {
            }

            public void windowActivated(WindowEvent e) {
            }

            public void windowDeactivated(WindowEvent e) {
            }
        });
        window.setLocationByPlatform(true);
        android6PermissionsFlag = pref.getBoolean("Android6Permissions", false);
        alwaysOnTop = pref.getBoolean("AlwaysOnTop", false);
        window.setAlwaysOnTop(alwaysOnTop);
        String reset = System.getProperty("resetSkins");
        if (reset != null && reset.equals("true")) {
            System.setProperty("resetSkins", "");
            pref = Preferences.userNodeForPackage(JavaSEPort.class);
            pref.put("skins", DEFAULT_SKINS);
        }
        if (hasSkins()) {
            if (m == null) {
                String f = System.getProperty("skin");
                if (f != null) {
                    loadSkinFile(f, window);
                } else {
                    String d = System.getProperty("dskin");
                    f = pref.get("skin", d);
                    loadSkinFile(f, window);
                }
            } else {
                try {
                    installMenu(window, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            Resources.setRuntimeMultiImageEnabled(true);
            window.setUndecorated(true);
            window.setExtendedState(JFrame.MAXIMIZED_BOTH);
        }
        window.pack();
        if (getSkin() != null && !scrollableSkin) {
            float w1 = ((float) canvas.getWidth() * (float) retinaScale) / ((float) getSkin().getWidth());
            float h1 = ((float) canvas.getHeight() * (float) retinaScale) / ((float) getSkin().getHeight());
            zoomLevel = Math.min(h1, w1);
        }
        portrait = pref.getBoolean("Portrait", true);
        if (!portrait && getSkin() != null) {
            canvas.setForcedSize(new java.awt.Dimension((int) (getSkin().getWidth() / retinaScale), (int) (getSkin().getHeight() / retinaScale)));
            window.setSize(new java.awt.Dimension((int) (getSkin().getWidth() / retinaScale), (int) (getSkin().getHeight() / retinaScale)));
        }
        window.setVisible(true);
    }
    if (useNativeInput) {
        Display.getInstance().setDefaultVirtualKeyboard(null);
    }
    float factor = ((float) getDisplayHeight()) / 480.0f;
    if (factor > 0 && autoAdjustFontSize && getSkin() != null) {
        // set a reasonable default font size
        setFontSize((int) (15.0f * factor), (int) (11.0f * factor), (int) (19.0f * factor));
    }
    if (m instanceof Runnable) {
        Display.getInstance().callSerially((Runnable) m);
    }
    inInit = false;
}
Also used : EncodedImage(com.codename1.ui.EncodedImage) Image(com.codename1.ui.Image) BufferedImage(java.awt.image.BufferedImage) JFrame(javax.swing.JFrame) Toolkit(java.awt.Toolkit) Preferences(java.util.prefs.Preferences) WindowListener(java.awt.event.WindowListener) Dimension(java.awt.Dimension) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) EOFException(java.io.EOFException) FontFormatException(java.awt.FontFormatException) Dimension(java.awt.Dimension) WindowEvent(java.awt.event.WindowEvent) Display(com.codename1.ui.Display) java.awt(java.awt)

Example 47 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class Tile method paintLoadingText.

private void paintLoadingText(Graphics g) {
    g.setColor(0x707070);
    g.fillRect(0, 0, dimension().getWidth(), dimension().getHeight());
    g.setColor(0xFFFFFF);
    g.setFont(f);
    Font f = g.getFont();
    int strWidth = f.stringWidth(tileLoadingText);
    g.drawString(tileLoadingText, (dimension().getWidth() - strWidth) / 2, (dimension().getHeight() - f.getHeight()) / 2);
}
Also used : Font(com.codename1.ui.Font) Point(com.codename1.ui.geom.Point)

Example 48 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class HTMLComponent method cleanup.

/**
 * Rebuilds the HTMLComponent, this is called usually after a new page was loaded.
 */
private void cleanup() {
    if (document != null) {
        cleanElementUI(document);
    }
    if (eventsListener != null) {
        eventsListener.deregisterAll();
    }
    displayWidth = Display.getInstance().getDisplayWidth();
    // reset all building process values
    leftIndent = 0;
    x = 0;
    containers = new Vector();
    // embeddedCSS=null; // Shouldn't nullify as embedded style tag is collected in the head phase which is before..
    marqueeComponents = new Vector();
    marqueeMotion = null;
    anchors = new Hashtable();
    anchor = null;
    accesskey = '\0';
    for (Enumeration e = accessKeys.keys(); e.hasMoreElements(); ) {
        int keyCode = ((Integer) e.nextElement()).intValue();
        getComponentForm().removeKeyListener(keyCode, this);
    }
    // =new Hashtable();
    accessKeys.clear();
    fieldsets = new Vector();
    curTable = null;
    tables = new Vector();
    tableCells = new Vector();
    ulLevel = 0;
    olIndex = Integer.MIN_VALUE;
    olUpperLevelIndex = new Vector();
    listType = HTMLListIndex.LIST_NUMERIC;
    underlineCount = 0;
    strikethruCount = 0;
    textDecoration = 0;
    imageMapComponents = null;
    imageMapData = null;
    curImageMap = null;
    superscript = 0;
    maxSuperscript = 0;
    counters = null;
    font = defaultFont;
    labelForID = null;
    inputFields = new Hashtable();
    link = null;
    linkVisited = false;
    mainLink = null;
    firstFocusable = null;
    curForm = null;
    curTextArea = null;
    curComboBox = null;
    textfieldsToForms = new Hashtable();
    optionTag = false;
    optionSelected = false;
    preTagCount = 0;
    quoteTagCount = 0;
    mainContainer = new Container();
    if (pageUIID != null) {
        mainContainer.setUIID(pageUIID);
    }
    if (pageStyle != null) {
        applyPageStyle();
    }
    mainContainer.setScrollableX(false);
    mainContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    curContainer = mainContainer;
    curLine = new Container();
    lastWasEmpty = false;
    width = Display.getInstance().getDisplayWidth() - getStyle().getMargin(Component.LEFT) - getStyle().getPadding(Component.LEFT) - getStyle().getMargin(Component.RIGHT) - getStyle().getPadding(Component.RIGHT) - // The -10 is arbitrary to avoid edge cases
    10;
    textColor = DEFAULT_TEXT_COLOR;
}
Also used : Container(com.codename1.ui.Container) Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) BoxLayout(com.codename1.ui.layouts.BoxLayout) Vector(java.util.Vector)

Example 49 with Font

use of com.codename1.ui.Font in project CodenameOne by codenameone.

the class Label method initAutoResize.

void initAutoResize() {
    if (autoSizeMode) {
        Style s = getUnselectedStyle();
        int p = s.getHorizontalPadding();
        int w = getWidth();
        if (w > p + 10) {
            if (originalFont == null) {
                originalFont = s.getFont();
            } else {
                if (w == widthAtLastCheck) {
                    return;
                }
            }
            Font currentFont = getUnselectedStyle().getFont();
            float fontSize = currentFont.getPixelSize();
            if (fontSize < 1) {
                Log.p("Autosize disabled probably because component wasn't using native fonts for UIID: " + getUIID());
                autoSizeMode = false;
                return;
            }
            widthAtLastCheck = w;
            autoSizeMode = false;
            int currentWidth = calcPreferredSize().getWidth();
            while (currentWidth < w) {
                fontSize++;
                currentFont = currentFont.derive(fontSize, currentFont.getStyle());
                getAllStyles().setFont(currentFont);
                currentWidth = calcPreferredSize().getWidth();
            }
            while (currentWidth > w) {
                fontSize--;
                currentFont = currentFont.derive(fontSize, currentFont.getStyle());
                getAllStyles().setFont(currentFont);
                currentWidth = calcPreferredSize().getWidth();
            }
            autoSizeMode = true;
        }
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 50 with Font

use of com.codename1.ui.Font 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

Font (com.codename1.ui.Font)30 Image (com.codename1.ui.Image)12 Hashtable (java.util.Hashtable)10 EditorFont (com.codename1.ui.EditorFont)8 Border (com.codename1.ui.plaf.Border)8 Component (com.codename1.ui.Component)7 Style (com.codename1.ui.plaf.Style)7 EncodedImage (com.codename1.ui.EncodedImage)6 TextArea (com.codename1.ui.TextArea)6 AnimationObject (com.codename1.ui.animations.AnimationObject)6 BufferedImage (java.awt.image.BufferedImage)6 IOException (java.io.IOException)6 java.awt (java.awt)5 Container (com.codename1.ui.Container)4 Dimension (com.codename1.ui.geom.Dimension)4 RoundBorder (com.codename1.ui.plaf.RoundBorder)4 RoundRectBorder (com.codename1.ui.plaf.RoundRectBorder)4 ParseException (java.text.ParseException)4 Paint (android.graphics.Paint)3 EditorTTFFont (com.codename1.ui.EditorTTFFont)3