
#include 'Gra.ch'
#include 'Xbp.ch'
 ////////////
 //
FUNCTION GraPrintScreen_(cFile, nFormat)
 //      Get the desktop screen and save it to a file, where:
 //            <@cFile>   -> full pathname of the file to be save, with its extension (a valid one to XbpBitmap())
 //           [<nFormat>] -> image format desired, defined in XBPBMP_FORMAT_... (Xbp.ch)
 //                          Default to XBPBMP_FORMAT_JPG
 //                          If not informed the file extension always will be ".jpg"
 //      Returns: .t. -> if the file was created
 //               .f. -> if the file could not be created
 //      Exapmles: if GraPrintScreen_('C:\Temp\PrtScr', XBPBMP_FORMAT_GIF)
 //                   ConfirmBox(NIL, 'Desktop screen saved in the file: C:\Temp\PrtScr.GIF')
 //                endif
 //                if GraPrintScreen_('C:\Temp\PrtScr')
 //                   ConfirmBox(NIL, 'Desktop screen saved in the file: C:\Temp\PrtScr.JPG')
 //                endif
 /////
LOCAL  oPS, oScreen, nAt, lOk
if nFormat == NIL
   nFormat := XBPBMP_FORMAT_JPG
   nAt     := RAt('.', cFile)
   if nAt   <> 0
      cFile := Left(cFile, (nAt - 1))
   endif
   cFile += '.JPG'
endif
oPS     := Appdesktop():lockPS()
oScreen := SaveAsBitMap_(oPS)
if oScreen <> NIL
   FErase(cFile)
   lOk := oScreen:saveFile(cFile, nFormat)
   oScreen:destroy()
   AppDesktop():unlockPS()
else
   lOk := .f.
endif
RETURN lOk

 ////////////
 //
FUNCTION SaveAsBitmap_(oSourcePS, aSourcePosSize, aTargetSize, nRasterOp, nCompress, lKeepRatio)
 //
 //   SaveAsBitmap_(<oSourcePS>, [<aSourcePosSize>], [<aTargetSize>],;
 //                              [<nCompress>])                      -> oBmp()
 //          <oSourcePS>      -> Presentation Space that should to be copied into XbpBitmap() object
 //                              Default SetAppwindow():presSpace()
 //         [<aSourcePosSize>]-> Start position in <oSouorcePS> to be copied in the format
 //                              {<nX>, <nY>, [<nWidth>, <nHeight>]}.
 //                              Defaults to: {0, 0, <oSourcePS>:xSize, <oSourcePS>:ySize}
 //         [<aTargetSize>]   -> Output image size in the format {nLen, nHeight}
 //                              Defaults to: <oSourcePS>:setPageSize()[01]
 //         [<nRasterOp>]     -> mode desired for to copy the image
 //                              See GraBitBlt()
 //                              Defaults to : GRA_BLT_ROP_SRCCOPY
 //         [<nCompress>]     -> compress mode.
 //                              see GraBitBlt()
 //                              Defaults to: GRA_BLT_BBO_OR
 //         [<lKeepRatio>]    -> keep the proportion of the sides of the image copied into the new
 //                              one image?
 //                              Defaults to: .t.
 //
 //   RETURNS: NIL if XbpBitmap() pbject could not to be created.
 //            Object of the class XbpBitmap() if the image could to bre copied
 //
 /////
LOCAL  nTargetWidth, nTargetHeight, oBmp, oPS, aPSSize, aPSColor

aPSSize           := oSourcePS:setPageSize()[01]
if aSourcePosSize == NIL
   aSourcePosSize := {0, 0, aPSSize[01], aPSSize[02]}
else
   ASize(aSourcePosSize, 4)
   if aSourcePosSize[03] == NIL .or.;
      aSourcePosSize[04] == NIL
      aSourcePosSize[03] := aPSSize[01]
      aSourcePosSize[04] := aPSSize[02]
   endif
endif

if aTargetSize == NIL
   aTargetSize := oSourcePS:setPageSize()[01]
endif
nTargetWidth  := aTargetSize[01]
nTargetHeight := aTargetSize[02]
GraCorrectNewSize_(aSourcePosSize[03], aSourcePosSize[04], @nTargetWidth, @nTargetHeight, lKeepRatio)

aSourcePosSize[03] -= 1                   // For to define the address
aSourcePosSize[04] -= 1

if nRasterOp == NIL
   nRasterOp := GRA_BLT_ROP_SRCCOPY
endif
if nCompress == NIL
   nCompress := GRA_BLT_BBO_OR
endif

oBmp := XbpBitmap():new():create(oSourcePS)
oBmp:make(nTargetWidth, nTargetHeight)

oPS := XbpPresSpace():new():create(oBMP, {nTargetWidth, nTargetHeight}, GRA_PU_PIXEL)
oBmp:presSpace(oPS)

aPSColor := oSourcePS:setColor()
oPS:setColor(aPSColor[01], aPSColor[02])
GraBox(oPS, {0, 0}, {(nTargetWidth - 1), (nTargetHeight - 1)}, GRA_FILL)
if !GraBitblt(oPS, oSourcePS, {0, 0, (nTargetWidth - 1), (nTargetHeight - 1)},;
                              aSourcePosSize,;
                              nRasterOp,;
                              nCompress)
   oBmp:destroy()
   oBmp := NIL
else
   oBmp:transparentClr := oBmp:getDefaultBGColor()
endif
RETURN oBmp
