/ImageKit/ImageBrowserViewAppearance/ImageBrowserBackgroundLayer.m

https://code.google.com/p/cocoalatte/ · Objective C · 129 lines · 42 code · 18 blank · 69 comment · 5 complexity · 447e702cf32d4ada03e6a432bd3de75c MD5 · raw file

  1. /*
  2. File: ImageBrowserBackgroundLayer.m
  3. Abstract: IKImageBrowserView is a view that can display and browse a
  4. large amount of images and movies. This sample code demonstrates
  5. how to use the view in a Cocoa Application.
  6. Version: 1.0
  7. Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
  8. Inc. ("Apple") in consideration of your agreement to the
  9. following terms, and your use, installation, modification or
  10. redistribution of this Apple software constitutes acceptance of these
  11. terms. If you do not agree with these terms, please do not use,
  12. install, modify or redistribute this Apple software.
  13. In consideration of your agreement to abide by the following terms, and
  14. subject to these terms, Apple grants you a personal, non-exclusive
  15. license, under Apple's copyrights in this original Apple software (the
  16. "Apple Software"), to use, reproduce, modify and redistribute the Apple
  17. Software, with or without modifications, in source and/or binary forms;
  18. provided that if you redistribute the Apple Software in its entirety and
  19. without modifications, you must retain this notice and the following
  20. text and disclaimers in all such redistributions of the Apple Software.
  21. Neither the name, trademarks, service marks or logos of Apple Inc.
  22. may be used to endorse or promote products derived from the Apple
  23. Software without specific prior written permission from Apple. Except
  24. as expressly stated in this notice, no other rights or licenses, express
  25. or implied, are granted by Apple herein, including but not limited to
  26. any patent rights that may be infringed by your derivative works or by
  27. other works in which the Apple Software may be incorporated.
  28. The Apple Software is provided by Apple on an "AS IS" basis. APPLE
  29. MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
  30. THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
  31. FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
  32. OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
  33. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
  34. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
  37. MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
  38. AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
  39. STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
  40. POSSIBILITY OF SUCH DAMAGE.
  41. Copyright Š 2009 Apple Inc. All Rights Reserved
  42. */
  43. #import "ImageBrowserBackgroundLayer.h"
  44. #import "Utilities.h"
  45. static CGImageRef backgroundImage()
  46. {
  47. static CGImageRef image = NULL;
  48. if(image == NULL)
  49. image = createImageWithName(@"metal_background.tif");
  50. return image;
  51. }
  52. @implementation ImageBrowserBackgroundLayer
  53. @synthesize owner;
  54. // -------------------------------------------------------------------------
  55. // init
  56. // -------------------------------------------------------------------------
  57. - (id) init
  58. {
  59. if((self = [super init])){
  60. //needs to redraw when bounds change
  61. [self setNeedsDisplayOnBoundsChange:YES];
  62. }
  63. return self;
  64. }
  65. // -------------------------------------------------------------------------
  66. // actionForKey:
  67. //
  68. // always return nil, to never animate
  69. // -------------------------------------------------------------------------
  70. - (id<CAAction>)actionForKey:(NSString *)event
  71. {
  72. return nil;
  73. }
  74. // -------------------------------------------------------------------------
  75. // drawInContext:
  76. //
  77. // draw a metal background that scrolls when the image browser scroll
  78. // -------------------------------------------------------------------------
  79. - (void)drawInContext:(CGContextRef)context
  80. {
  81. //retreive bounds and visible rect
  82. NSRect visibleRect = [owner visibleRect];
  83. NSRect bounds = [owner bounds];
  84. //retreive background image
  85. CGImageRef image = backgroundImage();
  86. float width = (float) CGImageGetWidth(image);
  87. float height = (float) CGImageGetHeight(image);
  88. //compute coordinates to fill the view
  89. float left, top, right, bottom;
  90. top = bounds.size.height - NSMaxY(visibleRect);
  91. top = fmod(top, height);
  92. top = height - top;
  93. right = NSMaxX(visibleRect);
  94. bottom = -height;
  95. // tile the image and take in account the offset to 'emulate' a scrolling background
  96. for (top = visibleRect.size.height-top; top>bottom; top -= height){
  97. for(left=0; left<right; left+=width){
  98. CGContextDrawImage(context, CGRectMake(left, top, width, height), image);
  99. }
  100. }
  101. }
  102. @end