Hackery for NSTabViewItem
[ITKit.git] / ITChasingArrowsView.m
1 //
2 //  ITChasingArrowsView.m
3 //  
4 //
5 //  Created by Doug Brown on Sat May 11 2002.
6 //  Copyright (c) 2002 iThink Software. All rights reserved.
7 //
8
9 #import "ITChasingArrowsView.h"
10
11
12 @implementation ITChasingArrowsView
13
14 - (id)initWithCoder:(NSCoder *)decoder
15 {
16     self = [super initWithCoder:decoder];
17
18     running = NO;
19
20     images = [[decoder decodeObject] retain];
21
22     curIndex = 0;
23     timer = nil;
24     
25     return self;
26 }
27
28 - (void)encodeWithCoder:(NSCoder *)coder
29 {
30     [super encodeWithCoder:coder];
31     
32     [coder encodeObject:images];
33 }
34
35 - (id)initWithFrame:(NSRect)frame {
36     self = [super initWithFrame:frame];
37     if (self) {
38         NSBundle *bund = [NSBundle bundleForClass:[self class]];
39         running = NO;
40         images = [[NSArray alloc] initWithObjects:
41             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow1.tiff"]] autorelease],
42             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow2.tiff"]] autorelease],
43             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow3.tiff"]] autorelease],
44             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow4.tiff"]] autorelease],
45             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow5.tiff"]] autorelease],
46             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow6.tiff"]] autorelease],
47             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow7.tiff"]] autorelease],
48             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow8.tiff"]] autorelease],
49             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow9.tiff"]] autorelease],
50             [[[NSImage alloc] initWithContentsOfFile:[bund pathForImageResource:@"ITChasingArrow10.tiff"]] autorelease],
51             nil];
52         
53         curIndex = 0;
54         timer = nil;
55     }
56     return self;
57 }
58
59 - (void)dealloc
60 {
61     if (timer)
62     {
63         [timer invalidate];
64         [timer release];
65         timer = nil;
66     }
67     [images release];
68     
69     [super dealloc];
70 }
71 - (void)drawRect:(NSRect)rect {
72
73     if (running)
74     {
75         NSImage *curImage = [images objectAtIndex:curIndex];
76         float amt;
77         if (inForeground)
78         {
79             amt = 1.0;
80         }
81         else
82         {
83             amt = 0.5;
84         }
85         [curImage compositeToPoint:NSMakePoint(0,0) operation:NSCompositeSourceOver fraction:amt];
86     }
87     else
88     {
89         // draw nothing.
90     }
91 }
92
93 - (IBAction)stop:(id)sender
94 {
95     running = NO;
96
97     if (timer)
98     {
99         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
100         [nc removeObserver:self];
101         [timer invalidate];
102         [timer release];
103         timer = nil;
104     }
105     
106     [self setNeedsDisplay:YES];
107 }
108     
109 - (IBAction)start:(id)sender
110 {
111     if (!timer)
112     {
113         NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
114         [nc addObserver:self selector:@selector(appWentToBackground:) name:NSApplicationWillResignActiveNotification object:nil];
115         [nc addObserver:self selector:@selector(appWentToForeground:) name:NSApplicationWillBecomeActiveNotification object:nil];
116         [nc addObserver:self selector:@selector(windowWentToBackground:) name:NSWindowDidResignMainNotification object:nil];
117         [nc addObserver:self selector:@selector(windowWentToForeground:) name:NSWindowDidBecomeMainNotification object:nil];
118         inForeground = ([NSApp isActive] && [[self window] isMainWindow]);
119         curIndex = 0;
120         running = YES;
121         timer = [[NSTimer scheduledTimerWithTimeInterval:0.05
122                                                   target:self
123                                                 selector:@selector(showNewImage:)
124                                                 userInfo:nil
125                                                  repeats:YES] retain];
126         [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSEventTrackingRunLoopMode];
127     }
128 }
129
130 - (void)showNewImage:(NSTimer *)t
131 {
132     [self setNeedsDisplay:YES];
133
134     if (curIndex == 9)
135     {
136         curIndex = 0;
137     }
138     else
139     {
140         curIndex++;
141     }
142 }
143
144 - (void)appWentToBackground:(NSNotification *)note
145 {
146     inForeground = NO;
147 }
148
149 - (void)appWentToForeground:(NSNotification *)note
150 {
151     inForeground = YES;
152 }
153
154 - (void)windowWentToBackground:(NSNotification *)note
155 {
156     NSWindow *window = [note object];
157     if (window == [self window])
158     {
159         inForeground = NO;
160     }
161 }
162
163 - (void)windowWentToForeground:(NSNotification *)note
164 {
165     NSWindow *window = [note object];
166     if (window == [self window])
167     {
168         inForeground = YES;
169     }
170 }
171
172 @end