File: CynthiuneSongTitleCell.m

package info (click to toggle)
cynthiune.app 0.9.5-12
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,456 kB
  • ctags: 603
  • sloc: objc: 12,639; ansic: 497; cpp: 152; makefile: 141; sh: 17
file content (166 lines) | stat: -rw-r--r-- 3,938 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
/* CynthiuneSongTitleCell.m - this file is part of Cynthiune
 *
 * Copyright (C) 2005 Wolfgang Sourdeau
 *
 * Author: Wolfgang Sourdeau <Wolfgang@Contre.COM>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This file 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <AppKit/NSColor.h>
#import <AppKit/NSGraphics.h>
#import <AppKit/NSImage.h>
#import <Foundation/NSString.h>

#import <Cynthiune/NSCellExtensions.h>
#import <Cynthiune/utils.h>

#import "CynthiuneTextCell.h"
#import "CynthiuneSongTitleCell.h"

#define imageOffset 2.0

@implementation CynthiuneSongTitleCell : CynthiuneTextCell

- (id) init
{
  if ((self = [super init]))
    {
      songPointer = nil;
      imageSize = NSMakeSize (0.0, 0.0);
      imageShown = NO;
    }

  return self;
}

- (void) dealloc
{
  if (songPointer)
    [songPointer release];
}

- (void) setShowImage: (BOOL) showImage
{
  imageShown = showImage;
}

- (BOOL) showImage
{
  return imageShown;
}

- (void) setPointerImage: (NSImage *) pointerImage
{
  SET (songPointer, pointerImage);
  imageSize = ((songPointer) ? [songPointer size] : NSMakeSize (0.0, 0.0));
}

- (NSImage *) pointerImage
{
  return songPointer;
}

- (void) drawWithFrame: (NSRect) cellFrame 
		inView: (NSView *) controlView
{
  NSRect aFrame, imageFrame;

  NSDivideRect (cellFrame, &aFrame, &cellFrame,
                3.0 + imageOffset + imageSize.width, NSMinXEdge);

  imageFrame = aFrame;
  imageFrame.size = imageSize;
  imageFrame.size.width += 3.0 + imageOffset;

  if ([self drawsBackground])
    {
      [[self backgroundColor] set];
      NSRectFill (aFrame);
    }

  if ([self isHighlighted])
    { 
      [[self highlightColor] set];
      NSRectFill (aFrame);
    }

  if (imageShown)
    {
      aFrame.size = imageSize;
      aFrame.origin.x += imageOffset;
      aFrame.origin.y += ((cellFrame.size.height + 1.0
                           + ([controlView isFlipped]
                              ? aFrame.size.height
                              : -aFrame.size.height))
                          / 2);

      [songPointer compositeToPoint: aFrame.origin 
                   operation: NSCompositeSourceOver];
    }

  [super drawWithFrame: cellFrame inView: controlView];
}

- (NSSize) cellSize 
{
  NSSize aSize;

  aSize = [super cellSize];
  aSize.width += imageSize.width + imageOffset + 3.0;

  return aSize;
}

- (float) widthOfText: (NSString *) text
{
  return ([super widthOfText: text] + imageSize.width + imageOffset + 3.0);
}

/* NSCopying protocol */
- (id) copyWithZone: (NSZone *) theZone 
{
  CynthiuneSongTitleCell *newTitleCell;

  newTitleCell = [[CynthiuneSongTitleCell allocWithZone: theZone] init];
  [newTitleCell setPointerImage: songPointer];
  [newTitleCell setShowImage: imageShown];

  return newTitleCell;
}

/* NSCoding protocol */
- (void) encodeWithCoder: (NSCoder *) encoder
{
  [super encodeWithCoder: encoder];
  [encoder encodeBool: imageShown forKey: @"imageShown"];
  [encoder encodeObject: songPointer forKey: @"songPointer"];
}

- (id) initWithCoder: (NSCoder *) decoder
{
  if ((self = [super initWithCoder: decoder]))
    {
      imageShown = [decoder decodeBoolForKey: @"imageShown"];
      songPointer = [decoder decodeObjectForKey: @"songPointer"];
      if (songPointer)
        [songPointer retain];
    }

  return self;
}

@end