1/*
2
3  ZipperCell.m
4  Zipper
5
6  Copyright (C) 2012 Free Software Foundation, Inc
7
8  Authors: Dirk Olmes <dirk@xanthippe.ping.de>
9           Riccardo Mottola <rm@gnu.org>
10
11  This application is free software; you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by the Free
13  Software Foundation; either version 2 of the License, or (at your option)
14  any later version.
15
16  This program is distributed in the hope that it will be useful, but
17  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18  or FITNESS FOR A PARTICULAR PURPOSE.
19  See the GNU General Public License for more details
20
21 */
22
23#import <AppKit/AppKit.h>
24#import "ZipperCell.h"
25
26@implementation ZipperCell : NSTextFieldCell
27
28- (id) init
29{
30	self = [super initTextCell: @""];
31	return self;
32}
33
34/*
35 * drawInteriorWithFrame is copied from NSCell.m and NSTextFieldCell.m
36 * and modified to to display an image _and_ text.
37 */
38- (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
39{
40  NSAttributedString *attStrVal;
41
42  if ([self drawsBackground])
43    {
44      [[self backgroundColor] set];
45      NSRectFill ([self drawingRectForBounds: cellFrame]);
46    }
47  if (![controlView window])
48    {
49      return;
50    }
51
52  cellFrame = [self drawingRectForBounds: cellFrame];
53
54  //FIXME: Check if this is also neccessary for images,
55  // Add spacing between border and inside
56  if ([self isBordered] || [self isBezeled])
57    {
58      cellFrame.origin.x += 3;
59      cellFrame.size.width -= 6;
60      cellFrame.origin.y += 1;
61      cellFrame.size.height -= 2;
62    }
63
64  if ([self image])
65    {
66      NSSize size;
67      NSPoint position;
68
69      size = [[self image] size];
70      position.x = 0.;
71      position.y = MAX(NSMidY(cellFrame) - (size.height/2.),0.);
72      /*
73       * Images are always drawn with their bottom-left corner
74       * at the origin so we must adjust the position to take
75       * account of a flipped view.
76       */
77      if ([controlView isFlipped])
78	position.y += size.height;
79      [[self image] compositeToPoint: position operation: NSCompositeSourceOver];
80
81      cellFrame.origin.x += size.width+3;
82      cellFrame.size.width -= (size.width+3);
83    }
84
85  attStrVal = [self attributedStringValue];
86
87  if (attStrVal)
88    {
89      NSRect aRect;
90      NSSize stringSize;
91
92      stringSize = [attStrVal size];
93      aRect = cellFrame;
94      aRect.origin.y = NSMidY (aRect) - stringSize.height/2;
95      aRect.size.height = stringSize.height;
96
97      [attStrVal drawInRect: cellFrame];
98    }
99
100  if ([self showsFirstResponder])
101    {
102      NSDottedFrameRect(cellFrame);
103    }
104}
105
106@end
107