/***********
*   MainMnu() -> nChoice          Модифицирована Богдановым А.Ю. 01-04-93
*   Стандартная процедура меню
*   с заголовком и подсказкой
***/
// Copyright (c) 1993-2005 "Компания БЭСТ". Все права сохраняются.


#include "inkey.ch"
#include "s_public.ch"

STATIC cLine2 := ''


FUNCTION MainMnu(cTitle,nTop,nLeft,aArray,aArray_M,nChoice,cFrame,cColor,aMenuBlock,bEsc,aMenuRec,aBlock)
   PLOCAL Old := SetColor(), Old_Cursor := SetCursor(0),nLastKey
   PLOCAL cOldScreen, oError, oOldError,lAllow, nNew
   PLOCAL nLen := 0  , i := 1, nBottom, nRight, aColor_, lDone := .F.
   PLOCAL lNoInit := !EMPTY(aBlock)
   PLOCAL lNoStek := aBlock != NIL
   IF !lNoInit
     cTitle  := IF(Empty(cTitle).or.cTitle == NIL, "", cTitle)
     cColor  := IF(cColor == NIL,"N/W,B/W,N/W,+W/N,+W/W", cColor)
     aColor_ := LineToArray(cColor, ',')
     nChoice := IF(nChoice = 0.or.nChoice == NIL, nChoice = 1, nChoice)
     cFrame  := IF( Empty(cFrame).or.cFrame == NIL, "╔═╗║╝═╚║ ", cFrame )
     AEVAL( aArray, {||nLen := IF(Len(aArray[i]) > nLen, Len(aArray[i]), nLen) , i++})
     nRight  := nLeft + nLen + 3
     IF (nTop + Len( aArray ) + 3 ) > 22
       nTop := MAX( nTop-2, 25-nTop - Len( aArray )   )
       if (nTop + Len( aArray ) + 3 ) > 22
         nTop := MAX( 3, 19-Len( aArray ))
       endif
     ENDIF
     nBottom := if(( nTop + Len( aArray ) + 3 ) > 22, 22, nTop+Len(aArray) + 3)
     aMenuBlock := IF(aMenuBlock == NIL .OR. LEN(aMenuBlock) == LEN(aArray), aMenuBlock, NIL)
     cOldScreen := SAVESCREEN(nTop,nLeft,nBottom+1,nRight+2)
     EVAL( {|| nNew:=GetAnalize( GlobalTask ), EVAL({||nNew := nNew[1],;
               IF( nNew==7, ERRORLEVEL(nNew),)}) } )

     DISPBEGIN()
//     #ifndef __HARBOUR__  gt_wvt
     SetColor('N/W')
//     #else
//     SetColor('N/G*')
//     #endif
     ShadowBox( '',nTop,nLeft,nBottom,nRight)
     @ nTop+2, nLeft+1 TO nTop+2, nRight-1
     @ nTop+2, nLeft  SAY chr(199)
     @ nTop+2, nRight SAY chr(182)

//     #ifndef __HARBOUR__   gt_wvt
     SetColor('B/W')
//     #else
//     SetColor('B/G*')
//     #endif
     Title( nTop+1, cTitle, nLeft+1, nRight-1)
     DISPEND()
   ELSE
     lDone      := IF(VALTYPE(aBlock[2])=="L", aBlock[2], .F.)
     Old        := aBlock[3]
     Old_Cursor := aBlock[4]
     nLastKey   := aBlock[5]
     cOldScreen := aBlock[6]
     oError     := aBlock[7]
     oOldError  := aBlock[8]
     lAllow     := aBlock[9]
     nNew       := aBlock[10]
     nLen       := aBlock[11]
     i          := aBlock[12]
     nBottom    := aBlock[13]
     nRight     := aBlock[14]
     aColor_    := aBlock[15]
     cTitle     := aBlock[16]
     nTop       := aBlock[17]
     nLeft      := aBlock[18]
     aArray     := aBlock[19]
     aArray_M   := aBlock[20]
     nChoice    := aBlock[21]
     cFrame     := aBlock[22]
     cColor     := aBlock[23]
     aMenuBlock := aBlock[24]
     bEsc       := aBlock[25]
     aMenuRec   := aBlock[26]
   ENDIF
   aBlock := NIL

   DO WHILE !lDone
    lAllow:=.T.
    DISPBEGIN()
//     #ifndef __HARBOUR__ gt_wvt
     SetColor('N/W')
//     #else
//     SetColor('N/G*')
//     #endif

    i:=1
    If aArray_M == NIL
      Aeval(aArray, {|| Prompt_(nTop+2+i, nLeft+2, aArray[i],,nLen) , i++})
    Else
      Aeval(aArray, {|| Prompt_(nTop+2+i, nLeft+2, aArray[i], aArray_M[i], nLen) , i++})
    Endif

//     #ifndef __HARBOUR__   gt_wvt
     SetColor( 'n/w,w+/n,,,n/w' )
//     #else
//     SetColor( 'n/g*,w+/n,,,n/w' )
//     #endif




    DISPEND()
    SETKEY( K_PGUP, {|| .T. } )
    SETKEY( K_PGDN, {|| .T. } )
    MENU TO nChoice
    SETKEY( K_PGUP, NIL )
    SETKEY( K_PGDN, NIL )
    nLastKey:=LASTKEY()
    IF nNew == 7
       QUIT
    ENDIF
    IF aMenuBlock <> NIL  .AND. nChoice <> 0
       IF SELECT('CARGO')>0.AND.aMenuRec != NIL.AND.nChoice<=LEN(aMenuRec)  //Титов 7.02.95
          Cargo->(DBGOTO(aMenuRec[nChoice]))
          lAllow:=Parol(Cargo->Cargo)
       ENDIF
       IF lAllow
         IF lNoStek
           aBlock := {aMenuBlock[nChoice],lDone,Old,Old_Cursor,nLastKey,cOldScreen,oError,oOldError,lAllow,nNew,nLen,i,nBottom,nRight,aColor_,cTitle,nTop,nLeft,aArray,aArray_M,nChoice,cFrame,cColor,aMenuBlock,bEsc,aMenuRec}
           EXIT
         ELSE
           lDone := EVAL(IF(ValType(aMenuBlock[nChoice]) = "B",aMenuBlock[nChoice],&(aMenuBlock[nChoice])))
         ENDIF
       ENDIF

    ELSEIF bEsc <> NIL .AND. VALTYPE(bEsc) == "B"
         lDone := EVAL(bEsc)
    ELSE
      lDone := .T.
    ENDIF
    lDone := IF(VALTYPE(lDone)=="L", lDone, .F.)
   ENDDO
   IF aBlock = NIL
     SetColor( Old )
     SetCursor( Old_Cursor )
     IF aMenuBlock <> NIL
       RESTSCREEN(nTop,nLeft,nBottom+1,nRight+2,cOldScreen)
     ENDIF
     IF nLen == 7
       QUIT
     ENDIF
     SETLASTKEY(nLastKey)
   ENDIF
RETURN nChoice

STATIC FUNCTION Prompt_(nRow, nCol, cMenu, cMessage, nLen)
   PRIVATE bBlock_Mess := {|| T_Message(cMessage, "N/W" ) }
   if cMessage == NIL
      @ nRow, nCol PROMPT PADR(cMenu,nLen)
   else
      @ nRow, nCol PROMPT PADR(cMenu,nLen) MESSAGE bBlock_Mess
   endif
RETURN NIL

FUNCTION T_Message( cMess, cColor)
@ MAXROW(),0 SAY CENTER( cMess, MAXCOL()+1, " ", .T.) COLOR cColor
Return ''

FUNCTION Title(nRow, cString, nLeft, nRight, Frame, cColor)
   PLOCAL nFrame , Old := SetColor()
   nLeft  := Iif(nLeft  = NIL, 0, nLeft)
   nRight := Iif(nRight = NIL, MaxCol(), nRight)
   Frame  := Iif(Frame  = NIL, " ", Frame)
   nFrame := int((nRight - nLeft - 1 - Len( cString )) / 2)
	 cColor := If(cColor==NIL, SetColor(), cColor)
   @ nRow, nLeft+1  SAY  Replicate(Frame,nFrame) + cString +;
   Replicate(Frame,nFrame) COLOR cColor
	 SetColor(Old)
RETURN ''

FUNCTION LineToArray(cString,cKey,aPos)
   PLOCAL   aArray := {}, i,j:=1,k
   aPos:={}
   cKey := Iif(cKey == NIL, ";", cKey)
   DO WHILE ( (i := At(cKey, cString)) > 0 )
      Aadd(aArray, Left(cString, i-1))
      k:=AT(cKey,cString)-1
      AADD(aPos,{j,k})
      cString := LTrim(SubStr(cString,i+1))
      j+=k+1
   ENDDO
   Aadd(aArray, cString)
   AADD(aPos,{j,LEN(cString)})
RETURN aArray


FUNCTION Parol()
RETURN .T.

PROCEDURE IsToSim( )
   PLOCAL cLine1, nVar,cVar,;
         cSot:='', cSec:='',cHours:='', cAllDay:='',;
         aSetKeys := SaveSetKey()
   cSot    := RIGHT(STR(TIMETOSEC(),10,2),2)
   cSec    := SUBSTR( TIME(), 7, 2 )
   cHours  := SUBSTR( TIME(), 1, 2 )
   cAllDay := PADL(LTRIM(STR( LASTDAYOM(DATE())-DAY(DATE()), 2 )),2,'0')

 cLine1 := ALLTRIM(NTOC( VAL( LEFT(cSec,1)+;
                              RIGHT(cAllDay,1)+;
                              RIGHT(cHours,1)+;
                              LEFT(cSot,1)+;
                              LEFT(cAllDay,1)+;
                              RIGHT(cSec,1)+;
                              RIGHT(cSot,1)+;
                             LEFT(cHours,1)), 36 ))

 cLine2 := ALLTRIM(NTOC(VAL(RIGHT(cSec,1)+LEFT(cAllDay,1)+;
                            LEFT(cHours,1)+RIGHT(cSot,1)+;
                            RIGHT(cAllDay,1)+LEFT(cSec,1)+;
                            LEFT(cSot,1)+RIGHT(cHours,1)),16) )

 cSot:=''; cSec:='';cHours:=''; cAllDay:=''
 ALERT( 'Код доступа: "'+LOWER(cLine1)+'"' )
 RestSetKey(aSetKeys)
RETURN

FUNCTION MySystem( cLine1 )
//  SAYSCREEN( cLine1+' ; '+cLine2, 0, 0 )
RETURN ( !EMPTY(cLine2).AND.UPPER(ALLTRIM(cLine1)) == cLine2 )




