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