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

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

Allow loading in GC-enabled apps and implement finalization to prevent leaking ICeCoffEETriggers.

File size: 2.4 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- (id)initForEvent:(NSEvent *)anEvent onTarget:(NSView<NSTextInput> *)aTarget;
18{
19    if ( (self = [super init]) != nil) {
20        target = [aTarget retain];
21        event = [anEvent retain];
22        unsigned characterIndex = [aTarget characterIndexForPoint:
23                                   [[aTarget window] convertBaseToScreen: [anEvent locationInWindow]]];
24        range = [aTarget selectedRange];
25        ICLog(@"ICeCoffEETrigger characterIndex %u selectedRange %@", characterIndex, NSStringFromRange(range));
26        if (range.location == NSNotFound || range.length == 0 ||
27            !NSLocationInRange(characterIndex, range)) {
28            range.location = characterIndex;
29            range.length = 0;
30        }
31        // no typical definition of Command-Option-double-click, so we don't need to delay
32        NSTimeInterval interval = ([anEvent modifierFlags] & NSAlternateKeyMask) ? 0 :
33                                    TicksToEventTime(GetDblTime());
34        timer = [NSTimer scheduledTimerWithTimeInterval: interval
35                                                 target: self
36                                               selector: @selector(timerFired:)
37                                               userInfo: nil
38                                                repeats: NO];
39    }
40    return self;
41}
42
43+ (void)setTriggerForEvent:(NSEvent *)anEvent onTarget:(NSView<NSTextInput> *)aTarget;
44{
45    ICCF_sharedTrigger = [[self alloc] initForEvent: anEvent onTarget: aTarget];
46    [ICCF_sharedTrigger release];
47    ICLog(@"set %@", ICCF_sharedTrigger);
48}
49
50- (void)dealloc;
51{
52    ICLog(@"DEALLOC %@", self);
53    timer = nil;
54    [target release];
55    target = nil;
56    [event release];
57    event = nil;
58    [super dealloc];
59}
60
61- (void)finalize;
62{
63    ICLog(@"FINALIZE %@", self);
64    timer = nil;
65    target = nil;
66    event = nil;
67    [super finalize];
68}
69
70- (void)timerFired:(NSTimer *)aTimer;
71{
72    ICCF_sharedTrigger = nil;
73}
74
75- (void)cancel;
76{
77    ICLog(@"%@ cancelling", self);
78    ICCF_sharedTrigger = nil;
79    [timer invalidate]; // we get dealloced inside here, so don't do anything else
80}
81
82+ (BOOL)cancel;
83{
84    if (ICCF_sharedTrigger == nil)
85        return NO;
86   
87    [ICCF_sharedTrigger cancel];
88    return YES;
89}
90
91- (NSString *)description;
92{
93    return [NSString stringWithFormat: @"<%@ on %@ for %@ %@>", [self className],
94            target, event, NSStringFromRange(range)];
95}
96
97@end
Note: See TracBrowser for help on using the repository browser.