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 "ICeCoffEEActionMenu.h" |
---|
16 | #include <ApplicationEnhancer/ApplicationEnhancer.h> |
---|
17 | |
---|
18 | //============================================================================= |
---|
19 | // Global Variables |
---|
20 | //----------------------------------------------------------------------------- |
---|
21 | TEClickProcPtr gTEClick = NULL; |
---|
22 | Handle gErrors; |
---|
23 | |
---|
24 | //============================================================================= |
---|
25 | // Private function prototypes |
---|
26 | //----------------------------------------------------------------------------- |
---|
27 | void ICCF_LaunchURLFromTEClick(TEHandle teH, short oldSelStart, short oldSelEnd, Boolean optionKey); |
---|
28 | OSStatus ICCF_DoCommandClick(TEHandle teH, long selStart, long selEnd, Boolean optionKey); |
---|
29 | |
---|
30 | //============================================================================= |
---|
31 | // ICCF_LaunchURLFromTEClick : TEClick addition. |
---|
32 | //----------------------------------------------------------------------------- |
---|
33 | void ICCF_LaunchURLFromTEClick(TEHandle teH, short oldSelStart, short oldSelEnd, Boolean optionKey) |
---|
34 | { |
---|
35 | OSStatus err; |
---|
36 | ICapeprintf("ICCF_LaunchURLFromTEClick entered\n"); |
---|
37 | if (ICCF_enabled && ICCF_prefs.commandClickEnabled) { |
---|
38 | if ( !( (oldSelStart <= (**teH).selStart) && ((**teH).selStart <= oldSelEnd) |
---|
39 | && (oldSelStart <= (**teH).selEnd) && ((**teH).selEnd <= oldSelEnd) ) ) { |
---|
40 | oldSelStart = (**teH).selStart; |
---|
41 | oldSelEnd = (**teH).selEnd; |
---|
42 | } |
---|
43 | err = ICCF_DoCommandClick(teH, oldSelStart, oldSelEnd, optionKey); |
---|
44 | if (err != noErr && err != userCanceledErr) { |
---|
45 | ICapeprintf("ICCF_DoCommandClick failed: %ld\n", err); |
---|
46 | if (ICCF_prefs.errorSoundEnabled) SysBeep(10); |
---|
47 | if (ICCF_prefs.errorDialogEnabled) { |
---|
48 | CFStringRef alertTitle = ICCF_CopyLocalizedString(CFSTR("AlertTitle")); |
---|
49 | CFStringRef alertMessageTemplate = ICCF_CopyLocalizedString(CFSTR("AlertMessage%@")); |
---|
50 | CFStringRef errStr = ICCF_CopyErrString(err, CFSTR("ICCF_DoCommandClick")); |
---|
51 | CFStringRef alertMessage = CFStringCreateWithFormat(NULL, NULL, alertMessageTemplate, errStr); |
---|
52 | CFStringRef disableButton = ICCF_CopyLocalizedString(CFSTR("AlertDisableButton")); |
---|
53 | AlertStdCFStringAlertParamRec paramRec = { kStdCFStringAlertVersionOne, true, false, NULL, disableButton, NULL, kStdOkItemIndex, 0, kWindowDefaultPosition, kNilOptions }; |
---|
54 | |
---|
55 | DialogRef alertRef = NULL; |
---|
56 | DialogItemIndex itemHit = kStdOkItemIndex; |
---|
57 | err = CreateStandardAlert(kAlertNoteAlert, alertTitle, alertMessage, ¶mRec, &alertRef); |
---|
58 | |
---|
59 | if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf |
---|
60 | apeprintf("ICCF_DoCommandClick failed, couldn't create alert (%ld): %@\n", err, errStr); |
---|
61 | } else { |
---|
62 | err = RunStandardAlert(alertRef, NULL, &itemHit); |
---|
63 | if (err != noErr) { // last resort: should be apeprintf, not ICapeprintf |
---|
64 | apeprintf("ICCF_DoCommandClick failed, couldn't run alert (%ld): %@\n", err, errStr); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | if (alertTitle != NULL) CFRelease(alertTitle); |
---|
69 | if (alertMessageTemplate != NULL) CFRelease(alertMessageTemplate); |
---|
70 | if (alertMessage != NULL) CFRelease(alertMessage); |
---|
71 | if (disableButton != NULL) CFRelease(disableButton); |
---|
72 | if (errStr != NULL) CFRelease(errStr); |
---|
73 | |
---|
74 | if (itemHit == kStdCancelItemIndex) { |
---|
75 | alertTitle = ICCF_CopyLocalizedString(CFSTR("DisableAlertTitle")); |
---|
76 | alertMessage = ICCF_CopyLocalizedString(CFSTR("DisableAlertMessage")); |
---|
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); |
---|
94 | if (alertMessage != NULL) CFRelease(alertMessage); |
---|
95 | if (disableButton != NULL) CFRelease(disableButton); |
---|
96 | if (dontDisableButton != NULL) CFRelease(dontDisableButton); |
---|
97 | |
---|
98 | if (itemHit == kStdOkItemIndex) ICCF_enabled = false; |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|
103 | ICapeprintf("ICCF_LaunchURLFromTEClick exit\n"); |
---|
104 | } |
---|
105 | |
---|
106 | //============================================================================= |
---|
107 | // ICCF_DoCommandClick : Handle the click. |
---|
108 | //----------------------------------------------------------------------------- |
---|
109 | OSStatus ICCF_DoCommandClick(TEHandle teH, long selStart, long selEnd, Boolean optionKey) |
---|
110 | { |
---|
111 | ICInstance inst = NULL; |
---|
112 | OSStatus err = noErr, err2; |
---|
113 | ICapeprintf("ICCF_DoCommandClick entered\n"); |
---|
114 | if (inst == NULL) { |
---|
115 | if ( (err = ICStart(&inst, kICCFCreator)) != noErr) |
---|
116 | return err; |
---|
117 | } |
---|
118 | Handle textH; |
---|
119 | SInt8 s; |
---|
120 | ConstStringPtr hint; |
---|
121 | textH = (Handle)TEGetText(teH); |
---|
122 | s = HGetState(textH); |
---|
123 | HLock(textH); |
---|
124 | hint = ICCF_GetHint(inst, *textH, selStart, selEnd); |
---|
125 | if (hint != NULL) { |
---|
126 | CGrafPtr thePort = teH[0]->inPort; |
---|
127 | if (!QDIsPortBuffered(thePort)) thePort = NULL; |
---|
128 | |
---|
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); |
---|
138 | if (err == noErr) |
---|
139 | err = ICCF_DoURLActionMenu(inst, hint, *urlH, 0, GetHandleSize(urlH) - 1); |
---|
140 | DisposeHandle(urlH); |
---|
141 | } |
---|
142 | } else { |
---|
143 | err = ICLaunchURL(inst, hint, *textH, GetHandleSize(textH), &selStart, &selEnd); |
---|
144 | } |
---|
145 | |
---|
146 | if (err == noErr) { |
---|
147 | SInt32 junk; |
---|
148 | SInt16 i = ICCF_prefs.textBlinkCount; |
---|
149 | SInt32 blinkDelay = kICBlinkDelayUsecs / 10000; // XXX incorrect, fix |
---|
150 | TESetSelect(selStart, selEnd, teH); |
---|
151 | if (thePort != NULL) QDFlushPortBuffer(thePort, NULL); |
---|
152 | while (i--) { |
---|
153 | Delay(blinkDelay, &junk); |
---|
154 | TEDeactivate(teH); |
---|
155 | if (thePort != NULL) QDFlushPortBuffer(thePort, NULL); |
---|
156 | Delay(blinkDelay, &junk); |
---|
157 | TEActivate(teH); |
---|
158 | if (thePort != NULL) QDFlushPortBuffer(thePort, NULL); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | HSetState(textH, s); |
---|
163 | err2 = ICStop(inst); |
---|
164 | if (err == noErr) |
---|
165 | err = err2; |
---|
166 | ICapeprintf("ICCF_DoCommandClick exit\n"); |
---|
167 | return(err); |
---|
168 | } |
---|
169 | |
---|
170 | void ICCF_TEClick(Point pt, Boolean fExtend, TEHandle teH) { |
---|
171 | short oldSelStart, oldSelEnd; |
---|
172 | |
---|
173 | ICapeprintf("ICCF_TEClick entered\n"); |
---|
174 | |
---|
175 | // Save old selection |
---|
176 | oldSelStart = (**teH).selStart; |
---|
177 | oldSelEnd = (**teH).selEnd; |
---|
178 | // Get modifier keys |
---|
179 | UInt32 modifiers = GetCurrentEventKeyModifiers(); |
---|
180 | // Call the original TEClick() |
---|
181 | gTEClick(pt, fExtend, teH); |
---|
182 | // Do our thing |
---|
183 | if (modifiers & cmdKey) { |
---|
184 | ICCF_LaunchURLFromTEClick(teH, oldSelStart, oldSelEnd, (modifiers & optionKey) != 0); |
---|
185 | } |
---|
186 | ICapeprintf("ICCF_TEClick exited\n"); |
---|
187 | } |
---|