GME  13
GmePrintDialog.cpp
Go to the documentation of this file.
00001 // GmePrintDialog.cpp : implementation file
00002 //
00003 
00004 #include "stdafx.h"
00005 #include "gme.h"
00006 #include "GmePrintDialog.h"
00007 #include "resource.h"
00008 
00009 #ifdef _DEBUG
00010 #define new DEBUG_NEW
00011 #undef THIS_FILE
00012 static char THIS_FILE[] = __FILE__;
00013 #endif
00014 
00015 using namespace std;
00017 // CGmePrintDialog
00018 
00019 //IMPLEMENT_DYNAMIC(CGmePrintDialog, CPrintDialog)
00020 
00021 #define MAX_PRNPAGE     5
00022 CGmePrintDialog::CGmePrintDialog(CGMEView *view, CGuiMetaModel *guiMeta, BOOL bPrintSetupOnly, DWORD dwFlags, CWnd* pParentWnd) :
00023         CPrintDialog(bPrintSetupOnly, dwFlags, pParentWnd)
00024 {
00025         //{{AFX_DATA_INIT(CGmePrintDialog)
00026         m_noheader = FALSE;
00027         m_1page = TRUE; 
00028         m_autorotate = TRUE;
00029         m_scale = 1;
00030         //}}AFX_DATA_INIT
00031         m_portrait = true;
00032         m_autoPortrait = true;
00033         m_selasp = CUR_ASP;
00034         m_guiMeta = guiMeta;
00035         m_gmeview = view;
00036 }
00037 
00038 void CGmePrintDialog::DoDataExchange(CDataExchange* pDX)
00039 {
00040         CDialog::DoDataExchange(pDX);
00041         //{{AFX_DATA_MAP(CGmePrintDialog)
00042         DDX_Control(pDX, IDC_PREVIEWBTN, m_demo);
00043         DDX_Control(pDX, IDC_SCALETXT, m_scaletxt);
00044         DDX_Control(pDX, IDC_BTNSELECT, m_aspSelBtn);
00045         DDX_Control(pDX, IDC_BTNDESELECT, m_aspDeselBtn);
00046         DDX_Control(pDX, IDC_XLANDSCAPE, m_landBtn);
00047         DDX_Control(pDX, IDC_XPORTRAIT, m_portBtn);
00048         DDX_Control(pDX, IDC_CURASP1056, m_curraspBtn);
00049         DDX_Control(pDX, IDC_ALLASP1058, m_allaspBtn);
00050         DDX_Control(pDX, IDC_SELASP1057, m_selaspBtn);
00051         DDX_Control(pDX, IDC_CHKNOHEADER, m_noheaderBtn);
00052         DDX_Control(pDX, IDC_SLIDER2, m_scaleslider);
00053         DDX_Control(pDX, IDC_ASPECTSLIST, m_asplist);
00054         DDX_Check(pDX, IDC_CHKNOHEADER, m_noheader);
00055         DDX_Check(pDX, IDC_FITTO1PAGE, m_1page);
00056         DDX_Check(pDX, IDC_AUTOROTATE, m_autorotate);
00057         DDX_Slider(pDX, IDC_SLIDER2, m_scale);
00058         //}}AFX_DATA_MAP
00059 }
00060 
00061 
00062 BEGIN_MESSAGE_MAP(CGmePrintDialog, CPrintDialog)
00063         //{{AFX_MSG_MAP(CGmePrintDialog)
00064         ON_BN_CLICKED(IDC_BTNDESELECT, OnBtndeselect)
00065         ON_BN_CLICKED(IDC_BTNSELECT, OnBtnselect)
00066         ON_BN_CLICKED(IDC_XLANDSCAPE, OnXlandscape)
00067         ON_BN_CLICKED(IDC_XPORTRAIT, OnXportrait)
00068         ON_BN_CLICKED(IDC_CHKNOHEADER, OnChknoheader)
00069         ON_BN_CLICKED(IDC_FITTO1PAGE, OnFitto1page)
00070         ON_BN_CLICKED(IDC_AUTOROTATE, OnAutorotate)
00071         ON_BN_CLICKED(IDC_SELASP1057, OnSelasp1057)
00072         ON_BN_CLICKED(IDC_ALLASP1058, OnAllasp1058)
00073         ON_BN_CLICKED(IDC_CURASP1056, OnCurasp1056)
00074         ON_BN_CLICKED(IDC_REVERSE, OnReverse)
00075         ON_WM_HSCROLL()
00076         ON_WM_DEVMODECHANGE()
00077         //}}AFX_MSG_MAP
00078 END_MESSAGE_MAP()
00079 
00080 BOOL CGmePrintDialog::OnInitDialog() 
00081 {
00082         BOOL ret = CPrintDialog::OnInitDialog();
00083         m_landBtn.SetCheck(0);
00084         m_portBtn.SetCheck(1);
00085         m_curraspBtn.SetCheck(1);
00086         m_allaspBtn.SetCheck(0);
00087         m_selaspBtn.SetCheck(0);
00088         m_guiMeta->InitAspectList(&m_asplist);
00089         m_asplist.EnableWindow(FALSE);
00090         m_aspSelBtn.EnableWindow(FALSE);
00091         m_aspDeselBtn.EnableWindow(FALSE);
00092         m_landBtn.EnableWindow(FALSE);
00093         m_portBtn.EnableWindow(FALSE);
00094         m_scaleslider.EnableWindow(FALSE);
00095         m_scaletxt.EnableWindow(FALSE);
00096         m_scaleslider.SetRange(1, MAX_PRNPAGE);
00097         m_scaleslider.SetPos(1);
00098         m_scale = 1;
00099         RECT rect;
00100         m_demo.GetClientRect(&rect);
00101         m_previewrect = rect; 
00102         m_selAspects.clear();
00103 
00104         CDC* cdc = CreatePreviewBitmap();
00105         m_demo.SetDC(cdc);
00106         return ret;
00107 }
00108 
00109 
00110 void CGmePrintDialog::OnBtndeselect() 
00111 {
00112         m_asplist.SelItemRange(FALSE, 0, m_asplist.GetCount());
00113 }
00114 
00115 void CGmePrintDialog::OnBtnselect() 
00116 {
00117         m_asplist.SelItemRange(TRUE, 0, m_asplist.GetCount());
00118 }
00119 
00120 void CGmePrintDialog::OnReverse() 
00121 {
00122         for (int i=0; i < m_asplist.GetCount(); i++)
00123                 m_asplist.SetSel(i, m_asplist.GetSel(i) == 0);
00124 }
00125 
00126 void CGmePrintDialog::OnXlandscape() 
00127 {
00128         UpdateData(TRUE);
00129         m_portrait = false;
00130         m_landBtn.SetCheck(1);
00131         m_portBtn.SetCheck(0);
00132         m_selaspBtn.SetCheck(0);
00133         CDC* cdc = CreatePreviewBitmap();
00134         if (cdc)
00135         {
00136                 m_demo.SetDC(cdc);
00137                 m_demo.Invalidate();
00138                 m_demo.UpdateWindow();
00139         }
00140         // átrajzomni a preview-t
00141         UpdateData(FALSE);
00142 }
00143 
00144 void CGmePrintDialog::OnXportrait() 
00145 {
00146         UpdateData(TRUE);
00147         m_portrait = true;
00148         m_landBtn.SetCheck(0);
00149         m_portBtn.SetCheck(1);
00150         CDC* cdc = CreatePreviewBitmap();
00151         if (cdc)
00152         {
00153                 m_demo.SetDC(cdc);
00154                 m_demo.Invalidate();
00155                 m_demo.UpdateWindow();
00156         }
00157         // átrajzomni a preview-t
00158         UpdateData(FALSE);
00159 }
00160 
00161 #define x_min(x,y) ((x<y)? (x): (y))
00162 #define x_max(x,y) ((x>y)? (x): (y))
00163 #define THIRDINCH 84  // 1/10 mm
00164 CDC * CGmePrintDialog::CreatePreviewBitmap()
00165 {
00166         CRect bitRect = m_previewrect;
00167         // extent of graphics
00168         CRect extent, objext, annext;
00169         CGuiObject::GetExtent(m_gmeview->children,objext);
00170         CGuiAnnotator::GetExtent(m_gmeview->annotators,annext);
00171         extent.UnionRect(&objext, &annext);
00172         extent.right = (int)(extent.right*EXTENT_ERROR_CORR); // ??
00173         extent.bottom = (int)(extent.bottom*EXTENT_ERROR_CORR); // ??
00174         // papersize
00175         //      DEVMODE *lpdevm = GetDevMode();
00176         //      lpdevm->dmPaperLength // 1/10 mm
00177         //      lpdevm->dmPaperWidth // 1/10 mm
00178         //      double pWidth = lpdevm->dmPaperWidth;   // the smaller // x_min(lpdevm->dmPaperWidth, lpdevm->dmPaperLength);
00179         //      double pLength = lpdevm->dmPaperLength; // the longer // x_max(lpdevm->dmPaperWidth, lpdevm->dmPaperLength);
00180         // same sizes as in case of real printing
00181         HDC phdc = CreatePrinterDC();
00182         CDC dc;
00183         dc.Attach(phdc);
00184         double wmargin = GetDeviceCaps(phdc,PHYSICALOFFSETX);
00185         double hmargin = GetDeviceCaps(phdc,PHYSICALOFFSETY);
00186         double w = GetDeviceCaps(phdc,PHYSICALWIDTH) - 2*wmargin;
00187         double h = GetDeviceCaps(phdc,PHYSICALHEIGHT)- 2*hmargin;
00188         double pWidth = x_min(w, h);
00189         double pLength = x_max(w, h);
00190 
00191         double xdr, ydr;
00192         bool oriPortrait = m_portrait;
00193         if (m_autorotate) // rotate PAGE to fit drawing better to the page
00194         {   // orientation changes !! automatically - user cannot change
00195                 if (extent.Width() > extent.Height()) // rotate it
00196                 {
00197                         oriPortrait = false;
00198                         m_autoPortrait = false;
00199                 }
00200                 else
00201                 {
00202                         oriPortrait = true;
00203                         m_autoPortrait = true;
00204                 }
00205         }
00206         if (oriPortrait)
00207         {
00208                 xdr = m_scale*pWidth/m_previewrect.Width(); //  1/10 mm per pixel
00209                 ydr = m_scale*pLength/m_previewrect.Height(); //  1/10 mm per pixel
00210         }
00211         else // landscape
00212         {
00213                 xdr = m_scale*pLength/m_previewrect.Width(); //  1/10 mm per pixel
00214                 ydr = m_scale*pWidth/m_previewrect.Height(); //  1/10 mm per pixel
00215         }
00216         
00217         if (xdr >= ydr)
00218                 bitRect.bottom = (int)((double)m_previewrect.bottom*ydr/xdr);
00219         else // ydr > xdr
00220                 bitRect.right = (int)((double)m_previewrect.right*xdr/ydr);
00221 
00222 //      m_demo.SetWindowPos(NULL, 0, 0, bitRect.Width(), bitRect.Height(), 
00223 //                                      SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOMOVE|SWP_NOOWNERZORDER|SWP_NOREDRAW|SWP_NOZORDER);
00224 
00225         CDC*  peviewDC = new CDC();
00226         CClientDC demoDC(&m_demo);
00227 
00228         // TODO: Error checking
00229         peviewDC->CreateCompatibleDC(&demoDC);
00230         CBitmap * prevBmp = new CBitmap();
00231 
00232 
00233         // TODO: Error checking
00234         prevBmp->CreateCompatibleBitmap(&demoDC, bitRect.Width(), bitRect.Height());
00235         peviewDC->SelectObject(prevBmp);
00236 
00237         // set background
00238         DWORD dw1 = GetSysColor(COLOR_WINDOW);
00239         BYTE r1 = GetRValue(dw1);
00240         BYTE g1 = GetGValue(dw1); 
00241         BYTE b1 = GetBValue(dw1); 
00242         peviewDC->SetMapMode(MM_TEXT);
00243         peviewDC->FillSolidRect(&bitRect, RGB(r1,g1,b1));
00244 
00245         // draw papes
00246         DWORD pgdw = GetSysColor(COLOR_ACTIVECAPTION);
00247         BYTE pr1 = GetRValue(pgdw);
00248         BYTE pg1 = GetGValue(pgdw); 
00249         BYTE pb1 = GetBValue(pgdw); 
00250         CPoint currp = peviewDC->GetCurrentPosition();
00251         CPen pagePen(PS_SOLID, 1, RGB(pr1, pg1, pb1));
00252         CPen *oriPen = peviewDC->SelectObject(&pagePen);
00253         for (int p=1; p<m_scale; p++)
00254         { 
00255 
00256                 int l = (int)((double)bitRect.Height()/m_scale*p);
00257                 peviewDC->MoveTo(bitRect.left, bitRect.top + l);
00258                 peviewDC->LineTo(bitRect.right, bitRect.top + l);
00259                 int b = (int)((double)bitRect.Width()/m_scale*p);
00260                 peviewDC->MoveTo(bitRect.left + b, bitRect.top);
00261                 peviewDC->LineTo(bitRect.left + b, bitRect.bottom);
00262         }
00263         peviewDC->MoveTo(currp);
00264         peviewDC->SelectObject(oriPen);
00265 
00266         // 1/3 inch for header, draw rectangle, make the paper shorter with 1/3 inch 
00267         // 1 inch = 254 1/10 mm -> 1/3 inch ~= 84 1/10 mm
00268         if (!m_noheader)
00269         {
00270                 CBrush brush;
00271                 DWORD dw = GetSysColor(COLOR_WINDOWTEXT);
00272                 BYTE r = GetRValue(dw);
00273                 BYTE g = GetGValue(dw); 
00274                 BYTE b = GetBValue(dw); 
00275                 brush.CreateHatchBrush(HS_BDIAGONAL, RGB(r,g,b));
00276                 peviewDC->SetMapMode(MM_TEXT);
00277                 for (int line=0; line<m_scale; line++)
00278                 {
00279                         for (int col=0; col<m_scale; col++)
00280                         {
00281                                 CRect header;
00282                                 header.left = bitRect.left + bitRect.Width()/m_scale*col;
00283                                 header.right = header.left + bitRect.Width()/m_scale;
00284                                 header.top = bitRect.top + bitRect.Height()/m_scale*line;
00285                                 header.bottom = header.top + (int)(THIRDINCH/ydr);
00286                                 peviewDC->FillRect(&header, &brush);
00287                         }
00288                 }
00289                 bitRect.top += m_scale * (int)(THIRDINCH/ydr); 
00290         }
00291 
00292         m_gmeview->SetPreview(true);
00293         peviewDC->SetMapMode(MM_ISOTROPIC);
00294         // skip the header - written in an other mapping mode
00295         int z = (int)((double)extent.bottom /bitRect.Height() *bitRect.top);
00296         peviewDC->SetWindowExt(extent.right, extent.bottom +z);
00297         peviewDC->SetViewportExt((int)bitRect.Width(), (int)bitRect.Height() +bitRect.top);
00298         peviewDC->SetWindowOrg(0,-z);
00299 
00300         // test
00301 /*      CBrush brush1;
00302         DWORD dw = GetSysColor(COLOR_ACTIVECAPTION);
00303         BYTE r = GetRValue(dw);
00304         BYTE g = GetGValue(dw); 
00305         BYTE b = GetBValue(dw); 
00306         brush1.CreateHatchBrush(HS_CROSS, RGB(r,g,b));
00307         peviewDC->FillRect(&extent, &brush1);
00308 */
00309         m_gmeview->OnDraw(peviewDC); // allways shows the current Aspect in the preview
00310         m_gmeview->SetPreview(false);
00311         peviewDC->SetWindowOrg(0,0);
00312 
00313         return peviewDC;
00314 }
00315 
00316 void CGmePrintDialog::OnChknoheader() 
00317 {
00318         UpdateData(TRUE);
00319         CDC* cdc = CreatePreviewBitmap();
00320         if (cdc)
00321         {
00322                 m_demo.SetDC(cdc);
00323                 m_demo.Invalidate();
00324                 m_demo.UpdateWindow();
00325         }
00326         UpdateData(FALSE);
00327 }
00328 
00329 
00330 void CGmePrintDialog::OnFitto1page() 
00331 {
00332         UpdateData(TRUE);
00333         if (m_1page)
00334         {
00335                 m_scale = 1;
00336                 m_scaleslider.EnableWindow(FALSE);
00337                 m_scaletxt.EnableWindow(FALSE);
00338                 m_noheaderBtn.EnableWindow(TRUE);
00339         }
00340         else
00341         {
00342                 m_scaleslider.EnableWindow(TRUE);
00343                 m_scaletxt.EnableWindow(TRUE);
00344                 m_noheader = true;
00345                 m_noheaderBtn.EnableWindow(FALSE);
00346         }
00347         CDC* cdc = CreatePreviewBitmap();
00348         if (cdc)
00349         {
00350                 m_demo.SetDC(cdc);
00351                 m_demo.Invalidate();
00352                 m_demo.UpdateWindow();
00353         }
00354         UpdateData(FALSE);
00355 }
00356 
00357 void CGmePrintDialog::OnAutorotate() 
00358 {
00359         UpdateData(TRUE);
00360         if (m_autorotate)
00361         {
00362                 m_portrait = true;
00363                 m_landBtn.SetCheck(0);
00364                 m_portBtn.SetCheck(1);
00365                 m_landBtn.EnableWindow(FALSE);
00366                 m_portBtn.EnableWindow(FALSE);
00367         }
00368         else
00369         {
00370                 m_landBtn.EnableWindow(TRUE);
00371                 m_portBtn.EnableWindow(TRUE);
00372         }
00373         CDC* cdc = CreatePreviewBitmap();
00374         if (cdc)
00375         {
00376                 m_demo.SetDC(cdc);
00377                 m_demo.Invalidate();
00378                 m_demo.UpdateWindow();
00379         }
00380         UpdateData(FALSE);
00381 }
00382 
00383 
00384 void CGmePrintDialog::OnSelasp1057() 
00385 {
00386         m_selasp = SEL_ASP;     
00387 
00388         UpdateData(TRUE);
00389         m_curraspBtn.SetCheck(0);
00390         m_allaspBtn.SetCheck(0);
00391         m_selaspBtn.SetCheck(1);
00392         m_asplist.EnableWindow(TRUE);
00393         m_aspSelBtn.EnableWindow(TRUE);
00394         m_aspDeselBtn.EnableWindow(TRUE);
00395         UpdateData(FALSE);
00396 }
00397 
00398 void CGmePrintDialog::OnAllasp1058() 
00399 {
00400         m_selasp = ALL_ASP;     
00401 
00402         UpdateData(TRUE);
00403         m_curraspBtn.SetCheck(0);
00404         m_allaspBtn.SetCheck(1);
00405         m_selaspBtn.SetCheck(0);
00406         m_asplist.EnableWindow(FALSE);
00407         m_aspSelBtn.EnableWindow(FALSE);
00408         m_aspDeselBtn.EnableWindow(FALSE);
00409         UpdateData(FALSE);
00410 }
00411 
00412 void CGmePrintDialog::OnCurasp1056() 
00413 {
00414         m_selasp = ALL_ASP;     
00415 
00416         UpdateData(TRUE);
00417         m_curraspBtn.SetCheck(1);
00418         m_allaspBtn.SetCheck(0);
00419         m_selaspBtn.SetCheck(0);
00420         m_asplist.EnableWindow(FALSE);
00421         m_aspSelBtn.EnableWindow(FALSE);
00422         m_aspDeselBtn.EnableWindow(FALSE);
00423         UpdateData(FALSE);
00424 }
00425 
00426 
00427 void CGmePrintDialog::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
00428 {
00429         CPrintDialog::OnHScroll(nSBCode, nPos, pScrollBar);
00430 
00431         if (m_scaleslider.m_hWnd == pScrollBar->m_hWnd)
00432         {
00433                 UpdateData(TRUE);
00434                 CDC* cdc = CreatePreviewBitmap();
00435                 if (cdc)
00436                 {
00437                         m_demo.SetDC(cdc);
00438                         m_demo.Invalidate();
00439                         m_demo.UpdateWindow();
00440                 }
00441                 UpdateData(FALSE);
00442         }
00443 }
00444 
00445 
00446 bool CGmePrintDialog::IsSelectedAspect(CString aspect)
00447 { // if no selected, answers all of them selected
00448         if (m_selasp == CUR_ASP)
00449                 return true;
00450         AspectList::iterator it = m_selAspects.find(aspect);
00451         return (it != m_selAspects.end());      
00452 }
00453 
00454 
00455 void CGmePrintDialog::OnOK() 
00456 {
00457         // save m_asplist
00458         m_selAspects.clear();
00459         int sel = m_asplist.GetSelCount();
00460         bool all = false;
00461         if ((m_selasp == SEL_ASP  &&  sel == 0)  ||  m_selasp == ALL_ASP)
00462                 all = true;
00463 
00464         if (m_selasp != CUR_ASP)
00465         { // copy 
00466                 int count = m_asplist.GetCount();
00467                 for (int i=0; i<count; i++)
00468                 {
00469                         if (all ||  m_asplist.GetSel(i)>0)
00470                         {
00471                                 CString str;
00472                                 m_asplist.GetText(i, str);              
00473                                 m_selAspects.insert(str);
00474                         }
00475                 }
00476         }
00477 
00478         CPrintDialog::OnOK();
00479 }
00480 
00481 BOOL CGmePrintDialog::OnCommand(WPARAM wParam, LPARAM lParam) 
00482 {
00483         // update preview if the printer settings changed 
00484         CWnd* win = GetDlgItem(1154);
00485         if (win  &&  IsWindowVisible()  &&
00486                   wParam == MAKEWPARAM((WORD)1154, (WORD)EN_UPDATE)  &&
00487                   (HWND)lParam == win->m_hWnd)
00488         {
00489                 UpdateData(TRUE);
00490                 CDC* cdc = CreatePreviewBitmap();
00491                 if (cdc)
00492                 {
00493                         m_demo.SetDC(cdc);
00494                         m_demo.Invalidate();
00495                         m_demo.UpdateWindow();
00496                 }
00497                 UpdateData(FALSE);
00498         }
00499         return CPrintDialog::OnCommand(wParam, lParam);
00500 }