/* * ICeCoffEETextEdit.c * ICeCoffEE APE * * Created by Nicholas Riley on Sun Jan 26 2003. * Copyright (c) 2003 Nicholas Riley. All rights reserved. * */ // based on ICeTEe.c by Michael Schürig #include "ICeCoffEETextEdit.h" #include "ICeCoffEEConfig.h" #include "ICeCoffEEShared.h" #include "ICeCoffEEActionMenu.h" #include //============================================================================= // Global Variables //----------------------------------------------------------------------------- TEClickProcPtr gTEClick = NULL; //============================================================================= // Private function prototypes //----------------------------------------------------------------------------- void ICCF_LaunchURLFromTEClick(TEHandle teH, short oldSelStart, short oldSelEnd, Boolean optionKey); OSStatus ICCF_DoCommandClick(TEHandle teH, long selStart, long selEnd, Boolean optionKey); //============================================================================= // ICCF_LaunchURLFromTEClick : TEClick addition. //----------------------------------------------------------------------------- void ICCF_LaunchURLFromTEClick(TEHandle teH, short oldSelStart, short oldSelEnd, Boolean optionKey) { OSStatus err; ICapeprintf("ICCF_LaunchURLFromTEClick entered\n"); if (ICCF_enabled && ICCF_prefs.commandClickEnabled) { if ( !( (oldSelStart <= (**teH).selStart) && ((**teH).selStart <= oldSelEnd) && (oldSelStart <= (**teH).selEnd) && ((**teH).selEnd <= oldSelEnd) ) ) { oldSelStart = (**teH).selStart; oldSelEnd = (**teH).selEnd; } err = ICCF_DoCommandClick(teH, oldSelStart, oldSelEnd, optionKey); if (err != noErr && err != userCanceledErr) { ICapeprintf("ICCF_DoCommandClick failed: %ld\n", err); if (ICCF_prefs.errorSoundEnabled) SysBeep(10); if (ICCF_prefs.errorDialogEnabled) { CFStringRef alertTitle = ICCF_CopyLocalizedString(CFSTR("AlertTitle")); CFStringRef alertMessageTemplate = ICCF_CopyLocalizedString(CFSTR("AlertMessage%@")); CFStringRef errStr = ICCF_CopyErrString(err, CFSTR("ICCF_DoCommandClick")); CFStringRef alertMessage = CFStringCreateWithFormat(NULL, NULL, alertMessageTemplate, errStr); CFStringRef disableButton = ICCF_CopyLocalizedString(CFSTR("AlertDisableButton")); AlertStdCFStringAlertParamRec paramRec = { kStdCFStringAlertVersionOne, true, false, NULL, disableButton, NULL, kStdOkItemIndex, 0, kWindowDefaultPosition, kNilOptions }; DialogRef alertRef = NULL; DialogItemIndex itemHit = kStdOkItemIndex; err = CreateStandardAlert(kAlertNoteAlert, alertTitle, alertMessage, ¶mRec, &alertRef); if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf apeprintf("ICCF_DoCommandClick failed, couldn't create alert (%ld): %@\n", err, errStr); } else { err = RunStandardAlert(alertRef, NULL, &itemHit); if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf apeprintf("ICCF_DoCommandClick failed, couldn't run alert (%ld): %@\n", err, errStr); } } if (alertTitle != NULL) CFRelease(alertTitle); if (alertMessageTemplate != NULL) CFRelease(alertMessageTemplate); if (alertMessage != NULL) CFRelease(alertMessage); if (disableButton != NULL) CFRelease(disableButton); if (errStr != NULL) CFRelease(errStr); if (itemHit == kStdCancelItemIndex) { alertTitle = ICCF_CopyLocalizedString(CFSTR("DisableAlertTitle")); alertMessage = ICCF_CopyLocalizedString(CFSTR("DisableAlertMessage")); disableButton = ICCF_CopyLocalizedString(CFSTR("DisableAlertDisableButton")); CFStringRef dontDisableButton = ICCF_CopyLocalizedString(CFSTR("DisableAlertDontDisableButton")); AlertStdCFStringAlertParamRec disableParamRec = { kStdCFStringAlertVersionOne, true, false, disableButton, dontDisableButton, NULL, kStdOkItemIndex, kStdCancelItemIndex, kWindowDefaultPosition, kNilOptions }; itemHit = kStdCancelItemIndex; err = CreateStandardAlert(kAlertNoteAlert, alertTitle, alertMessage, &disableParamRec, &alertRef); if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf apeprintf("ICCF_DoCommandClick couldn't create alert (%ld)\n", err); } else { err = RunStandardAlert(alertRef, NULL, &itemHit); if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf apeprintf("ICCF_DoCommandClick couldn't run alert (%ld)\n", err); } } if (alertTitle != NULL) CFRelease(alertTitle); if (alertMessage != NULL) CFRelease(alertMessage); if (disableButton != NULL) CFRelease(disableButton); if (dontDisableButton != NULL) CFRelease(dontDisableButton); if (itemHit == kStdOkItemIndex) ICCF_enabled = false; } } } } ICapeprintf("ICCF_LaunchURLFromTEClick exit\n"); } //============================================================================= // ICCF_DoCommandClick : Handle the click. //----------------------------------------------------------------------------- OSStatus ICCF_DoCommandClick(TEHandle teH, long selStart, long selEnd, Boolean optionKey) { ICInstance inst = NULL; OSStatus err = noErr, err2; ICapeprintf("ICCF_DoCommandClick entered\n"); if (inst == NULL) { if ( (err = ICStart(&inst, kICCFCreator)) != noErr) return err; } Handle textH; SInt8 s; ConstStringPtr hint; Boolean needsSlashes; textH = (Handle)TEGetText(teH); s = HGetState(textH); HLock(textH); hint = ICCF_GetHint(inst, *textH, GetHandleSize(textH), &selStart, &selEnd, &needsSlashes); ICapeprintf("ICCF_DoCommandClick: %sneedsSlashes\n", needsSlashes ? "" : "doesn't "); if (hint != NULL) { CGrafPtr thePort = teH[0]->inPort; if (!QDIsPortBuffered(thePort)) thePort = NULL; TESetSelect(selStart, selEnd, teH); if (thePort != NULL) QDFlushPortBuffer(thePort, NULL); if (optionKey) { Handle urlH = NewHandle(0); if (urlH == NULL) { err = MemError(); } else { err = ICParseURL(inst, hint, *textH, GetHandleSize(textH), &selStart, &selEnd, urlH); if (err == noErr) { if (needsSlashes) ICCF_AddSlashes(urlH, hint); err = ICCF_DoURLActionMenu(inst, hint, *urlH, 0, GetHandleSize(urlH)); } DisposeHandle(urlH); } } else { if (needsSlashes) { Handle urlH = NewHandle(0); err = ICParseURL(inst, hint, *textH, GetHandleSize(textH), &selStart, &selEnd, urlH); if (err == noErr) { ICCF_AddSlashes(urlH, hint); // don't want to disturb text selection long tmpEnd = GetHandleSize(urlH); long tmpStart = 0; err = ICLaunchURL(inst, hint, *urlH, GetHandleSize(urlH), &tmpStart, &tmpEnd); } } else { err = ICLaunchURL(inst, hint, *textH, GetHandleSize(textH), &selStart, &selEnd); } } if (err == noErr) { SInt32 junk; SInt16 i = ICCF_prefs.textBlinkCount; SInt32 blinkDelay = kICBlinkDelayUsecs / 10000; // XXX incorrect, fix TESetSelect(selStart, selEnd, teH); if (thePort != NULL) QDFlushPortBuffer(thePort, NULL); while (i--) { Delay(blinkDelay, &junk); TEDeactivate(teH); if (thePort != NULL) QDFlushPortBuffer(thePort, NULL); Delay(blinkDelay, &junk); TEActivate(teH); if (thePort != NULL) QDFlushPortBuffer(thePort, NULL); } } } HSetState(textH, s); err2 = ICStop(inst); if (err == noErr) err = err2; ICapeprintf("ICCF_DoCommandClick exit\n"); return(err); } void ICCF_TEClick(Point pt, Boolean fExtend, TEHandle teH) { short oldSelStart, oldSelEnd; ICapeprintf("ICCF_TEClick entered\n"); // Save old selection oldSelStart = (**teH).selStart; oldSelEnd = (**teH).selEnd; // Get modifier keys UInt32 modifiers = GetCurrentEventKeyModifiers(); // Call the original TEClick() gTEClick(pt, fExtend, teH); // Do our thing if (modifiers & cmdKey) { ICCF_LaunchURLFromTEClick(teH, oldSelStart, oldSelEnd, (modifiers & optionKey) != 0); } ICapeprintf("ICCF_TEClick exited\n"); }