OOFILE  1.9
oofGrMFC.cpp
Go to the documentation of this file.
1 // COPYRIGHT 1997 A.D. Software, All rights reserved
2 //
8 #include "stdafx.h"
9 #include "oofGrMFC.h"
10 #include "oofDraw.h"
11 #include "oofRpRes.h"
12 
13 #ifdef _DEBUG
14 #define new DEBUG_NEW
15 #undef THIS_FILE
16 static char THIS_FILE[] = __FILE__;
17 #endif
18 
19 // -------------------------------------------------------
20 // o o f A f x C r e a t e G r a p h V i e w
21 // -------------------------------------------------------
22 
27 BOOL __stdcall oofAfxCreateGraphView(CRuntimeClass* pFrameClass, UINT nId, oofGraph* pGraph, BOOL bMakeMain)
28 {
29  CFrameWnd* pFrame = (CFrameWnd*)pFrameClass->CreateObject();
30 
31  if (!pFrame || !pFrame->LoadFrame(nId, WS_OVERLAPPEDWINDOW))
32  return FALSE;
33 
34  if (bMakeMain)
35  // Required for SDI applications.
36  // Do NOT use for MDI apps. Main window should have already been created & set
37  // by calling routine.
38  AfxGetThread()->m_pMainWnd = pFrame;
39 
40  oofGraphWindow* pView = oofGraphWindow::CreateGraphWindow(pGraph, pFrame, AFX_IDW_PANE_FIRST);
41 
42  pFrame->SetActiveView(pView);
43  pFrame->RecalcLayout();
44  pFrame->InitialUpdateFrame(NULL, TRUE);
45 
46  return TRUE;
47 }
48 
49 
51 // oofGraphWindow
52 
53 IMPLEMENT_DYNCREATE(oofGraphWindow, CScrollView)
54 
55 BEGIN_MESSAGE_MAP(oofGraphWindow, CScrollView)
56  //{{AFX_MSG_MAP(oofGraphWindow)
57  ON_COMMAND(ID_FILE_PAGE_SETUP, OnFilePageSetup)
58  ON_COMMAND(ID_GRAPH_SCALE_TO_FIT, OnGraphScaleToFit)
59  ON_COMMAND(ID_GRAPH_FULL_SIZE, OnGraphFullSize)
60  ON_UPDATE_COMMAND_UI(ID_GRAPH_FULL_SIZE, OnUpdateGraphFullSize)
61  ON_UPDATE_COMMAND_UI(ID_GRAPH_SCALE_TO_FIT, OnUpdateGraphScaleToFit)
62  //}}AFX_MSG_MAP
63  // Standard printing commands
64  ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
65  ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
66  ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
67 END_MESSAGE_MAP()
68 
69 oofGraphWindow::oofGraphWindow()
70 {
71  mGraph = NULL;
72 #ifdef OOFILE_USE_OLDMETAFILE
73  mhmf = 0;
74 #else
75  mhemf = 0;
76 #endif
77  mFullSize = TRUE;
78 
79  // Default font. We want a TrueType scaleable default font
80  mDefaultFont.CreatePointFont(10, _T("Times New Roman"));
81 
82  // Get the default print record - for pagesizing, etc.
83  CPageSetupDialog ps(PSD_RETURNDEFAULT | PSD_MARGINS | PSD_DEFAULTMINMARGINS, NULL);
84  ps.DoModal();
85  setPageSetup(&ps);
86 }
87 
89 {
90  if (mGraph)
91  delete mGraph;
92 #ifdef OOFILE_USE_OLDMETAFILE
93  if (mhmf)
94  ::DeleteMetaFile(mhmf);
95 #else
96  if (mhemf)
97  ::DeleteEnhMetaFile(mhemf);
98 #endif
99  ::DeleteDC(oofCurrentDraw::getInfoDC());
100 }
101 
102 // Common Factory
104 oofGraphWindow::CreateGraphWindow(oofGraph* inGraphPtr, CWnd * pParent, UINT nId, CPageSetupDialog * pageSetup)
105 {
106  oofGraphWindow* displayWindow = (oofGraphWindow *)RUNTIME_CLASS(oofGraphWindow)->CreateObject();
107  // views are always created with a border!
108  if (!displayWindow->Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
109  CRect(0,0,0,0), pParent, nId, NULL))
110  {
111  TRACE0("Warning: could not create view for frame.\n");
112  return NULL; // can't continue without a view
113  }
114 
115  displayWindow->setGraph(inGraphPtr);
116  if (pageSetup)
117  displayWindow->setPageSetup(pageSetup);
118  displayWindow->start();
119 
120  inGraphPtr->finishConstructionWithPresets();
122  inGraphPtr->draw();
123 
124  displayWindow->finish();
125 
126  displayWindow->ShowWindow(SW_SHOW);
127  displayWindow->OnInitialUpdate();
128  return displayWindow;
129 }
130 
132 {
133  if (oofCurrentDraw::getInfoDC() == NULL) {
134  HDC ic = ::CreateIC("DISPLAY", NULL, NULL, NULL);
135  ASSERT(ic != NULL);
136  oofCurrentDraw::setInfoDC(ic);
137  }
138 
139  if (!mhdc.GetSafeHdc()) {
140  // Create our metafile dc
141 #if OOFILE_USE_OLDMETAFILE
142  VERIFY(mhdc.Create());
143 #else
144  VERIFY(mhdc.CreateEnhanced(&mhdcRef, NULL, &mPaperHimetric, NULL));
145 #endif
146  // Notify our static object that this is now the current drawing device context
147  oofCurrentDraw::setCurrentDC(mhdc.GetSafeHdc());
148 
149  mGraph->setCurrentDC(mhdc.GetSafeHdc()); // ES
150 
151 /* // Setup the clipping region for the margins
152  mhdc.SelectClipRgn(NULL);
153  mhdc.ExcludeClipRect(&mMargin);
154  mhdc.MoveTo(0, 0);
155 */
156  // Select in our default font
157 // mhdc.SelectObject(&mDefaultFont);
158  oofTextDrawer::selectFont(mhdc, mDefaultFont);
159 
160  if (mGraph)
162  }
163 }
164 
166 {
167  if (mhdc.GetSafeHdc()) {
168 #ifdef OOFILE_USE_OLDMETAFILE
169  mhmf = mhdc.Close();
170 #else
171  mhemf = mhdc.CloseEnhanced();
172 #endif
173  // Notify our static object that we don't have a valid device context
174  oofCurrentDraw::setCurrentDC(mhdc.GetSafeHdc());
175  }
176 
177  if (mhdcRef.GetSafeHdc())
178  mhdcRef.DeleteDC();
179 }
180 
181 void oofGraphWindow::setPageSetup(CPageSetupDialog * psd)
182 {
183  PAGESETUPDLG* pageSetup = &psd->m_psd;
184 
185  mMargin = pageSetup->rtMargin;
186  mPaperRect = oofRect(0, 0, pageSetup->ptPaperSize.x, pageSetup->ptPaperSize.y);
188 
189  mhdcRef.CreateDC("DISPLAY",NULL,NULL,NULL);
190 
191  // Now convert to pixels & HIMETRIC for drawing & creating the metafile.
192  mhdcRef.SaveDC();
193  // Set the maping mode according to the units used in the PAGESETUPDLG structure.
194  if (pageSetup->Flags & PSD_INTHOUSANDTHSOFINCHES)
195  mhdcRef.SetMapMode(MM_HIENGLISH);
196  else if (pageSetup->Flags & PSD_INHUNDREDTHSOFMILLIMETERS)
197  mhdcRef.SetMapMode(MM_HIMETRIC);
198 
199  mhdcRef.LPtoDP(mPaperRect);
200  mhdcRef.LPtoDP(mPaperHimetric);
201  mhdcRef.SetMapMode(MM_HIMETRIC);
202  mhdcRef.DPtoLP(mPaperHimetric);
203  mhdcRef.RestoreDC(-1);
204  // We need to adjust for the reverse Y axis in MM_HIENGLISH.
205  // Because mPaperHimetric has been transformed twice, the effect is cancelled.
206 
207  mPaperRect.transform(1, -1);
208 }
209 
211 {
212  pDC->SetMapMode(MM_ANISOTROPIC);
213 
214  CWinApp* pApp = AfxGetApp();
215  ASSERT(pApp != NULL);
216 
217  PRINTDLG printDlg;
218  memset(&printDlg, '\0', sizeof(PRINTDLG));
219 
220  if (pApp->GetPrinterDeviceDefaults(&printDlg))
221  {
222  POINT paperSize;
223  VERIFY(getPaperSize(printDlg.hDevNames, printDlg.hDevMode, &paperSize));
224 
225  CDC ic;
226  VERIFY(ic.CreateIC("DISPLAY", NULL, NULL, NULL));
227 
228  ic.SetMapMode(MM_HIMETRIC);
229  // paperSize is in tens of millimetres, but we need hundreds of millimetres
230  POINT paperRect = {paperSize.x * 10, paperSize.y * 10};
231  // convert paperRect into screen points
232  ic.LPtoDP(&paperRect);
233  // adjust the paper height
234  paperRect.y *= -1;
235 
236  // set the transforming mapping mode
237  pDC->SetWindowExt(paperRect.x, paperRect.y);
238  pDC->SetViewportExt(pDC->GetDeviceCaps(HORZRES), pDC->GetDeviceCaps(VERTRES));
239 
240  return TRUE;
241  }
242  else
243  return FALSE;
244 }
245 
247 {
248  CDC* pDC = CDC::FromHandle(hDC);
249  BOOL bRetVal = setMappingMode(pDC);
250  pDC->Detach();
251 
252  return bRetVal;
253 }
254 
255 BOOL oofGraphWindow::getPaperSize(HANDLE hDevNames, HANDLE hDevMode, LPPOINT paperSize)
256 {
257  LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(hDevNames);
258  LPDEVMODE lpDevMode = (LPDEVMODE)::GlobalLock(hDevMode);
259 
260  if (lpDevNames == NULL || lpDevMode == NULL)
261  return FALSE;
262 
263  // get relationship between portrait and landscape orientations
264  // (0, 90 or 270 degrees)
265  DWORD dwOrientRel = ::DeviceCapabilities(
266  (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset,
267  (LPTSTR)lpDevNames + lpDevNames->wOutputOffset,
268  DC_ORIENTATION,
269  NULL,
270  lpDevMode);
271 
272  if (lpDevMode->dmOrientation == DMORIENT_LANDSCAPE && dwOrientRel != 0) {
273  paperSize->y = lpDevMode->dmPaperWidth;
274  paperSize->x = lpDevMode->dmPaperLength;
275  }
276  else {
277  paperSize->x = lpDevMode->dmPaperWidth;
278  paperSize->y = lpDevMode->dmPaperLength;
279  }
280 
281  ::GlobalUnlock(hDevMode);
282  ::GlobalUnlock(hDevNames);
283 
284  return TRUE;
285 }
286 
288 // oofGraphWindow drawing
289 
291 {
292 #ifdef OOFILE_USE_OLDMETAFILE
293  pDC->PlayMetaFile(mhmf);
294 #else
295  pDC->PlayMetaFile(mhemf, mPaperRect);
296 #endif
297 }
298 
300 // oofGraphWindow diagnostics
301 
302 #ifdef _DEBUG
303 void oofGraphWindow::AssertValid() const
304 {
305  CScrollView::AssertValid();
306 }
307 
308 void oofGraphWindow::Dump(CDumpContext& dc) const
309 {
310  CScrollView::Dump(dc);
311 }
312 #endif //_DEBUG
313 
315 // oofGraphWindow message handlers
316 
318 {
319  CScrollView::OnInitialUpdate();
320 
321  // Calculate the total size of this view
322  SetScrollSizes(MM_TEXT, CSize(mPaperRect.width(), mPaperRect.height()));
323 }
324 
325 BOOL oofGraphWindow::OnPreparePrinting(CPrintInfo* pInfo)
326 {
327  pInfo->SetMaxPage(1);
328 
329  DoPreparePrinting(pInfo);
330  return CScrollView::OnPreparePrinting(pInfo);
331 }
332 
333 void oofGraphWindow::OnPrint(CDC* pDC, CPrintInfo* pInfo)
334 {
335 #ifdef OOFILE_USE_OLDMETAFILE
336  pDC->PlayMetaFile(mhmf);
337 #else
338  pDC->PlayMetaFile(mhemf, &pInfo->m_rectDraw);
339 #endif
340 }
341 
342 
344 {
345  CPageSetupDialog ps(PSD_MARGINS | PSD_DEFAULTMINMARGINS, this);
346 
347  if (ps.DoModal() == IDOK) {
348  setPageSetup(&ps);
349  // Redo the report for the new size
350  start();
351  if (mGraph)
352  mGraph->draw();
353  finish();
354  Invalidate();
355  }
356 }
357 
359 {
360  mFullSize = FALSE;
361  SetScaleToFitSize(CSize(mPaperRect.width(), mPaperRect.height()));
362  Invalidate();
363 }
364 
366 {
367  pCmdUI->SetCheck(!mFullSize);
368 }
369 
371 {
372  mFullSize = TRUE;
373  SetScrollSizes(MM_TEXT, CSize(mPaperRect.width(), mPaperRect.height()));
374  Invalidate();
375 }
376 
378 {
379  pCmdUI->SetCheck(mFullSize);
380 }
381 
382 
383 void oofGraphWindow::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)
384 {
385  if (pDC->IsPrinting())
386  {
387 #ifdef OOFILE_USE_OLDMETAFILE
388  // Set the mapping mode that will convert the report's dimension from screen
389  // into the printer resolution. We needn't do that for the enhanced metafile
390  // as it allows the sizing directly.
391  VERIFY(setMappingMode(pDC));
392 #endif
393  }
394  else
395  CScrollView::OnPrepareDC(pDC, pInfo);
396 }
virtual void OnDraw(CDC *pDC)
Definition: oofGrMFC.cpp:290
Preview window used to show an oofGraph in PowerPlant framework.
Definition: oofGrMfc.h:19
Cross-platform way to specify rectangle with Mac-style setter.
Definition: oofRect.h:39
MFC base class for any window or embedded control.
MFC base class for applications.
BOOL mFullSize
Definition: oofGrMfc.h:41
HENHMETAFILE mhemf
Definition: oofGrMfc.h:32
virtual void OnPrint(CDC *pDC, CPrintInfo *pInfo)
Definition: oofGrMFC.cpp:333
afx_msg void OnUpdateGraphScaleToFit(CCmdUI *pCmdUI)
Definition: oofGrMFC.cpp:365
BOOL __stdcall oofAfxCreateGraphView(CRuntimeClass *pFrameClass, UINT nId, oofGraph *pGraph, BOOL bMakeMain)
Factory function that creates the view for a graph inside an MFC SDI window.
Definition: oofGrMFC.cpp:27
void setPaneRect(Rect inRect)
Definition: oofGrphs.cpp:105
void transform(double dx, double dy)
Definition: oofRect.cpp:146
Definition: oofync.h:31
void draw()
Definition: oofGrphs.cpp:81
void setDrawStyles(oofDrawStyleSet &)
Definition: oofGrphs.cpp:120
Graph class subclassed to draw different graph types.
Definition: oofGrphs.h:71
void setGraph(oofGraph *inGraph)
Definition: oofGrMfc.h:57
int width()
Definition: oofRect.h:71
virtual void OnPrepareDC(CDC *pDC, CPrintInfo *pInfo=NULL)
Definition: oofGrMFC.cpp:383
oofRect mMargin
Definition: oofGrMfc.h:38
virtual BOOL OnPreparePrinting(CPrintInfo *pInfo)
Definition: oofGrMFC.cpp:325
virtual void finishConstructionWithPresets()
Definition: oofGrphs.cpp:213
oofRect mPaperRect
Definition: oofGrMfc.h:37
oofRect mPaperHimetric
Definition: oofGrMfc.h:39
Common set generator generates well-distributed colours.
Definition: oofDStyl.h:131
virtual ~oofGraphWindow()
Definition: oofGrMFC.cpp:88
int height()
Definition: oofRect.h:73
afx_msg void OnUpdateGraphFullSize(CCmdUI *pCmdUI)
Definition: oofGrMFC.cpp:377
#define ID_GRAPH_FULL_SIZE
Definition: oofRPRes.h:26
void setPageSetup(CPageSetupDialog *psd)
Definition: oofGrMFC.cpp:181
CMetaFileDC mhdc
Definition: oofGrMfc.h:36
CFont mDefaultFont
Definition: oofGrMfc.h:40
static BOOL getPaperSize(HANDLE hDevNames, HANDLE hDevMode, LPPOINT paperSize)
Definition: oofGrMFC.cpp:255
static BOOL setMappingMode(CDC *pDC)
Definition: oofGrMFC.cpp:210
oofGraph * mGraph
Definition: oofGrMfc.h:34
afx_msg void OnGraphScaleToFit()
Definition: oofGrMFC.cpp:358
afx_msg void OnFilePageSetup()
Definition: oofGrMFC.cpp:343
MFC base class for SDI and other frame windows that handle menus.
afx_msg void OnGraphFullSize()
Definition: oofGrMFC.cpp:370
MFC base class for a scrollable window.
static oofGraphWindow * CreateGraphWindow(oofGraph *inGraphPtr, CWnd *pParent, UINT nId=0, CPageSetupDialog *pageSetup=NULL)
Definition: oofGrMFC.cpp:104
virtual void OnInitialUpdate()
Definition: oofGrMFC.cpp:317
#define ID_GRAPH_SCALE_TO_FIT
Definition: oofRPRes.h:25