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 / propbar.c < prev    next >
Text File  |  1996-06-12  |  15KB  |  494 lines

  1.     /****************************************************************************/
  2.     /*                                                                          */
  3.     /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4.     /*                           All Rights Reserved                            */
  5.     /*                                                                          */
  6.     /****************************************************************************/
  7.     /****************************** Module Header *******************************
  8.     * Module Name: propbar.c
  9.     *
  10.     * Support for the Properties Bar.
  11.     *
  12.     * History:
  13.     *
  14.     ****************************************************************************/
  15.     
  16.     #include "imagedit.h"
  17.     #include "dialogs.h"
  18.  
  19.      #include <windowsx.h>
  20.     
  21.     #define IMPOSSIBLEVALUE     0x7FFF
  22.     
  23.     STATICFN VOID NEAR PropBarProcessCommand(HWND hwnd, INT idCtrl,
  24.         INT NotifyCode);
  25.     
  26.     /*
  27.      * Cache variables.  These cache the values of some displayed fields.
  28.      * The field is updated only if the value changes, which avoids flicker.
  29.      * They are initialized to a large value so that the first "set" to
  30.      * any value will always cause the field to be updated.
  31.      */
  32.     static INT xSave = IMPOSSIBLEVALUE;
  33.     static INT ySave = IMPOSSIBLEVALUE;
  34.     static INT cxSave = IMPOSSIBLEVALUE;
  35.     static INT cySave = IMPOSSIBLEVALUE;
  36.     static INT xHotSpotSave = IMPOSSIBLEVALUE;
  37.     static INT yHotSpotSave = IMPOSSIBLEVALUE;
  38.     
  39.     
  40.     
  41.     /************************************************************************
  42.     * PropBarDlgProc
  43.     *
  44.     * This is the dialog procedure for the PropBar ribbon window.
  45.     *
  46.     * History:
  47.     *
  48.     ************************************************************************/
  49.     
  50.     DIALOGPROC PropBarDlgProc(
  51.         HWND hwnd,
  52.         UINT msg,
  53.         WPARAM wParam,
  54.         LPARAM lParam)
  55.     {
  56.         switch (msg) {
  57.             case WM_INITDIALOG:
  58.                 /*
  59.                  * Set this global right away.  Other routines that
  60.                  * might be called before CreateDialog returns depend
  61.                  * on this global.
  62.                  */
  63.                 ghwndPropBar = hwnd;
  64.     
  65.                 PropBarUpdate();
  66.     
  67.                 /*
  68.                  * Return TRUE so that the dialog manager does NOT
  69.                  * set the focus for me.  This prevents the PropBar
  70.                  * window from initially having the focus when the
  71.                  * editor is started.
  72.                  */
  73.                 return TRUE;
  74.     
  75.             case WM_PAINT:
  76.                 {
  77.                     HDC hdc;
  78.                     RECT rc;
  79.                     PAINTSTRUCT ps;
  80.                     HPEN hpenWindowFrame;
  81.     
  82.                     /*
  83.                      * Draw our border lines.
  84.                      */
  85.                     GetClientRect(hwnd, &rc);
  86.                     hdc = BeginPaint(hwnd, &ps);
  87.     
  88.                     SelectObject(hdc, GetStockObject(WHITE_PEN));
  89.                     MoveToEx(hdc, rc.left, rc.top, NULL);
  90.                     LineTo(hdc, rc.right, rc.top);
  91.     
  92.                     SelectObject(hdc, hpenDarkGray);
  93.                     MoveToEx(hdc, rc.left, (rc.top + gcyPropBar) - gcyBorder - 1, NULL);
  94.                     LineTo(hdc, rc.right, (rc.top + gcyPropBar) - gcyBorder - 1);
  95.     
  96.                     hpenWindowFrame = CreatePen(PS_SOLID, gcyBorder,
  97.                             GetSysColor(COLOR_WINDOWFRAME));
  98.                     SelectObject(hdc, hpenWindowFrame);
  99.                     MoveToEx(hdc, rc.left, (rc.top + gcyPropBar) - gcyBorder, NULL);
  100.                     LineTo(hdc, rc.right, (rc.top + gcyPropBar) - gcyBorder);
  101.     
  102.                     EndPaint(hwnd, &ps);
  103.                     DeleteObject(hpenWindowFrame);
  104.                 }
  105.     
  106.                 break;
  107.     
  108.             case WM_CTLCOLORBTN:
  109.             case WM_CTLCOLORDLG:
  110.             case WM_CTLCOLORLISTBOX:
  111.             case WM_CTLCOLORSTATIC:
  112.                 switch (GET_WM_CTLCOLOR_TYPE(wParam, lParam, msg)) {
  113.                     case CTLCOLOR_BTN:
  114.                     case CTLCOLOR_DLG:
  115.                     case CTLCOLOR_LISTBOX:
  116.                         return (BOOL)GetStockObject(LTGRAY_BRUSH);
  117.     
  118.                     case CTLCOLOR_STATIC:
  119.                         SetBkColor(GET_WM_CTLCOLOR_HDC(wParam, lParam, msg),
  120.                                 RGB_LIGHTGRAY);
  121.                         return (BOOL)GetStockObject(LTGRAY_BRUSH);
  122.                 }
  123.     
  124.                 return (BOOL)NULL;
  125.     
  126.             case WM_COMMAND:
  127.                 PropBarProcessCommand(hwnd,
  128.                         GET_WM_COMMAND_ID(wParam, lParam),
  129.                         GET_WM_COMMAND_CMD(wParam, lParam));
  130.                 break;
  131.     
  132.             case WM_DESTROY:
  133.                 /*
  134.                  * Null out the global window handle for the prop bar
  135.                  * for safety's sake.
  136.                  */
  137.                 ghwndPropBar = NULL;
  138.     
  139.                 break;
  140.     
  141.             default:
  142.                 return FALSE;
  143.         }
  144.     
  145.         return FALSE;
  146.     }
  147.     
  148.     
  149.     
  150.     /************************************************************************
  151.     * PropBarProcessCommand
  152.     *
  153.     *
  154.     * Arguments:
  155.     *   HWND hwnd        - The window handle.
  156.     *   INT idCtrl       - The id of the control the WM_COMMAND is for.
  157.     *   INT NotifyCode   - The control's notification code.
  158.     *
  159.     * History:
  160.     *
  161.     ************************************************************************/
  162.     
  163.     STATICFN VOID NEAR PropBarProcessCommand(
  164.         HWND hwnd,
  165.         INT idCtrl,
  166.         INT NotifyCode)
  167.     {
  168.         INT iSelect;
  169.         PIMAGEINFO pImage;
  170.     
  171.         switch (idCtrl) {
  172.             case DID_PROPBARIMAGE:
  173.                 if (NotifyCode == CBN_SELCHANGE) {
  174.                     if ((iSelect = (INT)SendDlgItemMessage(hwnd,
  175.                             DID_PROPBARIMAGE, CB_GETCURSEL, 0, 0L)) != CB_ERR) {
  176.                         /*
  177.                          * Get a pointer to the selected image (stored in the
  178.                          * listbox items data field).
  179.                          */
  180.                         pImage = (PIMAGEINFO)SendDlgItemMessage(hwnd,
  181.                                 DID_PROPBARIMAGE, CB_GETITEMDATA, iSelect, 0L);
  182.     
  183.                         /*
  184.                          * Open the image.  If it fails, be sure to set the
  185.                          * combobox selection back to the current image.
  186.                          */
  187.                         if (!ImageOpen(pImage)) {
  188.                             PropBarSetImage(gpImageCur);
  189.                             break;
  190.                         }
  191.     
  192.                         SetFocus(ghwndMain);
  193.                     }
  194.                 }
  195.     
  196.                 break;
  197.     
  198.             case IDOK:
  199.             case IDCANCEL:
  200.                 SetFocus(ghwndMain);
  201.                 break;
  202.         }
  203.     }
  204.     
  205.     
  206.     
  207.     /************************************************************************
  208.     * PropBarUpdate
  209.     *
  210.     * This function updates the Properties Bar for the selection of a
  211.     * new image or file.  It fills the Image combo with the names of
  212.     * the images in the current file and shows/hides the HotSpot display.
  213.     *
  214.     * History:
  215.     *
  216.     ************************************************************************/
  217.     
  218.     VOID PropBarUpdate(VOID)
  219.     {
  220.         HWND hwndCombo;
  221.         PIMAGEINFO pImage;
  222.         INT idLabel;
  223.         INT i;
  224.     
  225.         if (gpImageCur || gpszFileName) {
  226.             switch (giType) {
  227.                 case FT_BITMAP:
  228.                     idLabel = IDS_BITMAPIMAGELABEL;
  229.                     break;
  230.     
  231.                 case FT_ICON:
  232.                     idLabel = IDS_ICONIMAGELABEL;
  233.                     break;
  234.     
  235.                 case FT_CURSOR:
  236.                     idLabel = IDS_CURSORIMAGELABEL;
  237.                     break;
  238.             }
  239.         }
  240.         else {
  241.             idLabel = IDS_NULL;
  242.         }
  243.     
  244.         SetDlgItemText(ghwndPropBar, DID_PROPBARIMAGELABEL, ids(idLabel));
  245.     
  246.         /*
  247.          * Get the handle to the combo box and clear out all items.
  248.          */
  249.         hwndCombo = GetDlgItem(ghwndPropBar, DID_PROPBARIMAGE);
  250.         SendMessage(hwndCombo, CB_RESETCONTENT, 0, 0);
  251.     
  252.         /*
  253.          * Fill the combo box with the images.
  254.          */
  255.         for (pImage = gpImageHead; pImage; pImage = pImage->pImageNext) {
  256.             i = (INT)SendMessage(hwndCombo, CB_INSERTSTRING, (WPARAM)-1,
  257.                     pImage->pDevice ?
  258.                     (DWORD)(LPSTR)pImage->pDevice->szDesc :
  259.                     (DWORD)(LPSTR)ids(IDS_UNKNOWNIMAGEFORMAT));
  260.     
  261.             SendMessage(hwndCombo, CB_SETITEMDATA, i, (DWORD)(LPSTR)pImage);
  262.         }
  263.     
  264.         /*
  265.          * Select the current image.
  266.          */
  267.         PropBarSetImage(gpImageCur);
  268.     
  269.         /*
  270.          * Show/Hide the HotSpot info depending on whether this is
  271.          * a cursor or not.
  272.          */
  273.         if (giType == FT_CURSOR) {
  274.             if (gpImageCur)
  275.                 PropBarSetHotSpot(gpImageCur->iHotspotX, gpImageCur->iHotspotY);
  276.             else
  277.                 PropBarClearHotSpot();
  278.     
  279.             PropBarShowHotSpot(TRUE);
  280.         }
  281.         else {
  282.             PropBarShowHotSpot(FALSE);
  283.         }
  284.     }
  285.     
  286.     
  287.     
  288.     /************************************************************************
  289.     * PropBarSetImage
  290.     *
  291.     *
  292.     * History:
  293.     *
  294.     ************************************************************************/
  295.     
  296.     VOID PropBarSetImage(
  297.         PIMAGEINFO pImage)
  298.     {
  299.         if (pImage)
  300.             SendDlgItemMessage(ghwndPropBar, DID_PROPBARIMAGE, CB_SELECTSTRING,
  301.                     (WPARAM)-1, (DWORD)(LPSTR)pImage->pDevice->szDesc);
  302.         else
  303.             SendDlgItemMessage(ghwndPropBar, DID_PROPBARIMAGE, CB_SETCURSEL,
  304.                     (WPARAM)-1, 0);
  305.     }
  306.     
  307.     
  308.     
  309.     /************************************************************************
  310.     * PropBarSetPos
  311.     *
  312.     *
  313.     * Arguments:
  314.     *
  315.     * History:
  316.     *
  317.     ************************************************************************/
  318.     
  319.     VOID PropBarSetPos(
  320.         INT x,
  321.         INT y)
  322.     {
  323.         CHAR szBuf[CCHTEXTMAX];
  324.     
  325.         if (x != xSave || y != ySave) {
  326.             wsprintf(szBuf, "%d, %d", x, y);
  327.             SetDlgItemText(ghwndPropBar, DID_PROPBARPOS, szBuf);
  328.     
  329.             /*
  330.              *  Save them for the next time.
  331.              */
  332.             xSave = x;
  333.             ySave = y;
  334.         }
  335.     }
  336.     
  337.     
  338.     
  339.     /************************************************************************
  340.     * PropBarClearPos
  341.     *
  342.     *
  343.     * Arguments:
  344.     *
  345.     * History:
  346.     *
  347.     ************************************************************************/
  348.     
  349.     VOID PropBarClearPos(VOID)
  350.     {
  351.         SetDlgItemText(ghwndPropBar, DID_PROPBARPOS, "");
  352.     
  353.         /*
  354.          * Reset the cache variables so that the next "set" of
  355.          * the position is sure to update the display fields.
  356.          */
  357.         xSave = IMPOSSIBLEVALUE;
  358.         ySave = IMPOSSIBLEVALUE;
  359.     }
  360.     
  361.     
  362.     
  363.     /************************************************************************
  364.     * PropBarSetSize
  365.     *
  366.     *
  367.     * Arguments:
  368.     *
  369.     * History:
  370.     *
  371.     ************************************************************************/
  372.     
  373.     VOID PropBarSetSize(
  374.         POINT pt1,
  375.         POINT pt2)
  376.     {
  377.         CHAR szBuf[CCHTEXTMAX];
  378.         INT cx;
  379.         INT cy;
  380.     
  381.         NormalizePoints(&pt1, &pt2);
  382.         cx = ((pt2.x - pt1.x) / gZoomFactor) + 1;
  383.         cy = ((pt2.y - pt1.y) / gZoomFactor) + 1;
  384.     
  385.         if (cx != cxSave || cy != cySave) {
  386.             wsprintf(szBuf, "%dx%d", cx, cy);
  387.             SetDlgItemText(ghwndPropBar, DID_PROPBARSIZE, szBuf);
  388.     
  389.             /*
  390.              *  Save them for the next time.
  391.              */
  392.             cxSave = cx;
  393.             cySave = cy;
  394.         }
  395.     }
  396.     
  397.     
  398.     
  399.     /************************************************************************
  400.     * PropBarClearSize
  401.     *
  402.     *
  403.     * Arguments:
  404.     *
  405.     * History:
  406.     *
  407.     ************************************************************************/
  408.     
  409.     VOID PropBarClearSize(VOID)
  410.     {
  411.         SetDlgItemText(ghwndPropBar, DID_PROPBARSIZE, "");
  412.     
  413.         /*
  414.          * Reset the cache variables so that the next "set" of
  415.          * the position is sure to update the display fields.
  416.          */
  417.         cxSave = IMPOSSIBLEVALUE;
  418.         cySave = IMPOSSIBLEVALUE;
  419.     }
  420.     
  421.     
  422.     
  423.     /************************************************************************
  424.     * PropBarSetHotSpot
  425.     *
  426.     *
  427.     * Arguments:
  428.     *
  429.     * History:
  430.     *
  431.     ************************************************************************/
  432.     
  433.     VOID PropBarSetHotSpot(
  434.         INT xHotSpot,
  435.         INT yHotSpot)
  436.     {
  437.         CHAR szBuf[CCHTEXTMAX];
  438.     
  439.         if (xHotSpot != xHotSpotSave || yHotSpot != yHotSpotSave) {
  440.             wsprintf(szBuf, "%d, %d", xHotSpot, yHotSpot);
  441.             SetDlgItemText(ghwndPropBar, DID_PROPBARHOTSPOT, szBuf);
  442.     
  443.             /*
  444.              *  Save them for the next time.
  445.              */
  446.             xHotSpotSave = xHotSpot;
  447.             yHotSpotSave = yHotSpot;
  448.         }
  449.     }
  450.     
  451.     
  452.     
  453.     /************************************************************************
  454.     * PropBarClearHotSpot
  455.     *
  456.     *
  457.     * Arguments:
  458.     *
  459.     * History:
  460.     *
  461.     ************************************************************************/
  462.     
  463.     VOID PropBarClearHotSpot(VOID)
  464.     {
  465.         SetDlgItemText(ghwndPropBar, DID_PROPBARHOTSPOT, "");
  466.     
  467.         /*
  468.          * Reset the cache variables so that the next "set" of
  469.          * the hotspot is sure to update the display fields.
  470.          */
  471.         xHotSpotSave = IMPOSSIBLEVALUE;
  472.         yHotSpotSave = IMPOSSIBLEVALUE;
  473.     }
  474.     
  475.     
  476.     
  477.     /************************************************************************
  478.     * PropBarShowHotSpot
  479.     *
  480.     *
  481.     * Arguments:
  482.     *
  483.     * History:
  484.     *
  485.     ************************************************************************/
  486.     VOID PropBarShowHotSpot(
  487.         BOOL fShow)
  488.     {
  489.         ShowWindow(GetDlgItem(ghwndPropBar, DID_PROPBARHOTSPOTLABEL),
  490.                 fShow ? SW_SHOWNA : SW_HIDE);
  491.         ShowWindow(GetDlgItem(ghwndPropBar, DID_PROPBARHOTSPOT),
  492.                 fShow ? SW_SHOWNA : SW_HIDE);
  493.     }
  494.