-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCPatternUtilities.m
More file actions
679 lines (460 loc) · 22.1 KB
/
BCPatternUtilities.m
File metadata and controls
679 lines (460 loc) · 22.1 KB
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
//
// BCPatternUtilities.m
// Xynk
//
// Created by Tom Houpt on 14/11/19.
//
//
#import <Foundation/Foundation.h>
#import "BCPatternUtilities.h"
NSImage *HatchPatternImage(CGFloat lineSpacing, CGFloat strokeWidth, NSColor *strokeColor, NSColor *fillColor, NSInteger patternMask) {
// tl2br top left to bottom right
// tr2bl top right to bottom left
// t2b top to bottom
// l2r left to right
NSImage *theImage = [[NSImage alloc] initWithSize:NSMakeSize(lineSpacing,lineSpacing)];
[theImage lockFocus];
NSBezierPath *path;
// NOTE: need to handle case of strokeColor == nil, fillColor != nil
if (nil != fillColor) { [fillColor setFill]; }
else { [[NSColor clearColor] setFill]; }
NSRectFill(NSMakeRect(0,0,lineSpacing,lineSpacing));
path = [NSBezierPath bezierPath];
// [path setLineCapStyle:NSLineCapStyleSquare];
// NSGraphicsContext *theContext = [NSGraphicsContext currentContext];
// [theContext setShouldAntialias:NO];
#define offset 2
if (patternMask & kFillPatternLinesTopLeft2BottomRight) {
// [path moveToPoint:NSMakePoint(0,0)];
// [path lineToPoint:NSMakePoint(lineSpacing,lineSpacing)];
//
// [path moveToPoint:NSMakePoint(-lineSpacing,0)];
// [path lineToPoint:NSMakePoint(0,lineSpacing)];
//
//
// [path moveToPoint:NSMakePoint(lineSpacing,0)];
// [path lineToPoint:NSMakePoint(2 * (lineSpacing),lineSpacing)];
[path moveToPoint:NSMakePoint(lineSpacing/2-1,0-1)];
[path lineToPoint:NSMakePoint(lineSpacing+1,lineSpacing/2+1)];
[path moveToPoint:NSMakePoint(0-1,lineSpacing/2-1)];
[path lineToPoint:NSMakePoint(lineSpacing/2+1,lineSpacing+1)];
// [path moveToPoint:NSMakePoint(lineSpacing,0)];
// [path lineToPoint:NSMakePoint(lineSpacing+lineSpacing,lineSpacing)];
//
}
if (patternMask & kFillPatternLinesTopRight2BottomLeft) {
[path moveToPoint:NSMakePoint(0-1,lineSpacing/2+1)];
[path lineToPoint:NSMakePoint(lineSpacing/2+1,0-1)];
[path moveToPoint:NSMakePoint(lineSpacing/2-1,lineSpacing+1)];
[path lineToPoint:NSMakePoint(lineSpacing+1,lineSpacing/2-1)];
// CGFloat x = lineSpacing/2;
//
// [path moveToPoint:NSMakePoint(x-lineSpacing,0)];
// [path lineToPoint:NSMakePoint(x-lineSpacing-lineSpacing,lineSpacing)];
//
// [path moveToPoint:NSMakePoint(x,0)];
// [path lineToPoint:NSMakePoint(x-lineSpacing,lineSpacing)];
//
// [path moveToPoint:NSMakePoint(x+lineSpacing,0)];
// [path lineToPoint:NSMakePoint(x,lineSpacing)];
//
}
if (patternMask & kFillPatternLinesVertical) {
[path moveToPoint:NSMakePoint(lineSpacing/2,-0)];
[path lineToPoint:NSMakePoint(lineSpacing/2,lineSpacing)];
}
if (patternMask & kFillPatternLinesHorizontal) {
[path moveToPoint:NSMakePoint(0,lineSpacing/2)];
[path lineToPoint:NSMakePoint(lineSpacing,lineSpacing/2)];
}
[strokeColor setStroke];
[path setLineWidth:strokeWidth];
[path stroke];
// // grab the subimage from the center of the superimage
// NSBitmapImageRep *subImageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:
// NSMakeRect(lineSpacing/2,lineSpacing/2,lineSpacing,lineSpacing)];
//
// [theSuperImage unlockFocus];
//
// NSImage *theImage = [[NSImage alloc] initWithCGImage:[subImageRep CGImage] size:NSMakeSize(lineSpacing,lineSpacing)];
[theImage unlockFocus];
return theImage;
}
NSImage *DotPatternImage(CGFloat dotSpacing, CGFloat dotDiameter, NSColor *dotColor, NSColor *fillColor, BOOL staggered) {
NSBezierPath *path;
NSRect dot;
NSImage *theImage;
// NOTE: need to handle case of strokeColor == nil, fillColor != nil
if (staggered == REGULAR_DOTS) {
theImage = [[NSImage alloc] initWithSize:NSMakeSize(dotSpacing,dotSpacing)];
[theImage lockFocus];
path = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,dotSpacing,dotSpacing)];
if (nil != fillColor) { [fillColor setFill]; }
else { [[NSColor clearColor] setFill]; }
[path fill];
dot = NSMakeRect((dotSpacing - dotDiameter)/2,(dotSpacing - dotDiameter)/2,dotDiameter,dotDiameter);
[dotColor setFill];
path = [NSBezierPath bezierPathWithOvalInRect:dot];
[path fill];
}
else { // staggered
CGFloat imageWidth = dotSpacing * 2.0;
theImage = [[NSImage alloc] initWithSize:NSMakeSize(imageWidth,imageWidth)];
[theImage lockFocus];
path = [NSBezierPath bezierPathWithRect:NSMakeRect(0,0,imageWidth,imageWidth)];
if (nil != fillColor) { [fillColor setFill]; }
else { [[NSColor clearColor] setFill]; }
[path fill];
dot = NSMakeRect(0,0,dotDiameter,dotDiameter);
path = [NSBezierPath bezierPathWithOvalInRect:dot];
dot = NSMakeRect(dotSpacing,dotSpacing,dotDiameter,dotDiameter);
[path appendBezierPathWithOvalInRect:dot];
[dotColor setFill];
[path fill];
}
[theImage unlockFocus];
return theImage;
}
#define LINE_THIN_WIDTH 1.0
#define LINE_REGULAR_WIDTH 2.0
#define LINE_THICK_WIDTH 4.0
#define LINE_SPARSE_SPACING 12.0
#define LINE_REGULAR_SPACING 9.0
#define LINE_DENSE_SPACING 6.0
#define LINE_DIAG_SPARSE_SPACING 16.0
#define LINE_DIAG_REGULAR_SPACING 11.0
#define LINE_DIAG_DENSE_SPACING 8.0
#define DOT_SMALL_WIDTH 1.0
#define DOT_REGULAR_WIDTH 3.0
#define DOT_LARGE_WIDTH 5.0
#define DOT_SPARSE_SPACING 13.0
#define DOT_REGULAR_SPACING 9.0
#define DOT_DENSE_SPACING 6.0
#define DOT_STAGGERED_SPARSE_SPACING 10.0
#define DOT_STAGGERED_REGULAR_SPACING 6.0
#define DOT_STAGGERED_DENSE_SPACING 4.0
NSImage *FillPatternImage(BCFillPatternFlags patternMask, NSColor *patternColor, NSColor *backgroundColor) {
CGFloat lineWidth = LINE_REGULAR_WIDTH;
CGFloat lineSpacing = LINE_REGULAR_SPACING;
CGFloat dotDiameter = DOT_REGULAR_WIDTH;
CGFloat dotSpacing = DOT_REGULAR_SPACING;
NSColor *strokeColor, *fillColor;
NSImage *patternImage;
if (kFillPatternSolid == patternMask) { return nil; }
if (kFillPatternInverted & patternMask) {
// invert the image colors
fillColor = patternColor;
strokeColor = backgroundColor;
}
else {
strokeColor = patternColor;
fillColor = backgroundColor;
}
if (kFillPatternLineHatch & patternMask) {
// hatched...
// set the lineWidth
if ( kFillPatternThinElements & patternMask) {
lineWidth = LINE_THIN_WIDTH;
}
else if ( kFillPatternThickElements & patternMask) {
lineWidth = LINE_THICK_WIDTH;
}
else {
lineWidth = LINE_REGULAR_WIDTH;
}
BOOL diagonalLines = (kFillPatternLinesTopLeft2BottomRight & patternMask) || (kFillPatternLinesTopRight2BottomLeft & patternMask);
// NOTE: you think we could set up an multidimensional array to look up values
// spacing = lines/dots, (perpendicular/diagonal)(staggered/regular),
// set the line spacing....
if (diagonalLines) {
if ( kFillPatternSparse & patternMask ) {
lineSpacing = LINE_DIAG_SPARSE_SPACING;
}
else if ( kFillPatternDense & patternMask) {
lineSpacing = LINE_DIAG_DENSE_SPACING;
}
else {
lineSpacing = LINE_DIAG_REGULAR_SPACING;
}
}
else {
if ( kFillPatternSparse & patternMask ) {
lineSpacing = LINE_SPARSE_SPACING;
}
else if ( kFillPatternDense & patternMask) {
lineSpacing = LINE_DENSE_SPACING;
}
else {
lineSpacing = LINE_REGULAR_SPACING;
}
} // hatching
patternImage = HatchPatternImage(lineSpacing, lineWidth, strokeColor, fillColor, patternMask);
}
else {
// stippled...
if ( kFillPatternThinElements & patternMask) {
dotDiameter = DOT_SMALL_WIDTH;
}
if ( kFillPatternThickElements & patternMask) {
dotDiameter = DOT_LARGE_WIDTH;
}
BOOL staggered = !(0 == (kFillPatternStaggeredDots & patternMask));
if (staggered) {
if ( kFillPatternSparse & patternMask) {
dotSpacing = DOT_STAGGERED_SPARSE_SPACING;
}
else if ( kFillPatternDense & patternMask) {
dotSpacing = DOT_STAGGERED_DENSE_SPACING;
}
else {
dotSpacing = DOT_STAGGERED_REGULAR_SPACING;
}
}
else {
if ( kFillPatternSparse & patternMask) {
dotSpacing = DOT_SPARSE_SPACING;
}
else if ( kFillPatternDense & patternMask) {
dotSpacing = DOT_DENSE_SPACING;
}
else {
dotSpacing = DOT_REGULAR_SPACING;
}
}
patternImage = DotPatternImage(dotSpacing,dotDiameter, strokeColor, fillColor, staggered );
} // stippled
return patternImage;
}
NSColor *FillColorWithPattern(BCFillPatternFlags patternMask, NSColor *patternColor, NSColor *backgroundColor) {
if (patternMask != kFillPatternSolid) {
NSImage *patternImage = FillPatternImage(patternMask, patternColor, backgroundColor);
return [NSColor colorWithPatternImage:patternImage];
}
return patternColor;
}
NSArray *GetFillPatternArray(void) {
NSArray *patternArray = @[
@( kFillPatternSolid),
// dense hatches
@( kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternDense ),
@( kFillPatternLineHatch + kFillPatternLinesTopRight2BottomLeft + kFillPatternDense ),
@( kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternLinesTopRight2BottomLeft + kFillPatternDense ),
@( kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternDense ),
@( kFillPatternLineHatch + kFillPatternLinesHorizontal + kFillPatternDense ),
@( kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternLinesHorizontal + kFillPatternDense ),
// regular hatches
@( kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight ),
@( kFillPatternLineHatch + kFillPatternLinesTopRight2BottomLeft ),
@( kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternLinesTopRight2BottomLeft ),
@( kFillPatternLineHatch + kFillPatternLinesVertical ),
@( kFillPatternLineHatch + kFillPatternLinesHorizontal ),
@( kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternLinesHorizontal ),
// sparse hatches
@( kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternSparse ),
@( kFillPatternLineHatch + kFillPatternLinesTopRight2BottomLeft + kFillPatternSparse ),
@( kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternLinesTopRight2BottomLeft + kFillPatternSparse ),
@( kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternSparse ),
@( kFillPatternLineHatch + kFillPatternLinesHorizontal + kFillPatternSparse ),
@( kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternLinesHorizontal + kFillPatternSparse ),
// dense dots
@( kFillPatternDotStipple + kFillPatternStaggeredDots + kFillPatternDense ),
// regular dots
@( kFillPatternDotStipple + kFillPatternStaggeredDots ),
// sparse dots
@( kFillPatternDotStipple + kFillPatternStaggeredDots + kFillPatternSparse ),
// dense dots
@( kFillPatternDotStipple + kFillPatternDense ),
// regular dots
@( kFillPatternDotStipple ),
// sparse dots
@( kFillPatternDotStipple + kFillPatternSparse ),
// inverted forms...
// dense hatches
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternDense ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopRight2BottomLeft + kFillPatternDense ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternLinesTopRight2BottomLeft + kFillPatternDense ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternDense ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesHorizontal + kFillPatternDense ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternLinesHorizontal + kFillPatternDense ),
// regular hatches
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopRight2BottomLeft ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternLinesTopRight2BottomLeft ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesVertical ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesHorizontal ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternLinesHorizontal ),
// sparse hatches
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternSparse ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopRight2BottomLeft + kFillPatternSparse ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesTopLeft2BottomRight + kFillPatternLinesTopRight2BottomLeft + kFillPatternSparse ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternSparse ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesHorizontal + kFillPatternSparse ),
@( kFillPatternInverted + kFillPatternLineHatch + kFillPatternLinesVertical + kFillPatternLinesHorizontal + kFillPatternSparse ),
// dense dots
@( kFillPatternInverted + kFillPatternDotStipple + kFillPatternStaggeredDots + kFillPatternDense ),
// regular dots
@( kFillPatternInverted + kFillPatternDotStipple + kFillPatternStaggeredDots ),
// sparse dots
@( kFillPatternInverted + kFillPatternDotStipple + kFillPatternStaggeredDots + kFillPatternSparse ),
// dense dots
@( kFillPatternInverted + kFillPatternDotStipple + kFillPatternDense ),
// regular dots
@( kFillPatternInverted + kFillPatternDotStipple ),
// sparse dots
@( kFillPatternInverted + kFillPatternDotStipple + kFillPatternSparse )
];
return patternArray;
}
NSInteger GetPatternArrayIndexByMatchingPattern(NSNumber *thePattern) {
NSArray *patternArray = GetFillPatternArray();
for (NSNumber *eachPattern in patternArray) {
if ([eachPattern isEqualToNumber:thePattern]) {
return [patternArray indexOfObject:eachPattern];
}
}
return -1;
}
// --------------------------------------------------------------------------------
// STROKE DASH OR DOT PATTERNS
#define kDotLength 1
#define kDotSpace 2
#define kDashShortLength 5
#define kDashShortSpace 2
#define kDashLongLength 10
#define kDashLongSpace 3
#define kMaxDashElements 6
void SetPathStrokePattern(NSBezierPath *thePath, BCStrokePatternType dashPattern, CGFloat strokeWidth) {
CGFloat pattern[kMaxDashElements];
[thePath setLineWidth:strokeWidth];
switch (dashPattern) {
case kSolidStroke:
// clear the line dash
[thePath setLineCapStyle:NSLineCapStyleSquare];
[thePath setLineDash:nil count:0 phase:0.0];
break;
case kDotted:
// ••••••
[thePath setLineCapStyle:NSLineCapStyleRound];
pattern[0] = kDotLength;
pattern[1] = kDotSpace*strokeWidth;
[thePath setLineDash:pattern count:2 phase:0.0];
break;
case kDottedSpaced:
// • • • •
[thePath setLineCapStyle:NSLineCapStyleRound];
pattern[0] = kDotLength;
pattern[1] = kDotSpace * 2*strokeWidth;
[thePath setLineDash:pattern count:2 phase:0.0];
break;
case kDottedTriple:
// ••• ••• •••
[thePath setLineCapStyle:NSLineCapStyleRound];
pattern[0] = kDotLength;
pattern[1] = kDotSpace*strokeWidth;
pattern[2] = kDotLength;
pattern[3] = kDotSpace*strokeWidth;
pattern[4] = kDotLength;
pattern[5] = kDotSpace * 2*strokeWidth;
[thePath setLineDash:pattern count:6 phase:0.0];
break;
case kDashedShort:
// -----
[thePath setLineCapStyle:NSLineCapStyleSquare];
pattern[0] = kDashShortLength;
pattern[1] = kDashShortSpace*strokeWidth;
[thePath setLineDash:pattern count:2 phase:0.0];
break;
case kDashedLong:
// — — — —
[thePath setLineCapStyle:NSLineCapStyleSquare];
pattern[0] = kDashLongLength;
pattern[1] = kDashLongSpace*strokeWidth;
[thePath setLineDash:pattern count:2 phase:0.0];
break;
case kDashedShortSpaced:
// - - - - -
[thePath setLineCapStyle:NSLineCapStyleSquare];
pattern[0] = kDashShortLength;
pattern[1] = kDashShortSpace * 2*strokeWidth;
[thePath setLineDash:pattern count:2 phase:0.0];
break;
// case kDashedLongSpaced:
// // — — — —
// [thePath setLineCapStyle:NSLineCapStyleSquare];
// pattern[0] = kDashLongLength;
// pattern[1] = kDashLongSpace * 2 *strokeWidth;
// [thePath setLineDash:pattern count:2 phase:0.0];
// break;
//
case kDashedShortLong:
// -—-—-—-—
[thePath setLineCapStyle:NSLineCapStyleSquare];
pattern[0] = kDashShortLength;
pattern[1] = kDashShortSpace*strokeWidth;
pattern[2] = kDashLongLength;
pattern[3] = kDashShortSpace*strokeWidth;
[thePath setLineDash:pattern count:4 phase:0.0];
break;
case kDotDashShort:
// •-•-•-•-
[thePath setLineCapStyle:NSLineCapStyleSquare];
pattern[0] = kDotLength;
pattern[1] = kDashShortSpace*strokeWidth;
pattern[2] = kDashShortLength;
pattern[3] = kDashShortSpace*strokeWidth;
[thePath setLineDash:pattern count:4 phase:0.0];
break;
case kDotDashLong:
// •—•—•—•—
[thePath setLineCapStyle:NSLineCapStyleSquare];
pattern[0] = kDotLength;
pattern[1] = kDotSpace*strokeWidth;
pattern[2] = kDashLongLength;
pattern[3] = kDotSpace*strokeWidth;
[thePath setLineDash:pattern count:4 phase:0.0];
break;
case kDotDotDashDashLong:
// ••——••——••——
[thePath setLineCapStyle:NSLineCapStyleSquare];
pattern[0] = kDotLength;
pattern[1] = kDotSpace*strokeWidth;
pattern[2] = kDotLength;
pattern[3] = kDotSpace*strokeWidth;
pattern[4] = kDashLongLength;
pattern[5] = kDotSpace*strokeWidth;
[thePath setLineDash:pattern count:6 phase:0.0];
break;
};
}
#define DASH_BOX_WIDTH 72
#define DASH_BOX_HEIGHT 16
#define DASH_SAMPLE_STROKE_WIDTH 3
NSMenu *SetUpDashPickerMenu(CGFloat strokeWidth, NSColor *strokeColor, NSColor *backgroundColor) {
// make a pop-up menu of the dash lines
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"dashes"];
NSMenuItem *item;
NSRect dashBox = NSMakeRect(0.5,0.5,DASH_BOX_WIDTH,DASH_BOX_HEIGHT);
for (NSUInteger i= 0; i < kNumDashPatterns; i++) {
// [theMenu addItemWithTitle:@"" action:NULL keyEquivalent:@""];
item= [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""];
// item = [theMenu itemAtIndex:i];
// draw the pallete color onto theme background, attach image to menu item
NSImage* menuImage = [[NSImage alloc] initWithSize:dashBox.size] ;
[menuImage lockFocus];
[backgroundColor setFill];
NSRectFill(dashBox);
NSBezierPath *path = [NSBezierPath bezierPath];
[strokeColor setStroke];
[path setLineWidth:strokeWidth]; // or use DASH_SAMPLE_STROKE_WIDTH?
SetPathStrokePattern(path, i, strokeWidth);
[path moveToPoint:NSMakePoint(0,DASH_BOX_HEIGHT/2)];
[path lineToPoint:NSMakePoint(DASH_BOX_WIDTH,DASH_BOX_HEIGHT/2)];
[path stroke];
[menuImage unlockFocus];
[item setImage:menuImage];
[item setOnStateImage:nil];
[item setMixedStateImage:nil];
[theMenu addItem:item];
}
return theMenu;
}