use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class Component method getInvisibleAreaUnderVKB.
/**
* Returns the area of this component that is currently hidden by the virtual keyboard.
* @return
*/
private int getInvisibleAreaUnderVKB() {
Form f = getComponentForm();
if (f != null) {
int invisibleAreaUnderVKB = Form.getInvisibleAreaUnderVKB(f);
if (invisibleAreaUnderVKB == 0) {
return 0;
}
int bottomGap = f.getHeight() - getAbsoluteY() - getScrollY() - getHeight();
if (bottomGap < invisibleAreaUnderVKB) {
return invisibleAreaUnderVKB - bottomGap;
} else {
return 0;
}
}
return 0;
}
use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class Component method scrollRectToVisible.
/**
* Makes sure the component is visible in the scroll if this container
* is scrollable
*
* @param x
* @param y
* @param width
* @param height
* @param coordinateSpace the component according to whose coordinates
* rect is defined. Rect's x/y are relative to that component
* (they are not absolute).
*/
public void scrollRectToVisible(int x, int y, int width, int height, Component coordinateSpace) {
if (isScrollable()) {
int scrollPosition = getScrollY();
Style s = getStyle();
int w = getWidth() - s.getHorizontalPadding();
int h = getHeight() - s.getVerticalPadding();
Rectangle view;
int invisibleAreaUnderVKB = getInvisibleAreaUnderVKB();
if (isSmoothScrolling() && destScrollY > -1) {
view = new Rectangle(getScrollX(), destScrollY, w, h - invisibleAreaUnderVKB);
} else {
view = new Rectangle(getScrollX(), getScrollY(), w, h - invisibleAreaUnderVKB);
}
int relativeX = x;
int relativeY = y;
// component needs to be in absolute coordinates...
Container parent = null;
if (coordinateSpace != null) {
parent = coordinateSpace.getParent();
}
if (parent == this) {
if (view.contains(x, y, width, height)) {
return;
}
} else {
while (parent != this) {
// mostly a special case for list
if (parent == null) {
relativeX = x;
relativeY = y;
break;
}
relativeX += parent.getX();
relativeY += parent.getY();
parent = parent.getParent();
}
if (view.contains(relativeX, relativeY, width, height)) {
return;
}
}
if (isScrollableX()) {
if (getScrollX() > relativeX) {
setScrollX(relativeX);
}
int rightX = relativeX + width - s.getHorizontalPadding();
if (getScrollX() + w < rightX) {
setScrollX(getScrollX() + (rightX - (getScrollX() + w)));
} else {
if (getScrollX() > relativeX) {
setScrollX(relativeX);
}
}
}
if (isScrollableY()) {
if (getScrollY() > relativeY) {
scrollPosition = relativeY;
}
int bottomY = relativeY + height - s.getVerticalPadding();
if (getScrollY() + h < bottomY + invisibleAreaUnderVKB) {
scrollPosition = getScrollY() + (bottomY - (getScrollY() + h)) + invisibleAreaUnderVKB;
} else {
if (getScrollY() > relativeY) {
scrollPosition = relativeY;
}
}
if (isSmoothScrolling() && isInitialized()) {
initialScrollY = getScrollY();
destScrollY = scrollPosition;
initScrollMotion();
} else {
setScrollY(scrollPosition);
}
}
repaint();
} else {
// try to move parent scroll if you are not scrollable
Container parent = getParent();
if (parent != null) {
parent.scrollRectToVisible(getAbsoluteX() - parent.getAbsoluteX() + x, getAbsoluteY() - parent.getAbsoluteY() + y, width, height, parent);
}
}
}
use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class Component method refreshTheme.
/**
* Makes sure the component is up to date with the given UIID
*
* @param id The Style Id to update the Component with
* @param merge indicates if the current styles should be merged with the new styles
*/
protected void refreshTheme(String id, boolean merge) {
UIManager manager = getUIManager();
if (merge) {
Style unSelected = getUnselectedStyle();
if (hasInlineUnselectedStyle()) {
setUnselectedStyle(mergeStyle(unSelected, manager.parseComponentStyle(getInlineStylesTheme(), id, getInlineStylesUIID(id), getInlineUnselectedStyleStrings())));
} else {
setUnselectedStyle(mergeStyle(unSelected, manager.getComponentStyle(id)));
}
if (selectedStyle != null) {
if (hasInlineSelectedStyle()) {
setSelectedStyle(mergeStyle(selectedStyle, manager.parseComponentSelectedStyle(getInlineStylesTheme(), id, getInlineStylesUIID(id), getInlineSelectedStyleStrings())));
} else {
setSelectedStyle(mergeStyle(selectedStyle, manager.getComponentSelectedStyle(id)));
}
}
if (disabledStyle != null) {
if (hasInlineDisabledStyle()) {
setDisabledStyle(mergeStyle(disabledStyle, manager.parseComponentCustomStyle(getInlineStylesTheme(), id, getInlineStylesUIID(id), "dis", getInlineDisabledStyleStrings())));
} else {
setDisabledStyle(mergeStyle(disabledStyle, manager.getComponentCustomStyle(id, "dis")));
}
}
if (pressedStyle != null) {
if (hasInlinePressedStyle()) {
setPressedStyle(mergeStyle(pressedStyle, manager.parseComponentCustomStyle(getInlineStylesTheme(), id, getInlineStylesUIID(id), "press", getInlinePressedStyleStrings())));
} else {
setPressedStyle(mergeStyle(pressedStyle, manager.getComponentCustomStyle(id, "press")));
}
}
} else {
unSelectedStyle = null;
unSelectedStyle = getUnselectedStyle();
selectedStyle = null;
disabledStyle = null;
pressedStyle = null;
allStyles = null;
}
checkAnimation();
manager.getLookAndFeel().bind(this);
}
use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class Component method pointerDragged.
/**
* If this Component is focused, the pointer dragged event
* will call this method
*
* @param x the pointer x coordinate
* @param y the pointer y coordinate
*/
public void pointerDragged(final int x, final int y) {
Form p = getComponentForm();
if (p == null) {
return;
}
if (pointerDraggedListeners != null && pointerDraggedListeners.hasListeners()) {
pointerDraggedListeners.fireActionEvent(new ActionEvent(this, ActionEvent.Type.PointerDrag, x, y));
}
if (dragAndDropInitialized) {
// keep call to pointerDragged to move the parent scroll if needed
if (dragCallbacks < 2) {
dragCallbacks++;
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (dragActivated) {
pointerDragged(x, y);
}
dragCallbacks--;
}
});
}
if (!dragActivated) {
dragActivated = true;
setVisible(false);
p.setDraggedComponent(this);
oldx = x;
oldy = y;
draggedx = getAbsoluteX();
draggedy = getAbsoluteY();
}
Component dropTo = findDropTarget(this, x, y);
if (dropTo != null && dragOverListener != null) {
ActionEvent ev = new ActionEvent(this, dropTo, x, y);
dragOverListener.fireActionEvent(ev);
if (ev.isConsumed()) {
return;
}
}
if (dropTargetComponent != dropTo) {
if (dropTargetComponent != null) {
dropTargetComponent.dragExit(this);
}
dropTargetComponent = dropTo;
if (dropTargetComponent != null) {
dropTargetComponent.dragEnter(this);
}
}
// we repaint twice to create an intersection of the old and new position
p.repaint(draggedx, draggedy, getWidth(), getHeight());
draggedx = draggedx + (x - oldx);
draggedy = draggedy + (y - oldy);
oldx = x;
oldy = y;
p.repaint(draggedx, draggedy, getWidth(), getHeight());
Container scrollParent = getParent();
while (scrollParent != null && !scrollParent.isScrollable()) {
scrollParent = scrollParent.getParent();
}
if (scrollParent != null) {
Style s = getStyle();
int w = getWidth() - s.getHorizontalPadding();
int h = getHeight() - s.getVerticalPadding();
Rectangle view;
int invisibleAreaUnderVKB = getInvisibleAreaUnderVKB();
view = new Rectangle(getScrollX(), getScrollY(), w, h - invisibleAreaUnderVKB);
// if the dragging component is out of bounds move the scrollable parent
if (!view.contains(draggedx - scrollParent.getAbsoluteX(), draggedy - scrollParent.getAbsoluteY(), getWidth(), getHeight())) {
if ((scrollParent.isScrollableY() && scrollParent.getScrollY() >= 0 && scrollParent.getScrollY() + (draggedy + getHeight()) < scrollParent.getScrollDimension().getHeight()) || (scrollParent.isScrollableX() && scrollParent.getScrollX() >= 0 && scrollParent.getScrollX() + (draggedx + getWidth()) < scrollParent.getScrollDimension().getWidth())) {
int yposition = draggedy - scrollParent.getAbsoluteY() - 40;
if (yposition < 0) {
yposition = 0;
}
int xposition = draggedx - scrollParent.getAbsoluteX() - 40;
if (xposition < 0) {
xposition = 0;
}
int height = getHeight() + 80;
if (scrollParent.getScrollY() + draggedy + height >= scrollParent.getScrollDimension().getHeight()) {
yposition = draggedy - scrollParent.getAbsoluteY();
height = scrollParent.getScrollDimension().getHeight() - yposition;
}
int width = getWidth() + 80;
if (scrollParent.getScrollX() + draggedx + width >= scrollParent.getScrollDimension().getWidth()) {
xposition = draggedx - scrollParent.getAbsoluteX();
width = scrollParent.getScrollDimension().getWidth() - xposition;
}
scrollParent.scrollRectToVisible(xposition, yposition, width, height, scrollParent);
}
}
}
return;
}
if (dragActivated && p.getDraggedComponent() == null) {
dragActivated = false;
}
if (!dragActivated) {
boolean draggedOnX = Math.abs(p.initialPressX - x) > Math.abs(p.initialPressY - y);
shouldGrabScrollEvents = (isScrollableX() && draggedOnX) || isScrollableY() && !draggedOnX;
}
if (isScrollable() && isSmoothScrolling() && shouldGrabScrollEvents) {
if (!dragActivated) {
dragActivated = true;
lastScrollY = y;
lastScrollX = x;
p.setDraggedComponent(this);
p.registerAnimatedInternal(this);
Component fc = p.getFocused();
if (fc != null && fc != this) {
fc.dragInitiated();
}
}
// and pulling it in the reverse direction of the drag
if (isScrollableY()) {
int tl;
if (getTensileLength() > -1 && refreshTask == null) {
tl = getTensileLength();
} else {
tl = getHeight() / 2;
}
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
tl = 0;
}
int scroll = getScrollY() + (lastScrollY - y);
if (isAlwaysTensile() && getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() <= getHeight()) {
if (scroll >= -tl && scroll < getHeight() + tl) {
setScrollY(scroll);
}
} else {
if (scroll >= -tl && scroll < getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() - getHeight() + tl) {
setScrollY(scroll);
}
}
updateTensileHighlightIntensity(lastScrollY, y, false);
}
if (isScrollableX()) {
int tl;
if (getTensileLength() > -1) {
tl = getTensileLength();
} else {
tl = getWidth() / 2;
}
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
tl = 0;
}
int scroll = getScrollX() + (lastScrollX - x);
if (scroll >= -tl && scroll < getScrollDimension().getWidth() - getWidth() + tl) {
setScrollX(scroll);
}
}
lastScrollY = y;
lastScrollX = x;
} else {
// try to find a scrollable element until you reach the Form
Component parent = getParent();
if (!(parent instanceof Form)) {
parent.pointerDragged(x, y);
}
}
}
use of com.codename1.ui.Component in project CodenameOne by codenameone.
the class Component method setScrollY.
/**
* Indicates the X position of the scrolling, this number is relative to the
* component position and so a position of 0 would indicate the x position
* of the component.
*
* @param scrollY the Y position of the scrolling
*/
protected void setScrollY(int scrollY) {
if (this.scrollY != scrollY) {
CodenameOneImplementation ci = Display.impl;
if (ci.isAsyncEditMode() && ci.isEditingText()) {
Component editingText = ci.getEditingText();
if (editingText != null && this instanceof Container && ((Container) this).contains(editingText)) {
ci.hideTextEditor();
}
}
}
// the setter must always update the value regardless...
int scrollYtmp = scrollY;
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
Form parentForm = getComponentForm();
int v = getInvisibleAreaUnderVKB();
int h = getScrollDimension().getHeight() - getHeight() + v;
scrollYtmp = Math.min(scrollYtmp, h);
scrollYtmp = Math.max(scrollYtmp, 0);
}
if (isScrollableY()) {
if (Form.activePeerCount > 0) {
onParentPositionChange();
}
repaint();
}
if (scrollListeners != null) {
scrollListeners.fireScrollEvent(this.scrollX, scrollYtmp, this.scrollX, this.scrollY);
}
this.scrollY = scrollYtmp;
onScrollY(this.scrollY);
}
Aggregations