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

  1.     /****************************************************************************/
  2.     /*                                                                          */
  3.     /*                 Copyright (C) 1987-1996 Microsoft Corp.                */
  4.     /*                           All Rights Reserved                            */
  5.     /*                                                                          */
  6.     /****************************************************************************/
  7.     /****************************** Module Header *******************************
  8.     * Module Name: menucmd.c
  9.     *
  10.     * Contains routines to dispatch the menu commands.
  11.     *
  12.     * History:
  13.     *
  14.     ****************************************************************************/
  15.     
  16.     #include "imagedit.h"
  17.     #include "dialogs.h"
  18.     #include "ids.h"
  19.     
  20.     #include <direct.h>
  21.     #include <string.h>
  22.     
  23.     STATICFN INT GetHelpContext(INT idSubject, PHELPMAP phmap);
  24.     
  25.     
  26.     
  27.     /************************************************************************
  28.     * InitMenu
  29.     *
  30.     * This function grays/enables and checks/unchecks the menu items
  31.     * appropriately for the given state.
  32.     *
  33.     * Arguments:
  34.     *   HMENU hMenu - The menu handle.
  35.     *
  36.     * History:
  37.     *
  38.     ************************************************************************/
  39.     
  40.     VOID InitMenu(
  41.         HMENU hMenu)
  42.     {
  43.         BOOL fEnable;
  44.         INT i;
  45.     
  46.         MyEnableMenuItem(hMenu, MENU_FILE_SAVE, fImageDirty || fFileDirty);
  47.         MyEnableMenuItem(hMenu, MENU_FILE_SAVEAS, gpImageHead);
  48.         MyEnableMenuItem(hMenu, MENU_FILE_LOADCOLORS, gnColors != 2);
  49.         MyEnableMenuItem(hMenu, MENU_FILE_SAVECOLORS, gnColors != 2);
  50.     
  51.         /*
  52.          * Only enable the option to restore the default colors if this
  53.          * is not a monochrome palette and at least one of the colors
  54.          * has been changed.
  55.          */
  56.         fEnable = FALSE;
  57.         if (gnColors != 2) {
  58.             for (i = 0; i < COLORSMAX; i++) {
  59.                 if (gargbColor[i] != gargbDefaultColor[i]) {
  60.                     fEnable = TRUE;
  61.                     break;
  62.                 }
  63.             }
  64.         }
  65.     
  66.         MyEnableMenuItem(hMenu, MENU_FILE_DEFAULTCOLORS, fEnable);
  67.     
  68.         MyEnableMenuItem(hMenu, MENU_EDIT_UNDO, ghbmUndo);
  69.         MyEnableMenuItem(hMenu, MENU_EDIT_RESTORE,
  70.                 gpImageCur && gpImageCur->DIBPtr && fImageDirty);
  71.         MyEnableMenuItem(hMenu, MENU_EDIT_COPY, gpImageCur);
  72.         MyEnableMenuItem(hMenu, MENU_EDIT_PASTE,
  73.                 gpImageCur && IsClipboardFormatAvailable(CF_BITMAP));
  74.         MyEnableMenuItem(hMenu, MENU_EDIT_CLEAR, gpImageCur);
  75.     
  76.         /*
  77.          * We can add new images if the current image is not a bitmap,
  78.          * and we have possible new images to add, and there is a current
  79.          * file being edited.  This last case is checked by looking to
  80.          * see that there is either a current file name, or there is a
  81.          * current image (the case for new files).
  82.          */
  83.         MyEnableMenuItem(hMenu, MENU_EDIT_NEWIMAGE,
  84.                 giType != FT_BITMAP &&
  85.                 ((giType == FT_ICON) ?
  86.                 (gnImages < gnIconDevices) : (gnImages < gnCursorDevices)) &&
  87.                 (gpImageCur || gpszFileName));
  88.     
  89.         MyEnableMenuItem(hMenu, MENU_EDIT_SELECTIMAGE,
  90.                 giType != FT_BITMAP && gnImages > 0);
  91.         MyEnableMenuItem(hMenu, MENU_EDIT_DELETEIMAGE,
  92.                 giType != FT_BITMAP && gnImages > 0);
  93.     
  94.         MyCheckMenuItem(hMenu, MENU_OPTIONS_GRID, gfGrid);
  95.         MyCheckMenuItem(hMenu, MENU_OPTIONS_BRUSH2, gnBrushSize == 2);
  96.         MyCheckMenuItem(hMenu, MENU_OPTIONS_BRUSH3, gnBrushSize == 3);
  97.         MyCheckMenuItem(hMenu, MENU_OPTIONS_BRUSH4, gnBrushSize == 4);
  98.         MyCheckMenuItem(hMenu, MENU_OPTIONS_BRUSH5, gnBrushSize == 5);
  99.         MyCheckMenuItem(hMenu, MENU_OPTIONS_SHOWCOLOR, gfShowColor);
  100.         MyCheckMenuItem(hMenu, MENU_OPTIONS_SHOWVIEW, gfShowView);
  101.         MyCheckMenuItem(hMenu, MENU_OPTIONS_SHOWTOOLBOX, gfShowToolbox);
  102.     }
  103.     
  104.     
  105.     
  106.     /************************************************************************
  107.     * MenuCmd
  108.     *
  109.     * Dispatches all the menu commands.
  110.     *
  111.     * Arguments:
  112.     *
  113.     * History:
  114.     *
  115.     ************************************************************************/
  116.     
  117.     VOID MenuCmd(
  118.         INT item)
  119.     {
  120.         switch (item) {
  121.     
  122.             /*
  123.              * File menu ----------------------------------------------------
  124.              */
  125.     
  126.             case MENU_FILE_OPEN:
  127.                 if (VerifySaveFile())
  128.                     OpenAFile();
  129.     
  130.                 break;
  131.     
  132.             case MENU_FILE_NEW:
  133.                 if (VerifySaveFile()) {
  134.                     if (DlgBox(DID_RESOURCETYPE,
  135.                             (WNDPROC)ResourceTypeDlgProc) == IDOK) {
  136.                         /*
  137.                          * Clear out the current resource.
  138.                          */
  139.                         ClearResource();
  140.     
  141.                         if (iNewFileType == FT_BITMAP)
  142.                             DlgBox(DID_BITMAPSIZE, (WNDPROC)BitmapSizeDlgProc);
  143.                         else
  144.                             ImageNewDialog(iNewFileType);
  145.                     }
  146.                 }
  147.     
  148.                 break;
  149.     
  150.             case MENU_FILE_SAVE:
  151.                 SaveFile(FALSE);
  152.                 break;
  153.     
  154.             case MENU_FILE_SAVEAS:
  155.                 SaveFile(TRUE);
  156.                 break;
  157.     
  158.             case MENU_FILE_LOADCOLORS:
  159.                 LoadColorFile();
  160.                 break;
  161.     
  162.             case MENU_FILE_SAVECOLORS:
  163.                 SaveColorFile();
  164.                 break;
  165.     
  166.             case MENU_FILE_DEFAULTCOLORS:
  167.                 RestoreDefaultColors();
  168.                 break;
  169.     
  170.             case MENU_FILE_EXIT:
  171.                 SendMessage(ghwndMain, WM_SYSCOMMAND, SC_CLOSE, 0L);
  172.                 break;
  173.     
  174.             /*
  175.              * Edit menu ----------------------------------------------------
  176.              */
  177.     
  178.             case MENU_EDIT_UNDO:
  179.                 ImageUndo();
  180.                 break;
  181.     
  182.             case MENU_EDIT_RESTORE:
  183.                 /*
  184.                  * Reopen the most recently retained image (without
  185.                  * prompting for a save).
  186.                  */
  187.                 ImageOpen2(gpImageCur);
  188.                 break;
  189.     
  190.             case MENU_EDIT_COPY:
  191.                 CopyImageClip();
  192.                 break;
  193.     
  194.             case MENU_EDIT_PASTE:
  195.                 PasteImageClip();
  196.                 break;
  197.     
  198.             case MENU_EDIT_CLEAR:
  199.                 ImageUpdateUndo();
  200.                 ImageDCClear();
  201.                 ViewUpdate();
  202.                 break;
  203.     
  204.             case MENU_EDIT_NEWIMAGE:
  205.                 ImageNewDialog(giType);
  206.                 break;
  207.     
  208.             case MENU_EDIT_SELECTIMAGE:
  209.                 ImageSelectDialog();
  210.                 break;
  211.     
  212.             case MENU_EDIT_DELETEIMAGE:
  213.                 ImageDelete();
  214.                 break;
  215.     
  216.             /*
  217.              * Options menu -------------------------------------------------
  218.              */
  219.     
  220.             case MENU_OPTIONS_GRID:
  221.                 /*
  222.                  * Toggle the grid state.
  223.                  */
  224.                 gfGrid ^= TRUE;
  225.     
  226.                 /*
  227.                  * Repaint the workspace window to show/remove the grid.
  228.                  */
  229.                 WorkUpdate();
  230.     
  231.                 break;
  232.     
  233.             case MENU_OPTIONS_BRUSH2:
  234.             case MENU_OPTIONS_BRUSH3:
  235.             case MENU_OPTIONS_BRUSH4:
  236.             case MENU_OPTIONS_BRUSH5:
  237.                 switch (item) {
  238.                     case MENU_OPTIONS_BRUSH2:
  239.                         gnBrushSize = 2;
  240.                         break;
  241.     
  242.                     case MENU_OPTIONS_BRUSH3:
  243.                         gnBrushSize = 3;
  244.                         break;
  245.     
  246.                     case MENU_OPTIONS_BRUSH4:
  247.                         gnBrushSize = 4;
  248.                         break;
  249.     
  250.                     case MENU_OPTIONS_BRUSH5:
  251.                         gnBrushSize = 5;
  252.                         break;
  253.                 }
  254.     
  255.                 break;
  256.     
  257.             case MENU_OPTIONS_SHOWCOLOR:
  258.                 /*
  259.                  * Toggle the state of the color palette.
  260.                  */
  261.                 gfShowColor = gfShowColor ? FALSE : TRUE;
  262.                 ColorShow(gfShowColor);
  263.                 break;
  264.     
  265.             case MENU_OPTIONS_SHOWVIEW:
  266.                 /*
  267.                  * Toggle the state of the view window.
  268.                  */
  269.                 gfShowView = gfShowView ? FALSE : TRUE;
  270.                 ViewShow(gfShowView);
  271.                 break;
  272.     
  273.             case MENU_OPTIONS_SHOWTOOLBOX:
  274.                 /*
  275.                  * Toggle the state of the Toolbox.
  276.                  */
  277.                 gfShowToolbox = gfShowToolbox ? FALSE : TRUE;
  278.                 ToolboxShow(gfShowToolbox);
  279.                 break;
  280.     
  281.             /*
  282.              * Help menu ----------------------------------------------------
  283.              */
  284.     
  285.             case MENU_HELP_CONTENTS:
  286.                 WinHelp(ghwndMain, gszHelpFile, HELP_CONTENTS, 0L);
  287.                 break;
  288.     
  289.             case MENU_HELP_SEARCH:
  290.                 /*
  291.                  * Tell winhelp to be sure this app's help file is current,
  292.                  * then invoke a search with an empty starting key.
  293.                  */
  294.                 WinHelp(ghwndMain, gszHelpFile, HELP_FORCEFILE, 0);
  295.                 WinHelp(ghwndMain, gszHelpFile, HELP_PARTIALKEY, (DWORD)(LPSTR)"");
  296.                 break;
  297.     
  298.             case MENU_HELP_ABOUT:
  299.                 DlgBox(DID_ABOUT, (WNDPROC)AboutDlgProc);
  300.                 break;
  301.     
  302.             /*
  303.              * Hidden menu commands (accessed by accelerators) --------------
  304.              */
  305.     
  306.             case MENU_HIDDEN_TOCOLORPAL:
  307.                 if (IsWindowVisible(ghwndColor))
  308.                     SetFocus(ghwndColor);
  309.     
  310.                 break;
  311.     
  312.             case MENU_HIDDEN_TOVIEW:
  313.                 if (IsWindowVisible(ghwndView))
  314.                     SetFocus(ghwndView);
  315.     
  316.                 break;
  317.     
  318.             case MENU_HIDDEN_TOTOOLBOX:
  319.                 if (IsWindowVisible(ghwndToolbox))
  320.                     SetFocus(ghwndToolbox);
  321.     
  322.                 break;
  323.     
  324.             case MENU_HIDDEN_TOPROPBAR:
  325.                 SetFocus(ghwndPropBar);
  326.                 break;
  327.         }
  328.     }
  329.     
  330.     
  331.     
  332.     /************************************************************************
  333.     * MsgFilterHookFunc
  334.     *
  335.     * This is the exported message filter function that is hooked into
  336.     * the message stream for detecting the pressing of the F1 key, at
  337.     * which time it calls up the appropriate help.
  338.     *
  339.     * Arguments:
  340.     *
  341.     * History:
  342.     *
  343.     ************************************************************************/
  344.     
  345.     DWORD  APIENTRY MsgFilterHookFunc(
  346.         INT nCode,
  347.         WPARAM wParam,
  348.         LPMSG lpMsg)
  349.     {
  350.         if ((nCode == MSGF_MENU || nCode == MSGF_DIALOGBOX) &&
  351.                 (lpMsg->message == WM_KEYDOWN && lpMsg->wParam == VK_F1)) {
  352.             /*
  353.              * Display help.
  354.              */
  355.             ShowHelp((nCode == MSGF_MENU) ? TRUE : FALSE);
  356.     
  357.             /*
  358.              * Tell Windows to swallow this message.
  359.              */
  360.             return 1;
  361.         }
  362.     
  363.         return DefHookProc(nCode, wParam, (LONG)lpMsg, &ghhkMsgFilter);
  364.     }
  365.     
  366.     
  367.     
  368.     /************************************************************************
  369.     * ShowHelp
  370.     *
  371.     * This function is called when the user has requested help.  It will
  372.     * look at the menu state (if fMenuHelp is TRUE) or which dialog
  373.     * is currently up to determine the help topic, then it calls WinHelp.
  374.     *
  375.     * Arguments:
  376.     *   BOOL fMenuHelp - TRUE if this help is for a menu (help was requested
  377.     *                    in the menu modal loop).  If FALSE, general help
  378.     *                    or help for a dialog is assumed.
  379.     *
  380.     * History:
  381.     *
  382.     ************************************************************************/
  383.     
  384.     VOID ShowHelp(
  385.         BOOL fMenuHelp)
  386.     {
  387.         INT nHelpContext = 0;
  388.         HWND hwndFocus;
  389.     
  390.         if (fMenuHelp) {
  391.             nHelpContext = GetHelpContext(gMenuSelected, gahmapMenu);
  392.         }
  393.         else {
  394.             /*
  395.              * Look for help for the current dialog.
  396.              */
  397.             if (gidCurrentDlg) {
  398.                 nHelpContext = GetHelpContext(gidCurrentDlg, gahmapDialog);
  399.             }
  400.             else {
  401.                 /*
  402.                  * There is no current dialog.  Is the window with the
  403.                  * focus a control on the Properties Bar?
  404.                  */
  405.                 if ((hwndFocus = GetFocus()) && IsChild(ghwndPropBar, hwndFocus))
  406.                     nHelpContext = GetHelpContext(DID_PROPBAR, gahmapDialog);
  407.             }
  408.         }
  409.     
  410.         /*
  411.          * If there is help context, display it.  Otherwise display
  412.          * the Contents screen.
  413.          */
  414.         if (nHelpContext)
  415.             WinHelp(ghwndMain, gszHelpFile, HELP_CONTEXT, nHelpContext);
  416.         else
  417.             WinHelp(ghwndMain, gszHelpFile, HELP_CONTENTS, 0L);
  418.     }
  419.     
  420.     
  421.     
  422.     /************************************************************************
  423.     * GetHelpContext
  424.     *
  425.     * This function takes a subject and returns its matching help
  426.     * context id from the given HELPMAP table.
  427.     *
  428.     * Arguments:
  429.     *   INT idSubject   - ID of the subject to find the help context for.
  430.     *   PHELPMAP phmap  - The help map table.  It is assumed that the
  431.     *                     last entry in the table has a NULL subject id.
  432.     *
  433.     * History:
  434.     *
  435.     ************************************************************************/
  436.     
  437.     STATICFN INT GetHelpContext(
  438.         INT idSubject,
  439.         PHELPMAP phmap)
  440.     {
  441.         while (phmap->idSubject) {
  442.             if (phmap->idSubject == idSubject)
  443.                 return phmap->HelpContext;
  444.     
  445.             phmap++;
  446.         }
  447.     
  448.         return 0;
  449.     }
  450.     
  451.     
  452.     
  453.     /************************************************************************
  454.     * AboutDlgProc
  455.     *
  456.     * This is the About Box dialog procedure.
  457.     *
  458.     * History:
  459.     *
  460.     ************************************************************************/
  461.     
  462.     DIALOGPROC AboutDlgProc(
  463.         HWND hwnd,
  464.         UINT msg,
  465.         WPARAM wParam,
  466.         LPARAM lParam)
  467.     {
  468.         switch (msg) {
  469.             case WM_INITDIALOG:
  470.                 {
  471.                     CHAR szVersion[CCHTEXTMAX];
  472.     
  473.                     strcpy(szVersion, ids(IDS_VERSION));
  474.                     strcat(szVersion, ids(IDS_VERSIONMINOR));
  475.     
  476.     #ifdef DBG
  477.                     strcat(szVersion, " (debug)");
  478.     #endif
  479.     
  480.                     SetDlgItemText(hwnd, DID_ABOUTVERSION, szVersion);
  481.                     CenterWindow(hwnd);
  482.                 }
  483.     
  484.                 return TRUE;
  485.     
  486.             case WM_COMMAND:
  487.                 EndDialog(hwnd, IDOK);
  488.                 return TRUE;
  489.     
  490.             default:
  491.                 return FALSE;
  492.         }
  493.     }
  494.