-
Notifications
You must be signed in to change notification settings - Fork 3
/
IconUtils.c
6733 lines (5173 loc) · 228 KB
/
IconUtils.c
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
/*
File: IconUtils.c
Contains: C laguage portions of color icon plotting utilities
Written by: David Collins
Copyright: © 1990-1992 by Apple Computer, Inc., all rights reserved.
Change History (most recent first):
<SM7> 12/3/92 RB Renamed GetClut to IconGetClut to avoid a conflict.
<78> 11/24/92 KSM Fix ROMMapInsert setting in CubeE version (77+74 problem).
<77> 11/23/92 DTY Make the CubeE part of this file build too.
<76> 11/13/92 JDR Updated QuickDraw.h to not use the dangerous pattern type.
<75> 10/26/92 DTY Replace explicit references to ExpandMem globals with macro API.
<74> 9/03/92 JDR Changed noMaskFound to noMaskFoundErr. Removed private inline
version of myGetHandleSize, which must be glue because the
Memory Manager may return MemErr in D0. Removed a duplicate
define for RomMapInsert. Removed private inline to NewGestalt.
<73> 7/2/92 DC Sigh, rolled back the cool changes I made to the 7.0 versions of
IconUtils because the fix I made makes Finder Printing work
differently.
<72> 7/1/92 DC #1032893 <csd>: Changed the onScreen field of the ParamBlock
structure to useCopyMask. The flag is now true if the current
port has user-defined bottlenecks or is recording to a PICT. No
longer overload this flag to mean "always plot 8-bits to a color
port". The world is now safe for Tsunami and the bug is fixed.
<71> 6/5/92 DC Added registry of the Gestalt selector. (conditionalized for
theFuture)
<70> 3/20/92 DC Added UpdateIconGlobals to reinitialize the icon globals in
response to dynamic changes in the make up of the device list.
Used GetExpandMem to replace the GetGlobalPtr.
<69> 3/19/92 DC Changed references to printerPortCache in the globalIconData
structure to a separate ExpandMem reference.
<68> 10/29/91 DC Rolled out all post-7.0 changes by one huge conditional for
Cube-E.
<67> 8/30/91 DC Changed globals from a handle to a ptr (makes code more readable
and avoids locking and unlocking). Changed the way printing is
detected to avoid screen plotting problems during background
printing. Fixed maskRgn to scale back the region to a rectangle
under low memory conditions.
<66> 8/28/91 DC changed name of ParamBlock to IconParamBlock to avoid confusion
with HFS, fixed problem with alignment of shrunken icons. Fixed
end condition in ApplyPattern loop.
<65> 7/15/91 dba get rid of MemErr, TheGDevice, and fixed up the ShortMul and
ShortDiv functions
<64> 3/11/91 DC sad, #84447: took out conditionalization so that the bug fix
will go into 7.0
<63> 3/7/91 DC ngk, #84447: completely factored out 4-bit selection from 4-bit
labeling in MakeClut. Conditionalized with forPost70
<62> 3/5/91 DC csd,#84160: Fixed memory moves in the middle of purging icon
cluts in SetLabel
<61> 2/27/91 DC dba,#DCC004: Icon cluts made purgeable when built as opposed to
when the purged handles are created
<60> 2/22/91 DC dba - added initials and bug number reference: worksheet number
DCC003
<59> 2/21/91 DC Changed transform and hit-test code to use offscreen bitmaps
instead of region operations. Fixed IconToRgn type code to use
less stack space.
<58> 1/30/91 DC KSM - Fixed Memory moving bug in DoTransform
<57> 1/19/91 DC KSM - In the case of an SICN without a mask (where I synthesize
one) the wrong rowBytes is used on the data when generating a
boundary rectangle. The fix is to use maskMap.baseAddr as the
source data for boundary generation.
<56> 1/16/91 DC Fix Greg and Kip's previous fix to my potential divide-by-zero
error
<55> 1/14/91 KIP <gbm> Fix greg's boneheaded mistake.
<54> 1/14/91 DC KSM - Minor performance shore-ups to improve Finder behavior on
Plus's and SE's
<53> 1/11/91 DC KSM - Fixed 2 bugs in PlotCIconHandle which prevented correct
drawing of CIcons with no one-bit data (which is legal) and/or
pixel values outside the range of specified clut entries (also
legal)
<52> 12/14/90 DC KSM - Use true grays wherever possible in 2-bit mode
<51> 12/13/90 ngk need to include PalettesPriv to get CheckColors (used to be in
Palettes)
<50> 12/6/90 DC ngk - Change one bit to color plots to use darken when plotting
labeled icons. Rolled some color back into 4-bits. (Went a
little overboard on change <47>)
<49> 11/29/90 DC ngk - Fix the crashing bug when a mask with no data is present.
<48> 11/27/90 DC KSM - Create some routines needed for menu manager
<47> 10/30/90 DC ngk - Fix labeling algorithm to look good in 4-bits and
gray-scale (if I can)
<46> 10/24/90 DC VL - Add a routine to brighten colors before using them to tint
labeled icons. Rewrite Darken() to be correct and faster.
Changed MakeClut to cause all colors in a clut to be disabled,
rather than just Paulien's colors.
<45> 10/21/90 gbm (with dba) Made compatible with 3.2 C compiler (and incompatible
with previous compilers).
<45> 10/2/90 JAL Changing #pragmas to 3.2 version.
<44> 9/25/90 DC Made two fixes for revision 42. One fixed a bug with saving and
restoring handle states which caused a locked handle not to
plot. This was stopping Alex Kazim. This change was reviewed by
Scott Boyd. The other was a minor change to the way black and
white systems plotted non-transformed icons in an attempt to
boost performance on the plus and SE. This change was refined in
in revision 43, but time-tests showed that it was not a win.
This revision rolls out the change to the black and white draw,
but keeps the necessary change to the way icon handle states are
saved and restored (by merging 42 with 41)
<43> 9/24/90 DC Fixing plotting to Black and White Macs that I broke on my last
fix.
<42> 9/23/90 DC <ngk> Fixed bug with get and SetState using sign extended data
<41> 9/21/90 DC <ksm>Fixed clip region bug.
<40> 9/15/90 DC Rewrote callbacks as assembly, used GetGray in one-bit color
disabled case
<39> 9/15/90 csd Removed the declaration of DeviceLoop and fixed the call to
match the interfaces.
<38> 9/10/90 DC Fixed minor bug in 1-bit color quickdraw opened case
<37> 9/7/90 DC Fix up appearance of disabled state
<36> 9/6/90 JSM SetLabel was setting the length of the string one too many.
<35> 9/6/90 DC Merge in new labeling/darkening algorithm
<34> 9/1/90 dvb Add CIconToIconSuite routine
<33> 8/29/90 DC allow one-bit versions without masks. I.e. ICON
<32> 8/22/90 DC fixed some low-memory condition bugs
<31> 8/9/90 DC Fixed to prevent hanging in icons that have a mask with empty
data
<30> 8/7/90 DC Temporary fix for bug that caused icons not to plot in low
memory conditions
<29> 8/6/90 DC added a true gray disabled state for black and white icons on
color systems (will not degrade correctly yet)
<28> 8/2/90 DC Changed the transfer mode in CopyBits in Render() from srcCopy
to srcOr (makes 'PICT''s look better)
<27> 7/26/90 DC Fixed some dumb bugs that caused malfunction
<26> 7/25/90 DC Converted some routines to assembly language for efficiency
<25> 7/23/90 DC Added CheckColors, fixed black and white transforms
<24> 7/13/90 dba fixed C warnings
<23> 7/12/90 DC Fixed bug in 4-bit mode. Added support for printing and
offscreen cases. Started to add support for graceful
degradation.
<22> 7/5/90 DC Added hit-testing and region-producing
<21> 6/29/90 DC Fix bad resolution values in SetUpPixMap. Added set up of
global data and colorized labeling.
<20> 6/15/90 DC Get my cluts from a global now (although its none of your
business where I get my cluts from)
<19> 6/13/90 DC Fixed 4-bit clut in MakeIconData
<18> 6/13/90 DC Added MakeIconData and associated types
<17> 6/7/90 ngk auto-include quickdraw
<16> 5/31/90 DC Make some fixes to icon itteration
<15> 5/30/90 DC Removed DisposeIconCache and changed name of interface from
IconUtils.h to Icons.h
<14> 5/22/90 DC Made IconCache a subclass of IconSuite.
<13> 5/9/90 DC Use srcOr mode in stead of transparent mode in color plotting
(faster, more correct)
<12> 5/8/90 DC Fixed alignment problem in icon caches. improved efficiency of
loading heuristic
<11> 5/4/90 DC Added many new routines NewIconSuite, ForEachIconDo, etc.
<10> 4/18/90 DC Use typeTable to map IconType values to corresponding ResType
<9> 4/18/90 DC Fixed drawing in 4-bits and small size
<7> 4/11/90 DC Changed for interface fixes
<6> 4/10/90 DC Fixed bug in ploticoncache - might crash if no icon was
available
<5> 4/10/90 DC Merged PlotIconCache and PlotIconSuite into one routine -
PlotBestIcon
<4> 4/9/90 DC IconCache stuff now is working - isn't life strange and
wonderful
<1> 4/7/90 DC first checked in - IconCache stuff not yet working
To Do:
*/
// this is a big conditional used to protect Cube-E from all my post-7.0 changes. I thought
// this was the safest way to roll back completely to 7.0. The code in the else clause of this
// conditional is the entirety (minus header comments) of version 64 of IconUtils.c, which is
// the version that went into 7.0 final.
#if TheFuture
#ifndef __ICONS__
#include <Icons.h>
#endif
#ifndef __EXPANDMEMPRIV__
#include <ExpandMemPriv.h>
#endif
#ifndef __ERRORS__
#include <Errors.h>
#endif
#ifndef __QUICKDRAW__
#include <QuickDraw.h>
#endif
#ifndef __PALETTES__
#include <Palettes.h>
#endif
#include <IconUtilsPriv.h>
#include <PalettesPriv.h>
#ifndef __MEMORY__
#include <Memory.h>
#endif
#ifndef __RESOURCES__
#include <Resources.h>
#endif
#ifndef __TRAPS__
#include <Traps.h>
#endif
#ifndef __SYSEQU__
#include <SysEqu.h>
#endif
#ifndef __OSUTILS__
#include <OSUtils.h>
#endif
#ifndef __QDOFFSCREEN__
#include <QDOffScreen.h>
#endif
#ifndef __GESTALTEQU__
#include <GestaltEqu.h>
#endif
GrafPtr myGetPort() =
{
0x2255, // MOVEA.L (A5), A1 ; Get Pointer to globals
0x2011 // MOVE.L (A1), D0 ; Return current grafport
};
ConstPatternParam GetBlack() =
{
0x70F0, // MOVEQ.L black, D0
0xD095 // ADD.L GrafGlobals(A5), D0
};
ConstPatternParam GetHalfGray() =
{
0x70E8, // MOVEQ.L gray, D0
0xD095 // ADD.L GrafGlobals(A5), D0
};
ConstPatternParam GetLightGray() =
{
0x70E0, // MOVEQ.L ltGray, D0
0xD095 // ADD.L GrafGlobals(A5), D0
};
#define ResErr (*((short *)0xA60))
#define DeviceList (*((GDHandle *)0x8A8))
#define MainDevice (*((GDHandle *)0x8A4))
#define ROM85 (*(unsigned short *) 0x28E)
#define HaveCQD() (ROM85 <= 0x3fff)
// These are machine language multiply and divide routines, which are
// used to avoid using CRuntime.o
#pragma parameter __D0 ShortMul(__D1, __D0)
long ShortMul(short, short)
= 0xC1C1;
#pragma parameter __D0 ShortDiv(__D0, __D1)
short ShortDiv(short, short)
= 0x81C1;
#define MakeLong(aThing) (*((long *)(&(aThing))))
// This external table is defined in IconUtils.a
extern ResType typeTable[IconSpace];
// typedef for call-back used internally for routines like PlotIconGeneric, HitTestIconGeneric, etc.
typedef Handle (*IconRetriever)(long data, IconType theIcon);
// the folowing is the definition of the parameter block used throughout the code
typedef struct
{
PixMap dataMap; // PixMap (or BitMap) structure used for icon image data
BitMap maskMap; // BitMap used for icon mask data
BitMapPtr dstMap; // Pointer to the destination BitMap
Handle theMask, // Handle to one-bit data (usually with mask appended)
theData; // Handle to 4 or 8 bit icon image data
Rect theRect, // destination rectangle (as modified by alignment)
boundary; // boundary of non-zero data within the maskMap bounds.
short rowBytes; // basic rowBytes of data (2 or 4)
unsigned short theTransform, // transform broken out from transform code
theLabel, // label broken out from transform code
selected, // selected state broken out from transform code
dontDraw, // flag which is set to true for LoadIconCache, cuts operation short before drawing
useCopyMask, // true if the icon does not need to go through bottlenecks.
screenDepth, // used to record the depth of the device we'll be drawing to
dataDepth, // used to record the depth of the data we'll be drawing
clutNumber; // index into an array of CTabHandles, specifies CLUT we will use with dataMap
Boolean useRegions; // true if we will use regions to perform transforms, false normally
IconType theGroup; // size category of icon we are using (mini, small or large) also used to indicate a cicn
long userData; // data for icon-data-getting call back
IconRetriever userProc; // ProcPtr to icon-data-getting call back
CTabHandle theClut; // handle to CLUT we will use with dataMap
GDHandle theDevice; // device we are currently drawing to or NULL if black and white or if single device
unsigned long maskBuffer[32]; // buffer in which we store masks we create for one reason or another
} IconParamBlock;
#define HorizontalCodes 0xC // mask for horizontal positioning codes
#define VerticalCodes 0x3 // mask for vertical positioning codes
short DivByTwoShort(short theValue)
{
if (theValue < 0)
++theValue;
theValue >>= 1;
return theValue;
}
// PerformAlignment offsets theBlock->theRect to cause the icon to have the alignment specified by the caller
pascal void PerformAlignment(IconParamBlock *theBlock, short align)
{
short offsetx,
offsety;
Rect *theRect = &(theBlock->theRect),
theBoundary = theBlock->boundary;
MapRect(&theBoundary, &(theBlock->dataMap.bounds), theRect);
switch (align & HorizontalCodes)
{
case atHorizontalCenter:
offsetx = DivByTwoShort((theRect->right + theRect->left) - (theBoundary.right + theBoundary.left));
break;
case atLeft:
offsetx = theRect->left - theBoundary.left;
break;
case atRight:
offsetx = theRect->right - theBoundary.right;
break;
default:
offsetx = 0;
}
switch (align & VerticalCodes)
{
case atVerticalCenter:
offsety = DivByTwoShort((theRect->bottom + theRect->top) - (theBoundary.bottom + theBoundary.top));
break;
case atTop:
offsety = theRect->top - theBoundary.top;
break;
case atBottom:
offsety = theRect->bottom - theBoundary.bottom;
break;
default:
offsety = 0;
}
OffsetRect(theRect, offsetx, offsety);
}
// The following routines are used to create regions from mask BitMap's for IconToRgn-type calls and
// for hit-testing and transforms when an icon is draw larger than 32-bits. They are faster than the
// more general BitMapRgn code because they are optimized for a bitMap no greater that a longword in
// width.
#define TempRgnBufferSize 100
pascal Boolean AddBufferToRgn(RgnHandle theRgn, unsigned short *rgnBuffer, unsigned short *regionPtr)
{
unsigned long incrementSize = (unsigned long)regionPtr - (unsigned long)rgnBuffer;
short rgnSize = (*theRgn)->rgnSize;
SetHandleSize((Handle)theRgn, rgnSize + incrementSize);
if (MemError() != noErr)
return false;
BlockMoveData((Ptr)rgnBuffer, (Ptr)(((unsigned char *)(*theRgn)) + rgnSize), incrementSize);
(*theRgn)->rgnSize += incrementSize;
return true;
}
pascal void MaskRgn(RgnHandle theRgn,
IconParamBlock *theBlock)
{
unsigned char *data = theBlock->maskMap.baseAddr; // This may or may not be locked down
unsigned short rowBytes = theBlock->maskMap.rowBytes,
rowIndex,
rgnBuffer[TempRgnBufferSize],
*regionPtr = rgnBuffer,
spaceLeftInBuffer = TempRgnBufferSize;
unsigned long sizeMask = (rowBytes > 2) ? 0xFFFFFFFF : 0xFFFF0000,
lastRow = 0;
// initialize output region
(*theRgn)->rgnSize = sizeof(Region);
(*theRgn)->rgnBBox = theBlock->boundary;
data += ShortMul(rowBytes, theBlock->boundary.top); // Point to the top of real data
// find the inversion points row-by-row
for (rowIndex = theBlock->boundary.top;
rowIndex <= theBlock->boundary.bottom;
++rowIndex, data += rowBytes)
{
unsigned long thisRow,
inversion;
if (rowIndex != theBlock->boundary.bottom)
{
thisRow = *((unsigned long *)data);
inversion = thisRow ^ lastRow; // inversion is a bit-map difference between this row and the last
lastRow = thisRow;
}
else
inversion = lastRow; // To finish the region, we compare the last row to nothing (all zeros)
inversion &= sizeMask; // Take only the significant data.
// if there is any difference between this row and the last, we describe this difference as a set of inversion points
if (inversion)
{
short columnIndex = 0;
// I'm about to add two words to the region (the row index and the first column index, so I make one check to see
// if I've got two words left in my buffer. If I don't, I dump my temporary buffer to the output region and free
// up the temp buffer.
if (spaceLeftInBuffer < 2)
{
if (!AddBufferToRgn(theRgn, rgnBuffer, regionPtr))
goto error;
regionPtr = rgnBuffer;
spaceLeftInBuffer = TempRgnBufferSize;
}
*regionPtr++ = rowIndex;
// find the first inversion point (may be the first point in the row)
while (!(inversion & 0x80000000))
{
++columnIndex;
inversion <<= 1;
}
// record the first inversion point.
*regionPtr++ = columnIndex++;
// update our gas guage.
spaceLeftInBuffer -= 2;
// find and record all subsequent inversion points
do
{
unsigned long check = inversion & 0xC0000000;
if (check && (check != 0xC0000000))
{
// check if I need to flush my temp buffer
if (!spaceLeftInBuffer)
{
if (!AddBufferToRgn(theRgn, rgnBuffer, regionPtr))
goto error;
regionPtr = rgnBuffer;
spaceLeftInBuffer = TempRgnBufferSize;
}
*regionPtr++ = columnIndex;
--spaceLeftInBuffer;
}
columnIndex++;
inversion <<= 1;
} while (inversion);
// cap off the row with a row-capping marker.
if (!spaceLeftInBuffer)
{
if (!AddBufferToRgn(theRgn, rgnBuffer, regionPtr))
goto error;
regionPtr = rgnBuffer;
spaceLeftInBuffer = TempRgnBufferSize;
}
*regionPtr++ = 0x7FFF;
--spaceLeftInBuffer;
}
}
if (!spaceLeftInBuffer)
{
if (!AddBufferToRgn(theRgn, rgnBuffer, regionPtr))
goto error;
regionPtr = rgnBuffer;
spaceLeftInBuffer = TempRgnBufferSize;
}
*regionPtr++ = 0x7FFF; // cap off the rgn
// If the region is purely rectangular, minimize it and return it.
if (((*theRgn)->rgnSize == sizeof(Region)) &&
(((unsigned long)regionPtr - (unsigned long)rgnBuffer) == 18) &&
(rgnBuffer[1] == rgnBuffer[5]) &&
(rgnBuffer[2] == rgnBuffer[6]))
{
SetHandleSize((Handle)theRgn, sizeof(Region));
return;
}
// Flush the remainder of the buffer to the region.
if (!AddBufferToRgn(theRgn, rgnBuffer, regionPtr))
goto error;
return;
error:
// This handle had to be at least sizeof(Region) to start with and the error occurred
// white trying to size it up. Therefore, SetHandleSize to sizeof(Region) can only
// be sizing it down or leaving it unchanged. It should always work.
SetHandleSize((Handle)theRgn, sizeof(Region));
(*theRgn)->rgnSize = sizeof(Region);
}
//-------------------------------------------------------------------------------------
// Search utilities.
//-------------------------------------------------------------------------------------
// GetBestData finds the best available data that is appropriate for the destination device.
// Initially, theBlock->dataDepth is set equal to the screen depth (by SetUpParamBlock). On
// exit, theData is set to the best available color data, theClut is set to the appropriate
// CLUT to use, and dataDepth is updated to reflect the depth of the output data. If no color
// data is available or appropriate, theData and theClut are set NULL and dataDepth is set to 1,
// all of which means that this is a one-bit plot. GetClut calls CheckClut to see if the clut
// of the specified depth fits into the current color environments. It returns false if it is not,
// causing us to degrade to a smaller bit-depth.
pascal void GetBestData(IconParamBlock *theBlock)
{
long theData = theBlock->userData,
IconGetClut(IconParamBlock *, short);
short checkDepth = 8,
whichIcon = theBlock->theGroup + largeIcon8; // whichIcon is initialized to the code
// for the deepest data in our size group
long IconGetClut(IconParamBlock *, short);
IconRetriever theProc = theBlock->userProc;
void MakeClut(IconParamBlock *);
do {
if ((theBlock->dataDepth >= checkDepth)
&& IconGetClut(theBlock, checkDepth)
&& (theBlock->theData = (*theProc)(theData, whichIcon)))
{
theBlock->dataDepth = checkDepth;
// if the the clut returned by IconGetClut is purged, build a new one
if (!(*(theBlock->theClut)))
MakeClut(theBlock);
return;
}
checkDepth -= 4;
--whichIcon;
} while (checkDepth);
theBlock->dataDepth = 1;
theBlock->theClut = NULL;
}
// ChooseBestSize maps a destination rectangle size to an icon size category.
short ChooseBestSize(const Rect *theRect)
{
int width = theRect->right - theRect->left,
height = theRect->bottom - theRect->top;
if ((width >= 32) || (height >= 32))
return largeIcon1;
if (height > 12)
return smallIcon1;
return miniIcon1;
}
#define FIXED(a) (long)(((a)<<16))
#define THEPIXMAP ((PixMapPtr)theBlock) // only works if the pixmap is the first element
// SetUpPixMap initializes the necessary fields of dataMap for a color plot.
pascal void SetUpPixMap(IconParamBlock *theBlock)
{
unsigned short depth = theBlock->dataDepth;
THEPIXMAP->baseAddr = *(theBlock->theData);
// we use the pixelSize field to indicate whether or not we already have the dataMap
// initialized for a color plot, and if so to what depth.
if (THEPIXMAP->pixelSize == depth)
return;
THEPIXMAP->pmVersion = 0;
THEPIXMAP->packType = 0;
THEPIXMAP->packSize = 0;
THEPIXMAP->hRes = FIXED(72);
THEPIXMAP->vRes = FIXED(72);
THEPIXMAP->pixelType = 0;
THEPIXMAP->cmpCount = 1;
THEPIXMAP->planeBytes = 0;
THEPIXMAP->pixelSize = depth;
THEPIXMAP->cmpSize = depth;
// if the icon is a true CIcon, the rowBytes could be any arbitrary value. Otherwise,
// we can calculate the proper PixMap rowBytes value from the basic rowBytes and the
// depth.
if (theBlock->theGroup != aCIcon)
THEPIXMAP->rowBytes = ShortMul(theBlock->rowBytes, depth) | 0x8000;
else
THEPIXMAP->rowBytes = ((CIconPtr)(theBlock->userProc))->iconPMap.rowBytes;
THEPIXMAP->pmTable = theBlock->theClut;
}
//-------------------------------------------------------------------------------------
// IconSuite Manipulation routines.
//-------------------------------------------------------------------------------------
// __NewIconSuite is called directly by the dispatcher to implement the NewIconSuite call.
pascal OSErr __NewIconSuite(IconSuiteHandle *theSuite)
{
*theSuite = (IconSuiteHandle)NewHandleClear(sizeof(IconSuite));
return MemError();
}
// __MakeIconCache is called directly by the dispatcher to implement the MakeIconCache call.
pascal OSErr __MakeIconCache(IconCacheHandle *theHandle,
IconGetter makeIcon,
void *yourDataPtr)
{
IconCacheHandle theCache;
if (!(theCache = (IconCacheHandle)NewHandleClear(sizeof(IconCache))))
return MemError();
(*theCache)->theSuite.type = CACHE; // set the suite type to indicate that this is a cache
(*theCache)->userPtr = yourDataPtr; // install the call-back and the data.
(*theCache)->userMethod = makeIcon;
*theHandle = theCache;
return noErr;
}
// EditIconSuite is called by __AddIconToSuite and __GetIconFromSuite (defined in IconUtils.a)
// If isAdd is false, EditIconSuite assumes refCon is a pointer to a Handle and returns the
// entry of type theType from theSuite (possibly NULL). If isAdd is true, the value in refcon
// is added to theSuite in the slot for data of type theType.
pascal OSErr EditIconSuite(long refCon, // Handle or referenct to handle of icon data
IconSuiteHandle theSuite,
ResType theType,
long isAdd)
{
Handle *theSlot = (*theSuite)->table;
long theEnd = (long)(theSlot + IconSpace);
ResType *theTable = typeTable;
do
{
if (theType == *theTable++)
{
if (isAdd)
*theSlot = (Handle)refCon;
else
*((Handle *)refCon) = *theSlot;
return noErr;
}
} while ((long)(++theSlot) < theEnd);
return paramErr;
}
// __ForEachIconDo is called by the trap dispatcher to implement the ForEachIconDo call. The action
// call-back is called for every member of theSuite specified by selector (See ERS for selector values).
pascal OSErr __ForEachIconDo(IconSuiteHandle theSuite,
unsigned long selector,
IconAction action,
void *yourDataPtr)
{
Handle *theTable = (Handle *)((*theSuite)->table);
long end = (long)(theTable + IconSpace); // there are more data registers than address
ResType *theTypes = typeTable;
OSErr rval = noErr;
char theState = HGetState((Handle)theSuite);
HLock((Handle) theSuite);
do
{
short i = IconSizeSpace;
do
{
if (selector & 0x00000001)
if (rval = (*action)(*theTypes, theTable, yourDataPtr))
goto out;
theTable++;
theTypes++;
selector >>= 1;
} while (--i);
selector >>= (8 - IconSizeSpace);
} while (theTable < (Handle *)end);
out:
HSetState((Handle) theSuite, theState);
return rval;
}
// This is the call back used with ForEachIconDo to implement the __GetIconSuite
// call below. A more sophisticated call-back could be used to implement a
// Get1IconSuite or to ensure that GetIconSuite only got icons from a single
// file. A user could do this kind of thing by rewriting GetIconSuite with
// their own call-back.
pascal OSErr GetIconFromResource(ResType theType, Handle *theIcon, void *yourDataPtr)
{
*theIcon = GetResource(theType, (short)yourDataPtr);
return noErr;
}
// Implements the GetIconSuite call. The selector allows only specific members of the
// suite to be asked for.
pascal OSErr __GetIconSuite(Handle *theHandle, short theID, unsigned long selector)
{
if (__NewIconSuite((IconSuiteHandle *)theHandle))
return MemError();
__ForEachIconDo((IconSuiteHandle)(*theHandle), selector, GetIconFromResource, (void *)theID);
return noErr;
}
// Implements the SetSuiteLabel call. This specifies a default label for a suite
// which is used if no label is specified by the transform. Yes, this is currently used.
// Namely, by the process manager for the Apple Menu.
pascal OSErr __SetSuiteLabel(IconSuiteHandle theSuite, unsigned short theLabel)
{
if (theLabel > 7)
return paramErr;
theLabel <<= labelShift;
(*theSuite)->label = theLabel;
return noErr;
}
// Implements the GetSuiteLabel. Just for completeness.
pascal short __GetSuiteLabel(IconSuiteHandle theSuite)
{
return (((*theSuite)->label) >> labelShift);
}
// This is the call back used with ForEachIconDo to implement DisposIconSuite. An icon
// is only disposed if it is not a resource.
pascal OSErr Disposer(ResType , Handle *theIcon, void *)
{
if (*theIcon && (HomeResFile(*theIcon) == -1))
DisposHandle(*theIcon);
*theIcon = NULL;
return noErr;
}
// Implements the DisposeIconSuite call. disposeData indicates whether an attempt should
// be made to dispose of the data in the suite. Only non-resource handles will be disposed.
pascal OSErr __DisposeIconSuite(IconSuiteHandle theSuite, Boolean disposeData)
{
if (disposeData)
(void)__ForEachIconDo(theSuite, svAllAvailableData, Disposer, NULL);
DisposHandle((Handle)theSuite);
return MemError();
}
// These are the call-backs used with all the generic routines (PlotIconGeneric, HitTestIconGeneric and
// IconToRgnGeneric) to implement PlotIconID, PlotIconMethod, PlotIconSuite, HitTestIconID, etc. etc.
// FromMethod and FromResource are defined in IconUtils.a
Handle FromMethod(long data, IconType theIcon);
Handle FromResource(long data, IconType theIcon);
Handle FromSuite(long data, IconType theIcon)
{
IconCachePtr theCache = *((IconCacheHandle)data);
Handle rval = theCache->theSuite.table[theIcon];
// if a suite is a cache (See Cache routines below) and we don't have any data, we call the user's
// call back to get some data.
if (!rval && theCache->theSuite.type)
{
IconGetter theProc = theCache->userMethod;
if (theProc)
{
rval = (*theProc)(typeTable[theIcon], theCache->userPtr);
(*((IconSuiteHandle)data))->table[theIcon] = rval;
}
}
return rval;
}
// AdjustRgn checks the from and to rects and decides whether the region needs mapping,
// offsetting or nothing. It is defined in IconUtils.a and is called by DoRegionTransform
// and HitTestIconGeneric.
extern pascal void AdjustRgn(RgnHandle maskRgn, Rect *from, Rect *to);
// DoRegionTransform performs a transform using region operations. This will be called
// when the useRegions flag is true.
pascal void DoRegionTransform(IconParamBlock *theBlock, short theTransform)
{
RgnHandle patRgn;
Boolean isASuite = (theBlock->theGroup != aCIcon);
PenState saveState;
char maskState;
GetPenState(&saveState);
// If we are not a true CIcon, we need to generate a region from the icon mask. The
// locking of theMask is redundant in the color case and extraneous in the case where
// we synthesize a mask but the complexity of determining exactly whether it needs to be done
// dosen't buy that much.
if (isASuite)
{
maskState = HGetState(theBlock->theMask);
HLock(theBlock->theMask);
patRgn = NewRgn();
MaskRgn(patRgn, theBlock);
AdjustRgn(patRgn, &(theBlock->maskMap.bounds), &(theBlock->theRect));
}
else
patRgn = (RgnHandle)theBlock->userData; // This is used for true CIcon to hold a region equivalent
// to its mask (See SetupParamBlockForCIcon)
switch (theTransform)
{
// makes an outline and fills it with a 25% gray pattern
case ttOpen:
FillRgn(patRgn, GetBlack());
InsetRgn(patRgn, 1, 1);
FillRgn(patRgn, GetLightGray());
break;
// Or's over the data (already drawn) with a 25% gray pattern
case ttOffline:
PenPat(GetLightGray());
PenMode(patOr);
PaintRgn(patRgn);
break;
// Bit-clears the already drawn data with a 50% pattern
case ttDisabled:
PenPat(GetHalfGray());
PenMode(patBic);
PaintRgn(patRgn);
break;
}
SetPenState(&saveState);
if (isASuite)
{
DisposeRgn(patRgn);
HSetState(theBlock->theMask, maskState);
}
}
// The following routines are used by DoBitMapTransform to accomplish the various
// transform effects. They rely on the fact that the theBlock->maskMap describes
// exactly the countours of the shape they are transforming. This is guaranteed
// by PrescaleMask (See below)
// DoOutline takes the pre-scaled maskMap and OR's an outline of the icon
// over destination. It is used to implement the open transform.
void DoOutline(BitMapPtr maskMap, unsigned long *destination)
{
unsigned char *source = (unsigned char *)(maskMap->baseAddr);
unsigned long dataMask = (unsigned long)(-1),
lastRow,
row,
nextRow,
destRow,
checkRow,
setMask;
short end = maskMap->bounds.bottom - 1,
i;
// data mask is used to mask off all but the bits belonging to one row of the mask map.
dataMask <<= (32 - maskMap->bounds.right);
// last row represents the row of mask data above the one we're analyzing. It is initialized
// to zero because logicly, there are zero bits outside the bounds of our maskMap.
lastRow = 0L;
// row is the row of mask data we are currently trying to hollow out.
row = *((unsigned long *)source) & dataMask;
source += maskMap->rowBytes;
// we march through the rows of our mask, hollowing our rows as we go. We only go as far
// as the row before the bottom-most row, since we are only interested in examining rows
// that are samwiched between other rows with data.
for (i = 0; i < end; i++)
{
// destRow will be our hollowed out row. It is initialized to row since we don't know if it
// needs hollowing.
destRow = row;
// nextRow is the row below the one we are examining. We advance our source pointer here. We
// won't be advancing off the icon because we're stopping one row before the last.
nextRow = *((unsigned long *)source) & dataMask;
source += maskMap->rowBytes;
// if row has data above and below it, we examine its bits left-to-right. If any one bit has
// a one-bit to left of it, to the right of it, on top of it and below it, we turn that bit
// off in destRow. It works, really.
if (destRow)
{
if (lastRow && nextRow)
{
setMask = 0x40000000;
for (checkRow = row; checkRow; checkRow <<= 1, setMask >>= 1)
if (((checkRow & 0xE0000000) == 0xE0000000) && (setMask & lastRow) && (setMask & nextRow))
destRow ^= setMask;
}
// here we OR the destRow with the destination data
*destination |= destRow;
}
// advance the destination pointer as well as lastRow and row.
++destination;
lastRow = row;
row = nextRow;