Click here to Skip to main content
15,886,137 members
Articles / Desktop Programming / MFC

CBFViewCtrl (BigFile Viewer Controller)

Rate me:
Please Sign up or sign in to vote.
4.79/5 (15 votes)
9 Sep 20044 min read 75.5K   2.2K   37  
Controller that allows you to view very large files
// BFViewCtrlDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BFViewCtrlDemo.h"
#include "BFViewCtrlDemoDlg.h"

#include "afxdlgs.h" // CFileDialog
#include ".\bfviewctrldemodlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


#define ZU_FILESIZE_BYTES	0
#define ZU_FILESIZE_KBYTES	1
#define ZU_FILESIZE_MBYTES	2
#define ZU_FILESIZE_AUTO	3

BOOL MakeSizeString( TCHAR* strOut /*[out]*/, __int64 size , short sFileSizeType , bool addPostFix )
{
	TCHAR strNewSize[50]={0};
	char strSize[25];
	short FilesizeTypeUsed = ZU_FILESIZE_BYTES;

	if( sFileSizeType == ZU_FILESIZE_BYTES )
	{
		FilesizeTypeUsed = ZU_FILESIZE_BYTES;
	}
	else if( sFileSizeType == ZU_FILESIZE_KBYTES )
	{
		size = size / 1024;
		FilesizeTypeUsed = ZU_FILESIZE_KBYTES;
	}
	else if( sFileSizeType == ZU_FILESIZE_MBYTES )
	{
		size = (size / 1024 )/ 1024;
		FilesizeTypeUsed = ZU_FILESIZE_MBYTES;
	}
	else if( sFileSizeType == ZU_FILESIZE_AUTO )
	{
		if( size < 1024*10 )
		{
			FilesizeTypeUsed = ZU_FILESIZE_BYTES;
		}
		else if( size < 1024*1100)
		{
			FilesizeTypeUsed = ZU_FILESIZE_KBYTES;
			size = size / 1024;
		}
		else 
		{
			FilesizeTypeUsed = ZU_FILESIZE_MBYTES;
			size = (size / 1024)/1024;
		}
	}


	_i64toa(size , strSize , 10);
	int cnt = strlen(strSize);

	// who many spaces there will be ( "123 321 123" = 3spaces  )
	int spaces = cnt/3;

	// 4 - 3 = 1 , 1 
	int gidx = cnt - (3*spaces);

	long np = 0;

	for( int i = 0 ; i < cnt ; i++ )
	{
		if( gidx == 0 )
		{
			strNewSize[np]=_T(' ');
			gidx=3;
			np++;
		}
		gidx--;
		strNewSize[np]=strSize[i];
		np++;
	}
	//	CString retval;
	//	retval = strNewSize;
	if( addPostFix )
	{
		if( FilesizeTypeUsed == ZU_FILESIZE_BYTES )
			_tcscat( strNewSize , _T(" B") );
		else if( FilesizeTypeUsed == ZU_FILESIZE_KBYTES )
			_tcscat( strNewSize , _T(" K") );
		else if( FilesizeTypeUsed == ZU_FILESIZE_MBYTES )
			_tcscat( strNewSize , _T(" M") );
	}

	_tcscpy( strOut , strNewSize );
	return TRUE;
}


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CBFViewCtrlDemoDlg dialog



CBFViewCtrlDemoDlg::CBFViewCtrlDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBFViewCtrlDemoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBFViewCtrlDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_BFV , m_Viewer );
}

BEGIN_MESSAGE_MAP(CBFViewCtrlDemoDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_SIZE()
	
	ON_WM_INITMENUPOPUP()

	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
	ON_COMMAND(ID_EDIT_COPYTOCLIPBOARD, OnEditCopytoclipboard)
	ON_COMMAND(ID_EDIT_COPYTOFILE, OnEditCopytofile)
	ON_COMMAND(ID_EDIT_SELECTALL, OnEditSelectall)
	ON_COMMAND(ID_VIEW_ASCII, OnViewAscii)
	ON_COMMAND(ID_VIEW_UNICODE, OnViewUnicode)
	ON_COMMAND(ID_VIEW_BINARY, OnViewBinary)
	ON_UPDATE_COMMAND_UI(ID_VIEW_ASCII, OnUpdateViewAscii)
	ON_UPDATE_COMMAND_UI(ID_VIEW_UNICODE, OnUpdateViewUnicode)
	ON_UPDATE_COMMAND_UI(ID_VIEW_BINARY, OnUpdateViewBinary)

	ON_NOTIFY(  BFVN_OPEN , IDC_BFV , OnUpdateStatus ) 
	ON_NOTIFY(  BFVN_RELOADED , IDC_BFV , OnUpdateStatus ) 

	ON_COMMAND(ID_COLOR_STYLE1, OnColorStyle1)
	ON_COMMAND(ID_COLOR_STYLE2, OnColorStyle2)
	ON_COMMAND(ID_COLOR_STYLE3, OnColorStyle3)
	ON_COMMAND(ID_OPTIONS_AUTORELOAD, OnOptionsAutoreload)
	ON_COMMAND(ID_OPTIONS_FONT, OnOptionsFont)
	ON_COMMAND(ID_COLOR_STYLE4, OnColorStyle4)
	ON_UPDATE_COMMAND_UI(ID_OPTIONS_AUTORELOAD, OnUpdateOptionsAutoreload)
END_MESSAGE_MAP()


// CBFViewCtrlDemoDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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_bar.Create(this); //We create the status bar
	m_bar.SetIndicators(indicators,2); //Set the number of panes 

	CRect rect;
	GetClientRect(&rect);
	//Size the two panes
	m_bar.SetPaneInfo(0,ID_INDICATOR_SIZE, SBPS_NORMAL,rect.Width()-100);      
	m_bar.SetPaneInfo(1,ID_INDICATOR_TYPE, SBPS_STRETCH ,0);

	//This is where we actually draw it on the screen
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,ID_INDICATOR_TYPE);

	m_bar.SetPaneText( 0 , _T("") );
	m_bar.SetPaneText( 1 , _T("") );

	m_Viewer.DragAcceptFiles( TRUE );
	CRect rcView;
	CRect rcClient;

	m_Viewer.GetClientRect( &rcView );
	m_Viewer.ClientToScreen( rcView );
	GetClientRect( rcClient );
	ClientToScreen( rcClient );
	
	m_rcViewCtrl.top = rcView.top - rcClient.top;
	m_rcViewCtrl.bottom = rcClient.bottom - rcView.bottom;
	m_rcViewCtrl.left = rcView.left - rcClient.left;
	m_rcViewCtrl.right= rcClient.right - rcView.right;

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

void CBFViewCtrlDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CBFViewCtrlDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBFViewCtrlDemoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CBFViewCtrlDemoDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, ID_INDICATOR_TYPE);

	CRect rect;
	GetClientRect( rect );
	rect.top += m_rcViewCtrl.top;
	rect.bottom -= m_rcViewCtrl.bottom;
	rect.left =+ m_rcViewCtrl.left;
	rect.right -= m_rcViewCtrl.right;
	if( m_Viewer.GetSafeHwnd() != 0 )
		m_Viewer.MoveWindow( rect.left , rect.top , rect.Width() , rect.Height() , TRUE );
	
}
void CBFViewCtrlDemoDlg::OnFileOpen()
{
	
	CFileDialog FileDlg( TRUE , _T("txt") );
	if( FileDlg.DoModal() == IDOK )
	{
		CString strFilename = FileDlg.GetFileName();
		m_Viewer.OpenFile( strFilename );
	}
}

void CBFViewCtrlDemoDlg::OnFileClose()
{
	PostMessage( WM_CLOSE );
}

void CBFViewCtrlDemoDlg::OnEditCopytoclipboard()
{
	m_Viewer.DoCopy();
}

void CBFViewCtrlDemoDlg::OnEditCopytofile()
{
	m_Viewer.DoCopy( TRUE );
}

void CBFViewCtrlDemoDlg::OnEditSelectall()
{
	m_Viewer.SetSelection( 0 , -1 );
}

void CBFViewCtrlDemoDlg::OnViewAscii()
{
	if( m_Viewer.GetViewMode() != TEXTTYPE_ASCII )
		m_Viewer.ChangeViewMode( TEXTTYPE_ASCII );

}

void CBFViewCtrlDemoDlg::OnViewUnicode()
{
	if( m_Viewer.GetViewMode() != TEXTTYPE_UNICODE )
		m_Viewer.ChangeViewMode( TEXTTYPE_UNICODE );
}

void CBFViewCtrlDemoDlg::OnViewBinary()
{
	if( m_Viewer.GetViewMode() != TEXTTYPE_BINARY )
		m_Viewer.ChangeViewMode( TEXTTYPE_BINARY);
}

void CBFViewCtrlDemoDlg::OnUpdateViewAscii(CCmdUI *pCmdUI)
{
	if( m_Viewer.GetViewMode() == TEXTTYPE_ASCII )
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);

}

void CBFViewCtrlDemoDlg::OnUpdateViewUnicode(CCmdUI *pCmdUI)
{
	if( m_Viewer.GetViewMode() == TEXTTYPE_UNICODE )
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}

void CBFViewCtrlDemoDlg::OnUpdateViewBinary(CCmdUI *pCmdUI)
{
	if( m_Viewer.GetViewMode() == TEXTTYPE_BINARY )
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);
}

void CBFViewCtrlDemoDlg::OnInitMenuPopup(CMenu* pPopupMenu, UINT /*nIndex*/, BOOL /*bSysMenu*/)
{
	//	CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
	ASSERT(pPopupMenu != NULL);
	// Check the enabled state of various menu items.

	CCmdUI state;
	state.m_pMenu = pPopupMenu;
	ASSERT(state.m_pOther == NULL);
	ASSERT(state.m_pParentMenu == NULL);

	// Determine if menu is popup in top-level menu and set m_pOther to
	// it if so (m_pParentMenu == NULL indicates that it is secondary popup).
	HMENU hParentMenu;
	if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
		state.m_pParentMenu = pPopupMenu;    // Parent == child for tracking popup.
	else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
	{
		CWnd* pParent = this;
		// Child windows don't have menus--need to go to the top!
		if (pParent != NULL &&
			(hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
		{
			int nIndexMax = ::GetMenuItemCount(hParentMenu);
			for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
			{
				if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
				{
					// When popup is found, m_pParentMenu is containing menu.
					state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
					break;
				}
			}
		}
	}

	state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
	for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
		state.m_nIndex++)
	{
		state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
		if (state.m_nID == 0)
			continue; // Menu separator or invalid cmd - ignore it.

		ASSERT(state.m_pOther == NULL);
		ASSERT(state.m_pMenu != NULL);
		if (state.m_nID == (UINT)-1)
		{
			// Possibly a popup menu, route to first item of that popup.
			state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
			if (state.m_pSubMenu == NULL ||
				(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
				state.m_nID == (UINT)-1)
			{
				continue;       // First item of popup can't be routed to.
			}
			state.DoUpdate(this, TRUE);   // Popups are never auto disabled.
		}
		else
		{
			// Normal menu item.
			// Auto enable/disable if frame window has m_bAutoMenuEnable
			// set and command is _not_ a system command.
			state.m_pSubMenu = NULL;
			state.DoUpdate(this, FALSE);
		}

		// Adjust for menu deletions and additions.
		UINT nCount = pPopupMenu->GetMenuItemCount();
		if (nCount < state.m_nIndexMax)
		{
			state.m_nIndex -= (state.m_nIndexMax - nCount);
			while (state.m_nIndex < nCount &&
				pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
			{
				state.m_nIndex++;
			}
		}
		state.m_nIndexMax = nCount;
	}

}
void CBFViewCtrlDemoDlg::UpdateStatus()
{
	TCHAR strSize[256];
	CString strTmp;

	if( MakeSizeString( strSize , m_Viewer.GetTotalSize() , ZU_FILESIZE_BYTES , FALSE ) )
	{
		strTmp.Format( _T("%s bytes") , strSize );
	}
	else
		strTmp.Format( _T("Unknown") );

	m_bar.SetPaneText( 0 , strTmp );

	strTmp = m_Viewer.GetViewModeAsString();

	m_bar.SetPaneText( 1 , strTmp );

	CString Str;
	Str = _T("BFViewCtrl Demo - [");
	Str += m_Viewer.GetFileName();
	Str += _T("]");
	SetWindowText( Str );

}

void CBFViewCtrlDemoDlg::OnUpdateStatus(NMHDR * /*pNotifyStruct*/, LRESULT* /*pResult*/) 
{
	UpdateStatus();
}


BOOL CBFViewCtrlDemoDlg::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Add your specialized code here and/or call the base class
	cs.style |= WS_OVERLAPPED;
	cs.dwExStyle |= WS_EX_APPWINDOW;
	return CDialog::PreCreateWindow(cs);
}

void CBFViewCtrlDemoDlg::OnColorStyle1()
{
	m_Viewer.SetColor( RGB(255,255,255) , RGB(0,0,0) , RGB(0,0,0) , RGB(255,255,255), TRUE  );
}

void CBFViewCtrlDemoDlg::OnColorStyle2()
{
	m_Viewer.SetColor( RGB(255,128,64) , RGB(2,15,125) , RGB(0,0,255) , RGB(120,200,195) , TRUE );
}

void CBFViewCtrlDemoDlg::OnColorStyle3()
{
	m_Viewer.SetColor( RGB(255,255,0) , RGB(0,0,0) , RGB(128,0,64) , RGB(255,0,0) , TRUE );
}
void CBFViewCtrlDemoDlg::OnColorStyle4()
{
	m_Viewer.SetColor( RGB(0,0,0) , RGB(255,255,255) , RGB(255,255,255) , RGB(0,0,0), TRUE  );
}

void CBFViewCtrlDemoDlg::OnOptionsAutoreload()
{
	if( m_Viewer.IsAutoReload() )
		m_Viewer.SetReloadChkTimer( 0 );
	else
		m_Viewer.SetReloadChkTimer( 2500 ); // check for file changes every 2.5s 
}
void CBFViewCtrlDemoDlg::OnUpdateOptionsAutoreload(CCmdUI *pCmdUI)
{
	if( m_Viewer.IsAutoReload() )
		pCmdUI->SetCheck(TRUE);
	else
		pCmdUI->SetCheck(FALSE);


}
void CBFViewCtrlDemoDlg::OnOptionsFont()
{
	m_Viewer.ShowFontDlg();
	//CFont* pFont = m_Viewer.GetFont();
}



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions