| 1 |
masaki |
2 |
// |
| 2 |
|
|
// XspfMovieWindowController.m |
| 3 |
|
|
// XspfQT |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 08/08/31. |
| 6 |
|
|
// Copyright 2008 masakih. All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
|
|
#import "XspfMovieWindowController.h" |
| 10 |
|
|
#import "XspfDocument.h" |
| 11 |
|
|
#import "XspfTrackList.h" |
| 12 |
|
|
#import "XspfFullScreenWindow.h" |
| 13 |
|
|
|
| 14 |
|
|
|
| 15 |
|
|
@interface XspfMovieWindowController (Private) |
| 16 |
|
|
- (NSSize)fitSizeToSize:(NSSize)toSize; |
| 17 |
|
|
- (NSWindow *)fullscreenWindow; |
| 18 |
|
|
@end |
| 19 |
|
|
|
| 20 |
|
|
@implementation XspfMovieWindowController |
| 21 |
|
|
|
| 22 |
|
|
static const float sVolumeDelta = 0.2; |
| 23 |
|
|
static NSString *const kCurrentIndexKeyPath = @"trackList.currentIndex"; |
| 24 |
|
|
|
| 25 |
|
|
- (id)initWithWindowNibName:(NSString *)windowNibName |
| 26 |
|
|
{ |
| 27 |
|
|
if(self = [super initWithWindowNibName:@"XspfDocument"]) { |
| 28 |
|
|
// |
| 29 |
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
| 30 |
|
|
[nc addObserver:self |
| 31 |
|
|
selector:@selector(applicationWillTerminate:) |
| 32 |
|
|
name:NSApplicationWillTerminateNotification |
| 33 |
|
|
object:NSApp]; |
| 34 |
|
|
|
| 35 |
|
|
updateTime = [NSTimer scheduledTimerWithTimeInterval:0.3 |
| 36 |
|
|
target:self |
| 37 |
|
|
selector:@selector(updateTimeIfNeeded:) |
| 38 |
|
|
userInfo:NULL |
| 39 |
|
|
repeats:YES]; |
| 40 |
|
|
} |
| 41 |
|
|
|
| 42 |
|
|
return self; |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
|
- (void)dealloc |
| 46 |
|
|
{ |
| 47 |
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
| 48 |
|
|
[nc removeObserver:self]; |
| 49 |
|
|
|
| 50 |
|
|
[fullscreenWindow release]; |
| 51 |
|
|
|
| 52 |
|
|
[qtMovie release]; |
| 53 |
|
|
|
| 54 |
|
|
[super dealloc]; |
| 55 |
|
|
} |
| 56 |
|
|
|
| 57 |
|
|
- (void)awakeFromNib |
| 58 |
|
|
{ |
| 59 |
masaki |
3 |
prevMouseMovedDate = [[NSDate dateWithTimeIntervalSinceNow:0.0] retain]; |
| 60 |
|
|
|
| 61 |
masaki |
2 |
id d = [self document]; |
| 62 |
|
|
// NSLog(@"Add Observed! %@", d); |
| 63 |
|
|
[d addObserver:self |
| 64 |
|
|
forKeyPath:kCurrentIndexKeyPath |
| 65 |
|
|
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld |
| 66 |
|
|
context:NULL]; |
| 67 |
|
|
|
| 68 |
|
|
|
| 69 |
|
|
id window = [self window]; |
| 70 |
|
|
NSRect frame = [window frame]; |
| 71 |
|
|
NSSize newSize = [self fitSizeToSize:frame.size]; |
| 72 |
|
|
frame.size = newSize; |
| 73 |
|
|
frame.origin.y -= frame.size.height - newSize.height; |
| 74 |
|
|
|
| 75 |
|
|
[window setFrame:frame display:YES]; |
| 76 |
|
|
|
| 77 |
|
|
[self play]; |
| 78 |
|
|
} |
| 79 |
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath |
| 80 |
|
|
ofObject:(id)object |
| 81 |
|
|
change:(NSDictionary *)change |
| 82 |
|
|
context:(void *)context |
| 83 |
|
|
{ |
| 84 |
|
|
// NSLog(@"Observed!"); |
| 85 |
|
|
if([keyPath isEqual:kCurrentIndexKeyPath]) { |
| 86 |
|
|
id old = [change objectForKey:NSKeyValueChangeOldKey]; |
| 87 |
|
|
id new = [change objectForKey:NSKeyValueChangeNewKey]; |
| 88 |
|
|
if([old isEqual:new]) return; |
| 89 |
|
|
[self setQtMovie:[self valueForKeyPath:@"document.trackList.qtMovie"]]; |
| 90 |
|
|
return; |
| 91 |
|
|
} |
| 92 |
|
|
|
| 93 |
|
|
[super observeValueForKeyPath:keyPath |
| 94 |
|
|
ofObject:object |
| 95 |
|
|
change:change |
| 96 |
|
|
context:context]; |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
|
|
- (void)setQtMovie:(QTMovie *)qt |
| 100 |
|
|
{ |
| 101 |
|
|
if(qtMovie == qt) return; |
| 102 |
|
|
if([qtMovie isEqual:qt]) return; |
| 103 |
|
|
|
| 104 |
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
| 105 |
|
|
|
| 106 |
|
|
if(qtMovie) { |
| 107 |
|
|
[nc removeObserver:self name:nil object:qtMovie]; |
| 108 |
|
|
} |
| 109 |
|
|
if(qt) { |
| 110 |
|
|
[nc addObserver:self selector:@selector(didEndMovie:) name:QTMovieDidEndNotification object:qt]; |
| 111 |
|
|
} |
| 112 |
masaki |
3 |
|
| 113 |
|
|
if(qtMovie) { |
| 114 |
|
|
[qt setVolume:[qtMovie volume]]; |
| 115 |
|
|
[qt setMuted:[qtMovie muted]]; |
| 116 |
|
|
} |
| 117 |
masaki |
2 |
[qtMovie autorelease]; |
| 118 |
|
|
qtMovie = [qt retain]; |
| 119 |
|
|
[self synchronizeWindowTitleWithDocumentName]; |
| 120 |
|
|
|
| 121 |
|
|
[self play]; |
| 122 |
|
|
} |
| 123 |
|
|
- (QTMovie *)qtMovie |
| 124 |
|
|
{ |
| 125 |
|
|
return qtMovie; |
| 126 |
|
|
} |
| 127 |
|
|
|
| 128 |
masaki |
3 |
|
| 129 |
masaki |
2 |
- (NSSize)fitSizeToSize:(NSSize)toSize |
| 130 |
|
|
{ |
| 131 |
|
|
QTMovie *curMovie = [self qtMovie]; |
| 132 |
|
|
if(!curMovie) return toSize; |
| 133 |
|
|
|
| 134 |
|
|
NSSize qtViewSize = [qtView frame].size; |
| 135 |
|
|
NSSize currentWindowSize = [[self window] frame].size; |
| 136 |
|
|
|
| 137 |
|
|
NSSize delta = NSMakeSize(currentWindowSize.width - qtViewSize.width, |
| 138 |
|
|
currentWindowSize.height - qtViewSize.height); |
| 139 |
|
|
|
| 140 |
|
|
NSSize movieSize = [[curMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue]; |
| 141 |
|
|
|
| 142 |
|
|
float targetViewWidth = toSize.width - delta.width; |
| 143 |
|
|
float targetViewHeight = targetViewWidth * (movieSize.height / movieSize.width); |
| 144 |
|
|
|
| 145 |
|
|
targetViewWidth += delta.width; |
| 146 |
|
|
targetViewHeight += delta.height; |
| 147 |
|
|
|
| 148 |
|
|
NSSize newSize = NSMakeSize(targetViewWidth, targetViewHeight); |
| 149 |
|
|
|
| 150 |
|
|
return newSize; |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
|
|
- (void)play |
| 154 |
|
|
{ |
| 155 |
|
|
[qtView performSelectorOnMainThread:@selector(play:) withObject:self waitUntilDone:NO]; |
| 156 |
|
|
} |
| 157 |
|
|
|
| 158 |
masaki |
3 |
- (void)enterFullScreen |
| 159 |
masaki |
2 |
{ |
| 160 |
masaki |
3 |
// if([qtView respondsToSelector:@selector(isInFullScreenMode)]) { |
| 161 |
|
|
// // System is 10.5 or later. |
| 162 |
|
|
// id op = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] |
| 163 |
|
|
// forKey:@"NSFullScreenModeAllScreens"]; |
| 164 |
|
|
// [qtView enterFullScreenMode:[NSScreen mainScreen] |
| 165 |
|
|
// withOptions:op]; |
| 166 |
|
|
// NSLog(@"Use enterFullScreen:withOptions:"); |
| 167 |
|
|
// } else { |
| 168 |
|
|
NSWindow *w = [self fullscreenWindow]; |
| 169 |
|
|
|
| 170 |
|
|
nomalModeSavedFrame = [qtView frame]; |
| 171 |
|
|
|
| 172 |
masaki |
4 |
[[self window] orderOut:self]; |
| 173 |
masaki |
3 |
[w setContentView:qtView]; |
| 174 |
|
|
|
| 175 |
|
|
// [NSMenu setMenuBarVisible:NO]; |
| 176 |
|
|
SetSystemUIMode (kUIModeAllHidden, kUIOptionAutoShowMenuBar); |
| 177 |
|
|
|
| 178 |
|
|
[w makeKeyAndOrderFront:self]; |
| 179 |
|
|
[w makeFirstResponder:qtView]; |
| 180 |
|
|
// } |
| 181 |
|
|
|
| 182 |
masaki |
2 |
} |
| 183 |
masaki |
3 |
- (void)exitFullScreen |
| 184 |
masaki |
2 |
{ |
| 185 |
masaki |
3 |
// if([qtView respondsToSelector:@selector(isInFullScreenMode)]) { |
| 186 |
|
|
// // System is 10.5 or later. |
| 187 |
|
|
// [qtView exitFullScreenModeWithOptions:nil]; |
| 188 |
|
|
// } else { |
| 189 |
|
|
NSWindow *w = [self fullscreenWindow]; |
| 190 |
masaki |
2 |
|
| 191 |
|
|
[qtView retain]; |
| 192 |
|
|
{ |
| 193 |
|
|
[qtView removeFromSuperview]; |
| 194 |
|
|
[qtView setFrame:nomalModeSavedFrame]; |
| 195 |
|
|
[[[self window] contentView] addSubview:qtView]; |
| 196 |
|
|
} |
| 197 |
|
|
[qtView release]; |
| 198 |
|
|
|
| 199 |
|
|
[NSMenu setMenuBarVisible:YES]; |
| 200 |
|
|
[w orderOut:self]; |
| 201 |
|
|
[[self window] makeKeyAndOrderFront:self]; |
| 202 |
|
|
[w makeFirstResponder:qtView]; |
| 203 |
masaki |
3 |
// } |
| 204 |
|
|
} |
| 205 |
|
|
- (IBAction)turnUpVolume:(id)sender |
| 206 |
|
|
{ |
| 207 |
|
|
NSNumber *cv = [self valueForKeyPath:@"qtMovie.volume"]; |
| 208 |
|
|
cv = [NSNumber numberWithFloat:[cv floatValue] + sVolumeDelta]; |
| 209 |
|
|
[self setValue:cv forKeyPath:@"qtMovie.volume"]; |
| 210 |
|
|
} |
| 211 |
|
|
- (IBAction)turnDownVolume:(id)sender |
| 212 |
|
|
{ |
| 213 |
|
|
NSNumber *cv = [self valueForKeyPath:@"qtMovie.volume"]; |
| 214 |
|
|
cv = [NSNumber numberWithFloat:[cv floatValue] - sVolumeDelta]; |
| 215 |
|
|
[self setValue:cv forKeyPath:@"qtMovie.volume"]; |
| 216 |
|
|
} |
| 217 |
|
|
- (IBAction)toggleFullScreenMode:(id)sender |
| 218 |
|
|
{ |
| 219 |
|
|
if(fullscreenMode) { |
| 220 |
|
|
[self exitFullScreen]; |
| 221 |
masaki |
2 |
fullscreenMode = NO; |
| 222 |
|
|
} else { |
| 223 |
masaki |
3 |
[self enterFullScreen]; |
| 224 |
masaki |
2 |
fullscreenMode = YES; |
| 225 |
|
|
} |
| 226 |
|
|
} |
| 227 |
|
|
|
| 228 |
masaki |
4 |
- (IBAction)forwardTagValueSecends:(id)sender |
| 229 |
|
|
{ |
| 230 |
|
|
if(![sender respondsToSelector:@selector(tag)]) return; |
| 231 |
|
|
|
| 232 |
|
|
int tag = [sender tag]; |
| 233 |
|
|
if(tag == 0) return; |
| 234 |
|
|
|
| 235 |
|
|
QTTime current = [[self qtMovie] currentTime]; |
| 236 |
|
|
NSTimeInterval cur; |
| 237 |
|
|
if(!QTGetTimeInterval(current, &cur)) return; |
| 238 |
|
|
|
| 239 |
|
|
QTTime new = QTMakeTimeWithTimeInterval(cur + tag); |
| 240 |
masaki |
9 |
[[self qtMovie] setCurrentTime:new]; |
| 241 |
masaki |
4 |
} |
| 242 |
|
|
- (IBAction)backwardTagValueSecends:(id)sender |
| 243 |
|
|
{ |
| 244 |
|
|
if(![sender respondsToSelector:@selector(tag)]) return; |
| 245 |
|
|
|
| 246 |
|
|
int tag = [sender tag]; |
| 247 |
|
|
if(tag == 0) return; |
| 248 |
|
|
|
| 249 |
|
|
QTTime current = [[self qtMovie] currentTime]; |
| 250 |
|
|
NSTimeInterval cur; |
| 251 |
|
|
if(!QTGetTimeInterval(current, &cur)) return; |
| 252 |
|
|
|
| 253 |
|
|
QTTime new = QTMakeTimeWithTimeInterval(cur - tag); |
| 254 |
masaki |
9 |
[[self qtMovie] setCurrentTime:new]; |
| 255 |
masaki |
4 |
} |
| 256 |
|
|
|
| 257 |
masaki |
2 |
- (NSWindow *)fullscreenWindow |
| 258 |
|
|
{ |
| 259 |
|
|
if(fullscreenWindow) return fullscreenWindow; |
| 260 |
|
|
|
| 261 |
|
|
NSRect mainScreenRect = [[NSScreen mainScreen] frame]; |
| 262 |
|
|
fullscreenWindow = [[XspfFullScreenWindow alloc] initWithContentRect:mainScreenRect |
| 263 |
|
|
styleMask:NSBorderlessWindowMask |
| 264 |
|
|
backing:NSBackingStoreBuffered |
| 265 |
|
|
defer:YES]; |
| 266 |
|
|
[fullscreenWindow setReleasedWhenClosed:NO]; |
| 267 |
|
|
[fullscreenWindow setBackgroundColor:[NSColor blackColor]]; |
| 268 |
|
|
[fullscreenWindow setDelegate:self]; |
| 269 |
|
|
|
| 270 |
|
|
return fullscreenWindow; |
| 271 |
|
|
} |
| 272 |
|
|
|
| 273 |
|
|
- (void)didEndMovie:(id)notification |
| 274 |
|
|
{ |
| 275 |
|
|
[[[self document] trackList] next]; |
| 276 |
|
|
// [self setQtMovie:[[[self document] trackList] qtMovie]]; |
| 277 |
|
|
} |
| 278 |
|
|
- (void)updateTimeIfNeeded:(id)timer |
| 279 |
|
|
{ |
| 280 |
|
|
QTMovie *qt = [self qtMovie]; |
| 281 |
|
|
if(qt) { |
| 282 |
|
|
[qt willChangeValueForKey:@"currentTime"]; |
| 283 |
|
|
[qt didChangeValueForKey:@"currentTime"]; |
| 284 |
|
|
} |
| 285 |
masaki |
3 |
|
| 286 |
|
|
// Hide cursor and controller, if mouse didn't move for 3 seconds. |
| 287 |
|
|
NSPoint mouse = [NSEvent mouseLocation]; |
| 288 |
|
|
if(!NSEqualPoints(prevMouse, mouse)) { |
| 289 |
|
|
prevMouse = mouse; |
| 290 |
|
|
[prevMouseMovedDate autorelease]; |
| 291 |
|
|
prevMouseMovedDate = [[NSDate dateWithTimeIntervalSinceNow:0.0] retain]; |
| 292 |
|
|
} else if(fullscreenMode && [prevMouseMovedDate timeIntervalSinceNow] < -3.0 ) { |
| 293 |
|
|
[NSCursor setHiddenUntilMouseMoves:YES]; |
| 294 |
|
|
// |
| 295 |
|
|
// hide controller. |
| 296 |
|
|
} |
| 297 |
masaki |
2 |
} |
| 298 |
|
|
|
| 299 |
|
|
- (void)cancelOperation:(id)sender |
| 300 |
|
|
{ |
| 301 |
|
|
if(fullscreenMode) { |
| 302 |
|
|
[self toggleFullScreenMode:self]; |
| 303 |
|
|
} |
| 304 |
|
|
} |
| 305 |
|
|
|
| 306 |
|
|
- (void)applicationWillTerminate:(NSNotification *)notification |
| 307 |
|
|
{ |
| 308 |
|
|
if(fullscreenMode) { |
| 309 |
|
|
[self toggleFullScreenMode:self]; |
| 310 |
|
|
} |
| 311 |
|
|
[[self document] removeObserver:self forKeyPath:kCurrentIndexKeyPath]; |
| 312 |
|
|
} |
| 313 |
|
|
|
| 314 |
|
|
|
| 315 |
|
|
- (BOOL)windowShouldClose:(id)sender |
| 316 |
|
|
{ |
| 317 |
|
|
[qtView pause:self]; |
| 318 |
|
|
[self setQtMovie:nil]; |
| 319 |
|
|
|
| 320 |
|
|
[[self document] removeObserver:self forKeyPath:kCurrentIndexKeyPath]; |
| 321 |
|
|
[self setShouldCloseDocument:YES]; |
| 322 |
|
|
|
| 323 |
|
|
[updateTime release]; |
| 324 |
|
|
updateTime = nil; |
| 325 |
|
|
|
| 326 |
|
|
return YES; |
| 327 |
|
|
} |
| 328 |
|
|
- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize |
| 329 |
|
|
{ |
| 330 |
|
|
return [self fitSizeToSize:frameSize]; |
| 331 |
|
|
} |
| 332 |
|
|
@end |