home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Demos / Evatac Software / Preditor 3.0 / Tools / Extension Module Builder / Examples / Cut⁄Copy Matching / Cut.c next >
Encoding:
C/C++ Source or Header  |  1996-01-28  |  4.1 KB  |  222 lines  |  [TEXT/TCEd]

  1. /************************************************************
  2.  
  3.     Cut.c
  4.     
  5.     Preditor 3.0 extension.
  6.     
  7.     Cuts or copies any line of text in the selection that matches a match
  8.     string.  The match string provided by the user.
  9.  
  10.     © Copyright Evatac Software  1988-1996
  11.     All rights reserved
  12.  
  13. ************************************************************/
  14.  
  15. #include "PreditorExtension.h"
  16.  
  17. #include <SetupA4.h>
  18. #ifndef THINKC
  19. #include <A4Stuff.h>
  20. #else
  21. #define SetCurrentA4()    0; RememberA4()
  22. #define SetA4(x)        SetUpA4()
  23. #endif
  24.  
  25. #ifdef powerc
  26. ProcInfoType __procinfo = ExtensionUPPInfo;
  27. #endif
  28.  
  29. enum {
  30.     kOk        = 1,
  31.     kCancel,
  32.     kCaseSensitive,
  33.     kCutRadio,
  34.     kCopyRadio,
  35.     kMatchString
  36. };
  37.     
  38. typedef struct containInfo {
  39.     Boolean            cut;
  40.     Boolean            caseSensitive;
  41.     unsigned char    matchStr[256];
  42. } containInfo;
  43.  
  44. /*
  45.  * containSetControl
  46.  */
  47. static void containSetControl(DialogRef dialog, short item, Boolean flag)
  48. {
  49.     Rect     r;
  50.     Handle     hdl;
  51.     short     type;
  52.     
  53.     GetDialogItem(dialog, item, &type, &hdl, &r);
  54.     SetCtlValue((ControlHandle) hdl, flag);
  55. }
  56.  
  57. static void containSetButtons(DialogRef dialog, containInfo *info)
  58. {
  59.     unsigned char    str[256];
  60.     Rect             r;
  61.     Handle             hdl;
  62.     short             type;
  63.     
  64.     containSetControl(dialog, kCutRadio, info->cut);
  65.     containSetControl(dialog, kCopyRadio, !info->cut);
  66.     
  67.     containSetControl(dialog, kCaseSensitive, info->caseSensitive);
  68.     
  69.     GetDialogItem(dialog, kMatchString, &type, &hdl, &r);
  70.     GetIText(hdl, str);
  71.  
  72.     GetDialogItem(dialog, kOk, &type, &hdl, &r);
  73.     HiliteControl((ControlHandle) hdl, str[0] != 0 ? 0 : 0xff);
  74. }
  75.  
  76. /*
  77.  * containHandler
  78.  */
  79. static short containHandler(
  80.     DialogRef        dialog,
  81.     short            message,
  82.     short            itemHit,
  83.     void            *data
  84.     )
  85. {
  86.     short        type;
  87.     Handle        hdl;
  88.     Rect        r;
  89.     containInfo    *info = (containInfo *) data;
  90.     
  91.     switch (message) {
  92.     
  93.     case kDlgInit:
  94.         GetDialogItem(dialog, kMatchString, &type, &hdl, &r);
  95.         SetIText(hdl, info->matchStr);
  96.         SelIText(dialog, kMatchString, 0, 255);
  97.         
  98.         containSetButtons(dialog, info);
  99.         break;
  100.     
  101.     case kDlgItemHit:
  102.         
  103.         switch (itemHit) {
  104.         
  105.         case kCutRadio:
  106.         case kCopyRadio:
  107.             info->cut = (itemHit == kCutRadio);
  108.             break;
  109.         
  110.         case kCaseSensitive:
  111.             info->caseSensitive = !info->caseSensitive;
  112.             break;
  113.         }
  114.         
  115.         containSetButtons(dialog, info);
  116.         break;
  117.     
  118.     case kDlgClose:
  119.         GetDialogItem(dialog, kMatchString, &type, &hdl, &r);
  120.         GetIText(hdl, info->matchStr);
  121.         break;
  122.     }
  123.     
  124.     return(-1);
  125. }
  126.  
  127. void main(
  128.     ExternalCallbackBlock        *callbacks,
  129.     WindowRef                    window
  130.     )
  131. {
  132.     long             saved_a4;
  133.     containInfo        info;
  134.     long            length = sizeof(info);
  135.     short            itemHit;
  136.     Handle            text;
  137.     
  138.     saved_a4 = SetCurrentA4();
  139.     
  140.     /*
  141.      * Set up our default settings
  142.      */
  143.      
  144.     extGetPreferences(callbacks, 'Cont', &info, &length);
  145.     
  146.     if (length <= 0) {
  147.         
  148.         info.cut             = false;
  149.         info.caseSensitive     = false;
  150.         
  151.         *info.matchStr = 0;
  152.     }
  153.     
  154.     extDisplayDialog(callbacks, 128, containHandler, &info, &itemHit);
  155.  
  156.     if (itemHit == kOk) {
  157.  
  158.         /*
  159.          * Save our settings for next time
  160.          */
  161.          
  162.         length = sizeof(info);
  163.         extSetPreferences(callbacks, 'Cont', &info, &length);
  164.     }
  165.     
  166.     if (itemHit == kOk && info.matchStr[0] != 0) {
  167.     
  168.         long        startLine = 1, endLine;
  169.         long        pos, start, end;
  170.         Handle        hdl;
  171.         
  172.         /*
  173.          * Determine the line to operate on
  174.          */
  175.          
  176.         endLine = extLineCount(callbacks);
  177.         text    = NewHandle(0);
  178.         
  179.         extStartProgress(callbacks, info.cut ? 
  180.                          "\pCutting Lines..." :
  181.                          "\pCopying Lines...",
  182.                          endLine, true);
  183.         
  184.         /*
  185.          * Now process all the lines
  186.          */
  187.          
  188.         for (; startLine <= endLine; startLine++) {
  189.                 
  190.             if (extDoProgress(callbacks, startLine))
  191.                 break;
  192.                     
  193.             start = extLineToPosition(callbacks, startLine);
  194.             end   = extLineEnd(callbacks, startLine);
  195.             
  196.             pos = extFindText(callbacks, (unsigned char *) info.matchStr + 1,
  197.                               info.matchStr[0],
  198.                               start, nil, nil, info.caseSensitive, false);
  199.             
  200.             if (pos >= start && pos + info.matchStr[0] <= end) {
  201.                 
  202.                 extSetSelection(callbacks, start, startLine == endLine ?
  203.                                 end : end + 1);
  204.                 
  205.                 hdl = extCopy(callbacks);
  206.                 
  207.                 HandAndHand(hdl, text);
  208.                 
  209.                 DisposeHandle(hdl);
  210.                 
  211.                 if (info.cut)
  212.                     extDelete(callbacks);
  213.             }
  214.         }
  215.         
  216.         extDoneProgress(callbacks);
  217.         extPushClipboard(callbacks, text);
  218.     }
  219.     
  220.     SetA4(saved_a4);
  221. }
  222.