/*****************************************************************************
* 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
*
*****************************************************************************/


// EQMBitmap.cpp: implementation of the CEQMBitmap class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EQMBitmap.h"


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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#define EQMBMP_BORDER_WIDTH   (10)
#define EQMBMP_AXIS_WIDTH     (1)

#define EQMBMP_X_MAX          (1024)
#define EQMBMP_Y_MAX          (1024)

#define EQMBMP_WIDTH    ((EQMBMP_BORDER_WIDTH * 2) + EQMBMP_X_MAX)
#define EQMBMP_HEIGHT   ((EQMBMP_BORDER_WIDTH * 2) + EQMBMP_Y_MAX)

#define EQMBMP_TRANS_X(x)  ((x) + (EQMBMP_X_MAX / 2))     // should translate a signed point into
#define EQMBMP_TRANS_Y(y)  ((y) + (EQMBMP_Y_MAX / 2))     // our unsigned coord. system with (0, 0)
                                                          // in the upper left corner

CEQMBitmap::CEQMBitmap() : CBitmap()
{
   // Setup our DC
   m_dc.CreateCompatibleDC(NULL);

   // creat ourselves
   int cBitsPerPixel = m_dc.GetDeviceCaps(BITSPIXEL);
   TRACE("Creating a bitmap with %d bits/pixel (%d colors)\n", cBitsPerPixel, (1 << cBitsPerPixel));
   ASSERT(cBitsPerPixel > 0);
   VERIFY(CreateBitmap(EQMBMP_WIDTH, EQMBMP_HEIGHT, 1, cBitsPerPixel, NULL));

   // now draw
   Clear();
}

CEQMBitmap::~CEQMBitmap()
{
}

void CEQMBitmap::PlotPoint(CPoint pt)
{
   CPoint ptTranslated(EQMBMP_TRANS_X(pt.x), EQMBMP_TRANS_Y(pt.y));  // this moves us into our positive coor. system

   TRACE("PlotPoint(%d, %d) has been translated to (%d, %d)\n", pt.x, pt.y, ptTranslated.x, ptTranslated.y);

   if ((ptTranslated.x < 0) || (ptTranslated.x > EQMBMP_X_MAX) ||
       (ptTranslated.y < 0) || (ptTranslated.y > EQMBMP_Y_MAX))
   {
      TRACE("Throwing out point (%d, %d)!\n", ptTranslated.x, ptTranslated.y);
   }
   else
   {
      ptTranslated.y = EQMBMP_Y_MAX - ptTranslated.y;

      ptTranslated.x += EQMBMP_BORDER_WIDTH;
      ptTranslated.y += EQMBMP_BORDER_WIDTH;

      TRACE("  and actually plotted at (%d, %d) which is %s\n", ptTranslated.x, ptTranslated.y, m_dc.PtVisible(ptTranslated) ? "visible" : "clipped");

      CBitmap * pOldBmp = m_dc.SelectObject(this);
      m_dc.SetPixelV(ptTranslated, RGB(255, 255, 255));
      m_dc.SelectObject(pOldBmp);
   }
}

void CEQMBitmap::Clear()
{
   CBitmap * pOldBmp = m_dc.SelectObject(this);
   m_dc.FillSolidRect(CRect(CPoint(0, 0), CSize(EQMBMP_WIDTH, EQMBMP_HEIGHT)), RGB(0, 0, 0));
   m_dc.SelectObject(pOldBmp);

   DrawAxes();
}

void CEQMBitmap::DrawAxes()
{
   CPen pen(PS_SOLID, EQMBMP_AXIS_WIDTH, RGB(128, 128, 128));

   CPen * pOldPen = m_dc.SelectObject(&pen);
   CBitmap * pOldBmp = m_dc.SelectObject(this);

   CPoint apts[2];

   // draw x axis first
   apts[0].x = EQMBMP_BORDER_WIDTH;
   apts[1].x = EQMBMP_WIDTH - EQMBMP_BORDER_WIDTH;
   apts[0].y = apts[1].y = EQMBMP_HEIGHT / 2;
   m_dc.Polyline(apts, 2);

   // now y
   apts[0].y = EQMBMP_BORDER_WIDTH;
   apts[1].y = EQMBMP_HEIGHT - EQMBMP_BORDER_WIDTH;
   apts[0].x = apts[1].x = EQMBMP_WIDTH / 2;
   m_dc.Polyline(apts, 2);

   m_dc.SelectObject(pOldPen);
   m_dc.SelectObject(pOldBmp);
}
