home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 14 / CDACTUAL.iso / cdactual / demobin / share / program / c / WUNZ13SR.ZIP / ACTION.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-02  |  10.4 KB  |  349 lines

  1. #include "wizunzip.h"
  2. #include "unzip.h"     
  3.  
  4. /* Action.c module of WizUnzip.
  5.  * Author: Robert A. Heath, 1992
  6.  * I, Robert Heath, place this source code module in the public domain.
  7.  */
  8. char __based(__segname("STRINGS_TEXT")) szNoMemory[] = 
  9.             "Insufficient memory for this operation!";
  10.  
  11.  
  12. /* Get Selection Count returns a count of the selected 
  13.  * list box items. If the count is  greater than zero, it also returns
  14.  * a pointer to a locked list in local memory of the item nos.
  15.  * and their local memory handle.
  16.  * A value of -1 indicates an error.
  17.  */
  18. int CLBItemsGet(HWND hListBox, int __far * __far *ppnSelItems, HANDLE *phnd)
  19. {
  20.     int cSelLBItems = (int)SendMessage(hListBox, LB_GETSELCOUNT, 0, 0L);
  21.  
  22.     if ( !phnd )
  23.         return -1;
  24.  
  25.     *phnd = 0;
  26.     if (cSelLBItems)
  27.     {
  28.         *phnd = GlobalAlloc(GMEM_FIXED, cSelLBItems * sizeof(int));
  29.         if ( !*phnd )
  30.             return -1;
  31.  
  32.         *ppnSelItems = (int __far *)GlobalLock( *phnd );
  33.         if ( !*ppnSelItems )
  34.         {
  35.             GlobalFree( *phnd );
  36.             *phnd = 0;
  37.             return -1;
  38.         }
  39.  
  40.         if (SendMessage(hWndList, LB_GETSELITEMS, cSelLBItems, (LONG)*ppnSelItems) != cSelLBItems)
  41.         {
  42.             GlobalUnlock(*phnd);
  43.             GlobalFree(*phnd);
  44.             *phnd = 0;
  45.             return -1;
  46.         }
  47.     }
  48.     return cSelLBItems;
  49. }
  50.  
  51. /* Re-select listbox contents from given list. The pnSelItems is a
  52.  * list containing the indices of those items selected in the listbox.
  53.  * This list was probably created by GetLBSelCount() above.
  54.  */
  55. void ReselectLB(HWND hListBox, int cSelLBItems, int __far *pnSelItems)
  56. {
  57.     int i;
  58.  
  59.     
  60.     for (i = 0; i < cSelLBItems; ++i)
  61.     {
  62.         SendMessage(hListBox, LB_SETSEL, TRUE, MAKELPARAM(pnSelItems[i],0));
  63.     }
  64. }
  65.  
  66.  
  67. #define cchFilesMax 1024
  68.  
  69. /* Action is called on double-clicking, or selecting one of the 3
  70.  * main action buttons. The action code is the action
  71.  * relative to the listbox or the button ID.
  72.  */
  73. void Action(HWND hWnd, WORD wActionCode)
  74. {
  75.     HANDLE  hMem = 0;
  76.     int i;
  77.     int iSelection;
  78.     int cch;
  79.     int cchCur;
  80.     int cchTotal;
  81.     int __far *pnSelItems;  /* pointer to list of selected items */
  82.     HANDLE  hnd = 0;
  83.     int cSelLBItems = CLBItemsGet(hWndList, &pnSelItems, &hnd);
  84.     int argc;
  85.     LPSTR   lpszT;
  86.     char **pszIndex;
  87.     char *sz;
  88.     WORD wIndex = !uf.fFormatLong ? SHORT_FORM_FNAME_INX
  89.                             : LONG_FORM_FNAME_INX;
  90.  
  91.     /* if no items were selected */
  92.     if (cSelLBItems < 1)
  93.         return;
  94.  
  95.     /* Note: this global value can be overriden in replace.c */
  96.     uf.fDoAll = (uf.fOverwrite) ? 1 : 0;
  97.  
  98.     SetCapture(hWnd);
  99.     hSaveCursor = SetCursor(hHourGlass);
  100.     ShowCursor(TRUE);
  101.  
  102. #if 1
  103.     /* If all the files are selected pass in no filenames */
  104.     /* since unzipping all files is the default */
  105.     hMem = GlobalAlloc( GPTR, 4096 );
  106.     if ( !hMem )
  107.         goto done;
  108.     lpszT = (LPSTR)GlobalLock( hMem );
  109.     if ( !lpszT )
  110.     {
  111.         GlobalFree( hMem );
  112.         goto done;
  113.     }
  114.  
  115.     argc = ((WORD)cSelLBItems == cZippedFiles) ? 0 : 1;
  116.     iSelection = 0;
  117.  
  118.     do
  119.     {
  120.         char rgszFiles[cchFilesMax];
  121.  
  122.         if (argc)
  123.         {
  124.             cchCur = 0;
  125.             pszIndex = (char **)rgszFiles;
  126.             cch = (sizeof(char *) * ((cSelLBItems > (cchFilesMax/16)-1 ) ? (cchFilesMax/16) : cSelLBItems+1));
  127.             cchTotal = (cchFilesMax-1) - cch;
  128.             sz = rgszFiles + cch;
  129.             
  130.             for (i=0; ((i+iSelection)<cSelLBItems) && (i<(cchFilesMax/16)-1); ++i)
  131.             {
  132.                 cch = (int)SendMessage(hWndList, LB_GETTEXTLEN, pnSelItems[i+iSelection], 0L);
  133.                 if (cch != LB_ERR)
  134.                 {
  135.                     if ((cchCur+cch+1-wIndex) > cchTotal)
  136.                         break;
  137.                     cch = (int)SendMessage(hWndList, LB_GETTEXT, pnSelItems[i+iSelection], (LONG)lpszT);
  138.                     if ((cch != LB_ERR) && (cch>wIndex))
  139.                     {
  140.                         lstrcpy(sz, lpszT+wIndex);
  141.                         pszIndex[i] = sz;
  142.                         cchCur += (cch + 1 - wIndex);
  143.                         sz += (cch + 1 - wIndex);
  144.                     }
  145.                     else
  146.                     {
  147.                         break;
  148.                     }
  149.                 }
  150.                 else
  151.                 {
  152.                     MessageBeep(1);
  153.                     goto done;
  154.                 }
  155.             }
  156.             if (i == 0)
  157.                 goto done;
  158.             argc = i;
  159.  
  160.             pszIndex[i] = 0;
  161.             iSelection += i;
  162.         }
  163.         else
  164.         {
  165.             iSelection = cSelLBItems;
  166.         }
  167.         
  168.         switch (wActionCode)
  169.         {
  170.         case 0:         /* extract */
  171.             if (FSetUpToProcessZipFile(0, 0, 0, 1, 0, 
  172.                             (int)(uf.fRecreateDirs ? 1 : 0), 
  173.                             uf.fDoAll, (int)(uf.fTranslate ? 1 : 0),
  174.                                 argc, lpumb->szFileName, (char **)rgszFiles))
  175.             {
  176.                 process_zipfile();
  177.             }
  178.             else
  179.             {
  180.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  181.             }
  182.             TakeDownFromProcessZipFile();
  183.             break;
  184.         case 1:     /* display to message window */
  185.             bRealTimeMsgUpdate = FALSE;
  186.             if (FSetUpToProcessZipFile(1, 0, 0, 1, 0,  0, 0, 0,
  187.                                 argc, lpumb->szFileName, (char **)rgszFiles))
  188.             {
  189.                 process_zipfile();
  190.             }
  191.             else
  192.             {
  193.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  194.             }
  195.  
  196.             TakeDownFromProcessZipFile();
  197.             bRealTimeMsgUpdate = TRUE;
  198.             UpdateMsgWndPos();
  199.             break;
  200.         case 2:     /* test */
  201.             if (FSetUpToProcessZipFile(0, 1, 0, 1, 0,  0, 0, 0,
  202.                                 argc, lpumb->szFileName, (char **)rgszFiles))
  203.             {
  204.                 process_zipfile();
  205.             }
  206.             else 
  207.             {
  208.                 MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION);
  209.             }
  210.  
  211.             TakeDownFromProcessZipFile();
  212.             break;
  213.         }
  214.     } while (iSelection < cSelLBItems);
  215.     
  216.  
  217.     /* march through list box checking what's selected
  218.      * and what is not.
  219.      */
  220. #else
  221.     for (i = 0; i < cSelLBItems ; ++i)
  222.     {
  223.         int nLength;
  224.         char szBuffer[256];
  225.  
  226.         /* extract item from list box...        */
  227.         if ((nLength = (int)SendMessage(hWndList, LB_GETTEXT, 
  228.                             pnSelItems[i], (LONG)(LPSTR)szBuffer)) > 0)
  229.         {
  230.             /* index of filename in buffer */
  231.             WORD wIndex = !uf.fFormat ? SHORT_FORM_FNAME_INX
  232.                                     : LONG_FORM_FNAME_INX;
  233.  
  234.             PSTR pfn = &szBuffer[wIndex]; /* points to filename */
  235.             /* fake arg list for process_zipfile() */
  236.             static char *FileNameVector[] = { "", "" }; 
  237.         
  238.             WinAssert(nLength < (256-1));
  239.             /* pass desired filename */
  240.             FileNameVector[0] = pfn;
  241.             switch (wActionCode)
  242.             {
  243.             case 0:         /* extract */
  244.                 if (FSetUpToProcessZipFile(0, 0, 0, 1, 0, 
  245.                                 (int)(uf.fRecreateDirs ? 1 : 0), 
  246.                                 uf.fDoAll, (int)(uf.fTranslate ? 1 : 0),
  247.                                     1, lpumb->szFileName, FileNameVector))
  248.                 {
  249.                     process_zipfile();
  250.                 }
  251.                 else
  252.                 {
  253.                     MessageBox(hWndMain, szNoMemory, NULL, 
  254.                                 MB_OK|MB_ICONEXCLAMATION);
  255.                 }
  256.                 TakeDownFromProcessZipFile();
  257.                 break;
  258.             case 1:     /* display to message window */
  259.                 bRealTimeMsgUpdate = FALSE;
  260.                 if (FSetUpToProcessZipFile(1, 0, 0, 1, 0,  0, 0, 0,
  261.                                     1, lpumb->szFileName, FileNameVector))
  262.                 {
  263.                     process_zipfile();
  264.                 }
  265.                 else
  266.                 {
  267.                     MessageBox(hWndMain, szNoMemory, NULL, 
  268.                                 MB_OK|MB_ICONEXCLAMATION);
  269.                 }
  270.                 
  271.                 TakeDownFromProcessZipFile();
  272.                 bRealTimeMsgUpdate = TRUE;
  273.                 UpdateMsgWndPos();
  274.                 break;
  275.             case 2:     /* test */
  276.                 if (FSetUpToProcessZipFile(0, 1, 0, 1, 0,  0, 0, 0,
  277.                                     1, lpumb->szFileName, FileNameVector))
  278.                 {
  279.                     process_zipfile();
  280.                 }
  281.                 else 
  282.                 {
  283.                     MessageBox(hWndMain, szNoMemory, NULL, 
  284.                                 MB_OK|MB_ICONEXCLAMATION);
  285.                 }
  286.                 
  287.                 TakeDownFromProcessZipFile();
  288.                 break;
  289.             }
  290.         }
  291.     }
  292. #endif
  293.  
  294. done:
  295.  
  296.     if ( hMem )
  297.     {
  298.         GlobalUnlock( hMem );
  299.         GlobalFree( hMem );
  300.     }
  301.     GlobalUnlock(hnd);
  302.     GlobalFree(hnd);
  303.  
  304.  
  305.     ShowCursor(FALSE);
  306.     SetCursor(hSaveCursor);
  307.     ReleaseCapture();
  308.     if ( uf.fBeepOnFinish )
  309.         MessageBeep(1);
  310.  
  311.     if (!uf.fIconSwitched)  /* if haven't already, switch icons */
  312.     {
  313.         HANDLE hIcon;
  314.  
  315.         hIcon = LoadIcon(hInst,"UNZIPPED"); /* load final icon   */
  316.         assert(hIcon);
  317.         SetClassWord(hWndMain, GCW_HICON, hIcon);
  318.         uf.fIconSwitched = TRUE;    /* flag that we've switched it  */
  319.     }
  320. }
  321.  
  322. /* Display the archive comment using the Info-ZIP engine. */
  323. void DisplayComment(HWND hWnd)
  324. {
  325.  
  326.     SetCapture(hWnd);
  327.     hSaveCursor = SetCursor(hHourGlass);
  328.     ShowCursor(TRUE);
  329.     bRealTimeMsgUpdate = FALSE;
  330.     if (FSetUpToProcessZipFile(0, 0, 0, 1, 1, 0, 0, 0,
  331.                         0, lpumb->szFileName, NULL))
  332.     {
  333.         process_zipfile();
  334.     }
  335.     else 
  336.     {
  337.         MessageBox(hWndMain, szNoMemory, NULL, MB_OK | MB_ICONEXCLAMATION); 
  338.     }
  339.     
  340.     TakeDownFromProcessZipFile();
  341.     ShowCursor(FALSE);
  342.     SetCursor(hSaveCursor);
  343.     bRealTimeMsgUpdate = TRUE;
  344.     UpdateMsgWndPos();
  345.     ReleaseCapture();
  346.     if ( uf.fBeepOnFinish )
  347.         MessageBeep(1);
  348. }
  349.