source: trunk/ICeCoffEE/ICeCoffEE/ICeCoffEETrigger.m @ 460

Last change on this file since 460 was 442, checked in by Nicholas Riley, 12 years ago

Move index-to-range conversion into a class method for launch methods that do not use a trigger

File size: 2.6 KB
Line 
1//
2//  ICeCoffEETrigger.m
3//  ICeCoffEE APE
4//
5//  Created by Nicholas Riley on 5/14/05.
6//  Copyright 2005 Nicholas Riley. All rights reserved.
7//
8
9#import "ICeCoffEETrigger.h"
10#import "ICeCoffEEShared.h"
11#include <Carbon/Carbon.h>
12
13static ICeCoffEETrigger *ICCF_sharedTrigger = nil;
14
15@implementation ICeCoffEETrigger
16
17+ (NSRange)rangeForEvent:(NSEvent *)anEvent onTarget:(NSView<NSTextInput> *)aTarget;
18{
19    unsigned characterIndex = [aTarget characterIndexForPoint:
20                               [[aTarget window] convertBaseToScreen: [anEvent locationInWindow]]];
21    NSRange range = [aTarget selectedRange];
22    ICLog(@"ICeCoffEETrigger characterIndex %u selectedRange %@", characterIndex, NSStringFromRange(range));
23    if (range.location == NSNotFound || range.length == 0 ||
24        !NSLocationInRange(characterIndex, range)) {
25        range.location = characterIndex;
26        range.length = 0;
27    }
28    return range;
29}
30
31- (id)initForEvent:(NSEvent *)anEvent onTarget:(NSView<NSTextInput> *)aTarget;
32{
33    if ( (self = [super init]) != nil) {
34        target = [aTarget retain];
35        event = [anEvent retain];
36        range = [[self class] rangeForEvent: anEvent onTarget: aTarget];
37        // no typical definition of Command-Option-double-click, so we don't need to delay
38        NSTimeInterval interval = ([anEvent modifierFlags] & NSAlternateKeyMask) ? 0 :
39                                    TicksToEventTime(GetDblTime());
40        timer = [NSTimer scheduledTimerWithTimeInterval: interval
41                                                 target: self
42                                               selector: @selector(timerFired:)
43                                               userInfo: nil
44                                                repeats: NO];
45    }
46    return self;
47}
48
49+ (void)setTriggerForEvent:(NSEvent *)anEvent onTarget:(NSView<NSTextInput> *)aTarget;
50{
51    ICCF_sharedTrigger = [[self alloc] initForEvent: anEvent onTarget: aTarget];
52    [ICCF_sharedTrigger release];
53    ICLog(@"set %@", ICCF_sharedTrigger);
54}
55
56- (void)dealloc;
57{
58    ICLog(@"DEALLOC %@", self);
59    timer = nil;
60    [target release];
61    target = nil;
62    [event release];
63    event = nil;
64    [super dealloc];
65}
66
67- (void)finalize;
68{
69    ICLog(@"FINALIZE %@", self);
70    timer = nil;
71    target = nil;
72    event = nil;
73    [super finalize];
74}
75
76- (void)timerFired:(NSTimer *)aTimer;
77{
78    ICCF_sharedTrigger = nil;
79}
80
81- (void)cancel;
82{
83    ICLog(@"%@ cancelling", self);
84    ICCF_sharedTrigger = nil;
85    [timer invalidate]; // we get dealloced inside here, so don't do anything else
86}
87
88+ (BOOL)cancel;
89{
90    if (ICCF_sharedTrigger == nil)
91        return NO;
92   
93    [ICCF_sharedTrigger cancel];
94    return YES;
95}
96
97- (NSString *)description;
98{
99    return [NSString stringWithFormat: @"<%@ on %@ for %@ %@>", [self className],
100            target, event, NSStringFromRange(range)];
101}
102
103@end
Note: See TracBrowser for help on using the repository browser.