File: FigureCell.m

package info (click to toggle)
helpviewer.app 0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,940 kB
  • sloc: objc: 2,416; makefile: 12
file content (307 lines) | stat: -rw-r--r-- 10,909 bytes parent folder | download
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
/*
    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
*/

#import "FigureCell.h"

@implementation FigureCell

- (id) initWithSize: (NSSize) size 
{
  if ((self = [super init]))
    {
      _size = size;
      _image = nil;
      _legends = nil;
      border = 12.0;
      spaceMargin = 20.0;
    }
  return self;    
}

- (void) setLegends: (NSArray*) legends
{
    ASSIGN (_legends, legends);
}

- (void) setImage:  (NSImage*) img
{
    ASSIGN (_image, img);
}

- (NSSize) cellSize 
{
    return _size;
}

- (void) resize: (id) sender
{
	[self resizeWithTextView: [sender object]];
}

- (void) resizeWithTextView: (NSTextView*) textView
{
  CGFloat width = 0.0;
  CGFloat minimalHeight;
  CGFloat imageWidth = [_image size].width;
  CGFloat imageHeight = [_image size].height;   

  width = [textView bounds].size.width - -2*spaceMargin - 2*border;
    if (_legends)
    {
	      NSUInteger i;
	      CGFloat margin = (width - imageWidth)/2;

	      NSMutableParagraphStyle* paragraph = [NSMutableParagraphStyle new];
	      [paragraph setAlignment: NSLeftTextAlignment];
	      [paragraph setTailIndent: margin];
	      [paragraph setHeadIndent: 0.0];
	      [paragraph setFirstLineHeadIndent: 0.0];

	      NSDictionary* attributes = [NSDictionary dictionaryWithObject: paragraph 
		    forKey: NSParagraphStyleAttributeName];
	      [paragraph release];

	      NSMutableArray* preLeftLegends = [NSMutableArray array];
	      NSMutableArray* preRightLegends = [NSMutableArray array];
	      
	      for (i=0; i<[_legends count]; i++)
	      {
		  Legend* current = [_legends objectAtIndex: i];
		  [[current legend] addAttributes: attributes 
			range: NSMakeRange (0, [[current legend] length])];
		  NSSize s = [[current legend] size];

		  [current setHeight: s.height];
		  if ([current point].x > (imageWidth/2))
		  {
		    [current setRightPos]; // on range  droite
		    [preRightLegends addObject: current];
		  }
		  else
		  {
		    [preLeftLegends addObject: current];
		  }        
	      }
		    
	      NSArray* leftLegends = [preLeftLegends sortedArrayUsingSelector: @selector (compareWith:)];
	      NSArray* rightLegends = [preRightLegends sortedArrayUsingSelector: @selector (compareWith:)];

	      CGFloat leftLegendsHeight = 0;
	      CGFloat rightLegendsHeight = 0;
	      
	      for (i=0; i < [leftLegends count]; i++)
		{
		  leftLegendsHeight += [[leftLegends objectAtIndex: i] height];
		}

	      for (i=0; i < [rightLegends count]; i++)
		{
		  rightLegendsHeight += [[rightLegends objectAtIndex: i] height];
		}
	      
	      CGFloat minimalInterspace = 8;

	      minimalHeight = leftLegendsHeight+([leftLegends count]+1)*minimalInterspace;

	      if (imageHeight < minimalHeight)
		imageHeight = minimalHeight;
    }

    _size = NSMakeSize (width, imageHeight);
}

- (void) drawLegends: (NSArray*) legends onRight: (BOOL) right 
    withInterspace: (CGFloat) interspace withAttributes: (NSDictionary*) attributes
    withFrame: (NSRect) cellFrame withBorder: (CGFloat) aBorder withMargin: (CGFloat) Margin
    withSpaceMargin: (CGFloat) aSpaceMargin withImageWidth: (CGFloat) imageWidth withPosImage: (CGFloat) posImage
{
  CGFloat posY = interspace;
  NSUInteger i;
  for (i = 0; i < [legends count]; i++)
    {
      NSRect r;
      NSPoint t, tp, p;
      Legend* current = [legends objectAtIndex: i];
      [[current legend] addAttributes: attributes
				range: NSMakeRange (0, [[current legend] length])];
      NSSize s = [[current legend] size];
      //      NSSize s = [[current legend] sizeWithAttributes: attributes];

      if (!right)
	{
	  r  = NSMakeRect (cellFrame.origin.x + aBorder + (Margin-s.width) - 2, cellFrame.origin.y + posY - 2, s.width + 4, s.height + 4);
	  t  = NSMakePoint (cellFrame.origin.x + aBorder + (Margin-s.width), cellFrame.origin.y + posY);
	  tp = NSMakePoint (cellFrame.origin.x + aBorder + Margin + 2, cellFrame.origin.y + posY + s.height/2);
	}
      else
	{
	  r  = NSMakeRect (cellFrame.origin.x + aBorder + Margin + (2*aSpaceMargin) + imageWidth - 2,
			   cellFrame.origin.y + posY - 2, s.width + 4, s.height + 4);
	  t  = NSMakePoint (cellFrame.origin.x + aBorder + Margin + (2*aSpaceMargin) + imageWidth,
			    cellFrame.origin.y + posY);
	  tp = NSMakePoint (cellFrame.origin.x + aBorder + Margin + (2*aSpaceMargin) + imageWidth - 2,
			    cellFrame.origin.y + posY + s.height/2);
	}
          
      p  = NSMakePoint (cellFrame.origin.x + aBorder + Margin + aSpaceMargin + [current point].x,
			cellFrame.origin.y + [current point].y + posImage);
          
      // [[NSBezierPath bezierPathWithRect: r] stroke];
      [[NSColor colorWithCalibratedRed: 0.81 green: 0.84 blue: 0.88 alpha:1.0] set];
      NSBezierPath* path = [[NSBezierPath alloc] init];

      CGFloat radius = 8;

      NSPoint p1 = NSMakePoint (r.origin.x, r.origin.y + radius);
      NSPoint p2 = NSMakePoint (r.origin.x, r.origin.y + r.size.height - radius);
      NSPoint p4 = NSMakePoint (r.origin.x + r.size.width - radius, r.origin.y + r.size.height);
      NSPoint p6 = NSMakePoint (r.origin.x + r.size.width, r.origin.y + radius);
      NSPoint p8 = NSMakePoint (r.origin.x + radius, r.origin.y);

      NSPoint pr1 = NSMakePoint (r.origin.x + radius, r.origin.y + r.size.height - radius);
      NSPoint pr2 = NSMakePoint (r.origin.x + r.size.width - radius, r.origin.y + r.size.height - radius);
      NSPoint pr3 = NSMakePoint (r.origin.x + r.size.width - radius, r.origin.y + radius);
      NSPoint pr4 = NSMakePoint (r.origin.x + radius, r.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 appendBezierPathWithArcFromPoint: p8 toPoint: p1 radius: radius];
      [path appendBezierPathWithArcWithCenter: pr4 radius: radius startAngle: 270 endAngle: 180 clockwise: YES];
      [path fill];
      [path release];

//    [[current legend] drawAtPoint: t withAttributes: attributes];
      [[current legend] drawAtPoint: t];

      NSBezierPath* path2 = [NSBezierPath bezierPath];
          
      [path2 moveToPoint: tp];
      [path2 lineToPoint: p];
      [path2 stroke];
          
      posY += s.height + interspace;
    }
}


- (void) drawWithFrame: (NSRect) cellFrame
    inView: (NSView*) controlView
{
  if (![controlView window])
    return;

	[[NSColor whiteColor] set];
	NSRectFill (cellFrame);

      //NSLog (@"drawInteriorWithFrame de FigureCell ... ");
      //NSLog (@"image : %@", _image);
      //NSLog (@"cellframe origin x : %.2f origin y : %.2f", cellFrame.origin.x, cellFrame.origin.y);
      NSLog (@"cellFrame height : %.2f", cellFrame.size.height);

	if (_legends)
	{
		  
	      NSUInteger i;
	      CGFloat interspace = 20.0;
	      CGFloat imageWidth = [_image size].width;
	      CGFloat imageHeight = [_image size].height;   
	      CGFloat margin = (cellFrame.size.width - imageWidth - 2*spaceMargin - 2*border)/2;

	      CGFloat posImage = 0.0;
	      if (imageHeight < cellFrame.size.height)
	      {
	      	posImage = (cellFrame.size.height - imageHeight)/2;
	      }
	      NSPoint imageOrigin = NSMakePoint (cellFrame.origin.x + border + margin + spaceMargin,
		      cellFrame.origin.y + imageHeight + posImage);
	      [_image compositeToPoint: imageOrigin operation: NSCompositeSourceAtop];
	      
	      NSMutableParagraphStyle* paragraph = [NSMutableParagraphStyle new];
	      [paragraph setAlignment: NSLeftTextAlignment];
	      [paragraph setTailIndent: margin];
	      [paragraph setHeadIndent: 0.0];
	      [paragraph setFirstLineHeadIndent: 0.0];
	      
	      NSDictionary* attributes = [NSDictionary dictionaryWithObject: paragraph 
		    forKey: NSParagraphStyleAttributeName];
	      [paragraph release];

	      NSMutableArray* preLeftLegends = [NSMutableArray array];
	      NSMutableArray* preRightLegends = [NSMutableArray array];
	      
	      for (i=0; i<[_legends count]; i++)
	      {
		  Legend* current = [_legends objectAtIndex: i];
		  [[current legend] addAttributes: attributes 
			range: NSMakeRange (0, [[current legend] length])];
		  NSSize s = [[current legend] size];

		  [current setHeight: s.height];
		  if ([current point].x > (imageWidth/2))
		  {
		    [current setRightPos]; // on range  droite
		    [preRightLegends addObject: current];
		  }
		  else
		  {
		    [preLeftLegends addObject: current];
		  }        
	      }
		    
	      NSArray* leftLegends = [preLeftLegends sortedArrayUsingSelector: @selector (compareWith:)];
	      NSArray* rightLegends = [preRightLegends sortedArrayUsingSelector: @selector (compareWith:)];

	      CGFloat leftLegendsHeight = 0.0;
	      CGFloat rightLegendsHeight = 0.0;
	      
	      for (i=0; i < [leftLegends count]; i++)
		{
		  leftLegendsHeight += [[leftLegends objectAtIndex: i] height];
		}

	      for (i=0; i < [rightLegends count]; i++)
		{
		  rightLegendsHeight += [[rightLegends objectAtIndex: i] height];
		}
	      
	      interspace = (cellFrame.size.height - leftLegendsHeight)/ ([leftLegends count]+1);      

	      [self drawLegends: leftLegends onRight: NO withInterspace: interspace
		    withAttributes: attributes withFrame: cellFrame withBorder: border
		    withMargin: margin withSpaceMargin: spaceMargin withImageWidth: imageWidth
		    withPosImage: posImage];

	      interspace = (cellFrame.size.height - rightLegendsHeight)/ ([rightLegends count]+1);      
	      [self drawLegends: rightLegends onRight: YES withInterspace: interspace
		    withAttributes: attributes withFrame: cellFrame withBorder: border
		    withMargin: margin withSpaceMargin: spaceMargin withImageWidth: imageWidth
		    withPosImage: posImage];

	}
}

@end