-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSIconRenderer.m
829 lines (692 loc) · 23.2 KB
/
CSIconRenderer.m
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
//
// CSIconRenderer.m
// CSIconView
//
// Created by Alastair Houghton on 29/07/2005.
// Copyright (c) 2005-2010 Coriolis Systems Limited
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "CSIconRenderer.h"
#import "CSShading.h"
// A Finder-like icon cell class
@implementation CSIconRenderer
- (id)init
{
if ((self = [super init])) {
layoutManager = [[NSLayoutManager alloc] init];
textContainer = [[NSTextContainer alloc] init];
textStorage = [[NSTextStorage alloc] init];
[self setVariant:kCSNormalIconVariant];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
}
return self;
}
- (void)dealloc
{
[textStorage release];
[layoutManager release];
[textContainer release];
[labelColor release];
[labelShadeColor release];
[icon release];
[variant release];
[super dealloc];
}
- (CSIcon *)icon
{
return icon;
}
- (void)setIcon:(CSIcon *)theIcon
{
if (icon != theIcon) {
CSIcon *oldIcon = icon;
icon = [theIcon retain];
[oldIcon release];
}
}
- (NSString *)variant
{
return variant;
}
- (void)setVariant:(NSString *)newVariant
{
if (variant != newVariant) {
NSString *oldVariant = variant;
variant = [newVariant retain];
[oldVariant release];
}
}
- (NSString *)title
{
return [textStorage string];
}
- (void)setTitle:(NSString *)newTitle
{
if (!newTitle)
newTitle = @"";
[textStorage replaceCharactersInRange:NSMakeRange (0, [textStorage length])
withString:newTitle];
needLayout = YES;
}
- (void)setTitleAttributes:(NSDictionary *)newAttributes
{
[textStorage setAttributes:newAttributes
range:NSMakeRange (0, [textStorage length])];
}
- (NSAttributedString *)attributedTitle
{
return textStorage;
}
- (void)setAttributedTitle:(NSAttributedString *)newTitle
{
[textStorage replaceCharactersInRange:NSMakeRange (0, [textStorage length])
withAttributedString:newTitle];
needLayout = YES;
}
- (NSColor *)labelColor
{
return labelColor;
}
- (void)setLabelColor:(NSColor *)newColor
{
if (newColor != labelColor) {
NSColor *oldColor = labelColor;
labelColor = [newColor retain];
[oldColor release];
}
}
- (NSColor *)labelShadeColor
{
return labelShadeColor;
}
- (void)setLabelShadeColor:(NSColor *)newColor
{
if (newColor != labelShadeColor) {
NSColor *oldColor = labelShadeColor;
labelShadeColor = [newColor retain];
[oldColor release];
}
}
- (NSSize)iconSize
{
return iconSize;
}
- (void)setIconSize:(NSSize)newSize
{
iconSize = newSize;
}
/* Assumes the rectangles are all centred. */
- (NSBezierPath *)bezierPathSurroundingRects:(NSMutableArray *)rectArray
radius:(float)r
padding:(float)padding
{
BOOL changed1, changed2;
unsigned count = [rectArray count];
float a = M_SQRT1_2 * r;
NSBezierPath *path = [NSBezierPath bezierPath];
NSRect prevRect, rect;
float x[4], y[4];
unsigned n;
if (!count)
return path;
/* First check if there are any rectangles that are too close in size
to draw smooth curves between them. If so, expand the smaller ones
to match the size of the larger ones. */
changed1 = changed2 = NO;
do {
for (n = 0; n + 1 < count; ++n) {
NSRect r1, r2;
float xdiff;
changed1 = changed2 = NO;
r1 = [[rectArray objectAtIndex:n] rectValue];
r2 = [[rectArray objectAtIndex:n + 1] rectValue];
xdiff = NSMinX (r1) - NSMinX (r2);
if (xdiff != 0.0f && fabs (xdiff) < r) {
if (xdiff < 0.0f) {
r2.origin.x = r1.origin.x;
r2.size.width -= xdiff;
changed2 = YES;
} else {
r1.origin.x = r2.origin.x;
r1.size.width += xdiff;
changed1 = YES;
}
}
xdiff = NSMaxX (r1) - NSMaxX (r2);
if (xdiff != 0.0f && fabs (xdiff) < r) {
if (xdiff < 0.0f) {
r1.size.width -= xdiff;
changed1 = YES;
} else {
r2.size.width += xdiff;
changed2 = YES;
}
}
if (changed1) {
[rectArray replaceObjectAtIndex:n
withObject:[NSValue valueWithRect:r1]];
}
if (changed2) {
[rectArray replaceObjectAtIndex:n + 1
withObject:[NSValue valueWithRect:r2]];
}
}
} while (changed1 || changed2);
// Do the top part of the top element
rect = [[rectArray objectAtIndex:0] rectValue];
x[0] = NSMinX (rect) - padding;
x[1] = NSMinX (rect) + a - padding;
x[2] = NSMaxX (rect) + padding;
y[0] = NSMaxY (rect) + padding;
y[1] = NSMaxY (rect) - a + padding;
[path moveToPoint:NSMakePoint (x[0], y[1])];
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[0], y[0])
toPoint:NSMakePoint (x[1], y[0])
radius:r];
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[2], y[0])
toPoint:NSMakePoint (x[2], y[1])
radius:r];
// Now go down the right hand side
for (n = 1; n < count; ++n) {
float prevX, thisX;
prevRect = rect;
rect = [[rectArray objectAtIndex:n] rectValue];
prevX = NSMaxX (prevRect);
thisX = NSMaxX (rect);
if (prevX > thisX) {
x[0] = NSMaxX (prevRect) + padding;
x[1] = NSMaxX (prevRect) - a + padding;
x[2] = NSMaxX (rect) + padding;
y[0] = NSMinY (prevRect) - padding;
y[1] = NSMaxY (rect) - M_SQRT1_2 * a - padding;
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[0], y[0])
toPoint:NSMakePoint (x[1], y[0])
radius:r];
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[2], y[0])
toPoint:NSMakePoint (x[2], y[1])
radius:0.6 * r];
} else if (prevX < thisX) {
x[0] = NSMaxX (prevRect) + padding;
x[1] = NSMaxX (prevRect) + M_SQRT1_2 * a + padding;
x[2] = NSMaxX (rect) + padding;
y[0] = NSMinY (prevRect) + padding;
y[1] = NSMaxY (rect) - a + padding;
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[0], y[0])
toPoint:NSMakePoint (x[1], y[0])
radius:0.6 * r];
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[2], y[0])
toPoint:NSMakePoint (x[2], y[1])
radius:r];
} else {
[path lineToPoint:NSMakePoint (NSMaxX (rect) + padding, NSMaxY (rect))];
}
}
// Do the bottom part of the bottom element
rect = [[rectArray objectAtIndex:count - 1] rectValue];
x[0] = NSMaxX (rect) + padding;
x[1] = NSMaxX (rect) - a + padding;
x[2] = NSMinX (rect) - padding;
y[0] = NSMinY (rect) - padding;
y[1] = NSMinY (rect) + a - padding;
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[0], y[0])
toPoint:NSMakePoint (x[1], y[0])
radius:r];
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[2], y[0])
toPoint:NSMakePoint (x[2], y[1])
radius:r];
// Now go up the left hand side
for (n = count - 1; n > 0; --n) {
float prevX, thisX;
prevRect = rect;
rect = [[rectArray objectAtIndex:n - 1] rectValue];
prevX = NSMinX (prevRect);
thisX = NSMinX (rect);
if (prevX < thisX) {
x[0] = NSMinX (prevRect) - padding;
x[1] = NSMinX (prevRect) + a - padding;
x[2] = NSMinX (rect) - padding;
y[0] = NSMaxY (prevRect) + padding;
y[1] = NSMinY (rect) + M_SQRT1_2 * a + padding;
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[0], y[0])
toPoint:NSMakePoint (x[1], y[0])
radius:r];
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[2], y[0])
toPoint:NSMakePoint (x[2], y[1])
radius:0.6 * r];
} else if (prevX > thisX) {
x[0] = NSMinX (prevRect) - padding;
x[1] = NSMinX (prevRect) - M_SQRT1_2 * a - padding;
x[2] = NSMinX (rect) - padding;
y[0] = NSMaxY (prevRect) - padding;
y[1] = NSMinY (rect) + a - padding;
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[0], y[0])
toPoint:NSMakePoint (x[1], y[0])
radius:0.6 * r];
[path appendBezierPathWithArcFromPoint:NSMakePoint (x[2], y[0])
toPoint:NSMakePoint (x[2], y[1])
radius:r];
} else {
[path lineToPoint:NSMakePoint (NSMinX (rect) - padding, NSMinY (rect))];
}
}
// Close the path
[path closePath];
return path;
}
/* If the string has changed, update the text storage object with the new
string to render. We might have to replace some of the characters with
an ellipsis if they won't fit. */
- (void)updateTextStorage
{
if (needLayout) {
NSRange glyphRange;
NSRect lineRect;
NSRange realRange;
CGFloat fraction;
unsigned character;
const unichar ellipsis = 0x2026;
NSString *ellipsisString = [NSString stringWithCharacters:&ellipsis
length:1];
unsigned numberOfGlyphs, glyph;
needLayout = NO;
numberOfGlyphs = [layoutManager numberOfGlyphs];
glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
if (glyphRange.length
&& glyphRange.length < numberOfGlyphs) {
lineRect = [layoutManager
lineFragmentUsedRectForGlyphAtIndex:glyphRange.length - 1
effectiveRange:&realRange];
glyph = [layoutManager glyphIndexForPoint:
NSMakePoint(0.5 * (NSMinX (lineRect) + NSMaxX (lineRect)),
0.5 * (NSMinY (lineRect) + NSMaxY (lineRect)))
inTextContainer:textContainer
fractionOfDistanceThroughGlyph:&fraction];
character = [layoutManager characterIndexForGlyphAtIndex:glyph];
while (glyphRange.length < numberOfGlyphs
&& [textStorage length] > character + 1) {
[textStorage replaceCharactersInRange:NSMakeRange (character, 2)
withString:ellipsisString];
glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
numberOfGlyphs = [layoutManager numberOfGlyphs];
}
}
}
}
- (NSMutableArray *)iconTitleRectsInRect:(NSRect)rect
{
NSRange glyphRange;
unsigned glyph, numberOfGlyphs;
NSMutableArray *rectArray = [NSMutableArray array];
[self updateTextStorage];
glyph = 0;
numberOfGlyphs = [layoutManager numberOfGlyphs];
while (glyph < numberOfGlyphs) {
NSRect lineRect;
lineRect = [layoutManager lineFragmentUsedRectForGlyphAtIndex:glyph
effectiveRange:&glyphRange];
// Subtract the width of a trailing space character
if (glyphRange.length) {
unsigned characterIndex = [layoutManager characterIndexForGlyphAtIndex:
glyphRange.location + glyphRange.length - 1];
unichar ch = [[textStorage string] characterAtIndex:characterIndex];
if (ch == ' ') {
NSUInteger rectCount = 0;
NSRectArray rectArray
= [layoutManager rectArrayForGlyphRange:
NSMakeRange (glyphRange.location + glyphRange.length - 1, 1)
withinSelectedGlyphRange:NSMakeRange (NSNotFound, 0)
inTextContainer:textContainer
rectCount:&rectCount];
float width = 0.0;
while (rectCount--) {
width += NSWidth (rectArray[rectCount]);
}
lineRect.size.width -= width;
}
}
lineRect.origin.x += rect.origin.x;
lineRect.origin.y += rect.origin.y;
[rectArray insertObject:[NSValue valueWithRect:lineRect]
atIndex:0];
glyph = glyphRange.location + glyphRange.length;
}
return rectArray;
}
- (float)heightOfTitleInRect:(NSRect)rect
{
NSSize currentSize = [textContainer containerSize];
NSRectArray rects;
NSUInteger rectCount, n;
CGFloat height = 0.0;
if (currentSize.height != rect.size.height
|| currentSize.width != rect.size.width) {
[textContainer setContainerSize:rect.size];
needLayout = YES;
}
[self updateTextStorage];
rects = [layoutManager rectArrayForGlyphRange:
[layoutManager glyphRangeForTextContainer:textContainer]
withinSelectedGlyphRange:NSMakeRange (NSNotFound, 0)
inTextContainer:textContainer
rectCount:&rectCount];
for (n = 0; n < rectCount; ++n) {
if (height < NSMaxY (rects[n]))
height = NSMaxY (rects[n]);
}
return height;
}
- (NSArray *)titleRectArrayInRect:(NSRect)rect
{
NSSize currentSize = [textContainer containerSize];
if (currentSize.height != rect.size.height
|| currentSize.width != rect.size.width) {
[textContainer setContainerSize:rect.size];
needLayout = YES;
}
return [self iconTitleRectsInRect:rect];
}
- (void)renderIconTitleInRect:(NSRect)rect
withBackground:(BOOL)background
andFocusRing:(BOOL)focusRing
inKeyView:(BOOL)inKeyView
{
NSRange glyphRange;
NSSize currentSize = [textContainer containerSize];
if (currentSize.height != rect.size.height
|| currentSize.width != rect.size.width) {
[textContainer setContainerSize:rect.size];
needLayout = YES;
}
[self updateTextStorage];
if (background) {
NSMutableArray *rectArray = [self iconTitleRectsInRect:rect];
float height = NSHeight ([[rectArray objectAtIndex:0] rectValue]);
NSBezierPath *backgroundPath = [self bezierPathSurroundingRects:rectArray
radius:0.5 * height
padding:1.0];
if (!labelColor) {
if (inKeyView)
[[NSColor alternateSelectedControlColor] set];
else
[[NSColor secondarySelectedControlColor] set];
[backgroundPath fill];
} else if (!labelShadeColor) {
[labelColor set];
[backgroundPath fill];
} else {
CSShadingColor colors[] = {
{ 0.0, labelColor },
{ 1.0, labelShadeColor }
};
CSShadingColorArray colorArray = { 2, colors };
NSRect bounds = [backgroundPath bounds];
NSPoint top = NSMakePoint (0.0, NSMinY (bounds));
NSPoint bottom = NSMakePoint (0.0, NSMaxY (bounds));
CSShading *shading
= [CSShading axialShadingFromPoint:bottom
toPoint:top
withColors:colorArray
flags:CSShadingDefaultFlags];
[shading set];
[backgroundPath shade];
}
if (focusRing) {
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle (NSFocusRingOnly);
[backgroundPath fill];
[NSGraphicsContext restoreGraphicsState];
}
}
glyphRange = [layoutManager glyphRangeForTextContainer:textContainer];
[layoutManager drawGlyphsForGlyphRange:glyphRange
atPoint:rect.origin];
}
- (void)drawWithFrame:(NSRect)iconFrame
enabled:(BOOL)enabled
highlighted:(BOOL)highlighted
filledHighlight:(BOOL)filledHighlight
textOnRight:(BOOL)textOnRight
inKeyView:(BOOL)inKeyView
withText:(BOOL)withText
{
NSRect iconRect;
NSPoint iconPos;
NSRect textRect;
iconFrame = NSInsetRect (iconFrame, 2.0, 2.0);
if (textOnRight) {
iconPos = NSMakePoint (NSMinX (iconFrame) + 2.0,
NSMinY (iconFrame)
+ 0.5 * (NSHeight (iconFrame) + iconSize.height));
} else {
iconPos = NSMakePoint (NSMinX (iconFrame)
+ 0.5 * (NSWidth (iconFrame) - iconSize.width),
NSMinY (iconFrame) + iconSize.height + 2.0);
}
iconRect = NSMakeRect (iconPos.x - 2.0, iconPos.y - iconSize.height - 2.0,
iconSize.width + 4.0, iconSize.height + 4.0);
if (enabled) {
NSRect drawRect = NSMakeRect (iconPos.x, iconPos.y - iconSize.height,
iconSize.width, iconSize.height);
if (highlighted) {
NSBezierPath *path = [NSBezierPath bezierPath];
[[NSColor secondarySelectedControlColor] set];
[path appendBezierPathWithRoundedRect:iconRect xRadius:4.0 yRadius:4.0];
if (filledHighlight)
[path fill];
else {
[path setLineWidth:2.0];
[path stroke];
}
}
if (icon) {
[icon drawVariant:variant inRect:drawRect
operation:NSCompositeSourceOver fraction:1.0];
} else {
NSBezierPath *path = [NSBezierPath bezierPath];
CGFloat dash[2] = { 5.0, 2.0 };
if (highlighted)
[[[NSColor secondarySelectedControlColor]
blendedColorWithFraction:0.3
ofColor:[NSColor blackColor]] set];
else
[[NSColor secondarySelectedControlColor] set];
[path appendBezierPathWithRoundedRect:iconRect xRadius:4.0 yRadius:4.0];
[path setLineWidth:2.0];
[path setLineDash:dash count:2 phase:0.0f];
[path stroke];
}
} else {
NSRect drawRect = NSMakeRect (iconPos.x, iconPos.y - iconSize.height,
iconSize.width, iconSize.height);
if (icon) {
[icon drawVariant:variant inRect:drawRect
operation:NSCompositeSourceOver fraction:0.5];
} else {
NSBezierPath *path = [NSBezierPath bezierPath];
CGFloat dash[2] = { 5.0, 2.0 };
[[[NSColor secondarySelectedControlColor] colorWithAlphaComponent:0.5]
set];
[path appendBezierPathWithRoundedRect:iconRect xRadius:4.0 yRadius:4.0];
[path setLineWidth:2.0];
[path setLineDash:dash count:2 phase:0];
[path stroke];
}
}
if (withText) {
if (textOnRight) {
float height;
textRect.origin.x = iconPos.x + iconSize.width + 6.0;
textRect.origin.y = 0.0f;
textRect.size.width = NSMaxX (iconFrame) - textRect.origin.x - 4.0;
textRect.size.height = NSHeight (iconFrame);
height = [self heightOfTitleInRect:textRect];
textRect.origin.y = (NSMinY (iconFrame)
+ 0.5 * (NSHeight (iconFrame) - height));
} else {
textRect.origin.x = iconFrame.origin.x + 2.0;
textRect.size.width = iconFrame.size.width - 4.0;
textRect.origin.y = iconPos.y + 6.0;
textRect.size.height = NSMaxY (iconFrame) - textRect.origin.y - 4.0;
}
[self renderIconTitleInRect:textRect
withBackground:(enabled && highlighted) || labelColor
andFocusRing:highlighted && labelShadeColor
inKeyView:inKeyView];
}
}
- (NSRect)textRectIfDrawnWithFrame:(NSRect)iconFrame
textOnRight:(BOOL)textOnRight
{
NSRect iconRect;
NSPoint iconPos;
NSRect textRect;
iconFrame = NSInsetRect (iconFrame, 2.0, 2.0);
if (textOnRight) {
iconPos = NSMakePoint (NSMinX (iconFrame) + 2.0,
NSMinY (iconFrame)
+ 0.5 * (NSHeight (iconFrame) + iconSize.height));
} else {
iconPos = NSMakePoint (NSMinX (iconFrame)
+ 0.5 * (NSWidth (iconFrame) - iconSize.width),
NSMinY (iconFrame) + iconSize.height + 2.0);
}
iconRect = NSMakeRect (iconPos.x - 2.0, iconPos.y - iconSize.height - 2.0,
iconSize.width + 4.0, iconSize.height + 4.0);
if (textOnRight) {
float height;
textRect.origin.x = iconPos.x + iconSize.width + 6.0;
textRect.origin.y = 0.0f;
textRect.size.width = NSMaxX (iconFrame) - textRect.origin.x - 4.0;
textRect.size.height = NSHeight (iconFrame);
height = [self heightOfTitleInRect:textRect];
textRect.origin.y = (NSMinY (iconFrame)
+ 0.5 * (NSHeight (iconFrame) - height));
} else {
textRect.origin.x = iconFrame.origin.x + 2.0;
textRect.size.width = iconFrame.size.width - 4.0;
textRect.origin.y = iconPos.y + 6.0;
textRect.size.height = NSMaxY (iconFrame) - textRect.origin.y - 4.0;
}
return textRect;
}
- (BOOL)isPoint:(NSPoint)pt
inTextIfDrawnWithFrame:(NSRect)iconFrame
textOnRight:(BOOL)textOnRight
{
NSRect iconRect;
NSPoint iconPos;
NSRect textRect;
NSArray *rectArray;
unsigned n, count;
iconFrame = NSInsetRect (iconFrame, 2.0, 2.0);
if (textOnRight) {
iconPos = NSMakePoint (NSMinX (iconFrame) + 2.0,
NSMinY (iconFrame)
+ 0.5 * (NSHeight (iconFrame) + iconSize.height));
} else {
iconPos = NSMakePoint (NSMinX (iconFrame)
+ 0.5 * (NSWidth (iconFrame) - iconSize.width),
NSMinY (iconFrame) + iconSize.height + 2.0);
}
iconRect = NSMakeRect (iconPos.x - 2.0, iconPos.y - iconSize.height - 2.0,
iconSize.width + 4.0, iconSize.height + 4.0);
if (NSPointInRect (pt, iconRect))
return NO;
if (textOnRight) {
float height;
textRect.origin.x = iconPos.x + iconSize.width + 6.0;
textRect.origin.y = 0.0f;
textRect.size.width = NSMaxX (iconFrame) - textRect.origin.x - 4.0;
textRect.size.height = NSHeight (iconFrame);
height = [self heightOfTitleInRect:textRect];
textRect.origin.y = (NSMinY (iconFrame)
+ 0.5 * (NSHeight (iconFrame) - height));
} else {
textRect.origin.x = iconFrame.origin.x + 2.0;
textRect.size.width = iconFrame.size.width - 4.0;
textRect.origin.y = iconPos.y + 6.0;
textRect.size.height = NSMaxY (iconFrame) - textRect.origin.y - 4.0;
}
rectArray = [self titleRectArrayInRect:textRect];
count = [rectArray count];
for (n = 0; n < count; ++n) {
if (NSPointInRect (pt, [[rectArray objectAtIndex:n] rectValue]))
return YES;
}
return NO;
}
- (BOOL)intersectsWithRect:(NSRect)rect
ifDrawnWithFrame:(NSRect)iconFrame
highlighted:(BOOL)highlighted
textOnRight:(BOOL)textOnRight
{
NSRect iconRect;
NSPoint iconPos;
NSRect textRect;
NSRect drawRect;
NSArray *rectArray;
unsigned n, count;
iconFrame = NSInsetRect (iconFrame, 2.0, 2.0);
if (textOnRight) {
iconPos = NSMakePoint (NSMinX (iconFrame) + 2.0,
NSMinY (iconFrame)
+ 0.5 * (NSHeight (iconFrame) + iconSize.height));
} else {
iconPos = NSMakePoint (NSMinX (iconFrame)
+ 0.5 * (NSWidth (iconFrame) - iconSize.width),
NSMinY (iconFrame) + iconSize.height + 2.0);
}
iconRect = NSMakeRect (iconPos.x - 2.0, iconPos.y - iconSize.height - 2.0,
iconSize.width + 4.0, iconSize.height + 4.0);
drawRect = NSMakeRect (iconPos.x, iconPos.y - iconSize.height,
iconSize.width, iconSize.height);
if (icon && !highlighted) {
if ([icon variant:variant wouldIntersectRect:rect
ifDrawnInRect:drawRect])
return YES;
} else if (highlighted && NSIntersectsRect (rect, iconRect))
return YES;
else if (NSIntersectsRect (rect, drawRect))
return YES;
if (textOnRight) {
float height;
textRect.origin.x = iconPos.x + iconSize.width + 6.0;
textRect.origin.y = 0.0f;
textRect.size.width = NSMaxX (iconFrame) - textRect.origin.x - 4.0;
textRect.size.height = NSHeight (iconFrame);
height = [self heightOfTitleInRect:textRect];
textRect.origin.y = (NSMinY (iconFrame)
+ 0.5 * (NSHeight (iconFrame) - height));
} else {
textRect.origin.x = iconFrame.origin.x + 2.0;
textRect.size.width = iconFrame.size.width - 4.0;
textRect.origin.y = iconPos.y + 6.0;
textRect.size.height = NSMaxY (iconFrame) - textRect.origin.y - 4.0;
}
rectArray = [self titleRectArrayInRect:textRect];
count = [rectArray count];
for (n = 0; n < count; ++n) {
if (NSIntersectsRect (rect, [[rectArray objectAtIndex:n] rectValue]))
return YES;
}
return NO;
}
@end