home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / sdktools / imagedit / viewwp.c < prev    next >
Text File  |  1996-06-12  |  15KB  |  470 lines

  1.     /****************************************************************************/
  2.     /*                                                                          */
  3.     /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4.     /*                           All Rights Reserved                            */
  5.     /*                                                                          */
  6.     /****************************************************************************/
  7.     /****************************** Module Header *******************************
  8.     * Module Name: viewwp.c
  9.     *
  10.     * Contains routines that handle the View window.
  11.     *
  12.     * History:
  13.     *
  14.     * 07/17/91 -  - Created.
  15.     *
  16.     ****************************************************************************/
  17.     
  18.     #include "imagedit.h"
  19.     #include "dialogs.h"
  20.  
  21.      #include <windowsx.h>
  22.     
  23.     /*
  24.      * Style of the view window. */
  25.     #define VIEWSTYLE       (WS_POPUP | WS_CLIPSIBLINGS | WS_CAPTION | WS_SYSMENU)
  26.     
  27.     
  28.     STATICFN VOID NEAR ViewChar(UINT uiChar);
  29.     
  30.     
  31.     static INT gViewBackMargin;     // Margin of background within view window.
  32.     
  33.     
  34.     
  35.     /****************************************************************************
  36.     * ViewCreate
  37.     *
  38.     * This function creates the View window.
  39.     *
  40.     * History:
  41.     *
  42.     ****************************************************************************/
  43.     
  44.     VOID ViewCreate(VOID)
  45.     {
  46.         INT x;
  47.         INT y;
  48.         INT cxDummy;
  49.         INT cyDummy;
  50.         RECT rc;
  51.         BOOL fMaximized;
  52.     
  53.         gViewBackMargin = GetSystemMetrics(SM_CXVSCROLL) / 2;
  54.     
  55.         /*
  56.          * Get the saved position of the Toolbox.  Note that we throw away
  57.          * the size fields, because we will calculate the required size
  58.          * later based on the image size.
  59.          */
  60.         if (!ReadWindowPos(szViewPos, &x, &y, &cxDummy, &cyDummy, &fMaximized)) {
  61.             /*
  62.              * The previous position of the View window couldn't be found.
  63.              * Position the window to the right side of the editor, just
  64.              * below the Toolbox.
  65.              */
  66.             if (ghwndToolbox) {
  67.                 GetWindowRect(ghwndToolbox, &rc);
  68.                 x = rc.left;
  69.                 y = rc.bottom + (2 * PALETTEMARGIN);
  70.             }
  71.             else {
  72.                 /*
  73.                  * Last resort.  Position it in the upper left corner
  74.                  * of the screen if the Toolbox cannot be found.  This
  75.                  * is unlikely because if the previous position of the
  76.                  * View window could not be found, this implies that
  77.                  * the editor has not been run before on this machine.
  78.                  * If this is true, the toolbox will come up by default
  79.                  * before this routine is called.  But just in case...
  80.                  */
  81.                 x = 2 * PALETTEMARGIN;
  82.                 y = 2 * PALETTEMARGIN;
  83.             }
  84.         }
  85.     
  86.         if (!(ghwndView = CreateWindow(szViewClass, NULL, VIEWSTYLE,
  87.                 x, y, 0, 0, ghwndMain, NULL, ghInst, NULL)))
  88.             return;
  89.     }
  90.     
  91.     
  92.     
  93.     /****************************************************************************
  94.     * ViewShow
  95.     *
  96.     * This function shows or hides the view window.
  97.     *
  98.     * History:
  99.     *
  100.     ****************************************************************************/
  101.     
  102.     VOID ViewShow(
  103.         BOOL fShow)
  104.     {
  105.         if (fShow) {
  106.             /*
  107.              * Only show it if there is an image to display!
  108.              */
  109.             if (gpImageCur)
  110.                 ShowWindow(ghwndView, SW_SHOWNA);
  111.         }
  112.         else {
  113.             ShowWindow(ghwndView, SW_HIDE);
  114.         }
  115.     }
  116.     
  117.     
  118.     
  119.     /****************************************************************************
  120.     * ViewUpdate
  121.     *
  122.     * This function updates the view window.  It should be called any time that
  123.     * the image changes (is drawn upon).
  124.     *
  125.     * History:
  126.     *
  127.     ****************************************************************************/
  128.     
  129.     VOID ViewUpdate(VOID)
  130.     {
  131.         InvalidateRect(ghwndView, NULL, TRUE);
  132.     
  133.         /*
  134.          * Update the workspace window also, because it must always
  135.          * match the state of the View window.
  136.          */
  137.         WorkUpdate();
  138.     }
  139.     
  140.     
  141.     
  142.     /****************************************************************************
  143.     * ViewReset
  144.     *
  145.     * This function resets the view window, sizing it to fit a new
  146.     * image.  It should be called any time that the current image
  147.     * is changed to another one.
  148.     *
  149.     * History:
  150.     *
  151.     ****************************************************************************/
  152.     
  153.     VOID ViewReset(VOID)
  154.     {
  155.         RECT rc;
  156.         RECT rcT;
  157.     
  158.         GetWindowRect(ghwndView, &rc);
  159.     
  160.         rcT.left = 0;
  161.         rcT.top = 0;
  162.         rcT.right = PALETTEMARGIN + gViewBackMargin +
  163.                 gpImageCur->cx + gViewBackMargin + PALETTEMARGIN;
  164.         rcT.bottom = PALETTEMARGIN + gViewBackMargin +
  165.                 gpImageCur->cy + gViewBackMargin + PALETTEMARGIN;
  166.         AdjustWindowRect(&rcT, VIEWSTYLE, FALSE);
  167.     
  168.         rc.right = rc.left + (rcT.right - rcT.left);
  169.         rc.bottom = rc.top + (rcT.bottom - rcT.top);
  170.         FitRectToScreen(&rc);
  171.     
  172.         SetWindowPos(ghwndView, NULL, rc.left, rc.top,
  173.                 rc.right - rc.left, rc.bottom - rc.top,
  174.                 SWP_NOACTIVATE | SWP_NOZORDER);
  175.     
  176.         /*
  177.          * If the user wants it, show the View window now.
  178.          */
  179.         if (gfShowView)
  180.             ViewShow(TRUE);
  181.     
  182.         ViewUpdate();
  183.     
  184.         /*
  185.          * Clear out the propbar size and position fields, because they
  186.          * probably show the wrong information now.
  187.          */
  188.         PropBarClearPos();
  189.         PropBarClearSize();
  190.     }
  191.     
  192.     
  193.     
  194.     /****************************************************************************
  195.     * ViewWndProc
  196.     *
  197.     * This is the window procedure for the view window.
  198.     *
  199.     * History:
  200.     *
  201.     ****************************************************************************/
  202.     
  203.     WINDOWPROC ViewWndProc(
  204.         HWND hwnd,
  205.         UINT msg,
  206.         WPARAM wParam,
  207.         LPARAM lParam)
  208.     {
  209.         switch (msg) {
  210.             case WM_CREATE:
  211.                 {
  212.                     HMENU hmenu = GetSystemMenu(hwnd, FALSE);
  213.     
  214.                     RemoveMenu(hmenu, 7, MF_BYPOSITION);    // Second separator.
  215.                     RemoveMenu(hmenu, 5, MF_BYPOSITION);    // First separator.
  216.     
  217.                     RemoveMenu(hmenu, SC_RESTORE, MF_BYCOMMAND);
  218.                     RemoveMenu(hmenu, SC_SIZE, MF_BYCOMMAND);
  219.                     RemoveMenu(hmenu, SC_MINIMIZE, MF_BYCOMMAND);
  220.                     RemoveMenu(hmenu, SC_MAXIMIZE, MF_BYCOMMAND);
  221.                     RemoveMenu(hmenu, SC_TASKLIST, MF_BYCOMMAND);
  222.                 }
  223.     
  224.                 return 0;
  225.     
  226.             case  WM_PAINT:
  227.                 {
  228.                     HDC hdc;
  229.                     PAINTSTRUCT ps;
  230.                     HBRUSH hbrOld;
  231.                     RECT rc;
  232.     
  233.                     hdc = BeginPaint(hwnd, &ps);
  234.     
  235.                     /*
  236.                      * The view window should not be showing if there
  237.                      * is not an image to view!
  238.                      */
  239.                     if (gpImageCur) {
  240.                         DrawMarginBorder(hwnd, hdc);
  241.     
  242.                         GetClientRect(hwnd, &rc);
  243.                         hbrOld = SelectObject(hdc, ghbrScreen);
  244.                         PatBlt(hdc, PALETTEMARGIN + 1, PALETTEMARGIN + 1,
  245.                                 rc.right - (PALETTEMARGIN * 2) - 2,
  246.                                 rc.bottom - (PALETTEMARGIN * 2) - 2,
  247.                                 PATCOPY);
  248.                         SelectObject(hdc, hbrOld);
  249.     
  250.                         BitBlt(hdc, PALETTEMARGIN + gViewBackMargin,
  251.                                 PALETTEMARGIN + gViewBackMargin,
  252.                                 gcxImage, gcyImage, ghdcImage, 0, 0, SRCCOPY);
  253.                     }
  254.     
  255.                     EndPaint(hwnd, &ps);
  256.                 }
  257.     
  258.                 break;
  259.     
  260.             case WM_ACTIVATE:
  261.                 if (GET_WM_ACTIVATE_STATE(wParam, lParam))
  262.                     gidCurrentDlg = DID_VIEW;
  263.     
  264.                 break;
  265.     
  266.             case WM_LBUTTONDOWN:
  267.                 SetScreenColor(gargbCurrent[giColorLeft]);
  268.                 break;
  269.     
  270.             case WM_CHAR:
  271.                 ViewChar(wParam);
  272.                 break;
  273.     
  274.             case WM_CLOSE:
  275.                 /*
  276.                  * The user closed the view window from the system menu.
  277.                  * Hide the window (we don't actually destroy it so
  278.                  * that it will appear in the same spot when they show
  279.                  * it again).
  280.                  */
  281.                 ViewShow(FALSE);
  282.                 gfShowView = FALSE;
  283.                 break;
  284.     
  285.             case WM_DESTROY:
  286.                 {
  287.                     RECT rc;
  288.     
  289.                     /*
  290.                      * Save the position of the toolbox.
  291.                      */
  292.                     GetWindowRect(hwnd, &rc);
  293.                     WriteWindowPos(&rc, FALSE, szViewPos);
  294.     
  295.                     /*
  296.                      * Null out the global window handle for the view window
  297.                      * for safety's sake.
  298.                      */
  299.                     ghwndView = NULL;
  300.                 }
  301.     
  302.                 break;
  303.     
  304.             default:
  305.                 return DefWindowProc(hwnd, msg, wParam, lParam);
  306.         }
  307.     
  308.         return 0;
  309.     }
  310.     
  311.     
  312.     
  313.     /************************************************************************
  314.     * ViewChar
  315.     *
  316.     * Handles WM_CHAR messages for the view window.  Currently this just
  317.     * includes the '+' and '-' keys, which are used to cycle through all
  318.     * the possible screen colors.
  319.     *
  320.     * Arguments:
  321.     *
  322.     * History:
  323.     *
  324.     ************************************************************************/
  325.     
  326.     STATICFN VOID NEAR ViewChar(
  327.         UINT uiChar)
  328.     {
  329.         INT i;
  330.         INT iNext;
  331.     
  332.         switch (uiChar) {
  333.             /*
  334.              * Advance to the next screen color.
  335.              */
  336.             case '+':
  337.                 iNext = 0;
  338.                 for (i = 0; i < 16; i++) {
  339.                     if (grgbScreen == gargbDefaultColor[i]) {
  340.                         iNext = i + 1;
  341.                         break;
  342.                     }
  343.                 }
  344.     
  345.                 if (iNext >= 16)
  346.                     iNext = 0;
  347.     
  348.                 SetScreenColor(gargbDefaultColor[iNext]);
  349.     
  350.                 break;
  351.     
  352.             /*
  353.              * Back up to the prior screen color.
  354.              */
  355.             case '-':
  356.                 iNext = 16 - 1;
  357.                 for (i = 0; i < 16; i++) {
  358.                     if (grgbScreen == gargbDefaultColor[i]) {
  359.                         iNext = i - 1;
  360.                         break;
  361.                     }
  362.                 }
  363.     
  364.                 if (iNext < 0)
  365.                     iNext = 16 - 1;
  366.     
  367.                 SetScreenColor(gargbDefaultColor[iNext]);
  368.     
  369.                 break;
  370.         }
  371.     }
  372.     
  373.     
  374.     
  375.     /****************************************************************************
  376.     * ViewSetPixel
  377.     *
  378.     * This function colors a pixel in the View window directly.  It is
  379.     * provided as an optimization when drawing a point.  The ghdcImage
  380.     * bitmap must be updated as well or the image on the screen will
  381.     * get out of synch with it.
  382.     *
  383.     * History:
  384.     *
  385.     ****************************************************************************/
  386.     
  387.     VOID ViewSetPixel(
  388.         INT x,
  389.         INT y,
  390.         INT nBrushSize)
  391.     {
  392.         HDC hDC;
  393.         HBRUSH hbrOld;
  394.         INT Size;
  395.         INT SizeX;
  396.         INT SizeY;
  397.     
  398.         hDC = GetDC(ghwndView);
  399.         hbrOld = SelectObject(hDC, ghbrDrawSolid);
  400.         SizeX = x - nBrushSize / 2;
  401.         SizeY = y - nBrushSize / 2;
  402.         PatBlt(hDC, PALETTEMARGIN + gViewBackMargin + (SizeX >= 0 ? SizeX : 0),
  403.                 PALETTEMARGIN + gViewBackMargin + (SizeY >=  0 ? SizeY :  0),
  404.                 ((Size = gcxImage - SizeX) >= nBrushSize ?
  405.                 nBrushSize : Size),
  406.                 ((Size = gcyImage - SizeY) >= nBrushSize ?
  407.                 nBrushSize : Size), PATCOPY);
  408.         SelectObject(hDC, hbrOld);
  409.         ReleaseDC(ghwndView, hDC);
  410.     }
  411.     
  412.     
  413.     
  414.     /****************************************************************************
  415.     * DrawMarginBorder
  416.     *
  417.     *
  418.     * History:
  419.     *
  420.     ****************************************************************************/
  421.     
  422.     VOID DrawMarginBorder(
  423.         HWND hwnd,
  424.         HDC hdc)
  425.     {
  426.         HBRUSH hbrOld;
  427.         HPEN hpenOld;
  428.         RECT rc;
  429.     
  430.         GetClientRect(hwnd, &rc);
  431.         hpenOld = SelectObject(hdc, GetStockObject(BLACK_PEN));
  432.         hbrOld = SelectObject(hdc, GetStockObject(NULL_BRUSH));
  433.         Rectangle(hdc, PALETTEMARGIN, PALETTEMARGIN,
  434.                 rc.right - PALETTEMARGIN,
  435.                 rc.bottom - PALETTEMARGIN);
  436.         SelectObject(hdc, hpenOld);
  437.         SelectObject(hdc, hbrOld);
  438.     }
  439.     
  440.     
  441.     
  442.     /****************************************************************************
  443.     * DrawSunkenRect
  444.     *
  445.     *
  446.     * History:
  447.     *
  448.     ****************************************************************************/
  449.     
  450.     VOID DrawSunkenRect(
  451.         PRECT prc,
  452.         HDC hdc)
  453.     {
  454.         HPEN hpenOld;
  455.     
  456.         hpenOld = SelectObject(hdc, hpenDarkGray);
  457.         MoveToEx(hdc, prc->left, prc->top, NULL);
  458.         LineTo(hdc, prc->right - 1, prc->top);
  459.         MoveToEx(hdc, prc->left, prc->top, NULL);
  460.         LineTo(hdc, prc->left, prc->bottom - 1);
  461.     
  462.         SelectObject(hdc, GetStockObject(WHITE_PEN));
  463.         MoveToEx(hdc, prc->left + 1, prc->bottom - 1, NULL);
  464.         LineTo(hdc, prc->right, prc->bottom - 1);
  465.         MoveToEx(hdc, prc->right - 1, prc->top + 1, NULL);
  466.         LineTo(hdc, prc->right - 1, prc->bottom - 1);
  467.     
  468.         SelectObject(hdc, hpenOld);
  469.     }
  470.