OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / macosx / HBImageAndTextCell.m
1 /* HBImageAndTextCell
2
3     This file is part of the HandBrake source code.
4     Homepage: <http://handbrake.fr/>.
5     It may be used under the terms of the GNU General Public License.
6 */
7
8
9 #import "HBImageAndTextCell.h"
10
11
12 static inline CGFloat
13 xLeftInRect(NSSize innerSize, NSRect outerRect)
14 {
15   return NSMinX(outerRect);
16 }
17
18 static inline CGFloat
19 xCenterInRect(NSSize innerSize, NSRect outerRect)
20 {
21   return MAX(NSMidX(outerRect) - (innerSize.width/2.0), 0.0);
22 }
23
24 static inline CGFloat
25 xRightInRect(NSSize innerSize, NSRect outerRect)
26 {
27   return MAX(NSMaxX(outerRect) - innerSize.width, 0.0);
28 }
29
30 static inline CGFloat
31 yTopInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
32 {
33   if (flipped)
34     return NSMinY(outerRect);
35   else
36     return MAX(NSMaxY(outerRect) - innerSize.height, 0.0);
37 }
38
39 static inline CGFloat
40 yCenterInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
41 {
42   return MAX(NSMidY(outerRect) - innerSize.height/2.0, 0.0);
43 }
44
45 static inline CGFloat
46 yBottomInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
47 {
48   if (flipped)
49     return MAX(NSMaxY(outerRect) - innerSize.height, 0.0);
50   else
51     return NSMinY(outerRect);
52 }
53
54 static inline NSSize
55 scaleProportionally(NSSize imageSize, NSRect canvasRect)
56 {
57   CGFloat ratio;
58
59   // get the smaller ratio and scale the image size by it
60   ratio = MIN(NSWidth(canvasRect) / imageSize.width,
61               NSHeight(canvasRect) / imageSize.height);
62
63   imageSize.width *= ratio;
64   imageSize.height *= ratio;
65
66   return imageSize;
67 }
68
69
70
71 @implementation HBImageAndTextCell
72
73 -(id)initTextCell:(NSString *)aString
74 {
75     if (self = [super initTextCell:aString])
76     {
77         imageAlignment = NSImageAlignTop;
78         imageSpacing = NSMakeSize (3.0, 2.0);
79     }
80     return self; 
81 }
82
83 -(id)initWithCoder:(NSCoder *)decoder
84 {
85     if (self = [super initWithCoder:decoder])
86     {
87         imageAlignment = NSImageAlignTop;
88         imageSpacing = NSMakeSize (3.0, 2.0);
89     }
90     return self; 
91 }
92
93 - (void)dealloc
94 {
95     [image release];
96     image = nil;
97     [super dealloc];
98 }
99
100 - copyWithZone:(NSZone *)zone
101 {
102     HBImageAndTextCell *cell = (HBImageAndTextCell *)[super copyWithZone:zone];
103     cell->image = [image retain];
104     return cell;
105 }
106
107 - (void)setImage:(NSImage *)anImage
108 {
109     if (anImage != image)
110     {
111         [image release];
112         image = [anImage retain];
113     }
114 }
115
116 - (NSImage *)image
117 {
118     return image;
119 }
120
121 - (void) setImageAlignment:(NSImageAlignment)alignment;
122 {
123     imageAlignment = alignment;
124 }
125
126 - (NSImageAlignment) imageAlignment;
127 {
128     return imageAlignment;
129 }
130
131 - (void)setImageSpacing:(NSSize)aSize;
132 {
133     imageSpacing = aSize;
134 }
135
136 - (NSSize)imageSpacing
137 {
138     return imageSpacing;
139 }
140
141 - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
142 {
143     NSRect textFrame, imageFrame;
144     NSDivideRect (aRect, &imageFrame, &textFrame, (imageSpacing.width * 2) + [image size].width, NSMinXEdge);
145     [super editWithFrame: textFrame inView: controlView editor:textObj delegate:anObject event: theEvent];
146 }
147
148 - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength
149 {
150     NSRect textFrame, imageFrame;
151     NSDivideRect (aRect, &imageFrame, &textFrame, (imageSpacing.width * 2) + [image size].width, NSMinXEdge);
152     [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength];
153 }
154
155 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
156 {
157 #if 1
158     if (image != nil)
159     {
160         NSSize imageSize;
161         NSRect imageFrame;
162
163         imageSize = [image size];
164         NSDivideRect(cellFrame, &imageFrame, &cellFrame, (imageSpacing.width * 2) + imageSize.width, NSMinXEdge);
165         if ([self drawsBackground])
166         {
167             [[self backgroundColor] set];
168             NSRectFill(imageFrame);
169         }
170         imageFrame.origin.x += imageSpacing.width;
171         imageFrame.size = imageSize;
172
173         switch (imageAlignment)
174         {
175             default:
176             case NSImageAlignTop:
177                 if ([controlView isFlipped])
178                     imageFrame.origin.y += imageFrame.size.height;
179                 else
180                     imageFrame.origin.y += (cellFrame.size.height - imageFrame.size.height);
181                 break;
182
183             case NSImageAlignCenter:
184                 if ([controlView isFlipped])
185                     imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
186                 else
187                     imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
188                 break;
189
190              case NSImageAlignBottom:
191                 if ([controlView isFlipped])
192                     imageFrame.origin.y += cellFrame.size.height;
193                 // for unflipped, imageFrame is already correct
194                 break;
195           
196         }
197         
198         [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
199     }
200
201     [super drawWithFrame:cellFrame inView:controlView];
202 #endif
203
204
205 #if 0 // this snippet supports all alignment values plus potentially scaling.
206     if (image != nil)
207     {
208         NSSize imageSize;
209         NSSize srcImageSize;
210         NSRect imageFrame;
211         NSPoint position;
212         BOOL flipped = [controlView isFlipped];
213
214         imageSize = [image size];
215         srcImageSize = imageSize;   // this will be more useful once/if we support scaling
216         
217         NSDivideRect(cellFrame, &imageFrame, &cellFrame, 12 + imageSize.width, NSMinXEdge);
218         if ([self drawsBackground])
219         {
220             [[self backgroundColor] set];
221             NSRectFill(imageFrame);
222         }
223         
224         switch (imageAlignment)
225         {
226             default:
227             case NSImageAlignLeft:
228                 position.x = xLeftInRect(imageSize, imageFrame);
229                 position.y = yCenterInRect(imageSize, imageFrame, flipped);
230                 break;
231             case NSImageAlignRight:
232                 position.x = xRightInRect(imageSize, imageFrame);
233                 position.y = yCenterInRect(imageSize, imageFrame, flipped);
234                 break;
235             case NSImageAlignCenter:
236                 position.x = xCenterInRect(imageSize, imageFrame);
237                 position.y = yCenterInRect(imageSize, imageFrame, flipped);
238                 break;
239             case NSImageAlignTop:
240                 position.x = xCenterInRect(imageSize, imageFrame);
241                 position.y = yTopInRect(imageSize, imageFrame, flipped);
242                 break;
243             case NSImageAlignBottom:
244                 position.x = xCenterInRect(imageSize, imageFrame);
245                 position.y = yBottomInRect(imageSize, imageFrame, flipped);
246                 break;
247             case NSImageAlignTopLeft:
248                 position.x = xLeftInRect(imageSize, imageFrame);
249                 position.y = yTopInRect(imageSize, imageFrame, flipped);
250                 break;
251             case NSImageAlignTopRight:
252                 position.x = xRightInRect(imageSize, imageFrame);
253                 position.y = yTopInRect(imageSize, imageFrame, flipped);
254                 break;
255             case NSImageAlignBottomLeft:
256                 position.x = xLeftInRect(imageSize, imageFrame);
257                 position.y = yBottomInRect(imageSize, imageFrame, flipped);
258                 break;
259             case NSImageAlignBottomRight:
260                 position.x = xRightInRect(imageSize, imageFrame);
261                 position.y = yBottomInRect(imageSize, imageFrame, flipped);
262                 break;
263         }
264
265         // account for flipped views
266         if (flipped)
267         {
268             position.y += imageSize.height;
269             imageSize.height = -imageSize.height;
270         }
271
272         // Set image flipping to match view. Don't know if this is really the best way
273         // to deal with flipped views and images.
274         if ([image isFlipped] != flipped)
275             [image setFlipped: flipped];
276
277         // draw!
278         [image drawInRect: NSMakeRect(position.x, position.y, imageSize.width, imageSize.height)
279             fromRect: NSMakeRect(0, 0, srcImageSize.width,
280             srcImageSize.height)
281             operation: NSCompositeSourceOver
282             fraction: 1.0];
283
284     }
285
286     [super drawWithFrame:cellFrame inView:controlView];
287 #endif
288 }
289
290 - (NSSize)cellSize
291 {
292     NSSize cellSize = [super cellSize];
293     cellSize.width += (image ? [image size].width + (imageSpacing.width * 2) : 0);
294     return cellSize;
295 }
296
297 @end
298