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
|
/*
This file is part of HelpViewer (http://www.roard.com/helpviewer)
Copyright (C) 2003 Nicolas Roard (nicolas@roard.com)
2020-2021 Riccardo Mottola <rm@gnu.org>
This program 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 of the License, or
(at your option) any later version.
This program 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; if not, write to the
Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "NoteCell.h"
@implementation NoteCell
- (id) initWithTextView: (NSTextView*) textview
{
if ((self = [super init]))
{
_image = nil;
_note = nil;
_color = nil;
imageBorder = 4.0;
leadingMargin = 8.0;
trailingMargin = 4.0;
[self resizeWithTextView: textview];
}
return self;
}
- (void) setText: (NSMutableAttributedString*) text
{
ASSIGN (_note, text);
NSMutableParagraphStyle* paragraph = [NSMutableParagraphStyle new];
[paragraph setAlignment: NSLeftTextAlignment];
[paragraph setHeadIndent: leadingMargin];
[paragraph setFirstLineHeadIndent: leadingMargin];
[paragraph setTailIndent: -trailingMargin];
NSDictionary* attributes = [NSDictionary dictionaryWithObject: paragraph
forKey: NSParagraphStyleAttributeName];
[paragraph release];
[_note addAttributes: attributes range: NSMakeRange (0, [_note length])];
}
- (void) setImage: (NSImage*) img
{
ASSIGN (_image, img);
}
- (void) setColor: (NSColor*) color
{
ASSIGN (_color, color);
}
- (NSSize) cellSize
{
return _size;
}
- (void) resize: (id) sender
{
[self resizeWithTextView: [sender object]];
}
- (void) resizeWithTextView: (NSTextView*) textView
{
CGFloat height = 0.0;
CGFloat width = 0.0;
CGFloat textHeight = 0.0;
CGFloat imageWidth = 0.0;
if (_image)
{
imageWidth = [_image size].width;
height = [_image size].height;
width = imageWidth;
}
if (_note)
{
NSSize size;
size.width = [textView bounds].size.width - width;
size.width -= leadingMargin; // Take in account of the text margins inside the view
size.width -= leadingMargin*2;
size.height = 9999.0;
if (size.width <= 0)
size.width = [textView bounds].size.width;
noteSize = [_note boundingRectWithSize:size options:0].size;
NSLog(@"input %@ output %@", NSStringFromSize(size), NSStringFromSize(noteSize));
textHeight = noteSize.height;
if (textHeight > height)
height = textHeight;
width += size.width;
}
NSLog (@"NoteCell: width : %.2f height : %.2f", width, height);
_size = NSMakeSize (width, height);
}
- (void) drawWithFrame: (NSRect) cellFrame
inView: (NSView*) controlView
{
[super drawWithFrame: cellFrame inView: controlView];
}
- (void) drawInteriorWithFrame: (NSRect) cellFrame
inView: (NSView*) controlView
{
if (![controlView window])
return;
NSLog(@"draw width: %f", cellFrame.size.width);
[_color set];
NSBezierPath* path = [[NSBezierPath alloc] init];
CGFloat radius = 8;
NSPoint p1 = NSMakePoint (cellFrame.origin.x, cellFrame.origin.y + radius);
NSPoint p2 = NSMakePoint (cellFrame.origin.x, cellFrame.origin.y + cellFrame.size.height - radius);
NSPoint p4 = NSMakePoint (cellFrame.origin.x + cellFrame.size.width - radius, cellFrame.origin.y + cellFrame.size.height);
NSPoint p6 = NSMakePoint (cellFrame.origin.x + cellFrame.size.width, cellFrame.origin.y + radius);
NSPoint p8 = NSMakePoint (cellFrame.origin.x + radius, cellFrame.origin.y);
NSPoint pr1 = NSMakePoint (cellFrame.origin.x + radius, cellFrame.origin.y + cellFrame.size.height - radius);
NSPoint pr2 = NSMakePoint (cellFrame.origin.x + cellFrame.size.width - radius, cellFrame.origin.y + cellFrame.size.height - radius);
NSPoint pr3 = NSMakePoint (cellFrame.origin.x + cellFrame.size.width - radius, cellFrame.origin.y + radius);
NSPoint pr4 = NSMakePoint (cellFrame.origin.x + radius, cellFrame.origin.y + radius);
[path moveToPoint: p1];
[path lineToPoint: p2];
[path appendBezierPathWithArcWithCenter: pr1 radius: radius startAngle: 180 endAngle: 90 clockwise: YES];
[path lineToPoint: p4];
[path appendBezierPathWithArcWithCenter: pr2 radius: radius startAngle: 90 endAngle: 0 clockwise: YES];
[path lineToPoint: p6];
[path appendBezierPathWithArcWithCenter: pr3 radius: radius startAngle: 0 endAngle: 270 clockwise: YES];
[path lineToPoint: p8];
[path appendBezierPathWithArcWithCenter: pr4 radius: radius startAngle: 270 endAngle: 180 clockwise: YES];
[path fill];
[path release];
if (_note)
{
CGFloat imageWidth = 0.0;
CGFloat imageHeight = 0.0;
NSPoint imageOrigin = NSZeroPoint;
NSPoint noteOrigin = NSZeroPoint;
NSRect noteRect;
if (_image)
{
imageWidth = [_image size].width;
imageHeight = [_image size].height;
imageOrigin.x = cellFrame.origin.x;
imageOrigin.y = cellFrame.origin.y + cellFrame.size.height - (cellFrame.size.height - imageHeight)/2;
[_image compositeToPoint: imageOrigin operation: NSCompositeSourceAtop];
}
noteOrigin.x = cellFrame.origin.x + imageWidth;
noteOrigin.y = cellFrame.origin.y + (cellFrame.size.height - noteSize.height)/2;
noteRect.origin = noteOrigin;
noteRect.size = noteSize;
[_note drawInRect: noteRect];
}
}
@end
|