Hide

SftPrintPreview/DLL 2.0 - Print Preview Control for C/C++

Display
Print

PreviewSftTree Sample (C++/MFC)

This sample illustrates SftTree/DLL control output.

The source code is located at C:\Program Files (x86)\Softelvdm\SftPrintPreview DLL 2.0\Samples\MFC\PreviewSftTree\PreviewSftTreeView.cpp or C:\Program Files\Softelvdm\SftPrintPreview DLL 2.0\Samples\MFC\PreviewSftTree\PreviewSftTreeView.cpp (on 32-bit Windows versions).


#include "stdafx.h"
#include "PreviewSftTree.h"

#include "PreviewSftTreeDoc.h"
#include "TreePreview.h"
#include "PreviewSftTreeView.h"

#define IDC_TREE 100                     /* Tree control ID */

#define LINETEXT TEXT("Softel vdm, Inc. - www.softelvdm.com")

#define COLUMNS             3           // # of columns in tree control

IMPLEMENT_DYNCREATE(CPreviewSftTreeView, CView)

BEGIN_MESSAGE_MAP(CPreviewSftTreeView, CView)
    //{{AFX_MSG_MAP(CPreviewSftTreeView)
    ON_WM_CREATE()
    ON_WM_SIZE()
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, OnFilePrintDirect)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, OnFilePrintPreview)
    /* Handling expanding/collapsing items */
    ON_SFTTREEN_LBUTTONDBLCLK_TEXT(IDC_TREE, OnLButtonExpandCollapse)
    ON_SFTTREEN_LBUTTONDOWN_BUTTON(IDC_TREE, OnLButtonExpandCollapse)
    ON_SFTTREEN_LBUTTONDBLCLK_BUTTON(IDC_TREE, OnLButtonExpandCollapse)
    ON_SFTTREEN_LBUTTONDOWN_PLUSMIN(IDC_TREE, OnLButtonExpandCollapse)
    ON_SFTTREEN_LBUTTONDBLCLK_PLUSMIN(IDC_TREE, OnLButtonExpandCollapse)
    /* Handling column resizing (double-clicks) */
    ON_SFTTREEN_LBUTTONDBLCLK_COLUMNRES(IDC_TREE, OnLButtonDblClkColumnResize)
END_MESSAGE_MAP()

CPreviewSftTreeView::CPreviewSftTreeView()
{
    srand( (unsigned)time( NULL ) ); // Init random number generator

    // load all bitmaps
    for (int i = 0 ; i < MAX_BITMAPS ; ++i)
        m_Bitmaps[i].LoadBitmap(IDB_BITMAP0+i);

    {
        HDC hDC = ::GetDC(NULL);
        int height = MulDiv(8, GetDeviceCaps(hDC, LOGPIXELSY), 72);// 8 point font
        m_TreeFont.CreateFont(- height, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET,
            OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE|DEFAULT_PITCH,
            TEXT("Microsoft Sans Serif"));
        _ASSERT(m_TreeFont.m_hObject);
        ::ReleaseDC(NULL, hDC);
    }
}

CPreviewSftTreeView::~CPreviewSftTreeView()
{
}

BOOL CPreviewSftTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
    // By registering our own class, we can eliminate CS_HREDRAW and CS_VREDRAW which
    // cause flicker
    cs.lpszClass = AfxRegisterWndClass(CS_DBLCLKS);
    return CView::PreCreateWindow(cs);
}

void CPreviewSftTreeView::OnDraw(CDC* pDC)
{
    CPreviewSftTreeDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
}

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

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

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

/**********************************************************************/
/*                   Initialize Tree Control Window                   */
/**********************************************************************/

int CPreviewSftTreeView::GetABitmapNumber()
{
    return rand() % MAX_BITMAPS;
}

HBITMAP CPreviewSftTreeView::GetABitmap()
{
    return m_Bitmaps[GetABitmapNumber()];
}

CBitmap& CPreviewSftTreeView::GetABitmapRef()
{
    return m_Bitmaps[GetABitmapNumber()];
}

void CPreviewSftTreeView::AddItem(CSftTree& Tree, int level, BOOL fChild, LPCTSTR lpszText, BOOL fLabel, BOOL fCell, BOOL fRow)
{
    SFT_PICTURE Pic;
    int i, index;

    index = m_Tree.AddString(lpszText);/* Add an item */
    m_Tree.SetItemLevel(index, level);/* change level */

    Sft_InitPicture(&Pic);
    Sft_SetPictureBitmap(&Pic, GetABitmap());
    m_Tree.SetItemPicture(index, &Pic); 

    if (fLabel) {
        Sft_InitPicture(&Pic);
        Sft_SetPictureBitmap(&Pic, GetABitmap());
        m_Tree.SetItemLabel(index, GetABitmapRef());

        m_Tree.SetRowText(index, TEXT("Text"));
    }    

    if (fCell) {
        for (i = 0 ; i < COLUMNS ; ++i) {
            SFTTREE_CELLINFOPARM CellInfo;
            CellInfo.version = 5;
            CellInfo.index = index;
            CellInfo.iCol = i;
            m_Tree.GetCellInfo(&CellInfo);
            CellInfo.Cell.flag = SFTTREE_BMP_LEFT|SFTTREE_TEXT_LEFT;
            Sft_SetPictureBitmap(&CellInfo.Cell.CellPicture1, GetABitmap());
            m_Tree.SetCellInfo(&CellInfo);
        }
    }
    if (fRow) {
        SFTTREE_ROWINFOPARM RowInfo;
        RowInfo.version = 5;
        RowInfo.index = index;
        m_Tree.GetRowInfo(&RowInfo);
        Sft_SetPictureBitmap(&RowInfo.Row.RowPicture1, GetABitmap());
        RowInfo.Row.flag = SFTTREE_BMP_LEFT|SFTTREE_TEXT_LEFT;

        m_Tree.SetRowInfo(&RowInfo);
    }

    for (i = 1 ; i < COLUMNS ; ++i) {
        TCHAR szBuffer[80];
        if (((index + 3*i) % (COLUMNS+5)) == 0) {
            wsprintf(szBuffer, TEXT("This is a very long text line in column %d."), i);
            m_Tree.SetText(index, i, szBuffer);
            if (i < COLUMNS -1) {
                // leave next cell blank
                ++i;
                if (fCell) {
                    // clear cell bitmap in each cell
                    SFTTREE_CELLINFOPARM CellInfo;
                    CellInfo.version = 5;
                    CellInfo.index = index;
                    CellInfo.iCol = i;
                    m_Tree.GetCellInfo(&CellInfo);
                    CellInfo.Cell.flag = SFTTREE_BMP_LEFT|SFTTREE_TEXT_LEFT;
                    Sft_ClearPicture(&CellInfo.Cell.CellPicture1);
                    m_Tree.SetCellInfo(&CellInfo);
                }
            }
        } else {
            wsprintf(szBuffer, TEXT("Column %d(%ld)"), i, index);
            m_Tree.SetText(index, i, szBuffer);
        }
    }
}

void CPreviewSftTreeView::InitTreeWindow()
{
    SFT_PICTURE Pic;
    SFT_PICTURE aThreeItemPictures[3];

    m_Tree.SetShowHeader(TRUE);          /* Show column headers */
    /* Register the label picture size.  All label pictures    */
    /* used must be the same size.  Only one picture needs to  */
    /* be registered, even if several are used.                */
    Sft_InitPicture(&Pic);
    Sft_SetPictureBitmap(&Pic, m_Bitmaps[0]);/* Load a label bitmap */
    m_Tree.SetItemLabelPicture(-1, &Pic);/* Register the label picture size */
    /* Register the plus/minus bitmaps.  These bitmaps are     */
    /* used for all items in the tree control. All three       */
    /* bitmaps must be the same size. You cannot override      */
    /* the plus/minus bitmap for individual items.             */
    m_Tree.SetPlusMinus(m_Bitmaps+54);/* Use +/- bitmaps */
    /* Register the item pictures.  These pictures are used    */
    /* for all items in the tree control. All three pictures   */
    /* must be the same size.                                  */
    Sft_InitPicture(&aThreeItemPictures[0]);
    Sft_InitPicture(&aThreeItemPictures[1]);
    Sft_InitPicture(&aThreeItemPictures[2]);
    Sft_SetPictureBitmap(&aThreeItemPictures[0], m_Bitmaps[0]);/* Expandable picture */
    Sft_SetPictureBitmap(&aThreeItemPictures[1], m_Bitmaps[1]);/* Expanded picture */
    Sft_SetPictureBitmap(&aThreeItemPictures[2], m_Bitmaps[2]);/* Leaf picture */
    m_Tree.SetPictures(aThreeItemPictures);        /* Use item pictures */
    m_Tree.SetItemPictureAlign(TRUE);    /* Align item bitmaps */
    /* Register the cell picture size.  All cell pictures used */
    /* must be the same size.  Only one picture needs to be    */
    /* registered, even if several are used.                   */
    {
        SFTTREE_CELLINFOPARM CellInfo;
        CellInfo.version = 5;            
        CellInfo.index = -1;             /* Registering picture size */
        Sft_InitPicture(&CellInfo.Cell.CellPicture1);
        Sft_SetPictureBitmap(&CellInfo.Cell.CellPicture1, m_Bitmaps[0]);
        m_Tree.SetCellInfo(&CellInfo);   /* Register use of cell pictures */
    }
    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) */
    m_Tree.SetButtons(SFTTREE_BUTTON_AUTOMATIC);/* Automatic button style */
    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.SetSelectionStyle(SFTTREE_SELECTION_CELL1);/* Select first cell only */
    m_Tree.SetSelectionArea(SFTTREE_SELECTIONAREA_ALLCELLS);/* Selection changes by clicking on an item's cells */
    m_Tree.SetFlyby(TRUE);               /* Flyby highlighting */
    m_Tree.SetScrollTips(TRUE);          /* Show Scrolltips */
    m_Tree.SetReorderColumns(TRUE);      /* Column reordering */
    m_Tree.SetOpenEnded(TRUE);           /* Last column width */
    m_Tree.SetShowHeaderButtons(TRUE);   /* Show column header as buttons */

    /* Define columns */
    {
        SFTTREE_COLUMN_EX aCol[3] = {
          { 0, 0,                        /* Reserved */
            100,                         /* Width (in pixels) */
            ES_LEFT | SFTTREE_TOOLTIP,   /* Cell alignment */
            ES_LEFT,                     /* Title style */
            TEXT("Title0"),              /* Column header title */
            NULL, NULL,                  /* Reserved field and bitmap handle */
            SFTTREE_BMP_RIGHT,           /* 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 */
            0,                           /* Minimum column width */
          },                             
          { 0, 0,                        /* Reserved */
            100,                         /* Width (in pixels) */
            ES_LEFT | SFTTREE_TOOLTIP,   /* Cell alignment */
            ES_LEFT,                     /* Title style */
            TEXT("Title1"),              /* Column header title */
            NULL, NULL,                  /* Reserved field and bitmap handle */
            SFTTREE_BMP_RIGHT,           /* 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 */
            0,                           /* Minimum column width */
          },                             
          { 0, 0,                        /* Reserved */
            100,                         /* Width (in pixels) */
            ES_LEFT | SFTTREE_TOOLTIP,   /* Cell alignment */
            ES_LEFT,                     /* Title style */
            TEXT("Title2"),              /* Column header title */
            NULL, NULL,                  /* Reserved field and bitmap handle */
            SFTTREE_BMP_RIGHT,           /* 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) */
            2,                           /* 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 */
            0,                           /* Minimum column width */
          }                              
        };
        m_Tree.SetColumns(3, aCol);      /* Set column attributes */
    }

    /* Use column header pictures.  All pictures used must be  */
    /* the same size.                                          */
    {
        LPSFTTREE_COLUMN_EX lpCol;
        int nCols;
        nCols = m_Tree.GetColumns(&lpCol);/* Get column attributes */
        Sft_SetPictureBitmap(&lpCol[0].Picture1, GetABitmap());/* Set column header picture */
        Sft_ClearPicture(&lpCol[1].Picture1);/* Clear column header picture */
        Sft_SetPictureBitmap(&lpCol[2].Picture1, GetABitmap());/* Set column header picture */
        m_Tree.SetColumns(nCols, lpCol); /* Set new column attributes */
    }

    m_Tree.SetShowRowHeader(SFTTREE_ROWSTYLE_BUTTONCOUNT0);/* Row style */
    /* Register the row header picture size.  All row header   */
    /* pictures used must be the same size.  Only one picture  */
    /* needs to be registered, even if several are used.       */
    {
        SFTTREE_ROWINFOPARM RowInfo;
        RowInfo.version = 5;             
        RowInfo.index = -1;
        Sft_InitPicture(&RowInfo.Row.RowPicture1);
        Sft_SetPictureBitmap(&RowInfo.Row.RowPicture1, GetABitmap());
        m_Tree.SetRowInfo(&RowInfo);
    }
    m_Tree.SetRowColText(_T("?"));       /* Row/column header text */
    Sft_InitPicture(&Pic);
    Sft_SetPictureBitmap(&Pic, GetABitmap());/* Row/column header picture */
    m_Tree.SetRowColPicture(&Pic);       /* Row/column picture */
    m_Tree.SetRowColPictureStyle(SFTTREE_BMP_RIGHT);/* Row/column picture alignment */
    m_Tree.SetCharSearchMode(SFTTREE_CHARSEARCH_ALLCHARS, -1);/* Consider all characters typed */
    /* Change the default colors */
    {
        SFTTREE_COLORS Colors;
        m_Tree.GetCtlColors(&Colors);    /* Get current color settings */
        Colors.colorTreeLines = COLOR_BTNSHADOW | 0x80000000L;/* Tree line color */
        Colors.colorTreeLinesGrayed = COLOR_BTNSHADOW | 0x80000000L;/* Tree line color (disabled control) */
        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 */
    }

    {
        int count = 0;
        int i;
        for (i = 0 ; i < 200 ; ++i) {
            TCHAR szBuffer[80];
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 0, FALSE, szBuffer, FALSE, FALSE, FALSE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 1, FALSE, szBuffer, TRUE, FALSE, FALSE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 2, FALSE, szBuffer, FALSE, FALSE, FALSE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 3, TRUE, szBuffer, FALSE, TRUE, FALSE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 3, TRUE, szBuffer, TRUE, FALSE, TRUE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 2, FALSE, szBuffer, FALSE, FALSE, FALSE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 3, TRUE, szBuffer, FALSE, FALSE, FALSE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 2, TRUE, szBuffer, FALSE, TRUE, TRUE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 1, FALSE, szBuffer, FALSE, FALSE, FALSE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 2, TRUE, szBuffer, TRUE, FALSE, TRUE);
            wsprintf(szBuffer, TEXT("Item %d"), count++);
            AddItem(m_Tree, 2, TRUE, szBuffer, FALSE, TRUE, FALSE);
        }
    }

    /* Make all column widths optimal, so text and pictures are */
    /* not clipped horizontally.                               */
    m_Tree.MakeColumnOptimal(-1, 0, FALSE);/* Make column widths optimal */
    /* 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.RecalcHorizontalExtent(0, FALSE);/* Update horizontal scroll bar */
}

/////////////////////////////////////////////////////////////////////////////
// CPreviewSftTreeView message handlers

int CPreviewSftTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;
    
    if (!m_Tree.CreateEx(
        0,                
        SFTTREESTYLE_NOTIFY |            /* Notify parent window */
        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;

    // initialize tree with data
    m_Tree.SetRedraw(FALSE);
    m_Tree.SetFont(&m_TreeFont, FALSE);
    InitTreeWindow();
    m_Tree.SetRedraw(TRUE);
    m_Tree.Invalidate(TRUE);

    return 0;
}

void CPreviewSftTreeView::OnSize(UINT nType, int cx, int cy) 
{
    CView::OnSize(nType, cx, cy);
    
    if (m_Tree)
        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 CPreviewSftTreeView::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 events as the user double-clicks on the      */
/* column resizing area.  The events handled here can be   */
/* changed to suit your application.                       */

void CPreviewSftTreeView::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 */
    }
}

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