-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisting2.html
executable file
·1667 lines (1405 loc) · 63 KB
/
listing2.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>CarbonMDEF - /Sample.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">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/Carbon/index.html">Carbon</a> > <a href="../../samplecode/Carbon/idxHumanInterfaceToolbox-date.html">Human Interface Toolbox</a> > <A HREF="javascript:location.replace('index.html');">CarbonMDEF</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">CarbonMDEF</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>/Sample.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">/main.c</option>
<option value="listing2.html">/Sample.c</option>
<option value="listing3.html">/Sample.h</option></select>
</p>
</form>
<p><strong><a href="CarbonMDEF.zip">Download Sample</a></strong> (“CarbonMDEF.zip”, 21.4K)<BR>
<strong><a href="CarbonMDEF.dmg">Download Sample</a></strong> (“CarbonMDEF.dmg”, 84.6K)</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: Sample.c
Contains: Sample Appearance-savvy, Mac OS X-compatible custom MDEF.
Version: 1.0
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 © 2000-2001 Apple Computer, Inc., All Rights Reserved
*/
#define TARGET_API_MAC_CARBON 1
#include <Carbon.h>
#include "Sample.h"
/*
This sample code demonstrates how to write an Appearance-savvy, Mac OS X-compatible MDEF.
It imitates the appearance the standard system MDEF as far as possible.
Features of the standard MDEF that aren't implemented here:
kMenuDrawItemsMsg
icons
glyph-based command keys
custom menu font
custom menu item text encoding, font, and style
right-to-left drawing
pencil glyph substitution
This code has not yet been compiled or run on Mac OS 9. It should be fairly close to
implementing an Appearance-savvy MDEF for 9, but will require at least CarbonLib 1.3.1
for DrawThemeTextBox.
There are several tricky platform-specific and OS-version-specific issues to deal with
in writing an Appearance-savvy MDEF. The major ones:
- Because menus use a window buffer that contains an alpha channel, drawing into the
menu must be done with CoreGraphics and not Quickdraw if you would like to have
a transparent menu. Quickdraw does not understand alpha channels and will simply
set the alpha channel component of each pixel to 0xFF (making the menu opaque).
If you are drawing with the Appearance Manager menu-drawing APIs, you will automatically
use CoreGraphics and your menus will be transparent.
- Because Quickdraw does not deal with alpha channels, you cannot use Quickdraw to
scroll a menu's contents without making the menu opaque. Mac OS X 10.1 and later, and
CarbonLib 1.5 and later, provide a new ScrollMenuImage API that scrolls the menu
using CoreGraphics. On Mac OS X 10.0.x, there is no good substitute for ScrollMenuImage;
this sample code just uses ScrollRect.
- Because menus are partially transparent on Mac OS X, when the Appearance Manager
draws the menu background inside DrawThemeMenuBackground, it composites the menu
background together with whatever was previously visible in the menu's window
buffer. If the menu background is being redrawn because a previously selected menu
item is being redrawn in an unhilited state, then the previous content of the
menu's buffer is the blue hilited menu item, and a small fraction of the blue hilite
will show through the background of the unhilited menu item. The solution to this
is to erase the area where the menu background will be drawn before drawing the
background. Mac OS X 10.1 and later, and CarbonLib 1.5 and later, will offer a new
API to do this, EraseMenuBackground; on Mac OS X 10.0.x, you must use CoreGraphics
directly.
- The GetThemeMetric API in Mac OS X 10.1 and later, and in CarbonLib 1.5 and later, has
several new theme metrics to aid in the layout of a menu. These metrics are not available
in 10.0.x and must be hard-coded on earlier systems.
- The check, diamond, bullet, and dash characters, when drawn as mark characters for a
menu item by the Appearance Manager, are drawn with special customized glyphs that are
different from the glyphs in the system font. On Mac OS 10.0.x, the public Appearance
Manager APIs do not provide any way to get these special glyphs. On Mac OS X 10.1 and
later, the DrawThemeTextBox API has been modified to provide these glyphs automatically,
so an MDEF should simply always use DrawThemeTextBox to draw the mark characters.
- When drawing the command, option, control, and shift modifier glyphs in a menu item,
for compatibility with both Mac OS X 10.0.x and 10.1, you must create the CFString
containing the glyph characters using CFStringCreateWithBytes, providing a source
string containing the kMenuCommand/Option/Control/Glyph character codes from Menus.h
and using the kTextEncodingMacKeyboardGlyphs encoding. If you do not need compatibility
with 10.0.x, and your MDEF will only run on later releases of Mac OS X, then you can
create the CFString by specifying the Unicode characters for these glyphs directly
(these Unicode characters are listed in Events.h).
- The following theme fonts are used when drawing a menu:
kThemeMenuItemFont used for: menu item text, menu command key
kThemeMenuItemMarkFont used for: menu mark character
kThemeMenuItemCmdKeyFont used for: command key modifier glyphs, menu
command key if specified by glyph instead of
character code, menu mark if the mark's character
code is less than 32 (ASCII space)
- DrawThemeTextBox uses the ThemeDrawState constant to modify the shadow and boldness
of the text; it does not, however, modify the text color based on the draw state.
It assumes that you have already set up the text color appropriately using
SetThemeTextColor. However, there is currently no API for setting the text color
of a CoreGraphics context based on ThemeDrawState. For this reason, DrawThemeTextBox
makes an exception to its no-text-color-setup rule; if you pass NULL for the context,
the text color will be set up appropriately for you. This sample code therefore always
passes NULL as the CGContextRef parameter to DrawThemeTextBox, even though a context
is passed to the MDEF by the Menu Manager.
*/
//
// determines whether the MDEF is compatible with Mac OS X 10.0.x (set to zero)
// or only with Mac OS X 10.1 and later (set to one)
//
#define AFTER_MACOSX_10_0_x 0
/*==================================================================================================*/
/* *\xCAConstants */
/*==================================================================================================*/
// for our wrapper around GetThemeMetric
enum
{
kThemeMenuMetricMarkColumnWidth,
kThemeMenuMetricExcludedMarkColumnWidth,
kThemeMenuMetricMarkIndent,
kThemeMenuMetricTextLeadingEdgeMargin,
kThemeMenuMetricTextTrailingEdgeMargin,
kThemeMenuMetricIndentWidth,
kThemeMenuMetricIconTrailingEdgeMargin
};
typedef int ThemeMenuMetric;
/*==================================================================================================*/
/* *\xCATypes */
/*==================================================================================================*/
// for our wrapper glue around EraseMenuBackground, ScrollMenuImage, and CGContextClearRect
typedef OSStatus
(*EraseMenuBackgroundProc)(
MenuRef inMenu,
const Rect * inEraseRect,
CGContextRef inContext);
typedef OSStatus
(*ScrollMenuImageProc)(
MenuRef inMenu,
const Rect * inScrollRect,
int inHScroll,
int inVScroll,
CGContextRef inContext);
typedef void
(*CGContextClearRectProc)(
CGContextRef context,
CGRect rect);
// useful metrics for menu drawing
typedef struct
{
SInt16 extraWidth; // extra width for each item
SInt16 extraHeightPlain; // extra height for plain text items
SInt16 extraHeightIcon; // extra height for items with icons
SInt16 markWidth; // width of the column used to draw the mark character
SInt16 excludedMarkWidth; // width of the column when the mark character is excluded
SInt16 markIndent; // indent into the mark column where drawing starts
SInt16 textLeadingMargin; // margin on the leading edge of the text to where text drawing starts
SInt16 textTrailingMargin; // margin on the trailing edge of the text to the end of the text box
SInt16 indentWidth; // space allocated per indent level as set by SetMenuItemIndent
SInt16 itemHeight; // height of a plain text icon
SInt16 separatorHeight; // height of a separator
SInt16 itemBaseline; // distance from top of item text to baseline
SInt16 cmdGlyphWidth; // width of the command key symbol; used as command key width for items without command keys
SInt16 cmdCharWidth; // width of 'W' in the menu item font; used as width of all command chars
}
MenuMetrics;
// info needed to calculate the size of an item
typedef struct
{
MenuMetrics metrics;
MenuItemDataRec itemData;
}
MenuItemCalcInfo;
// our MenuItem drawing callback gets a pointer to this in its userData
typedef struct
{
MenuRef menu;
MenuAttributes menuAttr;
MenuItemCalcInfo calcInfo;
CGContextRef context;
Boolean itemSelected;
}
MenuItemDrawInfo;
/*==================================================================================================*/
/* * Prototypes */
/*==================================================================================================*/
static void DrawMenu( MenuRef menu, const Rect* bounds, MenuTrackingData* trackingData, CGContextRef context );
static void SetupItemDrawInfo( MenuRef menu, CGContextRef context, MenuItemDrawInfo* outDrawInfo );
static void SetupItemRect( const Rect* menuBounds, const MenuTrackingData* trackingData, Rect* outItemRect );
static void DrawItem( MenuRef menu, MenuItemIndex item, const Rect* menuRect, const Rect* itemRect,
const MenuTrackingData* trackingData, Boolean eraseFirst,
const MenuItemDrawInfo* drawInfo, CGContextRef context );
static void FetchMenuItemData( MenuRef menu, MenuItemIndex item, MenuItemDataRec* outItemData );
static void ReleaseMenuItemData( const MenuItemDataRec* itemData );
//static ThemeMenuType GetThemeMenuType( MenuRef menu );
static ThemeMenuState GetItemState( MenuItemAttributes attr, Boolean hilite );
static ThemeMenuItemType GetItemType( MenuRef menu, MenuItemIndex item, const MenuItemDataRec* itemData, Boolean eraseFirst );
static MenuItemDrawingUPP GetItemDrawingProc();
static pascal void ItemDrawingProc( const Rect *inBounds, SInt16 inDepth, Boolean inIsColorDevice, SInt32 inUserData);
static void DrawCharTextBox( Byte ch, TextEncoding encoding, ThemeFontID font, ThemeDrawState drawState,
const Rect* bounds, int baseline, int just, CGContextRef context );
static int MeasureUnicode( const UniChar* chars, ByteCount length, ThemeFontID font );
static void DrawUnicode( const UniChar*, ByteCount length, ThemeFontID font, ThemeDrawState drawState, const Rect* bounds, int baseline, int just, CGContextRef context );
static void DrawScrollArrow( MenuRef menu, const Rect* bounds, MenuTrackingData* trackingData, Boolean downArrow, const MenuMetrics* metrics, CGContextRef context );
static void SizeMenu( MenuRef menu, Point maxSizes );
static void GetMenuMetrics( MenuMetrics* outMetrics );
static UniChar GetCommandGlyph( void );
static void CalcItemSize( MenuRef menu, const MenuItemCalcInfo* calcInfo, int* outWidth, int* outHeight );
static int GetCommandKeyWidth( const MenuItemCalcInfo* calcInfo );
static const UniChar* BuildModifierString( UInt32 modifiers, ByteCount* outLength );
static void CalcMenuPopUpRect( MenuRef menu, Rect* bounds, int mouseH, int mouseV, short* whichItem );
static void FindMenuItem( MenuRef menu, const Rect* bounds, Point hitPt, MenuTrackingData* trackingData, CGContextRef context );
static void AutoScroll( MenuRef menu, const Rect* bounds, Point hitPt, MenuTrackingData* trackingData, MenuItemIndex prevItemSelected, const MenuMetrics* metrics, CGContextRef context );
static void DrawScrolledItem( MenuRef menu, MenuTrackingData* trackingData, const Rect* menuRect, const Rect* itemRect, const MenuMetrics* metrics, CGContextRef context );
static void HiliteMenuItem( MenuRef menu, const Rect* bounds, HiliteMenuItemData* hiliteData, CGContextRef context );
static void HiliteItem( MenuRef menu, const Rect* bounds, MenuTrackingData* trackingData, int i, Boolean hilite, CGContextRef context );
static void CalcMenuItemBounds( MenuRef menu, Rect* bounds, int i );
static void GetItemRect( MenuRef menu, const MenuTrackingData* trackingData, const Rect* bounds, int whichItem, int hintItem, int hintBottom, const MenuItemCalcInfo* whichItemInfo, Rect* itemRect );
static int GetThemeMenuMetric( ThemeMenuMetric metric );
static Boolean HasNoBackground();
static Boolean HasAqua();
static void DoEraseMenuBackground( MenuRef menu, const Rect* rect, CGContextRef context );
static void DoCGContextClearRect( CGContextRef context, const Rect* rect );
static void DoScrollMenuImage( MenuRef menu, const Rect* bounds, int dh, int dv, CGContextRef context );
/*==================================================================================================*/
/* *\xCAFunctions */
/*==================================================================================================*/
/*--------------------------------------------------------------------------------------------------*/
inline Boolean
HasCommandKey( const MenuItemDataRec* itemData )
{
// the standard MDEF also checks for cmdKeyGlyph and kMenuItemAttrUseVirtualKey here
return itemData->cmdKey != 0;
}
/*--------------------------------------------------------------------------------------------------*/
inline CGContextRef
GetTextContext( CGContextRef context )
{
//
// There is currently no API for setting up the appropriate text color for a ThemeDrawState
// in a CGContext. If you pass NULL to DrawThemeTextBox, it will set up the color for you,
// so we use NULL even though ideally we should be passing the actual context.
//
return NULL;
}
/*--------------------------------------------------------------------------------------------------*/
pascal void
SampleMDEF( short msg, MenuRef menu, Rect* bounds, Point hitPt, short* whichItem )
{
switch ( msg )
{
case kMenuInitMsg:
*whichItem = noErr;
break;
case kMenuDisposeMsg:
break;
case kMenuDrawMsg:
DrawMenu( menu, bounds, (MenuTrackingData*) whichItem, (CGContextRef) ((MDEFDrawData*) whichItem)->context );
break;
case kMenuSizeMsg:
SizeMenu( menu, hitPt );
break;
case kMenuPopUpMsg:
CalcMenuPopUpRect( menu, bounds, hitPt.v, hitPt.h, whichItem );
break;
case kMenuFindItemMsg:
FindMenuItem( menu, bounds, hitPt, (MenuTrackingData*) whichItem, (CGContextRef) ((MDEFFindItemData*) whichItem)->context );
break;
case kMenuHiliteItemMsg:
HiliteMenuItem( menu, bounds, (HiliteMenuItemData*) whichItem, (CGContextRef) ((MDEFHiliteItemData*) whichItem)->context );
break;
case kMenuCalcItemMsg:
CalcMenuItemBounds( menu, bounds, *whichItem );
break;
case kMenuThemeSavvyMsg:
*whichItem = kThemeSavvyMenuResponse;
break;
default:
break;
}
}
/*--------------------------------------------------------------------------------------------------*/
static void
DrawMenu( MenuRef menu, const Rect* bounds, MenuTrackingData* trackingData, CGContextRef context )
{
int i;
int cItems;
MenuItemDrawInfo drawInfo;
Rect itemRect;
trackingData->virtualMenuBottom = 0;
NormalizeThemeDrawingState();
SetupItemDrawInfo( menu, context, &drawInfo );
SetupItemRect( bounds, trackingData, &itemRect );
cItems = CountMenuItems( menu );
for ( i = 1; i <= cItems; i++ )
{
int height;
FetchMenuItemData( menu, i, &drawInfo.calcInfo.itemData );
CalcItemSize( menu, &drawInfo.calcInfo, NULL, &height );
itemRect.bottom = itemRect.top + height;
if ( ! ( itemRect.bottom <= bounds->top || itemRect.top >= bounds->bottom ) )
DrawItem( menu, i, bounds, &itemRect, trackingData, false, &drawInfo, context );
ReleaseMenuItemData( &drawInfo.calcInfo.itemData );
trackingData->virtualMenuBottom = itemRect.bottom;
itemRect.top = itemRect.bottom;
}
if ( trackingData->virtualMenuTop < bounds->top )
DrawScrollArrow( menu, bounds, trackingData, false, &drawInfo.calcInfo.metrics, context );
if ( trackingData->virtualMenuBottom > bounds->bottom )
DrawScrollArrow( menu, bounds, trackingData, true, &drawInfo.calcInfo.metrics, context );
}
/*--------------------------------------------------------------------------------------------------*/
static void
SetupItemDrawInfo( MenuRef menu, CGContextRef context, MenuItemDrawInfo* outDrawInfo )
{
outDrawInfo->menu = menu;
GetMenuAttributes( menu, &outDrawInfo->menuAttr );
GetMenuMetrics( &outDrawInfo->calcInfo.metrics );
outDrawInfo->context = context;
outDrawInfo->itemSelected = false;
}
/*--------------------------------------------------------------------------------------------------*/
static void
SetupItemRect( const Rect* menuBounds, const MenuTrackingData* trackingData, Rect* outItemRect )
{
outItemRect->left = menuBounds->left;
outItemRect->right = menuBounds->right;
outItemRect->top = trackingData->virtualMenuTop;
outItemRect->bottom = outItemRect->top;
}
/*--------------------------------------------------------------------------------------------------*/
static void
DrawItem( MenuRef menu, MenuItemIndex item, const Rect* menuRect, const Rect* itemRect,
const MenuTrackingData* trackingData, Boolean eraseFirst,
const MenuItemDrawInfo* drawInfo, CGContextRef context )
{
if ( eraseFirst )
DoEraseMenuBackground( menu, itemRect, context );
if ( ( drawInfo->calcInfo.itemData.attr & kMenuItemAttrSeparator ) != 0 )
{
DrawThemeMenuSeparator( itemRect );
}
else
{
DrawThemeMenuItem( menuRect, itemRect,
trackingData->virtualMenuTop, trackingData->virtualMenuBottom,
GetItemState( drawInfo->calcInfo.itemData.attr, drawInfo->itemSelected ),
GetItemType( menu, item, &drawInfo->calcInfo.itemData, eraseFirst ),
GetItemDrawingProc(), (UInt32) drawInfo );
}
}
/*--------------------------------------------------------------------------------------------------*/
static void
FetchMenuItemData( MenuRef menu, MenuItemIndex item, MenuItemDataRec* outItemData )
{
BlockZero( outItemData, sizeof( MenuItemDataRec ) );
outItemData->whichData = kMenuItemDataMark
| kMenuItemDataCmdKey
| kMenuItemDataCmdKeyModifiers
| kMenuItemDataSubmenuID
| kMenuItemDataSubmenuHandle
| kMenuItemDataEnabled
| kMenuItemDataAttributes
| kMenuItemDataCFString
| kMenuItemDataIndent;
CopyMenuItemData( menu, item, false, outItemData );
}
/*--------------------------------------------------------------------------------------------------*/
static void
ReleaseMenuItemData( const MenuItemDataRec* itemData )
{
if ( itemData->cfText != NULL )
CFRelease( itemData->cfText );
}
#if 0 // not currently used
/*--------------------------------------------------------------------------------------------------*/
static ThemeMenuType
GetThemeMenuType( MenuRef menu )
{
ThemeMenuType menuType;
GetMenuType( menu, &menuType );
if ( !IsMenuItemEnabled( menu, 0 ) )
menuType |= kThemeMenuTypeInactive;
return menuType;
}
#endif
/*--------------------------------------------------------------------------------------------------*/
static ThemeMenuState
GetItemState( MenuItemAttributes attr, Boolean hilite )
{
if ( hilite )
return kThemeMenuSelected;
else if ( ( attr & kMenuItemAttrDisabled ) != 0 )
return kThemeMenuDisabled;
else
return kThemeMenuActive;
}
/*--------------------------------------------------------------------------------------------------*/
static ThemeMenuItemType
GetItemType( MenuRef menu, MenuItemIndex item, const MenuItemDataRec* itemData, Boolean eraseFirst )
{
ThemeMenuType menuType;
ThemeMenuItemType itemType = kThemeMenuItemPlain;
GetMenuType( menu, &menuType );
if ( itemData->submenuHandle != NULL || itemData->submenuID != 0 )
itemType |= kThemeMenuItemHierarchical;
if ( item == 1 )
itemType |= kThemeMenuItemAtTop;
else if ( item == CountMenuItems( menu ) )
itemType |= kThemeMenuItemAtBottom;
if ( menuType == kThemeMenuTypeHierarchical )
itemType |= kThemeMenuItemHierBackground;
else if ( menuType == kThemeMenuTypePopUp )
itemType |= kThemeMenuItemPopUpBackground;
if ( !eraseFirst && HasNoBackground() )
itemType |= kThemeMenuItemNoBackground;
return itemType;
}
/*--------------------------------------------------------------------------------------------------*/
static MenuItemDrawingUPP
GetItemDrawingProc()
{
static MenuItemDrawingUPP sDrawingProc;
if ( sDrawingProc == NULL )
sDrawingProc = NewMenuItemDrawingUPP( ItemDrawingProc );
return sDrawingProc;
}
/*--------------------------------------------------------------------------------------------------*/
static pascal void
ItemDrawingProc(const Rect *inBounds, SInt16 inDepth, Boolean inIsColorDevice, SInt32 inUserData)
{
#pragma unused( inDepth, inIsColorDevice )
MenuItemDrawInfo* drawInfo = (MenuItemDrawInfo*) inUserData;
ThemeDrawState drawState;
Rect bounds = *inBounds;
Rect boundsT;
int baseline = bounds.top + drawInfo->calcInfo.metrics.itemBaseline;
if ( drawInfo->itemSelected )
drawState = kThemeStatePressed;
else if ( ( drawInfo->calcInfo.itemData.attr & kMenuItemAttrDisabled ) != 0 )
drawState = kThemeStateInactive;
else
drawState = kThemeStateActive;
// indent
if ( drawInfo->calcInfo.itemData.indent > 0 )
bounds.left += drawInfo->calcInfo.itemData.indent * drawInfo->calcInfo.metrics.indentWidth;
// mark character
if ( ( drawInfo->menuAttr & kMenuAttrExcludesMarkColumn ) == 0 )
{
boundsT = bounds;
bounds.left += drawInfo->calcInfo.metrics.markWidth;
if ( drawInfo->calcInfo.itemData.mark != 0 )
{
ThemeFontID font = kThemeMenuItemMarkFont;
TextEncoding encoding = GetApplicationTextEncoding();
if ( drawInfo->calcInfo.itemData.mark < kSpaceCharCode )
{
font = kThemeMenuItemCmdKeyFont;
encoding = kTextEncodingMacKeyboardGlyphs;
}
boundsT.left += drawInfo->calcInfo.metrics.markIndent;
boundsT.right = bounds.left;
DrawCharTextBox( drawInfo->calcInfo.itemData.mark, encoding, font, drawState,
&boundsT, baseline, teFlushDefault, drawInfo->context );
}
}
// text
if ( drawInfo->calcInfo.itemData.cfText != NULL )
{
boundsT = bounds;
boundsT.left += drawInfo->calcInfo.metrics.textLeadingMargin;
DrawThemeTextBox( drawInfo->calcInfo.itemData.cfText, kThemeMenuItemFont, drawState,
false, &boundsT, teFlushDefault, GetTextContext( drawInfo->context ) );
}
// command key
if ( HasCommandKey( &drawInfo->calcInfo.itemData ) )
{
ByteCount cch;
const UniChar* modifiers;
// the command key character itself
boundsT = bounds;
boundsT.left = boundsT.right - drawInfo->calcInfo.metrics.cmdCharWidth;
DrawCharTextBox( drawInfo->calcInfo.itemData.cmdKey, GetApplicationTextEncoding(), kThemeMenuItemFont,
drawState, &boundsT, baseline, teFlushDefault, drawInfo->context );
// the modifiers
boundsT.right = boundsT.left;
boundsT.left = bounds.right - GetCommandKeyWidth( &drawInfo->calcInfo );
modifiers = BuildModifierString( drawInfo->calcInfo.itemData.cmdKeyModifiers, &cch );
DrawUnicode( modifiers, cch, kThemeMenuItemCmdKeyFont, drawState, &boundsT,
baseline, teFlushDefault, drawInfo->context );
}
}
/*--------------------------------------------------------------------------------------------------*/
static void
DrawCharTextBox( Byte ch, TextEncoding encoding, ThemeFontID font, ThemeDrawState drawState,
const Rect* bounds, int baseline, int just, CGContextRef context )
{
Rect adjustedBounds = *bounds;
CFStringRef string = CFStringCreateWithBytes( NULL, &ch, 1, encoding, false );
/*
Menu item text drawn with the .Keyboard font (used for kThemeMenuItemCmdKeyFont) won't
always have the same ascent and baseline as text drawn with the regular menu item font,
since the glyphs in the .Keyboard font may have a different height. Therefore, we first
determine the baseline of the text and then adjust the bounds rect so the baseline aligns
with the overall baseline of the menu item.
*/
if ( font == kThemeMenuItemCmdKeyFont )
{
Point size;
SInt16 cmdKeyBaseline;
GetThemeTextDimensions( string, kThemeMenuItemCmdKeyFont, drawState, false, &size, &cmdKeyBaseline );
OffsetRect( &adjustedBounds, 0, baseline - bounds->top - size.v - cmdKeyBaseline );
}
DrawThemeTextBox( string, font, drawState, false, &adjustedBounds, just, GetTextContext( context ) );
CFRelease( string );
}
/*--------------------------------------------------------------------------------------------------*/
static int
MeasureUnicode( const UniChar* chars, ByteCount length, ThemeFontID font )
{
CFStringRef str = CFStringCreateWithCharacters( NULL, chars, length );
Point pt = {0, 0};
SInt16 baseline;
GetThemeTextDimensions( str, font, kThemeStateActive, false, &pt, &baseline );
CFRelease( str );
return pt.h;
}
/*--------------------------------------------------------------------------------------------------*/
static void
DrawUnicode( const UniChar* chars, ByteCount length, ThemeFontID font, ThemeDrawState drawState,
const Rect* bounds, int baseline, int just, CGContextRef context )
{
Rect adjustedBounds = *bounds;
CFStringRef string = CFStringCreateWithCharacters( NULL, chars, length );
/*
Menu item text drawn with the .Keyboard font (used for kThemeMenuItemCmdKeyFont) won't
always have the same ascent and baseline as text drawn with the regular menu item font,
since the glyphs in the .Keyboard font may have a different height. Therefore, we first
determine the baseline of the text and then adjust the bounds rect so the baseline aligns
with the overall baseline of the menu item.
*/
if ( font == kThemeMenuItemCmdKeyFont )
{
Point size;
SInt16 cmdKeyBaseline;
GetThemeTextDimensions( string, kThemeMenuItemCmdKeyFont, drawState, false, &size, &cmdKeyBaseline );
OffsetRect( &adjustedBounds, 0, baseline - bounds->top - size.v - cmdKeyBaseline );
}
DrawThemeTextBox( string, font, drawState, false, &adjustedBounds, just, GetTextContext( context ) );
CFRelease( string );
}
/*--------------------------------------------------------------------------------------------------*/
static void
DrawScrollArrow( MenuRef menu, const Rect* bounds, MenuTrackingData* trackingData, Boolean downArrow,
const MenuMetrics* metrics, CGContextRef context )
{
Rect itemRect = *bounds;
ThemeMenuState menuState;
ThemeMenuType menuType;
ThemeMenuItemType itemType;
if ( downArrow )
itemRect.top = itemRect.bottom - metrics->itemHeight;
else
itemRect.bottom = itemRect.top + metrics->itemHeight;
//
// If the entire menu is inactive, disable the item so that it matches the rest of the menu.
// The menu still scrolls, so technically the item really shouldn't be inactive, but it looks
// wrong otherwise.
//
if ( IsMenuItemEnabled( menu, 0 ) )
menuState = kThemeMenuActive;
else
menuState = kThemeMenuDisabled;
if ( downArrow )
itemType = kThemeMenuItemScrollDownArrow;
else
itemType = kThemeMenuItemScrollUpArrow;
GetMenuType( menu, &menuType );
if ( menuType == kThemeMenuTypeHierarchical )
itemType |= kThemeMenuItemHierBackground;
else if ( menuType == kThemeMenuTypePopUp )
itemType |= kThemeMenuItemPopUpBackground;
// the arrow's background will be drawn on top of whatever was there before, so erase first
DoEraseMenuBackground( menu, &itemRect, context );
DrawThemeMenuItem( bounds, &itemRect, trackingData->virtualMenuTop, trackingData->virtualMenuBottom,
menuState, itemType, NULL, NULL );
}
/*--------------------------------------------------------------------------------------------------*/
static void
SizeMenu( MenuRef menu, Point maxSizes )
{
int i;
int cItems;
int menuWidth = 0;
int menuHeight = 0;
MenuItemCalcInfo calcInfo;
SInt32 result;
int maxWidth;
int maxHeight;
Boolean hasCmdKey = false;
// determine the maximum allowed width and height of the menu
if ( ( Gestalt( gestaltMenuMgrAttr, &result ) == noErr )
&& ( result & gestaltMenuMgrSendsMenuBoundsToDefProcMask ) != 0 )
{
maxWidth = maxSizes.h;
maxHeight = maxSizes.v;
}
else
{
Rect mainDeviceRect = (*GetMainDevice())->gdRect;
maxWidth = mainDeviceRect.right - mainDeviceRect.left;
maxHeight = mainDeviceRect.bottom - mainDeviceRect.top - GetMBarHeight();
}
GetMenuMetrics( &calcInfo.metrics );
//
// Determine the true width and height of the menu. Note that we must examine every item,
// even if the height of the menu has already exceeded the maximum height, because we need
// to calculate the maximum width based on the width of every item.
//
cItems = CountMenuItems( menu );
for ( i = 1; i <= cItems; i++ )
{
int itemWidth;
int itemHeight;
int newHeight;
FetchMenuItemData( menu, i, &calcInfo.itemData );
hasCmdKey |= HasCommandKey( &calcInfo.itemData );
CalcItemSize( menu, &calcInfo, &itemWidth, &itemHeight );
ReleaseMenuItemData( &calcInfo.itemData );
if ( itemWidth > menuWidth )
menuWidth = itemWidth;
newHeight = menuHeight + itemHeight;
if ( newHeight <= maxHeight )
menuHeight = newHeight;
}
//
// CalcItemSize will add the command key width to every item, even if the item doesn't have a command key.
// This gives a better appearance when just some items have command keys. However, if no item has a command
// key, then we remove the command key width entirely.
//
if ( !hasCmdKey && menuWidth >= calcInfo.metrics.cmdGlyphWidth )
menuWidth -= calcInfo.metrics.cmdGlyphWidth;
if ( menuWidth > maxWidth )
menuWidth = maxWidth;
if ( menuHeight > maxHeight )
menuHeight = maxHeight;
SetMenuWidth( menu, menuWidth );
SetMenuHeight( menu, menuHeight );
}
/*--------------------------------------------------------------------------------------------------*/
static void
CalcItemSize( MenuRef menu, const MenuItemCalcInfo* calcInfo, int* outWidth, int* outHeight )
{
MenuAttributes menuAttr;
// initial height
if ( outHeight != NULL )
{
if ( ( calcInfo->itemData.attr & kMenuItemAttrSeparator ) != 0 )
*outHeight = calcInfo->metrics.separatorHeight;
else
*outHeight = calcInfo->metrics.itemHeight;
}
// initial width
if ( outWidth == NULL )
return;
else
*outWidth = 0;
// indent
if ( calcInfo->itemData.indent > 0 )
*outWidth += calcInfo->itemData.indent * calcInfo->metrics.indentWidth;
// mark character
GetMenuAttributes( menu, &menuAttr );
if ( ( menuAttr & kMenuAttrExcludesMarkColumn ) != 0 )
*outWidth += calcInfo->metrics.excludedMarkWidth;
else
*outWidth += calcInfo->metrics.markWidth;
// text
if ( calcInfo->itemData.cfText != NULL )
{
Point pt;
SInt16 baseline;
GetThemeTextDimensions( calcInfo->itemData.cfText, kThemeMenuItemFont, kThemeStateActive, false, &pt, &baseline );
*outWidth += pt.h + calcInfo->metrics.textLeadingMargin + calcInfo->metrics.textTrailingMargin;
}
// command key and modifiers
*outWidth += GetCommandKeyWidth( calcInfo );
// theme-specified extra spacing
*outWidth += calcInfo->metrics.extraWidth;
}
/*--------------------------------------------------------------------------------------------------*/
static int
GetCommandKeyWidth( const MenuItemCalcInfo* calcInfo )
{
int width = 0;
if ( HasCommandKey( &calcInfo->itemData ) )
{
width = calcInfo->metrics.cmdCharWidth;
if ( calcInfo->itemData.cmdKeyModifiers == 0 )
{
width += calcInfo->metrics.cmdGlyphWidth;
}
else
{
ByteCount cch;
const UniChar* modifiers = BuildModifierString( calcInfo->itemData.cmdKeyModifiers, &cch );
width += MeasureUnicode( modifiers, cch, kThemeMenuItemCmdKeyFont );
}
}
else
{
width = calcInfo->metrics.cmdGlyphWidth;
}
return width;
}
/*--------------------------------------------------------------------------------------------------*/
static const UniChar*
BuildModifierString( UInt32 modifiers, ByteCount* outLength )
{
static UniChar ustr[4];
ByteCount cch = 0;
/*
Mac OS X 10.0.x expects the CFStringRef used when drawing with kMenuItemCmdKeyFont to contain
characters with values in the range given in Menus.h for glyph codes in the .Keyboard font.
Those glyph codes are actually not valid Unicode values for the associated characters; that
Mac OS X 10.0.x requires them is a bug in that release.
Mac OS X 10.1 and later expects the CFStringRef to contain characters with the proper Unicode
values (kCommand/Option/Shift/ControlUnicode).
It so happens that creating a CFStringRef with CFStringCreateWithBytes, using byte values from
the .Keyboard font encoding, and specifying kTextEncodingMacKeyboardGlyphs, will create a CFString
with the right Unicode values on both platforms. That is the approach used by this sample code.
However, if your MDEF will only run on Mac OS X 10.1 and later, then it's somewhat more efficient
to just create the CFString by specifying the correct Unicode values in the first place.
*/
#if AFTER_MACOSX_10_0_x
if ( ( modifiers & kMenuControlModifier ) != 0 )
ustr[cch++] = kControlUnicode;
if ( ( modifiers & kMenuOptionModifier ) != 0 )
ustr[cch++] = kOptionUnicode;
if ( ( modifiers & kMenuShiftModifier ) != 0 )
ustr[cch++] = kShiftUnicode;
if ( ( modifiers & kMenuNoCommandModifier ) == 0 )
ustr[cch++] = kCommandUnicode;
#else
static Byte str[4];
CFStringRef cfString;
if ( ( modifiers & kMenuControlModifier ) != 0 )
str[cch++] = kMenuControlGlyph;
if ( ( modifiers & kMenuOptionModifier ) != 0 )
str[cch++] = kMenuOptionGlyph;
if ( ( modifiers & kMenuShiftModifier ) != 0 )
str[cch++] = kMenuShiftGlyph;
if ( ( modifiers & kMenuNoCommandModifier ) == 0 )
str[cch++] = kMenuCommandGlyph;
cfString = CFStringCreateWithBytes( NULL, str, cch, kTextEncodingMacKeyboardGlyphs, false );
check( cfString != NULL );
check( CFStringGetLength( cfString ) <= 4 );