Hide

SftTree/DLL 7.5 - Tree Control

Display
Print

ContentWindows Sample (C++)

This sample illustrates a tree control with cells containing Flash, Windows Media Player, Internet Explorer and a dialog.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTree DLL 7.5\Samples\MFC\ContentWindows\SamplVw.cpp or C:\Program Files\Softelvdm\SftTree DLL 7.5\Samples\MFC\ContentWindows\SamplVw.cpp (on 32-bit Windows versions).

/****************************************************************************/
/* SftTree/DLL 7.5 - Tree Control for C/C++                                 */
/* Copyright (C) 1995, 2016  Softel vdm, Inc. All Rights Reserved.          */
/****************************************************************************/

#include "stdafx.h"
#include "ContentWindows.h"

#include "sampldoc.h"
#include "samplvw.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSampleView

IMPLEMENT_DYNCREATE(CSampleView, CView)

BEGIN_MESSAGE_MAP(CSampleView, CView)
    //{{AFX_MSG_MAP(CSampleView)
    ON_WM_CREATE()
    ON_WM_SIZE()
    //}}AFX_MSG_MAP
    ON_SFTTREEN_LBUTTONDBLCLK_TEXT(IDC_TREE, OnLButtonExpandCollapse)
    ON_SFTTREEN_LBUTTONDOWN_BUTTON(IDC_TREE, OnLButtonExpandCollapse)
    ON_SFTTREEN_LBUTTONDBLCLK_BUTTON(IDC_TREE, OnLButtonExpandCollapse)
    ON_SFTTREEN_LBUTTONDBLCLK_COLUMNRES(IDC_TREE, OnLButtonDblClkColumnResize)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSampleView construction/destruction

CSampleView::CSampleView()
{
    // load sample images
    m_hIconSmall = (HICON) LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);

    // In this example, we are using GDI+ images.
    // GDI+ images CANNOT be added as BITMAP resources. They are always added with
    // a custom resource type, in this example we use "PNG".
    m_ButtonExpanded = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_BUTTONEXPANDED));
    m_ButtonExpandedHot = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_BUTTONEXPANDED_HOT));
    m_ButtonCollapsed = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_BUTTONCOLLAPSED));
    m_ButtonCollapsedHot = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_BUTTONCOLLAPSED_HOT));
    m_aThreeImages[0] = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_FOLDERCLOSED));
    m_aThreeImages[1] = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_FOLDEROPEN));
    m_aThreeImages[2] = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_FOLDERLEAF));
    m_FlashImage = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_FLASH));
    m_WMPImage = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_WMP));
    m_IExplorerImage = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_IE));
    m_DialogImage = (Gdiplus::Image*)SftTree_LoadGDIPlusImageFromResource(AfxGetApp()->m_hInstance, TEXT("PNG"), MAKEINTRESOURCE(IDR_DIALOG));
}

CSampleView::~CSampleView()
{
    if (m_hIconSmall)
        DestroyIcon(m_hIconSmall);
    if (m_ButtonExpanded) delete m_ButtonExpanded;
    if (m_ButtonExpandedHot) delete m_ButtonExpandedHot;
    if (m_ButtonCollapsed) delete m_ButtonCollapsed;
    if (m_ButtonCollapsedHot) delete m_ButtonCollapsedHot;
    if (m_aThreeImages[0]) delete m_aThreeImages[0];
    if (m_aThreeImages[1]) delete m_aThreeImages[1];
    if (m_aThreeImages[2]) delete m_aThreeImages[2];
    if (m_FlashImage) delete m_FlashImage;
    if (m_WMPImage) delete m_WMPImage;
    if (m_IExplorerImage) delete m_IExplorerImage;
    if (m_DialogImage) delete m_DialogImage;
}

/////////////////////////////////////////////////////////////////////////////
// CSampleView drawing

void CSampleView::OnDraw(CDC* pDC)
{
    CSampleDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
}

/////////////////////////////////////////////////////////////////////////////
// CSampleView diagnostics

#ifdef _DEBUG
void CSampleView::AssertValid() const
{
    CView::AssertValid();
}

void CSampleView::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}

CSampleDoc* CSampleView::GetDocument() // non-debug version is inline
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSampleDoc)));
    return (CSampleDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSampleView message handlers

int CSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    if (!m_Tree.CreateEx(
        WS_EX_CLIENTEDGE,
        SFTTREESTYLE_NOTIFY |            /* Notify parent window */
        SFTTREESTYLE_VARIABLE |          /* Variable height items */
        SFTTREESTYLE_LEFTBUTTONONLY |    /* Only respond to left mouse button */
        SFTTREESTYLE_SCROLL |            /* Honor WS_H/VSCROLL */
        WS_HSCROLL | WS_VSCROLL |        /* Vertical and horizontal scrollbars */
        WS_VISIBLE | WS_CHILD,           /* Visible, child window */
        CRect(0,0,0,0),                  /* Location */
        this,                            /* Parent window */
        IDC_TREE))                       /* Tree control ID */
            return -1;

    m_Tree.SetShowHeader(TRUE);          /* Show column headers */
    m_Tree.SetItemLines(5);              /* (Maximum) number of text lines */

    /* Register the item pictures.  These pictures are used    */
    /* for all items in the tree control. All three pictures   */
    /* must be the same size.                                  */
    SFT_PICTURE aPic[3];
    Sft_InitPicture(&aPic[0]);
    Sft_InitPicture(&aPic[1]);
    Sft_InitPicture(&aPic[2]);

    if (m_Tree.GetGDIPlusAvailable()) {
        // In this example, we are using GDI+ images.
        // GDI+ images CANNOT be added as BITMAP resources. They are always added with
        // a custom resource type, in this example we use "PNG".
        Sft_SetPictureGDIPlusImage(&aPic[0], m_aThreeImages[0]);
        Sft_SetPictureGDIPlusImage(&aPic[1], m_aThreeImages[1]);
        Sft_SetPictureGDIPlusImage(&aPic[2], m_aThreeImages[2]);
    } else {
        m_aThreeItemBitmaps[0].LoadBitmap(IDB_EXPANDABLE);/* Expandable bitmap */
        Sft_SetPictureBitmap(&aPic[0], m_aThreeItemBitmaps[0]);/* Assign bitmap */
        m_aThreeItemBitmaps[1].LoadBitmap(IDB_EXPANDED);/* Expanded bitmap */
        Sft_SetPictureBitmap(&aPic[1], m_aThreeItemBitmaps[1]);/* Assign bitmap */
        m_aThreeItemBitmaps[2].LoadBitmap(IDB_LEAF);/* Leaf bitmap */
        Sft_SetPictureBitmap(&aPic[2], m_aThreeItemBitmaps[2]);/* Assign bitmap */
    }
    m_Tree.SetPictures(aPic);        /* Use item pictures */
    /* Override individual item bitmaps using: */
    // m_OtherItemPicture.LoadBitmap(IDB_your_bitmap);/* Item bitmap */
    // Sft_SetPictureBitmap(&Pic, m_OtherItemPicture);/* Assign bitmap */
    // m_Tree.SetItemPicture(index, &Pic);/* Set an item picture */

    m_Tree.SetItemPictureAlign(TRUE);    /* Align item bitmaps */
    m_Tree.SetTreeLineStyle(SFTTREE_TREELINE_DOT0);/* Dotted tree lines (incl. level 0) */
    m_Tree.SetShowButtons(TRUE);         /* Expand/collapse buttons (level 1..n) */
    m_Tree.SetShowButton0(TRUE);         /* Show expand/collapse buttons (level 0) */
    if (m_Tree.GetGDIPlusAvailable())
        m_Tree.SetButtons(SFTTREE_BUTTON_USERDEF);/* User-defined buttons */
    else
        m_Tree.SetButtons(SFTTREE_BUTTON_SIMPLE);/* Button style */
    m_Tree.SetVAlign(SFTTREE_TOP);       /* Vertical alignment */
    m_Tree.SetShowGrid(TRUE);            /* Show grid */
    m_Tree.SetGridStyle(SFTTREE_GRID_BOTH_DOT);/* Dotted grid lines */
    m_Tree.SetShowTruncated(TRUE);       /* Show ... if truncated */
    m_Tree.SetShowFocus(FALSE);          /* Don't show focus rectangle (caret) */
    m_Tree.SetSelectionStyle(SFTTREE_SELECTION_ALL | SFTTREE_SELECTION_OUTLINE);/* Select entire item using outline */
    m_Tree.SetSelectionArea(SFTTREE_SELECTIONAREA_ALLCELLS);/* Selection changes by clicking on an item's cells */
    m_Tree.SetFlyby(TRUE);               /* Flyby highlighting */
    m_Tree.SetUpdateCaretExpandCollapse(FALSE);/* don't update caret location when expand/collapse button clicked */
    m_Tree.SetScrollTips(TRUE);          /* Show Scrolltips */
    m_Tree.SetInheritBgColor(TRUE);      /* Inherit background color of first cell */
    m_Tree.SetOpenEnded(FALSE);          /* Last column width */
    m_Tree.SetShowHeaderButtons(TRUE);   /* Show column header as buttons */

    /* Define control attributes */
    {
        SFTTREE_CONTROL CtrlInfo;
        memset(&CtrlInfo, 0, sizeof(SFTTREE_CONTROL));
        CtrlInfo.cbSize = sizeof(SFTTREE_CONTROL);
        if (!m_Tree.GetControlInfo(&CtrlInfo))/* Get current settings */
            ASSERT(0); /* Error handling goes here */
        // You need to create a images for at least the ButtonExpanded and ButtonCollapsed button state
        //Sft_SetPictureBitmap(&CtrlInfo.ButtonExpanded, a_bitmap_handle);
        //Sft_SetPictureBitmap(&CtrlInfo.ButtonCollapsed, a_bitmap_handle);

        if (m_Tree.GetGDIPlusAvailable()) {
            // In this example, we are using GDI+ images.
            // GDI+ images CANNOT be added as BITMAP resources. They are always added with
            // a custom resource type, in this example we use "PNG".
            Sft_SetPictureGDIPlusImage(&CtrlInfo.ButtonExpanded, m_ButtonExpanded);
            Sft_SetPictureGDIPlusImage(&CtrlInfo.ButtonExpandedHot, m_ButtonExpandedHot);
            Sft_SetPictureGDIPlusImage(&CtrlInfo.ButtonCollapsed, m_ButtonCollapsed);
            Sft_SetPictureGDIPlusImage(&CtrlInfo.ButtonCollapsedHot, m_ButtonCollapsedHot);
        }
        if (!m_Tree.SetControlInfo(&CtrlInfo))/* Save new settings */
            ASSERT(0); /* Error handling goes here */
    }

    /* Define columns */
    {
        SFTTREE_COLUMN_EX aCol[2] = {
          { 0, 0,                        /* Reserved */
            26,                          /* Width (in pixels) */
            ES_LEFT | SFTTREE_TOP | SFTTREE_MULTILINE | SFTTREE_WRAP | SFTTREE_TOOLTIP,/* Cell alignment */
            ES_LEFT | SFTTREE_VCENTER | SFTTREE_HEADER_UP,/* Title style */
            _T("Description"),           /* Column header title */
            NULL, NULL,                  /* Reserved field and bitmap handle */
            SFTTREE_BMP_RIGHT| SFTTREE_BMP_VCENTER,/* Picture alignment */
            0, 0, 0,                     /* Reserved fields */
            SFTTREE_NOCOLOR,             /* Cell background color */
            SFTTREE_NOCOLOR,             /* Cell foreground color */
            SFTTREE_NOCOLOR,             /* Selected cell background color */
            SFTTREE_NOCOLOR,             /* Selected cell foreground color */
            0,                           /* Real column position (set by SetColumns call) */
            0,                           /* Display column number (display position) */
            SFTTREE_COL_MERGE |          /* Column can merge with next */
            SFTTREE_COL_MERGEINTO |      /* Previous column can merge into this column */
            0,                           /* Column flag */
            75,                          /* Minimum column width */
          },
          { 0, 0,                        /* Reserved */
            403,                         /* Width (in pixels) */
            ES_LEFT | SFTTREE_VCENTER | SFTTREE_MULTILINE | SFTTREE_TOOLTIP,/* Cell alignment */
            ES_LEFT | SFTTREE_VCENTER | SFTTREE_HEADER_UP,/* Title style */
            _T("Sample"),                /* Column header title */
            NULL, NULL,                  /* Reserved field and bitmap handle */
            SFTTREE_BMP_RIGHT| SFTTREE_BMP_VCENTER,/* Picture alignment */
            0, 0, 0,                     /* Reserved fields */
            SFTTREE_NOCOLOR,             /* Cell background color */
            SFTTREE_NOCOLOR,             /* Cell foreground color */
            SFTTREE_NOCOLOR,             /* Selected cell background color */
            SFTTREE_NOCOLOR,             /* Selected cell foreground color */
            0,                           /* Real column position (set by SetColumns call) */
            1,                           /* Display column number (display position) */
            SFTTREE_COL_MERGE |          /* Column can merge with next */
            SFTTREE_COL_MERGEINTO |      /* Previous column can merge into this column */
            0,                           /* Column flag */
            75,                          /* Minimum column width */
          }
        };
        m_Tree.SetColumns(2, aCol);      /* Set column attributes */
    }

    m_Tree.SetShowRowHeader(SFTTREE_ROWSTYLE_BUTTONCOUNT1);/* Row style */
    m_Tree.SetRowColHeaderText(_T("?"));       /* Row/column header text */
    m_Tree.SetRowColHeaderStyle(ES_LEFT | SFTTREE_HEADER_UP);/* Row/column header style */
    m_Tree.SetCharSearchMode(SFTTREE_CHARSEARCH_ALLCHARS, -1);/* Consider all characters typed, find next item */
    /* Change the default colors */
    {
        SFTTREE_COLORS Colors;
        m_Tree.GetCtlColors(&Colors);    /* Get current color settings */
        Colors.colorTreeLines = COLOR_3DDKSHADOW | 0x80000000L;/* Tree line color */
        Colors.colorSelBgNoFocus = COLOR_BTNFACE | 0x80000000L;/* Selection background color (no input focus) */
        Colors.colorSelFgNoFocus = COLOR_BTNTEXT | 0x80000000L;/* Selection foreground color (no input focus) */
        m_Tree.SetCtlColors(&Colors);    /* Set new colors */
    }

    /*------------------------------------------------------------------------------*/
    /* Add a few items.                                                             */
    /*------------------------------------------------------------------------------*/

    int index;
    SFT_PICTURE Pic;
    Sft_InitPicture(&Pic);

    index = m_Tree.AddString(_T("SftTree/DLL 7.5 supports Content Windows, which are controls or dialogs (windows in general) that are displayed as cell contents."));
    index = m_Tree.AddString(
            _T("A number of sample controls are shown here, but basically anything that is contained within one window (represented by a window handle (HWND)) ")
            _T("can be shown in a cell.")
    );
    index = m_Tree.AddString(_T("Sample Content Windows"));

	// FLASH

    index = m_Tree.AddString(_T("Flash"));
     m_Tree.SetItemLevel(index, 1);
     Sft_SetPictureGDIPlusImage(&Pic, m_FlashImage);/* Assign image */
     m_Tree.SetItemPicture(index, &Pic);/* Set an item picture */

    // Create the dialog containing the Flash control
    if (!m_FlashDialog.Create(IDD_FLASH_DIALOG, &m_Tree) || !m_FlashDialog.HaveFlash()) {
         m_Tree.SetText(index, 1, _T("Flash ActiveX control could not be instantiated - maybe Flash Player is not installed\n\n")
                                  _T("Flash Player is available at www.adobe.com."));
    } else {
        m_Tree.SetText(index, 1, _T("Flash"));

        SFTTREE_CELLINFOPARM CellInfo;
        CellInfo.version = 7;
        CellInfo.index = index;
        CellInfo.iCol = 1;
        m_Tree.GetCellInfo(&CellInfo);
        CellInfo.Cell.hwndCell = m_FlashDialog.m_hWnd;
        CellInfo.Cell.flag2 |= SFTTREECELL_CONTENT_KEEPSIZE;
        m_Tree.SetCellInfo(&CellInfo);
    }

    // Windows Media Player

    index = m_Tree.AddString(_T("Windows Media Player"));
     m_Tree.SetItemLevel(index, 1);
     Sft_SetPictureGDIPlusImage(&Pic, m_WMPImage);/* Assign image */
     m_Tree.SetItemPicture(index, &Pic);/* Set an item picture */

    // Create the dialog containing the Windows Media Player control
    if (!m_WMPDialog.Create(IDD_WMP_DIALOG, &m_Tree) || !m_WMPDialog.HaveWMP()) {
         m_Tree.SetText(index, 1, _T("Windows Media Player control could not be instantiated - maybe Windows Media Player is not installed\n\n")
                                  _T("Windows Media Player is available at www.microsoft.com."));
    } else {
        m_Tree.SetText(index, 1, _T("Windows Media Player"));

        SFTTREE_CELLINFOPARM CellInfo;
        CellInfo.version = 7;
        CellInfo.index = index;
        CellInfo.iCol = 1;
        m_Tree.GetCellInfo(&CellInfo);
        CellInfo.Cell.hwndCell = m_WMPDialog.m_hWnd;
        CellInfo.Cell.flag2 |= SFTTREECELL_CONTENT_KEEPSIZE;
        m_Tree.SetCellInfo(&CellInfo);
    }

    // Internet Explorer

    index = m_Tree.AddString(_T("Internet Explorer"));
     m_Tree.SetItemLevel(index, 1);
     m_Tree.SetItemHeightMinMax(index, 300, 300); // make sure this item is always 300 pixels high
     Sft_SetPictureGDIPlusImage(&Pic, m_IExplorerImage);/* Assign image */
     m_Tree.SetItemPicture(index, &Pic);/* Set an item picture */

    // Create the dialog containing the web browser control
    if (!m_IEDialog.Create(IDD_IE_DIALOG, &m_Tree) || !m_IEDialog.HaveWebBrowser()) {
         m_Tree.SetText(index, 1, _T("Internet Explorer control could not be instantiated - maybe Internet Explorer is not installed\n\n")
                                  _T("Internet Explorer is available at www.microsoft.com."));
    } else {
        m_Tree.SetText(index, 1, _T("Internet Explorer"));

        SFTTREE_CELLINFOPARM CellInfo;
        CellInfo.version = 7;
        CellInfo.index = index;
        CellInfo.iCol = 1;
        m_Tree.GetCellInfo(&CellInfo);
        CellInfo.Cell.hwndCell = m_IEDialog.m_hWnd;
        m_Tree.SetCellInfo(&CellInfo);
    }

    // Simple Dialog

    index = m_Tree.AddString(_T("Simple Dialog"));
     m_Tree.SetItemLevel(index, 1);
     m_Tree.SetText(index, 1, _T("Simple Dialog"));
     Sft_SetPictureGDIPlusImage(&Pic, m_DialogImage);/* Assign image */
     m_Tree.SetItemPicture(index, &Pic);/* Set an item picture */

    // Create the dialog
    m_SimpleDialog.Create(IDD_SIMPLEDIALOG, &m_Tree);

    SFTTREE_CELLINFOPARM CellInfo;
    CellInfo.version = 7;
    CellInfo.index = index;
    CellInfo.iCol = 1;
    m_Tree.GetCellInfo(&CellInfo);
    CellInfo.Cell.hwndCell = m_SimpleDialog.m_hWnd;
    CellInfo.Cell.flag2 |= SFTTREECELL_CONTENT_KEEPSIZE;
    m_Tree.SetCellInfo(&CellInfo);

    /* Make row header width optimal, so text and pictures are  */
    /* not clipped horizontally.                               */
    m_Tree.MakeRowHeaderOptimal(0, FALSE);/* Make row header width optimal */

    m_Tree.MakeColumnOptimal(-1);/* Make all columns optimal */

    // double the size of the last column
    LPSFTTREE_COLUMN_EX lpCol;
    int nCols;
    nCols = m_Tree.GetColumns(&lpCol);    /* Get all column attributes */
    lpCol[1].width *= 2;
    m_Tree.SetColumns(nCols, lpCol);      /* Set new column attributes */

    m_Tree.RecalcHorizontalExtent(0, FALSE);/* Update horizontal scroll bar */

    m_Tree.SetCaretIndex(0);
    m_Tree.SetCurSel(0);
    return 0;
}

void CSampleView::OnSize(UINT nType, int cx, int cy)
{
    CView::OnSize(nType, cx, cy);
    m_Tree.MoveWindow(0, 0, cx, cy);
}

/* Respond to events as the user clicks on different tree  */
/* components.  The events handled here can be changed to  */
/* suit your application.                                  */

void CSampleView::OnLButtonExpandCollapse()
{
    /* get index of item to expand/collapse */
    int index = m_Tree.GetExpandCollapseIndex();
    /* get current expand/collapsed status */
    BOOL fExpanded = m_Tree.GetItemExpand(index);
    /* if control key is used we'll expand all dependents */
    BOOL fDepth = (::GetKeyState(VK_CONTROL)&0x8000);

    if (fExpanded)
        m_Tree.Collapse(index, TRUE);
    else
        m_Tree.Expand(index, TRUE, fDepth);
}

/* Respond to numeric keypad multiply key.                 */

void CSampleView::OnExpandAll()
{
    /* get index of item to expand/collapse */
    int index = m_Tree.GetExpandCollapseIndex();

   m_Tree.Expand(index, TRUE, TRUE);
}

/* Respond to events as the user double-clicks on the      */
/* column resizing area.  The events handled here can be   */
/* changed to suit your application.                       */

void CSampleView::OnLButtonDblClkColumnResize()
{
    /* Resize column optimally */
    int realCol = m_Tree.GetResizeColumn();
    if (realCol >= 0) {
        m_Tree.MakeColumnOptimal(realCol);/* Make column width optimal */
        m_Tree.RecalcHorizontalExtent(0, FALSE);/* Update horizontal scroll bar */
    }
}