File: NSImageCell.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (350 lines) | stat: -rw-r--r-- 9,132 bytes parent folder | download | duplicates (4)
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/** <title>NSImageCell</title>

   <abstract>The image cell class</abstract>

   Copyright (C) 1999, 2005 Free Software Foundation, Inc.

   Author: Jonathan Gapen <jagapen@smithlab.chem.wisc.edu>
   Date: 1999

   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/

#include "config.h"
#import <Foundation/NSDebug.h>
#import "AppKit/NSAffineTransform.h"
#import "AppKit/NSCell.h"
#import "AppKit/NSGraphics.h"
#import "AppKit/NSImageCell.h"
#import "AppKit/NSImage.h"
#import "GNUstepGUI/GSTheme.h"
#import "GSGuiPrivate.h"

@interface NSCell (Private)
- (NSSize) _scaleImageWithSize: (NSSize)imageSize
                   toFitInSize: (NSSize)canvasSize
                   scalingType: (NSImageScaling)scalingType;
@end

@implementation NSImageCell

//
// Class methods
//
+ (void) initialize
{
  if (self == [NSImageCell class])
    {
      [self setVersion: 1];
    }
}

//
// Instance methods
//
- (id) init
{
  return [self initImageCell: nil];
}

- (void) setImage:(NSImage *)anImage
{
  [super setImage:anImage];
  if (anImage)
    _original_image_size = [anImage size];
  else
    _original_image_size = NSMakeSize(1,1);
}

- (void)setObjectValue:(id)object
{
  if ((object == nil) || ([object isKindOfClass:[NSImage class]]))
    {
      [self setImage: object];
    }
  else
    {
      [super setObjectValue: object];
    }
}

//
// Aligning and scaling the image
//
- (NSImageAlignment) imageAlignment
{
  return _imageAlignment;
}

- (void) setImageAlignment: (NSImageAlignment)anAlignment
{
  NSDebugLLog(@"NSImageCell", @"NSImageCell -setImageAlignment");
  _imageAlignment = anAlignment;
}

- (NSImageScaling) imageScaling
{
  return _imageScaling;
}

- (void) setImageScaling: (NSImageScaling)scaling
{
  _imageScaling = scaling;
}

//
// Choosing the frame
//
- (NSImageFrameStyle) imageFrameStyle
{
  return _frameStyle;
}

- (void) setImageFrameStyle: (NSImageFrameStyle)aFrameStyle
{
  // We could set _cell.is_bordered and _cell.is_bezeled here to
  // reflect the border type, but this wont be used.
  _frameStyle = aFrameStyle;
}

//
// Displaying
//
- (void) _drawBorderAndBackgroundWithFrame: (NSRect)cellFrame 
                                    inView: (NSView *)controlView
{
  NSDebugLLog(@"NSImageCell", @"NSImageCell -_drawBorderAndBackgroundWithFrame");
  [[GSTheme theme] drawBorderForImageFrameStyle: _frameStyle 
                   frame: cellFrame 
                   view: controlView];
}

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);
}

- (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView
{
  NSPoint	position;
  BOOL		is_flipped = [controlView isFlipped];
  NSSize  imageSize, realImageSize;
  NSRect rect;

  NSDebugLLog(@"NSImageCell", @"NSImageCell drawInteriorWithFrame called");

  if (!_cell_image)
    return;

  // leave room for the frame
  cellFrame = [self drawingRectForBounds: cellFrame];

  realImageSize = [_cell_image size];
  imageSize = [self _scaleImageWithSize: realImageSize
			    toFitInSize: cellFrame.size
			    scalingType: _imageScaling];

  switch (_imageAlignment)
    {
      default:
      case NSImageAlignLeft:
        position.x = xLeftInRect(imageSize, cellFrame);
        position.y = yCenterInRect(imageSize, cellFrame, is_flipped);
        break;
      case NSImageAlignRight:
        position.x = xRightInRect(imageSize, cellFrame);
        position.y = yCenterInRect(imageSize, cellFrame, is_flipped);
        break;
      case NSImageAlignCenter:
        position.x = xCenterInRect(imageSize, cellFrame);
        position.y = yCenterInRect(imageSize, cellFrame, is_flipped);
        break;
      case NSImageAlignTop:
        position.x = xCenterInRect(imageSize, cellFrame);
        position.y = yTopInRect(imageSize, cellFrame, is_flipped);
        break;
      case NSImageAlignBottom:
        position.x = xCenterInRect(imageSize, cellFrame);
        position.y = yBottomInRect(imageSize, cellFrame, is_flipped);
        break;
      case NSImageAlignTopLeft:
        position.x = xLeftInRect(imageSize, cellFrame);
        position.y = yTopInRect(imageSize, cellFrame, is_flipped);
        break;
      case NSImageAlignTopRight:
        position.x = xRightInRect(imageSize, cellFrame);
        position.y = yTopInRect(imageSize, cellFrame, is_flipped);
        break;
      case NSImageAlignBottomLeft:
        position.x = xLeftInRect(imageSize, cellFrame);
        position.y = yBottomInRect(imageSize, cellFrame, is_flipped);
        break;
      case NSImageAlignBottomRight:
        position.x = xRightInRect(imageSize, cellFrame);
        position.y = yBottomInRect(imageSize, cellFrame, is_flipped);
        break;
    }

  rect = NSMakeRect(position.x, position.y,
		    imageSize.width, imageSize.height);

  if (nil != controlView)
    {
      rect = [controlView centerScanRect: rect];
    }

  CGFloat fraction = 1.0;

  if (_frameStyle == NSImageFrameNone
      && ![self isEnabled])
    {
      fraction = 0.5;
    }

  // draw!
  [_cell_image drawInRect: rect
               fromRect: NSMakeRect(0, 0, realImageSize.width,
                                    realImageSize.height)
               operation: NSCompositeSourceOver
               fraction: fraction
	   respectFlipped: YES
		    hints: nil];

}

- (NSSize) cellSize
{
  NSSize borderSize, s;
  
  // Get border size
  borderSize = [[GSTheme theme] sizeForImageFrameStyle: _frameStyle];
  
  // Get Content Size
  s = _original_image_size;
  
  // Add in border size
  s.width += 2 * borderSize.width;
  s.height += 2 * borderSize.height;
  
  return s;
}

- (NSRect) drawingRectForBounds: (NSRect)theRect
{
  NSSize borderSize;

  // Get border size
  borderSize = [[GSTheme theme] sizeForImageFrameStyle: _frameStyle];
  return NSInsetRect (theRect, borderSize.width, borderSize.height);
}

//
// NSCoding protocol
//
- (void) encodeWithCoder: (NSCoder *)aCoder
{
  [super encodeWithCoder: aCoder];
  if ([aCoder allowsKeyedCoding])
    {
      [aCoder encodeInt: _imageAlignment forKey: @"NSAlign"];
      [aCoder encodeInt: _imageScaling forKey: @"NSScale"];
      [aCoder encodeInt: _frameStyle forKey: @"NSStyle"];
      [aCoder encodeBool: NO forKey: @"NSAnimates"];
    }
  else
    {
      [aCoder encodeValueOfObjCType: @encode(int) at: &_imageAlignment];
      [aCoder encodeValueOfObjCType: @encode(int) at: &_frameStyle];
      [aCoder encodeValueOfObjCType: @encode(int) at: &_imageScaling];
      [aCoder encodeSize: _original_image_size];
    }
}

- (id) initWithCoder: (NSCoder *)aDecoder
{
  if ((self = [super initWithCoder: aDecoder]) != nil)
    {
      if ([aDecoder allowsKeyedCoding])
	{
	  if ([aDecoder containsValueForKey: @"NSAlign"])
	    {
	      [self setImageAlignment: [aDecoder decodeIntForKey: @"NSAlign"]];
	    }
	  if ([aDecoder containsValueForKey: @"NSScale"])
	    {
	      [self setImageScaling: [aDecoder decodeIntForKey: @"NSScale"]];
	    }
	  if ([aDecoder containsValueForKey: @"NSStyle"])
	    {
	      [self setImageFrameStyle: [aDecoder decodeIntForKey: @"NSStyle"]];
	    }
	  if ([aDecoder containsValueForKey: @"NSAnimates"])
	    {
	      //BOOL animates = [aDecoder decodeBoolForKey: @"NSAnimates"];
	    }
	}
      else
	{
	  [aDecoder decodeValueOfObjCType: @encode(int) at: &_imageAlignment];
	  [aDecoder decodeValueOfObjCType: @encode(int) at: &_frameStyle];
	  [aDecoder decodeValueOfObjCType: @encode(int) at: &_imageScaling];
	  _original_image_size = [aDecoder decodeSize];
	}
    }
  return self;
}

@end