Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

Email Sample (C++)

This sample illustrates using a splitter bar, cell merging, cell images, sorting, column reordering.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTree OCX 7.5\Samples\VC++\Email\EmailDlg.cpp or C:\Program Files\Softelvdm\SftTree OCX 7.5\Samples\VC++\Email\EmailDlg.cpp (on 32-bit Windows versions).

// EmailDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Email.h"
#include "EmailDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEmailDlg dialog

CEmailDlg::CEmailDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CEmailDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CEmailDlg)
        // 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);

    m_Attach.LoadBitmap(IDB_ATTACHMENT);
}

void CEmailDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CEmailDlg)
    DDX_Control(pDX, IDC_EDIT1, m_Edit1);
    DDX_Control(pDX, IDC_SFTTREE1, m_Tree);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CEmailDlg, CDialog)
    //{{AFX_MSG_MAP(CEmailDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEmailDlg message handlers

long CEmailDlg::AddFolder(LPCTSTR lpszFolder, LPCTSTR lpszDescription, int Count)
{
    long ItemIndex;
    ItemIndex = m_vTree->Items->Add(lpszFolder);
    m_vTree->Cell[ItemIndex][1]->Text = lpszDescription;// add description
    CString str;
    str.Format(_T("%d"), Count);
    m_vTree->Cell[ItemIndex][3]->Text = (LPCTSTR) str; // add number of messages
    m_vTree->Cell[ItemIndex][3]->Data = Count;    // also save number of messages for sorting

    // make the folder name bold
    IFontDispPtr pFont = m_pBoldCellFont;
    m_vTree->Cell[ItemIndex][0]->PutRefFont(pFont);

    // if this folder has no messages (ie. dependents), we still want the
    // folder graphic, not the email (leaf) graphic
    if (Count == 0)
        m_vTree->Item[ItemIndex]->Image = m_vTree->Items->ItemImageExpandable;
    return m_vTree->Item[ItemIndex]->ID;
}

long CEmailDlg::AddMessage(int Level, LPCTSTR lpszEmail, LPCTSTR lpszDesc, LPCTSTR lpszDt, int Size, BOOL Attachment)
{
    long ItemIndex;
    ItemIndex = m_vTree->Items->Add(lpszEmail); // add folder
    m_vTree->Item[ItemIndex]->Level = Level;
    m_vTree->Cell[ItemIndex][1]->Text = lpszDesc; // add description
    m_vTree->Cell[ItemIndex][2]->Text = lpszDt;   // add date
    CString str;
    str.Format(_T("%dK"), Size);
    m_vTree->Cell[ItemIndex][3]->Text = (LPCTSTR) str;
    m_vTree->Cell[ItemIndex][3]->Data = Size; // also save size for sorting
    //if this email has replies (ie. dependents), we still want the
    // email graphic, not the expand folder image
    m_vTree->Item[ItemIndex]->Image = m_vTree->Items->ItemImageLeaf;
    // if the email has an attachment, show the image
    if (Attachment) {
        m_vTree->Cell[ItemIndex][1]->Image->PutBitmapHandle((OLE_HANDLE)(HBITMAP)m_Attach);
        m_vTree->Cell[ItemIndex][1]->ImageHAlign = halignSftTreeRight;
    }
    return m_vTree->Item[ItemIndex]->ID;
}

BOOL CEmailDlg::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

    m_vTree = m_Tree.GetControlUnknown();
    ASSERT(m_vTree != NULL);

    // Make a bold font (one used for many cells)
    IFontPtr pFont = m_vTree->GetFont();
    IFontPtr pCellFont;
    HRESULT hr = pFont->Clone(&m_pBoldCellFont);
    ASSERT(SUCCEEDED(hr));
    m_pBoldCellFont->put_Bold(TRUE);


    m_InboxFolder = AddFolder(_T("Inbox"), _T("Viewed and unviewed mail"), 10);
        AddMessage(1, _T("support@softelvdm.com"), _T("Re: A support question"), _T("10/09/05"), 88, TRUE);
        AddMessage(1, _T("anyone@acompany.com"), _T("Re: Why did you say that"), _T("10/10/05"), 5, FALSE);
        AddMessage(1, _T("anyone@acompany.com"), _T("Re: You're fired"), _T("10/11/05"), 82, TRUE);
        AddMessage(1, _T("anyone@acompany.com"), _T("Re: You're hired"), _T("10/11/05"), 6, FALSE);
        m_vTree->Item[m_vTree->Items->ItemIndex[m_InboxFolder]]->Collapse(VARIANT_FALSE);
    m_OutboxFolder = AddFolder(_T("Outbox"), _T("Mail about to be sent"), 2);
        AddMessage(1, _T("me@mycompany.com"), _T("Re: You're fired"), _T("10/11/05"), 5, FALSE);
        AddMessage(1, _T("anyone@acompany.com"), _T("Re: Why did you say that"), _T("10/10/05"), 5, FALSE);
        m_vTree->Item[m_vTree->Items->ItemIndex[m_OutboxFolder]]->Collapse(VARIANT_FALSE);
    m_SavedFolder = AddFolder(_T("Saved"), _T("Saved messages"), 2);
        AddMessage(1, _T("me@mycompany.com"), _T("A support question"), _T("10/09/05"), 3, FALSE);
        AddMessage(2, _T("support@softelvdm.com"), _T("Re: A support question"), _T("10/09/05"), 88, TRUE);
        AddMessage(3, _T("me@mycompany.com"), _T("Re: A support question"), _T("10/09/05"), 3, FALSE);
        AddMessage(1, _T("me@mycompany.com"), _T("Why did you say that"), _T("10/06/05"), 5, FALSE);
        AddMessage(2, _T("anyone@acompany.com"), _T("Re: Why did you say that"), _T("10/10/05"), 5, FALSE);
        AddMessage(1, _T("me@mycompany.com"), _T("You're fired"), _T("10/08/05"), 2, FALSE);
        AddMessage(2, _T("anyone@acompany.com"), _T("Re: You're fired"), _T("10/11/05"), 82, FALSE);
        AddMessage(1, _T("me@mycompany.com"), _T("You're hired"), _T("10/01/05"), 4, FALSE);
        AddMessage(1, _T("anyone@acompany.com"), _T("Re: You're hired"), _T("10/11/05"), 6, FALSE);
        m_vTree->Item[m_vTree->Items->ItemIndex[m_SavedFolder]]->Collapse(VARIANT_FALSE);
    m_DeletedFolder = AddFolder(_T("Deleted"), _T("Deleted messages"), 0);
    m_vTree->ColumnsObj->MakeOptimal();
    m_vTree->Items->RecalcHorizontalExtent();
    m_vTree->Splitter->MakeOptimal();
    // allow sorting
    m_vTree->Headers->SortIndicators = headerSortIndicatorsSftTreeAuto;

    m_vTree->Items->Current = 0;
    OnCaretChangeSftTree1(0); // to update column headers

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

void CEmailDlg::OnCaretChangeSftTree1(long ItemIndex)
{
    // set column headers based on selected item
    // we saved a value in ItemData telling us if it's a folder
    if (m_vTree->Item[ItemIndex]->ID == m_InboxFolder || m_vTree->Item[ItemIndex]->ID == m_OutboxFolder
            || m_vTree->Item[ItemIndex]->ID == m_SavedFolder || m_vTree->Item[ItemIndex]->ID == m_DeletedFolder) {
        m_vTree->Header[0]->Text = _T("Folder");
        m_vTree->Header[1]->Text = _T("Description");
        m_vTree->Header[2]->Text = _T(""); // we can use this to merge the adjacent title
        m_vTree->Header[3]->Text = _T("Msgs");
    } else {
        m_vTree->Header[0]->Text = _T("From");
        m_vTree->Header[1]->Text = _T("Subject");
        m_vTree->Header[2]->Text = _T("Received");
        m_vTree->Header[3]->Text = _T("Size");
    }
}

void CEmailDlg::OnItemClickSftTree1(long ItemIndex, short ColIndex, short AreaType, short Button, short Shift)
{
    if (AreaType == constSftTreeButton) {
        if (m_vTree->Item[ItemIndex]->Expanded != VARIANT_FALSE)
            m_vTree->Item[ItemIndex]->Collapse(VARIANT_TRUE);
        else
            m_vTree->Item[ItemIndex]->Expand(VARIANT_TRUE, VARIANT_FALSE);
    } else if (AreaType == constSftTreeText) {
        // edit description
        if (ColIndex == 1)
            m_vTree->Cell[ItemIndex][ColIndex]->Edit(0, _variant_t(0L));
    } else if (AreaType == constSftTreeColumn) {
        int sortedColumn = m_vTree->Headers->SortedColumn;
        if (sortedColumn >= 0) {
            // we have to sort based on current column headers
            // we saved a value in Item.Data telling us if it's a folder
            long current = m_vTree->Items->Current;
            long currentID = m_vTree->Item[current]->ID;
            if (currentID == m_InboxFolder || currentID == m_OutboxFolder || currentID == m_SavedFolder || currentID == m_DeletedFolder) {
                if (m_vTree->Header[sortedColumn]->SortIndicator == sortIndicatorSftTreeAscending) {
                    if (sortedColumn == 3)
                        // last column is sorted by Cell.Data (numeric values)
                        m_vTree->Items->SortDependents(-1, sortedColumn, sortSftTreeAscCellItemData);
                    else
                        m_vTree->Items->SortDependents(-1, sortedColumn, sortSftTreeAscending);
                } else {
                    if (sortedColumn == 3)
                        m_vTree->Items->SortDependents(-1, sortedColumn, sortSftTreeDscCellItemData);
                    else
                        m_vTree->Items->SortDependents(-1, sortedColumn, sortSftTreeDescending);
                }
            } else {
                // we're on a message, sort inside folders
                // now sort the dependents of all folders
                ItemIndex = 0;
                do {
                    if (m_vTree->Header[sortedColumn]->SortIndicator == sortIndicatorSftTreeAscending) {
                        if (sortedColumn == 3)
                            // last column is sorted by Cell.Data (numeric values)
                            m_vTree->Items->SortDependents(ItemIndex, sortedColumn, sortSftTreeAscCellItemData);
                        else
                            m_vTree->Items->SortDependents(ItemIndex, sortedColumn, sortSftTreeAscending);
                    } else {
                        if (sortedColumn == 3)
                            m_vTree->Items->SortDependents(ItemIndex, sortedColumn, sortSftTreeDscCellItemData);
                        else
                            m_vTree->Items->SortDependents(ItemIndex, sortedColumn, sortSftTreeDescending);
                    }
                    ItemIndex = m_vTree->Item[ItemIndex]->NextSibling;
                } while (ItemIndex >= 0);
            }
        }
        // update column headers by calling CaretChange
        OnCaretChangeSftTree1(m_vTree->Items->Current);
    }
}

void CEmailDlg::OnItemDblClickSftTree1(long ItemIndex, short ColIndex, short AreaType, short Button, short Shift)
{
    if (AreaType == constSftTreeText || AreaType == constSftTreeButton) {
        // if an item on level 1 or lower is clicked, it's an
        // email message, display message box
        if (m_vTree->Item[ItemIndex]->Level > 0 && AreaType == constSftTreeText) {
            CString str;
            str.Format(_T("You could be viewing the message from %s with the subject '%s'."),
                     (LPCTSTR) m_vTree->Cell[ItemIndex][0]->Text,
                     (LPCTSTR) m_vTree->Cell[ItemIndex][1]->Text);
            AfxMessageBox(str);
        } else {
            // otherwise just expand or collapse the item
            if (m_vTree->Item[ItemIndex]->Expanded != VARIANT_FALSE)
                m_vTree->Item[ItemIndex]->Collapse(VARIANT_TRUE);
            else
                m_vTree->Item[ItemIndex]->Expand(VARIANT_TRUE, VARIANT_FALSE);
        }
    } else if (AreaType == constSftTreeColumnRes) {
        if (ColIndex >= 0) {
            m_vTree->Column[ColIndex]->MakeOptimal();
            m_vTree->Items->RecalcHorizontalExtent();
        } else {
            m_vTree->Splitter->MakeOptimal();
        }
    }
}

void CEmailDlg::OnEditAllowedSftTree1(long ItemIndex, short ColIndex, BOOL FAR* Allowed)
{
    // Only allow editing in column 1
    if (ColIndex == 1)
        *Allowed = TRUE;
    else
        *Allowed = FALSE;
}

void CEmailDlg::OnEditInitializingSftTree1(long FAR* Window, VARIANT FAR* vData, long EditIndex, short EditCol, long FAR* LeftPix, long FAR* TopPix, long FAR* WidthPix, long FAR* HeightPix)
{
    HWND hwndControl;

    hwndControl = m_Edit1.m_hWnd;

    // LeftPix/TopPix/WidthPix/HeightPix describes the current cell area
    // we need to return the position and size needed for editing.
    // In this example, we use the height of the control on the form
    // and center it over the cell.
    CRect rect;
    m_Edit1.GetWindowRect(&rect);
    *TopPix = *TopPix + (*HeightPix - rect.Height()) / 2;
    *HeightPix = rect.Height();

    // Set the text in the control used for cell editing and
    // set other control-specific properties
    CString str((LPCWSTR) m_vTree->Cell[EditIndex][EditCol]->Text);
    m_Edit1.SetWindowText(str);
    m_Edit1.SetSel(0, -1, FALSE);

    // Return the control's window handle
    *Window = (OLE_HANDLE) hwndControl;

    // Define navigation keys
    // VK_TAB
    m_vTree->CellEditIntercept(VK_TAB, (SftTreeCellEditInterceptStyleConstants) (cellEditInterceptSftTreeChar | cellEditInterceptSftTreeControlChar | cellEditInterceptSftTreeShiftChar));
    // VK_RETURN
    m_vTree->CellEditIntercept(VK_RETURN, (SftTreeCellEditInterceptStyleConstants) (cellEditInterceptSftTreeChar | cellEditInterceptSftTreeControlChar | cellEditInterceptSftTreeShiftChar));
    // VK_HOME
    m_vTree->CellEditIntercept(VK_HOME, cellEditInterceptSftTreeControlChar);
    // VK_END
    m_vTree->CellEditIntercept(VK_END, cellEditInterceptSftTreeControlChar);
    // VK_UP
    m_vTree->CellEditIntercept(VK_UP, (SftTreeCellEditInterceptStyleConstants) (cellEditInterceptSftTreeChar | cellEditInterceptSftTreeControlChar | cellEditInterceptSftTreeShiftChar));
    // VK_DOWN
    m_vTree->CellEditIntercept(VK_DOWN, (SftTreeCellEditInterceptStyleConstants) (cellEditInterceptSftTreeChar | cellEditInterceptSftTreeControlChar | cellEditInterceptSftTreeShiftChar));
}

void CEmailDlg::OnEditNavigatingSftTree1(long Key, short Shift, long ItemIndex, short ColIndex)
{
    // Process key pressed
    m_vTree->EditNavigate(Key, Shift);
}

void CEmailDlg::OnEditValidateSftTree1(long Window, const VARIANT FAR& vData, long EditIndex, short EditCol, BOOL FAR* InputValid)
{
    // Validate the new cell contents
    CString str;
    m_Edit1.GetWindowText(str);
    str.TrimLeft();
    if (str.GetLength() <= 0) {
        AfxMessageBox(_T("Please enter a description."));
        *InputValid = VARIANT_FALSE;
    }
}

void CEmailDlg::OnEditEndingSftTree1(long Window, const VARIANT FAR& vData, long EditIndex, short EditCol, BOOL SaveInput)
{
    // Save the new cell contents
    if (SaveInput) {
        CString str;
        m_Edit1.GetWindowText(str);
        m_vTree->Cell[EditIndex][EditCol]->Text = _bstr_t(str);
    }
}

BEGIN_EVENTSINK_MAP(CEmailDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CEmailDlg)
    ON_EVENT(CEmailDlg, IDC_SFTTREE1, 1 /* CaretChange */, OnCaretChangeSftTree1, VTS_I4)
    ON_EVENT(CEmailDlg, IDC_SFTTREE1, 4 /* ItemClick */, OnItemClickSftTree1, VTS_I4 VTS_I2 VTS_I2 VTS_I2 VTS_I2)
    ON_EVENT(CEmailDlg, IDC_SFTTREE1, 5 /* ItemDblClick */, OnItemDblClickSftTree1, VTS_I4 VTS_I2 VTS_I2 VTS_I2 VTS_I2)
    ON_EVENT(CEmailDlg, IDC_SFTTREE1, 44 /* EditAllowed */, OnEditAllowedSftTree1, VTS_I4 VTS_I2 VTS_PBOOL)
    ON_EVENT(CEmailDlg, IDC_SFTTREE1, 38 /* EditInitializing */, OnEditInitializingSftTree1, VTS_PI4 VTS_PVARIANT VTS_I4 VTS_I2 VTS_PI4 VTS_PI4 VTS_PI4 VTS_PI4)
    ON_EVENT(CEmailDlg, IDC_SFTTREE1, 43 /* EditNavigating */, OnEditNavigatingSftTree1, VTS_I4 VTS_I2 VTS_I4 VTS_I2)
    ON_EVENT(CEmailDlg, IDC_SFTTREE1, 37 /* EditValidate */, OnEditValidateSftTree1, VTS_I4 VTS_VARIANT VTS_I4 VTS_I2 VTS_PBOOL)
    ON_EVENT(CEmailDlg, IDC_SFTTREE1, 36 /* EditEnding */, OnEditEndingSftTree1, VTS_I4 VTS_VARIANT VTS_I4 VTS_I2 VTS_BOOL)
    //}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

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