Hide

SftTabs/DLL 6.5 - Tab Control

Display
Print

SDI Sample (C++/MFC)

This sample illustrates an SDI application using a tab control on its document view.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTabs DLL 6.5\Samples\MFC\SDI\MainFrm.cpp or C:\Program Files\Softelvdm\SftTabs DLL 6.5\Samples\MFC\SDI\MainFrm.cpp (on 32-bit Windows versions).

/****************************************************************************/
/* SftTabs/DLL 6.5 - Tab Control for C/C++                                  */
/* Copyright (C) 1994, 2017  Softel vdm, Inc. All Rights Reserved.          */
/****************************************************************************/

#include "stdafx.h"

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

IMPLEMENT_DYNCREATE(CSampleView, CFormView)

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

BEGIN_MESSAGE_MAP(CSampleView, CFormView)
    //{{AFX_MSG_MAP(CSampleView)
    ON_WM_DESTROY()
    ON_WM_SIZE()
	//}}AFX_MSG_MAP
    // Standard printing commands
    ON_SFTTABSN_SWITCHING(IDC_MAINTAB, OnTabSwitching)
    ON_SFTTABSN_SWITCHED(IDC_MAINTAB, OnTabSwitched)
    ON_WM_MOUSEACTIVATE()
END_MESSAGE_MAP()

/*- Tab Control Initialization Data --------------------------------------------*/

static const SFTTABS_CONTROL CtlInit = {
    SFTTABSSTYLE_MODERN_I,               /* tab style */
    1,                                   /* number of rows */
    0,                                   /* number of tabs per row (if fFixed) */
    2,                                   /* width of left margin */
    0,                                   /* width of right margin */
    FALSE,                               /* same width for all tabs */
    TRUE,                                /* Client area wanted */
    FALSE,                               /* allow multiline label text */
    TRUE,                                /* use with dialog */
    FALSE,                               /* use specified background color only for text */
    TRUE,                                /* scrollable tabs */
    FALSE,                               /* hide scroll buttons */
    FALSE,                               /* bold font for active tab wanted */
    FALSE,                               /* fill rows completely */
    NULL,                                /* scroll button bitmap */
    NULL,                                /* Dialog data associated with active tab */
    NULL,                                /* Dialog window handle associated with active tab */
    NULL,                                /* Frame, used as client area */
    TRUE,                                /* Tooltips wanted */
    FALSE,                               /* drop text if it doesn't fit */
    FALSE,                               /* conditional scroll buttons */
    BMBUTTONSTYLE_THEME_SCROLL,          /* scroll button style */
    TRUE,                                /* display ... if truncated */
    TRUE,                                /* Flyby highlighting */
    TRUE,                                /* use client area colors in partially obscured frames */
    TRUE,                                /* scroll buttons on left side */
    -1,                                  /* row indentation */
    FALSE,                               /* don't show truncated pattern for clipped tab */
    TRUE,                                /* full size scroll buttons */
    TRUE,                                /* use themes on Windows XP */
    TRUE,                                /* use exact window region */
    FALSE,                               /* always show prefix _ */
    0,0,0,0,                             /* animation values */
    NULL,                                /* disabled button bitmap */
    TRUE,                                /* focus rectangle if the control has i/p focus */
    FALSE,                               /* TRUE if Close button wanted */
    FALSE,                               /* TRUE if Close button disabled */
    FALSE,                               /* TRUE if WM_CLOSE message wanted */
    FALSE,                               /* TRUE if Minimize, Restore, Close buttons are full size */
    SFTTABS_BUTTON_NEAR,                 /* scroll button alignment */
    SFTTABS_BUTTON_NEAR,                 /* Minimize, Restore, Close button alignment */
    FALSE,                               /* TRUE if Minimize button wanted */
    FALSE,                               /* TRUE if Minimize button disabled */
    FALSE,                               /* TRUE if Restore button wanted */
    FALSE,                               /* TRUE if Restore button disabled */
    NULL,                                /* Close, Minimize, Restore button bitmap */
    NULL,                                /* Close, Minimize, Restore disabled button bitmap */
    TEXT(""),                            /* scroll left button tooltip */
    TEXT(""),                            /* scroll button tooltip */
    TEXT(""),                            /* Close button tooltip */
    TEXT(""),                            /* Minimize button tooltip */
    TEXT(""),                            /* Restore button tooltip */
    0,                                   /* custom modifications */
    0,                                   /* forced height/width depending on tab style - 0 to ignore */
    FALSE,                               /* switch tabs on button release (or down if FALSE) */
    FALSE,                               /* Rendering compatible with pre-6.0 */
    FALSE,                               /* don't display clientarea border - select styles only */
    SFTTABS_TABS_LEFT,                   /* alignment of tabs on all rows */
    SFTTABS_LAYOUT_DISTRIBUTE,           /* distribution of tabs on rows */
    0, 0,                                /* minimum and maximum number of rows for autoflow layout (none specified) */
    FALSE,                               /* reorderable tabs */
    FALSE,                               /* drag & drop */
};

static const SFTTABS_TAB Tab0 = {   /*&1 Text */
    SFTTABS_NOCOLOR, SFTTABS_NOCOLOR,    /* background, foreground color */
    SFTTABS_NOCOLOR, SFTTABS_NOCOLOR,    /* background, foreground color (when selected) */
    { SFTTABS_GRAPH_NONE, 0 },           /* location */
    TRUE,                                /* enabled/disabled */
    0,                                   /* userdata */
    0,                                   /* reserved */
    NULL,                                /* reserved */
    SFTTABS_NOCOLOR,                     /* Flyby foreground color */
    SFTTABS_NOCOLOR,                     /* Client area background color */
    0,0,0,0,                             /* animation values */
    NULL,                                /* tab-specific ImageList handle*/
    FALSE,                               /* hidden tab */
    SFTTABS_NOCOLOR,                     /* gradient fill background color */
    SFTTABS_NOCOLOR, 
    SFTTABS_NOCOLOR,                     /* gradient fill background color, active tab */
    SFTTABS_NOCOLOR, 
    SFTTABS_NOCOLOR,                     /* gradient fill client area color */
    SFTTABS_NOCOLOR, 
    0,0,0,                         /* TabPicture, TabPictureDisabled and TabPictureHot */
    FALSE,                               /* has tab close button */
    0,0,0,0,0,                   /* gap2, TabPicture2, TabPicture2Active, TabPicture2Disabled, TabPicture2Hot */
};
static const SFTTABS_TAB Tab1 = {   /*&2 Statistics */
    SFTTABS_NOCOLOR, SFTTABS_NOCOLOR,    /* background, foreground color */
    SFTTABS_NOCOLOR, SFTTABS_NOCOLOR,    /* background, foreground color (when selected) */
    { SFTTABS_GRAPH_NONE, 0 },           /* location */
    TRUE,                                /* enabled/disabled */
    0,                                   /* userdata */
    0,                                   /* reserved */
    NULL,                                /* reserved */
    SFTTABS_NOCOLOR,                     /* Flyby foreground color */
    SFTTABS_NOCOLOR,                     /* Client area background color */
    0,0,0,0,                             /* animation values */
    NULL,                                /* tab-specific ImageList handle*/
    FALSE,                               /* hidden tab */
    SFTTABS_NOCOLOR,                     /* gradient fill background color */
    SFTTABS_NOCOLOR, 
    SFTTABS_NOCOLOR,                     /* gradient fill background color, active tab */
    SFTTABS_NOCOLOR, 
    SFTTABS_NOCOLOR,                     /* gradient fill client area color */
    SFTTABS_NOCOLOR, 
    0,0,0,                         /* TabPicture, TabPictureDisabled and TabPictureHot */
    FALSE,                               /* has tab close button */
    0,0,0,0,0,                   /* gap2, TabPicture2, TabPicture2Active, TabPicture2Disabled, TabPicture2Hot */
};
static const SFTTABS_TAB Tab2 = {   /*&3 Attributes */
    SFTTABS_NOCOLOR, SFTTABS_NOCOLOR,    /* background, foreground color */
    SFTTABS_NOCOLOR, SFTTABS_NOCOLOR,    /* background, foreground color (when selected) */
    { SFTTABS_GRAPH_NONE, 0 },           /* location */
    TRUE,                                /* enabled/disabled */
    0,                                   /* userdata */
    0,                                   /* reserved */
    NULL,                                /* reserved */
    SFTTABS_NOCOLOR,                     /* Flyby foreground color */
    SFTTABS_NOCOLOR,                     /* Client area background color */
    0,0,0,0,                             /* animation values */
    NULL,                                /* tab-specific ImageList handle*/
    FALSE,                               /* hidden tab */
    SFTTABS_NOCOLOR,                     /* gradient fill background color */
    SFTTABS_NOCOLOR, 
    SFTTABS_NOCOLOR,                     /* gradient fill background color, active tab */
    SFTTABS_NOCOLOR, 
    SFTTABS_NOCOLOR,                     /* gradient fill client area color */
    SFTTABS_NOCOLOR, 
    0,0,0,                         /* TabPicture, TabPictureDisabled and TabPictureHot */
    FALSE,                               /* has tab close button */
    0,0,0,0,0,                   /* gap2, TabPicture2, TabPicture2Active, TabPicture2Disabled, TabPicture2Hot */
};
static const SFTTABS_TAB Tab3 = {   /*S&oftel vdm, Inc. */
    SFTTABS_NOCOLOR, SFTTABS_NOCOLOR,    /* background, foreground color */
    SFTTABS_NOCOLOR, SFTTABS_NOCOLOR,    /* background, foreground color (when selected) */
    { SFTTABS_GRAPH_NONE, 0 },           /* location */
    TRUE,                                /* enabled/disabled */
    0,                                   /* userdata */
    0,                                   /* reserved */
    NULL,                                /* reserved */
    SFTTABS_NOCOLOR,                     /* Flyby foreground color */
    SFTTABS_NOCOLOR,                     /* Client area background color */
    0,0,0,0,                             /* animation values */
    NULL,                                /* tab-specific ImageList handle*/
    FALSE,                               /* hidden tab */
    SFTTABS_NOCOLOR,                     /* gradient fill background color */
    SFTTABS_NOCOLOR, 
    SFTTABS_NOCOLOR,                     /* gradient fill background color, active tab */
    SFTTABS_NOCOLOR, 
    SFTTABS_NOCOLOR,                     /* gradient fill client area color */
    SFTTABS_NOCOLOR, 
    0,0,0,                         /* TabPicture, TabPictureDisabled and TabPictureHot */
    FALSE,                               /* has tab close button */
    0,0,0,0,0,                   /* gap2, TabPicture2, TabPicture2Active, TabPicture2Disabled, TabPicture2Hot */
};

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

CSampleView::CSampleView()
    : CFormView(CSampleView::IDD)
{
    //{{AFX_DATA_INIT(CSampleView)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // TODO: add construction code here

}

CSampleView::~CSampleView()
{
}

void CSampleView::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CSampleView)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

void CSampleView::OnInitialUpdate()
{
    CFormView::OnInitialUpdate();

    GetParentFrame()->RecalcLayout();

    // turn off scroll bars for this formview
    SIZE sz;
    sz.cx = sz.cy = 0;
    SetScrollSizes(MM_TEXT, sz);
}

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

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

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

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

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

void CSampleView::OnDestroy() 
{
    // Remove all pages from the tab control
    TerminateTabControl(this, &m_MainTab);
    // Unregister, or the window properties used won't be removed
    SftTabs_UnregisterWindow(m_hWnd);

    // the logo page must be explicitly delete'd
    delete m_pTabLogo;

    CFormView::OnDestroy();
}

void CSampleView::OnTabSwitching()
{
    // Switching away from the current tab
    TabSwitching(this, &m_MainTab);
}

void CSampleView::OnTabSwitched()
{
    // In case no one else wants to be an active view (such as the CLogo page),
    // we'll make the main view current
    GetParentFrame()->SetActiveView(this);

    // Switch to the new tab
    TabSwitched(this, &m_MainTab);

    // When switching tabs, views "grab" the input focus. You can explicitly set the
    // focus back to the tab control 
    m_MainTab.SetFocus();
}

int CSampleView::OnMouseActivate( CWnd* pDesktopWnd, UINT nHitTest, UINT message)
{
    // We need to intercept this and pass it to CWnd (instead of CView/CFormView), because
    // we don't want a mouse click to activate this view.  We need the view attached to
    // the tab control to remain active
    return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);
}

void CSampleView::OnSize(UINT nType, int cx, int cy) 
{
    CFormView::OnSize(nType, cx, cy);
    
    if (m_MainTab.m_hWnd) {
        CRect rect;
        m_MainTab.GetClientRect(&rect);
        m_MainTab.MapWindowPoints(this, (LPPOINT)&rect, 2);
        m_MainTab.MoveWindow(rect.left, rect.top, cx-2*rect.left, cy-rect.top - rect.left, TRUE);
    }
}

BOOL CSampleView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
    if (!CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext))
        return FALSE;

    m_SavedContext = *pContext;  // save so we can use it later

    int index;

    /* Associate the tab control created from the dialog       */
    /* resource with the C++ object.                           */
    m_MainTab.SubclassDlgItem(IDC_MAINTAB, this /* parent window */);

    /* You could use DDX/DDV instead and add the following     */
    /* line to the DoDataExchange function of the tab          */
    /* control's parent window (remove the //).                */
    // DDX_Control(pDX, IDC_MAINTAB, m_MainTab);

    /* Initialization is faster if we set redraw off */
    m_MainTab.SetRedraw(FALSE);

    m_pTabEdit = new CTabEdit();         // Edit FormView
    m_pTabStats = new CStatsPage();      // Statistics page
    m_pTabAttr = new CAttrPage();        // Attributes page
    m_pTabLogo = new CLogo();            // Logo page

    /* We are using new features */
    m_MainTab.SetVersion(SFTTABS_6_5);

    index = m_MainTab.AddTab(_T("&1 Text"));
    m_MainTab.SetToolTip(index, _T("Displays the currently opened file"));
    m_MainTab.SetTabInfo(index, &Tab0);
    m_MainTab.SetTabWindowPage(index, m_pTabEdit);
    m_pTabEdit->SaveContext(&m_SavedContext); // save doc/view context

    index = m_MainTab.AddTab(_T("&2 Statistics"));
    m_MainTab.SetToolTip(index, _T("Shows file information"));
    m_MainTab.SetTabInfo(index, &Tab1);
    m_MainTab.SetTabWindowPage(index, m_pTabStats);
    m_pTabStats->SaveContext(&m_SavedContext); // save doc/view context

    index = m_MainTab.AddTab(_T("&3 Attributes"));
    m_MainTab.SetToolTip(index, _T("Changes font and printer margin information"));
    m_MainTab.SetTabInfo(index, &Tab2);
    m_MainTab.SetTabWindowPage(index, m_pTabAttr);
    m_pTabAttr->SaveContext(&m_SavedContext); // save doc/view context

    index = m_MainTab.AddTab(_T("S&oftel vdm, Inc."));
    m_MainTab.SetToolTip(index, _T("Company logo"));
    m_MainTab.SetTabInfo(index, &Tab3);
    m_MainTab.SetTabWindowPage(index, m_pTabLogo);

    m_MainTab.SetControlInfo(&CtlInit);

    // Make sure to turn redraw back on
    m_MainTab.SetRedraw(TRUE);
    m_MainTab.InvalidateRect(NULL, TRUE);

    // If you are not using the sheet/page classes, remove the call to InitializeTabControl.
    // Initialize tab control
    InitializeTabControl(this, 0, &m_MainTab, NULL);
    // Mark the view as a main, tabbed window (so accel. keys work) by registering it.
    SftTabs_RegisterWindow(m_hWnd);

    // resize this formview based on size of dialog in resource
    ResizeParentToFit();
    
    return TRUE;
}

#if defined(USING_RICHEDIT)
# if defined(_WIN64)
static DWORD CALLBACK StreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
# else
static DWORD CALLBACK StreamInCallback(DWORD dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
# endif
{
    CArchive* pAr = (CArchive*) dwCookie;
    *pcb = pAr->Read(pbBuff, cb);
    return 0;
}
#endif

void CSampleView::Serialize(CArchive& ar) 
{
    if (ar.IsStoring())
    {   // storing code
    }
    else {
        // loading code
#if defined(USING_RICHEDIT)
        EDITSTREAM es;
# if defined(_WIN64)
        es.dwCookie = (DWORD_PTR) &ar;
# else
        es.dwCookie = (DWORD) &ar;
# endif
        es.pfnCallback = StreamInCallback; 
        m_pTabEdit->GetRichEditCtrl().StreamIn(SF_TEXT, es);
#else
        m_pTabEdit->SerializeRaw(ar);
#endif
    }
}

void CSampleView::DoDeleteContents()
{
    // clear edit control contents
#if defined(USING_RICHEDIT)
    COurEdit& editCtrl = m_pTabEdit->GetRichEditCtrl();
#else
    COurEdit& editCtrl = m_pTabEdit->GetEditCtrl();
#endif
    CString str(
        _T("This sample application uses a CFormView with a tab control.\r\n")
        _T("\r\n")
        _T("The first tab has an attached CRichEditView, the second and\r\n")
        _T("third tab are CFormViews, the last tab is a CWnd.\r\n")
        _T("\r\n")
        _T("Notice how tabbing into/out of the pages is possible, along\r\n")
        _T("with accelerator keys (Alt+xxx).\r\n")
        _T("\r\n")
        _T("If the main window is resized, the tab control along with its\r\n")
        _T("pages is resized and tab scrolling is possible.\r\n")
        _T("\r\n")
        _T("This sample uses the document/view architecture offered by MFC.\r\n")
    );        
    editCtrl.SetWindowText(str);
    editCtrl.EmptyUndoBuffer();
    // and switch to first tab
    m_MainTab.SetCurrentTab(0);
}

COurEdit& CSampleView::GetEditControl()
{
#if defined(USING_RICHEDIT)
    return m_pTabEdit->GetRichEditCtrl();
#else
    return m_pTabEdit->GetEditCtrl();
#endif
}

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