#include "CObjectSizeBar.h"
#include "container.h"

extern int OBJNODE;
extern mmachine mm;

#define ALPHA 0



CObjectSizeBar::CObjectSizeBar(container * cont,
							   Layer *layer,
							   Layer *shadow,
							   int x,int y,int w,int h,
							   int flags,int contflags,
							   int transp,
							   int direction
							  ):CObjectBase(cont,layer,x,y,w,h,flags,contflags,transp)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::CObjectSizeBar");
#endif
//***********************************

	SBshadowActive=0;
	SBdir=direction;
	SBshadowLayer=shadow;
	SBcurPos=0;
	SBoldPos=0;

	SBminmaxdefined=0;
	SBminPos=0;
	SBmaxPos=0;
	
	// si le container possede une fenetre
	SBsizeCursor=cont->GetPtrWindow()!=NULL?(cont->GetPtrWindow())->CrCursor(SBdir&SB_VERTICAL?IDC_SIZEWE:
																			 IDC_SIZENS)
											:0;
	SBpreviousCursor=0;
}




CObjectSizeBar::~CObjectSizeBar()
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::~CObjectSizeBar");
#endif
//***********************************

	if (ObjLayer!=NULL)
		ObjLayer->removeAllNext();
	delete(SBshadowLayer);
	if (SBsizeCursor)
		Container()->GetPtrWindow()->DsCursor(SBsizeCursor);
	0;
}




int CObjectSizeBar::GetIndexShadow()
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::GetIndexShadow");
#endif
//***********************************

	if (ObjFlags&SB_SHADOW)
		return 1;
	else
		return 0;
}



int ValInRange(int val,int i,int j)
{
	return (val>=i && val<=j);
}




int CObjectSizeBar::ValidCoordinates(int x,int y)
{
	return (!SBminmaxdefined) ||
		   (SBminmaxdefined && ValInRange(SBdir&SB_HORIZONTAL?y:x,SBminPos,SBmaxPos));
}

void CObjectSizeBar::AddShadow(int x,int y)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::AddShadow");
#endif
//***********************************

	SBoldPos=SBcurPos;
	
	if (SBdir&SB_HORIZONTAL)
		SBcurPos=y;
	else if (SBdir&SB_VERTICAL)
		SBcurPos=x;
	
	SBshadowActive=1;
	Redraw();
}




void CObjectSizeBar::RemShadow()
{
	SBshadowActive=0;
}



	
void CObjectSizeBar::MoveShadow(int x,int y,int keyflags)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::MoveShadow");
#endif
//***********************************

	int newx=x-ObjX;
	int newy=y-ObjY;
	int valid=ValidCoordinates(newx,newy);
	if (SBshadowActive && keyflags&MK_LBUTTON)
	{
		
		if (ValidCoordinates(newx,newy))
		{
			// entre min et max
			RemShadow();
			AddShadow(newx,newy);
		}
		else
		{	// en dehors des bornes
			int pos=SBdir&SB_HORIZONTAL?newy:newx;
			if (pos<SBminPos)
			{
				RemShadow();
				if (SBdir&SB_HORIZONTAL)
					AddShadow(0,SBminPos);
				else
					AddShadow(SBminPos,0);
			}
			else if (pos>SBmaxPos)
			{
				RemShadow();
				if (SBdir&SB_HORIZONTAL)
					AddShadow(0,SBmaxPos);
				else
					AddShadow(SBmaxPos,0);
			}
		}
	}

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::MoveShadow end");
#endif
//***********************************
}




void CObjectSizeBar::ChangeCursorToSize()
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::ChangeCursorToSize");
#endif
//***********************************

	if (Container()->GetPtrWindow()!=NULL)
		SBpreviousCursor=Container()->GetPtrWindow()->ChangeCursor(SBsizeCursor);
}


void CObjectSizeBar::RetrieveDefaultCursor()
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::RetrieveDefaultCursor");
#endif
//***********************************

	if (Container()->GetPtrWindow()!=NULL)
		Container()->GetPtrWindow()->ChangeCursor(SBpreviousCursor);
}





int CObjectSizeBar::ExecuteReflexeEndResize()
{
	int x,y,k,tmp_res;
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::ExecuteReflexeEndResize");
#endif
//***********************************


	if (SBdir&SB_VERTICAL)
	{
		x=SBcurPos;
		y=0;
	}
	else if (SBdir&SB_HORIZONTAL)
	{
		x=0;
		y=SBcurPos;
	}
	k=OBJbeginreflex(mm,OBJNODE,(int)this,RFLOBJNODE_UNCLICK);
	if (k>0) return 1L; 
	if (k==0)
	{
		CHECK(MMpush(mm,ITOM(ObjX+x)));
		CHECK(MMpush(mm,ITOM(ObjY+y)));
		return OBJcallreflex(mm,2);
	}
	return 0;
}






int CObjectSizeBar::SetMinMax(int min,int max)
{
	SBminmaxdefined=1;
	SBminPos=min;
	SBmaxPos=max;
	return 0;
}





int CObjectSizeBar::IsMouseOnObject(int x,int y, int p_tab)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::IsMouseOnObject");
#endif
//***********************************

	PtrObjBitmap bmp=GET_PTR_OBJ_BITMAP(GET_BMP(MTOP(MMfetch(mm,p_tab,ALPHA))));
	if ( bmp == NULL )
		return false;
	else if (SBshadowActive)
		return IsPointInRectangle(Point2D(x,y),Rect2D(ObjX,ObjY,ObjX+ObjW,ObjY+ObjH));
	else 
		return true;
}





Rect2D CObjectSizeBar::RectangleIncludingObject()
{
	int x1,y1,x2,y2;
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::RectangleIncludingObject");
#endif
//***********************************


	if (SBshadowActive)
	{
		if (SBdir&SB_HORIZONTAL)
			if (SBcurPos<0 && SBoldPos<0)
			{
				x1=ObjX;
				y1=ObjY+min(SBcurPos,SBoldPos);
				x2=ObjX+ObjW;
				y2=ObjY+ObjH;
			}
			else if (SBcurPos>=0 && SBoldPos>=0)
			{
				x1=ObjX;
				y1=ObjY;
				x2=ObjX+ObjW;
				y2=ObjY+max(SBcurPos,SBoldPos)+ObjH;
			}
			else
			{
				x1=ObjX;
				y1=ObjY+min(SBoldPos,SBcurPos);
				x2=ObjX+ObjW;
				y2=ObjY+max(SBoldPos,SBcurPos)+ObjH;
			}
		else if (SBdir&SB_VERTICAL)
			if (SBcurPos<0 && SBoldPos<0)
			{
				x1=ObjX+min(SBcurPos,SBoldPos);
				y1=ObjY;
				x2=ObjX+ObjW;
				y2=ObjY+ObjH;
			}
			else if (SBcurPos>=0 && SBoldPos>=0)
			{
				x1=ObjX;
				y1=ObjY;
				x2=ObjX+max(SBcurPos,SBoldPos)+ObjW;
				y2=ObjY+ObjH;
			}
			else
			{
				x1=ObjX+min(SBoldPos,SBcurPos);
				y1=ObjY;
				x2=ObjX+max(SBoldPos,SBcurPos)+ObjW;
				y2=ObjY+ObjH;
			}
		return (Rect2D (x1,y1,x2,y2));
	}
	return CObjectBase::RectangleIncludingObject();
}





int CObjectSizeBar::CursorMove(int x,int y,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::CursorMove");
#endif
//***********************************

	if ( this->IsObjectEnabled() )
	{
		MoveShadow(x,y,keyFlags);
	}
	return 0;
}





int CObjectSizeBar::CursorMoveIn(int x,int y,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::CursorMoveIn");
#endif
//***********************************

	if ( this->IsObjectEnabled() )
	{
		ChangeCursorToSize();
	}
	return 0;
}





int CObjectSizeBar::CursorMoveInWithBtnPushed(int x,int y,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::CursorMoveInWithBtnPushed");
#endif
//***********************************

	if ( this->IsObjectEnabled() )
	{
		if (!SBshadowActive)
			ChangeCursorToSize();
		MoveShadow(x,y,keyFlags);
	}
	return 0;
}




int CObjectSizeBar::CursorMoveOut(int x,int y,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::CursorMoveOut");
#endif
//***********************************

	if ( this->IsObjectEnabled() )
	{
		RetrieveDefaultCursor();
	}
	return 0;
}




int CObjectSizeBar::CursorMoveOutWithBtnPushed(int x,int y,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::CursorMoveOutWithBtnPushed");
#endif
//***********************************

	if ( this->IsObjectEnabled() )
	{
		MoveShadow(x,y,keyFlags);
	}
	return 0;
}




int CObjectSizeBar::CursorMoveOutsideWithBtnPushed(int x,int y,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::CursorMoveOutsideWithBtnPushed");
#endif
//***********************************


	if ( this->IsObjectEnabled() )
	{
		MoveShadow(x,y,keyFlags);
	}
	return 0;
}




int CObjectSizeBar::ClickIn(int x,int y,int btn,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::ClickIn");
#endif
//***********************************

	if (btn==1)
		AddShadow(0,0);
	else
	{
		RetrieveDefaultCursor();
		Rect2D rect=RectangleIncludingObject();
		RemShadow();
		Container()->RedrawArea(rect,1);
	}
	return 0;
}





int CObjectSizeBar::UnClickIn(int x,int y,int btn,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::UnClickIn");
#endif
//***********************************

	if (btn==1 && SBshadowActive)
	{
		Rect2D rect=RectangleIncludingObject();
		RemShadow();
		Container()->RedrawArea(rect,1);
		ExecuteReflexeEndResize();
	}
	return 0;
}



	
int CObjectSizeBar::ClickOut(int x,int y,int btn,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::ClickOut");
#endif
//***********************************

	int rfl=SBshadowActive;
	Rect2D rect=RectangleIncludingObject();
	RetrieveDefaultCursor();
	RemShadow();
	Container()->RedrawArea(rect,1);
	if (btn==1 && rfl)
		ExecuteReflexeEndResize();	
	return 0;
}





int CObjectSizeBar::UnClickOut(int x,int y,int btn,int keyFlags, int p_tab,int redrawobject)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::UnClickOut");
#endif
//***********************************


	if (btn==1 && SBshadowActive)
	{
		Rect2D rect=RectangleIncludingObject();
		RetrieveDefaultCursor();
		RemShadow();
		Container()->RedrawArea(rect,1);
		ExecuteReflexeEndResize();
	}
	return 0;
}

int CObjectSizeBar::DblClickIn(int x,int y,int btn,int keyFlags, int p_tab,int redrawobject)
{
	return 0;
}

int CObjectSizeBar::MouseWheel(int delta,int x,int y,int keyFlags,int p_tab,int redrawobject)
{
	return 0;
}


int CObjectSizeBar::KeyUp(UINT vk,int cRepeat, UINT flags, int p_tab)
{
	return 0;
}

int CObjectSizeBar::KeyDown(UINT vk,int keysys,int cRepeat, UINT flags, int p_tab)
{
	return 0;
}

int CObjectSizeBar::SetFocus(int reset,int p_tab,int redraw)
{
	return 0;
}

int CObjectSizeBar::KillFocus(int p_tab,int redraw)
{
	return 0;
}

int CObjectSizeBar::Timer( int timerID )
{
	return 0;
}







Layer *CObjectSizeBar::GetLayer(mmachine m,int p_tab)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::GetLayer");
#endif
//***********************************

	if (ObjLayer==NULL) return NULL;
	
	ObjLayer->removeAllNext();
	SBshadowLayer->RGBbitmap  = ObjLayer->RGBbitmap	  = GET_PTR_OBJ_BITMAP(GET_BMP(MTOP(MMfetch(mm,p_tab,ALPHA))));
	SBshadowLayer->AlphaBitmap= ObjLayer->AlphaBitmap = GET_PTR_OBJ_BITMAP(GET_ABMP(MTOP(MMfetch(mm,p_tab,ALPHA))));
	ObjLayer->sourceRect=Rect2D(0,0,ObjW-1,ObjH-1);
	ObjLayer->Xdecal=0;
	ObjLayer->Ydecal=0;

	if (SBshadowActive)
	{
		
		ObjLayer->addLayer(SBshadowLayer);
		int ndx=GetIndexShadow();
		if (SBdir&SB_HORIZONTAL)
			SBshadowLayer->sourceRect  = Rect2D(0,ndx*ObjH,ObjW-1,(ndx+1)*ObjH-1);
		else
			SBshadowLayer->sourceRect  = Rect2D(ndx*ObjW,0,(ndx+1)*ObjW-1,ObjH-1);
	}
	return ObjLayer;
}




Layer *CObjectSizeBar::GetLayerPart(mmachine m,Rect2D *area2redraw,int p_tab)
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::GetLayerPart");
#endif
//***********************************

	if (ObjLayer==NULL) return NULL;

	ObjLayer->removeAllNext();
	SBshadowLayer->RGBbitmap  = ObjLayer->RGBbitmap	  = GET_PTR_OBJ_BITMAP(GET_BMP(MTOP(MMfetch(mm,p_tab,ALPHA))));
	SBshadowLayer->AlphaBitmap= ObjLayer->AlphaBitmap = GET_PTR_OBJ_BITMAP(GET_ABMP(MTOP(MMfetch(mm,p_tab,ALPHA))));
	ObjLayer->sourceRect  = IntersectionRectangle(Rect2D(0,0,ObjW-1,ObjH-1),
												  *area2redraw
												 );
	ObjLayer->Xdecal=0;
	ObjLayer->Ydecal=0;

	if (SBshadowActive)
	{
		
		// evaluation du layer et source rect
		Point2D pt;
		Rect2D rect;
		if (SBdir&SB_HORIZONTAL)
		{	// SizeBar Horizontal
			pt=Point2D(0,-SBcurPos+GetIndexShadow()*ObjH);
			rect=IntersectionRectangle(Rect2D(0,SBcurPos,ObjW-1,SBcurPos+ObjH-1),*area2redraw);
		}
		else
		{
			// SizeBar Vertical
			pt=Point2D(-SBcurPos+GetIndexShadow()*ObjW,0);
			rect=IntersectionRectangle(Rect2D(SBcurPos,0,SBcurPos+ObjW-1,ObjH-1),*area2redraw);
		}


		if (!rect.IsRectEmpty())
		{
			// on ajoute le shadow ŕ l'objet
			ObjLayer->addLayer(SBshadowLayer);
			SBshadowLayer->sourceRect  = MoveRectangleByVecteur(rect,pt);
		
			if (SBdir&SB_HORIZONTAL)
			{	// SizeBar Horizontal
				ObjLayer->Xdecal=0;
				ObjLayer->Ydecal=-area2redraw->RctHG.iptY;
				SBshadowLayer->Xdecal=0;
				SBshadowLayer->Ydecal=SBcurPos-area2redraw->RctHG.iptY;
			}
			else
			{
				// SizeBar Vertical
				ObjLayer->Xdecal=-area2redraw->RctHG.iptX;
				ObjLayer->Ydecal=0;
				SBshadowLayer->Xdecal=SBcurPos-area2redraw->RctHG.iptX;
				SBshadowLayer->Ydecal=0;
			}
		}
	}


//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::GetLayerPart end");
#endif
//***********************************
	return ObjLayer;
}





int CObjectSizeBar::DestroyAllLayers()
{
//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::DestroyAllLayers");
#endif
//***********************************

	if (ObjLayer!=NULL)
	{
		ObjLayer->removeAllNext();
		delete(ObjLayer);
	}
	if (SBshadowLayer!=NULL)
		delete(SBshadowLayer);
	ObjLayer=NULL;
	SBshadowLayer=NULL;
	return 0;
}





int CObjectSizeBar::ResizeLayer(int w,int h,int p_tab)
{
	int k,tmp_res;

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::ResizeLayer");
#endif
//***********************************

	k=OBJbeginreflex(mm,OBJNODE,(int)this,RFLOBJNODE_RESIZE_RESSOURCE);
	if (k==0)
	{
		int nbre_etat=1;
		if (ObjFlags&SB_SHADOW) nbre_etat++;
		
		if (SBdir&SB_HORIZONTAL)
			h=h*nbre_etat;
		else
			w=w*nbre_etat;
		
		// execution du reflexe
		CHECK(MMpush(mm,ITOM(w)));
		CHECK(MMpush(mm,ITOM(h)));
		CHECK(OBJcallreflex(mm,2));
		
		/* prevent from destruction of container or object in user callback */
		if (GetObjectBase(mm,FindObjNodeFromHdlSys(mm,(int)this))==NULL) 
			return 0;

		// pile : AlphaBitmap
		int transparency;
		int p_abmp;

		if ((p_abmp=MTOP(MMget(mm,-2)))==NIL)
		{
			// ressource invalide!!
			MMechostr(1,"_CBcompSizeBarResizeResource: alphabitmap is nil\n");
			ChangeResource(mm,ALPHA,NIL);		
			return 0;
		}

		// remplacement du layer du rollover dans la structure
		// recuperation de l'alphabitmap utilisateur 
		// et creation du nouveau layer
		PtrObjBitmap bmp =GET_PTR_OBJ_BITMAP(GET_BMP(p_abmp));
		PtrObjBitmap abmp=GET_PTR_OBJ_BITMAP(GET_ABMP(p_abmp));
		if ((transparency = GET_TRANSP(p_abmp))==NIL) transparency = NO_TRANSPARENCY;

		if (bmp!=NULL)

		{
			if (bmp->TailleW==w && bmp->TailleH==h)
			{
				ObjLayer = new Layer(bmp,abmp, transparency );
				SBshadowLayer = new Layer(bmp,abmp, transparency );
			}
			else
				MMechostr(1,"_CBcompSizeBarResizeResource: not good format for alphabitmap (requested size:%d %d received size:%d %d)\n",w,h,bmp->TailleW,bmp->TailleH);
		}
		else
			MMechostr(1,"_CBcompSizeBarResizeResource: alphabitmap already destroyed\n");
		
		// remplacement de la valeur du pointeur de l'alphabitmap
		// dans le tab
		ChangeResource(mm,ALPHA,p_abmp);
		return 0;
	}
	// callback non definie ou invalide et objet doit etre absolument resizé!!
	MMechostr(1,"_CBcompSizeBarResizeResource not defined and object need to be resized!!!\n");
	return 1;
	

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nCObjectSizeBar::ResizeLayer end");
#endif
//***********************************

	return 0;
}





// FONCTIONS SCOLS
int _CRcompSizeBar(mmachine m)
{
	int tmp_res;
	int p_alphabmp,flags,contflags;
	int p_coordinates,p_objpere,p_container;
	int transparency,x,y,w,h;
	int direction;
	CObjectSizeBar *new_object;
	CObjectBase *obj_root,*obj_father;
	container *co;

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\n_CRcompSizeBar");
#endif
//***********************************

	// les differents tests de non validité
	if ((MMget(m,8))==NIL)
	{
		MMechostr(MSKTRACE,"_CRcompSizeBar: channel is nil\n");
		m->pp += 8;
		MMset(m,0,NIL);
		return 0;	
	}
	if ((MMget(m,7))==NIL)
	{
		MMechostr(MSKTRACE,"_CRcompSizeBar: container is nil\n");
		m->pp += 8;
		MMset(m,0,NIL);
		return 0;
	}
	if ((p_alphabmp=MTOP(MMget(m,0)))==NIL)
	{
		MMechostr(MSKTRACE,"_CRcompSizeBar: alphabitmap is nil\n");
		m->pp += 8;
		MMset(m,0,NIL);
		return 0;
	}
	if (GET_PTR_OBJ_BITMAP(GET_BMP(p_alphabmp))==NULL)
	{
		MMechostr(MSKTRACE,"_CRcompSizeBar: alphabitmap already destroyed\n");
		m->pp += 8;
		MMset(m,0,NIL);
		return 0;
	}
	
	// pile : Chn cont ObjNode [I I] I I I Alpha
	INVERT(m,0,4);
	// pile : Chn cont ObjNode Alpha I I I [I I]
	if ((p_coordinates=MTOP(MMpull(m)))==NIL)
	{
		x=0;
		y=0;
	}
	else
	{
		x=MTOI(MMfetch(m,p_coordinates,0));
		y=MTOI(MMfetch(m,p_coordinates,1));
	}
	if ((direction=MTOI(MMpull(m)))==NIL) direction=SB_HORIZONTAL;
	if ((contflags=MTOI(MMpull(m)))==NIL) contflags=0;
	if ((flags=MTOI(MMpull(m)))==NIL) flags=OBJ_ENABLE|OBJ_VISIBLE;
	
	int nbetat=1;
	if (flags&SB_SHADOW) nbetat++;	
	
	PtrObjBitmap bmp,abmp;
	p_alphabmp = MTOP( MMget( m, 0 ) );
	bmp =GET_PTR_OBJ_BITMAP(GET_BMP(p_alphabmp));
	abmp=GET_PTR_OBJ_BITMAP(GET_ABMP(p_alphabmp));
	if ((transparency = GET_TRANSP(p_alphabmp))==NIL) transparency = NO_TRANSPARENCY;
			
	if (direction&SB_HORIZONTAL)
	{
		w=bmp->TailleW;
		h=bmp->TailleH/nbetat;
	}
	else
	{
		w=bmp->TailleW/nbetat;
		h=bmp->TailleH;
	}

	// pile : Chn cont ObjNode Alpha
	p_objpere=MTOP(MMget(m,1));
	p_container=MTOP(MMget(m,2));
	obj_father=GetObjectBase(m,p_objpere);

	// on inverse p_objpere et channel
	INVERT( m, 1, 3 );
	
	// pile : ObjNode cont Chn Alpha
	if ((p_objpere!=NIL)&&(obj_father==NULL))
	{
		MMechostr(MSKTRACE,"_CRcompSizeBar: object father already destroyed\n");
		m->pp += 3;
		MMset(m,0,NIL);
		return 0;
	}
	else if ((co=RetrievePtrContainer(m,p_container))==NULL)
	{
		MMechostr(MSKTRACE,"_CRcompSizeBar: container already destroyed\n");
		m->pp += 3;
		MMset(m,0,NIL);
		return 0;
	}
	else if (!co->OwnsObject(obj_father))
	{
		MMechostr(MSKTRACE,"_CRcompSizeBar: father object was not created in this container!\n");
		m->pp += 3;
		MMset(m,0,NIL);
		return 0;
	}
	else
	{
		obj_root=co->Root();
	
		// creation du nouvel objet
		new_object=new CObjectSizeBar(co,
									  new Layer(bmp,abmp,transparency),
									  new Layer(bmp,abmp,transparency),
									  x,y,w,h,
									  flags,contflags,
									  transparency,
									  direction);
	
		// ajout de l'objet et creation du nouvel OBJNODE
		CHECK( AddNode(m,obj_root,obj_father,new_object,1) );

		// pile ObjPere Container ObjNode
		INVERT(m,0,2);
		m->pp+=2;
#if DEBUG_OBJNODE
	PrintTree(m,0,FindObjNodeFromHdlSys(m,(int)obj_root));
#endif
	}

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\n_CRcompSizeBar end");
#endif
//***********************************

	return 0;

}





int _DScompSizeBar(mmachine m)
{
	int p;

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\n_DScompSizeBar");
#endif
//***********************************

	if ((p=MTOP(MMpull(m)))!=NIL)
	{
		DsNode(m,p);
		return MMpush(m,0);
	}
	else
		return MMpush(m,NIL);
}





int _SETcompSizeBarMinMax(mmachine m)
{
	int min,max,p_obj;

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\n_SETcompSizeBarMinMax");
#endif
//***********************************

	max=MTOI(MMpull(m));
	min=MTOI(MMpull(m));
	if ((p_obj=MTOP(MMget(m,0)))==NIL)
		MMechostr(MSKTRACE,"_SETcompSizeBarMinMax: CompSizeBar is nil.\n");
	else
	{
		CObjectBase *obj;
		// on recupere l'objet
		if ((obj=GetObjectBase(m,p_obj))==NULL)
			MMechostr(MSKTRACE,"_SETcompSizeBarMinMax: Object already destroyed\n");
		else
			(static_cast <CObjectSizeBar *> (obj))->SetMinMax(min,max);
	}
	return 0;
}





int _CONVERTcompSizeBarToObjNode(mmachine m)
{
	return 0;
}

int _CBcompSizeBarEndResize(mmachine m)
{
	return OBJaddreflex( m, OBJNODE, RFLOBJNODE_UNCLICK );
}

int _CBcompSizeBarResizeResource(mmachine m)
{
	return OBJaddreflex(m,OBJNODE,RFLOBJNODE_RESIZE_RESSOURCE);
}

int _CBcompSizeBarResize(mmachine m)
{
	return OBJaddreflex(m,OBJNODE,RFLOBJNODE_RESIZE);
}

