Fixed a possible crasher where an ITMultilineTextFieldCell is treated as
[ITKit.git] / ITTransientStatusWindow.m
1 #import "ITTransientStatusWindow.h"
2 #import "ITWindowEffect.h"
3 #import <ApplicationServices/ApplicationServices.h>
4 #import "ITCoreGraphicsHacks.h"
5 #import "ITTextField.h"
6 #import "ITTSWBackgroundView.h"
7
8 #define EFFECT_FPS 30.0
9
10
11 /*************************************************************************/
12 #pragma mark -
13 #pragma mark PRIVATE METHOD DECLARATIONS
14 /*************************************************************************/
15
16 @interface ITTransientStatusWindow (Private)
17 - (id)initWithContentView:(NSView *)contentView
18                  exitMode:(ITTransientStatusWindowExitMode)exitMode
19            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType;
20 - (void)rebuildWindow;
21 - (void)startVanishTimer;
22 - (void)stopVanishTimer;
23 @end
24
25
26 /*************************************************************************/
27 #pragma mark -
28 #pragma mark IMPLEMENTATION
29 /*************************************************************************/
30
31 @implementation ITTransientStatusWindow
32
33
34 /*************************************************************************/
35 #pragma mark -
36 #pragma mark SHARED STATIC OBJECTS
37 /*************************************************************************/
38
39 static ITTransientStatusWindow *staticWindow = nil;
40
41
42 /*************************************************************************/
43 #pragma mark -
44 #pragma mark INITIALIZATION METHODS
45 /*************************************************************************/
46
47 + (ITTransientStatusWindow *)sharedWindow
48 {
49     if ( ! (staticWindow) ) {
50         staticWindow = [[self alloc] initWithContentView:nil
51                                                 exitMode:ITTransientStatusWindowExitAfterDelay
52                                           backgroundType:ITTransientStatusWindowRounded];
53     }
54     return staticWindow;
55 }
56
57 - (id)initWithContentView:(NSView *)contentView
58                  exitMode:(ITTransientStatusWindowExitMode)exitMode
59            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
60 {
61     NSRect contentRect;
62     
63     // If no Content View was provided, use a generic view.
64     if ( ! (contentView) ) {
65         contentView = [[[NSView alloc] initWithFrame:
66             NSMakeRect(100.0, 100.0, 200.0, 200.0)] autorelease];
67     }
68     
69     // Set the content rect to the frame of the content view, now guaranteed to exist.
70     contentRect = [contentView frame];
71     
72     if ( ( self = [super initWithContentRect:contentRect
73                                    styleMask:NSBorderlessWindowMask
74                                      backing:NSBackingStoreBuffered
75                                        defer:NO] ) ) {
76                                     
77         _visibilityState     = ITWindowHiddenState;
78         _exitMode            = exitMode;
79         _exitDelay           = DEFAULT_EXIT_DELAY;
80         _backgroundType      = backgroundType;
81         _verticalPosition    = ITWindowPositionBottom;
82         _horizontalPosition  = ITWindowPositionLeft;
83         _screenPadding       = 32.0;
84         //_screenNumber        = 0;
85         _entryEffect         = nil;
86         _exitEffect          = nil;
87         _reallyIgnoresEvents = YES;
88         _exitTimer           = nil;
89
90 //      if ( _backgroundType == ITTransientStatusWindowRounded ) {
91 //          _contentSubView = contentView;
92 //      } else {
93 //          [self setContentView:contentView];
94 //      }
95
96         [self setIgnoresMouseEvents:YES];
97         [self setLevel:NSScreenSaverWindowLevel];
98         [self setContentView:contentView];
99         [self rebuildWindow];
100     }
101     return self;
102 }
103
104
105 /*************************************************************************/
106 #pragma mark -
107 #pragma mark INSTANCE METHODS
108 /*************************************************************************/
109
110 - (BOOL)ignoresMouseEvents
111 {
112     return _reallyIgnoresEvents;
113 }
114
115 - (void)setIgnoresMouseEvents:(BOOL)flag
116 {
117     CGSValueObj          key;
118     CGSValueObj          ignore;
119
120     key = CGSCreateCString("IgnoreForEvents");
121     ignore = CGSCreateBoolean( (flag ? kCGSTrue : kCGSFalse) );
122     CGSSetWindowProperty([NSApp contextID], (CGSWindowID)[self windowNumber], key, ignore);
123     CGSReleaseObj(key);
124     CGSReleaseObj(ignore);
125
126     _reallyIgnoresEvents = flag;
127 }
128
129 /*
130
131 - (id)contentView
132 {
133     if ( _backgroundType == ITTransientStatusWindowRounded ) {
134         return _contentSubView;
135     } else {
136         return [super contentView];
137     }
138 }
139
140 - (void)setContentView:(NSView *)aView
141 {
142     if ( _backgroundType == ITTransientStatusWindowRounded ) {
143        [_contentSubView removeFromSuperview];
144         _contentSubView = aView;
145         [_contentSubView setFrame:[[super contentView] frame]];
146         [[super contentView] addSubview:_contentSubView];
147         [_contentSubView setNextResponder:self];
148     } else {
149         [super setContentView:aView];
150     }
151 }
152
153 */
154
155 - (IBAction)appear:(id)sender
156 {
157     if ( _visibilityState == ITWindowHiddenState ) {
158          // Window is hidden.  Appear as normal, and start the timer.
159         [_entryEffect performAppear];
160     } else if ( _visibilityState == ITWindowVisibleState ) {
161          // Window is completely visible.  Simply reset the timer.
162         [self startVanishTimer];
163     } else if ( _visibilityState == ITWindowAppearingState ) {
164          // Window is on its way in.  Do nothing.
165     } else if ( _visibilityState == ITWindowVanishingState ) {
166         // Window is on its way out.  Cancel the vanish.
167         [_exitEffect cancelVanish];
168     }
169 }
170
171 - (IBAction)vanish:(id)sender
172 {
173     if ( _visibilityState == ITWindowVisibleState ) {
174         // Window is totally visible.  Perform exit effect.
175         [_exitEffect performVanish];
176     } else if ( _visibilityState == ITWindowHiddenState ) {
177         // Window is hidden.  Do nothing.
178     } else if ( _visibilityState == ITWindowAppearingState ) {
179         // Window is on its way in.  Cancel appear.
180         [_entryEffect cancelAppear];
181     } else if ( _visibilityState == ITWindowVanishingState ) {
182         // Window is on its way out.  Do nothing.
183     }
184 }
185
186 - (void)setSizing:(ITTransientStatusWindowSizing)newSizing
187 {
188     _sizing = newSizing;
189 }
190
191 - (ITTransientStatusWindowSizing)sizing
192 {
193     return _sizing;
194 }
195
196 - (ITWindowVisibilityState)visibilityState
197 {
198     return _visibilityState;
199 }
200
201 - (void)setVisibilityState:(ITWindowVisibilityState)newState
202 {
203     _visibilityState = newState;
204     
205     if ( _visibilityState == ITWindowVisibleState ) {
206         [self startVanishTimer];
207     } else if ( (_visibilityState == ITWindowVanishingState) || (_visibilityState == ITWindowHiddenState) ) {
208         [self stopVanishTimer];
209     }
210 }
211
212 - (ITTransientStatusWindowExitMode)exitMode
213 {
214     return _exitMode;
215 }
216
217 - (void)setExitMode:(ITTransientStatusWindowExitMode)newMode
218 {
219     _exitMode = newMode;
220     
221     if ( _visibilityState == ITWindowVisibleState ) {
222         if ( _exitMode == ITTransientStatusWindowExitOnCommand ) {
223             [self stopVanishTimer];
224         } else if ( _exitMode == ITTransientStatusWindowExitAfterDelay ) {
225             [self startVanishTimer];
226         }
227     }
228 }
229
230 - (float)exitDelay
231 {
232     return _exitDelay;
233 }
234
235 - (void)setExitDelay:(float)seconds
236 {
237     _exitDelay = seconds;
238 }
239
240 - (ITTransientStatusWindowBackgroundType)backgroundType
241 {
242 //  return _backgroundType;
243     return ITTransientStatusWindowRounded;
244 }
245
246 - (void)setBackgroundType:(ITTransientStatusWindowBackgroundType)newType
247 {
248 //  setBackgroundType: is currently ignored.  Defaults to ITTransientStatusWindowRounded.
249 //  _backgroundType = newType;
250     _backgroundType = ITTransientStatusWindowRounded;
251 }
252
253 - (ITVerticalWindowPosition)verticalPosition;
254 {
255     return _verticalPosition;
256 }
257
258 - (void)setVerticalPosition:(ITVerticalWindowPosition)newPosition;
259 {
260     _verticalPosition = newPosition;
261 }
262
263 - (ITHorizontalWindowPosition)horizontalPosition;
264 {
265     return _horizontalPosition;
266 }
267
268 - (void)setHorizontalPosition:(ITHorizontalWindowPosition)newPosition;
269 {
270     _horizontalPosition = newPosition;
271 }
272
273 - (float)effectProgress
274 {
275     return _effectProgress;
276 }
277
278 - (void)setEffectProgress:(float)newProgress
279 {
280     _effectProgress = newProgress;
281 }
282
283 - (float)screenPadding
284 {
285     return _screenPadding;
286 }
287
288 - (void)setScreenPadding:(float)newPadding
289 {
290     _screenPadding = newPadding;
291 }
292
293 /*- (int)screenNumber
294 {
295     return _screenNumber;
296 }
297
298 - (void)setScreenNumber:(int)newNumber
299 {
300     _screenNumber = newNumber;
301 }*/
302
303 - (ITWindowEffect *)entryEffect
304 {
305     return _entryEffect;
306 }
307
308 - (void)setEntryEffect:(ITWindowEffect *)newEffect
309 {
310     [_entryEffect releaseWhenIdle];
311     _entryEffect = [newEffect retain];
312 }
313
314 - (ITWindowEffect *)exitEffect
315 {
316     return _exitEffect;
317 }
318
319 - (void)setExitEffect:(ITWindowEffect *)newEffect
320 {
321     [_exitEffect releaseWhenIdle];
322     _exitEffect = [newEffect retain];
323 }
324
325
326 /*************************************************************************/
327 #pragma mark -
328 #pragma mark PRIVATE METHODS
329 /*************************************************************************/
330
331 - (void)rebuildWindow;
332 {
333     if ( _backgroundType == ITTransientStatusWindowRounded ) {
334         ITTSWBackgroundView *roundedView = [[[ITTSWBackgroundView alloc] initWithFrame:[self frame]] autorelease];
335
336         [self setBackgroundColor:[NSColor clearColor]];
337         [self setHasShadow:NO];
338         [self setOpaque:NO];
339         
340         [self setContentView:roundedView];
341 //      [super setContentView:roundedView];
342 //      [_contentSubView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
343 //      [self setContentView:_contentSubView];
344     } else {
345         // YUO == CLWONBAOT
346     }
347 }
348
349 - (void)startVanishTimer
350 {
351     if ( _exitMode == ITTransientStatusWindowExitAfterDelay) {
352         [self stopVanishTimer];
353         _exitTimer = [NSTimer scheduledTimerWithTimeInterval:_exitDelay
354                                                       target:self
355                                                     selector:@selector(doDelayedExit)
356                                                     userInfo:nil
357                                                      repeats:NO];
358     }
359 }
360
361 - (void)doDelayedExit
362 {
363     [self vanish:self];
364     _exitTimer = nil;
365 }
366
367 - (void)stopVanishTimer
368 {
369     if ( _exitTimer ) {
370         [_exitTimer invalidate];
371         _exitTimer = nil;
372     }
373 }
374
375 @end