/*************************************************/
/*                                               */
/* checkbox.c                                    */
/*                                               */
/* checkbox management                           */
/*                                               */
/*************************************************/


#include "x/scolplugin.h"
#include "x/version.h"
#include "tests.h"
#include "objects/font.h"


WNDPROC CheckProc = NULL ;

/*******************************************************************************************/
/*                                                                                         */
/*  LRESULT CALLBACK  CheckProcBis ( HWND hwnd , unsigned msg , UINT wParam , LONG lParam )*/
/*                                                                                         */
/*  Cette fonction est la winproc par default pour les check box                           */
/*                                                                                         */
/*******************************************************************************************/

LRESULT CALLBACK CheckProcBis ( HWND hwnd , unsigned msg , UINT wParam , LONG lParam )
{
    switch ( msg ) 
    {
        case WM_KEYUP:                           
            if ( wParam == 13 ) GRTestDefaultButton ( GetParent ( hwnd )) ;
             if ( wParam == 9 ) GRTestTabFocus ( hwnd , OBJTYPCHECK ) ;
            break ;
        default : break;
    }

    return CallWindowProc ( CheckProc , hwnd , msg , wParam , lParam ) ;
}



/****************************************************************************************/
/*                                                                                      */
/* HWND NewObjCheckBox ( PtrObjButton Chk , char * Name , , HWND Parent ) ;             */
/*                                                                                      */
/* Cree un nouveau check box a partir de la description contenu dans la structure       */
/* PtrObjButton                                                                         */
/*                                                                                      */
/****************************************************************************************/

HWND NewObjCheckBox ( PtrObjButton Psh , char * Name , HWND Parent , int Dialog )
{
int Flags ;
int Ex_Flag = 0 ;

        if ( Psh->Flags & CH_RADIO ) Flags = BS_AUTORADIOBUTTON ; 
        else Flags = BS_AUTOCHECKBOX ;

        if ( Psh->Flags & CH_LEFT ) Flags |= BS_LEFTTEXT ;
        if ( Parent != NULL ) Flags |= WS_CHILD ;        
        if (( Psh->Flags & CH_TABFOCUS ) && Dialog ) Flags |= WS_TABSTOP ;
        if ( Psh->Flags & CH_DOWN ) Ex_Flag |= WS_EX_CLIENTEDGE ;
         

        return CreateWindowEx ( Ex_Flag , "button" , Name , Flags ,
                                Psh->PosX , Psh->PosY ,
                                Psh->TailleW, Psh->TailleH ,
                                Parent , NULL ,
                                (HINSTANCE)SCgetExtra("this_inst") , NULL ) ;
}



/*****************************************************************************/
/*                                                                           */
/* int GRCreateCheckBox ( mmachine m )                                       */
/*                                                                           */
/*  correspond a la fonction magma Obj _create_check_box                     */
/*  (ObjParent, I PosX , IPosY , I TailleW , I TailleH , I Flags, I MH,S NOm)*/
/*  qui cree une check box carree                                            */
/*                                                                           */
/*****************************************************************************/

int GRCreateCheckBox ( mmachine m )
{
   int s , s2 , l ,  res ;
   int dialog = 0 ;
   PtrObjVoid O , OP ;
   PtrObjButton Chk ;
   PtrObjWindow PW ;
   HWND Parent , HW ;
   char Name [ 1024 ] ;

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

    /* test channel NIL */
    if (MMget(m,7)==NIL)
    {
        MMechostr(MSKDEBUG,"_CRcheckBox : channel NIL\n");
        m->pp+=7;
        return 0;
    }

    /* test parent NIL */
    if ( MMget(m,6)==NIL)
    {
        MMechostr(MSKDEBUG,"_CRcheckBox : parent NIL\n");
        m->pp+=7;
        MMset(m,0,NIL);
        return 0;
    }

   /* saisie le libelle du check box */  
   res = MMpull(m) ;
   if ( res == NIL ) strcpy ( Name  , "" ) ; else
   strcpy ( Name , ( char * ) MMstart(m, ( res >>1) + 1) ) ;

    l = ( sizeof ( struct ObjVoid ) + 3 ) >> 2 ;
    s = MMmallocCLR (m,l,TYPETAB) ;
    if ( MMpush(m,(s<<1)+1)) return MERRMEM  ;
    l = ( sizeof ( struct ObjButton ) + 3 ) >> 2 ;
    s2 = MMmalloc ( m,l,TYPEBUF ) ;
    if ( s2 == NIL ) return MERRMEM ;
    s = MMpull(m) ;

    O = ( PtrObjVoid ) MMstart(m, (s>>1) ) ;
    Chk = ( PtrObjButton ) MMstart(m, s2 ) ;
    O->Type = OBJ_TYPE_CHECK_BOX << 1 ;
    O->Buffer = ( s2 << 1 ) + 1 ;
    O->Tab = NIL ;    
    Chk->Flags = MMpull(m) >> 1 ;
    Chk->TailleH = MMpull(m) >> 1 ;
    Chk->TailleW = MMpull(m) >> 1 ;
    Chk->PosY = MMpull(m) >> 1 ;
    Chk->PosX = MMpull(m) >> 1 ;
    O->Father = MMpull(m) ;

    /* recuperation du handler window du parent, si parent il y a */
    if ( O -> Father == NIL ) Parent = NULL ;
    else
    {
        OP = ( PtrObjVoid ) MMstart(m, (O->Father>>1) ) ;
        if ( OP->Type != OBJ_TYPE_WINDOW << 1 )
        {
            MMechostr ( 1 , "CreateCheckBox : Parent must be a window\n") ;
            MMpull(m);
			return MMpush(m,NIL) ;
        }
            
        PW = ( PtrObjWindow ) MMstart(m, (OP->Buffer >> 1 ) ) ;
        if ( PW->Flags & WN_DIALOG ) dialog = 1 ;
        Parent = PW->WHandler ;
    }

    HW = NewObjCheckBox ( Chk , Name , Parent , dialog  ) ;    
    if ( ! HW )
    {
        MMechostr(MSKDEBUG,"_create_check_box : Error in creating the WIN Object\n" ) ;
        MMpull(m);
		return MMpush(m,NIL) ;
    }
    Chk->WHandler = HW ;

    if ( CheckProc == NULL ) CheckProc = ( WNDPROC ) GetWindowLong ( HW , GWL_WNDPROC ) ;
    SetWindowLong ( HW , GWL_WNDPROC , ( long ) CheckProcBis ) ;
    if ( DefaultFont != NULL ) 
      SendMessage ( HW , WM_SETFONT , ( WPARAM ) DefaultFont , ( LPARAM ) TRUE ) ;      

    ShowWindow(HW,SW_SHOW);
    UpdateWindow(HW);
    MMpush(m,s) ;
    res = OBJcreate(m,OBJTYPCHECK,(int)HW,OBJTYPWINDOW,(int)Parent) ;    

    /************************** DEBUG 2D ****************************************/
    #ifdef TRACE2D
        FDebug2D ( m ) ;
        MMechostr( 1 , "DBG _create_check_box done\n" ) ;
    #endif
    /*****************************************************************************/
        
    return res  ;   
}           

/*****************************************************************************/
/*                                                                           */
/* int GRGetCheckBox ( mmachine m ) ;                                        */
/*                                                                           */
/* correspond a la fonction magma int _get_check_box Obj                     */
/* laquelle teste l'etat d'une check box                                     */
/*                                                                           */
/*****************************************************************************/

int GRGetCheckBox ( mmachine m )
{
    PtrObjVoid O ;
    PtrObjButton P ;
    int s ;

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

    s = MMpull(m) ;
    if ( s == NIL )
    {
        MMechostr ( 1 , "GetCheckBox : CheckBox is NIL\n" ) ;
        return MMpush(m,(-1)<<1) ;
    }
    O = ( PtrObjVoid ) MMstart(m, ( s >> 1 ) ) ;
    if ( O->Type != OBJ_TYPE_CHECK_BOX << 1 )
    {
        MMechostr ( 1 , "GetCheckBox : Not a check box \n" ) ;
        return MMpush(m,(-1)<<1) ;
    }
    P = ( PtrObjButton ) MMstart(m, (O->Buffer>>1) ) ;

    s = SendMessage(P->WHandler,BM_GETCHECK,0,0) ;

    /************************* DEBUG 2D ***************************/
    #ifdef TRACE2D
            FDebug2D ( m ) ; 
            MMechostr ( 1 , "DBG _get_check_box return value %d\n" , s ) ;
    #endif
    /*****************************************************************/

    return MMpush(m,s<<1) ;
    
}

/*****************************************************************************/
/*                                                                           */
/* int GRSetCheckBox ( mmachine m )                                          */
/*                                                                           */
/* correspond a la fonction magma Obj _set_check_box Obj ( Obj , I )         */
/* laquelle modifie l'etat d'un check box                                    */
/*                                                                           */
/*****************************************************************************/

int GRSetCheckBox ( mmachine m )
{
        PtrObjVoid O ;
        PtrObjButton P ;
        int Etat , s , res ;

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


        Etat = ( MMpull(m) >> 1 ) > 0 ;
        s = MMpull(m) ;
        if ( s == NIL )
        {
            MMechostr ( 1 , "SetCheckBox : Check Box is NIL\n" ) ;
            return MMpush(m,NIL) ;
        }
        O = ( PtrObjVoid ) MMstart(m, ( s >> 1 ) ) ;
        if ( O->Type == OBJ_TYPE_CHECK_BOX << 1 )
        {
      
            P = ( PtrObjButton ) MMstart(m, (O->Buffer >> 1 ) ) ;      
            SendMessage(P->WHandler,BM_SETCHECK,Etat,0) ;
        }
        else       MMechostr ( 1 , "SetCheckBox : Not a Check box\n" ) ;
        res = MMpush(m,s) ;

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