// MainFrm.cpp : implmentation of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "atlprint.h"
#include "aboutdlg.h"
#include "htmlayoutestView.h"
#include "MainFrm.h"
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
return TRUE;
return m_view.PreTranslateMessage(pMsg);
}
BOOL CMainFrame::OnIdle()
{
UIUpdateToolBar();
return FALSE;
}
LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
UISetCheck(ID_VIEW_TOOLBAR, 1);
UISetCheck(ID_VIEW_STATUS_BAR, 1);
CreateSimpleStatusBar();
//our HTML toolbar
m_toolbar.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0);
//set text to it (html)
m_toolbar.SetText(LoadHtmlRes("toolbar"));
m_url.SubclassWindow(m_toolbar.GetDlgItemByName("url"));
//our HTML view
m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_TABSTOP | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0);
//set text to it (html)
m_view.SetText(LoadHtmlRes("splash"));
//SetMenu(NULL);
//easy!
// register object for message filtering and idle updates
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != NULL);
pLoop->AddMessageFilter(this);
pLoop->AddIdleHandler(this);
return 0;
}
LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
PostMessage(WM_CLOSE);
return 0;
}
LRESULT CMainFrame::OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CFileDialog dlg(TRUE, "htm", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"HTML Files (*.htm,*.html)\0*.HTM;*.HTML\0"
"All Files (*.*)\0*.*\0",
m_hWnd);
if(dlg.DoModal() == IDOK)
{
//set default UI font
m_view.SendMessage(WM_SETFONT,0,0);
m_view.OpenFile(dlg.m_szFileName);
}
return 0;
}
LRESULT CMainFrame::OnFilePrint(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CFileDialog dlg(TRUE, "htm", NULL, 0,
"HTML Files (*.htm,*.html)\0*.HTM;*.HTML\0"
"All Files (*.*)\0*.*\0",
m_hWnd);
if(dlg.DoModal() != IDOK)
return 0;
HANDLE hFile = ::CreateFile(dlg.m_szFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
_ASSERTE(hFile != INVALID_HANDLE_VALUE);
if( hFile == INVALID_HANDLE_VALUE ) return 0;
DWORD textLength = ::GetFileSize(hFile, NULL);
LPBYTE text = new BYTE[textLength];
DWORD dwRead;
if(text == 0)
goto END;
if( !::ReadFile(hFile, text, textLength, &dwRead, NULL) )
goto END;
if(textLength != dwRead)
goto END;
DoPrintHTML(dlg.m_szFileName,text,textLength);
END:
if(hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
delete text;
return 0;
}
void CMainFrame::DoPrintHTML(LPCTSTR baseUri, const LPBYTE text, DWORD textLength)
{
CPrintDialog dlg(FALSE);
if(dlg.DoModal() == IDOK)
{
CDevMode devmode;
devmode.CopyFromHDEVMODE(dlg.m_pd.hDevMode);
TCHAR printerName[MAX_PATH];
DEVNAMES* pdn = (DEVNAMES*)::GlobalLock(dlg.m_pd.hDevNames);
if (pdn != NULL)
{
_tcscpy(printerName, (LPTSTR)pdn + pdn->wDeviceOffset);
::GlobalUnlock(dlg.m_pd.hDevNames);
}
HTMLayoutPrintA
(
printerName,
devmode.m_pDevMode->dmPaperSize,
devmode.m_pDevMode->dmDefaultSource,
devmode.m_pDevMode->dmOrientation,
devmode.m_pDevMode->dmDuplex,
1000,1000,1000,1000, //one inch margins
800, // screen 800 pix == printer page width
text,
textLength,
baseUri
);
}
}
LRESULT CMainFrame::OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
static BOOL bVisible = TRUE; // initially visible
bVisible = !bVisible;
CReBarCtrl rebar = m_hWndToolBar;
int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1); // toolbar is 2nd added band
rebar.ShowBand(nBandIndex, bVisible);
UISetCheck(ID_VIEW_TOOLBAR, bVisible);
UpdateLayout();
return 0;
}
LRESULT CMainFrame::OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
UpdateLayout();
return 0;
}
LRESULT CMainFrame::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
CAboutDlg dlg;
dlg.DoModal();
return 0;
}
LRESULT CMainFrame::OnHyperlinkToolbar(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
NMHL_HYPERLINK* phlink = (NMHL_HYPERLINK*)pnmh;
USES_CONVERSION;
BOOL dummy;
if(phlink->status == HL_HYPERLINK_CLICK)
{
//ShellExecute(m_hWnd, "open", W2CA(phlink->href), NULL, NULL, SW_SHOWNORMAL);
if(wcscmp(phlink->href,L"open")==0)
OnFileOpen(0,0,0,dummy);
else if(wcscmp(phlink->href,L"about")==0)
OnAppAbout(0,0,0,dummy);
}
/*
else if(phlink->status == HL_HYPERLINK_ENTER)
{
::SendMessage(m_hWndStatusBar, SB_SIMPLE, TRUE, 0L);
::SendMessage(m_hWndStatusBar, SB_SETTEXT, (255 | SBT_NOBORDERS), (LPARAM)W2CA(phlink->href));
}
else
::SendMessage(m_hWndStatusBar, SB_SIMPLE, FALSE, 0L);
*/
return 0;
}
LRESULT CMainFrame::OnHyperlinkView(int /*idCtrl*/, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
NMHL_HYPERLINK* phlink = (NMHL_HYPERLINK*)pnmh;
USES_CONVERSION;
if(phlink->status == HL_HYPERLINK_CLICK)
ShellExecute(m_hWnd, "open", W2CA(phlink->href), NULL, NULL, SW_SHOWNORMAL);
else if(phlink->status == HL_HYPERLINK_ENTER)
{
::SendMessage(m_hWndStatusBar, SB_SIMPLE, TRUE, 0L);
::SendMessage(m_hWndStatusBar, SB_SETTEXT, (255 | SBT_NOBORDERS), (LPARAM)W2CA(phlink->href));
}
else
::SendMessage(m_hWndStatusBar, SB_SIMPLE, FALSE, 0L);
return 0;
}