Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

How to wrap an MFC collection into an STL compliant iterator with the Boost iterator_facade

, 20 Oct 2008 CPOL
How to wrap an MFC collection into an STL compliant iterator with the Boost iterator_facade.
// IterTestView.cpp : implementation of the CIterTestView class
//

#include "stdafx.h"
#include "IterTest.h"

#include "IterTestDoc.h"
#include "IterTestView.h"

#include "mfc_iters.h"
#include <string>
#include <sstream>
#include <algorithm>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// Draw functor
struct F_Draw
{
   CDC* m_pDC;
   CScrollView* m_pView;
   int m_x;
   int m_y;
   int m_char_ht;
   std::string m_testing_what;
   int m_printer_ratio;

   F_Draw(CDC* pDC, CScrollView* pView, const char* what):
     m_pDC(pDC)
   , m_pView(pView)
   , m_x(5)
   , m_y(5)
   , m_char_ht(15)
   , m_testing_what(what) 
   , m_printer_ratio(1)
   {
      // Adjust character height for printing
      if(m_pDC->IsPrinting())
      {
         HDC hDC = ::GetDC(NULL);
         int nLogPixelY = ::GetDeviceCaps(hDC, LOGPIXELSY);
         ::ReleaseDC(NULL, hDC);

         if(nLogPixelY == 0)
            nLogPixelY = 1;

         m_printer_ratio = m_pDC->GetDeviceCaps(LOGPIXELSY)/nLogPixelY;

         m_char_ht *= m_printer_ratio;
      }
   }

   void operator()(int i)
   {
      std::stringstream outstr;
      std::string s;

      outstr << i;
      s = outstr.str();

      if(i == 0)
      {
         m_pDC->TextOut(m_x, m_y, m_testing_what.c_str(), m_testing_what.size());
         m_y += m_char_ht;
      }

      m_pDC->TextOut(m_x, m_y, s.c_str(), s.size());
      m_y += m_char_ht;
   }
};


// CIterTestView

IMPLEMENT_DYNCREATE(CIterTestView, CScrollView)

BEGIN_MESSAGE_MAP(CIterTestView, CScrollView)
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
   ON_COMMAND(ID_TESTMFCITERATORS_TESTCARRAYITERATOR, &CIterTestView::OnTestmfciteratorsTestcarrayiterator)
   ON_COMMAND(ID_TESTMFCITERATORS_TESTCLISTITERATOR, &CIterTestView::OnTestmfciteratorsTestclistiterator)
   ON_MESSAGE(MY_TEST_MSG, OnTestViewIter)
   ON_COMMAND(ID_TESTMFCITERATORS_TESTCDOCTEMPLATEITERATOR, &CIterTestView::OnTestmfciteratorsTestcdoctemplateiterator)
END_MESSAGE_MAP()

// CIterTestView construction/destruction

CIterTestView::CIterTestView()
{
	// TODO: add construction code here

}

CIterTestView::~CIterTestView()
{
}

BOOL CIterTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

// CIterTestView drawing

void CIterTestView::OnDraw(CDC* pDC)
{
	CIterTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

   // Test code for MFC iterators
   mfc_carray_iterator<int> itA(&m_array_of_ints);
   mfc_clist_iterator<int> itL(&m_list_of_ints);

   std::for_each(itA.begin(), itA.end(), F_Draw(pDC, this, "Testing CArray iterator"));
   std::for_each(itL.begin(), itL.end(), F_Draw(pDC, this, "Testing CList iterator"));
}


// CIterTestView printing

BOOL CIterTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CIterTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CIterTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}


// CIterTestView diagnostics

#ifdef _DEBUG
void CIterTestView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CIterTestView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

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


// CIterTestView message handlers

void CIterTestView::OnTestmfciteratorsTestcarrayiterator()
{
   for(int i=0; i<10; i++)
      m_array_of_ints.Add(i);
 
   Invalidate();
}

void CIterTestView::OnInitialUpdate()
{
   CScrollView::OnInitialUpdate();

   CSize sizeTotal;
   // TODO: calculate the total size of this view
   sizeTotal.cx = sizeTotal.cy = 1000;
   SetScrollSizes(MM_TEXT, sizeTotal);
}

void CIterTestView::OnTestmfciteratorsTestclistiterator()
{
   for(int i=0; i<10; i++)
      m_list_of_ints.AddTail(i);

   Invalidate();
}

LRESULT CIterTestView::OnTestViewIter(WPARAM w, LPARAM l)
{
   CClientDC dc(this);
   std::string s = (const char*)l;

   dc.TextOut(40, 5, s.c_str());
   return 0;
}

void CIterTestView::OnTestmfciteratorsTestcdoctemplateiterator()
{
   mfc_doc_templ_iterator it = mfc_doc_templ_iterator(AfxGetApp()).begin();
   while(*it)
   {
      it->SaveAllModified();//Save all documents that were modified.
      it++;
   }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

steveb
Architect Robotz Software
United States United States
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 20 Oct 2008
Article Copyright 2008 by steveb
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid