Search in sources :

Example 71 with Dimension

use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.

the class Dialog method getDialogPreferredSize.

/**
 * Returns the preferred size of the dialog, this allows developers to position a dialog
 * manually in arbitrary positions.
 *
 * @return the preferred size of this dialog
 */
public Dimension getDialogPreferredSize() {
    Component contentPane = super.getContentPane();
    Style contentPaneStyle = getDialogStyle();
    int width = Display.getInstance().getDisplayWidth();
    int prefHeight = contentPane.getPreferredH();
    int prefWidth = contentPane.getPreferredW();
    prefWidth = Math.min(prefWidth, width);
    if (contentPaneStyle.getBorder() != null) {
        prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
        prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
    }
    return new Dimension(prefWidth, prefHeight);
}
Also used : Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Example 72 with Dimension

use of com.codename1.ui.geom.Dimension 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);
        }
    }
}
Also used : Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) Hashtable(java.util.Hashtable) Label(com.codename1.ui.Label) Dimension(com.codename1.ui.geom.Dimension)

Example 73 with Dimension

use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.

the class List method paintFocus.

private void paintFocus(Graphics g, int width, Rectangle pos, Dimension rendererSize) {
    if (ignoreFocusComponentWhenUnfocused && !hasFocus()) {
        return;
    }
    if (!shouldRenderSelection()) {
        return;
    }
    calculateComponentPosition(getCurrentSelected(), width, pos, rendererSize, getElementSize(true, true), true);
    Dimension size = pos.getSize();
    Component cmp = renderer.getListFocusComponent(this);
    if (cmp != null) {
        cmp.setCellRenderer(true);
        int x = pos.getX();
        int y = pos.getY();
        // prevent focus animation from working during a drag operation
        if (orientation != HORIZONTAL) {
            y -= (animationPosition + fixedDraggedAnimationPosition);
        } else {
            x -= (animationPosition + fixedDraggedAnimationPosition);
        }
        renderComponentBackground(g, cmp, x, y, size.getWidth(), size.getHeight());
        renderComponent(g, cmp, x, y, size.getWidth(), size.getHeight());
    }
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Example 74 with Dimension

use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.

the class List method animate.

/**
 * {@inheritDoc}
 */
public boolean animate() {
    // parent is performing the animation we shouldn't do anything in this case
    // this is the scrolling animation which we don't want to interfear with
    boolean parentFinished = super.animate();
    if ((animationPosition != 0) && listMotion != null && !isDragActivated()) {
        if (animationPosition < 0) {
            animationPosition = Math.min(listMotion.getValue() - destination, 0);
        } else {
            animationPosition = Math.max(destination - listMotion.getValue(), 0);
        }
        if (animationPosition == 0) {
            listMotion = null;
            deregisterAnimatedInternal();
        }
        return true;
    }
    if (fixedDraggedMotion != null) {
        int val = -fixedDraggedMotion.getValue();
        fixedDraggedAnimationPosition = fixedDraggedAnimationPosition - (fixedDraggedPosition - val);
        fixedDraggedPosition = val;
        Dimension size = getElementSize(false, true);
        int s;
        if (orientation == VERTICAL) {
            s = size.getHeight();
        } else {
            s = size.getWidth();
        }
        if (fixedDraggedMotion.isFinished()) {
            deregisterAnimatedInternal();
            // is the closest and animate to it.
            if (fixedDraggedAnimationPosition <= -s / 2) {
                fixedDraggedSelection++;
                if (fixedDraggedSelection >= model.getSize()) {
                    fixedDraggedSelection = 0;
                }
            } else if (fixedDraggedAnimationPosition >= s / 2) {
                fixedDraggedSelection--;
                if (fixedDraggedSelection < 0) {
                    fixedDraggedSelection = model.getSize() - 1;
                }
            }
            if (fixedDraggedAnimationPosition != 0) {
                if (fixedDraggedAnimationPosition < 0) {
                    if (fixedDraggedAnimationPosition < -s / 2) {
                        destination = s + fixedDraggedAnimationPosition;
                        animationPosition = destination;
                    } else {
                        destination = -fixedDraggedAnimationPosition;
                        animationPosition = fixedDraggedAnimationPosition;
                    }
                } else {
                    if (fixedDraggedAnimationPosition > s / 2) {
                        destination = (s - fixedDraggedAnimationPosition);
                        animationPosition = -destination;
                    } else {
                        destination = fixedDraggedAnimationPosition;
                        animationPosition = fixedDraggedAnimationPosition;
                    }
                }
                initListMotion();
                fixedDraggedAnimationPosition = 0;
            }
            // this happens when dragging an empty list causing an exception on a negative selection
            if (fixedDraggedSelection >= 0 && fixedDraggedSelection < getModel().getSize()) {
                setSelectedIndex(fixedDraggedSelection, false);
            }
            setDragActivated(false);
            fixedDraggedMotion = null;
            return false;
        } else {
            if (fixedDraggedAnimationPosition <= -s) {
                fixedDraggedSelection++;
                if (fixedDraggedSelection >= model.getSize()) {
                    fixedDraggedSelection = 0;
                }
                fixedDraggedPosition = val;
            } else if (fixedDraggedAnimationPosition >= s) {
                fixedDraggedSelection--;
                if (fixedDraggedSelection < 0) {
                    fixedDraggedSelection = model.getSize() - 1;
                }
                fixedDraggedPosition = val;
            }
            fixedDraggedAnimationPosition = fixedDraggedAnimationPosition % s;
        }
        return true;
    }
    return parentFinished;
}
Also used : Dimension(com.codename1.ui.geom.Dimension)

Example 75 with Dimension

use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.

the class List method calculateComponentPosition.

/**
 * Calculates the desired bounds for the component and returns them within the
 * given rectangle.
 */
private void calculateComponentPosition(int index, int defaultWidth, Rectangle rect, Dimension rendererSize, Dimension selectedSize, boolean beforeSelected) {
    Style style = getStyle();
    int initialY = style.getPaddingTop();
    int initialX = style.getPaddingLeftNoRTL();
    boolean rtl = isRTL();
    if (rtl) {
        initialX += getSideGap();
    }
    int selection = getCurrentSelected();
    Dimension d = rect.getSize();
    int selectedDiff;
    // which will cause the bottom elements to "return" from the top.
    if (orientation != HORIZONTAL) {
        int height = rendererSize.getHeight();
        selectedDiff = selectedSize.getHeight() - height;
        rect.setX(initialX);
        d.setHeight(height);
        d.setWidth(defaultWidth);
        int y = 0;
        int listHeight = getHeight() - style.getVerticalPadding();
        int totalHeight = (height + itemGap) * getModel().getSize() + selectedDiff;
        switch(fixedSelection) {
            case FIXED_CENTER:
                y = listHeight / 2 - (height + itemGap + selectedDiff) / 2 + (index - selection) * (height + itemGap);
                if (!beforeSelected) {
                    y += selectedDiff;
                }
                y = recalcOffset(y, totalHeight, listHeight, height + itemGap);
                break;
            case FIXED_TRAIL:
                y = listHeight - (height + itemGap + selectedDiff);
            case FIXED_LEAD:
                y += (index - selection) * (height + itemGap);
                if (index - selection > 0) {
                    y += selectedDiff;
                }
                y = recalcOffset(y, totalHeight, listHeight, height + itemGap);
                break;
            default:
                y = index * (height + itemGap);
                if (!beforeSelected) {
                    y += selectedDiff;
                }
                break;
        }
        rect.setY(y + initialY);
        if (index == selection) {
            d.setHeight(d.getHeight() + selectedDiff);
        }
    } else {
        int width = rendererSize.getWidth();
        selectedDiff = selectedSize.getWidth() - width;
        rect.setY(initialY);
        d.setHeight(getHeight() - style.getVerticalPadding());
        d.setWidth(width);
        int x = 0;
        int listWidth = getWidth() - style.getHorizontalPadding();
        int totalWidth = (width + itemGap) * getModel().getSize() + selectedDiff;
        switch(fixedSelection) {
            case FIXED_CENTER:
                x = listWidth / 2 - (width + itemGap + selectedDiff) / 2 + (index - selection) * (width + itemGap);
                if (!beforeSelected) {
                    x += selectedDiff;
                }
                if (rtl) {
                    x = listWidth - x - width;
                }
                x = recalcOffset(x, totalWidth, listWidth, width + itemGap);
                break;
            case FIXED_TRAIL:
                x = listWidth - (width + itemGap + selectedDiff);
            case FIXED_LEAD:
                x += (index - selection) * (width + itemGap);
                if (index - selection > 0) {
                    x += selectedDiff;
                }
                if (rtl) {
                    x = listWidth - x - width;
                }
                x = recalcOffset(x, totalWidth, listWidth, width + itemGap);
                break;
            default:
                x = index * (width + itemGap);
                if (!beforeSelected) {
                    x += selectedDiff;
                }
                break;
        }
        int rectX = initialX + x;
        if ((rtl) && (fixedSelection < FIXED_NONE_BOUNDRY)) {
            rectX = initialX + totalWidth - (x - initialX) - (width + itemGap);
            if (index == getCurrentSelected()) {
                rectX -= selectedDiff;
            }
            if (totalWidth < listWidth) {
                rectX += (listWidth - totalWidth);
            }
        }
        rect.setX(rectX);
        if (index == selection) {
            d.setWidth(d.getWidth() + selectedDiff);
        }
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension)

Aggregations

Dimension (com.codename1.ui.geom.Dimension)74 Style (com.codename1.ui.plaf.Style)27 Component (com.codename1.ui.Component)16 Image (com.codename1.ui.Image)16 Rectangle (com.codename1.ui.geom.Rectangle)9 EncodedImage (com.codename1.ui.EncodedImage)8 Label (com.codename1.ui.Label)7 Container (com.codename1.ui.Container)6 FileEncodedImage (com.codename1.components.FileEncodedImage)5 StorageImage (com.codename1.components.StorageImage)5 ActionEvent (com.codename1.ui.events.ActionEvent)5 Form (com.codename1.ui.Form)4 Dimension (java.awt.Dimension)4 Font (com.codename1.ui.Font)3 FontImage (com.codename1.ui.FontImage)3 TextArea (com.codename1.ui.TextArea)3 ActionListener (com.codename1.ui.events.ActionListener)3 Point (com.codename1.ui.geom.Point)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 Border (com.codename1.ui.plaf.Border)3