home *** CD-ROM | disk | FTP | other *** search
- /************************************************************
-
- Cut.c
-
- Preditor 3.0 extension.
-
- Cuts or copies any line of text in the selection that matches a match
- string. The match string provided by the user.
-
- © Copyright Evatac Software 1988-1996
- All rights reserved
-
- ************************************************************/
-
- #include "PreditorExtension.h"
-
- #include <SetupA4.h>
- #ifndef THINKC
- #include <A4Stuff.h>
- #else
- #define SetCurrentA4() 0; RememberA4()
- #define SetA4(x) SetUpA4()
- #endif
-
- #ifdef powerc
- ProcInfoType __procinfo = ExtensionUPPInfo;
- #endif
-
- enum {
- kOk = 1,
- kCancel,
- kCaseSensitive,
- kCutRadio,
- kCopyRadio,
- kMatchString
- };
-
- typedef struct containInfo {
- Boolean cut;
- Boolean caseSensitive;
- unsigned char matchStr[256];
- } containInfo;
-
- /*
- * containSetControl
- */
- static void containSetControl(DialogRef dialog, short item, Boolean flag)
- {
- Rect r;
- Handle hdl;
- short type;
-
- GetDialogItem(dialog, item, &type, &hdl, &r);
- SetCtlValue((ControlHandle) hdl, flag);
- }
-
- static void containSetButtons(DialogRef dialog, containInfo *info)
- {
- unsigned char str[256];
- Rect r;
- Handle hdl;
- short type;
-
- containSetControl(dialog, kCutRadio, info->cut);
- containSetControl(dialog, kCopyRadio, !info->cut);
-
- containSetControl(dialog, kCaseSensitive, info->caseSensitive);
-
- GetDialogItem(dialog, kMatchString, &type, &hdl, &r);
- GetIText(hdl, str);
-
- GetDialogItem(dialog, kOk, &type, &hdl, &r);
- HiliteControl((ControlHandle) hdl, str[0] != 0 ? 0 : 0xff);
- }
-
- /*
- * containHandler
- */
- static short containHandler(
- DialogRef dialog,
- short message,
- short itemHit,
- void *data
- )
- {
- short type;
- Handle hdl;
- Rect r;
- containInfo *info = (containInfo *) data;
-
- switch (message) {
-
- case kDlgInit:
- GetDialogItem(dialog, kMatchString, &type, &hdl, &r);
- SetIText(hdl, info->matchStr);
- SelIText(dialog, kMatchString, 0, 255);
-
- containSetButtons(dialog, info);
- break;
-
- case kDlgItemHit:
-
- switch (itemHit) {
-
- case kCutRadio:
- case kCopyRadio:
- info->cut = (itemHit == kCutRadio);
- break;
-
- case kCaseSensitive:
- info->caseSensitive = !info->caseSensitive;
- break;
- }
-
- containSetButtons(dialog, info);
- break;
-
- case kDlgClose:
- GetDialogItem(dialog, kMatchString, &type, &hdl, &r);
- GetIText(hdl, info->matchStr);
- break;
- }
-
- return(-1);
- }
-
- void main(
- ExternalCallbackBlock *callbacks,
- WindowRef window
- )
- {
- long saved_a4;
- containInfo info;
- long length = sizeof(info);
- short itemHit;
- Handle text;
-
- saved_a4 = SetCurrentA4();
-
- /*
- * Set up our default settings
- */
-
- extGetPreferences(callbacks, 'Cont', &info, &length);
-
- if (length <= 0) {
-
- info.cut = false;
- info.caseSensitive = false;
-
- *info.matchStr = 0;
- }
-
- extDisplayDialog(callbacks, 128, containHandler, &info, &itemHit);
-
- if (itemHit == kOk) {
-
- /*
- * Save our settings for next time
- */
-
- length = sizeof(info);
- extSetPreferences(callbacks, 'Cont', &info, &length);
- }
-
- if (itemHit == kOk && info.matchStr[0] != 0) {
-
- long startLine = 1, endLine;
- long pos, start, end;
- Handle hdl;
-
- /*
- * Determine the line to operate on
- */
-
- endLine = extLineCount(callbacks);
- text = NewHandle(0);
-
- extStartProgress(callbacks, info.cut ?
- "\pCutting Lines..." :
- "\pCopying Lines...",
- endLine, true);
-
- /*
- * Now process all the lines
- */
-
- for (; startLine <= endLine; startLine++) {
-
- if (extDoProgress(callbacks, startLine))
- break;
-
- start = extLineToPosition(callbacks, startLine);
- end = extLineEnd(callbacks, startLine);
-
- pos = extFindText(callbacks, (unsigned char *) info.matchStr + 1,
- info.matchStr[0],
- start, nil, nil, info.caseSensitive, false);
-
- if (pos >= start && pos + info.matchStr[0] <= end) {
-
- extSetSelection(callbacks, start, startLine == endLine ?
- end : end + 1);
-
- hdl = extCopy(callbacks);
-
- HandAndHand(hdl, text);
-
- DisposeHandle(hdl);
-
- if (info.cut)
- extDelete(callbacks);
- }
- }
-
- extDoneProgress(callbacks);
- extPushClipboard(callbacks, text);
- }
-
- SetA4(saved_a4);
- }
-