-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisting5.html
executable file
·1028 lines (893 loc) · 36.2 KB
/
listing5.html
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>CarbonSketch - /Source/CSkObjects.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html#//apple_ref/doc/uid/TP30000943" target="_top">Reference Library</a> > <a href="../index.html#//apple_ref/doc/uid/TP30000925" target="_top">Sample Code</a> > <a href="../Carbon/index.html#//apple_ref/doc/uid/TP30000925-TP30000420" target="_top">Carbon</a> > <a href="../Carbon/idxGraphicsImaging-date.html#//apple_ref/doc/uid/TP30000925-TP30000420-TP30000450">Graphics & Imaging</a> > <A HREF="javascript:location.replace('index.html');">CarbonSketch</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">CarbonSketch</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/Source/CSkObjects.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/ReadMe.txt</option>
<option value="listing2.html">/Resources/CSkResources.r</option>
<option value="listing3.html">/Source/CSkConstants.h</option>
<option value="listing4.html">/Source/CSkDocStorage.h</option>
<option value="listing5.html">/Source/CSkObjects.c</option>
<option value="listing6.html">/Source/CSkObjects.h</option>
<option value="listing7.html">/Source/CSkPrinting.c</option>
<option value="listing8.html">/Source/CSkPrinting.h</option>
<option value="listing9.html">/Source/CSkShapes.c</option>
<option value="listing10.html">/Source/CSkShapes.h</option>
<option value="listing11.html">/Source/CSkToolPalette.c</option>
<option value="listing12.html">/Source/CSkToolPalette.h</option>
<option value="listing13.html">/Source/CSkUtils.c</option>
<option value="listing14.html">/Source/CSkUtils.h</option>
<option value="listing15.html">/Source/CSkWindow.c</option>
<option value="listing16.html">/Source/CSkWindow.h</option>
<option value="listing17.html">/Source/main.c</option>
<option value="listing18.html">/Source/NavServicesHandling.c</option>
<option value="listing19.html">/Source/NavServicesHandling.h</option></select>
</p>
</form>
<p><strong><a href="CarbonSketch.zip">Download Sample</a></strong> (“CarbonSketch.zip”, 110.5K)<BR>
<strong><a href="CarbonSketch.dmg">Download Sample</a></strong> (“CarbonSketch.dmg”, 114.0K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/*
File: CSkObjects.c
Contains: Definition of (opaque) CSkObject structure, and various related routines.
Refers to CSkShape for geometric description, and contains additional attributes.
CSkObjets form a doubly-linked list to deal with z-ordering.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
("Apple") in consideration of your agreement to the following terms, and your
use, installation, modification or redistribution of this Apple software
constitutes acceptance of these terms. If you do not agree with these terms,
please do not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and subject
to these terms, Apple grants you a personal, non-exclusive license, under Apple's
copyrights in this original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or without
modifications, in source and/or binary forms; provided that if you redistribute
the Apple Software in its entirety and without modifications, you must retain
this notice and the following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks or logos of
Apple Computer, Inc. may be used to endorse or promote products derived from the
Apple Software without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any patent rights that
may be infringed by your derivative works or by other works in which the Apple
Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2004 Apple Computer, Inc., All Rights Reserved
*/
#include <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h>
#include "CSkObjects.h"
// also includes "CSkShapes.h"
#include "CSkConstants.h"
#include "CSkToolPalette.h"
// CSkObjects (or "DrawObjects" as they were called in the first stages of development) are stored
// in a double-linked list. They contain a CSkShapePtr to the geometry definition, and
// specifications needed for drawing.
struct CSkObject
{
CSkShapePtr shape;
float lineWidth;
CGLineCap lineCap;
CGLineJoin lineJoin;
int lineStyle;
CGrgba strokeColor;
CGrgba fillColor;
Boolean selected;
CSkObjectPtr nextObj;
CSkObjectPtr prevObj;
};
//---------------------------------------------------------------------------------------------
// Return the specified line attributes lineWidth, lineCap, lineJoin, lineStyle.
// No NULL-checks in the out-parameters because
// a) the function is only used with parameters &lineWidth, &lineCap, &lineJoin, &lineStyle
// b) I want to keep the code short
void GetLineAttributes(const CSkObject* obj, float* width, CGLineCap* cap, CGLineJoin* join, int* style)
{
if (obj != NULL)
{
*width = obj->lineWidth;
*cap = obj->lineCap;
*join = obj->lineJoin;
*style = obj->lineStyle;
}
else
{
fprintf(stderr, "GetLineAttributes: NULL obj parameter\n");
}
}
//---------------------------------------------------------------------------------------------
// Allocate new drawObject with attributes from the current settings in the CSkToolPalette.
// Bounds are empty.
CSkObjectPtr CreateDrawObj(WindowRef toolPalette, int shapeType)
{
CSkObjectPtr obj = (CSkObjectPtr)NewPtrClear(sizeof(CSkObject));
if (obj != NULL)
{
SetDrawObjAttributesFromToolPalette(obj, toolPalette);
obj->shape = CSkShapeCreate(shapeType);
}
return obj;
}
//---------------------------------------------------------------------------------------------
// After mouse-tracking a selected CSkObject, we need an independent (unlinked) copy of it
// to draw during dragging.
CSkObjectPtr CopyDrawObject(const CSkObject* obj)
{
CSkObjectPtr newObj = (CSkObjectPtr)NewPtrClear(sizeof(CSkObject));
if (newObj)
{
memcpy(newObj, obj, sizeof(CSkObject));
newObj->shape = CSkShapeCreate(kUndefined);
memcpy(newObj->shape, obj->shape, CSkShapeSize());
newObj->nextObj = NULL;
newObj->prevObj = NULL;
}
return newObj;
}
//---------------------------------------------------------------------------------------------
void ReleaseDrawObj(CSkObjectPtr obj)
{
CSkShapeRelease(obj->shape);
DisposePtr((Ptr)obj);
}
void ReleaseDrawObjList(DrawObjListPtr objList)
{
CSkObjectPtr obj = objList->firstItem;
while ( obj != NULL )
{
CSkObjectPtr deleteThis = obj;
obj = obj->nextObj;
ReleaseDrawObj(deleteThis);
}
}
//---------------------------------------------------------------------------------------------
// Some obvious accessors
int GetDrawObjShapeType( const CSkObject* drawObj )
{
return CSkShapeGetType(drawObj->shape);
}
CSkShape* GetCSkObjectShape( const CSkObject* drawObj )
{
return drawObj->shape;
}
float GetFillAlpha( const CSkObject* drawObj )
{
return drawObj->fillColor.a;
}
float GetStrokeAlpha( const CSkObject* drawObj )
{
return drawObj->strokeColor.a;
}
void SetDrawObjSelectState( CSkObjectPtr drawObj, Boolean selected )
{
drawObj->selected = selected;
}
Boolean IsDrawObjSelected( const CSkObject* drawObj )
{
return drawObj->selected;
}
//----------------------------------------------------------------------------
void SetDrawObjAttributesFromToolPalette(CSkObjectPtr obj, WindowRef toolPalette)
{
obj->lineWidth = CSkToolPaletteGetLineWidth(toolPalette);
obj->lineCap = CSkToolPaletteGetLineCap(toolPalette);
obj->lineJoin = CSkToolPaletteGetLineJoin(toolPalette);
obj->lineStyle = CSkToolPaletteGetLineStyle(toolPalette);
CSkToolPaletteGetStrokeColor(toolPalette, &obj->strokeColor);
CSkToolPaletteGetFillColor(toolPalette, &obj->fillColor);
}
void SetSelectedDrawObjAttributesFromToolPalette(DrawObjListPtr objListP, WindowRef toolPalette)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
SetDrawObjAttributesFromToolPalette(obj, toolPalette);
obj = obj->nextObj;
}
}
//----------------------------------------------------------------------------
void SetLineWidthOfSelecteds(DrawObjListPtr objListP, float lineWidth)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
{
if (lineWidth == kMakeItThinner)
{
if (obj->lineWidth >= 2.0)
obj->lineWidth -= 1.0;
}
else if (lineWidth == kMakeItThicker)
obj->lineWidth += 1.0;
else
obj->lineWidth = lineWidth;
}
obj = obj->nextObj;
}
}
//----------------------------------------------------------------------------
void SetLineCapOfSelecteds(DrawObjListPtr objListP, CGLineCap lineCap)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
obj->lineCap = lineCap;
obj = obj->nextObj;
}
}
//----------------------------------------------------------------------------
void SetLineJoinOfSelecteds(DrawObjListPtr objListP, CGLineJoin lineJoin)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
obj->lineJoin = lineJoin;
obj = obj->nextObj;
}
}
//----------------------------------------------------------------------------
void SetLineStyleOfSelecteds(DrawObjListPtr objListP, int lineStyle)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
obj->lineStyle = lineStyle;
obj = obj->nextObj;
}
}
//----------------------------------------------------------------------------
void SetStrokeColorOfSelecteds(DrawObjListPtr objListP, CGrgba* color)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
obj->strokeColor = *color;
obj = obj->nextObj;
}
}
//----------------------------------------------------------------------------
void SetStrokeAlphaOfSelecteds(DrawObjListPtr objListP, float alpha)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
obj->strokeColor.a = alpha;
obj = obj->nextObj;
}
}
//----------------------------------------------------------------------------
void SetFillColorOfSelecteds(DrawObjListPtr objListP, CGrgba* color)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
obj->fillColor = *color;
obj = obj->nextObj;
}
}
//----------------------------------------------------------------------------
void SetFillAlphaOfSelecteds(DrawObjListPtr objListP, float alpha)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
obj->fillColor.a = alpha;
obj = obj->nextObj;
}
}
//--------------------------------------------------------
CSkObjectPtr FirstSelectedObject(const DrawObjList* objListP)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
return obj;
obj = obj->nextObj;
}
return NULL;
}
//------------------------------------------------------------------------
// Needed for dragselection of several objects
void CSkObjListSelectWithinRect(DrawObjList* objListP, CGRect selectionRect)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
CGRect bounds = CSkShapeGetBounds(obj->shape);
if ( CGRectContainsRect(selectionRect, bounds) )
obj->selected = true;
obj = obj->nextObj;
}
}
//-------------------------------------------
///////////// Drawing Routines //////////////
//
// We don't distinguish between "filled" and "not filled"; the result
// "stroke only" is achieved by setting the fillColor alpha to fully
// transparent. Similarly, if we want "filled only", we have to set
// the alpha of the strokeColor to 0.
// Consequently, we always pass kCGPathFillStroke to CGContextDrawPath.
//-------------------------------------------------------------------------
static void DrawRect(CGContextRef ctx, CGRect cgRect)
{
CGContextBeginPath(ctx); // reset current path to empty
CGContextAddRect(ctx, cgRect); // current path now represents cgRect
CGContextDrawPath(ctx, kCGPathFillStroke);
}
//-------------------------------------------------------------------------
static void DrawOval(CGContextRef ctx, CGRect cgRect)
{
const float TWOPI = 6.283185307;
float halfWidth = 0.5 * CGRectGetWidth(cgRect);
float halfHeight = 0.5 * CGRectGetHeight(cgRect);
float centerX, centerY, radius;
float scaleX, scaleY;
if (halfWidth < halfHeight)
{
radius = halfWidth;
scaleX = 1.0;
scaleY = halfHeight / halfWidth;
centerX = cgRect.origin.x + halfWidth;
centerY = (cgRect.origin.y + halfHeight) / scaleY;
}
else
{
radius = halfHeight;
scaleX = halfWidth / halfHeight;
scaleY = 1.0;
centerX = (cgRect.origin.x + halfWidth) / scaleX;
centerY = cgRect.origin.y + halfHeight;
}
CGContextSaveGState(ctx); // because we temporarily change the CTM
CGContextScaleCTM(ctx, scaleX, scaleY); // so the full-circle arc will appear as oval
CGContextBeginPath(ctx);
CGContextAddArc(ctx, centerX, centerY, radius, 0.0, TWOPI, 0);
CGContextClosePath(ctx);
CGContextRestoreGState(ctx); // back to the previous CTM
CGContextDrawPath(ctx, kCGPathFillStroke);
}
//--------------------------------------------------------------------------------------
static void DrawRRect(CGContextRef ctx, CGRect cgRect, float rX, float rY)
{
CGContextBeginPath(ctx);
if ((rX > 0) && (rY > 0))
{
float width = CGRectGetWidth(cgRect);
float height = CGRectGetHeight(cgRect);
float fw = width / rX;
float fh = height / rY;
if (fw < 2)
{
rX = width / 2;
fw = 2;
}
if (fh < 2)
{
rY = height / 2;
fh = 2;
}
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, CGRectGetMinX(cgRect), CGRectGetMinY(cgRect));
CGContextScaleCTM(ctx, rX, rY);
CGContextMoveToPoint(ctx, fw, fh/2);
CGContextAddArcToPoint(ctx, fw, fh, fw/2, fh, 1);
CGContextAddArcToPoint(ctx, 0, fh, 0, fh/2, 1);
CGContextAddArcToPoint(ctx, 0, 0, fw/2, 0, 1);
CGContextAddArcToPoint(ctx, fw, 0, fw, fh/2, 1);
CGContextRestoreGState(ctx);
}
else
{
CGContextAddRect(ctx, cgRect);
}
CGContextClosePath(ctx);
CGContextDrawPath(ctx, kCGPathFillStroke);
}
//--------------------------------------------------------------------------------------
static void DrawCGLine(CGContextRef ctx, CGPoint a, CGPoint b)
{
CGContextMoveToPoint( ctx, a.x, a.y );
CGContextAddLineToPoint( ctx, b.x, b.y );
CGContextStrokePath( ctx );
}
//--------------------------------------------------------------------------------------
// Keep this separate from RenderCSkObject; this way, RenderCSkObject can
// be reused from within the mousetracking loops when drawing into an overlay window.
void SetContextStateForDrawObject(CGContextRef ctx, const CSkObject* obj)
{
CGContextSetLineWidth(ctx, obj->lineWidth);
CGContextSetLineCap(ctx, obj->lineCap);
CGContextSetLineJoin(ctx, obj->lineJoin);
if (obj->lineStyle == kStyleDashed)
{
float dashLengths[2] = { obj->lineWidth + 4, obj->lineWidth + 4 };
CGContextSetLineDash(ctx, 1.0, dashLengths, 2);
}
CGContextSetStrokeColor( ctx, (float *)&(obj->strokeColor));
CGContextSetFillColor( ctx, (float *)&(obj->fillColor));
}
//--------------------------------------------------------------------------------------
// RenderCSkObject is being called from RenderDrawObjList, and also during MouseTracking
// (see CSkWindow.c).
void RenderCSkObject(CGContextRef ctx, const CSkObject* obj, Boolean drawSelection)
{
int shapeType = CSkShapeGetType(obj->shape);
CGRect shapeBounds = CSkShapeGetBounds(obj->shape);
CGPoint a, b;
if ( shapeType == kLineShape )
{
CSkShapeGetLine(obj->shape, &a, &b);
DrawCGLine(ctx, a, b);
}
else switch (shapeType)
{
case kRectShape:
DrawRect(ctx, shapeBounds);
break;
case kOvalShape:
DrawOval(ctx, shapeBounds);
break;
case kRRectShape:
{
float rX, rY;
CSkShapeGetRRectRadii(obj->shape, &rX, &rY);
DrawRRect(ctx, shapeBounds, rX, rY);
}
break;
}
if (drawSelection && obj->selected) // draw little "grabber" squares
{
CGRect grabRect;
UInt32 grabNum = 0;
CGContextSaveGState(ctx); // because we are changing colors and line width
CGContextSetRGBFillColor(ctx, 0.9, 0.9, 0.9, 0.7); // ltGray, a little transparent
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1.0); // black
CGContextSetLineWidth(ctx, 1.0);
while (NextGrabberRect(obj->shape, &grabNum, &grabRect))
{
DrawRect(ctx, grabRect);
// Alternatively:
// CGContextStrokeRect(ctx, grabRect);
// CGContextFillRect(ctx, grabRect);
}
CGContextRestoreGState(ctx);
}
} // RenderCSkObject
//---------------------------------------------------------------------------------------------
// Draw the CSkObjects in the linked list from back to front. For each object, the GState needs
// to be saved and restored.
void RenderDrawObjList(CGContextRef ctx, const DrawObjList* objListP, Boolean drawSelection)
{
CSkObjectPtr obj = objListP->lastItem; // draw from back to front
while (obj != NULL)
{
CGContextSaveGState(ctx); // because SetContextStateForDrawObject is doing what it says it will
SetContextStateForDrawObject(ctx, obj);
RenderCSkObject(ctx, obj, drawSelection);
CGContextRestoreGState(ctx); // undo the changes for the specific obj drawing
obj = obj->prevObj;
}
}
//---------------------------------------------------------------------------------------------
// Multiply in a transparency factor. Used for tracking feedback when resizing an object.
void MakeDrawObjTransparent(CSkObject* obj, float alpha)
{
obj->strokeColor.a *= alpha;
obj->fillColor.a *= alpha;
}
//---------------------------------------------------------------------------------------------
// The following is used during moving selected objects around (ctx is overlayWindowContext).
// Draw the selected objects only, and with an additional alpha multiplied in for more transparency.
void RenderSelectedDrawObjs(CGContextRef ctx, const DrawObjList* objListP, float offsetX, float offsetY, float alpha)
{
CSkObjectPtr obj = objListP->lastItem; // draw from back to front
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, offsetX, offsetY);
while (obj != NULL)
{
if (obj->selected)
{
CGrgba saveStrokeColor = obj->strokeColor;
CGrgba saveFillColor = obj->fillColor;
MakeDrawObjTransparent(obj, alpha);
SetContextStateForDrawObject(ctx, obj);
RenderCSkObject(ctx, obj, true);
obj->strokeColor = saveStrokeColor;
obj->fillColor = saveFillColor;
}
obj = obj->prevObj;
}
CGContextRestoreGState(ctx);
}
//---------------------------------------------------------------------------------------------
// Called at the end of mouse tracking when selected objects have been moved
void MoveSelectedDrawObjs(DrawObjList* objListP, float offsetX, float offsetY)
{
CSkObjectPtr obj = objListP->firstItem;
while (obj != NULL)
{
if (obj->selected)
{
CSkShapeOffset(obj->shape, offsetX, offsetY);
}
obj = obj->nextObj;
}
}
//---------------------------------------------------------------------------------------------
// Select all, or deselect all
void CSkObjListSetSelectState(DrawObjListPtr objList, Boolean state)
{
CSkObjectPtr obj = objList->firstItem;
while (obj != NULL)
{
obj->selected = state;
obj = obj->nextObj;
}
}
//---------------------------------------------------------------------------------------------
// Called when reording objects in the list
static CSkObjectPtr GetFirstSelectedDrawObj(const DrawObjList* objList)
{
CSkObjectPtr obj = objList->firstItem;
CSkObjectPtr selectedObject = NULL;
while (obj != NULL)
{
if (obj->selected == true)
{
selectedObject = obj;
break;
}
obj = obj->nextObj;
}
return selectedObject;
}
//----------------------------------------------------------------------------
// Some obvious linked list management routines
void AddDrawObjToList(DrawObjListPtr objList, CSkObjectPtr obj)
{
CSkObjectPtr firstObj = objList->firstItem;
obj->prevObj = NULL; // always put it in front of the list
obj->nextObj = firstObj;
objList->firstItem = obj;
if (firstObj == NULL)
{
objList->lastItem = obj;
}
else
{
firstObj->prevObj = obj;
}
}
//----------------------------------------------------------------------
static void RemoveDrawObjFromList(DrawObjListPtr objList, const CSkObject* obj)
{
CSkObjectPtr prevObj = obj->prevObj;
CSkObjectPtr nextObj = obj->nextObj;
if (prevObj != NULL)
prevObj->nextObj = nextObj;
if (nextObj != NULL)
nextObj->prevObj = prevObj;
if (objList->firstItem == obj)
objList->firstItem = obj->nextObj;
if (objList->lastItem == obj)
objList->lastItem = obj->prevObj;
}
//----------------------------------------------------------------------
void RemoveSelectedDrawObjs(DrawObjListPtr objList)
{
CSkObjectPtr obj = objList->firstItem;
while (obj != NULL)
{
CSkObjectPtr nextObj = obj->nextObj;
if (obj->selected)
{
RemoveDrawObjFromList(objList, obj);
}
obj = nextObj;
}
}
//----------------------------------------------------------------------
static void InsertDrawObjBefore(DrawObjListPtr objList, CSkObjectPtr obj, CSkObjectPtr beforeObj)
{
CSkObjectPtr prevObj = beforeObj->prevObj;
if (prevObj != NULL)
prevObj->nextObj = obj;
obj->prevObj = prevObj;
obj->nextObj = beforeObj;
beforeObj->prevObj = obj;
if (objList->firstItem == beforeObj)
objList->firstItem = obj;
}
//----------------------------------------------------------------------
static void InsertDrawObjAfter(DrawObjListPtr objList, CSkObjectPtr obj, CSkObjectPtr afterObj)
{
CSkObjectPtr nextObj = afterObj->nextObj;
if (nextObj != NULL)
nextObj->prevObj = obj;
obj->nextObj = nextObj;
obj->prevObj = afterObj;
afterObj->nextObj = obj;
if (objList->lastItem == afterObj)
objList->lastItem = obj;
}
//----------------------------------------------------------------------
static void DuplicateDrawObj(DrawObjListPtr objList, CSkObjectPtr obj, CGPoint offset)
{
CSkObjectPtr tempObj = CopyDrawObject(obj);
// Always deselect the original and select the new
obj->selected = false;
tempObj->selected = true;
// tempObj->bounds = CGRectOffset(tempObj->bounds, offset.x, offset.y); -- this normalizes the rect, which is not what we want for lines
CSkShapeOffset(tempObj->shape, offset.x, offset.y);
InsertDrawObjBefore(objList, tempObj, obj);
}
//----------------------------------------------------------------------
void DuplicateSelectedDrawObjs(DrawObjListPtr objList, CGPoint offset)
{
CSkObjectPtr obj = objList->firstItem;
while (obj != NULL)
{
CSkObjectPtr nextObj = obj->nextObj;
if (obj->selected)
{
DuplicateDrawObj(objList, obj, offset);
}
obj = nextObj;
}
}
//----------------------------------------------------------------------
void MoveObjectForward(DrawObjListPtr objList)
{
CSkObjectPtr obj = GetFirstSelectedDrawObj(objList);
if (obj != NULL)
{
// remember where we were
CSkObjectPtr prevObj = obj->prevObj;
// only remove and insert if there is a previous item
if (prevObj != NULL)
{
RemoveDrawObjFromList(objList, obj); // pull obj out of list
InsertDrawObjBefore(objList, obj, prevObj);
}
}
}
//----------------------------------------------------------------------
void MoveObjectBackward(DrawObjListPtr objList)
{
CSkObjectPtr obj = GetFirstSelectedDrawObj(objList);
if (obj != NULL)
{
// remember where we were
CSkObjectPtr nextObj = obj->nextObj;
// only remove and insert if there is a next
if (nextObj != NULL)
{
RemoveDrawObjFromList(objList, obj); // pull obj out of list
InsertDrawObjAfter(objList, obj, nextObj);
}
}
}
//----------------------------------------------------------------------
void MoveObjectToFront(DrawObjListPtr objList)
{
CSkObjectPtr obj = GetFirstSelectedDrawObj(objList);
if ((obj != NULL) && (obj != objList->firstItem))
{
RemoveDrawObjFromList(objList, obj); // pull obj out of list
AddDrawObjToList(objList, obj); // add it in front
}
}
//----------------------------------------------------------------------
void MoveObjectToBack(DrawObjListPtr objList)
{
CSkObjectPtr obj = GetFirstSelectedDrawObj(objList);
if ((obj != NULL) && (obj != objList->lastItem))
{
RemoveDrawObjFromList(objList, obj); // pull obj out of list
obj->prevObj = objList->lastItem; // hook it in at the end
obj->prevObj->nextObj = obj;
objList->lastItem = obj;
obj->nextObj = NULL;
}
}
//--------------------------------------------------------------------------------------
// Return the first (front-to-back) object hit by docPt, or NULL.
// If hit, *outFlags contains which "grabber" handle has been hit, if any (numbered 1..nGrabbers).
// Note the use of a 1x1-pixel CGBitmapContext bmCtx for hit-testing. We keep it around and pass it in;
// but you could also create it on the spot.
CSkObjectPtr DrawObjListHitTesting(DrawObjListPtr objList, CGContextRef bmCtx, CGAffineTransform m, CGPoint windowCtxPt, CGPoint docPt, UInt32* grabNum)
{
UInt32* baseAddr = (UInt32*)CGBitmapContextGetData(bmCtx); // Assume 4 bytes per pixel!
CSkObjectPtr obj = objList->firstItem; // traverse front to back
Boolean hit = false;
CGContextSaveGState(bmCtx); // because we are temporarily changing the CTM
CGContextTranslateCTM( bmCtx, -windowCtxPt.x, -windowCtxPt.y ); // move 1x1 bitmap context to "windowCtxPt"
CGContextConcatCTM(bmCtx, m); // apply document transform for drawing
while ((obj != NULL) && !hit)
{
int shapeType = CSkShapeGetType(obj->shape);
float d = 0.5 * obj->lineWidth;
CGRect cgR = CSkShapeGetBounds(obj->shape);
cgR = CGRectInset(cgR, -d, -d);
if (CGRectContainsPoint(cgR, docPt))
{
cgR = CGRectInset(cgR, d, d); // restore original shape bounds
// If the obj is selected, check for a hit in the grabbers first since they overlap the obj
if (obj->selected && (grabNum != NULL))
{
*grabNum = FindGrabberHit(obj->shape, docPt);
if (*grabNum > 0)
{
fprintf(stderr, "0x%x: grabber %d hit\n", (uint)obj, (int)*grabNum);
hit = true;
break; // done - return obj
}
}
if (shapeType == kRectShape)
{
// fprintf(stderr, "0x%x: Rectangle hit\n", (uint)obj);
hit = true;
break; // done - return obj
}
// Horizontal or vertical line? Hit if CGRectContainsPoint
if ( (shapeType == kLineShape)
&& ((cgR.size.height == 0) || (cgR.size.width == 0))
)
{
// fprintf(stderr, "0x%x: Horz/Vert Line hit\n", (uint)obj);
hit = true;
break; // done - return obj
}
// If none of the above, draw the object into the bitmapContext, and check whether this changed the point
cgR = CGRectInset(cgR, -d, -d); // outset once more
*baseAddr = 0; // clear the pixel in bmCtx
SetContextStateForDrawObject(bmCtx, obj);
RenderCSkObject(bmCtx, obj, true);
if (*baseAddr != 0) // got it!
{
hit = true;
// fprintf(stderr, "0x%x: BitmapContext hit\n", (uint)obj);
break; // done - return obj
}
else
{
// fprintf(stderr, "no BitmapContext hit for (%f, %f) in (%f, %f, %f, %f)\n",
// docPt.x, docPt.y,
// cgR.origin.x, cgR.origin.y, cgR.size.width, cgR.size.height);
}
}
if (!hit)
obj = obj->nextObj;
}
// obj == NULL if there was no hit
CGContextRestoreGState(bmCtx);
return obj;
}
</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div style="width: 100%; height: 1px; background-color: #919699; margin-top: 5px; margin-bottom: 15px"></div></td>
</tr>