home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sftick.zip / adv / object / good / SLIDEROB.C < prev    next >
Text File  |  1994-04-21  |  14KB  |  440 lines

  1. #define INCL_DOS
  2. #define INCL_WINDIALOGS
  3. #define INCL_WINMENUS
  4. #define INCL_WINSTDSLIDER
  5. #define INCL_WINSYS
  6. #define INCL_WINWINDOWMGR
  7.  
  8. #include <os2.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "sliderob.h"
  13.  
  14. #define CLS_OBJECT               "ObjectClass"
  15.  
  16. #define UM_STARTCOPY             ( WM_USER )
  17. #define UM_COPYDONE              ( WM_USER + 1 )
  18. #define UM_SUICIDE               ( WM_USER + 2 )
  19.  
  20. #define COPY_FILE                "SLIDEROB.C"
  21. #define BACKUP_FILE              "SLIDEROB.BAK"
  22.  
  23. #define SZ_STACK                 16384
  24.  
  25. typedef struct _SLIDERINFO {
  26.    ULONG       ulStructSize ;
  27.    HWND        hwndSlider ;
  28.    HWND        hwndObject ;
  29.    HWND        hwndDialog ;
  30.    HEV         hevQuitSem ;
  31. } SLIDERINFO, * PSLIDERINFO ;
  32.  
  33. typedef VOID ( * _Optlink PFNCOPY ) ( PVOID ) ;
  34.  
  35. BOOL CopyFile ( HWND hwndSlider ) ;
  36. MRESULT EXPENTRY ObjectWndProc ( HWND hwndWnd,
  37.                                  ULONG ulMsg,
  38.                                  MPARAM mpParm1,
  39.                                  MPARAM mpParm2 ) ;
  40.  
  41. VOID CopyThread ( PSLIDERINFO psiSlider ) ;
  42.  
  43. MRESULT EXPENTRY DlgProc ( HWND hwndDlg,
  44.                            ULONG ulMsg,
  45.                            MPARAM mpParm1,
  46.                            MPARAM mpParm2 ) ;
  47.  
  48. INT main ( VOID )
  49. {
  50.    HAB               habAnchor ;
  51.    HMQ               hmqQueue ;
  52.    PSLIDERINFO       pSliderInfo ;
  53.    APIRET            rc ;
  54.    BOOL              bSuccess ;
  55.  
  56.    habAnchor = WinInitialize ( 0 ) ;
  57.    hmqQueue = WinCreateMsgQueue ( habAnchor, 0 ) ;
  58.  
  59.    pSliderInfo = (PSLIDERINFO) malloc ( sizeof ( SLIDERINFO )) ;
  60.    if ( pSliderInfo == NULL ) {
  61.       WinMessageBox ( HWND_DESKTOP,
  62.                       HWND_DESKTOP,
  63.                       "Out of memory !",
  64.                       "Error",
  65.                       0,
  66.                       MB_ICONEXCLAMATION | MB_OK ) ;
  67.       WinDestroyMsgQueue ( hmqQueue ) ;
  68.       WinTerminate ( habAnchor ) ;
  69.       return 0 ;
  70.    } /* endif */
  71.  
  72.    pSliderInfo->ulStructSize = sizeof ( SLIDERINFO ) ;
  73.    rc  = DosCreateEventSem( NULL,
  74.                             &(pSliderInfo->hevQuitSem),
  75.                             DC_SEM_SHARED,
  76.                             FALSE ) ;
  77.    if (rc) {
  78.  
  79.       WinMessageBox ( HWND_DESKTOP,
  80.                       HWND_DESKTOP,
  81.                       "Cannot create Event Semaphore",
  82.                       "Error",
  83.                       0,
  84.                       MB_ICONEXCLAMATION | MB_OK ) ;
  85.  
  86.       free ( pSliderInfo ) ;
  87.       WinDestroyMsgQueue ( hmqQueue ) ;
  88.       WinTerminate ( habAnchor ) ;
  89.       return 0 ;
  90.  
  91.    } /* endif */
  92.  
  93.  
  94.    if ( _beginthread(( PFNCOPY ) CopyThread,
  95.                        0,
  96.                        SZ_STACK,
  97.                        ( PVOID ) pSliderInfo ) == - 1 ) {
  98.  
  99.       WinMessageBox ( HWND_DESKTOP,
  100.                       HWND_DESKTOP,
  101.                       "Cannot create thread !",
  102.                       "Error",
  103.                       0,
  104.                       MB_ICONEXCLAMATION | MB_OK ) ;
  105.  
  106.       free ( pSliderInfo ) ;
  107.       WinDestroyMsgQueue ( hmqQueue ) ;
  108.       WinTerminate ( habAnchor ) ;
  109.       return 0 ;
  110.    } /* endif */
  111.  
  112.    WinDlgBox ( HWND_DESKTOP,
  113.                HWND_DESKTOP,
  114.                DlgProc,
  115.                NULLHANDLE,
  116.                IDD_FCOPYDLG,
  117.                pSliderInfo ) ;
  118.  
  119.  
  120.    /* destroy the object window and wait until it signals that it
  121.       has been destroyed */
  122.  
  123.    /* BINGO */
  124.    /* Using WinSendMsg, makes this thread block until other thread
  125.       has received notification to QUIT */
  126.  
  127.    WinSendMsg( pSliderInfo->hwndObject, UM_SUICIDE, MPVOID, MPVOID ) ;
  128.    /* NOW we wait until the event semaphore has been posted */
  129.    DosWaitEventSem( pSliderInfo->hevQuitSem , SEM_INDEFINITE_WAIT ) ;
  130.    /* object window thread has freed all resources, start my cleanup */
  131.    WinDestroyMsgQueue ( hmqQueue ) ;
  132.    free ( pSliderInfo ) ;
  133.    WinTerminate ( habAnchor ) ;
  134.    return 0 ;
  135. }
  136.  
  137. MRESULT EXPENTRY DlgProc ( HWND hwndDlg,
  138.                            ULONG ulMsg,
  139.                            MPARAM mpParm1,
  140.                            MPARAM mpParm2 )
  141. {
  142.    PSLIDERINFO psiSlider ;
  143.  
  144.    psiSlider = WinQueryWindowPtr ( hwndDlg, 0 ) ;
  145.  
  146.    switch ( ulMsg ) {
  147.  
  148.    case WM_INITDLG:
  149.       {
  150.          CHAR        achFont [16] ;
  151.          USHORT      usIndex ;
  152.          CHAR        achMessage [64] ;
  153.  
  154.          psiSlider = (PSLIDERINFO) PVOIDFROMMP ( mpParm2 ) ;
  155.          WinSetWindowPtr ( hwndDlg, 0, psiSlider ) ;
  156.  
  157.          psiSlider->hwndDialog = hwndDlg ;
  158.          psiSlider->hwndSlider = WinWindowFromID ( hwndDlg,
  159.                                                      IDS_SLIDER ) ;
  160.  
  161.          //--------------------------------------------------------
  162.          // Set the size of the tick marks
  163.          //--------------------------------------------------------
  164.  
  165.          WinSendDlgItemMsg ( hwndDlg,
  166.                              IDS_SLIDER,
  167.                              SLM_SETTICKSIZE,
  168.                              MPFROM2SHORT ( SMA_SETALLTICKS, 7 ) ,
  169.                              0 ) ;
  170.  
  171.          strcpy ( achFont, "8.Tms Rmn" ) ;
  172.          WinSetPresParam ( WinWindowFromID ( hwndDlg, IDS_SLIDER ) ,
  173.                            PP_FONTNAMESIZE,
  174.                            strlen ( achFont ) + 1,
  175.                            achFont ) ;
  176.  
  177.          for ( usIndex = 0 ; usIndex < 11 ; usIndex ++ ) {
  178.             sprintf ( achMessage, "%d%%", usIndex * 10 ) ;
  179.  
  180.             WinSendDlgItemMsg ( hwndDlg,
  181.                                 IDS_SLIDER,
  182.                                 SLM_SETSCALETEXT,
  183.                                 MPFROMSHORT ( usIndex ) ,
  184.                                 MPFROMP ( achMessage )) ;
  185.          } /* endfor */
  186.       }
  187.       break ;
  188.  
  189.  
  190.    case WM_COMMAND:
  191.  
  192.       switch ( SHORT1FROMMP ( mpParm1 )) {
  193.  
  194.          case IDP_START:
  195.             WinPostMsg ( psiSlider->hwndObject,
  196.                          UM_STARTCOPY,
  197.                          0,
  198.                          0 ) ;
  199.             WinEnableWindow ( WinWindowFromID ( hwndDlg,
  200.                                                 IDP_START ) ,
  201.                               FALSE ) ;
  202.             break ;
  203.  
  204.          case IDP_CANCEL:
  205.             WinDismissDlg ( hwndDlg, FALSE ) ;
  206.             break ;
  207.  
  208.          default:
  209.             return WinDefDlgProc ( hwndDlg,
  210.                                    ulMsg,
  211.                                    mpParm1,
  212.                                    mpParm2 ) ;
  213.       } /* endswitch */
  214.       break ;
  215.  
  216.    case UM_COPYDONE:
  217.       {
  218.          USHORT      usState ;
  219.          CHAR        achMessage [60] ;
  220.  
  221.          WinEnableWindow ( WinWindowFromID ( hwndDlg,
  222.                                              IDP_START ) ,
  223.                            TRUE ) ;
  224.  
  225.          usState = SHORT1FROMMP ( mpParm1 ) ;
  226.          if ( usState == TRUE ) {
  227.             strcpy ( achMessage, "Copy completed Successfully" ) ;
  228.             WinAlarm ( HWND_DESKTOP, WA_NOTE ) ;
  229.          } else {
  230.             strcpy ( achMessage, "Copy failed" ) ;
  231.             WinAlarm ( HWND_DESKTOP, WA_ERROR ) ;
  232.          } /* endif */
  233.  
  234.          WinMessageBox ( HWND_DESKTOP,
  235.                          hwndDlg,
  236.                          achMessage,
  237.                          "Copy Status",
  238.                          0,
  239.                          MB_OK | MB_INFORMATION ) ;
  240.       }
  241.       break ;
  242.  
  243.    default:
  244.       return WinDefDlgProc ( hwndDlg,
  245.                              ulMsg,
  246.                              mpParm1,
  247.                              mpParm2 ) ;
  248.    } /* endswitch */
  249.  
  250.    return MRFROMSHORT ( FALSE ) ;
  251. }
  252.  
  253. VOID CopyThread ( PSLIDERINFO psiSlider )
  254. {
  255.    HAB        habAnchor ;
  256.    HMQ        hmqQueue ;
  257.    QMSG       qmMsg ;
  258.  
  259.    habAnchor = WinInitialize ( 0 ) ;
  260.    hmqQueue = WinCreateMsgQueue ( habAnchor, 0 ) ;
  261.  
  262.    WinRegisterClass ( habAnchor,
  263.                       CLS_OBJECT,
  264.                       ObjectWndProc,
  265.                       0,
  266.                       sizeof ( PVOID )) ;
  267.  
  268.    psiSlider->hwndObject = WinCreateWindow ( HWND_OBJECT,
  269.                                              CLS_OBJECT,
  270.                                              NULL,
  271.                                              0,
  272.                                              0,
  273.                                              0,
  274.                                              0,
  275.                                              0,
  276.                                              NULLHANDLE,
  277.                                              HWND_TOP,
  278.                                              ID_OBJECT,
  279.                                              psiSlider,
  280.                                              NULL ) ;
  281.  
  282.    if ( psiSlider->hwndObject != NULLHANDLE ) {
  283.       while (WinGetMsg ( habAnchor,
  284.                          &qmMsg,
  285.                          NULLHANDLE,
  286.                          0,
  287.                          0 ))
  288.  
  289.           WinDispatchMsg ( habAnchor, &qmMsg ) ;
  290.       WinDestroyWindow ( psiSlider->hwndObject ) ;
  291.    } /* endif */
  292.  
  293.    WinDestroyMsgQueue ( hmqQueue ) ;
  294.    WinTerminate ( habAnchor ) ;
  295.    DosPostEventSem( psiSlider->hevQuitSem ) ;
  296.    return ;
  297. }
  298.  
  299. MRESULT EXPENTRY ObjectWndProc ( HWND hwndWnd,
  300.                                  ULONG  ulMsg,
  301.                                  MPARAM mpParm1,
  302.                                  MPARAM mpParm2 )
  303. {
  304.    switch ( ulMsg ) {
  305.  
  306.       case WM_CREATE:
  307.          WinSetWindowPtr ( hwndWnd, 0, PVOIDFROMMP ( mpParm1 )) ;
  308.          break ;
  309.  
  310.       case UM_STARTCOPY:
  311.          {
  312.             PSLIDERINFO pSliderInfo ;
  313.  
  314.             pSliderInfo = WinQueryWindowPtr ( hwndWnd, 0 ) ;
  315.  
  316.             if ( CopyFile ( pSliderInfo->hwndSlider )) {
  317.                WinPostMsg ( pSliderInfo->hwndDialog,
  318.                             UM_COPYDONE,
  319.                             MPFROMSHORT ( TRUE ) ,
  320.                             0 ) ;
  321.             } else {
  322.                WinPostMsg ( pSliderInfo->hwndDialog,
  323.                             UM_COPYDONE,
  324.                             MPFROMSHORT ( FALSE ) ,
  325.                             0 ) ;
  326.             } /* endif */
  327.          }
  328.          break ;
  329.       case UM_SUICIDE:
  330.             WinPostMsg( hwndWnd,
  331.                         WM_QUIT,
  332.                         MPVOID,
  333.                         MPVOID ) ;
  334.             break;
  335.  
  336.       default:
  337.          return WinDefWindowProc ( hwndWnd,
  338.                                    ulMsg,
  339.                                    mpParm1,
  340.                                    mpParm2 ) ;
  341.    } /* endswitch */
  342.  
  343.    return MRFROMSHORT ( FALSE ) ;
  344. }
  345.  
  346. BOOL CopyFile ( HWND hwndSlider )
  347. {
  348.    APIRET            arRc ;
  349.    FILESTATUS3       fsStatus ;
  350.    PBYTE             pbBuffer ;
  351.    HFILE             hfRead ;
  352.    ULONG             ulAction ;
  353.    HFILE             hfWrite ;
  354.    ULONG             ulSzBlock ;
  355.    USHORT            usIndex ;
  356.    ULONG             ulBytesRead ;
  357.    ULONG             ulBytesWritten ;
  358.  
  359.    arRc = DosQueryPathInfo ( COPY_FILE,
  360.                              FIL_STANDARD,
  361.                              (PVOID) &fsStatus,
  362.                              sizeof ( fsStatus )) ;
  363.  
  364.    if ( !arRc ) {
  365.  
  366.       ulSzBlock = fsStatus.cbFile / 10 + 1 ;
  367.  
  368.       pbBuffer = (PBYTE) malloc ( ulSzBlock ) ;
  369.  
  370.       //-----------------------------------------------------------
  371.       // Open up the file for reading
  372.       //-----------------------------------------------------------
  373.  
  374.       arRc = DosOpen ( COPY_FILE,
  375.                        &hfRead,
  376.                        &ulAction,
  377.                        0,
  378.                        FILE_NORMAL,
  379.                        FILE_OPEN,
  380.                        OPEN_ACCESS_READONLY | OPEN_SHARE_DENYWRITE,
  381.                        0 ) ;
  382.  
  383.       //-----------------------------------------------------------
  384.       // Open up the backup file for write
  385.       //-----------------------------------------------------------
  386.  
  387.       arRc = DosOpen ( BACKUP_FILE,
  388.                        &hfWrite,
  389.                        &ulAction,
  390.                        0,
  391.                        FILE_NORMAL,
  392.                        FILE_CREATE,
  393.                        OPEN_ACCESS_WRITEONLY |
  394.                        OPEN_SHARE_DENYREADWRITE,
  395.                        0 ) ;
  396.  
  397.       for ( usIndex = 1 ; usIndex < 11 ; usIndex ++ ) {
  398.  
  399.          //--------------------------------------------------------
  400.          // Read a block
  401.          //--------------------------------------------------------
  402.  
  403.          DosRead ( hfRead,
  404.                    pbBuffer,
  405.                    ulSzBlock,
  406.                    &ulBytesRead ) ;
  407.  
  408.          //--------------------------------------------------------
  409.          // Write a block
  410.          //--------------------------------------------------------
  411.  
  412.          DosWrite ( hfWrite,
  413.                     pbBuffer,
  414.                     ulBytesRead,
  415.                     &ulBytesWritten ) ;
  416.  
  417.          //--------------------------------------------------------
  418.          // Tell the slider to move
  419.          //--------------------------------------------------------
  420.  
  421.          WinSendMsg ( hwndSlider,
  422.                       SLM_SETSLIDERINFO,
  423.                       MPFROM2SHORT ( SMA_SLIDERARMPOSITION,
  424.                          SMA_INCREMENTVALUE ) ,
  425.                       MPFROMSHORT ( usIndex )) ;
  426.       } /* endfor */
  427.  
  428.       //-----------------------------------------------------------
  429.       // Clean up
  430.       //-----------------------------------------------------------
  431.  
  432.       DosClose ( hfRead ) ;
  433.       DosClose ( hfWrite ) ;
  434.       free ( pbBuffer ) ;
  435.       return TRUE ;
  436.    } else {
  437.       return FALSE ;
  438.    } /* endif */
  439. }
  440.