home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap17 / patron / pagewin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  15.8 KB  |  588 lines

  1. /*
  2.  * PAGEWIN.CPP
  3.  * Patron Chapter 17
  4.  *
  5.  * Window procedure for the Pages window and support functions.
  6.  * This window manages its own scrollbars and viewport and provides
  7.  * printing capabilities as well.
  8.  *
  9.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Microsoft
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "patron.h"
  18.  
  19.  
  20. /*
  21.  * PagesWndProc
  22.  *
  23.  * Purpose:
  24.  *  Window procedure for the Pages window.
  25.  */
  26.  
  27. LRESULT APIENTRY PagesWndProc(HWND hWnd, UINT iMsg, WPARAM wParam
  28.     , LPARAM lParam)
  29.     {
  30.     PCPages         ppg;
  31.     PAINTSTRUCT     ps;
  32.     HDC             hDC;
  33.     int             iPos, iTmp;
  34.     int             iMin, iMax;
  35.     UINT            idScroll;
  36.     BOOL            fDirty=FALSE;
  37.  
  38.     ppg=(PCPages)GetWindowLong(hWnd, PAGEWL_STRUCTURE);
  39.  
  40.     switch (iMsg)
  41.         {
  42.         case WM_CREATE:
  43.             ppg=(PCPages)((LPCREATESTRUCT)lParam)->lpCreateParams;
  44.             SetWindowLong(hWnd, PAGEWL_STRUCTURE, (LONG)ppg);
  45.  
  46.             ppg->m_hWnd=hWnd;
  47.             break;
  48.  
  49.  
  50.         case WM_PAINT:
  51.             /*
  52.              * If there is currently a drag-rectangle showing, then
  53.              * remove it before painting.  This insures that
  54.              * painting doesn't blast part of that rectangle away so
  55.              * that when we draw it next, garbage is left around.
  56.              */
  57.             if (ppg->m_fDragRectShown)
  58.                 ppg->DrawDropTargetRect(NULL, NULL);
  59.  
  60.             hDC=BeginPaint(hWnd, &ps);
  61.  
  62.             //Draw only if we have a page to show.
  63.             if (0!=ppg->m_cPages)
  64.                 ppg->Draw(hDC, FALSE, FALSE);
  65.  
  66.             EndPaint(hWnd, &ps);
  67.  
  68.             //Turn the rectangle back on, if necessary.
  69.             if (ppg->m_fDragRectShown)
  70.                 ppg->DrawDropTargetRect(NULL, NULL);
  71.             break;
  72.  
  73.  
  74.         case WM_HSCROLL:
  75.         case WM_VSCROLL:
  76.             idScroll=(WM_HSCROLL==iMsg) ? SB_HORZ : SB_VERT;
  77.  
  78.             iPos=GetScrollPos(hWnd, idScroll);
  79.             iTmp=iPos;
  80.             GetScrollRange(hWnd, idScroll, &iMin, &iMax);
  81.  
  82.             switch (LOWORD(wParam))
  83.                 {
  84.                 case SB_LINEUP:     iPos -= 20;  break;
  85.                 case SB_PAGEUP:     iPos -=100;  break;
  86.                 case SB_LINEDOWN:   iPos += 20;  break;
  87.                 case SB_PAGEDOWN:   iPos +=100;  break;
  88.  
  89.                 case SB_THUMBPOSITION:
  90.                     iPos=ScrollThumbPosition(wParam, lParam);
  91.                     break;
  92.  
  93.                 //We don't want scrolling on this message.
  94.                 case SB_THUMBTRACK:
  95.                     return 0L;
  96.                 }
  97.  
  98.             iPos=max(iMin, min(iPos, iMax));
  99.  
  100.             if (iPos!=iTmp)
  101.                 {
  102.                 //Set the new position and scroll the window
  103.                 SetScrollPos(hWnd, idScroll, iPos, TRUE);
  104.  
  105.                 if (SB_HORZ==idScroll)
  106.                     {
  107.                     ppg->m_xPos=iPos;
  108.                     ScrollWindow(hWnd, iTmp-iPos, 0, NULL, NULL);
  109.                     }
  110.                 else
  111.                     {
  112.                     ppg->m_yPos=iPos;
  113.                     ScrollWindow(hWnd, 0, iTmp-iPos, NULL, NULL);
  114.                     }
  115.                 }
  116.  
  117.             break;
  118.  
  119.         //CHAPTER17MOD
  120.         case WM_RBUTTONDOWN:
  121.             if (NULL==ppg->m_pPageCur)
  122.                 break;
  123.  
  124.             fDirty=ppg->m_pPageCur->OnRightDown(wParam
  125.                 , LOWORD(lParam), HIWORD(lParam));
  126.             break;
  127.         //End CHAPTER17MOD
  128.  
  129.         case WM_LBUTTONDOWN:
  130.             if (NULL==ppg->m_pPageCur)
  131.                 break;
  132.  
  133.             fDirty=ppg->m_pPageCur->OnLeftDown(wParam
  134.                 , LOWORD(lParam), HIWORD(lParam));
  135.             break;
  136.  
  137.         case WM_LBUTTONUP:
  138.             if (NULL==ppg->m_pPageCur)
  139.                 break;
  140.  
  141.             fDirty=ppg->m_pPageCur->OnLeftUp(wParam
  142.                 , LOWORD(lParam), HIWORD(lParam));
  143.             break;
  144.  
  145.         case WM_LBUTTONDBLCLK:
  146.             if (NULL==ppg->m_pPageCur)
  147.                 break;
  148.  
  149.             fDirty=ppg->m_pPageCur->OnLeftDoubleClick(wParam, LOWORD(lParam)
  150.                 , HIWORD(lParam));
  151.             break;
  152.  
  153.         case WM_MOUSEMOVE:
  154.             if (NULL==ppg->m_pPageCur)
  155.                 break;
  156.  
  157.             ppg->m_pPageCur->OnMouseMove(wParam, LOWORD(lParam)
  158.                 , HIWORD(lParam));
  159.             break;
  160.  
  161.         case WM_TIMER:
  162.             if (NULL==ppg->m_pPageCur)
  163.                 break;
  164.  
  165.             ppg->m_pPageCur->OnTimer(wParam);
  166.             break;
  167.  
  168.         case WM_NCHITTEST:
  169.             if (NULL!=ppg->m_pPageCur)
  170.                 {
  171.                 /*
  172.                  * This just saves information in the page for
  173.                  * OnSetCursor
  174.                  */
  175.                 ppg->m_pPageCur->OnNCHitTest(LOWORD(lParam)
  176.                     , HIWORD(lParam));
  177.                 }
  178.  
  179.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  180.  
  181.         case WM_SETCURSOR:
  182.             if (NULL!=ppg->m_pPageCur)
  183.                 {
  184.                 if (ppg->m_pPageCur->OnSetCursor(LOWORD(lParam)))
  185.                     break;
  186.                 }
  187.  
  188.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  189.  
  190.  
  191.         default:
  192.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  193.         }
  194.  
  195.     ppg->m_fDirty |= fDirty;
  196.     return 0L;
  197.     }
  198.  
  199.  
  200.  
  201. /*
  202.  * RectConvertMappings
  203.  *
  204.  * Purpose:
  205.  *  Converts the contents of a rectangle from device to logical
  206.  *  coordinates where the hDC defines the logical coordinates.
  207.  *
  208.  * Parameters:
  209.  *  pRect           LPRECT containing the rectangle to convert.
  210.  *  hDC             HDC describing the logical coordinate system.
  211.  *                  if NULL, uses a screen DC in MM_LOMETRIC.
  212.  *  fToDevice       BOOL TRUE to convert from uConv to device,
  213.  *                  FALSE to convert device to uConv.
  214.  *
  215.  * Return Value:
  216.  *  None
  217.  */
  218.  
  219. void RectConvertMappings(LPRECT pRect, HDC hDC, BOOL fToDevice)
  220.     {
  221.     POINT   rgpt[2];
  222.     BOOL    fSysDC=FALSE;
  223.  
  224.     if (NULL==pRect)
  225.         return;
  226.  
  227.     rgpt[0].x=pRect->left;
  228.     rgpt[0].y=pRect->top;
  229.     rgpt[1].x=pRect->right;
  230.     rgpt[1].y=pRect->bottom;
  231.  
  232.     if (NULL==hDC)
  233.         {
  234.         hDC=GetDC(NULL);
  235.         SetMapMode(hDC, MM_LOMETRIC);
  236.         fSysDC=TRUE;
  237.         }
  238.  
  239.     if (fToDevice)
  240.         LPtoDP(hDC, rgpt, 2);
  241.     else
  242.         DPtoLP(hDC, rgpt, 2);
  243.  
  244.     if (fSysDC)
  245.         ReleaseDC(NULL, hDC);
  246.  
  247.     pRect->left=rgpt[0].x;
  248.     pRect->top=rgpt[0].y;
  249.     pRect->right=rgpt[1].x;
  250.     pRect->bottom=rgpt[1].y;
  251.  
  252.     return;
  253.     }
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. /*
  261.  * CPages::Draw
  262.  *
  263.  * Purpose:
  264.  *  Paints the current page in the pages window.
  265.  *
  266.  * Parameters:
  267.  *  hDC             HDC to draw on, could be a metafile or printer
  268.  *                  DC or any other type of DC.
  269.  *  fNoColor        BOOL indicating if we should use screen colors
  270.  *                  or printer colors (B&W).  Objects are printed
  271.  *                  as-is, however.  This is TRUE for printer DCs
  272.  *                  or print preview.
  273.  *  fPrinter        BOOL indicating if this is a printer DC in which
  274.  *                  case we eliminate some of the fancy drawing,
  275.  *                  like shadows on the page and so forth.
  276.  *
  277.  * Return Value:
  278.  *  None
  279.  */
  280.  
  281. void CPages::Draw(HDC hDC, BOOL fNoColor, BOOL fPrinter)
  282.     {
  283.     RECT            rc, rcT;
  284.     UINT            uMM;
  285.     HPEN            hPen;
  286.     HBRUSH          hBrush;
  287.     HGDIOBJ         hObj1, hObj2;
  288.     COLORREF        cr;
  289.     TCHAR           szTemp[20];
  290.     UINT            cch;
  291.     SIZE            sz;
  292.     PCPage          pPage;
  293.     RECT            rcPos;
  294.  
  295.     //Make sure the DC is in LOMETRIC
  296.     uMM=SetMapMode(hDC, MM_LOMETRIC);
  297.  
  298.     if (!fPrinter)
  299.         {
  300.         /*
  301.          * We maintain a 6mm border around the page on the screen
  302.          * besides 12.7mm margins.  We also have to account for
  303.          * the scroll position with m_*Pos which are in pixels so
  304.          * we have to convert them.
  305.          */
  306.  
  307.         SetRect(&rcPos, m_xPos, m_yPos, 0, 0);
  308.         RectConvertMappings(&rcPos, hDC, FALSE);
  309.  
  310.         rc.left  = LOMETRIC_BORDER-rcPos.left;
  311.         rc.top   =-LOMETRIC_BORDER-rcPos.top;
  312.         }
  313.     else
  314.         {
  315.         /*
  316.          * We define the corner of the printed paper at a negative
  317.          * offset so rc.right and rc.bottom come out right below.
  318.          */
  319.         SetRect(&rc, -(int)m_xMarginLeft, m_yMarginTop, 0, 0);
  320.         }
  321.  
  322.     rc.right=rc.left+m_cx+(m_xMarginLeft+m_xMarginRight);
  323.     rc.bottom=rc.top-m_cy-(m_yMarginTop+m_yMarginBottom);
  324.  
  325.     //Draw a rect filled with the window color to show the page.
  326.     if (!fPrinter)
  327.         {
  328.         if (fNoColor)
  329.             {
  330.             //Black frame, white box for printed colors.
  331.             hPen  =CreatePen(PS_SOLID, 0, RGB(0,0,0));
  332.             hBrush=CreateSolidBrush(RGB(255, 255, 255));
  333.             }
  334.         else
  335.             {
  336.             //Normal colors on display
  337.             hPen=CreatePen(PS_SOLID, 0
  338.                 , GetSysColor(COLOR_WINDOWFRAME));
  339.             hBrush=CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  340.             }
  341.  
  342.         hObj1=SelectObject(hDC, hPen);
  343.         hObj2=SelectObject(hDC, hBrush);
  344.  
  345.         //Paper boundary
  346.         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom+1);
  347.  
  348.         /*
  349.          * Draw a shadow on the *visual* bottom and right edges
  350.          * .5mm wide.  If the button shadow color and workspace
  351.          * colors match, then use black.  We always use black
  352.          * when printing as well.
  353.          */
  354.         if (fNoColor)
  355.             cr=RGB(0,0,0);
  356.         else
  357.             {
  358.             cr=GetSysColor(COLOR_BTNSHADOW);
  359.  
  360.             if (GetSysColor(COLOR_APPWORKSPACE)==cr)
  361.                 cr=RGB(0,0,0);
  362.             }
  363.  
  364.         cr=SetBkColor(hDC, cr);
  365.         SetRect(&rcT, rc.left+5, rc.bottom, rc.right+5,rc.bottom-5);
  366.         ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcT, NULL, 0, NULL);
  367.  
  368.         SetRect(&rcT, rc.right, rc.top-5, rc.right+5, rc.bottom-5);
  369.         ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcT, NULL, 0, NULL);
  370.         SetBkColor(hDC, cr);
  371.  
  372.         SelectObject(hDC, hObj1);
  373.         SelectObject(hDC, hObj2);
  374.         DeleteObject(hBrush);
  375.         DeleteObject(hPen);
  376.         }
  377.  
  378.     //Write the page number in the lower left corner
  379.     if (!fNoColor)
  380.         {
  381.         SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  382.         SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  383.         }
  384.  
  385.     //Write the page number in our page font.
  386.     cch=wsprintf(szTemp, TEXT("Page %d"), m_iPageCur+1);
  387.  
  388.     hObj1=SelectObject(hDC, m_hFont);
  389.     GetTextExtentPoint(hDC, szTemp, cch, &sz);
  390.  
  391.     TextOut(hDC, rc.left+m_xMarginLeft
  392.         , rc.bottom+m_yMarginBottom+sz.cy, szTemp, cch);
  393.  
  394.     SelectObject(hDC, hObj1);
  395.  
  396.     //Rectangle to show border.
  397.     MoveToEx(hDC, rc.left+m_xMarginLeft, rc.top-m_yMarginTop, NULL);
  398.     LineTo(hDC, rc.left+m_xMarginLeft,   rc.bottom+m_yMarginBottom);
  399.     LineTo(hDC, rc.right-m_xMarginRight, rc.bottom+m_yMarginBottom);
  400.     LineTo(hDC, rc.right-m_xMarginRight, rc.top-m_yMarginTop);
  401.     LineTo(hDC, rc.left+m_xMarginLeft,   rc.top-m_yMarginTop);
  402.  
  403.     /*
  404.      * Go draw the objects on this page.  If the page is not open,
  405.      * we open it anyway.  If it is already open, then opening again
  406.      * will bump it's reference count, so the Close in ineffectual.
  407.      */
  408.     if (PageGet(m_iPageCur, &pPage, TRUE))
  409.         {
  410.         if (!fPrinter)
  411.             {
  412.             pPage->Draw(hDC, rcPos.left, rcPos.top, fNoColor
  413.                 , fPrinter);
  414.             }
  415.         else
  416.             pPage->Draw(hDC, 0, 0, fNoColor, fPrinter);
  417.  
  418.         pPage->Close(FALSE);
  419.         }
  420.  
  421.     SetMapMode(hDC, uMM);
  422.     return;
  423.     }
  424.  
  425.  
  426.  
  427.  
  428.  
  429. /*
  430.  * CPages::UpdateScrollRanges
  431.  *
  432.  * Purpose:
  433.  *  Reset scrollbar ranges (horizontal and vertical) depending on
  434.  *  the window size and the page size.  This function may remove
  435.  *  the scrollbars altogether.
  436.  *
  437.  * Parameters:
  438.  *  None, but set m_cx, m_cy and size m_hWnd before calling.
  439.  *
  440.  * Return Value:
  441.  *  None
  442.  */
  443.  
  444. void CPages::UpdateScrollRanges(void)
  445.     {
  446.     UINT        cxSB;   //Scrollbar width and height.
  447.     UINT        cySB;
  448.     UINT        cx, cy;
  449.     UINT        dx, dy;
  450.     UINT        u;
  451.     int         iMin, iMax;
  452.     RECT        rc;
  453.     BOOL        fHScroll;
  454.     BOOL        fVScroll;
  455.     BOOL        fWasThere;
  456.  
  457.     GetClientRect(m_hWnd, &rc);
  458.  
  459.     cx=rc.right-rc.left;
  460.     cy=rc.bottom-rc.top;
  461.  
  462.     //Convert dimensions of the image in LOMETRIC to pixels.
  463.     SetRect(&rc, (m_cx+m_xMarginLeft+m_xMarginRight
  464.         +LOMETRIC_BORDER*2), (m_cy+m_yMarginTop
  465.         +m_yMarginBottom+LOMETRIC_BORDER*2), 0, 0);
  466.  
  467.     RectConvertMappings(&rc, NULL, TRUE);
  468.  
  469.     dx=rc.left;
  470.     dy=-rc.top;
  471.  
  472.     //Assume that both scrollbars will be visible.
  473.     fHScroll=TRUE;
  474.     fVScroll=TRUE;
  475.  
  476.     /*
  477.      * Determine:
  478.      *  1)  Which scrollbars are needed.
  479.      *  2)  How many divisions to give scrollbars so as to
  480.      *      only scroll as little as necessary.
  481.      */
  482.  
  483.     //Scrollbar dimensions in our units.
  484.     cxSB=GetSystemMetrics(SM_CXVSCROLL);
  485.     cySB=GetSystemMetrics(SM_CYHSCROLL);
  486.  
  487.     //Remove horizontal scroll if window >= cxPage+borders
  488.     if (cx >= dx)
  489.         fHScroll=FALSE;
  490.  
  491.  
  492.     /*
  493.      * If we still need a horizontal scroll, see if we need a
  494.      * vertical taking the height of the horizontal scroll into
  495.      * account.
  496.      */
  497.  
  498.     u=fHScroll ? cySB : 0;
  499.  
  500.     if ((cy-u) >= dy)
  501.         fVScroll=FALSE;
  502.  
  503.     //Check if adding vert scrollbar necessitates a horz now.
  504.     u=fVScroll ? cxSB : 0;
  505.     fHScroll=((cx-u) < dx);
  506.  
  507.     /*
  508.      * Modify cx,cy to reflect the new client area before scaling
  509.      * scrollbars.  We only affect the client size if there is a
  510.      * *change* in scrollbar status:  if the scrollbar was there
  511.      * but is no longer, then add to the client size; if it was
  512.      * not there but now is, then subtract.
  513.      */
  514.  
  515.     //Change cx depending on vertical scrollbar change
  516.     GetScrollRange(m_hWnd, SB_VERT, &iMin, &iMax);
  517.     fWasThere=(0!=iMin || 0!=iMax);
  518.  
  519.     if (fWasThere && !fVScroll)
  520.         cx+=cxSB;
  521.  
  522.     if (!fWasThere && fVScroll)
  523.         cx-=cxSB;
  524.  
  525.     //Change cy depending on horizontal scrollbar change
  526.     GetScrollRange(m_hWnd, SB_HORZ, &iMin, &iMax);
  527.     fWasThere=(0!=iMin || 0!=iMax);
  528.  
  529.     if (fWasThere && !fHScroll)
  530.         cy+=cySB;
  531.  
  532.     if (!fWasThere && fHScroll)
  533.         cy-=cySB;
  534.  
  535.  
  536.     /*
  537.      * Show/Hide the scrollbars if necessary and set the ranges.
  538.      * The range is the number of units of the page we cannot see.
  539.      */
  540.     if (fHScroll)
  541.         {
  542.         //Convert current scroll position to new range.
  543.         u=GetScrollPos(m_hWnd, SB_HORZ);
  544.  
  545.         if (0!=u)
  546.             {
  547.             GetScrollRange(m_hWnd, SB_HORZ, &iMin, &iMax);
  548.             u=MulDiv(u, (dx-cx), (iMax-iMin));
  549.             }
  550.  
  551.         SetScrollRange(m_hWnd, SB_HORZ, 0, dx-cx, FALSE);
  552.         SetScrollPos(m_hWnd, SB_HORZ, u, TRUE);
  553.         m_xPos=u;
  554.         }
  555.     else
  556.         {
  557.         SetScrollRange(m_hWnd, SB_HORZ, 0, 0, TRUE);
  558.         m_xPos=0;
  559.         }
  560.  
  561.     if (fVScroll)
  562.         {
  563.         //Convert current scroll position to new range.
  564.         u=GetScrollPos(m_hWnd, SB_VERT);
  565.  
  566.         if (0!=u)
  567.             {
  568.             GetScrollRange(m_hWnd, SB_VERT, &iMin, &iMax);
  569.             u=MulDiv(u, (dy-cy), (iMax-iMin));
  570.             }
  571.  
  572.         SetScrollRange(m_hWnd, SB_VERT, 0, dy-cy, FALSE);
  573.         SetScrollPos(m_hWnd, SB_VERT, u, TRUE);
  574.  
  575.         m_yPos=u;
  576.         }
  577.     else
  578.         {
  579.         SetScrollRange(m_hWnd, SB_VERT, 0, 0, TRUE);
  580.         m_yPos=0;
  581.         }
  582.  
  583.     //Repaint to insure that changes to m_x/yPos are reflected
  584.     InvalidateRect(m_hWnd, NULL, TRUE);
  585.  
  586.     return;
  587.     }
  588.