Removing an NSLog
[ITKit.git] / ITTransientStatusWindow.m
1 #import "ITTransientStatusWindow.h"
2 #import <CoreGraphics/CoreGraphics.h>
3 #import "ITCoreGraphicsHacks.h"
4 #import "ITTextField.h"
5 #import "ITGrayRoundedView.h"
6
7 @class ITTextField;
8 @class ITGrayRoundedView;
9
10 /*************************************************************************/
11 #pragma mark -
12 #pragma mark EVIL HACKERY
13 /*************************************************************************/
14
15 @interface NSApplication (HACKHACKHACKHACK)
16 - (CGSConnectionID)contextID;
17 @end
18
19
20 /*************************************************************************/
21 #pragma mark -
22 #pragma mark PRIVATE METHOD DECLARATIONS
23 /*************************************************************************/
24
25 @interface ITTransientStatusWindow (Private)
26 - (id)initWithContentView:(NSView *)contentView
27                  exitMode:(ITTransientStatusWindowExitMode)exitMode
28            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType;
29 - (void)rebuildWindow;
30 - (void)performEffect;
31 - (void)dissolveEffect:(BOOL)entering;
32 - (void)slideVerticalEffect:(BOOL)entering;
33 - (void)slideHorizontalEffect:(BOOL)entering;
34 @end
35
36
37 /*************************************************************************/
38 #pragma mark -
39 #pragma mark IMPLEMENTATION
40 /*************************************************************************/
41
42 @implementation ITTransientStatusWindow
43
44
45 /*************************************************************************/
46 #pragma mark -
47 #pragma mark SHARED STATIC OBJECTS
48 /*************************************************************************/
49
50 static ITTransientStatusWindow *staticWindow = nil;
51
52
53 /*************************************************************************/
54 #pragma mark -
55 #pragma mark INITIALIZATION METHODS
56 /*************************************************************************/
57
58 + (ITTransientStatusWindow *)sharedWindow
59 {
60     if ( ! (staticWindow) ) {
61         staticWindow = [[self alloc] initWithContentView:nil
62                                                 exitMode:ITTransientStatusWindowExitAfterDelay
63                                           backgroundType:ITTransientStatusWindowRounded];
64     }
65     return staticWindow;
66 }
67
68 - (id)initWithContentView:(NSView *)contentView
69                  exitMode:(ITTransientStatusWindowExitMode)exitMode
70            backgroundType:(ITTransientStatusWindowBackgroundType)backgroundType
71 {
72     NSRect contentRect;
73     
74     // If no Content View was provided, use a generic NSView with the app icon.
75     if ( ! (contentView) ) {
76         contentView = [[[NSView alloc] initWithFrame:
77             NSMakeRect(100.0, 100.0, 200.0, 200.0)] autorelease];
78     }
79     
80     // Set the content rect to the frame of the content view, now guaranteed to exist.
81     contentRect = [contentView frame];
82     
83     if ( ( self = [super initWithContentRect:contentRect
84                                    styleMask:NSBorderlessWindowMask
85                                      backing:NSBackingStoreBuffered
86                                        defer:NO] ) ) {
87                                     
88         _visibilityState     = ITTransientStatusWindowHiddenState;
89         _exitMode            = exitMode;
90         _exitDelay           = DEFAULT_EXIT_DELAY;
91         _backgroundType      = backgroundType;
92         _verticalPosition    = ITTransientStatusWindowPositionBottom;
93         _horizontalPosition  = ITTransientStatusWindowPositionLeft;
94         _entryEffect         = ITTransientStatusWindowEffectNone;
95         _exitEffect          = ITTransientStatusWindowEffectDissolve;
96         _effectTime          = DEFAULT_EFFECT_TIME;
97         _reallyIgnoresEvents = YES;
98         _delayTimer = nil;
99         _fadeTimer  = nil;
100
101 //        if ( _backgroundType == ITTransientStatusWindowRounded ) {
102 //            _contentSubView = contentView;
103 //        } else {
104 //            [self setContentView:contentView];
105 //        }
106
107         [self setIgnoresMouseEvents:YES];
108         [self setLevel:NSScreenSaverWindowLevel];
109         [self setContentView:contentView];
110         [self rebuildWindow];
111     }
112     return self;
113 }
114
115
116 /*************************************************************************/
117 #pragma mark -
118 #pragma mark INSTANCE METHODS
119 /*************************************************************************/
120
121 - (void)setRotation:(float)angle
122 {
123     CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
124     transform.tx = -32.0;
125     transform.ty = [self frame].size.height + 32.0;
126     CGSSetWindowTransform([NSApp contextID],
127                           (CGSWindowID)[self windowNumber],
128                           CGAffineTransformTranslate(transform,
129                                                      (([self frame].origin.x - 32.0) * -1),
130                                                      (([[self screen] frame].size.height - ([self frame].origin.y) + 32.0) * -1) ));
131 }
132
133 - (BOOL)ignoresMouseEvents
134 {
135     return _reallyIgnoresEvents;
136 }
137
138 - (void)setIgnoresMouseEvents:(BOOL)flag
139 {
140     CGSValueObj          key;
141     CGSValueObj          ignore;
142
143     key = CGSCreateCString("IgnoreForEvents");
144     ignore = CGSCreateBoolean( (flag ? kCGSTrue : kCGSFalse) );
145     CGSSetWindowProperty([NSApp contextID], (CGSWindowID)[self windowNumber], key, ignore);
146     CGSReleaseObj(key);
147     CGSReleaseObj(ignore);
148
149     _reallyIgnoresEvents = flag;
150 }
151
152 - (void)orderFront:(id)sender
153 {
154     if ( _exitMode == ITTransientStatusWindowExitAfterDelay ) {
155         // set the timer, and orderOut: when it lapses.
156     }
157
158     if ( _entryEffect == ITTransientStatusWindowEffectNone ) {
159         [super orderFront:sender];
160     } else {
161         [self performEffect];
162     }
163 }
164
165 - (void)makeKeyAndOrderFront:(id)sender
166 {
167     if ( _exitMode == ITTransientStatusWindowExitAfterDelay ) {
168         // set the timer, and orderOut: when it lapses.
169     }
170
171     if ( _entryEffect == ITTransientStatusWindowEffectNone ) {
172         [super makeKeyAndOrderFront:sender];
173     } else {
174         [self performEffect];
175         [self makeKeyWindow];
176     }
177 }
178
179 - (void)orderOut:(id)sender
180 {
181     if ( _entryEffect == ITTransientStatusWindowEffectNone ) {
182         [super orderOut:sender];
183     } else {
184         [self performEffect];
185     }
186 }
187
188 - (NSTimeInterval)animationResizeTime:(NSRect)newFrame
189 {
190     return _effectTime;
191 }
192
193 /*
194
195 - (id)contentView
196 {
197     if ( _backgroundType == ITTransientStatusWindowRounded ) {
198         return _contentSubView;
199     } else {
200         return [super contentView];
201     }
202 }
203
204 - (void)setContentView:(NSView *)aView
205 {
206     if ( _backgroundType == ITTransientStatusWindowRounded ) {
207        [_contentSubView removeFromSuperview];
208         _contentSubView = aView;
209         [_contentSubView setFrame:[[super contentView] frame]];
210         [[super contentView] addSubview:_contentSubView];
211         [_contentSubView setNextResponder:self];
212     } else {
213         [super setContentView:aView];
214     }
215 }
216
217 */
218
219 - (ITTransientStatusWindowVisibilityState)visibilityState
220 {
221     return _visibilityState;
222 }
223
224 - (ITTransientStatusWindowExitMode)exitMode
225 {
226     return _exitMode;
227 }
228
229 - (void)setExitMode:(ITTransientStatusWindowExitMode)newMode
230 {
231     _exitMode = newMode;
232 }
233
234 - (float)exitDelay
235 {
236     return _exitDelay;
237 }
238
239 - (void)setExitDelay:(float)seconds
240 {
241     _exitDelay = seconds;
242 }
243
244 - (ITTransientStatusWindowBackgroundType)backgroundType
245 {
246 //  return _backgroundType;
247     return ITTransientStatusWindowRounded;
248 }
249
250 - (void)setBackgroundType:(ITTransientStatusWindowBackgroundType)newType
251 {
252 //  setBackgroundType: is currently ignored.  Defaults to ITTransientStatusWindowRounded.
253 //  _backgroundType = newType;
254     _backgroundType = ITTransientStatusWindowRounded;
255 }
256
257 - (ITTransientStatusWindowPosition)verticalPosition;
258 {
259     return _verticalPosition;
260 }
261
262 - (void)setVerticalPosition:(ITTransientStatusWindowPosition)newPosition;
263 {
264     _verticalPosition = newPosition;
265 }
266
267 - (ITTransientStatusWindowPosition)horizontalPosition;
268 {
269     return _horizontalPosition;
270 }
271
272 - (void)setHorizontalPosition:(ITTransientStatusWindowPosition)newPosition;
273 {
274     _horizontalPosition = newPosition;
275 }
276
277 - (ITTransientStatusWindowEffect)entryEffect
278 {
279     return _entryEffect;
280 }
281
282 - (void)setEntryEffect:(ITTransientStatusWindowEffect)newEffect;
283 {
284     _entryEffect = newEffect;
285 }
286
287 - (ITTransientStatusWindowEffect)exitEffect;
288 {
289     return _exitEffect;
290 }
291
292 - (void)setExitEffect:(ITTransientStatusWindowEffect)newEffect;
293 {
294     _exitEffect = newEffect;
295 }
296
297
298 /*************************************************************************/
299 #pragma mark -
300 #pragma mark PRIVATE METHODS
301 /*************************************************************************/
302
303 - (void)rebuildWindow;
304 {
305     if ( _backgroundType == ITTransientStatusWindowRounded ) {
306         ITGrayRoundedView *roundedView = [[[ITGrayRoundedView alloc] initWithFrame:[self frame]] autorelease];
307
308         [self setBackgroundColor:[NSColor clearColor]];
309         [self setHasShadow:NO];
310         [self setOpaque:NO];
311         
312         [self setContentView:roundedView];
313 //      [super setContentView:roundedView];
314 //      [_contentSubView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
315 //      [self setContentView:_contentSubView];
316     } else {
317         // YUO == CLWONBAOT
318     }
319 }
320
321 - (void)performEffect
322 {
323     if ( _visibilityState == ITTransientStatusWindowHiddenState ) {
324         if ( _entryEffect == ITTransientStatusWindowEffectDissolve ) {
325             [self dissolveEffect:YES];
326         } else if ( _entryEffect == ITTransientStatusWindowEffectSlideVertically ) {
327             [self slideVerticalEffect:YES];
328         } else if ( _entryEffect == ITTransientStatusWindowEffectSlideHorizontally ) {
329             [self slideHorizontalEffect:YES];
330         }
331     } else if ( _visibilityState == ITTransientStatusWindowVisibleState ) {
332         if ( _exitEffect == ITTransientStatusWindowEffectDissolve ) {
333             [self dissolveEffect:NO];
334         } else if ( _exitEffect == ITTransientStatusWindowEffectSlideVertically ) {
335             [self slideVerticalEffect:NO];
336         } else if ( _exitEffect == ITTransientStatusWindowEffectSlideHorizontally ) {
337             [self slideHorizontalEffect:NO];
338         }
339     }
340 }
341
342 - (void)dissolveEffect:(BOOL)entering
343 {
344     if ( entering ) {
345         [super orderFront:self];
346     } else {
347         [super orderOut:self];
348     }
349 }
350
351 - (void)slideVerticalEffect:(BOOL)entering
352 {
353     if ( entering ) {
354         [super orderFront:self];
355     } else {
356         [super orderOut:self];
357     }
358 }
359
360 - (void)slideHorizontalEffect:(BOOL)entering
361 {
362     if ( entering ) {
363         [super orderFront:self];
364     } else {
365         [super orderOut:self];
366     }
367 }
368
369
370 @end