www.pudn.com > TidyWin32-src.zip > OutputView.cpp
#include "StdAfx.h"
#include "OutputView.h"
#include "TidyProxy.h"
#include "Profile.h"
//-------------------------------------------------------------------
COutputView::COutputView(CTidyProxy* p) : m_pTidyProxy(p)
{
}
BOOL COutputView::IsDialogMsg(LPMSG lpMsg)
{
return IsDialogMessage(lpMsg);
}
//-------------------------------------------------------------------
LRESULT COutputView::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Set HTML Tidy's icon
HICON hicon = LoadIcon(_Module.GetModuleInstance(),
MAKEINTRESOURCE(IDI_TIDY));
SetIcon(hicon);
m_OutputEdit.Attach(GetDlgItem(IDC_OUTPUT_EDIT));
// Compute edit control's margins
RECT viewRect;
GetClientRect(&viewRect);
RECT editRect;
m_OutputEdit.GetWindowRect(&editRect);
ScreenToClient(&editRect);
m_EditTopLeftMargins.cx = editRect.left;
m_EditTopLeftMargins.cy = editRect.top;
m_EditBottomRightMargins.cx = viewRect.right - editRect.right;
m_EditBottomRightMargins.cy = viewRect.bottom - editRect.bottom;
// Initialize m_logFont (we need m_logFont to keep track of
// the font selected through ChooseFont (in COutputView::OnFont))
ZeroMemory(&m_logFont, sizeof(LOGFONT));
m_logFont.lfHeight =
-MulDiv(10, GetDeviceCaps(m_OutputEdit.GetDC(), LOGPIXELSY), 72);
strcpy(reinterpret_cast(&m_logFont.lfFaceName), "Courier New");
// and edit control's font
HFONT hFont = CreateFontIndirect(&m_logFont);
m_OutputEdit.SetFont(hFont);
// View is initially read-only
CheckDlgButton(IDC_OUTPUT_READ_ONLY, BST_CHECKED);
m_OutputEdit.SetReadOnly(TRUE);
m_OutputEdit.SetOptions(ECOOP_OR, ECO_WANTRETURN);
return 1;
}
//-------------------------------------------------------------------
void COutputView::SetText(const char* str)
{
m_OutputEdit.SetWindowText(str);
}
//-------------------------------------------------------------------
LRESULT COutputView::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
ShowWindow(SW_HIDE);
return 0;
}
//-------------------------------------------------------------------
LRESULT COutputView::OnSaveAs(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
if (m_pTidyProxy->GetTidyOutput().empty())
return 0;
char fileName[_MAX_PATH] = "";
char initialDir[_MAX_PATH];
string prevFileName = GetProfileString(SOURCE_FILE_REG_KEY);
strcpy(initialDir, prevFileName.c_str());
// remove file name from complete path
for (int i = strlen(initialDir) - 1; i; --i) {
if (initialDir[i] != '\\')
initialDir[i] = '\0';
else break;
}
// Initialize OPENFILENAME
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = m_hWnd;
ofn.lpstrFile = fileName;
ofn.nMaxFile = sizeof(fileName);
ofn.lpstrInitialDir = initialDir;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT;
// Display the Save as dialog box
if (GetSaveFileName(&ofn) == TRUE) {
EDITSTREAM es;
FILE* f = fopen(fileName, "wb");
es.dwCookie = reinterpret_cast(f);
es.pfnCallback = COutputView::RichEditOutputCallback;
m_OutputEdit.StreamOut(SF_TEXT, es);
fclose(f);
}
return 0;
}
DWORD COutputView::RichEditOutputCallback(DWORD dwCookie, LPBYTE pbBuff,
LONG cb, LONG *pcb)
{
FILE* f = reinterpret_cast(dwCookie);
*pcb = fprintf(f, "%.*s", cb, pbBuff);
return 0;
}
LRESULT COutputView::OnFont(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// Prepare the CHOOSEFONT struct
CHOOSEFONT cf;
ZeroMemory(&cf, sizeof(CHOOSEFONT));
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = m_OutputEdit.m_hWnd;
cf.lpLogFont = &m_logFont;
cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
if (ChooseFont(&cf) == TRUE) {
HFONT hFont = CreateFontIndirect(cf.lpLogFont);
m_OutputEdit.SetFont(hFont);
m_OutputEdit.Invalidate();
}
return 0;
}
LRESULT COutputView::OnToggleReadOnly(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
if (IsDlgButtonChecked(wID))
m_OutputEdit.SetReadOnly(TRUE);
else m_OutputEdit.SetReadOnly(FALSE);
return 0;
}
//-------------------------------------------------------------------
LRESULT COutputView::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
int fwSizeType = wParam; // resizing flag
int nWidth = LOWORD(lParam); // width of client area
int nHeight = HIWORD(lParam); // height of client area
if (fwSizeType != SIZE_RESTORED)
return 0;
m_OutputEdit.MoveWindow(m_EditTopLeftMargins.cx, m_EditTopLeftMargins.cy,
nWidth - m_EditBottomRightMargins.cx - m_EditTopLeftMargins.cx,
nHeight - m_EditBottomRightMargins.cy - m_EditTopLeftMargins.cy,
TRUE);
return 0;
}