[182] | 1 | // ICeCoffEE - Internet Config Carbon/Cocoa Editor Extension
|
---|
[66] | 2 | // Nicholas Riley <mailto:icecoffee@sabi.net>
|
---|
| 3 |
|
---|
| 4 | #import "ICeCoffEE.h"
|
---|
| 5 | #import <Carbon/Carbon.h>
|
---|
| 6 | #include <unistd.h>
|
---|
| 7 | #import "ICeCoffEESuper.h"
|
---|
[320] | 8 | #import "ICeCoffEEServices.h"
|
---|
[388] | 9 | #import "ICeCoffEETextViewTrigger.h"
|
---|
[322] | 10 | #import "ICeCoffEEParser.h"
|
---|
[66] | 11 |
|
---|
[74] | 12 | iccfPrefRec ICCF_prefs;
|
---|
[66] | 13 |
|
---|
[74] | 14 | NSString *ICCF_ErrString(OSStatus err, NSString *context) {
|
---|
| 15 | if (err == noErr || err == userCanceledErr) return nil;
|
---|
[66] | 16 |
|
---|
[74] | 17 | NSString *errNum = [NSString stringWithFormat: @"%ld", err];
|
---|
| 18 | NSString *errDesc = ICCF_LocalizedString(errNum);
|
---|
[66] | 19 |
|
---|
[74] | 20 | if (errDesc == NULL || errDesc == errNum)
|
---|
| 21 | errDesc = [NSString stringWithFormat: ICCF_LocalizedString(@"An unknown error occurred in %@"), context];
|
---|
| 22 |
|
---|
[66] | 23 | return [NSString stringWithFormat: @"%@ (%d)", errDesc, (int)err];
|
---|
| 24 | }
|
---|
| 25 |
|
---|
[74] | 26 | CFStringRef ICCF_CopyErrString(OSStatus err, CFStringRef context) {
|
---|
| 27 | if (err == noErr || err == userCanceledErr) return NULL;
|
---|
| 28 |
|
---|
| 29 | CFStringRef errNum = CFStringCreateWithFormat(NULL, NULL, CFSTR("%ld"), err);
|
---|
| 30 | CFStringRef errDesc = ICCF_CopyLocalizedString(errNum);
|
---|
| 31 |
|
---|
| 32 | if (errDesc == NULL || errDesc == errNum) {
|
---|
| 33 | CFStringRef errDescFormat = ICCF_CopyLocalizedString(CFSTR("An unknown error occurred in %@"));
|
---|
| 34 | if (errDesc != NULL) CFRelease(errDesc);
|
---|
| 35 | errDesc = CFStringCreateWithFormat(NULL, NULL, errDescFormat, context);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | CFStringRef errStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@ (%d)"), errDesc, (int)err);
|
---|
| 39 |
|
---|
| 40 | if (errNum != NULL) CFRelease(errNum);
|
---|
| 41 | if (errDesc != NULL) CFRelease(errDesc);
|
---|
| 42 | return errStr;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[139] | 45 | CFStringRef ICCF_CopyAppName() {
|
---|
| 46 | ProcessSerialNumber psn = {0, kCurrentProcess};
|
---|
| 47 | CFStringRef appName = NULL;
|
---|
| 48 | CopyProcessName(&psn, &appName);
|
---|
| 49 | if (appName == NULL) return CFSTR("(unknown)");
|
---|
| 50 | return appName;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[66] | 53 | BOOL ICCF_EventIsCommandMouseDown(NSEvent *e) {
|
---|
[106] | 54 | return ([e type] == NSLeftMouseDown && ([e modifierFlags] & NSCommandKeyMask) != 0 && [e clickCount] == 1);
|
---|
[66] | 55 | }
|
---|
| 56 |
|
---|
[183] | 57 | iccfURLAction ICCF_KeyboardAction(NSEvent *e) {
|
---|
| 58 | unsigned int modifierFlags = [e modifierFlags];
|
---|
[106] | 59 | iccfURLAction action;
|
---|
| 60 | action.presentMenu = (modifierFlags & NSAlternateKeyMask) != 0;
|
---|
| 61 | action.launchInBackground = (modifierFlags & NSShiftKeyMask) != 0;
|
---|
| 62 | return action;
|
---|
[74] | 63 | }
|
---|
| 64 |
|
---|
[375] | 65 | // RFC-ordained max URL length, just to avoid passing IC/LS multi-megabyte documents
|
---|
| 66 | #if ICCF_DEBUG
|
---|
[381] | 67 | const long ICCF_MAX_URL_LEN = 1024; // XXX change later
|
---|
[375] | 68 | #else
|
---|
[381] | 69 | const long ICCF_MAX_URL_LEN = 1024;
|
---|
[375] | 70 | #endif
|
---|
| 71 |
|
---|
| 72 | void ICCF_CheckRange(NSRange range) {
|
---|
| 73 | NSCAssert(range.length > 0, ICCF_LocalizedString(@"No URL is selected"));
|
---|
| 74 | NSCAssert1(range.length <= ICCF_MAX_URL_LEN, ICCF_LocalizedString(@"The potential URL is longer than %lu characters"), ICCF_MAX_URL_LEN);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[79] | 77 | ConstStringPtr ICCF_GetHint(ICInstance inst, const char *urlData, Size length, long *selStart, long *selEnd, Boolean *needsSlashes) {
|
---|
[74] | 78 | Handle h = NewHandle(0);
|
---|
| 79 | OSStatus err;
|
---|
[322] | 80 |
|
---|
[74] | 81 | if (h == NULL) return NULL;
|
---|
[322] | 82 |
|
---|
[74] | 83 | // parse the URL providing a bogus protocol, to get rid of escaped forms
|
---|
[79] | 84 | err = ICParseURL(inst, "\p*", urlData, length, selStart, selEnd, h);
|
---|
[74] | 85 | if (err != noErr) return NULL;
|
---|
[322] | 86 |
|
---|
[74] | 87 | // scan through the parsed URL looking for characters not found in email addresses
|
---|
| 88 | Size hSize = GetHandleSize(h);
|
---|
| 89 | if (hSize == 0) return NULL;
|
---|
[322] | 90 |
|
---|
[74] | 91 | const char *urlParsed = *h;
|
---|
| 92 | long i = 0;
|
---|
| 93 | Boolean sawAt = false;
|
---|
[79] | 94 | if (hSize >= 2 && urlParsed[0] == '*' && urlParsed[1] == ':') {
|
---|
[74] | 95 | // this is an IC-inserted protocol; skip over it
|
---|
| 96 | i = 2;
|
---|
[79] | 97 | *needsSlashes = (hSize < i + 2 || urlParsed[i] != '/' || urlParsed[i + 1] != '/');
|
---|
| 98 | } else *needsSlashes = false;
|
---|
[74] | 99 | for ( ; i < hSize ; i++) {
|
---|
| 100 | char c = urlParsed[i];
|
---|
| 101 | if (c == '@') {
|
---|
| 102 | sawAt = true;
|
---|
| 103 | } else if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
|
---|
| 104 | (c == '+' || c == '-' || c == '_' || c == '!' || c == '.'))) {
|
---|
| 105 | DisposeHandle(h);
|
---|
| 106 | return "\phttp";
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | DisposeHandle(h);
|
---|
[79] | 110 | if (sawAt) {
|
---|
| 111 | *needsSlashes = false;
|
---|
| 112 | return "\pmailto";
|
---|
| 113 | }
|
---|
| 114 | return "\phttp";
|
---|
[74] | 115 | }
|
---|
| 116 |
|
---|
[79] | 117 | static const char *kICSlashes = "//";
|
---|
| 118 |
|
---|
| 119 | void ICCF_AddSlashes(Handle h, ConstStringPtr hint) {
|
---|
| 120 | Size sizeBefore = GetHandleSize(h);
|
---|
| 121 | unsigned char hintLength = StrLength(hint);
|
---|
| 122 | char *copy = (char *)malloc(sizeBefore);
|
---|
| 123 | memcpy(copy, *h, sizeBefore);
|
---|
| 124 | ICLog(@"ICCF_AddSlashes before: |%s|\n", *h);
|
---|
| 125 | ReallocateHandle(h, sizeBefore + 2);
|
---|
[322] | 126 |
|
---|
[79] | 127 | // if *h begins with '<hint>:', then copy the slashes after it
|
---|
[181] | 128 | if (sizeBefore > hintLength + 1 && strncmp((const char *)&hint[1], copy, hintLength) == 0 && copy[hintLength] == ':') {
|
---|
[79] | 129 | memcpy(*h, copy, hintLength + 1);
|
---|
| 130 | memcpy(*h + hintLength + 1, kICSlashes, 2);
|
---|
| 131 | memcpy(*h + hintLength + 3, ©[hintLength + 1], sizeBefore - hintLength - 1);
|
---|
| 132 | } else {
|
---|
| 133 | memcpy(*h, kICSlashes, 2);
|
---|
| 134 | memcpy(*h + 2, copy, sizeBefore);
|
---|
| 135 | }
|
---|
[322] | 136 |
|
---|
[79] | 137 | free(copy);
|
---|
| 138 | ICLog(@"ICCF_AddSlashes after: |%s|\n", *h);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[183] | 141 | BOOL ICCF_LaunchURL(NSString *string, iccfURLAction action) {
|
---|
| 142 | OSStatus err = noErr;
|
---|
[66] | 143 | long selStart, selEnd;
|
---|
[272] | 144 | NSMutableString *urlString = [[NSMutableString alloc] init];
|
---|
| 145 | NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
|
---|
| 146 | NSScanner *scanner = [[NSScanner alloc] initWithString: string];
|
---|
| 147 | NSString *fragmentString;
|
---|
| 148 | while ([scanner scanUpToCharactersFromSet: whitespace intoString: &fragmentString]) {
|
---|
| 149 | [urlString appendString: fragmentString];
|
---|
| 150 | }
|
---|
| 151 | unsigned len = [urlString length];
|
---|
[66] | 152 |
|
---|
[79] | 153 | Handle h = NULL;
|
---|
[66] | 154 |
|
---|
| 155 | NS_DURING
|
---|
[79] | 156 | h = NewHandle(len);
|
---|
| 157 | if (h == NULL)
|
---|
| 158 | ICCF_OSErrCAssert(MemError(), @"NewHandle");
|
---|
[66] | 159 |
|
---|
[272] | 160 | if (CFStringGetBytes((CFStringRef)urlString, CFRangeMake(0, len), kCFStringEncodingASCII, '\0', false, (UInt8 *)*h, len, NULL) != len)
|
---|
[79] | 161 | ICCF_OSErrCAssert(kTECNoConversionPathErr, @"CFStringGetBytes");
|
---|
[66] | 162 |
|
---|
| 163 | selStart = 0; selEnd = len;
|
---|
| 164 |
|
---|
[79] | 165 | Boolean needsSlashes;
|
---|
| 166 | ConstStringPtr hint = ICCF_GetHint(ICCF_GetInst(), *h, len, &selStart, &selEnd, &needsSlashes);
|
---|
[167] | 167 | NSCAssert(hint != NULL, @"Internal error: can't get protocol hint for URL");
|
---|
[74] | 168 |
|
---|
[79] | 169 | if (needsSlashes) {
|
---|
| 170 | ICCF_AddSlashes(h, hint);
|
---|
| 171 | len = selEnd = GetHandleSize(h);
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[106] | 174 | err = ICCF_DoURLAction(ICCF_GetInst(), hint, *h, selStart, selEnd, action);
|
---|
| 175 | ICCF_OSErrCAssert(err, @"ICCF_DoURLAction");
|
---|
[66] | 176 |
|
---|
| 177 | NS_HANDLER
|
---|
[79] | 178 | DisposeHandle(h);
|
---|
[272] | 179 | [urlString release];
|
---|
[66] | 180 | [localException raise];
|
---|
| 181 | NS_ENDHANDLER
|
---|
[272] | 182 |
|
---|
[183] | 183 | DisposeHandle(h);
|
---|
[272] | 184 | [urlString release];
|
---|
[183] | 185 |
|
---|
| 186 | return (err == noErr);
|
---|
[66] | 187 | }
|
---|
| 188 |
|
---|
[74] | 189 | // XXX not sure what to do if there's already a selection; BBEdit and MLTE extend it, Tex-Edit Plus doesn't.
|
---|
| 190 | Boolean ICCF_enabled = true;
|
---|
[66] | 191 |
|
---|
[388] | 192 | BOOL ICCF_HandleException(NSException *e, NSEvent *event) {
|
---|
[74] | 193 | if ([e reason] == nil || [[e reason] length] == 0)
|
---|
| 194 | return NO;
|
---|
| 195 |
|
---|
| 196 | if (ICCF_prefs.errorSoundEnabled) NSBeep();
|
---|
| 197 | if (!ICCF_prefs.errorDialogEnabled) return YES;
|
---|
| 198 |
|
---|
[388] | 199 | [[NSApplication sharedApplication] activateIgnoringOtherApps: YES];
|
---|
| 200 | [[event window] makeKeyAndOrderFront: nil];
|
---|
| 201 |
|
---|
[74] | 202 | int result = NSRunAlertPanel(ICCF_LocalizedString(@"AlertTitle"), ICCF_LocalizedString(@"AlertMessage%@"), nil, nil, ICCF_LocalizedString(@"AlertDisableButton"), e);
|
---|
[66] | 203 | if (result != NSAlertDefaultReturn) {
|
---|
[139] | 204 | result = NSRunAlertPanel(ICCF_LocalizedString(@"DisableAlertTitle"), ICCF_LocalizedString(@"DisableAlertMessage%@"), ICCF_LocalizedString(@"DisableAlertDisableButton"), ICCF_LocalizedString(@"DisableAlertDontDisableButton"), nil,
|
---|
| 205 | [(NSString *)ICCF_CopyAppName() autorelease]);
|
---|
[66] | 206 | if (result == NSAlertDefaultReturn)
|
---|
| 207 | ICCF_enabled = NO;
|
---|
| 208 | }
|
---|
[74] | 209 | return YES;
|
---|
[66] | 210 | }
|
---|
| 211 |
|
---|
[183] | 212 | void ICCF_LaunchURLFromTextView(NSTextView *self, NSEvent *triggeringEvent) {
|
---|
[66] | 213 | NSColor *insertionPointColor = [self insertionPointColor];
|
---|
| 214 |
|
---|
| 215 | NS_DURING
|
---|
| 216 |
|
---|
[388] | 217 | NSString *s = [[self textStorage] string]; // according to the class documentation, sending 'string' is guaranteed to be O(1)
|
---|
| 218 | unsigned length = [s length];
|
---|
[74] | 219 | NSCAssert(s != nil, ICCF_LocalizedString(@"Sorry, ICeCoffEE is unable to locate the insertion point or selection"));
|
---|
[388] | 220 | NSCAssert(length != 0, ICCF_LocalizedString(@"No text was found"));
|
---|
[66] | 221 |
|
---|
| 222 | ICCF_StartIC();
|
---|
| 223 |
|
---|
[388] | 224 | NSRange range = [self selectedRange];
|
---|
| 225 | NSCAssert(range.location != NSNotFound, ICCF_LocalizedString(@"There is no insertion point or selection in the text field where you clicked"));
|
---|
| 226 |
|
---|
| 227 | if (range.length == 0) {
|
---|
| 228 | if (range.location == length) range.location--;
|
---|
| 229 | range.length = 1;
|
---|
| 230 | range = ICCF_URLEnclosingRange(s, range);
|
---|
| 231 | [self setSelectedRange: range affinity: NSSelectionAffinityDownstream stillSelecting: NO];
|
---|
| 232 | }
|
---|
[66] | 233 |
|
---|
[183] | 234 | if (ICCF_LaunchURL([s substringWithRange: range], ICCF_KeyboardAction(triggeringEvent)) && ICCF_prefs.textBlinkEnabled) {
|
---|
[388] | 235 | for (unsigned i = 0 ; i < ICCF_prefs.textBlinkCount ; i++) {
|
---|
[74] | 236 | NSRange emptyRange = {range.location, 0};
|
---|
| 237 | [self setSelectedRange: emptyRange affinity: NSSelectionAffinityDownstream stillSelecting: YES];
|
---|
| 238 | [self display];
|
---|
| 239 | usleep(kICBlinkDelayUsecs);
|
---|
| 240 | [self setInsertionPointColor: [self backgroundColor]];
|
---|
| 241 | [self setSelectedRange: range affinity: NSSelectionAffinityDownstream stillSelecting: YES];
|
---|
| 242 | [self display];
|
---|
| 243 | usleep(kICBlinkDelayUsecs);
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[66] | 247 | NS_HANDLER
|
---|
[388] | 248 | ICCF_HandleException(localException, triggeringEvent);
|
---|
[66] | 249 | NS_ENDHANDLER
|
---|
| 250 |
|
---|
| 251 | ICCF_StopIC();
|
---|
| 252 | [self setInsertionPointColor: insertionPointColor];
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | NSString * const ICCF_SERVICES_ITEM = @"ICeCoffEE Services Item";
|
---|
| 256 |
|
---|
| 257 | NSMenuItem *ICCF_ServicesMenuItem() {
|
---|
| 258 | NSMenuItem *servicesItem;
|
---|
[182] | 259 | NSString *servicesTitle = nil;
|
---|
| 260 | NSMenu *servicesMenu = [NSApp servicesMenu];
|
---|
| 261 |
|
---|
| 262 | if (servicesMenu != nil) {
|
---|
| 263 | servicesTitle = [servicesMenu title];
|
---|
| 264 | if (servicesTitle == nil) {
|
---|
| 265 | ICLog(@"Can't get service menu title");
|
---|
| 266 | servicesTitle = @"Services";
|
---|
| 267 | }
|
---|
| 268 | } else {
|
---|
| 269 | servicesTitle = [[NSBundle bundleWithIdentifier: @"com.apple.AppKit"] localizedStringForKey: @"Services" value: nil table: @"ServicesMenu"];
|
---|
| 270 | if (servicesTitle == nil) {
|
---|
| 271 | ICLog(@"Can't get localized text for 'Services' in AppKit.framework");
|
---|
| 272 | servicesTitle = @"Services";
|
---|
| 273 | }
|
---|
[66] | 274 | }
|
---|
| 275 | servicesMenu = [[NSMenu alloc] initWithTitle: servicesTitle];
|
---|
| 276 | servicesItem = [[NSMenuItem alloc] initWithTitle: servicesTitle action:nil keyEquivalent:@""];
|
---|
[182] | 277 | ICCF_SetServicesMenu(servicesMenu);
|
---|
[66] | 278 | [servicesItem setSubmenu: servicesMenu];
|
---|
| 279 | [servicesItem setRepresentedObject: ICCF_SERVICES_ITEM];
|
---|
| 280 | [servicesMenu release];
|
---|
[139] | 281 | return [servicesItem autorelease];
|
---|
[66] | 282 | }
|
---|
| 283 |
|
---|
[139] | 284 | static const unichar UNICHAR_BLACK_RIGHT_POINTING_SMALL_TRIANGLE = 0x25b8;
|
---|
| 285 |
|
---|
| 286 | // returns YES if menu contains useful items, NO otherwise
|
---|
[320] | 287 | static BOOL ICCF_ConsolidateServicesMenu(NSMenu *menu, NSDictionary *serviceOptions, NSDictionary *serviceInfo) {
|
---|
[139] | 288 | [menu update]; // doesn't propagate to submenus, so we need to do this first
|
---|
| 289 | NSEnumerator *enumerator = [[menu itemArray] objectEnumerator];
|
---|
| 290 | NSMenuItem *menuItem;
|
---|
| 291 | NSMenu *submenu;
|
---|
[319] | 292 | NSDictionary *itemOptions = nil, *itemInfo = nil;
|
---|
[139] | 293 | BOOL shouldKeepItem = NO, shouldKeepMenu = NO;
|
---|
| 294 |
|
---|
| 295 | while ( (menuItem = [enumerator nextObject]) != nil) {
|
---|
[142] | 296 | if (serviceOptions != nil)
|
---|
| 297 | itemOptions = [serviceOptions objectForKey: [menuItem title]];
|
---|
[319] | 298 | if (serviceInfo != nil)
|
---|
| 299 | itemInfo = [serviceInfo objectForKey: [menuItem title]];
|
---|
[142] | 300 | if ([[itemOptions objectForKey: (NSString *)kICServiceHidden] boolValue]) {
|
---|
| 301 | shouldKeepItem = NO;
|
---|
| 302 | } else if ( (submenu = [menuItem submenu]) != nil) {
|
---|
[319] | 303 | // XXX don't rely on nil-sending working
|
---|
[320] | 304 | shouldKeepItem = ICCF_ConsolidateServicesMenu(submenu, [itemOptions objectForKey: (NSString *)kICServiceSubmenu], [itemInfo objectForKey: (NSString *)kICServiceSubmenu]);
|
---|
[139] | 305 | if (shouldKeepItem && [submenu numberOfItems] == 1) { // consolidate
|
---|
| 306 | NSMenuItem *serviceItem = [[submenu itemAtIndex: 0] retain];
|
---|
| 307 | [serviceItem setTitle:
|
---|
| 308 | [NSString stringWithFormat: @"%@ %@ %@", [menuItem title], [NSString stringWithCharacters: &UNICHAR_BLACK_RIGHT_POINTING_SMALL_TRIANGLE length: 1], [serviceItem title]]];
|
---|
[319] | 309 |
|
---|
[139] | 310 | int serviceIndex = [menu indexOfItem: menuItem];
|
---|
| 311 | [submenu removeItemAtIndex: 0]; // can't have item in two menus
|
---|
| 312 | [menu removeItemAtIndex: serviceIndex];
|
---|
| 313 | [menu insertItem: serviceItem atIndex: serviceIndex];
|
---|
| 314 | [serviceItem release];
|
---|
[319] | 315 | menuItem = serviceItem;
|
---|
[139] | 316 | }
|
---|
| 317 | } else {
|
---|
[182] | 318 | [menuItem setKeyEquivalent: @""];
|
---|
[139] | 319 | shouldKeepItem = [menuItem isEnabled];
|
---|
| 320 | }
|
---|
[319] | 321 | if (!shouldKeepItem) {
|
---|
[139] | 322 | [menu removeItem: menuItem];
|
---|
[319] | 323 | continue;
|
---|
[139] | 324 | }
|
---|
[319] | 325 | shouldKeepMenu = YES;
|
---|
| 326 |
|
---|
| 327 | if (itemInfo == nil) continue;
|
---|
| 328 | NSString *bundlePath = (NSString *)[itemInfo objectForKey: (NSString *)kICServiceBundlePath];
|
---|
| 329 | if (bundlePath == NULL) continue;
|
---|
| 330 | IconRef serviceIcon = ICCF_CopyIconRefForPath(bundlePath);
|
---|
| 331 | if (serviceIcon == NULL) continue;
|
---|
| 332 | [menuItem _setIconRef: serviceIcon];
|
---|
| 333 | ReleaseIconRef(serviceIcon);
|
---|
[139] | 334 | }
|
---|
| 335 |
|
---|
| 336 | return shouldKeepMenu;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | NSMenuItem *ICCF_ContextualServicesMenuItem() {
|
---|
| 340 | NSMenuItem *servicesItem = ICCF_ServicesMenuItem();
|
---|
[319] | 341 | NSDictionary *servicesInfo = ICCF_GetServicesInfo(); // XXX cache/retain
|
---|
| 342 | if (ICCF_ConsolidateServicesMenu([servicesItem submenu], (NSDictionary *)ICCF_prefs.serviceOptions, servicesInfo))
|
---|
[139] | 343 | return servicesItem;
|
---|
| 344 | else
|
---|
| 345 | return nil;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
[74] | 348 | void ICCF_AddRemoveServicesMenu() {
|
---|
| 349 | // needed because:
|
---|
| 350 | // (a) we get called before the runloop has properly started and will crash if we don't delay on app startup
|
---|
| 351 | // (b) the APE message handler calls us from another thread and nothing happens if we try to add a menu on it
|
---|
| 352 | [ICeCoffEE performSelectorOnMainThread: @selector(IC_addRemoveServicesMenu) withObject: nil waitUntilDone: NO];
|
---|
[66] | 353 | }
|
---|
| 354 |
|
---|
[167] | 355 | NSMenu *ICCF_MenuForEvent(NSView *self, NSMenu *contextMenu, NSEvent *e) {
|
---|
[66] | 356 | if (contextMenu != nil && [e type] == NSRightMouseDown || ([e type] == NSLeftMouseDown && [e modifierFlags] & NSControlKeyMask)) {
|
---|
| 357 | int servicesItemIndex = [contextMenu indexOfItemWithRepresentedObject: ICCF_SERVICES_ITEM];
|
---|
[139] | 358 | // always regenerate: make sure menu reflects context
|
---|
| 359 | if (servicesItemIndex != -1) {
|
---|
[74] | 360 | [contextMenu removeItemAtIndex: servicesItemIndex];
|
---|
| 361 | [contextMenu removeItemAtIndex: servicesItemIndex - 1];
|
---|
[66] | 362 | }
|
---|
[139] | 363 | if (ICCF_prefs.servicesInContextualMenu) {
|
---|
| 364 | NSMenuItem *contextualServicesItem = ICCF_ContextualServicesMenuItem();
|
---|
| 365 | if (contextualServicesItem != nil) {
|
---|
| 366 | [contextMenu addItem: [NSMenuItem separatorItem]];
|
---|
| 367 | [contextMenu addItem: contextualServicesItem];
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
[66] | 370 | }
|
---|
| 371 | return contextMenu;
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[183] | 374 | static NSEvent *ICCF_MouseDownEventWithModifierFlags(NSEvent *e, BOOL inheritModifierFlags) {
|
---|
| 375 | return [NSEvent mouseEventWithType: NSLeftMouseDown
|
---|
| 376 | location: [e locationInWindow]
|
---|
| 377 | modifierFlags: (inheritModifierFlags ? [e modifierFlags] : 0)
|
---|
| 378 | timestamp: [e timestamp]
|
---|
| 379 | windowNumber: [e windowNumber]
|
---|
| 380 | context: [e context]
|
---|
| 381 | eventNumber: [e eventNumber]
|
---|
| 382 | clickCount: 1
|
---|
| 383 | pressure: 0];
|
---|
| 384 | }
|
---|
[182] | 385 |
|
---|
[183] | 386 |
|
---|
[74] | 387 | @implementation ICeCoffEE
|
---|
| 388 |
|
---|
| 389 | + (void)IC_addRemoveServicesMenu;
|
---|
[66] | 390 | {
|
---|
| 391 | NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
|
---|
[74] | 392 | static NSMenuItem *servicesItem = nil;
|
---|
| 393 |
|
---|
| 394 | if (servicesItem == nil && ICCF_prefs.servicesInMenuBar) {
|
---|
| 395 | servicesItem = [ICCF_ServicesMenuItem() retain];
|
---|
[66] | 396 |
|
---|
[74] | 397 | int insertLoc = [mainMenu indexOfItemWithSubmenu: [NSApp windowsMenu]];
|
---|
| 398 | if (insertLoc == -1)
|
---|
| 399 | insertLoc = [mainMenu numberOfItems];
|
---|
[66] | 400 |
|
---|
[74] | 401 | [mainMenu insertItem: servicesItem atIndex: insertLoc];
|
---|
| 402 | } else if (servicesItem != nil && !ICCF_prefs.servicesInMenuBar) {
|
---|
| 403 | [mainMenu removeItem: servicesItem];
|
---|
| 404 | [servicesItem release];
|
---|
| 405 | servicesItem = nil;
|
---|
| 406 | }
|
---|
[66] | 407 | }
|
---|
| 408 |
|
---|
| 409 | // XXX localization?
|
---|
| 410 | - (NSMenu *)menuForEvent:(NSEvent *)e;
|
---|
| 411 | {
|
---|
| 412 | NSMenu *myMenu = [super menuForEvent: e];
|
---|
| 413 | return ICCF_MenuForEvent(self, myMenu, e);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[388] | 416 | - (void)mouseDown:(NSEvent *)downEvent;
|
---|
[66] | 417 | {
|
---|
| 418 | #if ICCF_DEBUG
|
---|
| 419 | static BOOL down = NO;
|
---|
| 420 | if (down) {
|
---|
| 421 | ICLog(@"recursive invocation!");
|
---|
| 422 | return;
|
---|
| 423 | }
|
---|
| 424 | down = YES;
|
---|
[388] | 425 | ICLog(@"ICeCoffEE down: %@", downEvent);
|
---|
[66] | 426 | #endif
|
---|
[388] | 427 | [ICeCoffEETrigger cancel];
|
---|
| 428 |
|
---|
| 429 | if (ICCF_enabled && ICCF_prefs.commandClickEnabled && ICCF_EventIsCommandMouseDown(downEvent)) {
|
---|
| 430 | [super mouseDown: ICCF_MouseDownEventWithModifierFlags(downEvent, YES)];
|
---|
[106] | 431 | // we don't actually get a mouseUp event, just wait for mouseDown to return
|
---|
[66] | 432 | NSEvent *upEvent = [[self window] currentEvent];
|
---|
[388] | 433 | NSPoint downPt = [downEvent locationInWindow];
|
---|
[66] | 434 | NSPoint upPt = [upEvent locationInWindow];
|
---|
| 435 | ICLog(@"next: %@", upEvent);
|
---|
| 436 | NSAssert([upEvent type] == NSLeftMouseUp, @"NSTextView mouseDown: did not return with current event as mouse up!");
|
---|
[74] | 437 | if (abs(downPt.x - upPt.x) <= kICHysteresisPixels && abs(downPt.y - upPt.y) <= kICHysteresisPixels) {
|
---|
[388] | 438 | // make sure we don't have a Command-double-click
|
---|
| 439 | [ICeCoffEETextViewTrigger setTriggerForEvent: downEvent onTarget: self]; // gets stored in ICCF_sharedTrigger; the reason for this weird calling pattern is that we don't want to add methods to NSTextView, and we don't want to add a method call on every mouseDown
|
---|
[66] | 440 | }
|
---|
[106] | 441 | } else {
|
---|
[388] | 442 | [super mouseDown: downEvent];
|
---|
[66] | 443 | }
|
---|
| 444 | #if ICCF_DEBUG
|
---|
| 445 | down = NO;
|
---|
| 446 | #endif
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | @end
|
---|