/*************************************************/
/*                                               */
/* win_events.c                                  */
/*                                               */
/* window events management                      */
/*                                               */
/*************************************************/



//
// Modifications History
//
//$LB (07/02/2003) : flags debug
//




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



int SCOLFocusWindow ( mmachine m , HWND h ) 
{
    int k ;

//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nSCOLFocusWindow");
#endif
//***********************************

    k = OBJbeginreflex(m,OBJTYPWINDOW,(int)h,RFLWINDOW_FOCUS);

//	MMechostr(MSKDEBUG,"SCOLFocusWindow %d %x (%x)\n",k,h,hscol);
    if ( k==0) return OBJcallreflex(m,0);

    if ( k<0) return 0;

//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nSCOLFocusWindow end");
#endif
//***********************************

    return 1 ;
}



int SCOLKillFocusWindow ( mmachine m , HWND h ) 
{
    int k ;

//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nSCOLKillFocusWindow");
#endif
//***********************************


    k = OBJbeginreflex(m,OBJTYPWINDOW,(int)h,RFLWINDOW_KILLFOCUS);
//	MMechostr(MSKDEBUG,"SCOLKillFocusWindow %d %x (%x)\n",k,h,hscol);
    if ( k==0) return OBJcallreflex(m,0);
    if ( k<0) return 0;


//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nSCOLKillFocusWindow end");
#endif
//***********************************

    return 1 ;
}




/**********************************************************************************************/
/*                                                                                            */
/*  LRESULT CALLBACK MDIProcBis ( HWND hwnd , unsigned msg , UINT wParam , LONG lParam ) ;    */
/*                                                                                            */
/*  Winproc pour les fenetres filles mdi                                                      */
/*                                                                                            */
/**********************************************************************************************/
/*
LRESULT CALLBACK MDIProcBis ( HWND hwnd , unsigned msg , UINT wParam , LONG lParam )
{
    switch ( msg )
    {
        case WM_MOVE : MMechostr ( 1 , "Test MOVEn" ) ;
            break ;

        case WM_SETFOCUS : MMechostr ( 1 , "Test FOCUS\n" ) ;
            break ;
        case WM_MDIACTIVATE : MMechostr ( 1 , "Test Activate\n" ) ;
            break ;

    }

    return CallWindowProc ( DefMDIChildProc , hwnd , msg , wParam , lParam ) ;
}
*/


BOOL CALLBACK MyDlgProc( HWND hwnddlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    extern int SCOLDestroyEvent(mmachine m,HWND h) ;
    extern int SCOLCommandEvent(mmachine m,HWND Parent,int Code,int Control,HWND Child) ;
    extern int SCOLKeyDownEvent(mmachine m,HWND h, int scancode, int KeyData) ;
    extern int SCOLHandleNotifyMessage ( mmachine m , NMHDR * p ) ;
    extern int convertvirtcode(int c) ;
    extern int SCOLKeyUpEvent(mmachine m, HWND h, int Key) ;
    extern void GRTestDefaultButton( HWND h ) ;
    extern void GRTestTextTransparent ( HWND h , HDC hdcedit) ;
    extern int TESTCURSORMOVE ;
    extern int mouse_x ;
    extern int mouse_y ;
    POINT P ;
    int c , k ;
    NMHDR * pnmh ;


//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nMyDlgProc");
#endif
//***********************************

    //if (mscol.tape) MMpush(&mscol,MMgetglobal(&mscol,OFFSCCUR));
    
    switch ( uMsg )
    {
         case WM_DROPFILES : SCOLDropFiles (mm,hwnddlg,(HANDLE)wParam,OBJTYPWINDOW,RFLWINDOW_DROPFILE);
               break; 


        case WM_NOTIFY : pnmh = (LPNMHDR) lParam ;
            SCOLHandleNotifyMessage (mm, pnmh ) ;
            break ;

        //if (mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE;

       case WM_INITDIALOG : //if (mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol)); 
           return TRUE  ;

       case WM_DESTROY:
       
       SCOLDestroyEvent(mm,hwnddlg);
       tstscoldead(102);
       //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
       return TRUE ;

       case WM_COMMAND :
       
        if ( lParam == 0 && (LOWORD(wParam))==2)
        {
            SCOLDestroyEvent(mm,hwnddlg);
            DestroyWindow (hwnddlg);

            //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
            return FALSE;
        } else
        {
            SCOLCommandEvent (mm,hwnddlg,HIWORD(wParam),LOWORD(wParam),(HWND)lParam);              
            tstscoldead(103);       
        }
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;

       case WM_CHAR:
     
        SCOLKeyDownEvent(mm,hwnddlg,(lParam>>16)&0x1ff,wParam);
        tstscoldead(104);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;

    case WM_KEYDOWN:
    
        c=convertvirtcode((int)wParam);
        if (c==0) break;
        SCOLKeyDownEvent (mm,hwnddlg,(lParam>>16)&0x1ff,c);
        tstscoldead(105);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;

    case WM_KEYUP:
        
        SCOLKeyUpEvent(mm,hwnddlg,(lParam>>16)&0x1ff);
         if ( wParam == 13 ) GRTestDefaultButton ( hwnddlg ) ;
        tstscoldead(106);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;

    case WM_SETFOCUS :
        SCOLFocusWindow (mm , ( HWND ) hwnddlg ) ;
        tstscoldead(115) ;
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return FALSE ;

    case WM_KILLFOCUS :
         SCOLKillFocusWindow (mm , ( HWND ) hwnddlg ) ;
         tstscoldead(116) ;
         //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
         return FALSE  ;

    case WM_CTLCOLOREDIT: 
              
        /* on regarde si l'objet texte possede le flag ET_TRANSPARENT ou non */
          GRTestTextTransparent ( (HWND) lParam , (HDC) wParam ) ;
          //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return FALSE ;

 case WM_LBUTTONUP :       
		ReleaseCapture();
        k=1; if ( wParam & MK_RBUTTON ) k +=2;
        SCOLEventUnClickMouse(mm,hwnddlg,LOWORD(lParam),HIWORD(lParam),k,OBJTYPWINDOW,RFLWINDOW_UNCLICK);
        tstscoldead(107);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;

    case WM_RBUTTONUP :

        k=2; if ( wParam & MK_LBUTTON ) k += 1 ;
        SCOLEventUnClickMouse(mm,hwnddlg,LOWORD(lParam),HIWORD(lParam),k,OBJTYPWINDOW,RFLWINDOW_UNCLICK);
        tstscoldead(108);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;

    case WM_LBUTTONDOWN :
 
		SetCapture(hwnddlg);
        k=1; if ( wParam & MK_RBUTTON ) k +=2;
        SCOLEventClickMouse(mm,hwnddlg,LOWORD(lParam),HIWORD(lParam),k,OBJTYPWINDOW,RFLWINDOW_CLICK);
        tstscoldead(109);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;

    case WM_RBUTTONDOWN :
  
        k=2; if ( wParam & MK_LBUTTON ) k += 1 ;
        SCOLEventClickMouse(mm,hwnddlg,LOWORD(lParam),HIWORD(lParam),k,OBJTYPWINDOW,RFLWINDOW_CLICK);
        tstscoldead(110);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;    

    case WM_MOUSEMOVE :
        if ( TESTCURSORMOVE )
        {
            TESTCURSORMOVE = 0 ;
            //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
            return 0 ;
        }
        GetCursorPos ( &P ) ;
        if ( P.x == mouse_x && P.y == mouse_y ) break ;
        mouse_x = P.x ; mouse_y = P.y ;        
        k=wParam&(MK_CONTROL|MK_LBUTTON|MK_MBUTTON|MK_RBUTTON|MK_SHIFT);
		SCOLEventMoveMouse(mm,hwnddlg,LOWORD(lParam),HIWORD(lParam),k);
        tstscoldead(111);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return TRUE ;

    case WM_LBUTTONDBLCLK : 
     
        SetCapture(hwnddlg);
        k=1;
        SCOLEventDClickMouse(mm,hwnddlg,LOWORD(lParam),HIWORD(lParam),k);
        return TRUE ;

    case WM_RBUTTONDBLCLK :
     
        SetCapture(hwnddlg);
        k=2 ;
        SCOLEventDClickMouse (mm,hwnddlg,LOWORD(lParam),HIWORD(lParam),k);
        return TRUE ;

    case WM_PAINT:


//$LDEBUG
		MMechostr(MSKDEBUG, "\nDlgProc handler %d\n", (int)hwnddlg);

        SCOLPaintEvent(mm,hwnddlg);
        tstscoldead(112);
        //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));
        return FALSE ;
        
        
    case WM_MOVE :       
        SCOLEventMoveWindow ( mm,hwnddlg,LOWORD(lParam),HIWORD(lParam)) ;
        tstscoldead(113) ;
        break ;

    case WM_SIZE :        
        SCOLEventSizeWindow ( mm,hwnddlg,LOWORD(lParam),HIWORD(lParam )) ;
        tstscoldead(114) ;        
        break ;
    }
    //if(mscol.tape) MMsetglobal(&mscol,OFFSCCUR,MMpull(&mscol));

//***********************************
#if DEBUG_LIB2DOS
MMechostr (0, "\nMyDlgProc end");
#endif
//***********************************

    return FALSE ; 
}



