[74] | 1 | /*
|
---|
| 2 | * ICeCoffEETextEdit.c
|
---|
| 3 | * ICeCoffEE APE
|
---|
| 4 | *
|
---|
| 5 | * Created by Nicholas Riley on Sun Jan 26 2003.
|
---|
| 6 | * Copyright (c) 2003 Nicholas Riley. All rights reserved.
|
---|
| 7 | *
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | // based on ICeTEe.c by Michael Schürig
|
---|
| 11 |
|
---|
| 12 | #include "ICeCoffEETextEdit.h"
|
---|
| 13 | #include "ICeCoffEEConfig.h"
|
---|
| 14 | #include "ICeCoffEEShared.h"
|
---|
[75] | 15 | #include "ICeCoffEEActionMenu.h"
|
---|
[74] | 16 | #include <ApplicationEnhancer/ApplicationEnhancer.h>
|
---|
| 17 |
|
---|
| 18 | //=============================================================================
|
---|
| 19 | // Global Variables
|
---|
| 20 | //-----------------------------------------------------------------------------
|
---|
| 21 | TEClickProcPtr gTEClick = NULL;
|
---|
| 22 |
|
---|
| 23 | //=============================================================================
|
---|
| 24 | // Private function prototypes
|
---|
| 25 | //-----------------------------------------------------------------------------
|
---|
| 26 | void ICCF_LaunchURLFromTEClick(TEHandle teH, short oldSelStart, short oldSelEnd, Boolean optionKey);
|
---|
| 27 | OSStatus ICCF_DoCommandClick(TEHandle teH, long selStart, long selEnd, Boolean optionKey);
|
---|
| 28 |
|
---|
| 29 | //=============================================================================
|
---|
| 30 | // ICCF_LaunchURLFromTEClick : TEClick addition.
|
---|
| 31 | //-----------------------------------------------------------------------------
|
---|
| 32 | void ICCF_LaunchURLFromTEClick(TEHandle teH, short oldSelStart, short oldSelEnd, Boolean optionKey)
|
---|
| 33 | {
|
---|
| 34 | OSStatus err;
|
---|
| 35 | ICapeprintf("ICCF_LaunchURLFromTEClick entered\n");
|
---|
| 36 | if (ICCF_enabled && ICCF_prefs.commandClickEnabled) {
|
---|
| 37 | if ( !( (oldSelStart <= (**teH).selStart) && ((**teH).selStart <= oldSelEnd)
|
---|
| 38 | && (oldSelStart <= (**teH).selEnd) && ((**teH).selEnd <= oldSelEnd) ) ) {
|
---|
| 39 | oldSelStart = (**teH).selStart;
|
---|
| 40 | oldSelEnd = (**teH).selEnd;
|
---|
| 41 | }
|
---|
| 42 | err = ICCF_DoCommandClick(teH, oldSelStart, oldSelEnd, optionKey);
|
---|
| 43 | if (err != noErr && err != userCanceledErr) {
|
---|
| 44 | ICapeprintf("ICCF_DoCommandClick failed: %ld\n", err);
|
---|
| 45 | if (ICCF_prefs.errorSoundEnabled) SysBeep(10);
|
---|
| 46 | if (ICCF_prefs.errorDialogEnabled) {
|
---|
| 47 | CFStringRef alertTitle = ICCF_CopyLocalizedString(CFSTR("AlertTitle"));
|
---|
| 48 | CFStringRef alertMessageTemplate = ICCF_CopyLocalizedString(CFSTR("AlertMessage%@"));
|
---|
| 49 | CFStringRef errStr = ICCF_CopyErrString(err, CFSTR("ICCF_DoCommandClick"));
|
---|
| 50 | CFStringRef alertMessage = CFStringCreateWithFormat(NULL, NULL, alertMessageTemplate, errStr);
|
---|
| 51 | CFStringRef disableButton = ICCF_CopyLocalizedString(CFSTR("AlertDisableButton"));
|
---|
| 52 | AlertStdCFStringAlertParamRec paramRec = { kStdCFStringAlertVersionOne, true, false, NULL, disableButton, NULL, kStdOkItemIndex, 0, kWindowDefaultPosition, kNilOptions };
|
---|
| 53 |
|
---|
| 54 | DialogRef alertRef = NULL;
|
---|
| 55 | DialogItemIndex itemHit = kStdOkItemIndex;
|
---|
| 56 | err = CreateStandardAlert(kAlertNoteAlert, alertTitle, alertMessage, ¶mRec, &alertRef);
|
---|
| 57 |
|
---|
| 58 | if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf
|
---|
| 59 | apeprintf("ICCF_DoCommandClick failed, couldn't create alert (%ld): %@\n", err, errStr);
|
---|
| 60 | } else {
|
---|
| 61 | err = RunStandardAlert(alertRef, NULL, &itemHit);
|
---|
| 62 | if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf
|
---|
| 63 | apeprintf("ICCF_DoCommandClick failed, couldn't run alert (%ld): %@\n", err, errStr);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | if (alertTitle != NULL) CFRelease(alertTitle);
|
---|
| 68 | if (alertMessageTemplate != NULL) CFRelease(alertMessageTemplate);
|
---|
| 69 | if (alertMessage != NULL) CFRelease(alertMessage);
|
---|
| 70 | if (disableButton != NULL) CFRelease(disableButton);
|
---|
| 71 | if (errStr != NULL) CFRelease(errStr);
|
---|
| 72 |
|
---|
| 73 | if (itemHit == kStdCancelItemIndex) {
|
---|
| 74 | alertTitle = ICCF_CopyLocalizedString(CFSTR("DisableAlertTitle"));
|
---|
| 75 | alertMessage = ICCF_CopyLocalizedString(CFSTR("DisableAlertMessage"));
|
---|
| 76 | disableButton = ICCF_CopyLocalizedString(CFSTR("DisableAlertDisableButton"));
|
---|
| 77 | CFStringRef dontDisableButton = ICCF_CopyLocalizedString(CFSTR("DisableAlertDontDisableButton"));
|
---|
| 78 |
|
---|
| 79 | AlertStdCFStringAlertParamRec disableParamRec = { kStdCFStringAlertVersionOne, true, false, disableButton, dontDisableButton, NULL, kStdOkItemIndex, kStdCancelItemIndex, kWindowDefaultPosition, kNilOptions };
|
---|
| 80 |
|
---|
| 81 | itemHit = kStdCancelItemIndex;
|
---|
| 82 | err = CreateStandardAlert(kAlertNoteAlert, alertTitle, alertMessage, &disableParamRec, &alertRef);
|
---|
| 83 |
|
---|
| 84 | if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf
|
---|
| 85 | apeprintf("ICCF_DoCommandClick couldn't create alert (%ld)\n", err);
|
---|
| 86 | } else {
|
---|
| 87 | err = RunStandardAlert(alertRef, NULL, &itemHit);
|
---|
| 88 | if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf
|
---|
| 89 | apeprintf("ICCF_DoCommandClick couldn't run alert (%ld)\n", err);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | if (alertTitle != NULL) CFRelease(alertTitle);
|
---|
| 93 | if (alertMessage != NULL) CFRelease(alertMessage);
|
---|
| 94 | if (disableButton != NULL) CFRelease(disableButton);
|
---|
| 95 | if (dontDisableButton != NULL) CFRelease(dontDisableButton);
|
---|
| 96 |
|
---|
| 97 | if (itemHit == kStdOkItemIndex) ICCF_enabled = false;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | ICapeprintf("ICCF_LaunchURLFromTEClick exit\n");
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | //=============================================================================
|
---|
| 106 | // ICCF_DoCommandClick : Handle the click.
|
---|
| 107 | //-----------------------------------------------------------------------------
|
---|
| 108 | OSStatus ICCF_DoCommandClick(TEHandle teH, long selStart, long selEnd, Boolean optionKey)
|
---|
| 109 | {
|
---|
| 110 | ICInstance inst = NULL;
|
---|
| 111 | OSStatus err = noErr, err2;
|
---|
| 112 | ICapeprintf("ICCF_DoCommandClick entered\n");
|
---|
| 113 | if (inst == NULL) {
|
---|
| 114 | if ( (err = ICStart(&inst, kICCFCreator)) != noErr)
|
---|
| 115 | return err;
|
---|
| 116 | }
|
---|
| 117 | Handle textH;
|
---|
| 118 | SInt8 s;
|
---|
| 119 | ConstStringPtr hint;
|
---|
[79] | 120 | Boolean needsSlashes;
|
---|
[74] | 121 | textH = (Handle)TEGetText(teH);
|
---|
| 122 | s = HGetState(textH);
|
---|
| 123 | HLock(textH);
|
---|
[79] | 124 | hint = ICCF_GetHint(inst, *textH, GetHandleSize(textH), &selStart, &selEnd, &needsSlashes);
|
---|
| 125 | ICapeprintf("ICCF_DoCommandClick: %sneedsSlashes\n", needsSlashes ? "" : "doesn't ");
|
---|
[74] | 126 | if (hint != NULL) {
|
---|
| 127 | CGrafPtr thePort = teH[0]->inPort;
|
---|
| 128 | if (!QDIsPortBuffered(thePort)) thePort = NULL;
|
---|
| 129 | TESetSelect(selStart, selEnd, teH);
|
---|
| 130 | if (thePort != NULL) QDFlushPortBuffer(thePort, NULL);
|
---|
| 131 |
|
---|
| 132 | if (optionKey) {
|
---|
| 133 | Handle urlH = NewHandle(0);
|
---|
| 134 | if (urlH == NULL) {
|
---|
| 135 | err = MemError();
|
---|
| 136 | } else {
|
---|
| 137 | err = ICParseURL(inst, hint, *textH, GetHandleSize(textH), &selStart, &selEnd, urlH);
|
---|
[79] | 138 | if (err == noErr) {
|
---|
| 139 | if (needsSlashes) ICCF_AddSlashes(urlH, hint);
|
---|
| 140 | err = ICCF_DoURLActionMenu(inst, hint, *urlH, 0, GetHandleSize(urlH));
|
---|
| 141 | }
|
---|
[74] | 142 | DisposeHandle(urlH);
|
---|
| 143 | }
|
---|
| 144 | } else {
|
---|
[79] | 145 | if (needsSlashes) {
|
---|
| 146 | Handle urlH = NewHandle(0);
|
---|
| 147 | err = ICParseURL(inst, hint, *textH, GetHandleSize(textH), &selStart, &selEnd, urlH);
|
---|
| 148 | if (err == noErr) {
|
---|
| 149 | ICCF_AddSlashes(urlH, hint);
|
---|
| 150 | // don't want to disturb text selection
|
---|
| 151 | long tmpEnd = GetHandleSize(urlH);
|
---|
| 152 | long tmpStart = 0;
|
---|
| 153 | err = ICLaunchURL(inst, hint, *urlH, GetHandleSize(urlH), &tmpStart, &tmpEnd);
|
---|
| 154 | }
|
---|
| 155 | } else {
|
---|
| 156 | err = ICLaunchURL(inst, hint, *textH, GetHandleSize(textH), &selStart, &selEnd);
|
---|
| 157 | }
|
---|
[74] | 158 | }
|
---|
| 159 |
|
---|
| 160 | if (err == noErr) {
|
---|
| 161 | SInt32 junk;
|
---|
| 162 | SInt16 i = ICCF_prefs.textBlinkCount;
|
---|
| 163 | SInt32 blinkDelay = kICBlinkDelayUsecs / 10000; // XXX incorrect, fix
|
---|
| 164 | TESetSelect(selStart, selEnd, teH);
|
---|
| 165 | if (thePort != NULL) QDFlushPortBuffer(thePort, NULL);
|
---|
| 166 | while (i--) {
|
---|
| 167 | Delay(blinkDelay, &junk);
|
---|
| 168 | TEDeactivate(teH);
|
---|
| 169 | if (thePort != NULL) QDFlushPortBuffer(thePort, NULL);
|
---|
| 170 | Delay(blinkDelay, &junk);
|
---|
| 171 | TEActivate(teH);
|
---|
| 172 | if (thePort != NULL) QDFlushPortBuffer(thePort, NULL);
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | HSetState(textH, s);
|
---|
| 177 | err2 = ICStop(inst);
|
---|
| 178 | if (err == noErr)
|
---|
| 179 | err = err2;
|
---|
| 180 | ICapeprintf("ICCF_DoCommandClick exit\n");
|
---|
| 181 | return(err);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | void ICCF_TEClick(Point pt, Boolean fExtend, TEHandle teH) {
|
---|
| 185 | short oldSelStart, oldSelEnd;
|
---|
| 186 |
|
---|
| 187 | ICapeprintf("ICCF_TEClick entered\n");
|
---|
| 188 |
|
---|
| 189 | // Save old selection
|
---|
| 190 | oldSelStart = (**teH).selStart;
|
---|
| 191 | oldSelEnd = (**teH).selEnd;
|
---|
| 192 | // Get modifier keys
|
---|
| 193 | UInt32 modifiers = GetCurrentEventKeyModifiers();
|
---|
| 194 | // Call the original TEClick()
|
---|
| 195 | gTEClick(pt, fExtend, teH);
|
---|
| 196 | // Do our thing
|
---|
| 197 | if (modifiers & cmdKey) {
|
---|
| 198 | ICCF_LaunchURLFromTEClick(teH, oldSelStart, oldSelEnd, (modifiers & optionKey) != 0);
|
---|
| 199 | }
|
---|
| 200 | ICapeprintf("ICCF_TEClick exited\n");
|
---|
| 201 | }
|
---|