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);
}
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);
}
}
}
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());
}
}
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;
}
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);
}
}
}
Aggregations