SKTextWithIconCell.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // SKTextWithIconCell.m
  3. // Skim
  4. //
  5. // Created by Christiaan Hofman on 9/13/07.
  6. /*
  7. This software is Copyright (c) 2007-2018
  8. Christiaan Hofman. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. - Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. - Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in
  16. the documentation and/or other materials provided with the
  17. distribution.
  18. - Neither the name of Christiaan Hofman nor the names of any
  19. contributors may be used to endorse or promote products derived
  20. from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #import "SKTextWithIconCell.h"
  34. #import "SKDictionaryFormatter.h"
  35. #import "NSImage_SKExtensions.h"
  36. //#import "NSGeometry_SKExtensions.h"
  37. //#import "NSEvent_SKExtensions.h"
  38. NSString *SKTextWithIconStringKey = @"string";
  39. NSString *SKTextWithIconImageKey = @"image";
  40. #define BORDER_BETWEEN_EDGE_AND_IMAGE (2.0)
  41. #define BORDER_BETWEEN_IMAGE_AND_TEXT (2.0)
  42. #define IMAGE_OFFSET (1.0)
  43. @implementation SKTextWithIconCell
  44. NSRect SKCenterRectVertically(NSRect rect, CGFloat height, CGFloat offset, BOOL flipped) {
  45. rect.origin.y += 0.5 * (NSHeight(rect) - height);
  46. rect.origin.y = flipped ? ceil(rect.origin.y) - offset : floor(rect.origin.y) + offset;
  47. rect.size.height = height;
  48. return rect;
  49. }
  50. static inline NSRect SKSliceRect(NSRect rect, CGFloat amount, NSRectEdge edge) {
  51. NSRect ignored;
  52. NSDivideRect(rect, &rect, &ignored, amount, edge);
  53. return rect;
  54. }
  55. static inline NSRect SKShrinkRect(NSRect rect, CGFloat amount, NSRectEdge edge) {
  56. NSRect ignored;
  57. NSDivideRect(rect, &ignored, &rect, amount, edge);
  58. return rect;
  59. }
  60. static SKDictionaryFormatter *textWithIconCellFormatter = nil;
  61. + (void)initialize {
  62. // SKINITIALIZE;
  63. textWithIconCellFormatter = [[SKDictionaryFormatter alloc] initWithKey:SKTextWithIconStringKey];
  64. }
  65. - (void)commonInit {
  66. imageCell = [[NSImageCell alloc] init];
  67. [imageCell setImageScaling:NSImageScaleProportionallyUpOrDown];
  68. if ([self formatter] == nil)
  69. [self setFormatter:textWithIconCellFormatter];
  70. }
  71. - (id)initTextCell:(NSString *)aString {
  72. self = [super initTextCell:aString];
  73. if (self) {
  74. [self commonInit];
  75. }
  76. return self;
  77. }
  78. - (id)initWithCoder:(NSCoder *)decoder {
  79. self = [super initWithCoder:decoder];
  80. if (self) {
  81. [self commonInit];
  82. }
  83. return self;
  84. }
  85. - (id)copyWithZone:(NSZone *)zone {
  86. SKTextWithIconCell *copy = [super copyWithZone:zone];
  87. copy->imageCell = [imageCell copyWithZone:zone];
  88. return copy;
  89. }
  90. - (void)dealloc {
  91. // SKDESTROY(imageCell);
  92. // [super dealloc];
  93. }
  94. - (NSSize)cellSizeForBounds:(NSRect)aRect {
  95. NSSize cellSize = [super cellSizeForBounds:aRect];
  96. cellSize.width += cellSize.height - 1 + BORDER_BETWEEN_EDGE_AND_IMAGE + BORDER_BETWEEN_IMAGE_AND_TEXT;
  97. cellSize.width = fmin(cellSize.width, NSWidth(aRect));
  98. return cellSize;
  99. }
  100. - (NSRect)textRectForBounds:(NSRect)aRect {
  101. return SKShrinkRect(aRect, NSHeight(aRect) - 1 + BORDER_BETWEEN_EDGE_AND_IMAGE + BORDER_BETWEEN_IMAGE_AND_TEXT, NSMinXEdge);
  102. }
  103. - (NSRect)iconRectForBounds:(NSRect)aRect {
  104. return SKSliceRect(SKShrinkRect(aRect, BORDER_BETWEEN_EDGE_AND_IMAGE, NSMinXEdge), NSHeight(aRect) - 1, NSMinXEdge);
  105. }
  106. - (void)drawInteriorWithFrame:(NSRect)aRect inView:(NSView *)controlView {
  107. // let super draw the text
  108. NSRect textRect = [self textRectForBounds:aRect];
  109. [super drawInteriorWithFrame:textRect inView:controlView];
  110. // Draw the image
  111. NSRect imageRect = [self iconRectForBounds:aRect];
  112. imageRect = SKCenterRectVertically(imageRect, NSWidth(imageRect), IMAGE_OFFSET, [controlView isFlipped]);
  113. [imageCell drawInteriorWithFrame:imageRect inView:controlView];
  114. }
  115. - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
  116. [super editWithFrame:[self textRectForBounds:aRect] inView:controlView editor:textObj delegate:anObject event:theEvent];
  117. }
  118. - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength {
  119. [super selectWithFrame:[self textRectForBounds:aRect] inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
  120. }
  121. //#if SDK_BEFORE(10_10)
  122. //#define NSCellHitResult NSUInteger
  123. //#endif
  124. - (NSCellHitResult)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
  125. NSRect textRect = [self textRectForBounds:cellFrame];
  126. NSPoint mouseLoc = [controlView convertPoint:[event locationInWindow] fromView:nil];
  127. NSCellHitResult hit = NSCellHitNone;
  128. if (NSMouseInRect(mouseLoc, textRect, [controlView isFlipped]))
  129. hit = [super hitTestForEvent:event inRect:textRect ofView:controlView];
  130. else if (NSMouseInRect(mouseLoc, [self iconRectForBounds:cellFrame], [controlView isFlipped]))
  131. hit = NSCellHitContentArea;
  132. return hit;
  133. }
  134. //#if SDK_BEFORE(10_10)
  135. //#undef NSCellHitResult
  136. //#endif
  137. - (void)setObjectValue:(id <NSCopying>)obj {
  138. [super setObjectValue:obj];
  139. if ([(id)obj respondsToSelector:@selector(objectForKey:)]) {
  140. [imageCell setImage:[(id)obj objectForKey:SKTextWithIconImageKey]];
  141. self.stringValue = [(id)obj objectForKey:SKTextWithIconStringKey];
  142. }
  143. }
  144. - (NSImage *)icon {
  145. return [imageCell image];
  146. }
  147. @end