home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / os2sdk / os2sdk12 / mdi / appdoc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-06  |  12.1 KB  |  438 lines

  1. /*
  2.     appdoc.c - MDI application
  3.     Created by Microsoft Corporation, 1989
  4. */
  5. #define INCL_WINSYS
  6. #define INCL_WINCOMMON
  7. #define INCL_WINMESSAGEMGR
  8. #define INCL_WINFRAMEMGR
  9. #define INCL_WINPOINTERS
  10. #define INCL_WINMENUS
  11. #define INCL_WINWINDOWMGR
  12. #define INCL_WINACCELERATORS
  13. #define INCL_WININPUT
  14. #define INCL_WINHEAP
  15. #define INCL_WINSCROLLBARS
  16. #define INCL_WINRECTANGLES
  17. #define INCL_WINCOUNTRY
  18. #define INCL_GPIPRIMITIVES
  19. #define INCL_GPILOGCOLORTABLE
  20.  
  21. #include <os2.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include "app.h"
  26. #include "appdata.h"
  27. #include "mdi.h"
  28. #include "mdidata.h"
  29.  
  30. /* Function prototypes */
  31. VOID AppHScroll(HWND hwnd, MPARAM mp1, MPARAM mp2);
  32. VOID AppVScroll(HWND hwnd, MPARAM mp1, MPARAM mp2);
  33. VOID AppEraseBackground(HWND hwnd, HPS hps);
  34. VOID AppPaint(HWND hwnd);
  35. VOID MDIClose(HWND hwndClient);
  36. BOOL MDICreate(HWND);
  37. BOOL MDIDestroy(HWND);
  38. BOOL MDIActivate(HWND, BOOL);
  39.  
  40. /*
  41.  * The array of RGB values for the rounded
  42.  * rectangles.
  43.  */
  44. LONG aclrRGB[16] = {
  45.     RGB_RED, RGB_WHITE, RGB_GREEN, RGB_BLACK,
  46.     RGB_BLUE, RGB_WHITE, RGB_YELLOW, RGB_BLACK,
  47.     RGB_CYAN, RGB_BLACK, RGB_PINK, RGB_BLACK,
  48.     RGB_WHITE, RGB_PINK, RGB_BLACK, RGB_RED
  49. };
  50.  
  51.  
  52.  
  53. MRESULT EXPENTRY DocWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  54. {
  55.  
  56.     switch (msg) {
  57.  
  58.     case WM_COMMAND:
  59.         return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  60.         break;
  61.  
  62.     case WM_CREATE:
  63.         if (MDICreate(hwnd) == FALSE)
  64.             return ( (MRESULT) TRUE);
  65.         break;
  66.  
  67.     case WM_DESTROY:
  68.         MDIDestroy(hwnd);
  69.         break;
  70.  
  71.     case WM_CLOSE:
  72.         MDIClose(hwnd);
  73.         break;
  74.  
  75.     case WM_HSCROLL:
  76.         AppHScroll(hwnd, mp1, mp2);
  77.         break;
  78.  
  79.     case WM_VSCROLL:
  80.         AppVScroll(hwnd, mp1, mp2);
  81.         break;
  82.  
  83.     case WM_ERASEBACKGROUND:
  84.         AppEraseBackground(hwnd, (HPS)mp1);
  85.         break;
  86.  
  87.     case WM_PAINT:
  88.         AppPaint(hwnd);
  89.         break;
  90.  
  91.     case WM_ACTIVATE:
  92.         MDIActivate(hwnd, (BOOL)SHORT1FROMMP(mp1));
  93.         break;
  94.  
  95.     default:
  96.         return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  97.         break;
  98.     }
  99.  
  100.     return (0L);
  101. }
  102.  
  103.  
  104. VOID AppEraseBackground(HWND hwnd, HPS hps)
  105. {
  106.     RECTL rclPaint;
  107.     HWND hwndFrame, hwndClient;
  108.     register NPDOC npdoc;
  109.  
  110.     npdoc = NPDOCFROMCLIENT(hwnd);
  111.     hwndFrame = WinQueryWindow(hwnd, QW_PARENT, FALSE);
  112.  
  113.     /*
  114.      * We know the main client is around so
  115.      * go ahead and erase it.
  116.      */
  117.     WinQueryWindowRect(hwnd, &rclPaint);
  118.     WinMapWindowPoints(hwnd, hwndFrame, (PPOINTL)&rclPaint, 2);
  119.     WinFillRect(hps, &rclPaint, npdoc->clrBackground);
  120.  
  121.     /*
  122.      * Now check to see which of the other client windows
  123.      * are around and erase them.
  124.      *
  125.      * We do all this to avoid erasing the splitbars.
  126.      */
  127.     if (npdoc->fs & DF_SPLITVERT) {
  128.  
  129.         hwndClient = WinWindowFromID(hwndFrame, ID_CLIENT2);
  130.         /*
  131.          * If it became invisible due to the frame
  132.          * window getting too small, then don't
  133.          * bother drawing.
  134.          */
  135.         if (WinIsWindowVisible(hwndClient) != FALSE) {
  136.             WinQueryWindowRect(hwndClient, &rclPaint);
  137.             WinMapWindowPoints(hwndClient, hwndFrame,
  138.                     (PPOINTL)&rclPaint, 2);
  139.             WinFillRect(hps, &rclPaint, npdoc->clrBackground);
  140.         }
  141.     }
  142.  
  143.     if (npdoc->fs & DF_SPLITHORZ) {
  144.  
  145.         hwndClient = WinWindowFromID(hwndFrame, ID_CLIENT3);
  146.         if (WinIsWindowVisible(hwndClient) != FALSE) {
  147.             WinQueryWindowRect(hwndClient, &rclPaint);
  148.             WinMapWindowPoints(hwndClient, hwndFrame,
  149.                     (PPOINTL)&rclPaint, 2);
  150.             WinFillRect(hps, &rclPaint, npdoc->clrBackground);
  151.         }
  152.     }
  153.  
  154.     /*
  155.      * If we're split in both directions, then there's
  156.      * a ID_CLIENT4 window.
  157.      */
  158.     if ((npdoc->fs & (DF_SPLITHORZ | DF_SPLITVERT)) ==
  159.             (DF_SPLITHORZ | DF_SPLITVERT)) {
  160.  
  161.         hwndClient = WinWindowFromID(hwndFrame, ID_CLIENT4);
  162.         if (WinIsWindowVisible(hwndClient) != FALSE) {
  163.             WinQueryWindowRect(hwndClient, &rclPaint);
  164.             WinMapWindowPoints(hwndClient, hwndFrame,
  165.                     (PPOINTL)&rclPaint, 2);
  166.             WinFillRect(hps, &rclPaint, npdoc->clrBackground);
  167.         }
  168.     }
  169. }
  170.  
  171.  
  172. VOID AppHScroll(HWND hwnd, MPARAM mp1, MPARAM mp2)
  173. {
  174.     HWND hwndFrame;
  175.     NPDOC npdoc;
  176.     RECTL rclPaintBottom, rclPaintTop;
  177.     RECTL rclWindowBottom, rclWindowTop;
  178.     HWND hwndClientBottom, hwndClientTop;
  179.     HWND hwndScrollbar;
  180.     register NPVIEW npviewBottom, npviewTop;
  181.     SHORT posSlider, xOriginOld;
  182.     USHORT cmd, idScrollbar;
  183.  
  184.     hwndFrame = WinQueryWindow(hwnd, QW_PARENT, FALSE);
  185.     npdoc = NPDOCFROMCLIENT(hwnd);
  186.  
  187.     idScrollbar = SHORT1FROMMP(mp1);
  188.  
  189.     switch (idScrollbar) {
  190.  
  191.     case FID_HORZSCROLL:
  192.         hwndClientTop = hwnd;
  193.         if (npdoc->fs & DF_SPLITHORZ) {
  194.             hwndClientBottom = WinWindowFromID(hwndFrame, ID_CLIENT3);
  195.         } else {
  196.             hwndClientBottom = NULL;
  197.         }
  198.         break;
  199.  
  200.     case ID_HORZSCROLL2:
  201.         hwndClientTop = WinWindowFromID(hwndFrame, ID_CLIENT2);
  202.         if (npdoc->fs & DF_SPLITHORZ) {
  203.             hwndClientBottom = WinWindowFromID(hwndFrame, ID_CLIENT4);
  204.         } else {
  205.             hwndClientBottom = NULL;
  206.         }
  207.         break;
  208.     }
  209.  
  210.     hwndScrollbar = WinWindowFromID(hwndFrame, idScrollbar);
  211.  
  212.     npviewTop = NPVIEWFROMCLIENT(hwndClientTop);
  213.     WinQueryWindowRect(hwndClientTop, &rclWindowTop);
  214.  
  215.     if (hwndClientBottom != NULL) {
  216.         npviewBottom = NPVIEWFROMCLIENT(hwndClientBottom);
  217.         WinQueryWindowRect(hwndClientBottom, &rclWindowBottom);
  218.     }
  219.  
  220.     posSlider = (SHORT) (ULONG) WinSendMsg(hwndScrollbar, SBM_QUERYPOS, NULL, NULL);
  221.  
  222.     cmd = SHORT2FROMMP(mp2);
  223.     switch (cmd) {
  224.  
  225.     case SB_LINELEFT:
  226.         posSlider -= 16;
  227.         break;
  228.  
  229.     case SB_LINERIGHT:
  230.         posSlider += 16;
  231.         break;
  232.  
  233.     case SB_PAGELEFT:
  234.         posSlider -= ((SHORT)rclWindowTop.xRight - 16);
  235.         break;
  236.  
  237.     case SB_PAGERIGHT:
  238.         posSlider += ((SHORT)rclWindowTop.xRight - 16);
  239.         break;
  240.  
  241.     case SB_SLIDERPOSITION:
  242.         posSlider = SHORT1FROMMP(mp2);
  243.         break;
  244.     }
  245.  
  246.     WinSendMsg(hwndScrollbar, SBM_SETPOS, MPFROMSHORT(posSlider), NULL);
  247.  
  248.     xOriginOld = npviewTop->xOrigin;
  249.     npviewTop->xOrigin = (SHORT) (ULONG) WinSendMsg(hwndScrollbar, SBM_QUERYPOS, NULL, NULL);
  250.     WinScrollWindow(hwndClientTop, xOriginOld - npviewTop->xOrigin, 0,
  251.             NULL, NULL, NULL, &rclPaintTop, NULL);
  252.  
  253.     if (hwndClientBottom != NULL) {
  254.         xOriginOld = npviewBottom->xOrigin;
  255.         npviewBottom->xOrigin = npviewTop->xOrigin;
  256.         WinScrollWindow(hwndClientBottom, xOriginOld - npviewBottom->xOrigin,
  257.                 0, NULL, NULL, NULL, &rclPaintBottom, NULL);
  258.     }
  259.  
  260.     WinMapWindowPoints(hwndClientTop, hwndFrame, (PPOINTL)&rclPaintTop, 2);
  261.     WinInvalidateRect(hwndFrame, &rclPaintTop, TRUE);
  262.  
  263.     if (hwndClientBottom != NULL) {
  264.         WinMapWindowPoints(hwndClientBottom, hwndFrame, (PPOINTL)&rclPaintBottom, 2);
  265.         WinInvalidateRect(hwndFrame, &rclPaintBottom, TRUE);
  266.     }
  267. }
  268.  
  269.  
  270. VOID AppVScroll(HWND hwnd, MPARAM mp1, MPARAM mp2)
  271. {
  272.     HWND hwndFrame;
  273.     NPDOC npdoc;
  274.     RECTL rclPaintRight, rclPaintLeft;
  275.     RECTL rclWindowRight, rclWindowLeft;
  276.     HWND hwndClientRight, hwndClientLeft;
  277.     HWND hwndScrollbar;
  278.     register NPVIEW npviewRight, npviewLeft;
  279.     SHORT posSlider, yOriginOld;
  280.     USHORT cmd, idScrollbar;
  281.  
  282.     hwndFrame = WinQueryWindow(hwnd, QW_PARENT, FALSE);
  283.     npdoc = NPDOCFROMCLIENT(hwnd);
  284.  
  285.     idScrollbar = SHORT1FROMMP(mp1);
  286.  
  287.     switch (idScrollbar) {
  288.  
  289.     case FID_VERTSCROLL:
  290.         hwndClientLeft = hwnd;
  291.         if (npdoc->fs & DF_SPLITVERT) {
  292.             hwndClientRight = WinWindowFromID(hwndFrame, ID_CLIENT2);
  293.         } else {
  294.             hwndClientRight = NULL;
  295.         }
  296.         break;
  297.  
  298.     case ID_VERTSCROLL2:
  299.         hwndClientLeft = WinWindowFromID(hwndFrame, ID_CLIENT3);
  300.         if (npdoc->fs & DF_SPLITVERT) {
  301.             hwndClientRight = WinWindowFromID(hwndFrame, ID_CLIENT4);
  302.         } else {
  303.             hwndClientRight = NULL;
  304.         }
  305.         break;
  306.     }
  307.  
  308.     hwndScrollbar = WinWindowFromID(hwndFrame, idScrollbar);
  309.  
  310.     npviewLeft = NPVIEWFROMCLIENT(hwndClientLeft);
  311.     WinQueryWindowRect(hwndClientLeft, &rclWindowLeft);
  312.  
  313.     if (hwndClientRight != NULL) {
  314.         npviewRight = NPVIEWFROMCLIENT(hwndClientRight);
  315.         WinQueryWindowRect(hwndClientRight, &rclWindowRight);
  316.     }
  317.  
  318.     posSlider = (SHORT) (ULONG) WinSendMsg(hwndScrollbar, SBM_QUERYPOS, NULL, NULL);
  319.  
  320.     cmd = SHORT2FROMMP(mp2);
  321.     switch (cmd) {
  322.  
  323.     case SB_LINEUP:
  324.         posSlider -= 16;
  325.         break;
  326.  
  327.     case SB_LINEDOWN:
  328.         posSlider += 16;
  329.         break;
  330.  
  331.     case SB_PAGEUP:
  332.         posSlider -= ((SHORT)rclWindowLeft.yTop - 16);
  333.         break;
  334.  
  335.     case SB_PAGEDOWN:
  336.         posSlider += ((SHORT)rclWindowLeft.yTop - 16);
  337.         break;
  338.  
  339.     case SB_SLIDERPOSITION:
  340.         posSlider = SHORT1FROMMP(mp2);
  341.         break;
  342.     }
  343.  
  344.     WinSendMsg(hwndScrollbar, SBM_SETPOS, MPFROMSHORT(posSlider), NULL);
  345.  
  346.     yOriginOld = npviewLeft->yOrigin;
  347.     npviewLeft->yOrigin = (SHORT) (ULONG) WinSendMsg(hwndScrollbar, SBM_QUERYPOS, NULL, NULL);
  348.     WinScrollWindow(hwndClientLeft, 0,  npviewLeft->yOrigin - yOriginOld,
  349.             NULL, NULL, NULL, &rclPaintLeft, NULL);
  350.  
  351.     if (hwndClientRight != NULL) {
  352.         yOriginOld = npviewRight->yOrigin;
  353.         npviewRight->yOrigin = npviewLeft->yOrigin;
  354.         WinScrollWindow(hwndClientRight, 0, npviewRight->yOrigin - yOriginOld,
  355.                 NULL, NULL, NULL, &rclPaintRight, NULL);
  356.     }
  357.  
  358.     WinMapWindowPoints(hwndClientLeft, hwndFrame, (PPOINTL)&rclPaintLeft, 2);
  359.     WinInvalidateRect(hwndFrame, &rclPaintLeft, TRUE);
  360.  
  361.     if (hwndClientRight != NULL) {
  362.         WinMapWindowPoints(hwndClientRight, hwndFrame, (PPOINTL)&rclPaintRight, 2);
  363.         WinInvalidateRect(hwndFrame, &rclPaintRight, TRUE);
  364.     }
  365. }
  366.  
  367.  
  368. VOID AppPaint(HWND hwnd)
  369. {
  370.     HPS hps;
  371.     RECTL rclPaint, rclWindow, rclTest, rclDst;
  372.     POINTL ptl, ptlPatternRef;
  373.     register NPVIEW npview;
  374.     AREABUNDLE abnd;
  375.     LONG clrStart, clrEnd, clrInc, clr;
  376.     SHORT i, j;
  377.  
  378.     hps = WinBeginPaint(hwnd, (HPS)NULL, &rclPaint);
  379.  
  380.     /*
  381.      * Go into RGB mode.
  382.      */
  383.     GpiCreateLogColorTable(hps, 0L, LCOLF_RGB, 0L, 0L, NULL);
  384.  
  385.     /*
  386.      * Make rclPaint an inclusive-inclusive rectangle
  387.      * since that's how GpiBox() will be output.
  388.      */
  389.     rclPaint.xLeft--;
  390.     rclPaint.yBottom--;
  391.  
  392.     npview = NPVIEWFROMCLIENT(hwnd);
  393.  
  394.     /*
  395.      * Set the pattern to be at the top-left
  396.      * since we're top-left aligning the bits.
  397.      */
  398.     WinQueryWindowRect(hwnd, (PRECTL)&rclWindow);
  399.     ptlPatternRef.x = rclWindow.xLeft - npview->xOrigin;
  400.     ptlPatternRef.y = rclWindow.yTop + npview->yOrigin;
  401.     GpiSetPatternRefPoint(hps, &ptlPatternRef);
  402.  
  403.     for (i = 0; i < 8; i++) {
  404.  
  405.         clr = clrStart = aclrRGB[i * 2];
  406.         clrEnd = aclrRGB[(i * 2) + 1];
  407.         clrInc = (clrEnd - clrStart) / 8;
  408.  
  409.         for (j = 0; j < 8; j++) {
  410.             abnd.lColor = clr + (j * clrInc);
  411.             GpiSetAttrs(hps, PRIM_AREA, ABB_COLOR, 0L, (PBUNDLE)&abnd);
  412.  
  413.             /*
  414.              * Only draw the box if it's going to
  415.              * be visible in the update region.
  416.              */
  417.             WinSetRect(NULL, &rclTest, 10 + (i * 75),
  418.                     (SHORT)rclWindow.yTop - 75 - (j * 75), 75 + (i * 75),
  419.                     (SHORT)rclWindow.yTop - 10 - (j * 75));
  420.  
  421.             WinOffsetRect(NULL, &rclTest, -npview->xOrigin, npview->yOrigin);
  422.  
  423.             if (WinIntersectRect(NULL, &rclDst, &rclTest, &rclPaint)) {
  424.  
  425.                 ptl.x = rclTest.xLeft;
  426.                 ptl.y = rclTest.yTop;
  427.                 GpiSetCurrentPosition(hps, (PPOINTL)&ptl);
  428.  
  429.                 ptl.x = rclTest.xRight;
  430.                 ptl.y = rclTest.yBottom;
  431.                 GpiBox(hps, DRO_OUTLINEFILL, (PPOINTL)&ptl, 40L, 40L);
  432.             }
  433.         }
  434.     }
  435.  
  436.     WinEndPaint(hps);
  437. }
  438.