/*****************************************************************************
* Copyright (c) 1990-98 Digi International Inc., All Rights Reserved
* 
* This software contains proprietary and confidential information of Digi
* International Inc.  By accepting transfer of this copy, Recipient agrees
* to retain this software in confidence, to prevent disclosure to others,
* and to make no use of this software other than that for which it was
* delivered.  This is an unpublished copyrighted work of Digi International
* Inc.  Except as permitted by federal law, 17 USC 117, copying is strictly
* prohibited.
* 
* Restricted Rights Legend
*
* Use, duplication, or disclosure by the Government is subject to
* restrictions set forth in sub-paragraph (c)(1)(ii) of The Rights in
* Technical Data and Computer Software clause at DFARS 252.227-7031 or
* subparagraphs (c)(1) and (2) of the Commercial Computer Software -
* Restricted Rights at 48 CFR 52.227-19, as applicable.
*
* Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
*
*****************************************************************************/


// Utils.cpp: implementation of Utils.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Utils.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////
// Get size (width, height) of bitmap.
// extern fn works for ordinary CBitmap objects.
//
CSize GetBitmapSize(CBitmap * pBitmap)
{
   BITMAP bm;
   return pBitmap->GetBitmap(&bm) ?
      CSize(bm.bmWidth, bm.bmHeight) : CSize(0,0);
}

//////////////////
// You can use this static function to draw ordinary
// CBitmaps as well as CDibs
//
BOOL DrawBitmap(CDC& dc, CBitmap* pBitmap, const CRect* rcDst, const CRect* rcSrc)
{
   ASSERT(pBitmap != NULL);

   // Compute rectangles where NULL specified
   CRect rc;
   if (!rcSrc)
   {
      // if no source rect, use whole bitmap
      rc = CRect(CPoint(0,0), GetBitmapSize(pBitmap));
      rcSrc = &rc;
   }

   if (!rcDst)
   {
      // if no destination rect, use source
      rcDst=rcSrc;
   }

   // Create memory DC
   CDC memdc;
   memdc.CreateCompatibleDC(&dc);
   CBitmap* pOldBm = memdc.SelectObject(pBitmap);

   // Blast bits from memory DC to target DC.
   // Use StretchBlt if size is different.
   //
   BOOL fSuccess = FALSE;
   if (rcDst->Size()==rcSrc->Size())
   {
      fSuccess = dc.BitBlt(rcDst->left, rcDst->top, 
         rcDst->Width(), rcDst->Height(),
         &memdc, rcSrc->left, rcSrc->top, SRCCOPY);
   }
   else
   {
      dc.SetStretchBltMode(COLORONCOLOR);
      // ::SetBrushOrgEx(dc, 0, 0, NULL);

      fSuccess = dc.StretchBlt(rcDst->left, rcDst->top, rcDst->Width(),
         rcDst->Height(), &memdc, rcSrc->left, rcSrc->top, rcSrc->Width(),
         rcSrc->Height(), SRCCOPY);
   }
   memdc.SelectObject(pOldBm);

   return fSuccess;
}
