/*************************************************/
/*                                               */
/* bitmaplist.c                                  */
/*                                               */
/* bitmap list management                        */
/*                                               */
/*************************************************/


#include "x/Version.h"
#include "x/scolplugin.h"
#include "objstr.h"
#include "objects/bitmap.h"


/*********************************************************************************/
/*                                                                               */
/*  HIMAGELIST NewBitmapList ( PtrObjListBitmap * L )                            */
/*  Cree une nouvelle liste de bitmap                                            */
/*                                                                               */
/*********************************************************************************/

HIMAGELIST NewBitmapList ( PtrObjListBitmap L )
{

    return ImageList_Create ( L->TailleW , L->TailleH ,ILC_COLOR16 , 
       1 , 1 ) ;
} ;



/************************************************************************************************/
/*                                                                                              */
/*      int GRCreateListBitmap ( mmachine m )                                                   */
/*      corerspond a la fonction ObjListBitmap _CRlistBitmap ( Chn I I I I ) ;                  */
/*                                                                                              */
/************************************************************************************************/

int GRCreateListBitmap ( mmachine m )
{
    int  w , h , s , s2 , l ;
    PtrObjVoid O ;
    PtrObjListBitmap L ;

    /********************* DEBUG 2D     ************************************************/
    #ifdef TRACE2D 
        MMechostr(MSKDEBUG,"DBG _CRbitmapList\n");
        FDebug2D (m);
    #endif
    /***********************************************************************************/

    w = MMpull(m)>>1;
    h = MMpull(m)>>1;

  /* creation de la zone memoire de l'objet general */
    l = ( sizeof ( struct ObjVoid ) + 3 ) >> 2 ;
    s = MMmallocCLR (m,l,TYPETAB) ;
    if ( s == NIL )
    {
        MMechostr(MSKDEBUG,"_create_edit_text : Error in alloc MAGMA Memory\n" ) ;         
        return MERRMEM ;
    }    
    if ( MMpush(m,(s<<1)+1)) return MERRMEM ;
    /* on rempile l'addresse au cas ou il y aurait un GC par la suite */

    /* creation de la zone memoire pour les donnees relatives aux objets textes */
    l = ( sizeof ( struct ObjListBitmap ) + 3 ) >> 2 ;
    s2 = MMmalloc ( m,l,TYPEBUF ) ;
    if ( s2 == NIL )
    {
        MMechostr(MSKDEBUG,"_create_edit_text : Error in alloc MAGMA Memory\n" ) ;
        return MERRMEM ;
    }


    L = ( PtrObjListBitmap ) MMstart(m, s2 ) ;
    s = MMpull (m ) ; /* recupere l'addresse de l'objet de base */
    O = ( PtrObjVoid ) MMstart(m, (s>>1) ) ;

    O -> Type             = OBJ_TYPE_LISTBITMAP << 1  ;        
    O->Buffer = (s2<<1)+1 ;
    O->Tab = NIL ;
    L->TailleW = w ;
    L->TailleH = h ;
   
    L->WHandler = NewBitmapList ( L ) ;
    if ( L->WHandler == NULL )     
    {

        MMechostr (1,"_CRbitmapList : Error in creating the WIN95 Object - Check the values\n" ) ;
        MMset(m,0,NIL) ;
        return 0 ;
    }

    MMpush(m,s) ;
    s = OBJcreate(m,OBJTYPLISTBITMAP,(int)L->WHandler,OBJTYPLISTBITMAP,(int)NULL) ;

    /****************************** DEBUG 2D **********************************************/
    #ifdef TRACE2D
        FDebug2D ( m ) ;
        MMechostr ( 1 , "DBG _CRbitmapList done\n" );
    #endif
    /***************************************************************************************/

    return s ;
}


/*******************************************************************************************/
/*                                                                                         */
/*      int GRAddBitmapList ( mmachine m )                                                 */
/*      correspond a la fonction I _ADDbitmapList ( ObjBitmapList , ObjBitmap ) ;          */
/*                                                                                         */
/*******************************************************************************************/

int GRAddBitmapList ( mmachine m )
{
    int s , s2 , res ;
    PtrObjVoid OB , OL ;
    PtrObjListBitmap L ;
    PtrObjBitmap B ;

    /****************************** DEBUG 2D ****************************************/
    #ifdef TRACE2D
        MMechostr (1,"(DBG) _ADDbitmapList\n");
        FDebug2D (m);
    #endif
    /*********************************************************************************/

    s = MMpull(m);
    s2 = MMpull(m) ;

    if ( s == NIL ||s2 == NIL )
    {
         MMechostr ( 1 , "(WARNING) _ADDbitmapList bitmap or bitmap list is NIL\n" ) ;
         res = NIL ;
    }
    else
    {

        OL = ( PtrObjVoid ) MMstart(m,(s2>>1));
        OB = ( PtrObjVoid ) MMstart(m,(s>>1));
        B = ( PtrObjBitmap ) MMstart(m,(OB->Buffer>>1));
        L = ( PtrObjListBitmap ) MMstart(m,(OL->Buffer>>1)) ;


		//$ LB (13/06/2002) : create DIBSection from bitmap buffer
		if (!B->DIBhandler) ObjBitmap_CreateDIBSection (B);

        res = ImageList_Add ( L->WHandler , B->DIBhandler, NULL ) ;
    }

    res = MMpush(m,res<<1);

    /**************************** DEBUG 2D *****************************************/
    #ifdef TRACE2D
        FDebug2D ( m ); 
        MMechostr (1,"(DBG) _ADDbitmapList Done\n" );
    #endif
    /*******************************************************************************/

    return res ;
}

/***********************************************************************************/
/*                                                                                 */
/*  int GRRemoveBitmapList ( mmachine m ) ;                                        */
/*  correspond a ObjBitmapList _DELbitmapFromList ( ObjBitmapList , BitmapIndex)   */
/*                                                                                 */
/***********************************************************************************/

int GRRemoveBitmapList ( mmachine m )
{
    int s , idx ;
    PtrObjVoid O ;
    PtrObjListBitmap L ;

    /************************** DEBUG 2D **************************************/
    #ifdef TRACE2D
        MMechostr(MSKDEBUG,"(DBG) _DELbitmapFromList\n");
        FDebug2D(m);
    #endif
    /**************************************************************************/

    idx = MMpull(m)>>1;
    s = MMpull(m);

    if ( idx!=NIL)
    if (s!=NIL)
    {
        O = ( PtrObjVoid ) MMstart(m,(s>>1));
        L = ( PtrObjListBitmap ) MMstart(m,(O->Buffer>>1)) ;
        ImageList_Remove (L->WHandler,idx>>1);
    } else MMechostr (1,"(ERROR) _DELbitmapFromList : list is NIL\n");

    s = MMpush(m,s);

    /***************************** DEBUG 2D ***********************************/
    #ifdef TRACE2D
        FDebug2D (m );
        MMechostr(MSKDEBUG,"(DBG) _DELbitmapFromList done\n");
    #endif
    /**************************************************************************/

     return s ;
}


/********************************************************************************/
/*                                                                              */
/*  int GRGetBitmapListCount ( mmachine m ) ;                                   */
/*  correspond a I _GETbitmapListCount ( ObjBitmapList ) ;                      */
/*                                                                              */
/********************************************************************************/

int GRGetBitmapListCount ( mmachine m ) 
{
    int s , res ;
    PtrObjVoid O ;
    PtrObjListBitmap L ;

    /********************* DEBUG 2D *********************************************/
    #ifdef TRACE2D
        MMechostr(MSKDEBUG,"(DBG) _GETbitmapListCount\n") ;
        FDebug2D(m);
    #endif
    /*****************************************************************************/

    s = MMpull(m);
    if(s!=NIL)
    {
        O=(PtrObjVoid) MMstart(m,(s>>1));
        L=(PtrObjListBitmap) MMstart(m,(O->Buffer>>1));
        res = ImageList_GetImageCount ( L->WHandler ) ;
    } else res = NIL ;

    s = MMpush(m,(res<<1)) ;

    /************************** DEBUG 2D ***************************************/
    #ifdef TRACE2D
        FDebug2D(m);
        MMechostr(MSKDEBUG,"(DBG) _GETbitmapListCount done\n");
    #endif
    /***************************************************************************/

    return s ;
}

/************************************************************************************************/
/*                                                                                              */
/*      int GRDestroyBitmapList ( mmachine m ) ;                                                */
/*      correspond a la fonction I _DSbitmapList ( ObjBitmapList ) ;                            */
/*                                                                                              */
/************************************************************************************************/

int GRDestroyBitmapList ( mmachine m )
{
    int s ;
    PtrObjVoid O ;
    PtrObjListBitmap L ;


    /************************** DEBUG 2D **********************************************/
    #ifdef TRACE2D
        MMechostr(MSKDEBUG,"(DBG) _DSbitmapList\n");
        FDebug2D(m);
    #endif
    /**********************************************************************************/

    s = MMget(m,0);
    if ( s != NIL )
    {
        O = ( PtrObjVoid ) MMstart(m,(s>>1)) ;
        L = ( PtrObjListBitmap ) MMstart(m,(O->Buffer>>1));
        OBJdelTH(m,OBJTYPLISTBITMAP,(int)L->WHandler) ;
    }


   
    /*********************** DEBUG 2D **************************************************/
    #ifdef TRACE2D
        FDebug2D(m);
        MMechostr(MSKDEBUG,"(DBG) _DSbitmapList Done\n");
    #endif
    /************************************************************************************/

    MMset(m,0,0);
    return 0 ;
}