// Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "SSFile Demo.h"
#include "SSFile.h"
#include "Dlg.h"
#include "textentrydlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlg dialog
CDlg::CDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlg)
m_Filename = _T("");
m_InnerPath = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlg)
DDX_Control(pDX, IDC_LIST, m_List);
DDX_Text(pDX, IDC_FILENAME, m_Filename);
DDX_Text(pDX, IDC_INNERPATH, m_InnerPath);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlg, CDialog)
//{{AFX_MSG_MAP(CDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CREATE, OnCreateSSFile)
ON_BN_CLICKED(IDC_OPEN, OnOpenSSFile)
ON_BN_CLICKED(IDC_CLOSE, OnCloseSSFile)
ON_LBN_SELCHANGE(IDC_LIST, OnItemChanged)
ON_BN_CLICKED(IDC_ENTER, OnEnterStg)
ON_BN_CLICKED(IDC_EXIT, OnExitStg)
ON_BN_CLICKED(IDC_EXPORT, OnExport)
ON_BN_CLICKED(IDC_IMPORT, OnImport)
ON_BN_CLICKED(IDC_CREATEINNERSTG, OnCreateStg)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlg message handlers
BOOL CDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
EnableDisable();
return TRUE; // return TRUE unless you set the focus to a control
}
void CDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDlg::EnableDisable()
{
if (m_SSFile.IsOpen())
{
GetDlgItem(IDC_CREATE)->EnableWindow(false);
GetDlgItem(IDC_OPEN)->EnableWindow(false);
GetDlgItem(IDC_CLOSE)->EnableWindow();
GetDlgItem(IDC_CREATEINNERSTG)->EnableWindow();
if (m_List.GetCurSel() == -1)
GetDlgItem(IDC_DELETE)->EnableWindow(false);
else
GetDlgItem(IDC_DELETE)->EnableWindow();
CString str;
if (m_List.GetCurSel() != -1)
m_List.GetText(m_List.GetCurSel(), str);
if (str.Right(9) == "<storage>")
GetDlgItem(IDC_ENTER)->EnableWindow();
else
GetDlgItem(IDC_ENTER)->EnableWindow(false);
GetDlgItem(IDC_EXIT)->EnableWindow(m_SSFile.GetCurrentStorage() != m_SSFile.GetRootStorage());
if (str.Right(8) == "<stream>")
GetDlgItem(IDC_EXPORT)->EnableWindow();
else
GetDlgItem(IDC_EXPORT)->EnableWindow(false);
GetDlgItem(IDC_IMPORT)->EnableWindow();
if (m_List.GetCurSel() != -1)
GetDlgItem(IDC_DELETE)->EnableWindow();
else
GetDlgItem(IDC_DELETE)->EnableWindow(false);
}
else
{
GetDlgItem(IDC_CREATE)->EnableWindow();
GetDlgItem(IDC_OPEN)->EnableWindow();
GetDlgItem(IDC_CLOSE)->EnableWindow(false);
GetDlgItem(IDC_CREATEINNERSTG)->EnableWindow(false);
GetDlgItem(IDC_DELETE)->EnableWindow(false);
GetDlgItem(IDC_ENTER)->EnableWindow(false);
GetDlgItem(IDC_EXIT)->EnableWindow(false);
GetDlgItem(IDC_EXPORT)->EnableWindow(false);
GetDlgItem(IDC_IMPORT)->EnableWindow(false);
}
}
void CDlg::PopulateList()
{
m_List.ResetContent();
UpdateData();
if (!m_SSFile.IsOpen())
return;
m_InnerPath = m_SSFile.GetPath();
UpdateData(false);
IEnumSTATSTG *estg;
IStorage *stg = m_SSFile.GetCurrentStorage();
if (stg)
{
if (stg->EnumElements(0, NULL, 0, &estg) != S_OK)
return;
STATSTG stgstruct;
ULONG fetched;
while (estg->Next(1, &stgstruct, &fetched) == S_OK)
{
char *c = new char[MAX_PATH];
WideCharToMultiByte(CP_ACP, NULL, stgstruct.pwcsName, -1, c, MAX_PATH, NULL, NULL);
CString title = c;
delete[] c;
if (stgstruct.type == STGTY_STORAGE)
title += " <storage>";
if (stgstruct.type == STGTY_STREAM)
title += " <stream>";
m_List.AddString(title);
}
estg->Release();
}
}
void CDlg::OnCreateSSFile()
{
UpdateData();
if (!m_SSFile.CreateSSFile(m_Filename))
AfxMessageBox(_T("Unable to create DocFile!"));
EnableDisable();
PopulateList();
}
void CDlg::OnOpenSSFile()
{
UpdateData();
if (!m_SSFile.OpenSSFile(m_Filename))
AfxMessageBox(_T("Unable to open DocFile!"));
EnableDisable();
PopulateList();
}
void CDlg::OnCloseSSFile()
{
UpdateData();
m_SSFile.Close();
EnableDisable();
PopulateList();
}
void CDlg::OnItemChanged()
{
EnableDisable();
}
void CDlg::OnEnterStg()
{
CString str;
m_List.GetText(m_List.GetCurSel(), str);
m_SSFile.EnterStorage(str.Left(str.GetLength() - 10));
EnableDisable();
PopulateList();
}
void CDlg::OnExitStg()
{
m_SSFile.ExitStorage();
EnableDisable();
PopulateList();
}
void CDlg::OnExport()
{
CString str;
m_List.GetText(m_List.GetCurSel(), str);
if (str.Right(8) != "<stream>")
return;
CTextEntryDlg ted;
ted.m_WindowTitle = "Export";
ted.m_Heading = "Please enter a filename to save the stream to::";
if (ted.DoModal() != IDOK)
return;
CFile out;
if (!out.Open(ted.m_Data, CFile::modeCreate | CFile::modeReadWrite))
return;