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

  1. /************************************************************
  2.  
  3.     Transform.c
  4.     
  5.     Preditor 3.0 extension.
  6.     
  7.     Displays a dialog prompting for text.  That text is then inserted or removed from
  8.     the beginning or end of all lines in the document, or optionally, all selected lines
  9.     in the document.
  10.  
  11.     © Copyright Evatac Software  1988-1996
  12.     All rights reserved
  13.  
  14. ************************************************************/
  15.  
  16. #include "PreditorExtension.h"
  17.  
  18. #include <SetupA4.h>
  19. #ifndef THINKC
  20. #include <A4Stuff.h>
  21. #else
  22. #define SetCurrentA4()    0; RememberA4()
  23. #define SetA4(x)        SetUpA4()
  24. #endif
  25.  
  26. #ifdef powerc
  27. ProcInfoType __procinfo = ExtensionUPPInfo;
  28. #endif
  29.  
  30. enum {
  31.     kOk        = 1,
  32.     kCancel,
  33.     kSelectionOnly,
  34.     kInsert,
  35.     kDelete,
  36.     kBeginning,
  37.     kEnd,
  38.     kTransformString
  39. };
  40.     
  41. typedef struct transformInfo {
  42.     Boolean            insert;
  43.     Boolean            lineStart;
  44.     Boolean            selOnly;
  45.     Boolean            pad;
  46.     
  47.     unsigned char    transformStr[256];
  48. } transformInfo;
  49.  
  50. /*
  51.  * transformSetControl
  52.  */
  53. static void transformSetControl(DialogRef dialog, short item, Boolean flag)
  54. {
  55.     Rect     r;
  56.     Handle     hdl;
  57.     short     type;
  58.     
  59.     GetDialogItem(dialog, item, &type, &hdl, &r);
  60.     SetCtlValue((ControlHandle) hdl, flag);
  61. }
  62.  
  63. static void transformSetButtons(DialogRef dialog, transformInfo *info)
  64. {
  65.     transformSetControl(dialog, kInsert, info->insert);
  66.     transformSetControl(dialog, kDelete, !info->insert);
  67.     
  68.     transformSetControl(dialog, kBeginning, info->lineStart);
  69.     transformSetControl(dialog, kEnd, !info->lineStart);
  70.  
  71.     transformSetControl(dialog, kSelectionOnly, info->selOnly);
  72. }
  73.  
  74. /*
  75.  * transformHandler
  76.  */
  77. static short transformHandler(
  78.     DialogRef        dialog,
  79.     short            message,
  80.     short            itemHit,
  81.     void            *data
  82.     )
  83. {
  84.     short            type;
  85.     Handle            hdl;
  86.     Rect            r;
  87.     transformInfo    *info = (transformInfo *) data;
  88.     
  89.     switch (message) {
  90.     
  91.     case kDlgInit:
  92.         GetDialogItem(dialog, kSelectionOnly, &type, &hdl, &r);
  93.         SetControlValue((ControlHandle) hdl, info->selOnly);
  94.         HiliteControl((ControlHandle) hdl,   info->selOnly ? 0 : 0xff);
  95.         
  96.         GetDialogItem(dialog, kTransformString, &type, &hdl, &r);
  97.         SetIText(hdl, info->transformStr);
  98.         SelIText(dialog, kTransformString, 0, 255);
  99.         
  100.         transformSetButtons(dialog, info);
  101.         break;
  102.     
  103.     case kDlgItemHit:
  104.         
  105.         switch (itemHit) {
  106.         
  107.         case kInsert:
  108.         case kDelete:
  109.             info->insert = (itemHit == kInsert);
  110.             break;
  111.         
  112.         case kBeginning:
  113.         case kEnd:
  114.             info->lineStart = (itemHit == kBeginning);
  115.             break;
  116.             
  117.         case kSelectionOnly:
  118.             info->selOnly = !info->selOnly;
  119.             break;
  120.         }
  121.         
  122.         transformSetButtons(dialog, info);
  123.         break;
  124.     
  125.     case kDlgClose:
  126.         GetDialogItem(dialog, kTransformString, &type, &hdl, &r);
  127.         GetIText(hdl, info->transformStr);
  128.         break;
  129.     }
  130.     
  131.     return(-1);
  132. }
  133.  
  134. void main(
  135.     ExternalCallbackBlock        *callbacks,
  136.     WindowRef                    window
  137.     )
  138. {
  139.     long             saved_a4;
  140.     transformInfo    info;
  141.     long            length = sizeof(info);
  142.     long            x, anchor, end;
  143.     short            itemHit;
  144.     
  145.     saved_a4 = SetCurrentA4();
  146.     
  147.     extGetSelection(callbacks, &anchor, &end);
  148.     
  149.     if (anchor > end) {
  150.         x = anchor; anchor = end; end = x;
  151.     }
  152.     
  153.     /*
  154.      * Set up our default settings
  155.      */
  156.      
  157.     extGetPreferences(callbacks, 'Prfx', &info, &length);
  158.     
  159.     if (length <= 0) {
  160.         
  161.         info.insert     = true;
  162.         info.lineStart     = true;
  163.         
  164.         BlockMove("\p> ", info.transformStr, 3);
  165.     }
  166.     
  167.     info.selOnly        = (anchor != end);
  168.     
  169.     extDisplayDialog(callbacks, 128, transformHandler, &info, &itemHit);
  170.  
  171.     if (itemHit == kOk) {
  172.  
  173.         /*
  174.          * Save our settings for next time
  175.          */
  176.          
  177.         length = sizeof(info);
  178.         extSetPreferences(callbacks, 'Prfx', &info, &length);
  179.     }
  180.     
  181.     if (itemHit == kOk && info.transformStr[0] != 0) {
  182.     
  183.         long        startLine = 1, endLine;
  184.         long        oldStart;
  185.         
  186.         /*
  187.          * Determine the line to operate on
  188.          */
  189.          
  190.         endLine = extLineCount(callbacks);
  191.         
  192.         if (info.selOnly) {
  193.             startLine    = extLineFromPosition(callbacks, anchor);
  194.             endLine        = extLineFromPosition(callbacks, end);
  195.         }
  196.         
  197.         oldStart = startLine - 1;
  198.         
  199.         extStartProgress(callbacks, (Char *) "\pChanging Lines...",
  200.                          endLine - startLine + 1, true);
  201.         
  202.         /*
  203.          * Now process all the lines
  204.          */
  205.          
  206.         if (info.lineStart) {
  207.         
  208.             for (; startLine <= endLine; startLine++) {
  209.                 
  210.                 if (extDoProgress(callbacks, startLine - oldStart))
  211.                     break;
  212.                     
  213.                 anchor = extLineToPosition(callbacks, startLine);
  214.                 
  215.                 if (info.insert) {
  216.                     extSetSelection(callbacks, anchor, anchor);
  217.                     extInsert(callbacks, info.transformStr + 1, info.transformStr[0]);
  218.                 }
  219.                 else {
  220.                     
  221.                     if (extFindText(callbacks, (unsigned char *) info.transformStr + 1,
  222.                                     info.transformStr[0],
  223.                                      anchor, nil, nil, false, false) >= 0) {
  224.                             
  225.                         extSetSelection(callbacks, anchor, anchor + info.transformStr[0]);
  226.                         extDelete(callbacks);
  227.                     }
  228.                 }
  229.             }
  230.         }
  231.         
  232.         else {
  233.         
  234.             for (; startLine < endLine; startLine++) {
  235.                 
  236.                 if (extDoProgress(callbacks, startLine - oldStart))
  237.                     break;
  238.                     
  239.                 anchor = extLineEnd(callbacks, startLine);
  240.                 
  241.                 if (info.insert) {
  242.                     extSetSelection(callbacks, anchor, anchor);
  243.                     extInsert(callbacks, info.transformStr + 1, info.transformStr[0]);
  244.                 }
  245.                 else {
  246.                     
  247.                     if (extFindText(callbacks, (unsigned char *) info.transformStr + 1,
  248.                                     info.transformStr[0],
  249.                                      anchor - info.transformStr[0],
  250.                                      nil, nil, false, false) >= 0) {
  251.                                 
  252.                         extSetSelection(callbacks, anchor - info.transformStr[0],
  253.                                         anchor);
  254.                         extDelete(callbacks);
  255.                     }
  256.                 }
  257.             }
  258.         }
  259.         
  260.         extDoneProgress(callbacks);
  261.     }
  262.     
  263.     SetA4(saved_a4);
  264. }
  265.