Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /XspfQT/XspfQTMovieWindowController.m

Parent Directory Parent Directory | Revision Log Revision Log


Revision 135 - (hide annotations) (download)
Thu Feb 5 15:18:33 2009 UTC (15 years, 2 months ago) by masaki
File size: 13528 byte(s)
経過時間更新用タイマーの更新頻度を上げた。
停止時はタイマーを停止するようにした。
1 masaki 2 //
2 masaki 72 // XspfQTMovieWindowController.m
3 masaki 2 // XspfQT
4     //
5     // Created by Hori,Masaki on 08/08/31.
6     // Copyright 2008 masakih. All rights reserved.
7     //
8    
9 masaki 72 #import "XspfQTMovieWindowController.h"
10     #import "XspfQTDocument.h"
11     #import "XspfQTComponent.h"
12     #import "XspfQTFullScreenWindow.h"
13 masaki 2
14    
15 masaki 72 @interface XspfQTMovieWindowController (Private)
16 masaki 45 - (NSSize)windowSizeWithoutQTView;
17 masaki 17 - (void)sizeTofitWidnow;
18 masaki 2 - (NSSize)fitSizeToSize:(NSSize)toSize;
19     - (NSWindow *)fullscreenWindow;
20 masaki 135 - (void)startedMovie;
21     - (void)stopedMovie;
22 masaki 2 @end
23    
24 masaki 72 @implementation XspfQTMovieWindowController
25 masaki 2
26 masaki 10 #pragma mark ### Static variables ###
27 masaki 132 static const float sVolumeDelta = 0.1;
28 masaki 135 static const NSTimeInterval sUpdateTimeInterval = 0.01;
29 masaki 38 static NSString *const kQTMovieKeyPath = @"trackList.qtMovie";
30 masaki 40 static NSString *const kIsPlayedKeyPath = @"trackList.isPlayed";
31 masaki 135 static NSString *const kVolumeKeyPath = @"qtMovie.volume";
32 masaki 2
33 masaki 10 - (id)init
34 masaki 2 {
35 masaki 73 if(self = [super initWithWindowNibName:@"XspfQTDocument"]) {
36 masaki 135 // updateTime = [NSTimer scheduledTimerWithTimeInterval:sUpdateTimeInterval
37     // target:self
38     // selector:@selector(updateTimeIfNeeded:)
39     // userInfo:NULL
40     // repeats:YES];
41 masaki 2 }
42    
43     return self;
44     }
45    
46     - (void)dealloc
47     {
48     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
49     [nc removeObserver:self];
50 masaki 49
51     [self setQtMovie:nil];
52 masaki 2
53     [fullscreenWindow release];
54 masaki 135 [self stopedMovie];
55 masaki 10 [prevMouseMovedDate release];
56 masaki 76
57 masaki 2 [super dealloc];
58     }
59     - (void)awakeFromNib
60     {
61 masaki 3 prevMouseMovedDate = [[NSDate dateWithTimeIntervalSinceNow:0.0] retain];
62    
63 masaki 100 id doc = [self document];
64     // NSLog(@"Add Observed! %@", doc);
65     [doc addObserver:self
66     forKeyPath:kQTMovieKeyPath
67     options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
68     context:NULL];
69     [doc addObserver:self
70     forKeyPath:kIsPlayedKeyPath
71     options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
72     context:NULL];
73 masaki 2
74 masaki 100 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
75     [nc addObserver:self
76     selector:@selector(documentWillClose:)
77     name:XspfQTDocumentWillCloseNotification
78     object:doc];
79    
80 masaki 66 [self setValue:[NSNumber numberWithInt:0]
81     forKeyPath:@"document.trackList.selectionIndex"];
82 masaki 17 [self sizeTofitWidnow];
83 masaki 2 [self play];
84     }
85 masaki 10
86 masaki 62 - (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName
87     {
88 masaki 70 id title1 = [self valueForKeyPath:@"document.trackList.title"];
89     id title2 = [self valueForKeyPath:@"document.trackList.currentTrack.title"];
90     if(title1 && title2) {
91 masaki 62 return [NSString stringWithFormat:@"%@ - %@",
92 masaki 70 title1, title2];
93 masaki 62 }
94 masaki 116 if(title2) {
95     return [NSString stringWithFormat:@"%@ - %@",
96     displayName, title2];
97     }
98 masaki 62 return displayName;
99     }
100    
101 masaki 10 #pragma mark ### KVO & KVC ###
102 masaki 2 - (void)observeValueForKeyPath:(NSString *)keyPath
103     ofObject:(id)object
104     change:(NSDictionary *)change
105     context:(void *)context
106     {
107     // NSLog(@"Observed!");
108 masaki 49 if([keyPath isEqualToString:kQTMovieKeyPath]) {
109 masaki 2 id new = [change objectForKey:NSKeyValueChangeNewKey];
110 masaki 49 [self setQtMovie:new];
111 masaki 2 return;
112     }
113 masaki 49 if([keyPath isEqualToString:kIsPlayedKeyPath]) {
114 masaki 14 id new = [change objectForKey:NSKeyValueChangeNewKey];
115 masaki 66 // NSLog(@"Observed!");
116 masaki 14 if([new boolValue]) {
117 masaki 135 [self startedMovie];
118 masaki 14 } else {
119 masaki 135 [self stopedMovie];
120 masaki 14 }
121     return;
122     }
123 masaki 2
124 masaki 14
125 masaki 2 [super observeValueForKeyPath:keyPath
126     ofObject:object
127     change:change
128     context:context];
129     }
130    
131     - (void)setQtMovie:(QTMovie *)qt
132     {
133     if(qtMovie == qt) return;
134     if([qtMovie isEqual:qt]) return;
135 masaki 62 if(qt == (id)[NSNull null]) qt = nil;
136 masaki 2
137     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
138    
139     if(qtMovie) {
140     [nc removeObserver:self name:nil object:qtMovie];
141     }
142     if(qt) {
143     [nc addObserver:self selector:@selector(didEndMovie:) name:QTMovieDidEndNotification object:qt];
144     }
145 masaki 3
146     if(qtMovie) {
147     [qt setVolume:[qtMovie volume]];
148     [qt setMuted:[qtMovie muted]];
149     }
150 masaki 2 [qtMovie autorelease];
151     qtMovie = [qt retain];
152 masaki 49
153     if(!qtMovie) return;
154    
155 masaki 2 [self synchronizeWindowTitleWithDocumentName];
156 masaki 17 [self sizeTofitWidnow];
157 masaki 2 [self play];
158     }
159     - (QTMovie *)qtMovie
160     {
161     return qtMovie;
162     }
163    
164 masaki 10 #pragma mark ### Other functions ###
165 masaki 45 - (NSSize)windowSizeWithoutQTView
166     {
167     if(windowSizeWithoutQTView.width == 0
168     && windowSizeWithoutQTView.height == 0) {
169     QTMovie *curMovie = [self qtMovie];
170     if(!curMovie) return windowSizeWithoutQTView;
171    
172     NSSize qtViewSize = [qtView frame].size;
173     NSSize currentWindowSize = [[self window] frame].size;
174    
175     windowSizeWithoutQTView = NSMakeSize(currentWindowSize.width - qtViewSize.width,
176     currentWindowSize.height - qtViewSize.height);
177     }
178    
179     return windowSizeWithoutQTView;
180     }
181 masaki 17 - (void)sizeTofitWidnow
182     {
183     id window = [self window];
184     NSRect frame = [window frame];
185     NSSize newSize = [self fitSizeToSize:frame.size];
186 masaki 19 frame.origin.y += frame.size.height - newSize.height;
187 masaki 17 frame.size = newSize;
188    
189 masaki 19 [window setFrame:frame display:YES animate:YES];
190 masaki 17 }
191 masaki 2 - (NSSize)fitSizeToSize:(NSSize)toSize
192     {
193     QTMovie *curMovie = [self qtMovie];
194     if(!curMovie) return toSize;
195    
196 masaki 10 // Area size without QTMovieView.
197 masaki 45 NSSize delta = [self windowSizeWithoutQTView];
198 masaki 2
199     NSSize movieSize = [[curMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
200 masaki 107 if(movieSize.width == 0) return toSize;
201 masaki 2
202     float targetViewWidth = toSize.width - delta.width;
203     float targetViewHeight = targetViewWidth * (movieSize.height / movieSize.width);
204    
205     targetViewWidth += delta.width;
206     targetViewHeight += delta.height;
207    
208 masaki 107 return NSMakeSize(targetViewWidth, targetViewHeight);
209 masaki 2 }
210    
211 masaki 134 - (NSSize)windowSizeFromMovieSize:(NSSize)movieSize
212     {
213    
214     //
215 masaki 135
216     return NSZeroSize;
217 masaki 134 }
218    
219     - (void)setMovieSize:(NSSize)movieSize
220     {
221     //
222     }
223 masaki 135 - (void)startedMovie
224     {
225     [playButton setTitle:@"||"];
226    
227     @synchronized(self) {
228     if(updateTime) return;
229    
230     updateTime = [[NSTimer timerWithTimeInterval:sUpdateTimeInterval
231     target:self
232     selector:@selector(updateTimeIfNeeded:)
233     userInfo:NULL
234     repeats:YES] retain];
235    
236     NSRunLoop *currentLoop = [NSRunLoop currentRunLoop];
237     [currentLoop addTimer:updateTime forMode:NSDefaultRunLoopMode];
238     }
239     }
240    
241     - (void)stopedMovie
242     {
243     [playButton setTitle:@">"];
244    
245     @synchronized(self) {
246     [updateTime invalidate];
247     [updateTime release];
248     updateTime = nil;
249     }
250     }
251 masaki 2 - (void)play
252     {
253     [qtView performSelectorOnMainThread:@selector(play:) withObject:self waitUntilDone:NO];
254     }
255 masaki 14 - (void)pause
256     {
257     [qtView performSelectorOnMainThread:@selector(pause:) withObject:self waitUntilDone:NO];
258     }
259 masaki 56 - (void)stop
260     {
261     [qtView performSelectorOnMainThread:@selector(pause:) withObject:self waitUntilDone:YES];
262     }
263 masaki 3 - (void)enterFullScreen
264 masaki 2 {
265 masaki 17 NSWindow *w = [self fullscreenWindow];
266 masaki 3
267 masaki 17 nomalModeSavedFrame = [qtView frame];
268    
269     [[self window] orderOut:self];
270     [w setContentView:qtView];
271    
272     // [NSMenu setMenuBarVisible:NO];
273 masaki 107 SetSystemUIMode(kUIModeAllHidden, kUIOptionAutoShowMenuBar);
274 masaki 17
275     [w makeKeyAndOrderFront:self];
276     [w makeFirstResponder:qtView];
277 masaki 2 }
278 masaki 3 - (void)exitFullScreen
279 masaki 2 {
280 masaki 17 [qtView retain];
281     {
282     [qtView removeFromSuperview];
283     [qtView setFrame:nomalModeSavedFrame];
284     [[[self window] contentView] addSubview:qtView];
285     }
286     [qtView release];
287    
288 masaki 132 [self sizeTofitWidnow];
289     NSSize movieSize = [[[self qtMovie] attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
290     if(movieSize.width != 0) {
291     NSRect windowRect = [[self window] frame];
292    
293     CGFloat contentViewHeight = [[[self window] contentView] frame].size.height;
294     NSRect newViewRect = nomalModeSavedFrame;
295     newViewRect.size.height = newViewRect.size.width * (movieSize.height / movieSize.width);
296     newViewRect.origin.y = contentViewHeight - newViewRect.size.height;
297    
298     NSSize delta = [self windowSizeWithoutQTView];
299     windowRect.size.height = newViewRect.size.height + delta.height;
300     [[self window] setFrame:windowRect display:NO];
301    
302     [qtView setFrame:newViewRect];
303     }
304    
305 masaki 17 [NSMenu setMenuBarVisible:YES];
306 masaki 132 [[self fullscreenWindow] orderOut:self];
307 masaki 107
308 masaki 17 [[self window] makeKeyAndOrderFront:self];
309     [[self window] makeFirstResponder:qtView];
310 masaki 3 }
311 masaki 10
312     - (NSWindow *)fullscreenWindow
313     {
314     if(fullscreenWindow) return fullscreenWindow;
315    
316     NSRect mainScreenRect = [[NSScreen mainScreen] frame];
317 masaki 72 fullscreenWindow = [[XspfQTFullScreenWindow alloc] initWithContentRect:mainScreenRect
318 masaki 132 styleMask:NSBorderlessWindowMask
319     backing:NSBackingStoreBuffered
320     defer:YES];
321 masaki 10 [fullscreenWindow setReleasedWhenClosed:NO];
322     [fullscreenWindow setBackgroundColor:[NSColor blackColor]];
323     [fullscreenWindow setDelegate:self];
324 masaki 49 [fullscreenWindow setWindowController:self];
325 masaki 10
326     return fullscreenWindow;
327     }
328    
329     #pragma mark ### Actions ###
330 masaki 14 - (IBAction)togglePlayAndPause:(id)sender
331     {
332     if([[self valueForKeyPath:@"document.trackList.isPlayed"] boolValue]) {
333     [self pause];
334     } else {
335     [self play];
336     }
337     }
338    
339 masaki 3 - (IBAction)turnUpVolume:(id)sender
340     {
341 masaki 135 NSNumber *cv = [self valueForKeyPath:kVolumeKeyPath];
342 masaki 3 cv = [NSNumber numberWithFloat:[cv floatValue] + sVolumeDelta];
343 masaki 135 [self setValue:cv forKeyPath:kVolumeKeyPath];
344 masaki 3 }
345     - (IBAction)turnDownVolume:(id)sender
346     {
347 masaki 135 NSNumber *cv = [self valueForKeyPath:kVolumeKeyPath];
348 masaki 3 cv = [NSNumber numberWithFloat:[cv floatValue] - sVolumeDelta];
349 masaki 135 [self setValue:cv forKeyPath:kVolumeKeyPath];
350 masaki 3 }
351     - (IBAction)toggleFullScreenMode:(id)sender
352     {
353 masaki 10 if(fullScreenMode) {
354 masaki 3 [self exitFullScreen];
355 masaki 10 fullScreenMode = NO;
356 masaki 2 } else {
357 masaki 3 [self enterFullScreen];
358 masaki 10 fullScreenMode = YES;
359 masaki 2 }
360     }
361    
362 masaki 4 - (IBAction)forwardTagValueSecends:(id)sender
363     {
364     if(![sender respondsToSelector:@selector(tag)]) return;
365    
366     int tag = [sender tag];
367     if(tag == 0) return;
368    
369     QTTime current = [[self qtMovie] currentTime];
370     NSTimeInterval cur;
371     if(!QTGetTimeInterval(current, &cur)) return;
372    
373     QTTime new = QTMakeTimeWithTimeInterval(cur + tag);
374 masaki 9 [[self qtMovie] setCurrentTime:new];
375 masaki 4 }
376     - (IBAction)backwardTagValueSecends:(id)sender
377     {
378     if(![sender respondsToSelector:@selector(tag)]) return;
379    
380     int tag = [sender tag];
381     if(tag == 0) return;
382    
383     QTTime current = [[self qtMovie] currentTime];
384     NSTimeInterval cur;
385     if(!QTGetTimeInterval(current, &cur)) return;
386    
387     QTTime new = QTMakeTimeWithTimeInterval(cur - tag);
388 masaki 9 [[self qtMovie] setCurrentTime:new];
389 masaki 4 }
390 masaki 24 - (IBAction)nextTrack:(id)sender
391     {
392 masaki 49 [qtView pause:sender];
393 masaki 24 [[[self document] trackList] next];
394     }
395     - (IBAction)previousTrack:(id)sender
396     {
397 masaki 49 [qtView pause:sender];
398 masaki 24 [[[self document] trackList] previous];
399     }
400 masaki 4
401 masaki 134 - (IBAction)normalSize:(id)sender
402     {
403     if(fullScreenMode) return;
404    
405     QTMovie *curMovie = [self qtMovie];
406     if(!curMovie) return;
407    
408     NSSize movieSize = [[curMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
409     if(movieSize.width == 0) return;
410    
411     [self setMovieSize:movieSize];
412     }
413     - (IBAction)halfSize:(id)sender
414     {
415     if(fullScreenMode) return;
416    
417     QTMovie *curMovie = [self qtMovie];
418     if(!curMovie) return;
419    
420     NSSize movieSize = [[curMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
421     if(movieSize.width == 0) return;
422    
423     movieSize.width *= 0.5;
424     movieSize.height *= 0.5;
425    
426     [self setMovieSize:movieSize];
427     }
428     - (IBAction)doubleSize:(id)sender
429     {
430     if(fullScreenMode) return;
431    
432     QTMovie *curMovie = [self qtMovie];
433     if(!curMovie) return;
434    
435     NSSize movieSize = [[curMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
436     if(movieSize.width == 0) return;
437    
438     movieSize.width *= 2;
439     movieSize.height *= 2;
440    
441     [self setMovieSize:movieSize];
442     }
443 masaki 135 - (IBAction)screenSize:(id)sender
444     {
445     //
446     //
447     }
448 masaki 134
449 masaki 10 #pragma mark ### Notification & Timer ###
450 masaki 2 - (void)didEndMovie:(id)notification
451     {
452     [[[self document] trackList] next];
453     }
454     - (void)updateTimeIfNeeded:(id)timer
455     {
456     QTMovie *qt = [self qtMovie];
457     if(qt) {
458 masaki 10 // force update time indicator.
459 masaki 2 [qt willChangeValueForKey:@"currentTime"];
460     [qt didChangeValueForKey:@"currentTime"];
461     }
462 masaki 3
463     // Hide cursor and controller, if mouse didn't move for 3 seconds.
464     NSPoint mouse = [NSEvent mouseLocation];
465     if(!NSEqualPoints(prevMouse, mouse)) {
466     prevMouse = mouse;
467     [prevMouseMovedDate autorelease];
468     prevMouseMovedDate = [[NSDate dateWithTimeIntervalSinceNow:0.0] retain];
469 masaki 10 } else if(fullScreenMode && [prevMouseMovedDate timeIntervalSinceNow] < -3.0 ) {
470 masaki 3 [NSCursor setHiddenUntilMouseMoves:YES];
471     //
472     // hide controller.
473     }
474 masaki 2 }
475    
476 masaki 10 #pragma mark ### NSResponder ###
477 masaki 2 - (void)cancelOperation:(id)sender
478     {
479 masaki 10 if(fullScreenMode) {
480 masaki 2 [self toggleFullScreenMode:self];
481     }
482     }
483    
484 masaki 10 #pragma mark ### NSMenu valivation ###
485     - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
486     {
487     if([menuItem action] == @selector(toggleFullScreenMode:)) {
488     if(fullScreenMode) {
489     [menuItem setTitle:NSLocalizedString(@"Exit Full Screen", @"Exit Full Screen")];
490     } else {
491     [menuItem setTitle:NSLocalizedString(@"Full Screen", @"Full Screen")];
492     }
493     return YES;
494     }
495    
496     return YES;
497     }
498    
499 masaki 100 #pragma mark ### XspfQTDocument Notification ###
500     - (void)documentWillClose:(NSNotification *)notification
501 masaki 2 {
502 masaki 100 id doc = [notification object];
503     // NSLog(@"Remove Observed! %@", doc);
504 masaki 10 if(fullScreenMode) {
505 masaki 2 [self toggleFullScreenMode:self];
506     }
507 masaki 100
508     [doc removeObserver:self forKeyPath:kQTMovieKeyPath];
509     [doc removeObserver:self forKeyPath:kIsPlayedKeyPath];
510    
511     NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
512     [nc removeObserver:self name:XspfQTDocumentWillCloseNotification object:doc];
513 masaki 2 }
514    
515 masaki 10 #pragma mark ### NSWindow Delegate ###
516 masaki 2 - (BOOL)windowShouldClose:(id)sender
517     {
518     [qtView pause:self];
519     [self setQtMovie:nil];
520    
521 masaki 66 [updateTime invalidate];
522 masaki 2 updateTime = nil;
523    
524     return YES;
525     }
526     - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize
527     {
528     return [self fitSizeToSize:frameSize];
529     }
530 masaki 16 - (void)windowDidMove:(NSNotification *)notification
531     {
532     if(fullscreenWindow && [notification object] == fullscreenWindow) {
533     NSRect r = [fullscreenWindow frame];
534 masaki 45 if(!NSEqualPoints(r.origin, NSZeroPoint)) {
535 masaki 16 [fullscreenWindow setFrameOrigin:NSZeroPoint];
536     }
537     }
538     }
539 masaki 2 @end

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26