1/* 2 Project: MPDCon 3 4 Copyright (C) 2004 5 6 Author: Daniel Luederwald 7 8 Created: 2004-05-12 17:59:14 +0200 by flip 9 10 Custom View : Shows Trackinfos etc. 11 12 This application is free software; you can redistribute it and/or 13 modify it under the terms of the GNU General Public 14 License as published by the Free Software Foundation; either 15 version 2 of the License, or (at your option) any later version. 16 17 This application is distributed in the hope that it will be useful, 18 but WITHOUT ANY WARRANTY; without even the implied warranty of 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 Library General Public License for more details. 21 22 You should have received a copy of the GNU General Public 23 License along with this library; if not, write to the Free 24 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. 25*/ 26 27#include <AppKit/AppKit.h> 28#include "PlayView.h" 29 30 31/* --------------------- 32 - Private Interface - 33 ---------------------*/ 34 35@interface PlayView(Private) 36- (NSString *) _getTime; 37 38- (void) _drawTrack; 39- (void) _drawArtist; 40- (void) _drawAlbum; 41- (void) _drawTitle; 42- (void) _drawTime; 43@end 44 45@implementation PlayView 46 47/* -------------------------- 48 - Initialization Methods - 49 --------------------------*/ 50 51+ (id) init 52{ 53 self = [super init]; 54 55 56 return self; 57} 58 59- (void) dealloc 60{ 61 [dispSong release]; 62 63 [super dealloc]; 64} 65 66/* ------------------- 67 - Display Methods - 68 -------------------*/ 69 70 71- (void) drawRect: (NSRect)rect 72{ 73 NSRect myBounds; 74 NSData *colorData; 75 NSColor *displayColor; 76 77 if (! notificationAdded) 78 { 79 notificationAdded = YES; 80 [[NSNotificationCenter defaultCenter] 81 addObserver: self 82 selector: @selector(songChanged:) 83 name: SongChangedNotification 84 object: nil]; 85 86 [[NSNotificationCenter defaultCenter] 87 addObserver: self 88 selector: @selector(songChanged:) 89 name: PreferencesChangedNotification 90 object: nil]; 91 } 92 93 colorData = [[NSUserDefaults standardUserDefaults] dataForKey: @"displayColor"]; 94 95 if (! colorData) 96 { 97 displayColor = [NSColor grayColor]; 98 } 99 else 100 { 101 displayColor = [NSUnarchiver unarchiveObjectWithData: colorData]; 102 } 103 104 [displayColor set]; 105 106 107 myBounds = [self bounds]; 108 [NSBezierPath fillRect: myBounds]; 109 [[NSColor blackColor] set]; 110 111 NSRect strokeRect = NSMakeRect(myBounds.origin.x, myBounds.origin.y+1, myBounds.size.width-1, myBounds.size.height-1); 112 [NSBezierPath strokeRect: strokeRect]; 113 114 if (displayEnabled) 115 { 116 [self _drawTrack]; 117 [self _drawArtist]; 118 [self _drawAlbum]; 119 [self _drawTitle]; 120 [self _drawTime]; 121 } 122 else 123 { 124 NSImage *gnustepImage; 125 int bWidth, bHeight, iWidth, iHeight; 126 127 bWidth = myBounds.size.width; 128 bHeight = myBounds.size.height; 129 130 gnustepImage = [NSImage imageNamed: @"GNUstep.tiff"]; 131 132 iWidth = [gnustepImage size].width; 133 iHeight = [gnustepImage size].height; 134 135 [gnustepImage compositeToPoint: NSMakePoint((bWidth/2.0) - (iWidth/2.0), (bHeight/2.0) - (iHeight/2.0)) 136 fromRect: myBounds 137 operation: NSCompositeSourceOver]; 138 } 139} 140 141- (void) setDisplaySong: (PlaylistItem *)newSong 142{ 143 [dispSong release]; 144 145 dispSong = [newSong retain]; 146 [self setNeedsDisplay: YES]; 147} 148 149- (void) setCurrentSong: (int)newSong 150{ 151 currentSong = newSong; 152} 153 154- (void) setTotalSongs: (int)newSong 155{ 156 totalSongs = newSong; 157} 158 159- (void) setElapsedTime: (int)newTime 160{ 161 if (dispSong) 162 { 163 [dispSong setElapsedTime: newTime]; 164 } 165 166 [self setNeedsDisplay: YES]; 167} 168 169- (void) setReversedTime: (BOOL)reversed 170{ 171 reversedTime = reversed; 172} 173 174- (void) enableDisplay: (BOOL)enabled 175{ 176 displayEnabled = enabled; 177 [self setNeedsDisplay: YES]; 178} 179 180/* ------------------------ 181 - Notification Methods - 182 ------------------------*/ 183 184- (void) songChanged: (NSNotification *)aNotif 185{ 186 artistScroll = 0; 187 titleScroll = 0; 188 albumScroll = 0; 189 artistScrollForward = YES; 190 titleScrollForward = YES; 191 albumScrollForward = YES; 192 193 [self setNeedsDisplay: YES]; 194} 195 196/* -------------------- 197 - Delegate Methods - 198 --------------------*/ 199 200- (void) mouseUp: (NSEvent *)anEvent 201{ 202 if (reversedTime) 203 { 204 reversedTime = NO; 205 } 206 else 207 { 208 reversedTime = YES; 209 } 210 211 [[NSUserDefaults standardUserDefaults] setBool: reversedTime 212 forKey: @"reversedTime"]; 213} 214 215@end 216/* ------------------- 217 - Private Methods - 218 -------------------*/ 219 220@implementation PlayView(Private) 221- (NSString *) _getTime 222{ 223 int totalTime, elapsedTime; 224 225 int tSecs, eSecs; 226 int tMin, eMin; 227 228 229 totalTime = [dispSong getTotalTime]; 230 elapsedTime = [dispSong getElapsedTime]; 231 tSecs = (totalTime % 60); 232 tMin = (int)totalTime/60; 233 234 if (! reversedTime) 235 { 236 eSecs = (elapsedTime % 60); 237 eMin = (int)elapsedTime/60; 238 return [NSString stringWithFormat: @"%d:%02d/%d:%02d" 239 , eMin, eSecs, tMin, tSecs]; 240 } 241 else 242 { 243 eSecs = ((totalTime - elapsedTime) % 60); 244 eMin = (int) (totalTime - elapsedTime) / 60; 245 return [NSString stringWithFormat: @"- %d:%02d/%d:%02d" 246 , eMin, eSecs, tMin, tSecs]; 247 } 248} 249 250- (void) _drawTrack 251{ 252 [[NSString stringWithFormat: _(@"Playing Track %d of %d") 253 , currentSong, totalSongs] 254 drawAtPoint: NSMakePoint(5, 60) withAttributes: nil]; 255} 256 257- (void) _drawArtist 258{ 259 NSString *theArtist; 260 NSRect myBounds; 261 262 NSMutableDictionary *attributes; 263 NSFont *theFont; 264 265 int enableScroll; 266 267 268 theArtist = [dispSong getArtist]; 269 myBounds = [self bounds]; 270 271 attributes = [NSMutableDictionary dictionary]; 272 273 enableScroll = [[NSUserDefaults standardUserDefaults] 274 integerForKey: @"enableScroll"]; 275 276 theFont = [NSFont boldSystemFontOfSize: 18]; 277 [attributes setObject: theFont forKey: NSFontAttributeName]; 278 279 if (([theArtist sizeWithAttributes: attributes].width 280 > myBounds.size.width-10) && enableScroll) 281 { 282 if ([theArtist sizeWithAttributes: attributes].width-artistScroll 283 < myBounds.size.width - 20) 284 { 285 artistScrollForward=NO; 286 } 287 288 if (artistScroll == 0) 289 { 290 artistScrollForward=YES; 291 } 292 293 if (artistScrollForward) 294 { 295 artistScroll+=5; 296 } 297 else 298 { 299 artistScroll-=5; 300 } 301 } 302 303 [theArtist drawAtPoint: NSMakePoint(10-artistScroll, 37) 304 withAttributes: attributes]; 305} 306 307- (void) _drawAlbum 308{ 309 NSString *theAlbum; 310 NSRect myBounds; 311 312 NSMutableDictionary *attributes; 313 NSFont *theFont; 314 315 int enableScroll; 316 317 318 myBounds = [self bounds]; 319 320 attributes = [NSMutableDictionary dictionary]; 321 322 enableScroll = [[NSUserDefaults standardUserDefaults] 323 integerForKey: @"enableScroll"]; 324 325 theAlbum = [dispSong getAlbum]; 326 327 theFont = [NSFont systemFontOfSize: 11]; 328 329 [attributes setObject: theFont forKey: NSFontAttributeName]; 330 331 if (([theAlbum sizeWithAttributes: attributes].width 332 > myBounds.size.width-3) && enableScroll) 333 { 334 if ([theAlbum sizeWithAttributes: attributes].width-albumScroll 335 < myBounds.size.width - 10) 336 { 337 albumScrollForward=NO; 338 } 339 340 if (albumScroll == 0) 341 { 342 albumScrollForward=YES; 343 } 344 345 if (albumScrollForward) 346 { 347 albumScroll+=5; 348 } 349 else 350 { 351 albumScroll-=5; 352 } 353 } 354 355 [theAlbum drawAtPoint: NSMakePoint(5-albumScroll, 2) 356 withAttributes: attributes]; 357} 358 359- (void) _drawTitle 360{ 361 NSString *theTitle; 362 NSString *theTrack; 363 NSRect myBounds; 364 365 NSMutableDictionary *attributes; 366 NSFont *theFont; 367 368 int enableScroll; 369 370 371 myBounds = [self bounds]; 372 373 attributes = [NSMutableDictionary dictionary]; 374 375 enableScroll = [[NSUserDefaults standardUserDefaults] integerForKey: @"enableScroll"]; 376 377 theTrack = [dispSong getTrackNr]; 378 379 if ([theTrack compare: @""] != NSOrderedSame) 380 { 381 theTitle = [theTrack stringByAppendingString: 382 [@" - " stringByAppendingString: 383 [dispSong getTitle]]]; 384 } 385 else 386 { 387 theTitle = [dispSong getTitle]; 388 } 389 390 391 theFont = [NSFont boldSystemFontOfSize: 13]; 392 393 [attributes setObject: theFont forKey: NSFontAttributeName]; 394 395 if (([theTitle sizeWithAttributes: attributes].width 396 > myBounds.size.width-10) && enableScroll) 397 { 398 if ([theTitle sizeWithAttributes: attributes].width-titleScroll 399 < myBounds.size.width - 10) 400 { 401 titleScrollForward=NO; 402 } 403 404 if (titleScroll == 0) 405 { 406 titleScrollForward=YES; 407 } 408 409 if (titleScrollForward) 410 { 411 titleScroll+=5; 412 } 413 else 414 { 415 titleScroll-=5; 416 } 417 } 418 419 [theTitle drawAtPoint: NSMakePoint(10-titleScroll, 22) 420 withAttributes: attributes]; 421} 422 423- (void) _drawTime 424{ 425 NSRect myBounds; 426 427 NSMutableDictionary *attributes; 428 NSFont *theFont; 429 430 431 myBounds = [self bounds]; 432 433 attributes = [NSMutableDictionary dictionary]; 434 435 theFont = [NSFont boldSystemFontOfSize: 12]; 436 437 [attributes setObject: theFont forKey: NSFontAttributeName]; 438 439 NSString *timeString = [self _getTime]; 440 NSSize timeSize = [timeString sizeWithAttributes: attributes]; 441 [timeString drawAtPoint: NSMakePoint(myBounds.size.width-timeSize.width-2 442 , 60) 443 withAttributes: attributes]; 444} 445 446/*NSColor *colorFromDict(NSDictionary *dict) 447{ 448 if(dict != nil) 449 { 450 return [NSColor colorWithCalibratedRed: [[dict objectForKey: @"red"] floatValue] 451 green: [[dict objectForKey: @"green"] floatValue] 452 blue: [[dict objectForKey: @"blue"] floatValue] 453 alpha: [[dict objectForKey: @"alpha"] floatValue]]; 454 } 455 return nil; 456}*/ 457@end 458