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 / chap21 / patron / pagewin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  15.8 KB  |  586 lines

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