// DemographicsView.cpp : implementation of the CDemographicsView class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resourcesp.h"
#include "DemographicsView.h"
#define DEMO_LICENSE _T("extendeddemo")
#define DIST_THRESHOLD 0.000025
CDemographicsView::CDemographicsView()
{
::InitializeCriticalSection(&m_cs);
memset(&m_info, 0, sizeof(DemographixQuery::SummaryInformation));
memset(&m_gpsDevice, 0, sizeof(GPS_DEVICE));
memset(&m_gpsPosition, 0, sizeof(GPS_POSITION));
m_hBackground = AtlLoadBitmap(IDB_BACKGROUND);
CFontHandle font = AtlGetDefaultGuiFont();
LOGFONT lf;
font.GetLogFont(&lf);
lf.lfHeight = -::MulDiv(10, ::GetDeviceCaps(NULL, LOGPIXELSY), 72);
m_DefaultFont.CreateFontIndirect(&lf);
lf.lfWeight = FW_BOLD;
m_DefaultFontBold.CreateFontIndirect(&lf);
lf.lfWeight = FW_NORMAL;
lf.lfHeight = -::MulDiv(9, ::GetDeviceCaps(NULL, LOGPIXELSY), 72);
m_SmallFont.CreateFontIndirect(&lf);
lf.lfWeight = FW_BOLD;
m_SmallFontBold.CreateFontIndirect(&lf);
m_oldLat = 0;
m_oldLong = 0;
}
CDemographicsView::~CDemographicsView()
{
::DeleteCriticalSection(&m_cs);
}
BOOL CDemographicsView::PreTranslateMessage(MSG* pMsg)
{
pMsg;
return FALSE;
}
LRESULT CDemographicsView::OnEraseBackground(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
return 1;
}
LRESULT CDemographicsView::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
CPaintDC dc(m_hWnd);
RECT clientRect = {0};
GetClientRect(&clientRect);
CMemoryDC memdc(dc, clientRect);
memdc.SetBkMode(TRANSPARENT);
CDC dcBmp;
dcBmp.CreateCompatibleDC(memdc);
HBITMAP hOld = dcBmp.SelectBitmap(m_hBackground);
memdc.BitBlt(0,0,clientRect.right,clientRect.bottom,dcBmp,0,0,SRCCOPY);
dcBmp.SelectBitmap(hOld);
INT iFontSizePixel = 0;
WCHAR wszTmp[MAX_PATH] = L"";
SHGetUIMetrics(SHUIM_FONTSIZE_PIXEL,&iFontSizePixel,sizeof(iFontSizePixel),NULL);
if (clientRect.bottom < iFontSizePixel * 9)
{
memdc.DrawText(L"Not enough Space", -1, &clientRect, DT_CENTER);
return E_FAIL;
}
INT x = 76;
INT y = 11;
INT nWidth = TextOut(x,y,_T("Sat: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%d ",m_gpsPosition.dwSatelliteCount);
x+=nWidth;
nWidth = TextOut(x,y,wszTmp,memdc,m_DefaultFont);
x+=nWidth;
nWidth = TextOut(x,y,_T("Lat: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%2.3f ",m_gpsPosition.dblLatitude);
x+=nWidth;
nWidth = TextOut(x,y,wszTmp,memdc,m_DefaultFont);
x+=nWidth;
nWidth = TextOut(x,y,_T("Long: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%2.3f ",m_gpsPosition.dblLongitude);
x+=nWidth;
nWidth = TextOut(x,y,wszTmp,memdc,m_DefaultFont);
//memdc.DrawText(wszTmp, -1, &clientRect, DT_LEFT);
CSize textSize;
memdc.GetTextExtent(_T("Income: "),9,&textSize);
nWidth = textSize.cx;
x = 165; y = 50;
TextOut(x,y,_T("Speed: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%.0f mph",
m_gpsPosition.flSpeed);
x+=nWidth;
TextOut(x,y,wszTmp,memdc,m_DefaultFont);
x = 165; y = 84;
TextOut(x,y,_T("Income: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"$%d",
m_info.MedianIncome);
x+=nWidth;
TextOut(x,y,wszTmp,memdc,m_DefaultFont);
x = 165; y = 118;
TextOut(x,y,_T("Value: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"$%d",
m_info.MedianHouseValue);
x+=nWidth;
TextOut(x,y,wszTmp,memdc,m_DefaultFont);
x= 65; y = 158;
nWidth = TextOut(x,y,_T("Age: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%d ",m_info.MedianAge);
x+=nWidth;
nWidth = TextOut(x,y,wszTmp,memdc,m_DefaultFont);
x+=nWidth;
nWidth = TextOut(x,y,_T("Male: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%3.0f%% ",m_info.GenderPercentages.Male);
x+=nWidth;
nWidth = TextOut(x,y,wszTmp,memdc,m_DefaultFont);
x+=nWidth;
nWidth = TextOut(x,y,_T("Female: "),memdc,m_DefaultFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%3.0f%% ",m_info.GenderPercentages.Female);
x+=nWidth;
nWidth = TextOut(x,y,wszTmp,memdc,m_DefaultFont);
memdc.GetTextExtent(_T("White: "),8,&textSize);
nWidth = textSize.cx;
INT startX = 20;
INT spaceY = 18;
x= startX; y = 52;
TextOut(x,y,_T("Asian: "),memdc,m_SmallFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%2.2f%% ",m_info.RacePercentages.Asian);
x+=nWidth;
TextOut(x,y,wszTmp,memdc,m_SmallFont);
x= startX;
y+= spaceY;
TextOut(x,y,_T("Black: "),memdc,m_SmallFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%2.2f%% ",m_info.RacePercentages.Black);
x+=nWidth;
TextOut(x,y,wszTmp,memdc,m_SmallFont);
x= startX;
y+= spaceY;
TextOut(x,y,_T("White: "),memdc,m_SmallFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%2.2f%% ",m_info.RacePercentages.White);
x+=nWidth;
TextOut(x,y,wszTmp,memdc,m_SmallFont);
x= startX;
y+= spaceY;
TextOut(x,y,_T("Other: "),memdc,m_SmallFontBold);
StringCchPrintfEx(wszTmp,
sizeof(wszTmp)/sizeof(wszTmp[0]),
NULL,
NULL,
STRSAFE_NULL_ON_FAILURE,
L"%2.2f%% ",m_info.RacePercentages.Other +
m_info.RacePercentages.Indian +
m_info.RacePercentages.Mixed +
m_info.RacePercentages.NativeHawaiian);
x+=nWidth;
TextOut(x,y,wszTmp,memdc,m_SmallFont);
return S_OK;
}
HRESULT CDemographicsView::SetGPSPosition(GPS_POSITION gps_Position)
{
::EnterCriticalSection(&m_cs);
m_gpsPosition = gps_Position;
//We will update only if we have data
if( m_gpsPosition.dwSatelliteCount != 0 )
{
double dist = (m_oldLat - gps_Position.dblLatitude) * (m_oldLat - gps_Position.dblLatitude) +
(m_oldLong - gps_Position.dblLongitude) * (m_oldLong - gps_Position.dblLongitude);
if( dist > DIST_THRESHOLD )
{
m_oldLat = gps_Position.dblLatitude;
m_oldLong = gps_Position.dblLongitude;
DemographixQuery::CDemographixQuery webService;
HRESULT hr = webService.GetLocationInformationByLatitudeLongitude(
m_gpsPosition.dblLatitude,
m_gpsPosition.dblLongitude,
CComBSTR(DEMO_LICENSE),
&m_info);
}
Invalidate();
}
::LeaveCriticalSection(&m_cs);
return S_OK;
}
HRESULT CDemographicsView::SetGPSDeviceInfo(GPS_DEVICE gps_Device)
{
::EnterCriticalSection(&m_cs);
m_gpsDevice = gps_Device;
Invalidate();
::LeaveCriticalSection(&m_cs);
return S_OK;
}
INT CDemographicsView::TextOut(INT x, INT y, LPCTSTR szText, CDC & dc, HFONT hfont)
{
HFONT oldFont = dc.SelectFont(hfont);
CSize textSize;
dc.ExtTextOut(x,y,0,NULL,szText);
dc.GetTextExtent(szText,_tcslen(szText),&textSize);
return textSize.cx;
}