// Copyright (c) 1993-2005 "Компания БЭСТ". Все права сохраняются.
#include "inkey.ch"
#include "s_public.ch"
#ifndef _SET_CH
#include "set.ch"
#endif

// Для совместимости
FUNCTION YesOrNo(cString, lIni, cYes, cNo, nRow,nLeft, cColor) //nLeft!!!
   cYes  := IIF (cYes == NIL, " Да  ", cYes)
   cNo   := IIF (cNo  == NIL, " Нет ", cNo)
   lIni  := IIF (lIni == NIL, 1, IIF(lIni, 1, 2))
RETURN ( NoOrYes(@cString, @lIni, {cYes, cNo}, @nRow,@nLeft, @cColor)== 1 )

FUNCTION SayAndWait (cString, nRow, lTone, lWide, lShadow)
/*
lWide   - признак наличия окаймления за рамкой
lShadow - признак наличия тени
*/
  nRow := IIF (nRow == NIL, 2, nRow)
  NoOrYes(@cString, 1, {" Продолжение - любая клавиша "}, ;
  ;// PVP
          @nRow,, COL_WAIT,, lTone, lWide, lShadow)
RETURN .T.

FUNCTION SayError (cString, nRow,YesLastKey)
If TYPE("__HiddenError")$"C,A" //z0ne 28.05.2004
  If ValType(__HiddenError)=="C"
    __HiddenError:={__HiddenError}
  endif
  If ValType(cString)=="C"
    cString:={cString}
  endif
  AEVAL(cString,{|e| AADD(__HiddenError,e) })
  RETURN .F.
endif

//  nRow := IIF (nRow == NIL, 15, nRow)
  NoOrYes(@cString, 1, {" Продолжение - любая клавиша "}, ;
          @nRow,, COL_ERROR,;
                  ,.T.)
RETURN .F.


FUNCTION NoOrYes(aString, nIni,   aMenuList, nRow, ;
                 nLeft,   cColor, cColor2, lTone, lWide, lShadow)
/*
lWide   - признак наличия окаймления за рамкой
lShadow - признак наличия тени
*/
   LOCAL nLine, ;
         nLen:=0, nSize:=0, nW, i, nMaxLen:=0, ;
         cDev := SET(_SET_DEVICE, "SCREEN"), ;
         aSetBlock :=SaveSetKey( .T. )
   LOCAL nCountDisp := DispCount(),;
         aOldButtom:= ReturnButtom(),cSave

   #ifdef __HARBOUR__

   LOCAL lSetPrinter:=set(_SET_PRINTER, .F.)
   LOCAL lSetConsole:=set(_SET_CONSOLE, .T.)

   #endif


   // PVP
   IF lWide==nil
    lWide:=.T.
   ENDIF
   IF lShadow==nil
    lShadow:=.T.
   ENDIF

   UnselectButtom( {} )

   WHILE DispCount() > 0
     DispEnd()
   ENDDO

   IF ValType(aString) == "C"
     aString := STR_TO_ARRAY(aString, 60 )
   ELSEIF ValType(aString) <> "A"
     aString := {}
   ENDIF
   nLine := LEN(aString)
   AEVAL( aString, {|x| nLen := MAX( LEN(RTRIM(x)), nLen) } )
   aMenuList := IF( ValType(aMenuList)=='A', aMenuList, {} )
   AEVAL( aMenuList, {|x|  nMaxLen += LEN(x)+2 } )
   nRow  := IIF (nRow == NIL, 15-nLine, nRow)
   nSize := MAX ( nLen+4, nMaxLen+2 )
   nLeft := IF(nLeft==NIL,40-nSize/2,nLeft)
   cColor := IF(cColor<>NIL, cColor, COL_YES_OR_NO)
   cColor2:= IF(cColor2<>NIL,cColor2,COL_YES_MENU )
   #ifdef __HARBOUR__
   cSave:=SAVESCREEN()
   #endif
   nW := NewWindow(nRow, nLeft, IF(LEN(aMenuList)<>0,5,2)+nLine, nSize, , 2, .T., lShadow, cColor, lWide)
   SETCURSOR(0)
   FOR i := 1 TO nLine
     @ If(lWide,1,0)+i, If(lWide,3,1) SAY PADC(LEFT(aString[i], nLen+2), nSize-2, " ")
   NEXT
   IF LEN(aMenuList) <> 0
     If (lTone != NIL .AND. lTone == .T.)
        TONE(300,1)
     EndIF
     i := 2+MAX( (nSize-nMaxLen+2)/2, 2 )
     AEVAL( aMenuList, {|x| CreateButtom( If(lWide,3,2)+nLine, i, x), i+=LEN(x)+2 } )
     nIni := IIF (nIni == NIL, 1, nIni)
     PaintButtom( cColor2 )
     nIni := SelectButtom( nIni )
     UnselectButtom( aOldButtom )
     If (WCLOSE() != nW)
        WSELECT(nW)
     EndIF
   ELSE
     nIni := WSELECT()
     WSELECT(nW)
   ENDIF


   RestSetKey( aSetBlock )
   If ( SETKEY(K_ESC) != NIL)
      If (LASTKEY() == K_ESC)
         EVAL ( SETKEY(K_ESC) )
      EndIF
   EndIF
   WHILE DispCount() < nCountDisp
     DispBegin()
   ENDDO
   SET(_SET_DEVICE, cDev)
   #ifdef __HARBOUR__
   RESTSCREEN(,,,,cSave)

   set(_SET_PRINTER,lSetPrinter )
   set(_SET_CONSOLE, lSetConsole)

   #endif
RETURN (nIni)


*
*Function: Busy
*Notes:
*
FUNCTION Busy (lBusy, cString, nBRow, cColor,OffBlink)
   STATIC aSaveBusy := {}
   LOCAL nW := WSELECT(), cClr := COL_REFER
   nBRow  := IIF(nBRow == NIL, 24, nBRow)
   cColor := IIF (cColor == NIL, cClr, cColor)
   cString := IIF(cString == NIL, "", cString+", ")
   WSELECT(0)
   If lBusy
      AADD(aSaveBusy,{SAVESCREEN (nBRow, 0, nBRow, 79),SETBLINK(IF(OffBlink==NIL,.T.,NIL)),nBRow})
      @ nBRow, 0 SAY TRIM(PADC(cString, 76)) COLOR IF(OffBlink==NIL,"*","")+;
      cColor
      @ nBRow, COL() SAY " ждите..."+SPACE(40) COLOR IF(OffBlink==NIL,"*","")+;
      cColor
   Elseif !EMPTY(aSaveBusy)
      RESTSCREEN (ATAIL(aSaveBusy)[3], 0, ATAIL(aSaveBusy)[3], 79, ATAIL(aSaveBusy)[1])
      SETBLINK(ATAIL(aSaveBusy)[2])
      ASIZE(aSaveBusy,LEN(aSaveBusy)-1)
   EndIF
   WSELECT(nW)
RETURN .T.
*** End of Busy()   ***********

*
*******************************
*Function: NewWindow()
*Notes: Open new window
*
FUNCTION NewWindow(nTop, nLeft, nHeight, nWidth, cTitle, nBorder, ;
                    lBurst, lShadow, cColor, lWide)
/*
lShadow - признак наличия тени
lWide   - признак наличия окаймления за рамкой
*/
   LOCAL nOldWin := WSELECT(), nBottom, nRight, n, nMax, cSym := " ", ;
         nBotWin, nRigWin

   WSELECT(0)
   nBorder := IIF(nBorder == NIL .OR. nBorder > 2 .OR. nBorder < 0, 2, nBorder)
   DO Case
      CASE nBorder == 1
         cSym := "─"
      CASE nBorder == 2
         cSym := "═"
   EndCASE
   nTop    := IIF(nTop == NIL, 0, nTop)
   nLeft   := IIF(nLeft == NIL, 0, nLeft)
   lShadow := IIF(lShadow == NIL, .F., lShadow)
   lWide   := IIF(lWide <> NIL, lWide, .F.)

   nBotWin := IIF(nHeight == NIL,MAXROW(), nHeight+nTop-IIF(lShadow, 0, 1)+ IIF(lWide, 2, 0))
   nRigWin := IIF(nWidth == NIL,MAXCOL(), nWidth+nLeft+IIF(lShadow, 1, -1)+ IIF(lWide, 4, 0))


   If (WOPEN (nTop    , ;
              nLeft   , ;
              nBotWin , ;
              nRigWin    ) < 1)
      WSELECT(nOldWin)
      RETURN -1
   EndIF
   nBottom := nBotWin - nTop  - IIF(lShadow, 1, 0) - IIF(lWide, 1, 0)
   nRight  := nRigWin - nLeft - IIF(lShadow, 2, 0) - IIF(lWide, 2, 0)
   nTop    := IIF(lWide, 1, 0)
   nLeft   := IIF(lWide, 2, 0)
   If (cColor != NIL)
      SETCOLOR(cColor)
   EndIF
#ifndef __PLATFORM__Linux
   If (lBurst == NIL .OR. lBurst == .F.)
#else
   If (.t.)
#endif
      @ 0, 0 CLEAR TO MAXROW()-IIF(lShadow, 1, 0), MAXCOL()-IIF(lShadow, 2, 0)
      @ nTop, nLeft, nBottom, nRight BOX nBorder
   Else
      nMax := MAX(MAXROW(), MAXCOL())
      For n := nMax To nMax //отрубил разворот == titov
         @ 0, 0 CLEAR TO MIN(n,MAXROW()-IIF(lShadow, 1, 0) ), MIN(n, MAXCOL()-IIF(lShadow, 2, 0))
         @ nTop, nLeft, MIN(n, nBottom), MIN(n, nRight) BOX nBorder
      Next
   EndIF

   If lShadow

      COLORWIN(1, MAXCOL()-1, MAXROW(), MAXCOL(), "8/0")
      COLORWIN(MAXROW(), 2, MAXROW(), MAXCOL(), "8/0")
   EndIF

   If (cTitle != NIL)
      @ nTop, nLeft+1 SAY CENTER(cTitle, nRight-1, cSym, .F.)
   EndIF

RETURN nOldWin

*** End of NewWindow() ********
*
FUNCTION AskString (cString, nLength, nRow, cColor, cIni)
   LOCAL nLen := LEN(TRIM(cString)), cRet, nLeft, nSize, nW, cPic, ;
         cClr := MEMVAR->COL_ASK,;
         cDev := SET(_SET_DEVICE, "SCREEN"), nCursor,GetList:={}

   nRow    := IIF (nRow == NIL, 15, nRow)
   cPic    := IIF(nLength==NIL, "@S12", "@K")
   nLength := IIF(nLength==NIL, MAX(12, nLen), nLength)
   cColor  := IIF (cColor == NIL, cClr, cColor)
   nSize   := MAX (LEN(TRIM(cString)), nLength)+1
   nLeft := 40-nSize/2
   nW := WSELECT()
   WSELECT(0)
   WOPEN (nRow, nLeft, nRow+2, nLeft+nSize, .T.)
   SETCOLOR(cColor)
   CLEAR SCREEN
   @ 0, 0, 2, nSize BOX 2
   @ 0, 1 SAY CENTER(TRIM(cString), nSize-1, "═", .F.)
   cRet := IIF(cIni == NIL, SPACE(nLength), PADR(cIni, nLength))
   nCursor := SETCURSOR(1)
   @ 1, 1 + (nSize-nLength)/2 GET cRet PICTURE cPic
   READ
   SETCURSOR(nCursor)
   WCLOSE()
   WSELECT(nW)
   SET(_SET_DEVICE, cDev)

RETURN TRIM(cRet)

