source: trunk/Cocoa/F-Script Anywhere/Source/NJRLabeledImageCell.m@ 360

Last change on this file since 360 was 342, checked in by rchin, 17 years ago

Leopard compatibility changes:

  • Removed some icon caching code that was causing crashes (not sure we really needed those optimizations anyway -- doesn't appear to affect performance)
  • Added code to automatically try to add certificate to keychain, for new code signing behavior (replaces previous procmod nonesense).
  • Note that the enclosed public certificate is mine, and so it will need to be signed by me. In the case that someone else wants to distribute this binary, please replace Certficiate.cer with your own public certificate, and then make sure o code sign the binary after it is built.
File size: 6.0 KB
Line 
1//
2// NJRLabeledImageCell.m
3// HostLauncher
4//
5// Created by nicholas on Wed Aug 01 2001.
6// Copyright (c) 2001 Nicholas Riley. All rights reserved.
7//
8
9/*
10
11 F-Script Anywhere is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 F-Script Anywhere is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with F-Script Anywhere; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24
25*/
26
27#import "NJRLabeledImageCell.h"
28#import "NSString-NJRExtensions.h"
29
30const static float LEFT_PADDING = 3;
31
32@implementation NJRLabeledImageCell
33
34+ (NJRLabeledImageCell *)cell {
35 return [[[self alloc] init] autorelease];
36}
37
38- (void)dealloc {
39 [image release];
40 image = nil;
41 [super dealloc];
42}
43
44- copyWithZone:(NSZone *)zone {
45 NJRLabeledImageCell *cell = [super copyWithZone:zone];
46 cell->image = [image retain];
47 return cell;
48}
49
50- (void)setImage:(NSImage *)anImage {
51 if (anImage != image) {
52 [image release];
53 image = [anImage retain];
54 }
55}
56
57- (NSImage *)image {
58 return image;
59}
60
61- (NSRect)imageFrameForCellFrame:(NSRect)cellFrame {
62 if (image != nil) {
63 NSRect imageFrame;
64 imageFrame.size = [image size];
65 imageFrame.origin = cellFrame.origin;
66 imageFrame.origin.x += LEFT_PADDING;
67 imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
68 return imageFrame;
69 }
70 return NSZeroRect;
71}
72
73- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent {
74 NSRect textFrame, imageFrame;
75 NSDivideRect(aRect, &imageFrame, &textFrame,
76 LEFT_PADDING + [image size].width, NSMinXEdge);
77 [super editWithFrame: textFrame inView: controlView
78 editor: textObj delegate: anObject event: theEvent];
79}
80
81- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength {
82 NSRect textFrame, imageFrame;
83 NSDivideRect(aRect, &imageFrame, &textFrame, LEFT_PADDING + [image size].width,
84 NSMinXEdge);
85 [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength];
86}
87
88- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
89 if (image != nil) {
90 NSSize imageSize;
91 NSRect imageFrame;
92 NSImageRep *preferredRep, *rep;
93
94 imageSize = NSMakeSize(cellFrame.size.height - 1, cellFrame.size.height - 1);
95
96 NSArray *imageReps = [image representations];
97 int i, repCount = [imageReps count];
98 NSSize repSize;
99
100 preferredRep = [imageReps objectAtIndex: 0];
101 // FSALog(@"%@ rejecting cached image: %@", [self stringValue], preferredRep);
102 for (i = 1 ; i < repCount ; i++) {
103 rep = [imageReps objectAtIndex: i];
104 repSize = [rep size];
105 if (repSize.width == imageSize.width && repSize.height == imageSize.height) {
106 preferredRep = rep;
107 break;
108 }
109 if (repSize.width >= imageSize.width || repSize.height >= imageSize.height) {
110 // pick the smallest of the larger representations
111 if (repSize.width <= [preferredRep size].width ||
112 repSize.height <= [preferredRep size].height) preferredRep = rep;
113 } else {
114 // or the largest of the smaller representations
115 if (repSize.width >= [preferredRep size].width ||
116 repSize.height >= [preferredRep size].height) preferredRep = rep;
117 }
118 }
119 /* // Working code as of OS X 10.0.4; this breaks in 10.1
120 for (i = repCount - 1 ; i >= 0 ; i--) {
121 rep = [imageReps objectAtIndex: i];
122 if (rep != preferredRep) {
123 [image removeRepresentation: rep];
124 }
125 }
126 // End working code
127 */
128 // Begin workaround code for bug in OS X 10.1 (removeRepresentation: has no effect)
129 if ([preferredRep size].width > imageSize.width || [preferredRep size].height > imageSize.height) {
130 NSImage *scaledImage = [[NSImage alloc] initWithSize: imageSize];
131 NSRect rect = { NSZeroPoint, imageSize };
132 FSALog(@"rescaling %@", [self stringValue]);
133 [scaledImage setFlipped: [controlView isFlipped]]; // XXX this works, but is correct?
134 [scaledImage lockFocus];
135 [preferredRep drawInRect: rect];
136 [scaledImage unlockFocus];
137 [image release];
138 image = scaledImage;
139 } else if (repCount > 1) {
140 NSImage *sizedImage = [[NSImage alloc] initWithSize: [preferredRep size]];
141 [sizedImage addRepresentation: preferredRep];
142 [image release];
143 image = sizedImage;
144 }
145 // End workaround code
146
147 NSDivideRect(cellFrame, &imageFrame, &cellFrame,
148 LEFT_PADDING + imageSize.width, NSMinXEdge);
149 if ([self drawsBackground]) {
150 [[self backgroundColor] set];
151 NSRectFill(imageFrame);
152 }
153 imageFrame.origin.x += LEFT_PADDING;
154 imageFrame.size = imageSize;
155
156 imageFrame.origin.y += ceil((cellFrame.size.height +
157 ([controlView isFlipped] ? -1 : 1) *
158 imageFrame.size.height - 2) / 2);
159 if ([image isFlipped] != [controlView isFlipped])
160 [image setFlipped: [controlView isFlipped]];
161 [image drawInRect: imageFrame
162 fromRect: NSOffsetRect(imageFrame, -imageFrame.origin.x, -imageFrame.origin.y)
163 operation: NSCompositeSourceOver fraction: 1.0];
164 }
165 [self setAttributedStringValue:
166 [[self stringValue] asAttributedStringTruncatedToWidth: cellFrame.size.width - 4]];
167 [super drawWithFrame: cellFrame inView: controlView];
168}
169
170- (NSSize)cellSize {
171 NSSize cellSize = [super cellSize];
172 cellSize.width += (image ? [image size].width : 0) + LEFT_PADDING;
173 return cellSize;
174}
175
176@end
Note: See TracBrowser for help on using the repository browser.