home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pmtools.zip / pmtools.c next >
C/C++ Source or Header  |  1997-08-26  |  9KB  |  368 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. #define  INCL_DOS
  5. #define  INCL_DOSERRORS /* how come this isn'd included by INCL_DOS ?! */
  6. #define  INCL_PM
  7.  
  8. #include <os2.h>
  9.  
  10. /**
  11.  * Center a window within its parent window.
  12.  *
  13.  * Entry:
  14.  *    
  15.  *    The handle of the window to center.
  16.  *
  17.  * Exit:
  18.  *
  19.  *    Nothing.
  20.  */
  21. void
  22. CenterWindow( HWND hwnd )
  23. /***********************/
  24. {
  25.    RECTL rclParent;
  26.    RECTL rclWindow;
  27.  
  28.    WinQueryWindowRect( hwnd, &hwndWindow );
  29.    WinQueryWindowRect( WinQueryWindow( hwnd, QW_PARENT ), &rclParent );
  30.  
  31.    rclWindow.xLeft   = ( rclParent.xRight - rclWindow.xRight ) / 2;
  32.    rclWindow.yBottom = ( rclParent.yTop   - rclWindow.yTop   ) / 2;
  33.  
  34.    WinSetWindowPos( hwnd, NULLHANDLE, rclWindow.xLeft, rclWindow.yBottom,
  35.                     0, 0, SWP_MOVE );
  36. }
  37.  
  38. /**
  39.  * Create a help instance and associate it with a window.
  40.  *
  41.  * Entry:
  42.  *
  43.  *    hwndFrame      - The window handle.
  44.  *    pszFileName    - The pathname of the help file.
  45.  *                     If NULL or empty, will build a helpfile name
  46.  *                     using the path and root file name of the executable.
  47.  *    hmod           - The handle of the module containing the help table.
  48.  *                     NULLHANDLE == current.
  49.  *    usTableID      - The resource ID of the help table.
  50.  *    pszWindowTitle - The tile for the help window (may be NULL of empty ).
  51.  *
  52.  * Exit:
  53.  *
  54.  *    The help window's handle on success.
  55.  */
  56. HWND
  57. CreateHelp( HWND    hwndFrame,
  58.             PSZ     pszFileName,
  59.             HMODULE hmod,
  60.             USHORT  usTableID,
  61.             PSZ     pszWindowTitle )
  62. /**********************************/
  63. {
  64.    PPIB     ppib;
  65.    PTIB     ptib;
  66.    HELPINIT hi;
  67.    PSZ      pszExt;
  68.    CHAR     szName[ CCHMAXPATH ];
  69.    HWND     hwndHelp;
  70.  
  71.    if (( pszFileName == NULL ) || ( *pszFileName ))
  72.    {
  73.       DosGetInfoBlocks( &ptib, &ppib );
  74.  
  75.       DosQueryModuleName( ppib->pib_hmte, sizeof( szName ), szName );
  76.  
  77.       pszExt = strrchr( szName, '.' );
  78.       if ( pszExt )
  79.       {
  80.          strcpy( pszExt, ".hlp" );
  81.       }
  82.       else
  83.       {
  84.          strcat( szName, ".hlp" );
  85.       }
  86.  
  87.       pszFileName = szName;
  88.    }
  89.  
  90.    hi.cb                       = sizeof( HELPINIT );
  91.    hi.ulReturnCode             = 0;
  92.    hi.pszTutorialName          = NULL;
  93.    hi.phtHelpTable             = ( PHELPTABLE ) MAKELONG( usTableID, 0xffff );
  94.    hi.hmodHelpTableModule      = hmod;
  95.    hi.hmodAccelActionBarModule = NULLHANDLE;
  96.    hi.idAccelTable             = 0;
  97.    hi.idActionBar              = 0;
  98.    hi.pszHelpWindowTitle       = pszWindowTitle;
  99.    hi.fShowPanelId             = CMIC_HIDE_PANEL_ID;
  100.    hi.pszHelpLibraryName       = pszFileName;
  101.  
  102.    hwndHelp = WinCreateHelpInstance( WinQueryAnchorBlock( hwndFrame ), &hi );
  103.    if ( hwndHelp != NULLHANDLE )
  104.    {
  105.       WinAssociateHelpInstance( hwndHelp, hwndFrame );
  106.       return ( hwndHelp );
  107.    }
  108.    else
  109.    {
  110.       return ( NULLHANDLE );
  111.    }
  112. }
  113.  
  114. /**
  115.  * Dispose of a help table instance.
  116.  *
  117.  * Entry:
  118.  *
  119.  *    hwndHelp - A help table handle from "CreateHelp()."
  120.  *
  121.  * Exit:
  122.  *
  123.  *    Nothing.
  124.  */
  125. void
  126. DestroyHelp( HWND hwndHelp )
  127. /**************************/
  128. {
  129.    if ( hwndHelp != NULLHANDLE )
  130.    {
  131.       WinAssociateHelpInstance( NULLHANDLE, hwnd );
  132.       WinDestroyHelpInstance( hwndHelp );
  133.    }
  134. }
  135.  
  136. /**
  137.  * Change the icon used on an icon-button control.
  138.  *
  139.  * Entry:
  140.  *
  141.  *    hwndButton - The button handle.
  142.  *    hicon      - The icons handle.
  143.  *
  144.  * Exit:
  145.  *
  146.  *    Nothing.
  147.  */
  148. void
  149. SetButtonIcon( HWND     hwndButton,
  150.                HPOINTER hicon      )
  151. /**********************************/
  152. {
  153.    WNDPARAMS wp;
  154.    BTNCDATA  bd;
  155.  
  156.    wp.fsStatus  = WPM_CTLDATA;
  157.    wp.cbCtlData = sizeof( BTNCDATA );
  158.    wp.pCtlData  = &bd;
  159.  
  160.    WinSendMsg( hwndButton, WM_QUERYWINDOWPARAMS, MPFROMP( &wp ), MPVOID );
  161.    bd.hImage = hicon;
  162.    WinSendMsg( hwndButton, WM_SETWINDOWPARAMS, MPFROMP( &wp ), MPVOID );
  163. }
  164.  
  165. /**
  166.  * Launch ( spawn ) another application.
  167.  *
  168.  * Entry:
  169.  *
  170.  *    hwnd           - The current window's handle.
  171.  *    pszProgram     - The pathname of the program to launch.
  172.  *    pszOptions     - Command-line options (if any) to pass to the spawned app.
  173.  *    pszDirectory   - The path to launch the app from; i.e., this will be
  174.  *                     the current directory at the time of launch.
  175.  *
  176.  * Exit:
  177.  * 
  178.  *    A handle to the launched app if successful.
  179.  *
  180.  *    NOTE:
  181.  *
  182.  *       The launching window ( hwnd ) will receive a WM_APPTERMINATENOTIFY
  183.  *       message if still running when the spawned app terminates.
  184.  *
  185.  */
  186. HWND
  187. LaunchApplication( HWND    hwnd,
  188.                    PSZ     pszProgram,
  189.                    PSZ     pszOptions,
  190.                    PSZ     pszDirectory )
  191. /***************************************/
  192. {
  193.    PROGDETAILS pd = { 0 };
  194.  
  195.    pd.Length                        = sizeof( pd );
  196.    pd.progt.progc                   = PROG_DEFAULT;
  197.    pd.pszExecutable                 = pszProgram;
  198.    pd.pszStartupDir                 = pszDirectory;
  199.    pd.pszParameters                 = pszOptions;
  200.    pd.swpInitial.hwndInsertBehind   = HWND_TOP;
  201.    pd.swpInitial.fl                 = SWP_ACTIVATE | SWP_SHOW;
  202.    pd.swpInitial.hwnd               = hwnd;
  203.  
  204.    return ( WinStartApp( hwnd, &pd, NULL, NULL, SAF_INSTALLEDCMDLINE ));
  205. }
  206.  
  207. /**
  208.  * Prevent more than one instance of an application from running.
  209.  *
  210.  * Entry:
  211.  *
  212.  *    pszSemName  - A unique semaphore name to identify this application.
  213.  *                  This is a shared semaphore, so the name MAY NOT be
  214.  *                  NULL or empty. As with all OS/2 semaphores, the
  215.  *                  name must begin with "\SEM32\."
  216.  *
  217.  *                  E.g.: "\\SEM32\\MYAPP"
  218.  *
  219.  *    fSwitch     - If TRUE, and another instance of the app is found
  220.  *                  to be running, we'll use the task manager to
  221.  *                  switch to the running version. Otherwise just
  222.  *                  return.
  223.  *
  224.  * Exit:
  225.  *
  226.  *    A mutex semaphore handle if this is the only instance of
  227.  *    the app running, otherwise, NULLHANDLE, indicating that
  228.  *    another copy exists.
  229.  */
  230. HMTX
  231. OneInstance( PSZ  pszSemName,
  232.              BOOL fSwitch    )
  233. /****************************/
  234. {
  235.    HMTX hmtx;
  236.  
  237.    if ( DosCreateMutexSem( pszSemName, &hmtx, DC_SEM_SHARED, TRUE ) == NO_ERROR )
  238.    {
  239.       return ( hmtx );
  240.    }
  241.  
  242.    hmtx = NULLHANDLE;
  243.  
  244.    if ( DosOpenMutexSem( pszSemName, &hmtx ) == NO_ERROR )
  245.    {
  246.       if ( fSwitch )
  247.       {
  248.          PID     pid = 0;
  249.          TID     tid = 0;
  250.          ULONG   ulJunk;
  251.  
  252.          if ( DosQueryMutexSem( hmtx, &pid, &tid, &ulJunk ) == NO_ERROR )
  253.          {
  254.             HSWITCH hswitch;
  255.  
  256.             hswitch = WinQuerySwitchHandle( NULLHANDLE, pid );
  257.             if ( hswitch != NULLHANDLE )
  258.             {
  259.                WinSwitchToProgram( hswitch );
  260.             }
  261.          }
  262.       }
  263.  
  264.       DosCloseMutexSem( hmtx );
  265.    }
  266.  
  267.    return ( NULLHANDLE );
  268. }
  269.  
  270. /**
  271.  * Load a menu.
  272.  *
  273.  * Entry:
  274.  *
  275.  *    hwnd     - The parent/owner of the menu. May be HWND_OBJECT.
  276.  *    hmod     - The module containing the menu resource. NULLHANDLE == current.
  277.  *    usResID  - The resource I.D.
  278.  *
  279.  * Exit:
  280.  *
  281.  *    A handle to the menu. If the parent/owner was NOT HWND_OBJECT,
  282.  *    this routine will attempt to associate the loaded menu with
  283.  *    the current window.
  284.  */
  285. HWND
  286. LoadMenu( HWND    hwnd,
  287.           HMODULE hmod,
  288.           USHORT  usResID )
  289. /*************************/
  290. {
  291.    HWND hmenu;
  292.  
  293.    hmenu = WinLoadMenu( hwnd, hmod, usResID );
  294.    if (( hmenu != NULLHANDLE ) && ( hwnd != HWND_OBJ ))
  295.    {
  296.       WinSendMsg( hwnd, WM_UPDATEFRAME, MPFROMLONG( FID_MENU ), MPVOID );
  297.    }
  298.  
  299.    return ( hmenu );
  300. }
  301.  
  302. /**
  303.  * Load an icon and make it the current window's.
  304.  *
  305.  * Entry:
  306.  *
  307.  *    hwnd     - The application's frame window handle.
  308.  *    hmod     - The module containing the resource - NULLHANDLE == current.
  309.  *    usResID  - The resource I.D.
  310.  *
  311.  * Exit:
  312.  *
  313.  *    A handle to the icon. The routine will "attach" the icon to the window
  314.  *    if it can.
  315.  */
  316. HPOINTER
  317. LoadIcon( HWND    hwnd,
  318.           HMODULE hmod,
  319.           USHORT  usResID )
  320. /*************************/
  321. {
  322.    HPOINTER hicon;
  323.  
  324.    hicon = WinLoadPointer( HWND_DESKTOP, hmod, usResID );
  325.    if ( hicon != NULLHANDLE )
  326.    {
  327.       WinSendMsg( hwnd, WM_SETICON, MPFROMLONG( hicon ), MPVOID );
  328.    }
  329.  
  330.    return ( hicon );
  331. }
  332.  
  333. /**
  334.  * Load an accelerator table and connect it to the window.
  335.  *
  336.  * Entry:
  337.  *
  338.  *    hwnd     - The application's frame window handle.
  339.  *    hmod     - The module containing the resource - NULLHANDLE == current.
  340.  *    usResID  - The resource I.D.
  341.  *
  342.  * Exit:
  343.  *
  344.  *    A handle to the accelerator table. The routine will "attach" the 
  345.  *    table to the window if it can.
  346.  */
  347. HACCEL
  348. LoadAccelerator( HWND    hwnd,
  349.                  HMODULE hmod,
  350.                  USHORT  usResID )
  351. /********************************/
  352. {
  353.    HACCEL haccel;
  354.    HAB    hab;
  355.  
  356.    hab    = WinQueryAnchorBlock( hwnd );
  357.    haccel = WinLoadAccelTable( hab, hmod, usResID );
  358.    if ( haccel != NULLHANDLE )
  359.    {
  360.       WinSetAccelTable( hab, haccel, hwnd );
  361.    }
  362.  
  363.    return ( haccel );
  364. }
  365.  
  366.  
  367.  
  368.