Compile function NDW_TOOL_STATUS_GAN_EVENTS(CtrlEntId, Event, @PARAMS)
#pragma precomp SRP_PreCompiler
#window NDW_TOOL_STATUS_GAN

DECLARE SUBROUTINE Set_Property, End_Dialog, Send_Event, Set_Status, ErrMsg
DECLARE SUBROUTINE obj_Appwindow, Start_Window, Btree.Extract, Msg, obj_Tool_Log
DECLARE FUNCTION   Get_Property, Get_Status, Dialog_Box, Utility, Send_Message, Popup, Collect.IXVals, Msg
DECLARE FUNCTION   obj_Tool, SRP_Array, GaN_Services

EQU CRLF$	TO \0D0A\

EQU FONTBLUE$	TO 16711680   ;* \FF0000\
EQU FONTRED$	TO 255        ;* \0000FF\
EQU APPYELLOW$	TO 255 + (255*256) + (202*65536)

$INSERT PS_EQUATES
$INSERT POPUP_EQUATES
$INSERT MSG_EQUATES
$INSERT TOOL_EQUATES
$INSERT APPCOLORS
$INSERT LOGICAL

GoToEvent Event for CtrlEntId else
    If Index(CtrlEntID, 'TB', 1) then GoSub TBClick
end

Return EventFlow or 1

//-----------------------------------------------------------------------------
// EVENT HANDLERS
//-----------------------------------------------------------------------------

Event WINDOW.CREATE(CreateParam)

    ScreenSize = Get_Property('SYSTEM','SIZE')
    DialogSize = Get_Property(@WINDOW,'SIZE')

    wScreen = ScreenSize<3>
    hScreen = ScreenSize<4>

    xDialog = DialogSize<1>
    yDialog = DialogSize<2>
    wDialog = DialogSize<3>
    hDialog = DialogSize<4>

    DialogSize<1> = (wScreen - wDialog)/2 + wDialog/5
    DialogSize<2> = 496	+ hDialog/3					;* 50 pixels @ top + 444 pixel dialog above + 2 pixel space

    Set_Property(@WINDOW,'SIZE',DialogSize:@FM:-1)
    
    CurSize = Get_Property(@Window, "SIZE")
    MinW    = CurSize<3>
    MinH    = CurSize<4>
    MaxW    = CurSize<3>
    MaxH    = CurSize<4>
    Set_Property(@Window,"TRACKINGSIZE",MinW:@FM:MinH:@FM:MaxW:@FM:MaxH)

    GOSUB Refresh

    Set_Property(@WINDOW,'TIMER',60000:@FM:40000)

    Set_Property(@WINDOW,'VISIBLE',1)
    
End Event


Event WINDOW.TIMER()
    
    GoSub Refresh
    
end event


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Internal GoSubs
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


TBClick:
    
    CtrlEntID = Get_Property(@WINDOW,'FOCUS')
    CtrlName = FIELD(CtrlEntID,'.',2)

    CONVERT 'B' TO 'P' In CtrlEntID								;* Buttons are named 'TB':n
    ToolID = Get_Property(CtrlEntID,'@TOOLID')					;* Properties are on panels named 'TP':n

    * Check and lock

    Open "TOOL" to hTable then
        DialogWin = "DIALOG_TOOL_MODE"
        DialogKey = DialogWin:"*":ToolID
        
        Lock hTable, DialogKey then
            ModeChangeParms = Dialog_Box("DIALOG_TOOL_MODE", @WINDOW, ToolID)
            Unlock hTable, DialogKey
        end else
            Mtext = "Another user is currently updating this tool's status."
            Msg(@Window, "", "ERROR", "", "Update Tool Status" :@FM: Mtext)
            ModeChangeParms = "Cancel"
        end
    end

    * Cancel returned from dialog box

    IF ModeChangeParms = 'Cancel' THEN RETURN

    UserID	= ModeChangeParms[1,@FM]
    Reason	= ModeChangeParms[COL2()+1,@FM]
    NewMode = ModeChangeParms[COL2()+1,@FM]

    * Two users trying to update at same time leaves more than one current mode.  Take the first one.

    CurrModeDTM = XLATE('TOOL', ToolID, TOOL_CURR_MODE_KEY$, 'X')<1,1>	
    CurrModeDTM = FIELD(CurrModeDTM,'*',2)
    CurrModeDTM = OCONV(CurrModeDTM,'DT4/^S')

    Parms	 = ToolID:@RM
    Parms	:= CurrModeDTM:@RM
    Parms	:= NewMode:@RM
    Parms	:= Reason
        
    obj_Tool_Log('SetMode', Parms)

    IF Get_Status(errCode) THEN
        Errmsg(errCode)
        RETURN
    END

    GoSub Refresh
    
return


Refresh:
    
    ToolIDs = GaN_Services('GetToolList', False$)

    CleansTools		= ''
    MetrologyTools	= ''
    InspectionTools	= ''
    TransferTools	= ''
    QuartzTools		= ''
    OtherTools		= ''

    DispTools   = ''

    For Each ToolID in ToolIDs using @FM
        ToolRec		= XLATE('TOOL',ToolID,'','X')
        ShowOnWIP   = ToolRec<TOOL_SHOW_ON_WIP$>
        If ShowOnWIP EQ True$ then
            ToolType	= ToolRec<TOOL_TOOL_TYPE$>
            ToolAbbr	= ToolRec<TOOL_ABBREVIATION$>
            ToolMode	= XLATE('TOOL', ToolID, 'CURR_MODE', 'X')<1,1>
            ToolHrs		= OCONV(XLATE('TOOL', ToolID, 'CURR_MODE_HRS', 'X'), 'MD2')
            
            IF ToolRec<TOOL_ACTIVE_ESCALATION$> = '' THEN
                ToolEsc = 0 
            END ELSE
                ToolEsc = 1
                ToolHrs = XLATE('TOOL_ESC',ToolRec<TOOL_ACTIVE_ESCALATION$>,'ELAPSED_HRS','X')
                ToolHrs = OCONV(ToolHrs,'MD2')
            END
            
            BEGIN CASE
                CASE INDEXC(ToolType,'char',1)
                    SortPriority    = 1

                CASE ToolType = 'Transfer'
                    SortPriority    = 2

                CASE Otherwise$
                    SortPriority    = 3
            
            END CASE

            DispTools  := ToolID : @VM : ToolAbbr : @VM : ToolMode : @VM : ToolHrs : @VM : ToolEsc : @VM : SortPriority : @FM
            
        end

    Next ToolID

    DispTools[-1, 1]    = ''
    DispTools           = SRP_Array('SortRows', DispTools, 'AR6' : @FM : 'AL1', 'LIST', @FM, @VM)

    GreenButton		= '.\BMPS\GreenButton.png'
    RedButton		= '.\BMPS\RedButton.png'
    YellowButton	= '.\BMPS\YellowButton.png'
    GrayButton		= '.\BMPS\GrayButton.png'
    QuestionButton  = '.\BMPS\help_32.bmp'

    DefaultBackColor = Get_Property(@WINDOW,'BACKCOLOR')

    FOR I = 1 TO 60

        ToolID		= DispTools<I,1>
        ToolAbbr	= DispTools<I,2>
        ToolStatus	= DispTools<I,3>
        ToolHrs		= DispTools<I,4>
        ToolEsc		= DispTools<I,5>
        
        IF ToolID NE '' THEN
            Set_Property(@WINDOW:'.TP':I,'TEXT',ToolAbbr)
            Set_Property(@WINDOW:'.TP':I,'@TOOLID',ToolID)
            Set_Property(@WINDOW:'.TT':I,'TEXT',ToolHrs)
            Set_Property(@WINDOW:'.TT':I,'FORECOLOR',FONTBLUE$)
            Set_Property(@WINDOW:'.TB':I,'ENABLED',1)
            
            IF ToolEsc = 1 THEN
                Set_Property(@WINDOW:'.TP':I,'BACKCOLOR',APPYELLOW$)
                Set_Property(@WINDOW:'.TT':I,'BACKCOLOR',APPYELLOW$)
                Set_Property(@WINDOW:'.TB':I,'BACKCOLOR',APPYELLOW$)
                Set_Property(@WINDOW:'.TT':I,'FORECOLOR',FONTRED$)
            END ELSE
                Set_Property(@WINDOW:'.TP':I,'BACKCOLOR',DefaultBackColor)
                Set_Property(@WINDOW:'.TT':I,'BACKCOLOR',DefaultBackColor)
                Set_Property(@WINDOW:'.TB':I,'BACKCOLOR',DefaultBackColor)
                Set_Property(@WINDOW:'.TT':I,'FORECOLOR',FONTBLUE$)
            END
            
            BEGIN CASE
                CASE ToolStatus = 'PROD'
                    Set_Property(@WINDOW:'.TB':I,'BITMAP',GreenButton)
                    
                CASE ToolStatus[-3,3] = 'MTC'
                    Set_Property(@WINDOW:'.TB':I,'BITMAP',RedButton)
                
                CASE ToolStatus = 'LIM' OR ToolStatus = 'QUAL' OR ToolStatus = 'EWORK'
                    Set_Property(@WINDOW:'.TB':I,'BITMAP',YellowButton)
                    
                CASE ToolStatus = 'OUT' OR ToolStatus = 'FACILITIES (UnSched)'
                    Set_Property(@WINDOW:'.TB':I,'BITMAP',GrayButton)
                    
                CASE 1
                    Set_Property(@WINDOW:'.TB':I,'BITMAP',QuestionButton)
                
            END CASE
            
        END ELSE
            Set_Property(@WINDOW:'.TP':I,'TEXT','')
            Set_Property(@WINDOW:'.TT':I,'TEXT','')
            Set_Property(@WINDOW:'.TB':I,'ENABLED',0)
            Set_Property(@WINDOW:'.TB':I,'BITMAP','')
        END
    NEXT I

*    Set_Property(@WINDOW, 'VISIBLE', 1)
    
return

