summaryrefslogtreecommitdiffstats
path: root/macosx/HBImageAndTextCell.m
blob: 2fe1ea97579bececd21c991128fe0c6e9f043a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/* HBImageAndTextCell

    This file is part of the HandBrake source code.
    Homepage: <http://handbrake.fr/>.
    It may be used under the terms of the GNU General Public License.
*/


#import "HBImageAndTextCell.h"


static inline float
xLeftInRect(NSSize innerSize, NSRect outerRect)
{
  return NSMinX(outerRect);
}

static inline float
xCenterInRect(NSSize innerSize, NSRect outerRect)
{
  return MAX(NSMidX(outerRect) - (innerSize.width/2.0), 0.0);
}

static inline float
xRightInRect(NSSize innerSize, NSRect outerRect)
{
  return MAX(NSMaxX(outerRect) - innerSize.width, 0.0);
}

static inline float
yTopInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
{
  if (flipped)
    return NSMinY(outerRect);
  else
    return MAX(NSMaxY(outerRect) - innerSize.height, 0.0);
}

static inline float
yCenterInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
{
  return MAX(NSMidY(outerRect) - innerSize.height/2.0, 0.0);
}

static inline float
yBottomInRect(NSSize innerSize, NSRect outerRect, BOOL flipped)
{
  if (flipped)
    return MAX(NSMaxY(outerRect) - innerSize.height, 0.0);
  else
    return NSMinY(outerRect);
}

static inline NSSize
scaleProportionally(NSSize imageSize, NSRect canvasRect)
{
  float ratio;

  // get the smaller ratio and scale the image size by it
  ratio = MIN(NSWidth(canvasRect) / imageSize.width,
	      NSHeight(canvasRect) / imageSize.height);

  imageSize.width *= ratio;
  imageSize.height *= ratio;

  return imageSize;
}



@implementation HBImageAndTextCell

-(id)initTextCell:(NSString *)aString
{
    if (self = [super initTextCell:aString])
    {
        imageAlignment = NSImageAlignTop;
        imageSpacing = NSMakeSize (3.0, 2.0);
    }
    return self; 
}

-(id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder])
    {
        imageAlignment = NSImageAlignTop;
        imageSpacing = NSMakeSize (3.0, 2.0);
    }
    return self; 
}

- (void)dealloc
{
    [image release];
    image = nil;
    [super dealloc];
}

- copyWithZone:(NSZone *)zone
{
    HBImageAndTextCell *cell = (HBImageAndTextCell *)[super copyWithZone:zone];
    cell->image = [image retain];
    return cell;
}

- (void)setImage:(NSImage *)anImage
{
    if (anImage != image)
    {
        [image release];
        image = [anImage retain];
    }
}

- (NSImage *)image
{
    return image;
}

- (void) setImageAlignment:(NSImageAlignment)alignment;
{
    imageAlignment = alignment;
}

- (NSImageAlignment) imageAlignment;
{
    return imageAlignment;
}

- (void)setImageSpacing:(NSSize)aSize;
{
    imageSpacing = aSize;
}

- (NSSize)imageSpacing
{
    return imageSpacing;
}

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
{
    NSRect textFrame, imageFrame;
    NSDivideRect (aRect, &imageFrame, &textFrame, (imageSpacing.width * 2) + [image size].width, NSMinXEdge);
    [super editWithFrame: textFrame inView: controlView editor:textObj delegate:anObject event: theEvent];
}

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength
{
    NSRect textFrame, imageFrame;
    NSDivideRect (aRect, &imageFrame, &textFrame, (imageSpacing.width * 2) + [image size].width, NSMinXEdge);
    [super selectWithFrame: textFrame inView: controlView editor:textObj delegate:anObject start:selStart length:selLength];
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
#if 1
    if (image != nil)
    {
        NSSize imageSize;
        NSRect imageFrame;

        imageSize = [image size];
        NSDivideRect(cellFrame, &imageFrame, &cellFrame, (imageSpacing.width * 2) + imageSize.width, NSMinXEdge);
        if ([self drawsBackground])
        {
            [[self backgroundColor] set];
            NSRectFill(imageFrame);
        }
        imageFrame.origin.x += imageSpacing.width;
        imageFrame.size = imageSize;

        switch (imageAlignment)
        {
            default:
            case NSImageAlignTop:
                if ([controlView isFlipped])
                    imageFrame.origin.y += imageFrame.size.height;
                else
                    imageFrame.origin.y += (cellFrame.size.height - imageFrame.size.height);
                break;

            case NSImageAlignCenter:
                if ([controlView isFlipped])
                    imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
                else
                    imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
                break;

             case NSImageAlignBottom:
                if ([controlView isFlipped])
                    imageFrame.origin.y += cellFrame.size.height;
                // for unflipped, imageFrame is already correct
                break;
          
        }
        
        [image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
    }

    [super drawWithFrame:cellFrame inView:controlView];
#endif


#if 0 // this snippet supports all alignment values plus potentially scaling.
    if (image != nil)
    {
        NSSize imageSize;
        NSSize srcImageSize;
        NSRect imageFrame;
        NSPoint	position;
        BOOL flipped = [controlView isFlipped];

        imageSize = [image size];
        srcImageSize = imageSize;   // this will be more useful once/if we support scaling
        
        NSDivideRect(cellFrame, &imageFrame, &cellFrame, 12 + imageSize.width, NSMinXEdge);
        if ([self drawsBackground])
        {
            [[self backgroundColor] set];
            NSRectFill(imageFrame);
        }
        
        switch (imageAlignment)
        {
            default:
            case NSImageAlignLeft:
                position.x = xLeftInRect(imageSize, imageFrame);
                position.y = yCenterInRect(imageSize, imageFrame, flipped);
                break;
            case NSImageAlignRight:
                position.x = xRightInRect(imageSize, imageFrame);
                position.y = yCenterInRect(imageSize, imageFrame, flipped);
                break;
            case NSImageAlignCenter:
                position.x = xCenterInRect(imageSize, imageFrame);
                position.y = yCenterInRect(imageSize, imageFrame, flipped);
                break;
            case NSImageAlignTop:
                position.x = xCenterInRect(imageSize, imageFrame);
                position.y = yTopInRect(imageSize, imageFrame, flipped);
                break;
            case NSImageAlignBottom:
                position.x = xCenterInRect(imageSize, imageFrame);
                position.y = yBottomInRect(imageSize, imageFrame, flipped);
                break;
            case NSImageAlignTopLeft:
                position.x = xLeftInRect(imageSize, imageFrame);
                position.y = yTopInRect(imageSize, imageFrame, flipped);
                break;
            case NSImageAlignTopRight:
                position.x = xRightInRect(imageSize, imageFrame);
                position.y = yTopInRect(imageSize, imageFrame, flipped);
                break;
            case NSImageAlignBottomLeft:
                position.x = xLeftInRect(imageSize, imageFrame);
                position.y = yBottomInRect(imageSize, imageFrame, flipped);
                break;
            case NSImageAlignBottomRight:
                position.x = xRightInRect(imageSize, imageFrame);
                position.y = yBottomInRect(imageSize, imageFrame, flipped);
                break;
        }

        // account for flipped views
        if (flipped)
        {
            position.y += imageSize.height;
            imageSize.height = -imageSize.height;
        }

        // Set image flipping to match view. Don't know if this is really the best way
        // to deal with flipped views and images.
        if ([image isFlipped] != flipped)
            [image setFlipped: flipped];

        // draw!
        [image drawInRect: NSMakeRect(position.x, position.y, imageSize.width, imageSize.height)
            fromRect: NSMakeRect(0, 0, srcImageSize.width,
            srcImageSize.height)
            operation: NSCompositeSourceOver
            fraction: 1.0];

    }

    [super drawWithFrame:cellFrame inView:controlView];
#endif
}

- (NSSize)cellSize
{
    NSSize cellSize = [super cellSize];
    cellSize.width += (image ? [image size].width + (imageSpacing.width * 2) : 0);
    return cellSize;
}

@end