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 / chap01 / patron / pagewin.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  13KB  |  485 lines

  1. /*
  2.  * PAGEWIN.CPP
  3.  * Patron Chapter 1
  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.  
  37.     ppg=(PCPages)GetWindowLong(hWnd, PAGEWL_STRUCTURE);
  38.  
  39.     switch (iMsg)
  40.         {
  41.         case WM_CREATE:
  42.             ppg=(PCPages)((LPCREATESTRUCT)lParam)->lpCreateParams;
  43.             SetWindowLong(hWnd, PAGEWL_STRUCTURE, (LONG)ppg);
  44.  
  45.             ppg->m_hWnd=hWnd;
  46.             ppg->New();
  47.             break;
  48.  
  49.  
  50.         case WM_PAINT:
  51.             hDC=BeginPaint(hWnd, &ps);
  52.  
  53.             //Draw only if we have a page to show.
  54.             if (0!=ppg->m_cPages)
  55.                 ppg->Draw(hDC, FALSE, FALSE);
  56.  
  57.             EndPaint(hWnd, &ps);
  58.             break;
  59.  
  60.  
  61.         case WM_HSCROLL:
  62.         case WM_VSCROLL:
  63.             idScroll=(WM_HSCROLL==iMsg) ? SB_HORZ : SB_VERT;
  64.  
  65.             iPos=GetScrollPos(hWnd, idScroll);
  66.             iTmp=iPos;
  67.             GetScrollRange(hWnd, idScroll, &iMin, &iMax);
  68.  
  69.             switch (LOWORD(wParam))
  70.                 {
  71.                 case SB_LINEUP:     iPos -= 20;  break;
  72.                 case SB_PAGEUP:     iPos -=100;  break;
  73.                 case SB_LINEDOWN:   iPos += 20;  break;
  74.                 case SB_PAGEDOWN:   iPos +=100;  break;
  75.  
  76.                 case SB_THUMBPOSITION:
  77.                     iPos=ScrollThumbPosition(wParam, lParam);
  78.                     break;
  79.  
  80.                 //We don't want scrolling on this message.
  81.                 case SB_THUMBTRACK:
  82.                     return 0L;
  83.                 }
  84.  
  85.             iPos=max(iMin, min(iPos, iMax));
  86.  
  87.             if (iPos!=iTmp)
  88.                 {
  89.                 //Set the new position and scroll the window
  90.                 SetScrollPos(hWnd, idScroll, iPos, TRUE);
  91.  
  92.                 if (SB_HORZ==idScroll)
  93.                     {
  94.                     ppg->m_xPos=iPos;
  95.                     ScrollWindow(hWnd, iTmp-iPos, 0, NULL, NULL);
  96.                     }
  97.                 else
  98.                     {
  99.                     ppg->m_yPos=iPos;
  100.                     ScrollWindow(hWnd, 0, iTmp-iPos, NULL, NULL);
  101.                     }
  102.                 }
  103.  
  104.             break;
  105.  
  106.  
  107.         default:
  108.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  109.         }
  110.  
  111.     return 0L;
  112.     }
  113.  
  114.  
  115.  
  116.  
  117.  
  118. /*
  119.  * RectConvertMappings
  120.  *
  121.  * Purpose:
  122.  *  Converts the contents of a rectangle from device to logical
  123.  *  coordinates where the hDC defines the logical coordinates.
  124.  *
  125.  * Parameters:
  126.  *  pRect           LPRECT containing the rectangle to convert.
  127.  *  hDC             HDC describing the logical coordinate system.
  128.  *                  if NULL, uses a screen DC in MM_LOMETRIC.
  129.  *  fToDevice       BOOL TRUE to convert from uConv to device,
  130.  *                  FALSE to convert device to uConv.
  131.  *
  132.  * Return Value:
  133.  *  None
  134.  */
  135.  
  136. void RectConvertMappings(LPRECT pRect, HDC hDC, BOOL fToDevice)
  137.     {
  138.     POINT   rgpt[2];
  139.     BOOL    fSysDC=FALSE;
  140.  
  141.     if (NULL==pRect)
  142.         return;
  143.  
  144.     rgpt[0].x=pRect->left;
  145.     rgpt[0].y=pRect->top;
  146.     rgpt[1].x=pRect->right;
  147.     rgpt[1].y=pRect->bottom;
  148.  
  149.     if (NULL==hDC)
  150.         {
  151.         hDC=GetDC(NULL);
  152.         SetMapMode(hDC, MM_LOMETRIC);
  153.         fSysDC=TRUE;
  154.         }
  155.  
  156.     if (fToDevice)
  157.         LPtoDP(hDC, rgpt, 2);
  158.     else
  159.         DPtoLP(hDC, rgpt, 2);
  160.  
  161.     if (fSysDC)
  162.         ReleaseDC(NULL, hDC);
  163.  
  164.     pRect->left=rgpt[0].x;
  165.     pRect->top=rgpt[0].y;
  166.     pRect->right=rgpt[1].x;
  167.     pRect->bottom=rgpt[1].y;
  168.  
  169.     return;
  170.     }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. /*
  178.  * CPages::Draw
  179.  *
  180.  * Purpose:
  181.  *  Paints the current page in the pages window.
  182.  *
  183.  * Parameters:
  184.  *  hDC             HDC to draw on, could be a metafile or printer
  185.  *                  DC or any other type of DC.
  186.  *  fNoColor        BOOL indicating if we should use screen colors
  187.  *                  or printer colors (B&W).  Objects are printed
  188.  *                  as-is, however.  This is TRUE for printer DCs
  189.  *                  or print preview.
  190.  *  fPrinter        BOOL indicating if this is a printer DC in which
  191.  *                  case we eliminate some of the fancy drawing,
  192.  *                  like shadows on the page and so forth.
  193.  *
  194.  * Return Value:
  195.  *  None
  196.  */
  197.  
  198. void CPages::Draw(HDC hDC, BOOL fNoColor, BOOL fPrinter)
  199.     {
  200.     RECT            rc, rcT;
  201.     UINT            uMM;
  202.     HPEN            hPen;
  203.     HBRUSH          hBrush;
  204.     HGDIOBJ         hObj1, hObj2;
  205.     COLORREF        cr;
  206.     TCHAR           szTemp[20];
  207.     UINT            cch;
  208.     SIZE            sz;
  209.  
  210.     //Make sure the DC is in LOMETRIC
  211.     uMM=SetMapMode(hDC, MM_LOMETRIC);
  212.  
  213.     if (!fPrinter)
  214.         {
  215.         /*
  216.          * We maintain a 6mm border around the page on the screen
  217.          * besides 12.7mm margins.  We also have to account for
  218.          * the scroll position with m_*Pos which are in pixels so
  219.          * we have to convert them.
  220.          */
  221.  
  222.         SetRect(&rcT, m_xPos, m_yPos, 0, 0);
  223.         RectConvertMappings(&rcT, hDC, FALSE);
  224.  
  225.         rc.left  = LOMETRIC_BORDER-rcT.left;
  226.         rc.top   =-LOMETRIC_BORDER-rcT.top;
  227.         }
  228.     else
  229.         {
  230.         /*
  231.          * We define the corner of the printed paper at a negative
  232.          * offset so rc.right and rc.bottom come out right below.
  233.          */
  234.         SetRect(&rc, -(int)m_xMarginLeft, m_yMarginTop, 0, 0);
  235.         }
  236.  
  237.     rc.right=rc.left+m_cx+(m_xMarginLeft+m_xMarginRight);
  238.     rc.bottom=rc.top-m_cy-(m_yMarginTop+m_yMarginBottom);
  239.  
  240.     //Draw a rect filled with the window color to show the page.
  241.     if (!fPrinter)
  242.         {
  243.         if (fNoColor)
  244.             {
  245.             //Black frame, white box for printed colors.
  246.             hPen  =CreatePen(PS_SOLID, 0, RGB(0,0,0));
  247.             hBrush=CreateSolidBrush(RGB(255, 255, 255));
  248.             }
  249.         else
  250.             {
  251.             //Normal colors on display
  252.             hPen=CreatePen(PS_SOLID, 0
  253.                 , GetSysColor(COLOR_WINDOWFRAME));
  254.             hBrush=CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  255.             }
  256.  
  257.         hObj1=SelectObject(hDC, hPen);
  258.         hObj2=SelectObject(hDC, hBrush);
  259.  
  260.         //Paper boundary
  261.         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom+1);
  262.  
  263.         /*
  264.          * Draw a shadow on the *visual* bottom and right edges
  265.          * .5mm wide.  If the button shadow color and workspace
  266.          * colors match, then use black.  We always use black
  267.          * when printing as well.
  268.          */
  269.         if (fNoColor)
  270.             cr=RGB(0,0,0);
  271.         else
  272.             {
  273.             cr=GetSysColor(COLOR_BTNSHADOW);
  274.  
  275.             if (GetSysColor(COLOR_APPWORKSPACE)==cr)
  276.                 cr=RGB(0,0,0);
  277.             }
  278.  
  279.         cr=SetBkColor(hDC, cr);
  280.         SetRect(&rcT, rc.left+5, rc.bottom, rc.right+5,rc.bottom-5);
  281.         ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcT, NULL, 0, NULL);
  282.  
  283.         SetRect(&rcT, rc.right, rc.top-5, rc.right+5, rc.bottom-5);
  284.         ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcT, NULL, 0, NULL);
  285.         SetBkColor(hDC, cr);
  286.  
  287.         SelectObject(hDC, hObj1);
  288.         SelectObject(hDC, hObj2);
  289.         DeleteObject(hBrush);
  290.         DeleteObject(hPen);
  291.         }
  292.  
  293.     //Write the page number in the lower left corner
  294.     if (!fNoColor)
  295.         {
  296.         SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  297.         SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  298.         }
  299.  
  300.     //Write the page number in our page font.
  301.     cch=wsprintf(szTemp, TEXT("Page %d"), m_iPageCur+1);
  302.  
  303.     hObj1=SelectObject(hDC, m_hFont);
  304.     GetTextExtentPoint(hDC, szTemp, cch, &sz);
  305.  
  306.     TextOut(hDC, rc.left+m_xMarginLeft
  307.         , rc.bottom+m_yMarginBottom+sz.cy, szTemp, cch);
  308.  
  309.     SelectObject(hDC, hObj1);
  310.  
  311.     //Rectangle to show border.
  312.     MoveToEx(hDC, rc.left+m_xMarginLeft, rc.top-m_yMarginTop, NULL);
  313.     LineTo(hDC, rc.left+m_xMarginLeft,   rc.bottom+m_yMarginBottom);
  314.     LineTo(hDC, rc.right-m_xMarginRight, rc.bottom+m_yMarginBottom);
  315.     LineTo(hDC, rc.right-m_xMarginRight, rc.top-m_yMarginTop);
  316.     LineTo(hDC, rc.left+m_xMarginLeft,   rc.top-m_yMarginTop);
  317.  
  318.     SetMapMode(hDC, uMM);
  319.     return;
  320.     }
  321.  
  322.  
  323.  
  324.  
  325.  
  326. /*
  327.  * CPages::UpdateScrollRanges
  328.  *
  329.  * Purpose:
  330.  *  Reset scrollbar ranges (horizontal and vertical) depending on
  331.  *  the window size and the page size.  This function may remove
  332.  *  the scrollbars altogether.
  333.  *
  334.  * Parameters:
  335.  *  None, but set m_cx, m_cy and size m_hWnd before calling.
  336.  *
  337.  * Return Value:
  338.  *  None
  339.  */
  340.  
  341. void CPages::UpdateScrollRanges(void)
  342.     {
  343.     UINT        cxSB;   //Scrollbar width and height.
  344.     UINT        cySB;
  345.     UINT        cx, cy;
  346.     UINT        dx, dy;
  347.     UINT        u;
  348.     int         iMin, iMax;
  349.     RECT        rc;
  350.     BOOL        fHScroll;
  351.     BOOL        fVScroll;
  352.     BOOL        fWasThere;
  353.  
  354.     GetClientRect(m_hWnd, &rc);
  355.  
  356.     cx=rc.right-rc.left;
  357.     cy=rc.bottom-rc.top;
  358.  
  359.     //Convert dimensions of the image in LOMETRIC to pixels.
  360.     SetRect(&rc, (m_cx+m_xMarginLeft+m_xMarginRight
  361.         +LOMETRIC_BORDER*2), (m_cy+m_yMarginTop
  362.         +m_yMarginBottom+LOMETRIC_BORDER*2), 0, 0);
  363.  
  364.     RectConvertMappings(&rc, NULL, TRUE);
  365.  
  366.     dx=rc.left;
  367.     dy=-rc.top;
  368.  
  369.     //Assume that both scrollbars will be visible.
  370.     fHScroll=TRUE;
  371.     fVScroll=TRUE;
  372.  
  373.     /*
  374.      * Determine:
  375.      *  1)  Which scrollbars are needed.
  376.      *  2)  How many divisions to give scrollbars so as to
  377.      *      only scroll as little as necessary.
  378.      */
  379.  
  380.     //Scrollbar dimensions in our units.
  381.     cxSB=GetSystemMetrics(SM_CXVSCROLL);
  382.     cySB=GetSystemMetrics(SM_CYHSCROLL);
  383.  
  384.     //Remove horizontal scroll if window >= cxPage+borders
  385.     if (cx >= dx)
  386.         fHScroll=FALSE;
  387.  
  388.  
  389.     /*
  390.      * If we still need a horizontal scroll, see if we need a
  391.      * vertical taking the height of the horizontal scroll into
  392.      * account.
  393.      */
  394.  
  395.     u=fHScroll ? cySB : 0;
  396.  
  397.     if ((cy-u) >= dy)
  398.         fVScroll=FALSE;
  399.  
  400.     //Check if adding vert scrollbar necessitates a horz now.
  401.     u=fVScroll ? cxSB : 0;
  402.     fHScroll=((cx-u) < dx);
  403.  
  404.     /*
  405.      * Modify cx,cy to reflect the new client area before scaling
  406.      * scrollbars.  We only affect the client size if there is a
  407.      * *change* in scrollbar status:  if the scrollbar was there
  408.      * but is no longer, then add to the client size; if it was
  409.      * not there but now is, then subtract.
  410.      */
  411.  
  412.     //Change cx depending on vertical scrollbar change
  413.     GetScrollRange(m_hWnd, SB_VERT, &iMin, &iMax);
  414.     fWasThere=(0!=iMin || 0!=iMax);
  415.  
  416.     if (fWasThere && !fVScroll)
  417.         cx+=cxSB;
  418.  
  419.     if (!fWasThere && fVScroll)
  420.         cx-=cxSB;
  421.  
  422.     //Change cy depending on horizontal scrollbar change
  423.     GetScrollRange(m_hWnd, SB_HORZ, &iMin, &iMax);
  424.     fWasThere=(0!=iMin || 0!=iMax);
  425.  
  426.     if (fWasThere && !fHScroll)
  427.         cy+=cySB;
  428.  
  429.     if (!fWasThere && fHScroll)
  430.         cy-=cySB;
  431.  
  432.  
  433.     /*
  434.      * Show/Hide the scrollbars if necessary and set the ranges.
  435.      * The range is the number of units of the page we cannot see.
  436.      */
  437.     if (fHScroll)
  438.         {
  439.         //Convert current scroll position to new range.
  440.         u=GetScrollPos(m_hWnd, SB_HORZ);
  441.  
  442.         if (0!=u)
  443.             {
  444.             GetScrollRange(m_hWnd, SB_HORZ, &iMin, &iMax);
  445.             u=MulDiv(u, (dx-cx), (iMax-iMin));
  446.             }
  447.  
  448.         SetScrollRange(m_hWnd, SB_HORZ, 0, dx-cx, FALSE);
  449.         SetScrollPos(m_hWnd, SB_HORZ, u, TRUE);
  450.         m_xPos=u;
  451.         }
  452.     else
  453.         {
  454.         SetScrollRange(m_hWnd, SB_HORZ, 0, 0, TRUE);
  455.         m_xPos=0;
  456.         }
  457.  
  458.     if (fVScroll)
  459.         {
  460.         //Convert current scroll position to new range.
  461.         u=GetScrollPos(m_hWnd, SB_VERT);
  462.  
  463.         if (0!=u)
  464.             {
  465.             GetScrollRange(m_hWnd, SB_VERT, &iMin, &iMax);
  466.             u=MulDiv(u, (dy-cy), (iMax-iMin));
  467.             }
  468.  
  469.         SetScrollRange(m_hWnd, SB_VERT, 0, dy-cy, FALSE);
  470.         SetScrollPos(m_hWnd, SB_VERT, u, TRUE);
  471.  
  472.         m_yPos=u;
  473.         }
  474.     else
  475.         {
  476.         SetScrollRange(m_hWnd, SB_VERT, 0, 0, TRUE);
  477.         m_yPos=0;
  478.         }
  479.  
  480.     //Repaint to insure that changes to m_x/yPos are reflected
  481.     InvalidateRect(m_hWnd, NULL, TRUE);
  482.  
  483.     return;
  484.     }
  485.