Fairly large ITTSW checkin.
[ITKit.git] / ITTransientStatusWindow.m
1 #import "ITTransientStatusWindow.h"
2 #import "ITWindowEffect.h"
3 #import <CoreGraphics/CoreGraphics.h>
4 #import "ITCoreGraphicsHacks.h"
5 #import "ITTextField.h"
6 #import "ITGrayRoundedView.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 @end
23
24
25 /*************************************************************************/
26 #pragma mark -
27 #pragma mark IMPLEMENTATION
28 /*************************************************************************/
29
30 @implementation ITTransientStatusWindow
31
32
33 /*************************************************************************/
34 #pragma mark -
35 #pragma mark SHARED STATIC OBJECTS
36 /*************************************************************************/
37
38 static ITTransientStatusWindow *staticWindow = nil;
39
40
41 /*************************************************************************/
42 #pragma mark -
43 #pragma mark INITIALIZATION METHODS
44 /*************************************************************************/
45
46 + (ITTransientStatusWindow *)sharedWindow
47 {
48     if ( ! (staticWindow) ) {
49         staticWindow = [[self alloc] initWithContentView:nil
50                                                 exitMode:ITTransientStatusWindowExitAfterDelay
51                                           backgroundType:ITTransientStatusWindowRounded];
52     }
53     return staticWindow;
54 }
55
56 - (id)initWithContentView:(NSView *)contentView
57                  exitMode:(ITTransientStatusWindowExitMode)exitMode
58            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
59 {
60     NSRect contentRect;
61     
62     // If no Content View was provided, use a generic view.
63     if ( ! (contentView) ) {
64         contentView = [[[NSView alloc] initWithFrame:
65             NSMakeRect(100.0, 100.0, 200.0, 200.0)] autorelease];
66     }
67     
68     // Set the content rect to the frame of the content view, now guaranteed to exist.
69     contentRect = [contentView frame];
70     
71     if ( ( self = [super initWithContentRect:contentRect
72                                    styleMask:NSBorderlessWindowMask
73                                      backing:NSBackingStoreBuffered
74                                        defer:NO] ) ) {
75                                     
76         _visibilityState     = ITTransientStatusWindowHiddenState;
77         _exitMode            = exitMode;
78         _exitDelay           = DEFAULT_EXIT_DELAY;
79         _backgroundType      = backgroundType;
80         _verticalPosition    = ITWindowPositionBottom;
81         _horizontalPosition  = ITWindowPositionLeft;
82         _screenPadding       = 32.0;
83         _screenNumber        = 0;
84         _entryEffect         = nil;
85         _exitEffect          = nil;
86         _reallyIgnoresEvents = YES;
87         _delayTimer          = nil;
88
89 //      if ( _backgroundType == ITTransientStatusWindowRounded ) {
90 //          _contentSubView = contentView;
91 //      } else {
92 //          [self setContentView:contentView];
93 //      }
94
95         [self setIgnoresMouseEvents:YES];
96         [self setLevel:NSScreenSaverWindowLevel];
97         [self setContentView:contentView];
98         [self rebuildWindow];
99     }
100     return self;
101 }
102
103
104 /*************************************************************************/
105 #pragma mark -
106 #pragma mark INSTANCE METHODS
107 /*************************************************************************/
108
109 - (BOOL)ignoresMouseEvents
110 {
111     return _reallyIgnoresEvents;
112 }
113
114 - (void)setIgnoresMouseEvents:(BOOL)flag
115 {
116     CGSValueObj          key;
117     CGSValueObj          ignore;
118
119     key = CGSCreateCString("IgnoreForEvents");
120     ignore = CGSCreateBoolean( (flag ? kCGSTrue : kCGSFalse) );
121     CGSSetWindowProperty([NSApp contextID], (CGSWindowID)[self windowNumber], key, ignore);
122     CGSReleaseObj(key);
123     CGSReleaseObj(ignore);
124
125     _reallyIgnoresEvents = flag;
126 }
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 == ITTransientStatusWindowHiddenState ) {
158          // Window is hidden.  Appear as normal, and start the timer.
159         [_entryEffect performAppear];
160     } else if ( _visibilityState == ITTransientStatusWindowVisibleState ) {
161          // Window is completely visible.  Simply reset the timer.
162         [self startVanishTimer];
163     } else if ( _visibilityState == ITTransientStatusWindowAppearingState ) {
164          // Window is on its way in.  Do nothing.
165     } else if ( _visibilityState == ITTransientStatusWindowVanishingState ) {
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 == ITTransientStatusWindowVisibleState ) {
174         // Window is totally visible.  Perform exit effect.
175         [_exitEffect performVanish];
176     } else if ( _visibilityState == ITTransientStatusWindowHiddenState ) {
177         // Window is hidden.  Do nothing.
178     } else if ( _visibilityState == ITTransientStatusWindowAppearingState ) {
179         // Window is on its way in.  Cancel appear.
180         [_entryEffect cancelAppear];
181     } else if ( _visibilityState == ITTransientStatusWindowVanishingState ) {
182         // Window is on its way out.  Do nothing.
183     }
184 }
185
186 - (ITWindowVisibilityState)visibilityState
187 {
188     return _visibilityState;
189 }
190
191 - (void)setVisibilityState:(ITWindowVisibilityState)newState
192 {
193     _visibilityState = newState;
194     
195     if ( _visibilityState == ITTransientStatusWindowVisibleState ) {
196         [self startVanishTimer];
197     }
198 }
199
200 - (ITTransientStatusWindowExitMode)exitMode
201 {
202     return _exitMode;
203 }
204
205 - (void)setExitMode:(ITTransientStatusWindowExitMode)newMode
206 {
207     _exitMode = newMode;
208 }
209
210 - (float)exitDelay
211 {
212     return _exitDelay;
213 }
214
215 - (void)setExitDelay:(float)seconds
216 {
217     _exitDelay = seconds;
218 }
219
220 - (ITTransientStatusWindowBackgroundType)backgroundType
221 {
222 //  return _backgroundType;
223     return ITTransientStatusWindowRounded;
224 }
225
226 - (void)setBackgroundType:(ITTransientStatusWindowBackgroundType)newType
227 {
228 //  setBackgroundType: is currently ignored.  Defaults to ITTransientStatusWindowRounded.
229 //  _backgroundType = newType;
230     _backgroundType = ITTransientStatusWindowRounded;
231 }
232
233 - (ITVerticalWindowPosition)verticalPosition;
234 {
235     return _verticalPosition;
236 }
237
238 - (void)setVerticalPosition:(ITVerticalWindowPosition)newPosition;
239 {
240     _verticalPosition = newPosition;
241 }
242
243 - (ITHorizontalWindowPosition)horizontalPosition;
244 {
245     return _horizontalPosition;
246 }
247
248 - (void)setHorizontalPosition:(ITHorizontalWindowPosition)newPosition;
249 {
250     _horizontalPosition = newPosition;
251 }
252
253 - (float)effectProgress
254 {
255     return _effectProgress;
256 }
257
258 - (void)setEffectProgress:(float)newProgress
259 {
260     _effectProgress = newProgress;
261 }
262
263 - (float)screenPadding
264 {
265     return _screenPadding;
266 }
267
268 - (void)setScreenPadding:(float)newPadding
269 {
270     _screenPadding = newPadding;
271 }
272
273 - (int)screenNumber
274 {
275     return _screenNumber;
276 }
277
278 - (void)setScreenNumber:(int)newNumber
279 {
280     _screenNumber = newNumber;
281 }
282
283 - (ITWindowEffect *)entryEffect
284 {
285     return _entryEffect;
286 }
287
288 - (void)setEntryEffect:(ITWindowEffect *)newEffect
289 {
290     [_entryEffect autorelease];
291     _entryEffect = [newEffect retain];
292 }
293
294 - (ITWindowEffect *)exitEffect
295 {
296     return _exitEffect;
297 }
298
299 - (void)setExitEffect:(ITWindowEffect *)newEffect
300 {
301     [_exitEffect autorelease];
302     _exitEffect = [newEffect retain];
303 }
304
305
306 /*************************************************************************/
307 #pragma mark -
308 #pragma mark PRIVATE METHODS
309 /*************************************************************************/
310
311 - (void)rebuildWindow;
312 {
313     if ( _backgroundType == ITTransientStatusWindowRounded ) {
314         ITGrayRoundedView *roundedView = [[[ITGrayRoundedView alloc] initWithFrame:[self frame]] autorelease];
315
316         [self setBackgroundColor:[NSColor clearColor]];
317         [self setHasShadow:NO];
318         [self setOpaque:NO];
319         
320         [self setContentView:roundedView];
321 //      [super setContentView:roundedView];
322 //      [_contentSubView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
323 //      [self setContentView:_contentSubView];
324     } else {
325         // YUO == CLWONBAOT
326     }
327 }
328
329 - (void)startVanishTimer
330 {
331     // start timer, if appropriate
332     // if timer already exists, restart it.
333 }
334
335 @end