Hide

SftDirectory 3.5 - ActiveX File/Folder Control

Display
Print

SimpleExplorer Sample (C++)

This sample illustrates implementation of a simple Windows Explorer/File Explorer.

The source code is located at C:\Program Files (x86)\Softelvdm\SftDirectory 3.5\Samples\VC++\SimpleExplorer\SimpleExplorerDlg.cpp or C:\Program Files\Softelvdm\SftDirectory 3.5\Samples\VC++\SimpleExplorer\SimpleExplorerDlg.cpp (on 32-bit Windows versions).

// SimpleExplorerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SimpleExplorer.h"
#include "SimpleExplorerDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSimpleExplorerDlg dialog

CSimpleExplorerDlg::CSimpleExplorerDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CSimpleExplorerDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CSimpleExplorerDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSimpleExplorerDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CSimpleExplorerDlg)
    DDX_Control(pDX, IDC_SFTDIRECTORYLEFT, m_DirLeft);
    DDX_Control(pDX, IDC_SFTDIRECTORYRIGHT, m_DirRight);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSimpleExplorerDlg, CDialog)
    //{{AFX_MSG_MAP(CSimpleExplorerDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_UP_BUTTON, OnUpButton)
    ON_BN_CLICKED(IDC_PROP_BUTTON, OnPropButton)
    ON_BN_DOUBLECLICKED(IDC_UP_BUTTON, OnUpButton)
    ON_BN_DOUBLECLICKED(IDC_PROP_BUTTON, OnPropButton)
    ON_BN_CLICKED(IDC_PROP_NEWFOLDER, OnPropNewFolderClicked)
    ON_BN_DOUBLECLICKED(IDC_PROP_NEWFOLDER, OnPropNewFolderClicked)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSimpleExplorerDlg message handlers

BOOL CSimpleExplorerDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // 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

    CDC* pDC;
    pDC = GetDC();

    LOGFONT lf;
    CFont* pFont = GetFont();
    pFont->GetLogFont(&lf);
    lf.lfHeight = -MulDiv(12, pDC->GetDeviceCaps(LOGPIXELSY), 72);
    m_FontBig.CreateFontIndirect(&lf);

    ReleaseDC(pDC);

    GetDlgItem(IDC_TITLE)->SetFont(&m_FontBig);

    UpdateLeftButtons();
    UpdateRightButtons();

    ISftDirectoryPtr vDirRight = m_DirRight.GetControlUnknown();
    vDirRight->MakeColumnsOptimal();
    vDirRight->PutFont(NULL);
    vDirRight->Headers->PutFont(NULL);

    ISftDirectoryPtr vDirLeft = m_DirLeft.GetControlUnknown();
    vDirLeft->PutFont(NULL);
    vDirLeft->Headers->PutFont(NULL);

    return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CSimpleExplorerDlg::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 CSimpleExplorerDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

void CSimpleExplorerDlg::UpdateLeftButtons()
{
    // Update the buttons dependent on the left tree
    ISftDirectoryFolderPtr f;
    // Enable/disable Up button
    ISftDirectoryPtr vDirLeft = m_DirLeft.GetControlUnknown();
    f = vDirLeft->CurrentFolder;
    if (f != NULL) {
        f = f->Parent;
    }
    GetDlgItem(IDC_UP_BUTTON)->EnableWindow(f != NULL);
}

void CSimpleExplorerDlg::UpdateRightButtons()
{
    ISftDirectoryPtr vDirRight = m_DirRight.GetControlUnknown();
    // Update the buttons dependent on the right side detaillist
    ISftDirectoryFolderPtr f;
    // Enable/disable properties button
    f = vDirRight->CurrentFolder;
    BOOL fEnable = FALSE;
    if (vDirRight->SelectionCount > 0 && f != NULL) {
        if (f->Can(_T("properties"))) {
            fEnable = TRUE;
        }
    }
    GetDlgItem(IDC_PROP_BUTTON)->EnableWindow(fEnable);

    // Enable/disable New Folder button
    fEnable = FALSE;
    f = vDirRight->RootFolder;
    if (f != NULL) {
        if (f->Can(_T("NewFolder"))) {
            fEnable = TRUE;
        }
    }
    GetDlgItem(IDC_PROP_NEWFOLDER)->EnableWindow(fEnable);
}

void CSimpleExplorerDlg::OnUpButton()
{
    ISftDirectoryPtr vDirLeft = m_DirLeft.GetControlUnknown();

    // Move up on level in left side tree control
    ISftDirectoryFolderPtr f;
    f = vDirLeft->CurrentFolder;
    if (f != NULL) {
        f = f->Parent;
        if (f != NULL) {
            f->MakeCurrent(); // make it the current item
            f->MakeVisible(); // make sure it's scrolled into view
        }
    }
}

void CSimpleExplorerDlg::OnPropButton()
{
    ISftDirectoryPtr vDirRight = m_DirRight.GetControlUnknown();

    // Display properties dialog for the currently selected item
    ISftDirectoryFolderPtr f;
    f = vDirRight->CurrentFolder;
    if (vDirRight->SelectionCount > 0 && f != NULL) {
        f->Do("properties");
    }
}

void CSimpleExplorerDlg::OnPropNewFolderClicked()
{
    ISftDirectoryPtr vDirRight = m_DirRight.GetControlUnknown();

    // Create a new folder
    ISftDirectoryFolderPtr f;
    f = vDirRight->RootFolder;
    if (f != NULL) {
        f->Do("NewFolder");
    }
}

void CSimpleExplorerDlg::OnKeyDown_SftDirectoryLeft(short FAR* KeyCode, short Shift)
{
    // F5 automatically reloads left side, but we also want right side updated
    if (*KeyCode == VK_F5) {
        ISftDirectoryPtr vDirRight = m_DirRight.GetControlUnknown();
        vDirRight->Reload(VARIANT_TRUE, VARIANT_TRUE);
    }
}

void CSimpleExplorerDlg::OnKeyDown_SftDirectoryRight(short FAR* KeyCode, short Shift)
{
    // F5 automatically reloads right side, but we also want left side updated
    if (*KeyCode == VK_F5) {
        ISftDirectoryPtr vDirLeft = m_DirLeft.GetControlUnknown();
        vDirLeft->Reload(VARIANT_TRUE, VARIANT_TRUE);
    }
}

void CSimpleExplorerDlg::OnSelectionFinal_SftDirectoryLeft()
{
    ISftDirectoryPtr vDirRight = m_DirRight.GetControlUnknown();
    ISftDirectoryPtr vDirLeft = m_DirLeft.GetControlUnknown();

    UpdateLeftButtons();
    vDirRight->Clear();
    vDirRight->CancelMode();
    vDirRight->Refresh();
    if (vDirLeft->CurrentFolder != NULL)
        vDirRight->TopMostFolderIDL = vDirLeft->CurrentFolder->ItemIDList;
    else
        vDirRight->TopMostFolderSpecial = specialSftDirectoryEmpty;
}

void CSimpleExplorerDlg::OnOpening_SftDirectoryRight(LPDISPATCH FAR* Folder, BOOL FAR* Allow)
{
    ISftDirectoryFolderPtr f = *Folder;
    _bstr_t bstrIDL;
    ISftDirectoryPtr vDirLeft = m_DirLeft.GetControlUnknown();

    // we're about to open a file/folder.  If we can add the folder on the
    // left side, that means we don't need to open it in a separate window

    bstrIDL = f->ItemIDList; // the folder we are looking for
    // this will add the complete hierarchy down to the folder in
    // left tree (if possible)
    ISftDirectoryFolderPtr FolderAdded;
    HRESULT hr = vDirLeft->get_FolderUsingIDL(bstrIDL, VARIANT_FALSE, &FolderAdded);

    // this will locate the exact item if we just added it
    ISftDirectoryFolderPtr FolderFound;
    hr = vDirLeft->get_FolderUsingIDL(bstrIDL, VARIANT_TRUE, &FolderFound);
    if (SUCCEEDED(hr) && FolderFound != NULL) {
        // The folder has been added on the left side
        FolderFound->MakeCurrent(); // make it the current item
        FolderFound->MakeVisible(); // make sure it's scrolled into view
        *Allow = VARIANT_FALSE;     // no need to open it
    }
}

void CSimpleExplorerDlg::OnSelectionFinal_SftDirectoryRight()
{
    UpdateRightButtons();
}

BEGIN_EVENTSINK_MAP(CSimpleExplorerDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CSimpleExplorerDlg)
    ON_EVENT(CSimpleExplorerDlg, IDC_SFTDIRECTORYLEFT, 23 /* SelectionFinal */, OnSelectionFinal_SftDirectoryLeft, VTS_NONE)
    ON_EVENT(CSimpleExplorerDlg, IDC_SFTDIRECTORYRIGHT, 20 /* Opening */, OnOpening_SftDirectoryRight, VTS_PDISPATCH VTS_PBOOL)
    ON_EVENT(CSimpleExplorerDlg, IDC_SFTDIRECTORYRIGHT, 23 /* SelectionFinal */, OnSelectionFinal_SftDirectoryRight, VTS_NONE)
    ON_EVENT(CSimpleExplorerDlg, IDC_SFTDIRECTORYLEFT, 12 /* KeyDown */, OnKeyDown_SftDirectoryLeft, VTS_PI2 VTS_I2)
    ON_EVENT(CSimpleExplorerDlg, IDC_SFTDIRECTORYRIGHT, 12 /* KeyDown */, OnKeyDown_SftDirectoryRight, VTS_PI2 VTS_I2)
    //}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.