source: trunk/ICeCoffEE/ICeCoffEE/ICeCoffEETextEdit.c@ 160

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