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 / imagedlg.c < prev    next >
Text File  |  1994-06-01  |  20KB  |  656 lines

  1.     /***************************************************************************
  2.      *                                                                         *
  3.      *  MODULE      : Imagedlg.c                                               *
  4.      *                                                                         *
  5.      *  PURPOSE     : Dialog functions for ImagEdit's dialogs.                 *
  6.      *                                                                         *
  7.      *  HISTORY     : 3/03/89 by LR                                            *
  8.      *                                                                         *
  9.      ***************************************************************************/
  10.     
  11.     #include "imagedit.h"
  12.     #include "dialogs.h"
  13.     #include "iehelp.h"
  14.  
  15.      #include <windowsx.h>
  16.     
  17.     STATICFN BOOL NEAR NewImageOK(HWND hwnd, INT iType);
  18.     STATICFN VOID NEAR SelectImageInit(HWND hwnd);
  19.     STATICFN BOOL NEAR SelectImageOK(HWND hwnd);
  20.     
  21.     
  22.     
  23.     /************************************************************************
  24.     * DlgBox
  25.     *
  26.     * This function basically does a DialogBox.
  27.     *
  28.     * Arguments:
  29.     *   INT idDlg       = Ordinal name of the dialog.
  30.     *   WNDPROC lpfnDlg = Dialog procedure to use (this function will
  31.     *                     call Make/FreeProcInstance).
  32.     *
  33.     * Returns:
  34.     *     What DialogBox returned.
  35.     *
  36.     * History:
  37.     *
  38.     ************************************************************************/
  39.     
  40.     INT DlgBox(
  41.         INT idDlg,
  42.         WNDPROC lpfnDlg)
  43.     {
  44.         WNDPROC lpfnDlgInst;
  45.         INT nResult;
  46.         INT idPrevDlg;
  47.     
  48.         EnteringDialog(idDlg, &idPrevDlg, TRUE);
  49.         lpfnDlgInst = (WNDPROC)MakeProcInstance((FARPROC)lpfnDlg, ghInst);
  50.         nResult = DialogBox(ghInst, MAKEINTRESOURCE(idDlg),
  51.                 ghwndMain, lpfnDlgInst);
  52.         FreeProcInstance(lpfnDlgInst);
  53.         EnteringDialog(idPrevDlg, NULL, FALSE);
  54.     
  55.         return nResult;
  56.     }
  57.     
  58.     
  59.     
  60.     /************************************************************************
  61.     * EnteringDialog
  62.     *
  63.     * This function enables or disables things based on whether we are
  64.     * going to show one of the editor's dialogs.  It must be called
  65.     * before and after showing a dialog box.
  66.     *
  67.     * Arguments:
  68.     *   INT idDlg       - Ordinal name of the dialog.
  69.     *   PINT pidPrevDlg - Points to where to save the id of the previous
  70.     *                     (current) dialog.  If fEntering is FALSE, this
  71.     *                     is not used and should be NULL.
  72.     *   BOOL fEntering  - TRUE if about ready to show the dialog.  FALSE if
  73.     *                     the dialog was just dismissed.  For the FALSE case,
  74.     *                     the idDlg should be zero, or the id of the previous
  75.     *                     dialog.
  76.     *
  77.     * History:
  78.     *
  79.     ************************************************************************/
  80.     
  81.     VOID EnteringDialog(
  82.         INT idDlg,
  83.         PINT pidPrevDlg,
  84.         BOOL fEntering)
  85.     {
  86.         /*
  87.          * If we are entering a new dialog, save the previous dialog
  88.          * in the place specified.
  89.          */
  90.         if (fEntering)
  91.             *pidPrevDlg = gidCurrentDlg;
  92.     
  93.         gidCurrentDlg = idDlg;
  94.     
  95.         if (ghwndToolbox)
  96.             EnableWindow(ghwndToolbox, !fEntering);
  97.     
  98.         if (ghwndColor)
  99.             EnableWindow(ghwndColor, !fEntering);
  100.     
  101.         if (ghwndView)
  102.             EnableWindow(ghwndView, !fEntering);
  103.     }
  104.     
  105.     
  106.     
  107.     /************************************************************************
  108.     * ImageNewDialog
  109.     *
  110.     *
  111.     *
  112.     * History:
  113.     *
  114.     ************************************************************************/
  115.     
  116.     VOID ImageNewDialog(
  117.         INT iType)
  118.     {
  119.         switch (iType) {
  120.             case FT_ICON:
  121.                 DlgBox(DID_NEWICONIMAGE, (WNDPROC)NewIconImageDlgProc);
  122.                 break;
  123.     
  124.             case FT_CURSOR:
  125.                 DlgBox(DID_NEWCURSORIMAGE, (WNDPROC)NewCursorImageDlgProc);
  126.                 break;
  127.         }
  128.     }
  129.     
  130.     
  131.     
  132.     /************************************************************************
  133.     * ImageSelectDialog
  134.     *
  135.     *
  136.     *
  137.     * History:
  138.     *
  139.     ************************************************************************/
  140.     
  141.     VOID ImageSelectDialog(VOID)
  142.     {
  143.         switch (giType) {
  144.             case FT_ICON:
  145.                 DlgBox(DID_SELECTICONIMAGE, (WNDPROC)SelectImageDlgProc);
  146.                 break;
  147.     
  148.             case FT_CURSOR:
  149.                 DlgBox(DID_SELECTCURSORIMAGE, (WNDPROC)SelectImageDlgProc);
  150.                 break;
  151.         }
  152.     }
  153.     
  154.     
  155.     
  156.     /************************************************************************
  157.     * ResourceTypeDlgProc
  158.     *
  159.     * Gets from the user the type of resource image that they want
  160.     * to edit (bitmap, icon or cursor).
  161.     *
  162.     * History:
  163.     *
  164.     ************************************************************************/
  165.     
  166.     DIALOGPROC ResourceTypeDlgProc(
  167.         HWND hwnd,
  168.         UINT msg,
  169.         WPARAM wParam,
  170.         LPARAM lParam)
  171.     {
  172.         INT idChecked;
  173.     
  174.         switch (msg) {
  175.             case WM_INITDIALOG:
  176.                 switch (giType) {
  177.                     case FT_BITMAP:
  178.                         idChecked = DID_RESOURCETYPEBITMAP;
  179.                         break;
  180.     
  181.                     case FT_ICON:
  182.                         idChecked = DID_RESOURCETYPEICON;
  183.                         break;
  184.     
  185.                     case FT_CURSOR:
  186.                         idChecked = DID_RESOURCETYPECURSOR;
  187.                         break;
  188.                 }
  189.     
  190.                 CheckDlgButton(hwnd, idChecked, 1);
  191.     
  192.                 CenterWindow(hwnd);
  193.     
  194.                 break;
  195.     
  196.             case WM_COMMAND:
  197.                 switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  198.                     case IDOK:
  199.                         if (IsDlgButtonChecked(hwnd, DID_RESOURCETYPEBITMAP))
  200.                             iNewFileType = FT_BITMAP;
  201.                         else if (IsDlgButtonChecked(hwnd, DID_RESOURCETYPEICON))
  202.                             iNewFileType = FT_ICON;
  203.                         else
  204.                             iNewFileType = FT_CURSOR;
  205.     
  206.                         EndDialog(hwnd, IDOK);
  207.                         break;
  208.     
  209.                     case IDCANCEL:
  210.                         EndDialog(hwnd, IDCANCEL);
  211.                         break;
  212.     
  213.                     case IDHELP:
  214.                         WinHelp(ghwndMain, gszHelpFile, HELP_CONTEXT,
  215.                                 HELPID_RESOURCETYPE);
  216.                         break;
  217.                 }
  218.     
  219.                 break;
  220.     
  221.             default:
  222.                 return FALSE;
  223.         }
  224.     
  225.         return TRUE;
  226.     }
  227.     
  228.     
  229.     
  230.     /************************************************************************
  231.     * NewIconImageDlgProc
  232.     *
  233.     *
  234.     *
  235.     * Arguments:
  236.     *
  237.     * History:
  238.     *
  239.     ************************************************************************/
  240.     
  241.     DIALOGPROC NewIconImageDlgProc(
  242.         HWND hwnd,
  243.         UINT msg,
  244.         WPARAM wParam,
  245.         LPARAM lParam)
  246.     {
  247.         HWND hwndList;
  248.         INT iSel;
  249.         PDEVICE pDevice;
  250.     
  251.         switch (msg) {
  252.             case WM_INITDIALOG:
  253.                 hwndList = GetDlgItem(hwnd, DID_NEWIMAGELIST);
  254.                 SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
  255.     
  256.                 for (pDevice = gpIconDeviceHead; pDevice;
  257.                         pDevice = pDevice->pDeviceNext) {
  258.                     if (!DeviceLinkUsed(pDevice)) {
  259.                         iSel = (INT)SendMessage(hwndList, LB_INSERTSTRING, (WPARAM)-1,
  260.                                 (LONG)(LPSTR)pDevice->szDesc);
  261.                         SendMessage(hwndList, LB_SETITEMDATA, iSel,
  262.                                 (DWORD)(LPSTR)pDevice);
  263.                     }
  264.                 }
  265.     
  266.                 /*
  267.                  * Select the first item.
  268.                  */
  269.                 SendMessage(hwndList, LB_SETCURSEL, 0, 0L);
  270.     
  271.                 CenterWindow(hwnd);
  272.     
  273.                 break;
  274.     
  275.             case WM_COMMAND:
  276.                 switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  277.                     case DID_NEWIMAGELIST:
  278.                         if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_DBLCLK) {
  279.                             if (NewImageOK(hwnd, FT_ICON))
  280.                                 EndDialog(hwnd, IDOK);
  281.                         }
  282.     
  283.                         break;
  284.     
  285.                     case IDOK:
  286.                         if (NewImageOK(hwnd, FT_ICON))
  287.                             EndDialog(hwnd, IDOK);
  288.     
  289.                         break;
  290.     
  291.                     case IDCANCEL:
  292.                         EndDialog(hwnd, IDCANCEL);
  293.                         break;
  294.     
  295.                     case IDHELP:
  296.                         WinHelp(ghwndMain, gszHelpFile, HELP_CONTEXT,
  297.                                 HELPID_NEWICONIMAGE);
  298.                         break;
  299.                 }
  300.     
  301.                 break;
  302.     
  303.             default:
  304.                 return FALSE;
  305.         }
  306.     
  307.         return TRUE;
  308.     }
  309.     
  310.     
  311.     
  312.     /************************************************************************
  313.     * NewImageOK
  314.     *
  315.     * Processes the selection of a new image from the New Icon (Cursor)
  316.     * Image dialog procedures.
  317.     *
  318.     * History:
  319.     *
  320.     ************************************************************************/
  321.     
  322.     STATICFN BOOL NEAR NewImageOK(
  323.         HWND hwnd,
  324.         INT iType)
  325.     {
  326.         HWND hwndLB;
  327.         INT iSelect;
  328.         PDEVICE pDevice;
  329.     
  330.         hwndLB = GetDlgItem(hwnd, DID_NEWIMAGELIST);
  331.     
  332.         if ((iSelect = (INT)SendMessage(hwndLB, LB_GETCURSEL, 0, 0)) != LB_ERR) {
  333.             /*
  334.              * Save away the current image.
  335.              */
  336.             ImageSave();
  337.     
  338.             pDevice = (PDEVICE)SendMessage(hwndLB, LB_GETITEMDATA, iSelect, 0);
  339.     
  340.             if (ImageNew(pDevice))
  341.                 return TRUE;
  342.         }
  343.     
  344.         return FALSE;
  345.     }
  346.     
  347.     
  348.     
  349.     /************************************************************************
  350.     * NewCursorImageDlgProc
  351.     *
  352.     *
  353.     *
  354.     * Arguments:
  355.     *
  356.     * History:
  357.     *
  358.     ************************************************************************/
  359.     
  360.     DIALOGPROC NewCursorImageDlgProc(
  361.         HWND hwnd,
  362.         UINT msg,
  363.         WPARAM wParam,
  364.         LPARAM lParam)
  365.     {
  366.         HWND hwndList;
  367.         INT iSel;
  368.         PDEVICE pDevice;
  369.     
  370.         switch (msg) {
  371.             case WM_INITDIALOG:
  372.                 hwndList = GetDlgItem(hwnd, DID_NEWIMAGELIST);
  373.                 SendMessage(hwndList, LB_RESETCONTENT, 0, 0);
  374.     
  375.                 for (pDevice = gpCursorDeviceHead; pDevice;
  376.                         pDevice = pDevice->pDeviceNext) {
  377.                     if (!DeviceLinkUsed(pDevice)) {
  378.                         iSel = (INT)SendMessage(hwndList, LB_INSERTSTRING, (WPARAM)-1,
  379.                                 (LONG)(LPSTR)pDevice->szDesc);
  380.                         SendMessage(hwndList, LB_SETITEMDATA, iSel,
  381.                                 (DWORD)(LPSTR)pDevice);
  382.                     }
  383.                 }
  384.     
  385.                 /*
  386.                  * Select the first item.
  387.                  */
  388.                 SendMessage(hwndList, LB_SETCURSEL, 0, 0L);
  389.     
  390.                 CenterWindow(hwnd);
  391.     
  392.                 break;
  393.     
  394.             case WM_COMMAND:
  395.                 switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  396.                     case DID_NEWIMAGELIST:
  397.                         if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_DBLCLK) {
  398.                             if (NewImageOK(hwnd, FT_CURSOR))
  399.                                 EndDialog(hwnd, IDOK);
  400.                         }
  401.     
  402.                         break;
  403.     
  404.                     case IDOK:
  405.                         if (NewImageOK(hwnd, FT_CURSOR))
  406.                             EndDialog(hwnd, IDOK);
  407.     
  408.                         break;
  409.     
  410.                     case IDCANCEL:
  411.                         EndDialog(hwnd, IDCANCEL);
  412.                         break;
  413.     
  414.                     case IDHELP:
  415.                         WinHelp(ghwndMain, gszHelpFile, HELP_CONTEXT,
  416.                                 HELPID_NEWCURSORIMAGE);
  417.                         break;
  418.                 }
  419.     
  420.                 break;
  421.     
  422.             default:
  423.                 return FALSE;
  424.         }
  425.     
  426.         return TRUE;
  427.     }
  428.     
  429.     
  430.     
  431.     /************************************************************************
  432.     * SelectImageDlgProc
  433.     *
  434.     * This is the Select Image dialog procedure.  This proc is used for both
  435.     * icon and cursor images.
  436.     *
  437.     * History:
  438.     *
  439.     ************************************************************************/
  440.     
  441.     DIALOGPROC SelectImageDlgProc(
  442.         HWND hwnd,
  443.         UINT msg,
  444.         WPARAM wParam,
  445.         LPARAM lParam)
  446.     {
  447.         switch (msg) {
  448.             case WM_INITDIALOG:
  449.                 SelectImageInit(hwnd);
  450.                 return TRUE;
  451.     
  452.             case WM_COMMAND:
  453.                 switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  454.                     case DID_SELECTIMAGELIST:
  455.                         if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_DBLCLK) {
  456.                             if (SelectImageOK(hwnd))
  457.                                 EndDialog(hwnd, IDOK);
  458.                         }
  459.     
  460.                         break;
  461.     
  462.                     case IDOK:
  463.                         if (SelectImageOK(hwnd))
  464.                             EndDialog(hwnd, IDOK);
  465.     
  466.                         break;
  467.     
  468.                     case IDCANCEL:
  469.                         EndDialog(hwnd, IDCANCEL);
  470.                         break;
  471.     
  472.                     case IDHELP:
  473.                         WinHelp(ghwndMain, gszHelpFile, HELP_CONTEXT,
  474.                                 (giType == FT_ICON) ?
  475.                                 HELPID_SELECTICONIMAGE : HELPID_SELECTCURSORIMAGE);
  476.                         break;
  477.                 }
  478.     
  479.                 return TRUE;
  480.     
  481.             default:
  482.                 return FALSE;
  483.         }
  484.     }
  485.     
  486.     
  487.     
  488.     /************************************************************************
  489.     * SelectImageInit
  490.     *
  491.     * Processes the WM_INITDIALOG message for the Open Image dialog
  492.     * procedure.
  493.     *
  494.     * This function fills the listbox with the names of all the current
  495.     * images for the current icon/cursor file.
  496.     *
  497.     * History:
  498.     *
  499.     ************************************************************************/
  500.     
  501.     STATICFN VOID NEAR SelectImageInit(
  502.         HWND hwnd)
  503.     {
  504.         HWND hwndLB;
  505.         INT i;
  506.         PIMAGEINFO pImage;
  507.     
  508.         hwndLB = GetDlgItem(hwnd, DID_SELECTIMAGELIST);
  509.     
  510.         for (pImage = gpImageHead; pImage; pImage = pImage->pImageNext) {
  511.             i = (INT)SendMessage(hwndLB, LB_INSERTSTRING, (WPARAM)-1,
  512.                     pImage->pDevice ?
  513.                     (DWORD)(LPSTR)pImage->pDevice->szDesc :
  514.                     (DWORD)(LPSTR)ids(IDS_UNKNOWNIMAGEFORMAT));
  515.     
  516.             SendMessage(hwndLB, LB_SETITEMDATA, i, (DWORD)(LPSTR)pImage);
  517.         }
  518.     
  519.         /*
  520.          * Select the first item.
  521.          */
  522.         SendMessage(hwndLB, LB_SETCURSEL, 0, 0L);
  523.     
  524.         CenterWindow(hwnd);
  525.     }
  526.     
  527.     
  528.     
  529.     /************************************************************************
  530.     * SelectImageOK
  531.     *
  532.     * Processes the selection of a new image from the Open Image
  533.     * dialog procedure.
  534.     *
  535.     * History:
  536.     *
  537.     ************************************************************************/
  538.     
  539.     STATICFN BOOL NEAR SelectImageOK(
  540.         HWND hwnd)
  541.     {
  542.         HWND hwndLB;
  543.         INT iSelect;
  544.         PIMAGEINFO pImage;
  545.     
  546.         hwndLB = GetDlgItem(hwnd, DID_SELECTIMAGELIST);
  547.     
  548.         if ((iSelect = (INT)SendMessage(hwndLB, LB_GETCURSEL, 0, 0)) == LB_ERR)
  549.             return FALSE;
  550.     
  551.         /*
  552.          * Get a pointer to the selected image (stored in the listbox
  553.          * items data field).
  554.          */
  555.         pImage = (PIMAGEINFO)SendMessage(hwndLB, LB_GETITEMDATA, iSelect, 0L);
  556.     
  557.         return ImageOpen(pImage);
  558.     }
  559.     
  560.     
  561.     
  562.     /************************************************************************
  563.     * BitmapSizeDlgProc
  564.     *
  565.     * Dialog that asks for the width, height and number of colors for
  566.     * a new bitmap file.
  567.     *
  568.     * The last values that the user successfully entered are remembered,
  569.     * and these values will be the defaults the next time that the
  570.     * dialog is invoked.
  571.     *
  572.     * History:
  573.     *
  574.     ************************************************************************/
  575.     
  576.     DIALOGPROC BitmapSizeDlgProc(
  577.         HWND hwnd,
  578.         UINT msg,
  579.         WPARAM wParam,
  580.         LPARAM lParam)
  581.     {
  582.         static INT cxLast = DEFAULTBITMAPWIDTH;
  583.         static INT cyLast = DEFAULTBITMAPHEIGHT;
  584.         static INT nColorsLast = DEFAULTBITMAPCOLORS;
  585.         INT cx;
  586.         INT cy;
  587.         BOOL fTranslated;
  588.         INT nColors;
  589.     
  590.         switch (msg) {
  591.             case WM_INITDIALOG:
  592.                 SetDlgItemInt(hwnd, DID_BITMAPSIZEWIDTH, cxLast, TRUE);
  593.                 SetDlgItemInt(hwnd, DID_BITMAPSIZEHEIGHT, cyLast, TRUE);
  594.     
  595.                 if (nColorsLast == 16)
  596.                     CheckRadioButton(hwnd, DID_BITMAPSIZE2, DID_BITMAPSIZE16,
  597.                             DID_BITMAPSIZE16);
  598.                 else
  599.                     CheckRadioButton(hwnd, DID_BITMAPSIZE2, DID_BITMAPSIZE16,
  600.                             DID_BITMAPSIZE2);
  601.     
  602.                 CenterWindow(hwnd);
  603.     
  604.                 break;
  605.     
  606.             case WM_COMMAND:
  607.                 switch (GET_WM_COMMAND_ID(wParam, lParam)) {
  608.                     case IDOK:
  609.                         cx = GetDlgItemInt(hwnd, DID_BITMAPSIZEWIDTH, &fTranslated, FALSE);
  610.                         if (!fTranslated) {
  611.                             SetFocus(GetDlgItem(hwnd, DID_BITMAPSIZEWIDTH));
  612.                             Message(MSG_ENTERANUMBER);
  613.                             break;
  614.                         }
  615.     
  616.                         cy = GetDlgItemInt(hwnd, DID_BITMAPSIZEHEIGHT, &fTranslated, FALSE);
  617.                         if (!fTranslated) {
  618.                             SetFocus(GetDlgItem(hwnd, DID_BITMAPSIZEHEIGHT));
  619.                             Message(MSG_ENTERANUMBER);
  620.                             break;
  621.                         }
  622.     
  623.                         if (IsDlgButtonChecked(hwnd, DID_BITMAPSIZE2))
  624.                             nColors = 2;
  625.                         else
  626.                             nColors = 16;
  627.     
  628.                         if (ImageNewBitmap(cx, cy, nColors)) {
  629.                             cxLast = cx;
  630.                             cyLast = cy;
  631.                             nColorsLast = nColors;
  632.     
  633.                             EndDialog(hwnd, IDOK);
  634.                         }
  635.     
  636.                         break;
  637.     
  638.                     case IDCANCEL:
  639.                         EndDialog(hwnd, IDCANCEL);
  640.                         break;
  641.     
  642.                     case IDHELP:
  643.                         WinHelp(ghwndMain, gszHelpFile, HELP_CONTEXT,
  644.                                 HELPID_BITMAPSIZE);
  645.                         break;
  646.                 }
  647.     
  648.                 break;
  649.     
  650.             default:
  651.                 return FALSE;
  652.         }
  653.     
  654.         return TRUE;
  655.     }
  656.