Files
Work/aladin/graph.cpp
2024-08-07 09:12:07 -04:00

200 lines
5.9 KiB
C++

#include <aladin/graph.hpp>
#include <aladin/profiledata.hpp>
#include <common/purehdc.hpp>
#include <common/resbmp.hpp>
#include <common/drawitem.hpp>
#include <jpgimg/dib24.hpp>
#include <jpgimg/jpgimg.hpp>
OwnerDrawGraph::OwnerDrawGraph(GUIWindow &parentWnd,HWND hControlWnd,UINT controlID)
: mParentWnd(parentWnd), Control(hControlWnd,controlID), mHasData(false)
{
PureDevice pureDevice(*this);
mBkGndBrush.createHatchBrush(Brush::HDiag,RGBColor(255,0,0));
mDIBitmap=new DIB24();
mDIBitmap.disposition(PointerDisposition::Delete);
mDIBitmap->create(width(),height(),pureDevice);
mDrawItemHandler.setCallback(this,&OwnerDrawGraph::drawItemHandler);
mControlColorHandler.setCallback(this,&OwnerDrawGraph::controlColorHandler);
mParentWnd.insertHandler(VectorHandler::DrawItemHandler,&mDrawItemHandler);
mParentWnd.insertHandler(VectorHandler::ControlColorHandler,&mControlColorHandler);
}
OwnerDrawGraph::~OwnerDrawGraph()
{
mParentWnd.removeHandler(VectorHandler::DrawItemHandler,&mDrawItemHandler);
mParentWnd.removeHandler(VectorHandler::ControlColorHandler,&mControlColorHandler);
}
CallbackData::ReturnType OwnerDrawGraph::drawItemHandler(CallbackData &someCallbackData)
{
DrawItem drawItem(*((LPDRAWITEMSTRUCT)someCallbackData.lParam()));
if(drawItem.controlID()!=controlID())return (CallbackData::ReturnType)TRUE;
return handleDraw(drawItem);
}
CallbackData::ReturnType OwnerDrawGraph::controlColorHandler(CallbackData &someCallbackData)
{
Control wndButton((HWND)someCallbackData.lParam(),::GetDlgCtrlID((HWND)someCallbackData.lParam()),FALSE);
if(!(*this==wndButton))return (CallbackData::ReturnType)FALSE;
PureDevice pureDevice((HDC)someCallbackData.wParam());
return handleControlColor(pureDevice,wndButton);
}
void OwnerDrawGraph::drawBitmap(const DrawItem &drawItem)
{
PureDevice displayDevice(drawItem.deviceContext());
mDIBitmap->bitBlt(displayDevice,Rect(0,0,width(),height()),Point(0,0));
}
// *************** virtuals
WORD OwnerDrawGraph::handleDraw(const DrawItem &drawItem)
{
WORD retCode(TRUE);
switch(drawItem.itemAction())
{
case ODA_DRAWENTIRE :
drawEntire(drawItem);
break;
case ODA_SELECT :
retCode=drawSelect(drawItem);
break;
case ODA_FOCUS :
drawFocus(drawItem);
break;
}
return retCode;
}
WORD OwnerDrawGraph::handleMeasureItem(MeasureItem &/*measureItem*/)
{
return TRUE;
}
LPARAM OwnerDrawGraph::handleControlColor(PureDevice &/*pureDevice*/,Control &/*wndButton*/)
{
return (CallbackData::ReturnType)mBkGndBrush.getBrush();
}
void OwnerDrawGraph::drawEntire(const DrawItem &drawItem)
{
if(drawItem.itemState()&ODS_SELECTED)drawBitmap(drawItem);
else if(drawItem.itemState()&ODS_FOCUS)drawBitmap(drawItem);
else if(drawItem.itemState()&ODS_DISABLED)drawBitmap(drawItem);
else drawBitmap(drawItem);
drawButtonText(drawItem);
drawButtonLines(drawItem);
return;
}
WORD OwnerDrawGraph::drawSelect(const DrawItem &drawItem)
{
WORD retCode(TRUE);
if(drawItem.itemState()&ODS_SELECTED)drawBitmap(drawItem);
else {drawBitmap(drawItem);retCode=FALSE;}
drawButtonText(drawItem);
drawButtonLines(drawItem);
return retCode;
}
void OwnerDrawGraph::drawFocus(const DrawItem &drawItem)
{
if(drawItem.itemState()&ODS_FOCUS)drawBitmap(drawItem);
else drawBitmap(drawItem);
drawButtonText(drawItem);
drawButtonLines(drawItem);
return;
}
void OwnerDrawGraph::drawButtonText(const DrawItem &drawItem)
{
/* TEXTMETRIC textMetric;
Rect drawRect;
String strText;
PureDevice controlDevice(drawItem.deviceContext());
windowText(strText);
if(strText.isNull())return;
controlDevice.setBkMode(PureDevice::Transparent);
controlDevice.select((GDIObj)mTextFont,TRUE);
controlDevice.setTextColor(textColor());
::GetTextMetrics(drawItem.deviceContext(),&textMetric);
drawRect=drawItem.rectItem();
controlDevice.textOut(0,(drawRect.bottom()+drawRect.top()-textMetric.tmHeight)/2,strText);
controlDevice.select((GDIObj)mTextFont,FALSE); */
}
void OwnerDrawGraph::drawButtonLines(const DrawItem &drawItem)
{
PureDevice controlDevice(drawItem.deviceContext());
controlDevice.line(Point(0,0),Point(width(),0),RGBColor(0,0,0));
controlDevice.line(Point(0,0),Point(0,height()),RGBColor(0,0,0));
controlDevice.line(Point(0,height()-1),Point(width(),height()-1),RGBColor(255,255,255));
controlDevice.line(Point(width(),height()-1),Point(width()-1,0),RGBColor(255,255,255));
}
void OwnerDrawGraph::clearData(void)
{
mDIBitmap->setBits(0);
mHasData=false;
}
void OwnerDrawGraph::setData(DiveProfile &diveProfile)
{
Block<ProfileData> &profileData=(Block<ProfileData> &)diveProfile;
int maxDepth(getMaxDepth(profileData));
double wRatio=(double)width()/(double)profileData.size();
double hRatio=((double)height()/maxDepth)-.05;
double x=wRatio;
mDIBitmap->setBits(0);
for(int index=0;index<profileData.size()-1;index++)
{
ProfileData &pData=profileData[index];
ProfileData &pNextData=profileData[index+1];
mDIBitmap->line(Point(x,height()-(int)(double)pData.getDepth()*(double)hRatio),Point(x+wRatio,height()-(int)(double)pNextData.getDepth()*(double)hRatio),RGB888(0,255,0));
x+=wRatio;
}
mHasData=true;
}
int OwnerDrawGraph::getMaxDepth(Block<ProfileData> &profileData)
{
int maxDepth(0);
for(int index=0;index<profileData.size();index++)
{
ProfileData &pData=profileData[index];
if(pData.getDepth()>maxDepth)maxDepth=pData.getDepth();
}
return maxDepth;
}
bool OwnerDrawGraph::saveBitmap(const String &strPathFileName)
{
JPGImage jpgImage;
RGB888 rgb888;
BitmapInfo &bmInfo=jpgImage.getBitmapInfo();
bmInfo.bitCount(BitmapInfo::Bit24);
bmInfo.width(mDIBitmap->width());
bmInfo.height(mDIBitmap->height());
bmInfo.colorUsed(0);
bmInfo.colorImportant(0);
jpgImage.getRGBArray().size(mDIBitmap->extent());
for(int row=0;row<mDIBitmap->height();row++)
{
for(int col=0;col<mDIBitmap->width();col++)
{
mDIBitmap->getAt(row,col,rgb888);
jpgImage.setAt(row,col,rgb888);
}
}
jpgImage.saveBitmap(strPathFileName);
return true;
}