Search in sources :

Example 21 with Rectangle

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

the class Dialog method showPopupDialog.

/**
 * A popup dialog is shown with the context of a component and  its selection, it is disposed seamlessly if the back button is pressed
 * or if the user touches outside its bounds. It can optionally provide an arrow in the theme to point at the context component. The popup
 * dialog has the PopupDialog style by default.
 *
 * @param rect the screen rectangle to which the popup should point
 * @return the command that might have been triggered by the user within the dialog if commands are placed in the dialog
 */
public Command showPopupDialog(Rectangle rect) {
    if (getDialogUIID().equals("Dialog")) {
        setDialogUIID("PopupDialog");
        if (getTitleComponent().getUIID().equals("DialogTitle")) {
            getTitleComponent().setUIID("PopupDialogTitle");
        }
        getContentPane().setUIID("PopupContentPane");
    }
    disposeOnRotation = true;
    disposeWhenPointerOutOfBounds = true;
    Command backCommand = null;
    if (getBackCommand() == null) {
        backCommand = new Command("Back");
        setBackCommand(backCommand);
    }
    Component contentPane = super.getContentPane();
    Label title = super.getTitleComponent();
    int menuHeight = calcMenuHeight();
    UIManager manager = getUIManager();
    // preferred size logic of the dialog won't work with large title borders
    if (dialogTitle != null && manager.isThemeConstant("hideEmptyTitleBool", false)) {
        boolean b = getTitle().length() > 0;
        getTitleArea().setVisible(b);
        getTitleComponent().setVisible(b);
        if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
            getTitleComponent().setPreferredSize(new Dimension(0, 0));
            getTitleComponent().getStyle().setBorder(null);
            getTitleArea().setPreferredSize(new Dimension(0, 0));
            if (getContentPane().getClientProperty("$ENLARGED_POP") == null) {
                getContentPane().putClientProperty("$ENLARGED_POP", Boolean.TRUE);
                int cpPaddingTop = getContentPane().getStyle().getPaddingTop();
                int titlePT = getTitleComponent().getStyle().getPaddingTop();
                byte[] pu = getContentPane().getStyle().getPaddingUnit();
                if (pu == null) {
                    pu = new byte[4];
                }
                pu[0] = Style.UNIT_TYPE_PIXELS;
                getContentPane().getStyle().setPaddingUnit(pu);
                int pop = Display.getInstance().convertToPixels(manager.getThemeConstant("popupNoTitleAddPaddingInt", 1), false);
                getContentPane().getStyle().setPadding(TOP, pop + cpPaddingTop + titlePT);
            }
        }
    }
    // allows a text area to recalculate its preferred size if embedded within a dialog
    revalidate();
    Style contentPaneStyle = getDialogStyle();
    boolean restoreArrow = false;
    if (manager.isThemeConstant(getDialogUIID() + "ArrowBool", false)) {
        Image t = manager.getThemeImageConstant(getDialogUIID() + "ArrowTopImage");
        Image b = manager.getThemeImageConstant(getDialogUIID() + "ArrowBottomImage");
        Image l = manager.getThemeImageConstant(getDialogUIID() + "ArrowLeftImage");
        Image r = manager.getThemeImageConstant(getDialogUIID() + "ArrowRightImage");
        Border border = contentPaneStyle.getBorder();
        if (border != null) {
            border.setImageBorderSpecialTile(t, b, l, r, rect);
            restoreArrow = true;
        }
    }
    int prefHeight = contentPane.getPreferredH();
    int prefWidth = contentPane.getPreferredW();
    if (contentPaneStyle.getBorder() != null) {
        prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
        prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
    }
    prefWidth += getUIManager().getLookAndFeel().getVerticalScrollWidth();
    int availableHeight = Display.getInstance().getDisplayHeight() - menuHeight - title.getPreferredH();
    int availableWidth = Display.getInstance().getDisplayWidth();
    int width = Math.min(availableWidth, prefWidth);
    int x = 0;
    int y = 0;
    Command result;
    boolean showPortrait;
    if (popupDirectionBiasPortrait != null) {
        showPortrait = popupDirectionBiasPortrait.booleanValue();
    } else {
        showPortrait = Display.getInstance().isPortrait();
    }
    // if we don't have enough space then disregard device orientation
    if (showPortrait) {
        if (availableHeight < (availableWidth - rect.getWidth()) / 2) {
            showPortrait = false;
        }
    } else {
        if (availableHeight / 2 > availableWidth - rect.getWidth()) {
            showPortrait = true;
        }
    }
    if (showPortrait) {
        if (width < availableWidth) {
            int idealX = rect.getX() - width / 2 + rect.getSize().getWidth() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealX > 0) {
                // if the idealX is too far to the right just align to the right
                if (idealX + width > availableWidth) {
                    x = availableWidth - width;
                } else {
                    x = idealX;
                }
            }
        }
        if (rect.getY() < availableHeight / 2) {
            // popup downwards
            y = rect.getY() + rect.getSize().getHeight();
            int height = Math.min(prefHeight, availableHeight - y);
            result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
        } else {
            // popup upwards
            int height = Math.min(prefHeight, availableHeight - (availableHeight - rect.getY()));
            y = rect.getY() - height;
            result = show(y, availableHeight - height - y, x, availableWidth - width - x, true, true);
        }
    } else {
        int height = Math.min(prefHeight, availableHeight);
        if (height < availableHeight) {
            int idealY = rect.getY() - height / 2 + rect.getSize().getHeight() / 2;
            // if the ideal position is less than 0 just use 0
            if (idealY > 0) {
                // if the idealY is too far up just align to the top
                if (idealY + height > availableHeight) {
                    y = availableHeight - height;
                } else {
                    y = idealY;
                }
            }
        }
        if (prefWidth > rect.getX()) {
            // popup right
            x = rect.getX() + rect.getSize().getWidth();
            if (x + prefWidth > availableWidth) {
                x = availableWidth - prefWidth;
            }
            width = Math.min(prefWidth, availableWidth - x);
            result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
        } else {
            // popup left
            width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
            x = rect.getX() - width;
            result = show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x), true, true);
        }
    }
    if (restoreArrow) {
        contentPaneStyle.getBorder().clearImageBorderSpecialTile();
    }
    if (result == backCommand) {
        return null;
    }
    return result;
}
Also used : UIManager(com.codename1.ui.plaf.UIManager) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension) Border(com.codename1.ui.plaf.Border)

Example 22 with Rectangle

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

the class Form method autoRelease.

private void autoRelease(int x, int y) {
    if (buttonsAwatingRelease != null && buttonsAwatingRelease.size() == 1) {
        // special case allowing drag within a button
        Component atXY = getComponentAt(x, y);
        if (atXY instanceof Container) {
            atXY = atXY.getLeadComponent();
        }
        Component pendingButton = buttonsAwatingRelease.get(0);
        if (atXY != pendingButton) {
            if (pendingButton instanceof Button) {
                Button b = (Button) pendingButton;
                int relRadius = b.getReleaseRadius();
                if (relRadius > 0) {
                    Rectangle r = new Rectangle(b.getAbsoluteX() - relRadius, b.getAbsoluteY() - relRadius, b.getWidth() + relRadius * 2, b.getHeight() + relRadius * 2);
                    if (!r.contains(x, y)) {
                        buttonsAwatingRelease = null;
                        b.dragInitiated();
                    }
                    return;
                }
                buttonsAwatingRelease = null;
                b.dragInitiated();
            }
        } else if (pendingButton instanceof Button && ((Button) pendingButton).isAutoRelease()) {
            buttonsAwatingRelease = null;
            ((Button) pendingButton).dragInitiated();
        }
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 23 with Rectangle

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

the class Form method pointerReleased.

/**
 * {@inheritDoc}
 */
public void pointerReleased(int x, int y) {
    rippleMotion = null;
    pressedCmp = null;
    boolean isScrollWheeling = Display.INSTANCE.impl.isScrollWheeling();
    Container actual = getActualPane(formLayeredPane, x, y);
    if (buttonsAwatingRelease != null && buttonsAwatingRelease.size() == 1) {
        // special case allowing drag within a button
        Component atXY = actual.getComponentAt(x, y);
        Component pendingButton = (Component) buttonsAwatingRelease.get(0);
        if (atXY == pendingButton) {
            buttonsAwatingRelease = null;
            if (dragged == pendingButton) {
                if (pendingButton.isDragAndDropInitialized()) {
                    pendingButton.dragFinishedImpl(x, y);
                } else {
                    pendingButton.pointerReleased(x, y);
                }
                dragged = null;
            } else {
                pendingButton.pointerReleased(x, y);
                if (dragged != null) {
                    if (dragged.isDragAndDropInitialized()) {
                        dragged.dragFinishedImpl(x, y);
                        dragged = null;
                    } else {
                        dragged.pointerReleased(x, y);
                        dragged = null;
                    }
                }
            }
            return;
        }
        if (pendingButton instanceof Button) {
            Button b = (Button) pendingButton;
            int relRadius = b.getReleaseRadius();
            if (relRadius > 0 || b.contains(x, y)) {
                Rectangle r = new Rectangle(b.getAbsoluteX() - relRadius, b.getAbsoluteY() - relRadius, b.getWidth() + relRadius * 2, b.getHeight() + relRadius * 2);
                if (r.contains(x, y)) {
                    buttonsAwatingRelease = null;
                    pointerReleased(b.getAbsoluteX() + 1, b.getAbsoluteY() + 1);
                    return;
                }
            }
        }
    }
    if (pointerReleasedListeners != null && pointerReleasedListeners.hasListeners()) {
        ActionEvent ev = new ActionEvent(this, ActionEvent.Type.PointerReleased, x, y);
        pointerReleasedListeners.fireActionEvent(ev);
        if (ev.isConsumed()) {
            if (dragged != null) {
                if (dragged.isDragAndDropInitialized()) {
                    dragged.dragFinishedImpl(x, y);
                }
                dragged = null;
            }
            return;
        }
    }
    if (dragStopFlag) {
        if (dragged != null) {
            if (dragged.isDragAndDropInitialized()) {
                dragged.dragFinishedImpl(x, y);
            }
            dragged = null;
        }
        dragStopFlag = false;
        return;
    }
    if (dragged == null) {
        // soft button.
        if (menuBar.contains(x, y)) {
            Component cmp = menuBar.getComponentAt(x, y);
            if (cmp != null && cmp.isEnabled()) {
                cmp.pointerReleased(x, y);
            }
            return;
        }
        if (stickyDrag != null) {
            stickyDrag.pointerReleased(x, y);
            repaint();
        } else {
            // Container actual = getActualPane();
            if (y >= actual.getY() && x >= actual.getX()) {
                Component cmp = actual.getComponentAt(x, y);
                while (cmp != null && cmp.isIgnorePointerEvents()) {
                    cmp = cmp.getParent();
                }
                if (cmp != null && cmp.isEnabled()) {
                    if (cmp.hasLead) {
                        Container leadParent;
                        if (cmp instanceof Container) {
                            leadParent = ((Container) cmp).getLeadParent();
                        } else {
                            leadParent = cmp.getParent().getLeadParent();
                        }
                        leadParent.repaint();
                        if (!isScrollWheeling) {
                            setFocused(leadParent);
                        }
                        cmp.getLeadComponent().pointerReleased(x, y);
                    } else {
                        if (cmp.isEnabled()) {
                            if (!isScrollWheeling && cmp.isFocusable()) {
                                setFocused(cmp);
                            }
                            cmp.pointerReleased(x, y);
                        }
                    }
                }
            } else {
                if (y < actual.getY()) {
                    Component cmp = getTitleArea().getComponentAt(x, y);
                    while (cmp != null && cmp.isIgnorePointerEvents()) {
                        cmp = cmp.getParent();
                    }
                    if (cmp != null && cmp.isEnabled()) {
                        cmp.pointerReleased(x, y);
                    }
                } else {
                    Component cmp = ((BorderLayout) super.getLayout()).getWest();
                    if (cmp != null) {
                        cmp = ((Container) cmp).getComponentAt(x, y);
                        while (cmp != null && cmp.isIgnorePointerEvents()) {
                            cmp = cmp.getParent();
                        }
                        if (cmp != null && cmp.isEnabled()) {
                            if (cmp.hasLead) {
                                Container leadParent;
                                if (cmp instanceof Container) {
                                    leadParent = ((Container) cmp).getLeadParent();
                                } else {
                                    leadParent = cmp.getParent().getLeadParent();
                                }
                                leadParent.repaint();
                                if (!isScrollWheeling) {
                                    setFocused(leadParent);
                                }
                                cmp = cmp.getLeadComponent();
                                cmp.pointerReleased(x, y);
                            } else {
                                cmp.pointerReleased(x, y);
                            }
                        }
                    }
                }
            }
        }
    } else {
        if (dragged.isDragAndDropInitialized()) {
            dragged.dragFinishedImpl(x, y);
            dragged = null;
        } else {
            dragged.pointerReleased(x, y);
            dragged = null;
        }
    }
    stickyDrag = null;
    if (buttonsAwatingRelease != null && !Display.getInstance().isRecursivePointerRelease()) {
        for (int iter = 0; iter < buttonsAwatingRelease.size(); iter++) {
            Button b = (Button) buttonsAwatingRelease.get(iter);
            b.setState(Button.STATE_DEFAULT);
            b.repaint();
        }
        buttonsAwatingRelease = null;
    }
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionEvent(com.codename1.ui.events.ActionEvent) Rectangle(com.codename1.ui.geom.Rectangle)

Example 24 with Rectangle

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

the class CSSBgPainter method paint.

/**
 * {@inheritDoc}
 */
public void paint(Graphics g, Rectangle rect) {
    Style s = parent.getStyle();
    int x = rect.getX();
    int y = rect.getY();
    int width = rect.getSize().getWidth();
    int height = rect.getSize().getHeight();
    if (width <= 0 || height <= 0) {
        return;
    }
    g.setColor(s.getBgColor());
    g.fillRect(x, y, width, height, s.getBgTransparency());
    Image bgImage = s.getBgImage();
    if (bgImage == null) {
        return;
    }
    if (fixedX) {
        if (scrollableParent == null) {
            scrollableParent = getScrollableParent(parent);
        }
        if (scrollableParent != null) {
            x += scrollableParent.getScrollX();
            y += scrollableParent.getScrollY();
            width = scrollableParent.getWidth();
            height = scrollableParent.getHeight();
        }
    }
    int iW = bgImage.getWidth();
    int iH = bgImage.getHeight();
    int offsetX = horizPos;
    int offsetY = vertPos;
    if (horizIsPercentage) {
        offsetX = (width - iW) * offsetX / 100;
    }
    if (vertIsPercentage) {
        offsetY = (height - iH) * offsetY / 100;
    }
    switch(s.getBackgroundType()) {
        case 0:
            g.drawImage(s.getBgImage(), x + offsetX, y + offsetY);
            return;
        case Style.BACKGROUND_IMAGE_TILE_BOTH:
            for (int xPos = getTiledPosition(offsetX, iW); xPos <= width; xPos += iW) {
                for (int yPos = getTiledPosition(offsetY, iH); yPos <= height; yPos += iH) {
                    g.drawImage(s.getBgImage(), x + xPos, y + yPos);
                }
            }
            return;
        case Style.BACKGROUND_IMAGE_TILE_HORIZONTAL:
            for (int xPos = getTiledPosition(offsetX, iW); xPos <= width; xPos += iW) {
                g.drawImage(s.getBgImage(), x + xPos, y + offsetY);
            }
            return;
        case Style.BACKGROUND_IMAGE_TILE_VERTICAL:
            for (int yPos = getTiledPosition(offsetY, iH); yPos <= height; yPos += iH) {
                g.drawImage(s.getBgImage(), x + offsetX, y + yPos);
            }
            return;
    }
}
Also used : Style(com.codename1.ui.plaf.Style) Image(com.codename1.ui.Image)

Example 25 with Rectangle

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

the class TextField method updateScroll.

private void updateScroll() {
    if (!isSingleLineTextArea() && isScrollableY()) {
        Font textFont = getStyle().getFont();
        int rowsGap = getRowsGap();
        int lineHeight = textFont.getHeight() + rowsGap;
        Rectangle rect = new Rectangle(getScrollX(), getCursorY() * lineHeight, getWidth(), lineHeight);
        scrollRectToVisible(rect, this);
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Aggregations

Rectangle (com.codename1.ui.geom.Rectangle)41 Dimension (com.codename1.ui.geom.Dimension)16 Style (com.codename1.ui.plaf.Style)13 Image (com.codename1.ui.Image)10 ActionEvent (com.codename1.ui.events.ActionEvent)5 Border (com.codename1.ui.plaf.Border)5 Component (com.codename1.ui.Component)4 Graphics (com.codename1.ui.Graphics)4 UIManager (com.codename1.ui.plaf.UIManager)4 Form (com.codename1.ui.Form)3 Label (com.codename1.ui.Label)3 GeneralPath (com.codename1.ui.geom.GeneralPath)3 Point (com.codename1.ui.geom.Point)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 Paint (com.codename1.charts.compat.Paint)2 Button (com.codename1.ui.Button)2 EncodedImage (com.codename1.ui.EncodedImage)2 Painter (com.codename1.ui.Painter)2 ActionListener (com.codename1.ui.events.ActionListener)2 RoundBorder (com.codename1.ui.plaf.RoundBorder)2