/*******************************************************/
/*                                                     */
/* header.c                                            */
/*                                                     */
/*******************************************************/


#include "x/scolplugin.h"
#include "x/version.h"

#include "objects/font.h"



/****************************************************************************/ 
/*                                                                          */ 
/*  HWND NewHeader ( PtrObjListTab L, HAWND Parent ) ;                      */ 
/*  cree un nouveau control header                                          */ 
/*                                                                          */ 
/****************************************************************************/ 

HWND NewHeader ( PtrObjListTab L , HWND Parent ) 
{
    int Flag , FlagEx ;

    Flag = WS_CHILD|HDS_BUTTONS ;
    FlagEx = 0 ;


    return CreateWindowEx ( FlagEx , WC_HEADER , "" , Flag ,
        L->PosX , L->PosY , L->TailleW , L->TailleH , Parent , ( HMENU ) 0 ,
        (HINSTANCE)SCgetExtra("this_inst") , NULL ) ;
}




/****************************************************************************/ 
/*                                                                          */ 
/*  int GRCreateHeader ( mmachine m ) ;                                     */ 
/*                                                                          */ 
/*  correspond a la fonction scol _CRheader (Chn,ObjWin,I,I,I,I,I) ->ObjHead*/ 
/*                                                                          */ 
/****************************************************************************/ 

int GRCreateHeader ( mmachine m ) 
{
int x,y,w,h,f,s,l,s2,res ;
    PtrObjVoid O ;
    PtrObjWindow W ;
    PtrObjListTab T ;
    HWND Parent ; 

    /************************ DEBUG 2D ******************************************************************/
    #ifdef TRACE2D
        MMechostr (1,"(DBG) _CRheader\n");
        FDebug2D(m);
    #endif
    /****************************************************************************************************/

    f = MMpull(m)>>1;
    h = MMpull(m)>>1;
    w = MMpull(m)>>1;
    y = MMpull(m)>>1;
    x = MMpull(m)>>1;

    s = MMpull(m) ;
    if ( s == NIL )
    {
        MMechostr (1,"(ERROR) _CRheader : Parent is NIL\n" );
        MMpull(m); /* depile channel */ 
        return MMpush(m,NIL);
    }
    O = ( PtrObjVoid ) MMstart(m,(s>>1)) ;
    W = ( PtrObjWindow ) MMstart(m,(O->Buffer>>1)) ;
    Parent = W->WHandler ;

    /* 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,"(ERROR) _CRlistTab : 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 ObjListTab ) + 3 ) >> 2 ;
    s2 = MMmalloc ( m,l,TYPEBUF ) ;
    if ( s2 == NIL )
    {
        MMechostr(MSKDEBUG,"(ERROR) _CRlistTab : Error in alloc MAGMA Memory\n" ) ;
        return MERRMEM ;
    }


    T = ( PtrObjListTab ) MMstart(m, s2 ) ;
    s = MMpull (m ) ; /* recupere l'addresse de l'objet de base */
    O = ( PtrObjVoid ) MMstart(m, (s>>1) ) ;

    O -> Type             = OBJ_TYPE_HEADER << 1  ;        
    O->Buffer = (s2<<1)+1 ;
    O->Tab = NIL ;
    T -> Flags          = f ;
    T -> TailleH        = h ;
    T -> TailleW        = w ;
    T -> PosY           = y ;
    T -> PosX           = x ;    

    T->WHandler = NewHeader ( T ,  Parent ) ;
    if ( !T->WHandler )
    {

        MMechostr (1,"(ERROR) _CRheader : Error in creating the WIN95 Object - Check the values\n" ) ;
        MMset(m,0,NIL) ;
        return 0 ;
    }
    if ( DefaultFont != NULL ) 
      SendMessage ( T->WHandler , WM_SETFONT , ( WPARAM ) DefaultFont , ( LPARAM ) TRUE ) ;      

    ShowWindow( T->WHandler, SW_SHOW) ;
    UpdateWindow(T->WHandler );
    /*ListView_SetCallbackMask (T->WHandler,0);*/
/*
    if ( ListTabProc == NULL ) ListTabProc = ( WNDPROC ) GetWindowLong ( T->WHandler , GWL_WNDPROC ) ;
    SetWindowLong ( T->WHandler , GWL_WNDPROC , ( long ) ListTabProcBis ) ;
 */
    MMpush(m,s) ;
    res = OBJcreate(m,OBJTYPHEADER,(int)T->WHandler,OBJTYPWINDOW,(int)Parent) ;

       

    /******************************** DEBUG 2D *********************************************************/
    #ifdef TRACE2D
        FDebug2D(m);
        MMechostr(MSKDEBUG,"(CBG) _CRheader Done\n");
    #endif
    /***************************************************************************************************/

    return res ;
}

/****************************************************************************/ 
/*                                                                          */ 
/*      int GRAddHeaderColumn ( mmachine m ) ;                              */ 
/*                                                                          */ 
/*      correspond a la fonction scol                                       */ 
/*  ObjHead _ADDheaderColumn(ObjHead,I,I,S);                                */ 
/*                                                                          */ 
/****************************************************************************/ 

int GRAddHeaderColumn ( mmachine m )
{
    int s , w , flag , i;
    char * name ; 
    PtrObjVoid O ;
    PtrObjListTab L ;
    HD_ITEM Hd ;

    /***************** DEBUG 2D *********************************************/ 
    #ifdef TRACE2D
        MMechostr(MSKDEBUG,"(DBG _ADDheaderColumn\n");
        FDebug2D(m);
    #endif
    /************************************************************************/ 

    s = MMpull(m);
    if ( s == NIL ) name = NULL ;
    else name = ( char * ) MMstart(m,(s>>1)+1) ;
    
    flag = MMpull(m)>>1 ;
    w = MMpull(m)>> 1 ;
    i = MMpull(m)>>1 ;
    s = MMpull(m) ;
    if ( s != NIL )
    {
        O = ( PtrObjVoid ) MMstart(m,(s>>1)) ;
        L = ( PtrObjListTab ) MMstart(m,(O->Buffer>>1)) ;
        Hd.mask = HDI_TEXT | HDI_WIDTH | HDI_FORMAT ;
        Hd.pszText = name ;
        Hd.cchTextMax = strlen ( name ) ;
        Hd.cxy = w ;
        Hd.fmt = HDF_STRING ;
        SendMessage ( L->WHandler , HDM_INSERTITEM , i , (LPARAM ) &Hd ) ;
        UpdateWindow ( L->WHandler ) ;
    }

    s = MMpush(m,s) ;

    /****************** DEBUG 2D ********************************************/ 
    #ifdef TRACE2D
        FDebug2D(m);
        MMechostr(MSKDEBUG,"(DBG) _ADDheaderColumn done\n");
    #endif
    /************************************************************************/ 

    return s ;
   
}