home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / pm / bmpsamp / procs.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  21KB  |  595 lines

  1. /******************************************************************************
  2. *
  3. *  File Name   : PROCS.C
  4. *
  5. *  Description : miscellaneous Jigsaw functions
  6. *
  7. *  Entry Points:
  8. *                SendCommand()
  9. *                ClientWndProc()
  10. *                WndProcPaint()
  11. *                WndProcCommand()
  12. *                FileOpen()
  13. *                SizeBitmap()
  14. *                MessageBox()
  15. *                JigsawOpenFilterProc()
  16. *
  17. *  Copyright (C) 1992 IBM Corporation
  18. *
  19. *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  20. *      sample code created by IBM Corporation. This sample code is not
  21. *      part of any standard or IBM product and is provided to you solely
  22. *      for  the purpose of assisting you in the development of your
  23. *      applications.  The code is provided "AS IS", without
  24. *      warranty of any kind.  IBM shall not be liable for any damages
  25. *      arising out of your use of the sample code, even if they have been
  26. *      advised of the possibility of such damages.                                                    *
  27. *
  28. ******************************************************************************/
  29.  
  30. #include "jigsaw.h"
  31. #include "jighelp.h"
  32. #include "globals.h"
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. #define PB_STAT_CANCEL 1
  37. #define PB_STAT_HELP   2
  38. #define DEFAULT_DIR    "C:\\OS2\\BITMAP"
  39.  
  40. /******************************************************************************/
  41. /*                                                                                                                         */
  42. /*  Define forward procedure references                                                  */
  43. /*                                                                                                                    */
  44. /******************************************************************************/
  45.  
  46. /* list of EA type strings */
  47. PSZ apszITL[]={"Jigsaw", (PSZ)NULL};
  48.  
  49.  
  50. /******************************************************************************
  51. *
  52. *  Name        : SendCommand
  53. *
  54. *  Description :
  55. *                SendCommand will attempt to post the required command and
  56. *                parameters to the asynchronous drawing thread's message
  57. *                queue.  The command will only be posted if the queue exists.
  58. *
  59. *  Parameters  : ULONG ulCommand, MPARAM mp1, MPARAM mp2
  60. *
  61. *  Return      : BOOL
  62. *
  63. ******************************************************************************/
  64.  
  65. BOOL SendCommand(ULONG ulCommand, MPARAM mp1, MPARAM mp2)
  66. {
  67.    if (!hmqAsync)                          /* no message queue; don't bother */
  68.       return( FALSE);
  69.  
  70.    switch (ulCommand)
  71.    {
  72.       case UM_DIE:
  73.       case UM_LEFTDOWN:
  74.       case UM_LEFTUP:
  75.       case UM_MOUSEMOVE:
  76.       case UM_DRAW:
  77.       case UM_HSCROLL:
  78.       case UM_VSCROLL:
  79.       case UM_ZOOM:
  80.       case UM_REDRAW:
  81.       case UM_SIZING:
  82.       case UM_JUMBLE:
  83.       case UM_LOAD:
  84.       case UM_CHAR:
  85.  
  86.          return( WinPostQueueMsg( hmqAsync    /* let worker thread handle it */
  87.                                 , ulCommand
  88.                                 , mp1
  89.                                 , mp2 ) );
  90.          break;
  91.  
  92.       default:
  93.          return( TRUE);
  94.  
  95.    }   /* end switch (ulCommand)  */
  96.  
  97. }   /* end SendCommand() */
  98.  
  99. /******************************************************************************
  100. *
  101. *  Name        : ClientWndProc
  102. *
  103. *  Description : ClientWndProc is the window procedure associated with the
  104. *                client window.
  105. *
  106. *  Parameters  : HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2
  107. *
  108. *  Return      : MRESULT
  109. *
  110. ******************************************************************************/
  111.  
  112.  
  113. MRESULT EXPENTRY ClientWndProc(HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
  114. {
  115.   CHAR   szTemp[128];
  116.   ULONG  ulSemPostCount;
  117.  
  118.   switch (msg)
  119.   {
  120.     case WM_CHAR:
  121.  
  122.       SendCommand( UM_CHAR, mp1, mp2);
  123.       break;
  124.  
  125.     case WM_CLOSE:
  126.       WinLoadString( habMain, 0, IDS_TERMINATE, sizeof(szTemp), (PSZ)szTemp );
  127.       if( WinMessageBox( HWND_DESKTOP
  128.                        , hwndFrame
  129.                        , szTemp
  130.                        , szTitle
  131.                        , 0
  132.                        , MB_CUAWARNING | MB_YESNO | MB_CUANOTIFICATION | MB_DEFBUTTON1)
  133.                == MBID_YES)
  134.           WinPostMsg( hwnd, WM_QUIT, NULL, NULL);
  135.       break;
  136.  
  137.     case WM_PAINT:
  138.       return( WndProcPaint());
  139.       break;
  140.  
  141.     /**************************************************************************/
  142.     /* Keys Help Message                                                                                                              */
  143.     /**************************************************************************/
  144.     case HM_QUERY_KEYS_HELP:
  145.       return (MRESULT)PANEL_HELPKEYS;   /* return id of key help panel */
  146.       break;
  147.  
  148.     /**************************************************************************/
  149.     /*                                                                                                                */
  150.     /**************************************************************************/
  151.     case WM_ERASEBACKGROUND:
  152.       return( ( MRESULT)TRUE);
  153.       break;
  154.  
  155.     /**************************************************************************/
  156.     /* Process menu item commands, and commands generated from the keyboard   */
  157.     /* via the accelerator table. Most are handled by the async thread        */
  158.     /**************************************************************************/
  159.     case WM_COMMAND:
  160.       return( WndProcCommand( hwnd, msg, mp1, mp2));
  161.       break;
  162.  
  163.     /**************************************************************************/
  164.     /* Scrolling is handled by the async drawing thread. Simply pass on the   */
  165.     /* command and parameters                                                                            */
  166.     /**************************************************************************/
  167.     case WM_HSCROLL:
  168.       SendCommand( UM_HSCROLL, mp2, 0);
  169.       break;
  170.  
  171.     case WM_VSCROLL:
  172.       SendCommand( UM_VSCROLL, mp2, 0);
  173.       break;
  174.  
  175.     /************************************************************************/
  176.     /* The client area is being resized.                                                         */
  177.     /************************************************************************/
  178.     case WM_SIZE:
  179.       DosResetEventSem( hevDrawOn, &ulSemPostCount);     /* disallow drawing */
  180.       SendCommand( UM_SIZING, mp1, mp2);
  181.       break;
  182.  
  183.     /**************************************************************************/
  184.     /* Mouse commands are handled by the async thread. Simply send on the     */
  185.     /* command and parameters.                                                                        */
  186.     /**************************************************************************/
  187.     case WM_BUTTON2DBLCLK:
  188.     case WM_BUTTON2DOWN:
  189.       if( hwnd != WinQueryFocus( HWND_DESKTOP))
  190.               WinSetFocus( HWND_DESKTOP, hwnd);
  191.       if( !fButtonDownMain)
  192.       {
  193.               fButtonDownMain = TRUE;
  194.               SendCommand( UM_LEFTDOWN, mp1, 0);
  195.       }
  196.       return( ( MRESULT)TRUE);
  197.       break;
  198.  
  199.     case WM_BUTTON2UP:
  200.       if( !fButtonDownMain)
  201.               return( ( MRESULT)TRUE);
  202.       if( SendCommand( UM_LEFTUP, mp1, 0))
  203.               fButtonDownMain = FALSE;
  204.       else
  205.               WinAlarm( HWND_DESKTOP, WA_WARNING);
  206.       return( ( MRESULT)TRUE);
  207.       break;
  208.  
  209.     case WM_MOUSEMOVE:
  210.       DosQueryEventSem( hevMouse, &ulSemPostCount);
  211.       if( ulSemPostCount)                         /* mouse tracking enabled? */
  212.               SendCommand( UM_MOUSEMOVE, mp1, 0);
  213.       return( WinDefWindowProc( hwnd, msg, mp1, mp2));
  214.       break;
  215.     /**************************************************************************/
  216.     /* Default for the rest                                                   */
  217.     /**************************************************************************/
  218.     default:
  219.       return( WinDefWindowProc( hwnd, msg, mp1, mp2));
  220.  
  221.   }   /* end switch (msg)  */
  222.  
  223.   return( FALSE);
  224.  
  225. }   /* end ClientWndProc() */
  226.  
  227. /******************************************************************************/
  228. /*                                                                                                                         */
  229. /* WM_PAINT message                                                                                            */
  230. /*                                                                                                               */
  231. /******************************************************************************/
  232. MRESULT WndProcPaint(VOID)
  233. {
  234.   HRGN   hrgnUpdt;
  235.   SHORT  sRgnType;
  236.  
  237.   hrgnUpdt = GpiCreateRegion( hpsPaint, 0L, NULL);
  238.   sRgnType = WinQueryUpdateRegion( hwndClient, hrgnUpdt);
  239.   SendCommand( UM_DRAW, (MPARAM)hrgnUpdt, 0);
  240.   WinValidateRegion( hwndClient, hrgnUpdt, FALSE);
  241.   return( FALSE);
  242. }
  243.  
  244. /******************************************************************************
  245. *
  246. *  Name        : WndProcCommand
  247. *
  248. *  Description : Process menu item commands, and commands generated from the
  249. *                keyboard via the accelerator table.  Most are handled by
  250. *                the async thread
  251. *
  252. *  Parameters  : HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2
  253. *
  254. *  Return      : MRESULT
  255. *
  256. ******************************************************************************/
  257.  
  258. MRESULT WndProcCommand(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  259. {
  260.   ULONG     ulSemPostCount;
  261.   ULONG     ulLoadPostCount;
  262.  
  263.    DosQueryEventSem( hevLoadingBitmap, &ulLoadPostCount); /* drawing enabled? */
  264.  
  265.    switch( SHORT1FROMMP(mp1))
  266.    {
  267.       case IDM_SIZE_SMALL:
  268.  
  269.          if (ulLoadPostCount == 0)    /* is a bitmap currently being loaded? */
  270.             break;                    /* ignore message while loading bitmap */
  271.          CheckMenu(Currentchecked, FALSE);       /* remove check on old menu */
  272.          SizeBitmap(-8);                            /* change transform size */
  273.          CheckMenu(IDM_SIZE_SMALL, TRUE);               /* add check to menu */
  274.          Currentchecked = IDM_SIZE_SMALL;               /* save cur. menu id */
  275.  
  276.          break;
  277.  
  278.       case IDM_SIZE_MEDIUM:
  279.  
  280.          if (ulLoadPostCount == 0)    /* is a bitmap currently being loaded? */
  281.             break;                    /* ignore message while loading bitmap */
  282.          CheckMenu(Currentchecked, FALSE);       /* remove check on old menu */
  283.          SizeBitmap(-4);                            /* change transform size */
  284.          CheckMenu(IDM_SIZE_MEDIUM, TRUE);              /* add check to menu */
  285.          Currentchecked = IDM_SIZE_MEDIUM;              /* save cur. menu id */
  286.  
  287.          break;
  288.  
  289.       case IDM_SIZE_LARGE:
  290.  
  291.          if (ulLoadPostCount == 0)    /* is a bitmap currently being loaded? */
  292.             break;                    /* ignore message while loading bitmap */
  293.          CheckMenu(Currentchecked, FALSE);       /* remove check on old menu */
  294.          SizeBitmap(-2);                            /* change transform size */
  295.          CheckMenu(IDM_SIZE_LARGE, TRUE);               /* add check to menu */
  296.          Currentchecked = IDM_SIZE_LARGE;               /* save cur. menu id */
  297.  
  298.          break;
  299.  
  300.       case IDM_SIZE_FULL:
  301.  
  302.          if (ulLoadPostCount == 0)    /* is a bitmap currently being loaded? */
  303.             break;                    /* ignore message while loading bitmap */
  304.          CheckMenu(Currentchecked, FALSE);       /* remove check on old menu */
  305.          SizeBitmap(0);                             /* change transform size */
  306.          CheckMenu(IDM_SIZE_FULL, TRUE);                /* add check to menu */
  307.          Currentchecked = IDM_SIZE_FULL;                /* save cur. menu id */
  308.  
  309.          break;
  310.  
  311.       case IDM_JUMBLE:
  312.  
  313.          DosResetEventSem( hevDrawOn, &ulSemPostCount);  /* disallow drawing */
  314.          SendCommand( UM_JUMBLE, 0, 0);
  315.  
  316.          break;
  317.  
  318.       case IDM_LOAD:
  319.  
  320.          FileOpen();           /* Open file and load/draw the selected image */
  321.  
  322.          break;
  323.  
  324.       /**********************************************************************/
  325.       /* Pass on the rest to the async thread.                              */
  326.       /**********************************************************************/
  327.  
  328.       case IDM_HELPHELPFORHELP:
  329.  
  330.          HelpHelpForHelp(mp2);
  331.  
  332.          break;
  333.  
  334.       case IDM_HELPEXTENDED:
  335.  
  336.          DisplayHelpPanel(PANEL_EXTENDED_CONTENTS);
  337.  
  338.          break;
  339.  
  340.       case IDM_HELPINDEX:
  341.  
  342.          HelpIndex(mp2);
  343.  
  344.          break;
  345.  
  346.       case IDM_HELPTUTORIAL:
  347.  
  348.          HelpTutorial(mp2);
  349.  
  350.          break;
  351.  
  352.       case IDM_HELPABOUT:
  353.  
  354.          HelpAbout(mp2);
  355.  
  356.          break;
  357.  
  358.  
  359.       /**********************************************************************/
  360.       /* Unrecognized => default                                            */
  361.       /**********************************************************************/
  362.  
  363.       default:
  364.          return( WinDefWindowProc(hwnd, msg, mp1, mp2));
  365.    }
  366.  
  367.    return( FALSE);
  368.  
  369. }   /* end WndProcCommand() */
  370.  
  371.  
  372. /****************************************************************\
  373.  *  Open file routine
  374.  *--------------------------------------------------------------
  375.  *
  376.  *  Name:   FileOpen()
  377.  *
  378.  *  Purpose: Processes the File menu's Open item.
  379.  *
  380.  *  Usage:  called whenever New from the File menu is selected
  381.  *
  382.  *  Method:  calls the standard file open dialog to get the
  383.  *          file name.  The file name is passed onto DosOpen
  384.  *          which returns the handle to the file.  The file
  385.  *          input procedure is called and then the file handle
  386.  *          is closed.
  387.  *
  388.  *  Returns:
  389.  *
  390. \****************************************************************/
  391. BOOL FileOpen( VOID )
  392. {
  393.  BOOL        fSuccess = FALSE;
  394.  PCH         pch;
  395.  FILEDLG     fdg;
  396.  CHAR        szHeader[MESSAGELEN], szButton[MESSAGELEN];
  397.  CHAR        szExtension[FILEPROTOSIZE];
  398.  ULONG       ulDummy;                       /* local temporary variable  */
  399.  ULONG       ulSemPostCount;
  400.  FILEFINDBUF FileBuff;     /*  used to locate default directory */
  401.  HDIR        hdir  = HDIR_SYSTEM;
  402.  ULONG       Count = 1;
  403.  
  404.    /*
  405.     * set up structure to pass to standard file dialog
  406.     */
  407.  fdg.cbSize = sizeof(FILEDLG);
  408.  fdg.fl = FDS_CENTER | FDS_HELPBUTTON | FDS_OPEN_DIALOG;
  409.  
  410.  if (!WinLoadString(habMain, 0, IDS_OPENDLGHEADER, MESSAGELEN, szHeader))
  411.    {
  412.    MessageBox(hwndFrame, IDS_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
  413.    return FALSE;
  414.    }
  415.  fdg.pszTitle = szHeader;                            /* dialog title */
  416.  
  417.  if (!WinLoadString(habMain, 0, IDS_OPENDLGBUTTON, MESSAGELEN, szButton))
  418.    {
  419.    MessageBox(hwndFrame, IDS_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
  420.    return FALSE;
  421.    }
  422.  fdg.pszOKButton = szButton;                 /* <ENTER> button text */
  423.  
  424.  fdg.ulUser = 0L;
  425.  fdg.pfnDlgProc = (PFNWP)JigsawOpenFilterProc;
  426.  fdg.lReturn = 0L;
  427.  fdg.lSRC = 0;
  428.  fdg.hMod = 0;
  429.  fdg.usDlgId = 0;
  430.  fdg.x = 0;
  431.  fdg.y = 0;
  432.  
  433.     /* setup EA type list & initial type */
  434.  fdg.pszIType = (PSZ)NULL;
  435.  fdg.papszITypeList = (PAPSZ)apszITL;
  436.  
  437.     /* setup drive list & initial drive */
  438.  fdg.pszIDrive = (PSZ)NULL;
  439.  fdg.papszIDriveList = (PAPSZ)NULL;
  440.  
  441.    /*
  442.     * get the default file extension (i.e. *.BMP)
  443.     */
  444.  if (!WinLoadString(habMain,  0, IDS_FILEOPENEXTENSION, FILEPROTOSIZE,
  445.                                                                szExtension))
  446.    {
  447.    MessageBox(hwndFrame, IDS_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
  448.    return FALSE;
  449.    }
  450.  
  451.    /*
  452.     * if dialog has been previously invoked, then ensure the
  453.     * dialog is brought with the last user-selected directory
  454.     */
  455.  if (*pli->szFileName)
  456.    {
  457.    pch = strrchr(pli->szFileName, '\\');
  458.    strcpy(++pch, szExtension);
  459.    strcpy(fdg.szFullFile, pli->szFileName);
  460.    }
  461.  else       /* search for \os2\bitmap directory */
  462.    {
  463.    if (!DosFindFirst(DEFAULT_DIR, &hdir, FILE_DIRECTORY, &FileBuff,
  464.                               sizeof(FILEFINDBUF), &Count, FIL_STANDARD))
  465.      sprintf(fdg.szFullFile, "%s\\%s", DEFAULT_DIR, szExtension);
  466.    else
  467.      strcpy(fdg.szFullFile, szExtension);
  468.    }
  469.  
  470.  
  471.  fdg.sEAType = -1;
  472.  fdg.papszFQFilename = 0;
  473.  fdg.ulFQFCount = 0;
  474.  
  475.    /*
  476.     * invoke the standard file dialog and get a file
  477.     */
  478.  if (!WinFileDlg(HWND_DESKTOP, hwndFrame, (PFILEDLG)&fdg))
  479.    {
  480.    MessageBox(hwndFrame, IDS_CANNOTRUNFILEDLG,MB_OK | MB_ERROR, TRUE);
  481.    return FALSE;
  482.    }
  483.  
  484.    /*
  485.     *  Upon sucessful return of a file, open it for reading
  486.     */
  487.  if (fdg.lReturn == DID_OK)
  488.  {
  489.    strcpy(pli->szFileName, fdg.szFullFile);
  490.    DosOpen(
  491.       pli->szFileName,       /* pointer to filename                          */
  492.       &(pli->hf),            /* pointer to variable for file handle          */
  493.       &ulDummy,              /* pointer to variable for action taken         */
  494.       0,                     /* file size if created or truncated            */
  495.       FILE_NORMAL,           /* file attribute                               */
  496.       FILE_OPEN,             /* action taken if file exists/does not exist   */
  497.       OPEN_ACCESS_READONLY | /* open mode of file                            */
  498.        OPEN_SHARE_DENYWRITE,
  499.       NULL);                 /* pointer to structure for extended attributes */
  500.  
  501.    DosResetEventSem( hevDrawOn, &ulSemPostCount);        /* disallow drawing */
  502.    SendCommand( UM_LOAD, (MPARAM)pli, 0);
  503.  }
  504. }   /* end FileOpen() */
  505.  
  506.  
  507. VOID SizeBitmap(LONG lTempScale )
  508. {
  509.    ULONG  ulSemPostCount;
  510.  
  511.         lScale = lTempScale;
  512.         DosResetEventSem( hevDrawOn, &ulSemPostCount);   /* disallow drawing */
  513.         DosPostEventSem( hevKillDraw);
  514.         SendCommand( UM_ZOOM, MPVOID, MPVOID);
  515. } /* SizeBitmap */
  516.  
  517.  
  518.  
  519. /****************************************************************\
  520.  *  Message Box procedure
  521.  *--------------------------------------------------------------
  522.  *
  523.  *  Name:   MessageBox(hwndOwner, idMsg, fsStyle, fBeep)
  524.  *
  525.  *  Purpose: Displays the message box with the message
  526.  *              given in idMsg retrived from the message table
  527.  *              and using the style flags in fsStyle
  528.  *
  529.  *  Usage:  Called whenever a MessageBox is to be displayed
  530.  *
  531.  *  Method: - Message string is loaded from the process'
  532.  *              message table
  533.  *          - Alarm beep is sounded if desired
  534.  *          - Message box with the message is displayed
  535.  *          - WinMessageBox return value is returned
  536.  *
  537.  *  Returns: return value from WinMessageBox()
  538.  *
  539. \****************************************************************/
  540. ULONG MessageBox(HWND hwndOwner, ULONG idMsg, ULONG fsStyle, BOOL fBeep)
  541. {
  542.    CHAR szText[MESSAGELEN];
  543.    PSZ  pszTitle;
  544.  
  545.    if (fsStyle & MB_ERROR)
  546.       pszTitle = NULL;                                 /* default is "Error" */
  547.    else
  548.       pszTitle = szTitle;                     /* use "Jigsaw" for non-errors */
  549.  
  550.    if (!WinLoadString(habMain,
  551.            (HMODULE)NULLHANDLE,
  552.            idMsg,
  553.            MESSAGELEN,
  554.            (PSZ)szText))
  555.    {
  556.       if (idMsg == IDS_CANNOTLOADSTRING)
  557.       {
  558.          strcpy(szText, "Failed to load resource string");
  559.       }
  560.       else
  561.       {
  562.          WinAlarm(HWND_DESKTOP, WA_ERROR);
  563.          return MBID_ERROR;
  564.       }
  565.    }
  566.  
  567.    if (fBeep)
  568.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  569.  
  570.    return WinMessageBox(HWND_DESKTOP,     /* handle of the parent window     */
  571.              hwndOwner,                   /* handle of the owner window      */
  572.              szText,                      /* address of text in message box  */
  573.              pszTitle,                    /* address of title of message box */
  574.              MSGBOXID,                    /* message-box identifier          */
  575.              fsStyle);                    /* type of message box             */
  576.  
  577. }   /* end MessageBox() */
  578.  
  579. /***************************************************************************
  580.  *** JigsawOpenFilterProc - This is a procedure that will filter the help
  581.  *                           messages to the open dialog.
  582. ***************************************************************************/
  583. MRESULT EXPENTRY JigsawOpenFilterProc(HWND hwnd,ULONG message,MPARAM mp1,MPARAM mp2 )
  584. {
  585.   if(message == WM_HELP)
  586.   {
  587.        DisplayHelpPanel(HID_FS_OPEN_DLG_HELP_PANEL);
  588.        return FALSE ;
  589.    }
  590.  
  591.    return WinDefFileDlgProc( hwnd, message, mp1, mp2 ) ;
  592. }
  593.  
  594. /*******************************  END PROCS.C  *******************************/
  595.