|
|
1.1 root 1: //--------------------------------------------------------------------
2: // File: View.Cxx
3: //
4: // Classes:
5: // CClientCanvas - Device context for client area
6: // CPrintCanvas - Device context for printing
7: //
8: // CView - Base class for Views
9: // CScrollaleView - Scrollable View
10: // CFullPageView - NonScrollable FullPage View
11: //
12: // History: 22-Jan-1993 Asmusf Created
13: //
14: // Copyright (c) 1993 Microsoft Corp. All Rights reserved
15: //
16: //--------------------------------------------------------------------
17: #include <windows.h>
18: #include <windowsx.h>
19: #include <commdlg.h>
20: #include "app.h"
21: #include "view.hxx"
22: #include "grid.hxx"
23:
24: //+--------------------------------------------------------
25: // Class: CView
26: //
27: // Purpose: Standard fixed View for printing, or base class
28: //
29: // History: 22-Jan-1993 asmusf created
30: //----------------------------------------------------------
31: CView::CView(int cx, int cy)
32: {
33: _iScale = 100; // percentage magnification
34: _ptOrg.x = 0; // 0..(PAGEWIDTH - widht of window)
35: _ptOrg.y = 0; // 0..(PAGEHEIGHT - height of window)
36: _ptScroll.x = 0; // 0..PAGEWIDTH
37: _ptScroll.y = 0; // 0..PAGEHEIGHT
38: _size.cx = cx; // width of window (dev. coord)
39: _size.cy = cy; // widht of window (dev. coord)
40: }
41:
42: void CView::SetSize( int cx, int cy)
43: {
44: _size.cx = cx;
45: _size.cy = cy;
46: }
47:
48: void CView::Invalidate(HWND hwnd, LPRECT lpRect)
49: {
50: InvalidateRect(hwnd, lpRect, TRUE);
51: }
52:
53: BOOL CView::Paint(CCanvas& canvas, CModel * _pModel, RECT rc)
54: {
55: // Scale using MapMode = logical TWIPS
56: canvas.Scale(_iScale);
57:
58: // Client rect in logical coordinates
59: _ptClient.x=_size.cx;
60: _ptClient.y=_size.cy;
61: canvas.DPtoLP(&_ptClient);
62:
63: // Adjust GDIwindow origin, set _ptOrg
64: Scroll(canvas);
65:
66: // ClipRectangle in logical coordinates
67: POINT pt;
68: pt.x = rc.left; pt.y = rc.top;
69: canvas.DPtoLP(&pt);
70: rc.left = pt.x; rc.top = pt.y;
71:
72: pt.x = rc.right; pt.y = rc.bottom;
73: canvas.DPtoLP(&pt);
74: rc.right = pt.x; rc.bottom = pt.y;
75:
76:
77: // Paint
78: _pModel->Paint(canvas, rc);
79:
80: return TRUE; // screen Paints don't fail
81: }
82:
83: UINT CView::Hittest(CCanvas& canvas, POINT ptTest, CModel * _pModel)
84: {
85: // Scale using MapMode = logical TWIPS
86: canvas.Scale(_iScale);
87:
88: // Client rect in logical coordinates
89: _ptClient.x=_size.cx;
90: _ptClient.y=_size.cy;
91: canvas.DPtoLP(&_ptClient);
92:
93: // Adjust GDIwindow origin, set _ptOrg
94: Scroll(canvas);
95:
96: // Hittest
97:
98: canvas.DPtoLP(&ptTest);
99: return _pModel->Hittest( ptTest);
100: }
101:
102: void CView::Scroll(CCanvas &canvas)
103: {
104: int dx=MulDiv(_ptClient.x,_ptScroll.x,PAGEWIDTH);
105: int dy=MulDiv(_ptClient.y,_ptScroll.y,PAGEHEIGHT);
106:
107: // Adjust GDIwindow origin, set _ptOrg
108: _ptOrg.x = _ptScroll.x-dx;
109: _ptOrg.y = _ptScroll.y-dy;
110:
111: canvas.Scroll(_ptOrg.x, _ptOrg.y);
112: }
113:
114: //+--------------------------------------------------------
115: // Class: CScrollableView
116: //
117: // Purpose: View for scrollable window
118: //
119: // History: 22-Jan-1993 asmusf created
120: //----------------------------------------------------------
121: void CScrollableView::SetHScrollPos(HWND hwnd, WPARAM wParam, LPARAM lParam, CModel * pModel)
122: {
123: CScreenCanvas canvas(hwnd);
124: int nScrollPos = GetScrollPos( hwnd, SB_HORZ );
125: canvas.Scale(_iScale);
126:
127:
128: SIZE size;
129: pModel->GetLineSize(&size);
130:
131: // scroll position relative to page dimension
132: switch (wParam)
133: {
134: case SB_LINEUP:
135: nScrollPos -= 2 * size.cx;
136: break;
137: case SB_LINEDOWN:
138: nScrollPos += 2 * size.cx;
139: break;
140:
141: case SB_PAGEUP:
142: nScrollPos -= _ptClient.x; // size of Window in log coord
143: break;
144:
145: case SB_PAGEDOWN:
146: nScrollPos += _ptClient.x;
147: break;
148:
149: case SB_THUMBPOSITION:
150: nScrollPos = LOWORD(lParam);
151: break;
152:
153: default:
154: break;
155: }
156:
157: nScrollPos = max( 0, min (nScrollPos, PAGEWIDTH));
158:
159: POINT pt;
160: pt.x = GetScrollPos( hwnd, SB_HORZ ) - nScrollPos;
161: if( pt.x )
162: {
163: SetScrollPos(hwnd, SB_HORZ, nScrollPos, TRUE );
164:
165: pt.y = 0;
166: canvas.LPtoDP(&pt);
167:
168: pt.x -= MulDiv( _ptClient.x, pt.x, PAGEWIDTH);
169: ScrollWindow(hwnd, pt.x, pt.y, NULL, NULL );
170: }
171: _ptScroll.x = nScrollPos;
172: }
173:
174: void CScrollableView::SetVScrollPos(HWND hwnd, WPARAM wParam, LPARAM lParam, CModel * pModel)
175: {
176: CScreenCanvas canvas(hwnd);
177: int nScrollPos = GetScrollPos( hwnd, SB_VERT );
178: canvas.Scale(_iScale);
179:
180:
181: SIZE size;
182: pModel->GetLineSize(&size);
183:
184: // scroll position relative to page dimension
185: switch (wParam)
186: {
187: case SB_LINEUP:
188: nScrollPos -= 2 * size.cy;
189: break;
190: case SB_LINEDOWN:
191: nScrollPos += 2 * size.cy;
192: break;
193:
194: case SB_PAGEUP:
195: nScrollPos -= _ptClient.y; // size of Window in log coord
196: break;
197:
198: case SB_PAGEDOWN:
199: nScrollPos += _ptClient.y;
200: break;
201:
202: case SB_THUMBPOSITION:
203: nScrollPos = LOWORD(lParam);
204: break;
205:
206: default:
207: break;
208: }
209:
210: nScrollPos = max( 0, min (nScrollPos, PAGEHEIGHT));
211:
212: POINT pt;
213: pt.y = GetScrollPos( hwnd, SB_VERT ) - nScrollPos;
214: if( pt.y )
215: {
216: SetScrollPos(hwnd, SB_VERT, nScrollPos, TRUE );
217:
218: pt.x = 0;
219: canvas.LPtoDP(&pt);
220:
221: pt.y -= MulDiv( _ptClient.y, pt.y, PAGEHEIGHT);
222: ScrollWindow(hwnd, pt.x, pt.y, NULL, NULL );
223: }
224: _ptScroll.y = nScrollPos;
225: }
226:
227: //+--------------------------------------------------------
228: // Class: CPrintCanvas
229: //
230: // Purpose: Device Context for Printing
231: //
232: // History: 22-Jan-1993 asmusf created
233: //----------------------------------------------------------
234:
235: // fully inline //
236:
237: //+--------------------------------------------------------
238: // Class: CPrintRequest
239: //
240: // Purpose: Print Job selection and parameters
241: //
242: // History: 22-Jan-1993 asmusf created
243: //----------------------------------------------------------
244: CPrintAux PrGlobal; // global flags for printing control
245:
246: CPrintRequest::CPrintRequest(HWND hwnd, UINT nMinPage, UINT nMaxPage ) :
247: _hwnd(hwnd),
248: _hdc(0),
249: _hDevNames(0)
250: {
251: PRINTDLG pd;
252:
253: pd.lStructSize= sizeof(PRINTDLG);
254: pd.hwndOwner=NULL;
255: pd.hDevMode=NULL;
256: pd.hDevNames=NULL;
257: pd.hDC;
258: pd.nFromPage = 0xFFFF; // cause initial blank field
259: pd.nToPage = 0xFFFF; // in dialog
260: pd.nMinPage=nMinPage;
261: pd.nMaxPage=nMaxPage;
262: pd.nCopies=1;
263: pd.hInstance=NULL;
264: pd.lCustData=0L;
265: pd.lpfnPrintHook=NULL;
266: pd.lpfnSetupHook=NULL;
267: pd.lpPrintTemplateName=NULL;
268: pd.lpSetupTemplateName=NULL;
269: pd.hPrintTemplate=NULL;
270: pd.hSetupTemplate=NULL;
271:
272: pd.Flags = PD_RETURNDEFAULT;
273:
274: if( PrintDlg(&pd) )
275: {
276: pd.Flags = PD_SELECTION | PD_HIDEPRINTTOFILE | PD_RETURNDC;
277: if( PrintDlg( &pd ) )
278: {
279: if(pd.Flags & PD_PAGENUMS)
280: {
281: _nFromPage = pd.nFromPage;
282: _nToPage = pd.nToPage;
283: }
284: else if (pd.Flags & PD_ALLPAGES )
285: {
286: _nFromPage = nMinPage;
287: _nToPage = nMaxPage;
288: }
289: else // Selection
290: {
291: _nFromPage = _nToPage = 0xFFFF;
292: }
293: _hdc = pd.hDC;
294: _hDevNames = pd.hDevNames;
295:
296: if( pd.hDevMode )
297: {
298: GlobalFree (pd.hDevMode);
299: }
300: _status = PREQ_SUCCESS;
301: return;
302: }
303: else
304: {
305: _status = PREQ_ERROR;
306: return;
307: }
308: }
309:
310: _status = PREQ_CANCEL;
311: }
312:
313: CPrintRequest:: ~CPrintRequest()
314: {
315: if( _hDevNames )
316: {
317: GlobalFree( _hDevNames );
318: }
319:
320: if( _hdc )
321: {
322: DeleteDC(_hdc );
323: }
324: }
325:
326: UINT CPrintRequest::Print(HINSTANCE hInst, CCanvas &canvas, CModel *pModel)
327: {
328: // needs to become app & font name....
329: static TCHAR szMessage [] = TEXT("Grid: Printing") ;
330:
331: TCHAR szCaption[30];
332:
333: BOOL fError = PREQ_SUCCESS ;
334: ABORTPROC lpfnAbortProc;
335: DLGPROC lpfnPrintDlgProc ;
336: short cxPage, cyPage ;
337:
338: cxPage = GetDeviceCaps (canvas, HORZRES) ;
339: cyPage = GetDeviceCaps (canvas, VERTRES) ;
340:
341: // disable user input to main window
342: EnableWindow (_hwnd, FALSE) ;
343:
344: lpfnPrintDlgProc = (DLGPROC) MakeProcInstance ((FARPROC) PrintDlgProc, hInst) ;
345:
346: LoadString(hInst, IDS_MSGCAPTION, szCaption, 30);
347: PrGlobal._bUserAbort = FALSE ;
348: PrGlobal._hDlgPrint =
349: CreateDialogParam (hInst, TEXT("PrintDlgBox"), _hwnd,
350: lpfnPrintDlgProc, (LONG)(LPSTR) szCaption) ;
351:
352: lpfnAbortProc = (ABORTPROC) MakeProcInstance ((FARPROC) AbortProc, hInst) ;
353:
354: SetAbortProc(canvas, lpfnAbortProc);
355:
356: DOCINFO docinfo;
357: docinfo.cbSize = sizeof(DOCINFO);
358: docinfo.lpszDocName = szMessage;
359: docinfo.lpszOutput = NULL;
360:
361: if (Escape (canvas, STARTDOC, sizeof szMessage - 1, (LPCSTR)szMessage, NULL) > 0)
362: //if (StartDoc(canvas, &docinfo) > 0)
363: {
364: UINT fuFormat;
365: pModel->GetFormat(fuFormat);
366: pModel->SetFormat(fuFormat | PAGEPRINT|PAGEELEMS);
367: UINT iSavePage = pModel->GetPage();
368:
369:
370: // create printer view to
371: CView View(cxPage, cyPage);
372:
373:
374: if( _nToPage == 0xFFFF || _nFromPage == 0xFFFF )
375: {
376: _nToPage = pModel->GetPage()+1;
377: _nFromPage = pModel->GetPage()+1;
378: }
379:
380: for( ; _nFromPage <= _nToPage && ! fError ; _nFromPage++ )
381: {
382: if( StartPage(canvas) < 0 )
383: {
384: fError = PREQ_ERRSTARTPAGE ;
385: break;
386: }
387: pModel->SetPage(_nFromPage-1); // <- pages are 0 based in Model
388: RECT rc;
389: rc.left = rc.top = 0;
390: rc.right= cxPage;
391: rc.bottom = cyPage;
392: View.Paint (canvas, pModel, rc) ;
393:
394: if ( EndPage(canvas) < 0)
395: {
396: fError = PREQ_ERRENDPAGE ;
397: }
398: }
399: if( !fError )
400: {
401: EndDoc(canvas);
402: }
403: // restore screen format
404: pModel->SetPage(iSavePage);
405: pModel->SetFormat(fuFormat);
406: }
407: else
408: {
409: fError = PREQ_ERRSTARTDOC ;
410: }
411:
412: if (!PrGlobal._bUserAbort)
413: {
414: EnableWindow (_hwnd, TRUE) ;
415: DestroyWindow (PrGlobal._hDlgPrint) ;
416: }
417:
418: FreeProcInstance ((FARPROC)lpfnPrintDlgProc) ;
419: FreeProcInstance ((FARPROC)lpfnAbortProc) ;
420:
421: if( PrGlobal._bUserAbort )
422: {
423: fError |= PREQ_USERABORT;
424: }
425:
426: return fError ;
427: }
428:
429:
430:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.