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 | //-----------------------------------------------------------------------------
|
---|
20 | TEClickProcPtr gTEClick = NULL;
|
---|
21 |
|
---|
22 | //=============================================================================
|
---|
23 | // Private function prototypes
|
---|
24 | //-----------------------------------------------------------------------------
|
---|
25 | void ICCF_LaunchURLFromTEClick(TEHandle teH, short oldSelStart, short oldSelEnd, iccfURLAction action);
|
---|
26 | OSStatus ICCF_DoCommandClick(TEHandle teH, long selStart, long selEnd, iccfURLAction action);
|
---|
27 |
|
---|
28 | //=============================================================================
|
---|
29 | // ICCF_LaunchURLFromTEClick : TEClick addition.
|
---|
30 | //-----------------------------------------------------------------------------
|
---|
31 | void ICCF_LaunchURLFromTEClick(TEHandle teH, short oldSelStart, short oldSelEnd, iccfURLAction action)
|
---|
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 | }
|
---|
41 | err = ICCF_DoCommandClick(teH, oldSelStart, oldSelEnd, action);
|
---|
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, ¶mRec, &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"));
|
---|
74 | alertMessageTemplate = ICCF_CopyLocalizedString(CFSTR("DisableAlertMessage%@"));
|
---|
75 | CFStringRef appName = ICCF_CopyAppName();
|
---|
76 | alertMessage = CFStringCreateWithFormat(NULL, NULL, alertMessageTemplate, appName);
|
---|
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 (alertMessageTemplate != NULL) CFRelease(alertMessageTemplate);
|
---|
95 | if (alertMessage != NULL) CFRelease(alertMessage);
|
---|
96 | if (appName != NULL) CFRelease(appName);
|
---|
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 | //-----------------------------------------------------------------------------
|
---|
111 | OSStatus ICCF_DoCommandClick(TEHandle teH, long selStart, long selEnd, iccfURLAction action)
|
---|
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;
|
---|
123 | Boolean needsSlashes;
|
---|
124 | textH = (Handle)TEGetText(teH);
|
---|
125 | s = HGetState(textH);
|
---|
126 | HLock(textH);
|
---|
127 | hint = ICCF_GetHint(inst, *textH, GetHandleSize(textH), &selStart, &selEnd, &needsSlashes);
|
---|
128 | ICapeprintf("ICCF_DoCommandClick: %sneedsSlashes\n", needsSlashes ? "" : "doesn't ");
|
---|
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 |
|
---|
135 | Handle urlH = NewHandle(0);
|
---|
136 | if (urlH == NULL) {
|
---|
137 | err = MemError();
|
---|
138 | } else {
|
---|
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);
|
---|
143 | }
|
---|
144 | DisposeHandle(urlH);
|
---|
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 |
|
---|
171 | void 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) {
|
---|
185 | iccfURLAction action;
|
---|
186 | action.presentMenu = (modifiers & optionKey) != 0;
|
---|
187 | action.launchInBackground = (modifiers & shiftKey) != 0;
|
---|
188 | ICCF_LaunchURLFromTEClick(teH, oldSelStart, oldSelEnd, action);
|
---|
189 | }
|
---|
190 | ICapeprintf("ICCF_TEClick exited\n");
|
---|
191 | }
|
---|