Click here to Skip to main content
15,885,891 members
Articles / Desktop Programming / WTL

WTL integration of Lightweight HTML layout and rendering engine

Rate me:
Please Sign up or sign in to vote.
3.67/5 (32 votes)
10 Oct 20034 min read 253.8K   4.9K   62  
WTL integration of Lightweight HTML layout and rendering engine
// 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;  
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Founder Terra Informatica Software
Canada Canada
Andrew Fedoniouk.

MS in Physics and Applied Mathematics.
Designing software applications and systems since 1991.

W3C HTML5 Working Group, Invited Expert.

Terra Informatica Software, Inc.
http://terrainformatica.com

Comments and Discussions