package anywheresoftware.b4a.objects;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.Button;
import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BALayout;
import anywheresoftware.b4a.ObjectWrapper;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Hide;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BALayout.LayoutParams;
import anywheresoftware.b4a.keywords.Common;
import anywheresoftware.b4a.objects.drawable.ColorDrawable;
import anywheresoftware.b4a.objects.drawable.CanvasWrapper.BitmapWrapper;
import anywheresoftware.b4a.objects.drawable.ColorDrawable.GradientDrawableWithCorners;
@Hide
@Events(values={"Click", "LongClick"})
public class ViewWrapper<T extends View> extends AbsObjectWrapper<T>{
protected BA ba;
@Hide
public static int lastId;
public void Initialize(final BA ba, String EventName) {
innerInitialize(ba, EventName.toLowerCase(BA.cul), false);
}
@Hide
public void innerInitialize(final BA ba, final String eventName, boolean keepOldObject) {
this.ba = ba;
getObject().setId(++lastId);
if (ba.subExists(eventName + "_click")) {
getObject().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ba.raiseEvent(ViewWrapper.this.getObject(), eventName + "_click");
}
});
}
if (ba.subExists(eventName + "_longclick")) {
getObject().setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ba.raiseEvent(ViewWrapper.this.getObject(), eventName + "_longclick");
return true;
}
});
}
}
/**
* Gets or sets the background drawable.
*/
public Drawable getBackground() {
return getObject().getBackground();
}
public void setBackground(Drawable drawable) {
getObject().setBackgroundDrawable(drawable);
}
public void SetBackgroundImage(Bitmap Bitmap) {
anywheresoftware.b4a.objects.drawable.BitmapDrawable bd = new anywheresoftware.b4a.objects.drawable.BitmapDrawable();
bd.Initialize(Bitmap);
getObject().setBackgroundDrawable(bd.getObject());
}
/**
* Invalidates the whole view forcing the view to redraw itself.
*Redrawing will only happen when the program can process messages. Usually when it finishes running the current code.
*/
public void Invalidate() {
getObject().invalidate();
}
/**
* Invalidates the given rectangle.
*Redrawing will only happen when the program can process messages. Usually when it finishes running the current code.
*/
public void Invalidate2(Rect Rect) {
getObject().invalidate(Rect);
}
/**
* Invalidates the given rectangle.
*Redrawing will only happen when the program can process messages. Usually when it finishes running the current code.
*/
public void Invalidate3(int Left, int Top, int Right, int Bottom) {
getObject().invalidate(Left, Top, Right, Bottom);
}
/**
* Gets or sets the view's width.
*/
public void setWidth(int width) {
ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) getObject().getLayoutParams();
lp.width = width;
getObject().getParent().requestLayout();
}
public int getWidth() {
return ((ViewGroup.LayoutParams)getObject().getLayoutParams()).width;
}
public int getHeight() {
return ((ViewGroup.LayoutParams)getObject().getLayoutParams()).height;
}
public int getLeft() {
BALayout.LayoutParams lp = (LayoutParams) getObject().getLayoutParams();
return lp.left;
}
public int getTop() {
BALayout.LayoutParams lp = (LayoutParams) getObject().getLayoutParams();
return lp.top;
}
public void setHeight(int height) {
ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) getObject().getLayoutParams();
lp.height = height;
getObject().getParent().requestLayout();
}
public void setLeft(int left) {
BALayout.LayoutParams lp = (LayoutParams) getObject().getLayoutParams();
lp.left = left;
getObject().getParent().requestLayout();
}
public void setTop(int top) {
BALayout.LayoutParams lp = (LayoutParams) getObject().getLayoutParams();
lp.top = top;
getObject().getParent().requestLayout();
}
/**
* Sets the background of the view to be a ColorDrawable with the given color.
*If the current background is of type GradientDrawable or ColorDrawable the round corners will be kept.
*/
public void setColor(int color) {
Drawable d = getObject().getBackground();
if (d != null && d instanceof GradientDrawable) {
float radius = 0;
if (d instanceof GradientDrawableWithCorners) {
radius = ((GradientDrawableWithCorners)d).cornerRadius;
}
else {
GradientDrawable g = (GradientDrawable) getObject().getBackground();
try {
Field state = g.getClass().getDeclaredField("mGradientState");
state.setAccessible(true);
Object gstate = state.get(g);
Field radiusF = gstate.getClass().getDeclaredField("mRadius");
radius = (Float) radiusF.get(gstate);
} catch (Exception e) {
Common.Log(e.toString());
}
}
ColorDrawable cd = new ColorDrawable();
cd.Initialize(color, (int)radius);
getObject().setBackgroundDrawable(cd.getObject());
}
else {
getObject().setBackgroundColor(color);
}
}
public void setTag(Object tag) {
getObject().setTag(tag);
}
/**
* Gets or sets the Tag value. This is a place holder which can used to store additional data.
*/
public Object getTag() {
return getObject().getTag();
}
public void setVisible(boolean Visible) {
getObject().setVisibility(Visible ? View.VISIBLE : View.GONE);
}
public boolean getVisible() {
return getObject().getVisibility() == View.VISIBLE ? true : false;
}
public void setEnabled(boolean Enabled) {
getObject().setEnabled(Enabled);
}
public boolean getEnabled() {
return getObject().isEnabled();
}
/**
* Changes the Z order of this view and brings it to the front.
*/
public void BringToFront() {
if (getObject().getParent() instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) getObject().getParent();
vg.removeView(getObject());
vg.addView(getObject());
}
}
/**
* Changes the Z order of this view and sends it to the back.
*/
public void SendToBack() {
if (getObject().getParent() instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) getObject().getParent();
vg.removeView(getObject());
vg.addView(getObject(), 0);
}
}
/**
* Removes this view from its parent.
*/
public void RemoveView() {
if (getObject().getParent() instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) getObject().getParent();
vg.removeView(getObject());
}
}
/**
* Changes the view position and size.
*/
public void SetLayout(int Left, int Top, int Width, int Height) {
BALayout.LayoutParams lp = (LayoutParams) getObject().getLayoutParams();
lp.left = Left;
lp.top = Top;
lp.width = Width;
lp.height = Height;
getObject().getParent().requestLayout();
}
/**
* Tries to set the focus to this view.
*Returns True if the focus was set.
*/
public boolean RequestFocus() {
return getObject().requestFocus();
}
}