use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class InteractionDialog 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
*/
public void showPopupDialog(Rectangle rect) {
disposed = false;
if (getUIID().equals("Dialog")) {
setUIID("PopupDialog");
if (getTitleComponent().getUIID().equals("DialogTitle")) {
getTitleComponent().setUIID("PopupDialogTitle");
}
getContentPane().setUIID("PopupContentPane");
}
Component contentPane = getContentPane();
Label title = getTitleComponent();
UIManager manager = getUIManager();
String dialogTitle = title.getText();
// preferred size logic of the dialog won't work with large title borders
if ((dialogTitle != null || dialogTitle.length() == 0) && manager.isThemeConstant("hideEmptyTitleBool", false)) {
boolean b = getTitle().length() > 0;
titleArea.setVisible(b);
getTitleComponent().setVisible(b);
if (!b && manager.isThemeConstant("shrinkPopupTitleBool", true)) {
getTitleComponent().setPreferredSize(new Dimension(0, 0));
getTitleComponent().getStyle().setBorder(null);
titleArea.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 = getStyle();
boolean restoreArrow = false;
if (manager.isThemeConstant(getUIID() + "ArrowBool", false)) {
Image t = manager.getThemeImageConstant(getUIID() + "ArrowTopImage");
Image b = manager.getThemeImageConstant(getUIID() + "ArrowBottomImage");
Image l = manager.getThemeImageConstant(getUIID() + "ArrowLeftImage");
Image r = manager.getThemeImageConstant(getUIID() + "ArrowRightImage");
Border border = contentPaneStyle.getBorder();
if (border != null) {
border.setImageBorderSpecialTile(t, b, l, r, rect);
restoreArrow = true;
}
}
calcPreferredSize();
int prefHeight = getPreferredH();
int prefWidth = getPreferredW();
if (contentPaneStyle.getBorder() != null) {
prefWidth = Math.max(contentPaneStyle.getBorder().getMinimumWidth(), prefWidth);
prefHeight = Math.max(contentPaneStyle.getBorder().getMinimumHeight(), prefHeight);
}
Form f = Display.getInstance().getCurrent();
int availableHeight = getLayeredPane(f).getParent().getHeight();
int availableWidth = getLayeredPane(f).getParent().getWidth();
int width = Math.min(availableWidth, prefWidth);
int x = 0;
int y = 0;
boolean 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();
int height = Math.min(prefHeight, availableHeight - y);
show(y, Math.max(0, availableHeight - height - y), x, Math.max(0, availableWidth - width - x));
} else {
// popup upwards
int height = Math.min(prefHeight, rect.getY() - getLayeredPane(f).getAbsoluteY());
y = rect.getY() - height - getLayeredPane(f).getAbsoluteY();
show(y, Math.max(0, getLayeredPane(f).getComponentForm().getHeight() - rect.getY()), x, Math.max(0, availableWidth - width - x));
}
} 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);
show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x));
} else {
// popup left
width = Math.min(prefWidth, availableWidth - (availableWidth - rect.getX()));
x = rect.getX() - width;
show(y, availableHeight - height - y, Math.max(0, x), Math.max(0, availableWidth - width - x));
}
}
/*if(restoreArrow) {
contentPaneStyle.getBorder().clearImageBorderSpecialTile();
}*/
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class ScaleImageButton method calcPreferredSize.
/**
* {@inheritDoc}
*/
@Override
protected Dimension calcPreferredSize() {
Image i = getIcon();
if (i == null) {
return new Dimension();
}
int dw = Display.getInstance().getDisplayWidth();
int iw = i.getWidth();
int ih = i.getHeight();
// a scrollable container the vertical height might be granted providing so much space as to make this unrealistic...
if (iw > dw) {
float ratio = ((float) iw) / ((float) dw);
iw = (int) (((float) iw) / ((float) ratio));
ih = (int) (((float) ih) / ((float) ratio));
}
Style s = getStyle();
return new Dimension(iw + s.getPaddingLeftNoRTL() + s.getPaddingRightNoRTL(), ih + s.getPaddingTop() + s.getPaddingBottom());
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class ComboBox method createPopupDialog.
/**
* Subclasses can override this method to change the creation of the dialog
*
* @param l the list of the popup
* @return a dialog instance
*/
protected Dialog createPopupDialog(List<T> l) {
Dialog popupDialog = new Dialog(getUIID() + "Popup", getUIID() + "PopupTitle") {
void sizeChangedInternal(int w, int h) {
// resize the popup just resize the parent form
if (getWidth() == w && getHeight() != h) {
Form frm = getPreviousForm();
if (frm != null) {
frm.sizeChangedInternal(w, h);
}
setSize(new Dimension(w, h));
repaint();
} else {
dispose();
}
}
};
popupDialog.setScrollable(false);
popupDialog.getContentPane().setAlwaysTensile(false);
popupDialog.setAlwaysTensile(false);
popupDialog.getContentPane().setUIID("PopupContentPane");
popupDialog.setDisposeWhenPointerOutOfBounds(true);
popupDialog.setTransitionInAnimator(CommonTransitions.createEmpty());
popupDialog.setTransitionOutAnimator(CommonTransitions.createEmpty());
popupDialog.setLayout(new BorderLayout());
popupDialog.addComponent(BorderLayout.CENTER, l);
return popupDialog;
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class Component method parsePreferredSize.
public static Dimension parsePreferredSize(String preferredSize, Dimension baseSize) {
String strVal = (String) preferredSize;
int spacePos = strVal.indexOf(" ");
if (spacePos == -1) {
return baseSize;
}
String wStr = strVal.substring(0, spacePos).trim();
String hStr = strVal.substring(spacePos + 1).trim();
int unitPos = -1;
float pixelsPerMM = Display.getInstance().convertToPixels(1000f) / 1000f;
try {
if ((unitPos = wStr.indexOf("mm")) != -1) {
baseSize.setWidth((int) Math.round(Float.parseFloat(wStr.substring(0, unitPos)) * pixelsPerMM));
} else if ((unitPos = wStr.indexOf("px")) != -1) {
baseSize.setWidth(Integer.parseInt(wStr.substring(0, unitPos)));
} else if (!"inherit".equals(wStr)) {
baseSize.setWidth(Integer.parseInt(wStr));
}
} catch (Throwable t) {
}
try {
if ((unitPos = hStr.indexOf("mm")) != -1) {
baseSize.setHeight((int) Math.round(Float.parseFloat(hStr.substring(0, unitPos)) * pixelsPerMM));
} else if ((unitPos = hStr.indexOf("px")) != -1) {
baseSize.setHeight(Integer.parseInt(hStr.substring(0, unitPos)));
} else if (!"inherit".equals(hStr)) {
baseSize.setHeight(Integer.parseInt(hStr));
}
} catch (Throwable t) {
}
return baseSize;
}
use of com.codename1.ui.geom.Dimension in project CodenameOne by codenameone.
the class Component method setPreferredSize.
/**
* Sets the Component Preferred Size, there is no guarantee the Component will
* be sized at its Preferred Size. The final size of the component may be
* smaller than its preferred size or even larger than the size.<br>
* The Layout manager can take this value into consideration, but there is
* no guarantee or requirement.
*
* @param d the component dimension
* @deprecated this method shouldn't be used, use sameWidth/Height, padding, margin or override calcPeferredSize
* to reach similar functionality
*/
public void setPreferredSize(Dimension d) {
if (d == null) {
sizeRequestedByUser = false;
preferredSize = null;
shouldCalcPreferredSize = true;
return;
}
Dimension dim = preferredSize();
dim.setWidth(d.getWidth());
dim.setHeight(d.getHeight());
sizeRequestedByUser = true;
}
Aggregations