// Copyright (c) 1993-2005 "Компания БЭСТ". Все права сохраняются.

#include "common.ch"

#include "getexit.ch"
#include "inkey.ch"

Static nClearA := 7
Static cClearB := Chr(255)

#ifdef __PLATFORM__Linux
Function Sleep()
Return NIL
#endif


Function GetClearA
Return nClearA


Function SetClearA(xAttr)
if ValType(xAttr) = 'C'
  nClearA := ColorToN(hb_ColorIndex(xAttr,0))
elseif ValType(xAttr) = 'N' .and. Val(xAttr)>=0 .and. Val(xAttr)<=255
  nClearA := xAttr
endif
return ''


Function GetClearB
Return cClearB


Function SetClearB(xAttr)
if ValType(xAttr) = 'C'
  cClearB := xAttr
elseif ValType(xAttr) = 'N'
  cClearB := Val(xAttr)
endif
return ''


Function CharWin(nTop, nLeft, nBottom, nRight, xNewChar, xOldChar)
Local cScr, cNewChar, cOldChar, ser
if nTop == nil
   nTop := 0
endif
if nLeft == nil
   nLeft := 0
endif
if nBottom == nil
   nBottom := MaxRow()
endif
if nRight == nil
   nRight := MaxCol()
endif
if xNewChar == nil
   cNewChar := GetClearB()
elseif ValType(xNewChar) = 'N'
   cNewChar := Chr(xNewChar)
else
   cNewChar := xNewChar
endif
if xOldChar # nil
   cOldChar := if(ValType(xOldChar)='N', Chr(xOldChar), xOldChar)
endif
cScr := SaveScreen(nTop, nLeft, nBottom, nRight)
for ser := 1 to len(cScr) / 2
   if cOldChar == nil .or. Substr(cScr, (ser-1)*2+1, 1) == cOldChar
      cScr := Stuff(cScr, (ser-1)*2+1, 1, cNewChar)
   endif
next
RestScreen(nTop, nLeft, nBottom, nRight, cScr)
Return ''


Function ClearWin(nTop, nLeft, nBottom, nRight, xAttr, xChar)
Local cChar, nAttr
   DEFAULT nTop TO Row()
   DEFAULT nLeft TO Col()
   DEFAULT nBottom TO MaxRow()
   DEFAULT nRight TO MaxCol()


if xChar == nil
   cChar := GetClearB()
   elseif ValType(xChar) = 'N' .and. xChar >= 0 .and. xChar <= 255
          cChar := Chr(xChar)
          else
          cChar := xChar
endif

if xAttr == nil
   nAttr := GetClearA()
   elseif ValType(xAttr) = 'C'
          nAttr := ColorToN(hb_ColorIndex(xAttr,0))
          else
          nAttr := xAttr
endif
RestScreen(nTop, nLeft, nBottom, nRight,;
Replicate(cChar + Chr(nAttr), (nRight-nLeft+1)*(nBottom-nTop+1)))
Return ''
//
Function ClearEOL(nRow, nCol, xAttr, xChar)
Local cAttr, cChar, cScr
if nRow == nil
   nRow := Row()
endif
if nCol == nil
   nCol := Col()
endif
if xAttr == nil
   cAttr := GetClearA()
elseif ValType(xAttr) = 'N'
   cAttr := Chr(xAttr)
else
   cAttr := xAttr
endif
if xChar == nil
   cChar := GetClearB()
elseif ValType(xChar) = 'N'
   cChar := Chr(xChar)
else
   cChar := xChar
endif
cScr := Replicate(cChar+cAttr, MaxCol()-nCol+1)
RestScreen(nRow, nCol, nRow, MaxCol(), cScr)
Return ''


Function ClEOL(nRow, nCol)
Local cScr
if nRow == nil
   nRow := Row()
endif
if nCol == nil
   nCol := Col()
endif
cScr := Replicate(' '+Chr(0), MaxCol()-nCol+1)
RestScreen(nRow, nCol, nRow, MaxCol(), cScr)
Return ''


Function ClearSlow(nDelay, nTop, nLeft, nBottom, nRight, xChar)
Local cChar, nt, nl, nb, nr, ser

if nTop == nil
   nTop := 0
endif
if nLeft == nil
   nLeft := 0
endif
if nBottom == nil
   nBottom := MaxRow()
endif
if nRight == nil
   nRight := MaxCol()
endif
if xChar == nil
   cChar := GetClearB()
elseif ValType(xChar) = 'N'
   cChar := Chr(xChar)
else
   cChar := xChar
endif
nt := nTop
nl := nLeft
nb := nBottom
nr := nRight
while nt < nb .or. nl < nr
   if nl < nr
      DispOutAt(nt, nl, Replicate(cChar, nr-nl+1))
      DispOutAt(nb, nl, Replicate(cChar, nr-nl+1))
   endif
   if nt < nb
      nt ++
      nb --
   endif
   if nt <= nb
      for ser := nt to nb
         DispOutAt(ser, nl, cChar)
         DispOutAt(ser, nr, cChar)
      next
   endif
   SecondsSleep( nDelay*0.001 )
   if nl < nr
      nl ++
      nr --
   endif
enddo
Return ''


Function ClWin(nTop, nLeft, nBottom, nRight)
Local cScr
if nTop == nil
   nTop := Row()
endif
if nLeft == nil
   nLeft := Col()
endif
if nBottom == nil
   nBottom := MaxRow() - 1
endif
if nRight == nil
   nRight := MaxCol() - 1
endif
cScr := Replicate(' ' + Chr(0), (nRight-nLeft+1)*(nBottom-nTop+1))
RestScreen(nTop, nLeft, nBottom, nRight, cScr)
Return ''


Function ColorWin(nTop, nLeft, nBottom, nRight, xAttr, xOld)
Local cScr, nAttr, cAttr, nOld, ser, np

if nTop == nil
   nTop := Row()
endif
if nLeft == nil
   nLeft := Col()
endif
if nBottom == nil
   nBottom := MaxRow()
endif
if nRight == nil
   nRight := MaxCol()
endif
if xAttr == nil
   nAttr := GetClearA()
elseif ValType(xAttr) = 'C'
   nAttr := ColorToN(hb_ColorIndex(xAttr,0))
else
   nAttr := xAttr
endif
if xOld <> nil
   nOld := if(ValType(xOld) = 'C', ColorToN(hb_ColorIndex(xOld,0)), xOld)
endif

cScr := SaveScreen(nTop, nLeft, nBottom, nRight)
for ser := 1 to len(cScr) / 2
   np := (ser-1)*2 + 2
   cAttr := Substr(cScr, np, 1)
   if nOld == nil .or. nOld == Asc(cAttr)
      cScr := Stuff(cScr, np, 1, Chr(nAttr))
   endif
next
RestScreen(nTop, nLeft, nBottom, nRight, cScr)
Return ''


Function ColorRepl(xNewAttr, xOldAttr)
Local cNewAttr, cOldAttr, cScr, ser, np
if xNewAttr == nil
   cNewAttr := GetClearA()
elseif ValType(xNewAttr) = 'N'
   cNewAttr := Chr(xNewAttr)
else
   cNewAttr := xNewAttr
endif
if xOldAttr # nil
   cOldAttr := if(ValType(xOldAttr)='N', Chr(xOldAttr), xOldAttr)
endif
cScr := SaveScreen(0, 0, MaxRow(), MaxCol())
for ser := 1 to len(cScr) / 2
   np := (ser - 1)*2 + 2
   if cOldAttr == nil .or. Substr(cScr, np, 1) == cOldAttr
      cScr := Stuff(cScr, np, 1, cNewAttr)
   endif
next
RestScreen(0, 0, MaxRow(), MaxCol(), cScr)
Return ''




Function SaySpread(cStr, nDelay, nRow, nCol)
Local n1 := Int(len(cStr)/2), n2
if n1 = 0
  n1 := 1
endif
n2 := n1 + 1
if nDelay == nil
  nDelay := 4
endif
if nRow == nil
  nRow := Row()
endif
if nCol == nil
  nCol := Col()
endif

nCol -= n1
SetPos(nRow, nCol)
while .t.
   if n1 > 0
      SetPos(nRow, nCol + n1)
      DispOut(Substr(cStr, n1, 1))
   endif
   if n2 <= len(cStr)
      SetPos(nRow, nCol + n2)
      DispOut(Substr(cStr, n2, 1))
   endif
   SecondsSleep( nDelay * 0.001 )
   n1 --
   n2 ++
   if n1 < 1 .and. n2 > Len(cStr)
      exit
   endif
enddo
SetPos(nRow, nCol)
Return ''


Function SayMoveIn(cStr, nDelay, nRow, nCol, lDir)
Local n1 := 1

if nDelay == nil
   nDelay := 4
endif
if lDir == nil
   lDir := .f.
endif
if nRow == nil
   nRow := Row()
endif
if nCol == nil
   nCol := Col()
endif
while n1 <= len(cStr)
   DispOutAt(nRow, if(lDir, nCol+len(cStr)-n1, nCol),;
                   iif(lDir, Left(cStr, n1), Right(cStr, n1)))
   SecondsSleep( nDelay * 0.001 )
   n1 ++
enddo
SetPos(nRow, nCol)
Return ''


Function SayDown(cStr, nDelay, nRow, nCol)
Local ser
if nDelay == nil
   nDelay := 4
endif
if nRow == nil
   nRow := Row()
endif
if nCol == nil
   nCol := Col()
endif
for ser := 1 to len(cStr)
   DispOutAt(nRow+ser-1, nCol, Substr(cStr, ser, 1))
   SecondsSleep( nDelay * 0.001 )
next
SetPos(nRow, nCol)
Return ''

#include "common.ch"

Function Random(lMode)
DEFAULT lMode TO .T.
Return if( lMode, HB_RandomInt( 0, 65535 ), HB_RandomInt( -32768, 32767 ) )

Function Rand(nStart)
if nStart <> nil
   HB_RandomSeed(nStart)
endif
Return HB_Random()
//

/***************

Function SayScreen(cStr,nRow,nCol)
Local cBuf,nPos := 1
Local nL,nR,i
nRow := if(nRow== NIL,Row(),nRow)
nL   := if(nCol== NIL,Col(),nCol)
cStr := if(cStr== NIL,'',cStr)
nR   := Min(nL+Len(cStr),MaxCol())
cBuf := SaveScreen(nRow,nL,nRow,nR)


For i := 1 To nR-nL  //+1 //Len(cStr)
    cBuf[nPos] := cStr[i]
    nPos += 2
Next




RestScreen(nRow,nL,nRow,nR, cBuf)
Return NIL

*********************/





//
static proc GetPassword( oGet )
Local nKey,nChar,cKey
Local OldCursor
  IF GetPreValidate(oGet)
     OldCursor := SetCursor(1)
     oGet:SetFocus()

    oGet:cargo := ""
    DO WHILE ( oGet:exitState == GE_NOEXIT )
      // check for initial typeout (no editable positions)
      IF ( oGet:typeOut )
        oGet:exitState := GE_ENTER
      ENDIF

      // apply keystrokes until exit
      DO WHILE ( oGet:exitState == GE_NOEXIT )
        nKey := InKeyTrap(0)
        IF nKey >= 32 .AND. nKey <= 255
          oGet:cargo += Chr(nKey)
          GetApplyKey(oGet, Asc("*"))
       	ELSEIF nKey == K_BS
          oGet:cargo := Left(oGet:cargo,Len(oGet:cargo) - 1)
          GetapplyKey(oGet, nKey)
        ELSEIF nKey == K_ENTER
          GetApplyKey(oGet, nKey)
        ELSEIF nKey == K_ESC
          GetApplyKey(oGet, nKey)
        ENDIF
      ENDDO


      IF !GetPostValidate(oGet)
        oGet:exitState := GE_NOEXIT
      ENDIF
    ENDDO
    oGet:KillFocus()
    SetCursor(OldCursor)
  ENDIF
  IF oGet:exitState != GE_ESCAPE

    oGet:varPut(Padr(oGet:cargo,Len(oGet:varGet())))
  ENDIF

RETURN

FUNCTION GetSecret( cDef,nrow,ncol,lSay,lPrompt,cPict)

   Local OldRow := Row()
   Local OldCol := Col()
   Local GetList := {}

   Local cVar   := Space( Len( cDef ) )

   DEFAULT nRow to Row(), nCol to Col(), lSay To .F.

   IF lPrompt <> Nil
      @ nRow,nCol Say lPrompt Get cVar
   ELSE
      @ nRow,nCol Get cVar
   ENDIF

   GetList[1]:reader := {|oget| GetPassword(oGet) }
   if !Empty(cPict)
      GetList[1]:picture := cPict
   endif
   READ
   SetPos(OldRow,OldCol)

RETURN cVar

FUNCTION GETINPUT(xSource, nRow, nCol, lSay, xSay, cPic, bValid, bWhen)
LOCAL nOldRow := ROW(), nOldCol := COL(), GetList[1]

  IF nRow = NIL
    nRow := nOldRow
  ENDIF
  IF nCol = NIL
    nCol := nOldCol
  ENDIF
  IF lSay = NIL
    lSay := .F.
  ENDIF

  SetPos(nRow, nCol)

  IF xSay <> NIL
    DispOut(xSay)
    nCol := COL() +1
    SetPos(nRow, nCol)
  ENDIF

  IF xSource = NIL
    RETURN( "" )
  ENDIF

  GetList[1] := _GET_(xSource, "_GETINPUT", cPic, bValid, bWhen)

  ReadModal(GetList)

  IF lSay
    SetPos(nRow, nCol)
    DispOut(xSource)
  ENDIF

  SetPos(nOldRow, nOldCol)

RETURN( xSource )
//
Function ScreenStr(nRow,nCol,nCount)
Local cBuf,i := 1,n := 1
Local nL,nR
Local cStr := ''
nRow := if(nRow== NIL,Row(),nRow)
nL   := if(nCol== NIL,Col(),nCol)
nCount := if(nCount== NIL,1,nCount)
nR   := Min(nL+nCount,MaxCol())
cBuf := SaveScreen(nRow,nL,nRow,nR)

For n := 1 To nCount
    cStr += cBuf[i]
    i += 2
Next
Return cStr


// EXPAND(<cString>, [<nNumber>],[<cChar|nChar>]) --> cExpandString
FUNCTION Expand(cString,nCount,cChar)
Local i,n
Local cStr := ''
if( cString == NIL ,cString := '', )
if( nCount  == NIL ,nCount := 1, )
if( cChar  == NIL ,cChar := ' ', )
if ValType(cChar) == 'N'
   cChar := Chr(cChar)
endif
n := Len(cString)
For i := 1 To n
    cStr += cString[i]+if(i == n,'', Replicate(cChar,nCount))
next
RETURN cStr
//
Function AppendFile(cDest,cSrc)
Return FileAppend(cSrc,cDest)
//

//FUNCTION SETRC(nRow,nCol)
//RETURN SETPOS(nRow,nCol)

FUNCTION KEYSEND(cStr)
LOCAL i,j
IF cStr==NIL
   CLEAR TYPEAHEAD
ELSEIF VALTYPE(cStr)="C"
   FOR i:=1 TO LEN(cStr)
   j:=ASC(cStr[i])
   hb_keyput(if(j>200.AND.j<256,j-256,j))
   NEXT
ELSEIF VALTYPE(cStr)="N"
   j:=cStr
   hb_keyput(if(j>200.AND.j<256,j-256,j))
ENDIF
RETURN NIL



FUNCTION KEYREAD()
RETURN LASTKEY()


#include "fileio.ch"
function StrFile( cString, cFile, lOverwrite, nOffSet, lCutOff )

   local nHandle
   local nBytes := 0

   lOverWrite := IF( lOverWrite == NIL, .F., lOverWrite )
   lCutOff := IF( lCutOff == NIL, .F., lCutOff )

   if lOverWrite .and. file( cFile )
      /* TODO: when SETSHARE()/GETSHARE() will be implemented respect
               flags returned by GETSHARE() function */
      nHandle := Fopen( cFile, FO_WRITE )
   else
      /* TODO: when SETFCREATE() will be implemented use
               flags returned by this function */
      nHandle := Fcreate( cFile )
      lCutOff := .F.
   endif
   if nHandle > 0
      if nOffSet == NIL
         Fseek( nHandle, 0, FS_END )
         lCutOff := .F.
      else
         Fseek( nHandle, nOffSet, FS_SET )
      endif
      nBytes := Fwrite( nHandle, cString, len( cString ) )
      if lCutOff
         /* FWRITE with 0 bytes trunc the file */
         Fwrite( nHandle, "", 0 )
      endif
      Fclose( nHandle )
   endif
return( nBytes )

#ifdef __PLATFORM__Linux

FUNCTION LNX_SAYSCREEN( cStr, nRow, nCol )
   LOCAL cBuf
   LOCAL nRight
   LOCAL i

   DEFAULT cStr TO ""
   DEFAULT nRow TO Row()
   DEFAULT nCol TO Col()

   IF Len( cStr ) > 0
     nRight := Min( nCol + Len( cStr ) - 1, MaxCol() )
     // cStr1 := hb_Translate( cStr,"RU866","RUKOI8" )
     cBuf := SaveScreen( nRow, nCol, nRow, nRight )
     FOR i := 1 TO nRight - nCol + 1
       // cBuf[ i * 2 - 1 ] := cStr1[ i ]
       cBuf[ i * 2 - 1 ] := Chr( HB_TRANSCHAR( Asc( cStr[i] ) ) )
     NEXT
     RestScreen( nRow, nCol, nRow, nRight, cBuf )
   ENDIF
RETURN ""

#pragma BEGINDUMP

#include "hbapi.h"
#include "hbapiitm.h"

extern char gt_Crs_TransChar( char c );

HB_FUNC( HB_TRANSCHAR )
{
   hb_retni( gt_Crs_TransChar( hb_parni(1) ) );
}

#pragma ENDDUMP

#endif

