forked from cellularmitosis/ADC-reference-library-2009-july
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisting3.html
More file actions
executable file
·1400 lines (1205 loc) · 51.3 KB
/
Copy pathlisting3.html
File metadata and controls
executable file
·1400 lines (1205 loc) · 51.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
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>MenuViews - /LabelItemView.cp</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');">MenuViews</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">MenuViews</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>/LabelItemView.cp</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">/GridMenu.c</option>
<option value="listing2.html">/GridMenu.h</option>
<option value="listing3.html">/LabelItemView.cp</option>
<option value="listing4.html">/LabelItemView.h</option>
<option value="listing5.html">/main.cp</option>
<option value="listing6.html">/MenuViews_Prefix.h</option>
<option value="listing7.html">/VolumeMenu.c</option>
<option value="listing8.html">/VolumeMenu.h</option></select>
</p>
</form>
<p><strong><a href="MenuViews.zip">Download Sample</a></strong> (“MenuViews.zip”, 106.0K)<BR>
<strong><a href="MenuViews.dmg">Download Sample</a></strong> (“MenuViews.dmg”, 170.1K)</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: LabelItemView.cp
Version: Mac OS X
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 © 2003 Apple Computer, Inc., All Rights Reserved
*/
#include "LabelItemView.h"
#include "TView.h"
//======================================================================================
// * Constants
//======================================================================================
#define kTViewHILabelViewClassID CFSTR( "com.apple.sample.HILabelView" ) // HILabelView class ID
#define kRolloverTimerDelay 0.1 // delay before the rollover timer fires
enum
{
kLabelNameHeight = 15, // height allowed for the label name
kLabelPartWidth = 17, // width of the entire label part
kLabelPartHeight = 16, // height of the entire label part
kFirstLabelSpace = 5 // extra space between the "none" part and the first label part
};
// menu item property constants
enum
{
kMenuPropertyLabelViewCreator = 'Labl', // used for all LabelView properties
kMenuPropertyLabelViewTag = 'LbVw' // an HILabelView*
};
//======================================================================================
// * Types
//======================================================================================
class HILabelView : public TView
{
public:
static OSStatus Create( MenuRef inMenu, MenuItemIndex inMenuItem, HIViewRef inMenuContentView, HIViewRef* outView );
protected:
HILabelView( HIViewRef inControl );
~HILabelView();
ControlKind GetKind();
OSStatus Initialize( TCarbonEvent& inEvent );
OSStatus ControlHit( HIViewPartCode inPart, UInt32 inModifiers );
void Draw( RgnHandle inLimitRgn, CGContextRef inContext );
OSStatus GetRegion( ControlPartCode inPart, RgnHandle outRgn );
HIViewPartCode HitTest( const HIPoint& inWhere );
OSStatus Track( TCarbonEvent& inEvent, ControlPartCode* outPartHit );
OSStatus GetSizeConstraints( HISize* outMin, HISize* outMax );
OSStatus GetOptimalSizeSelf( HISize* outSize, float* outBaseLine );
void VisibilityChanged();
private:
enum { kNumParts = 8 }; // seven label buttons, plus the "none" button
static const EventTypeSpec kControlEvents[];
static const EventTypeSpec kWindowEvents[];
static const EventTypeSpec kMenuEvents[];
static const EventTypeSpec kMenuContentEvents[];
static CGImageRef sImages[ kNumParts ];
static CGImageRef sSelectedBackgroundImage;
static CGImageRef sPressedBackgroundImage;
static CFStringRef sTitleString;
static CFStringRef sLabelStrings[ 7 ];
MenuRef fMenu; // menu that owns this label item view
MenuItemIndex fMenuItem; // menu item that is being used to draw this label item view
HIViewRef fMenuContentView; // menu content view in which we are embedded
EventHandlerRef fMenuHandler; // event handler installed on the menu
EventHandlerRef fMenuContentHandler; // event handler installed on the menu content view
EventHandlerRef fWindowHandler; // event handler installed on our owning window
EventLoopTimerRef fRolloverTimer; // event loop timer used to avoid flicker
HIViewPartCode fRollover; // the item part that is currently showing rollover effects
HIViewPartCode fTextRollover; // during SimulateHit, the item part that shows rollover for its text
HIViewPartCode fPartRollover; // during SimulateHit, the item part that shows rollover for its part
HIViewPartCode fNewRollover; // the item part that the mouse has just moved over
OptionBits fSelected; // which labels are selected
int fLeftEdge; // x offset from left side of view to left edge of text and label parts
float fPartYOffset; // y offset from top of label view to top of label parts
static OSStatus Construct( HIObjectRef inObjectRef, TObject** outObject );
static pascal OSStatus EventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon );
static pascal void RolloverChangedTimer( EventLoopTimerRef inTimer, void* inRefcon );
static CFStringRef GetTitleString();
static CFStringRef GetLabelString( int inPart );
static OSStatus LoadPartImages();
static int LabelIndexFromPart( int inPart );
static CGImageRef GetPartImage( int inPart );
static CGImageRef GetSelectedBackgroundImage();
static CGImageRef GetPressedBackgroundImage();
static CGImageRef GetBackgroundImage( CFStringRef inName, CGImageRef* ioImage );
MenuItemIndex FindItem();
HILabelView* GetMenuItemViewProperty( MenuItemIndex inItem );
void RedrawWithDelay( int inDelayTicks );
void DrawCenteredImage( CGContextRef inContext, CGPoint inCenter, CGImageRef inImage );
void GetTitleBounds( HIRect* outBounds );
void GetLabelBounds( HIRect* outBounds );
void GetPartBounds( int inPart, HIRect* outBounds );
int GetLeftEdge();
void ClearRollover()
{
fRollover = fTextRollover = fPartRollover = fNewRollover = 0;
}
};
static void _HIViewConvertFromGlobalPoint( HIPoint* ioPoint, HIViewRef inDestView );
//======================================================================================
// * Globals
//======================================================================================
const EventTypeSpec HILabelView::kControlEvents[] =
{
{ kEventClassControl, kEventControlVisibilityChanged }
};
const EventTypeSpec HILabelView::kWindowEvents[] =
{
{ kEventClassMouse, kEventMouseMoved },
{ kEventClassMouse, kEventMouseDragged }
};
const EventTypeSpec HILabelView::kMenuEvents[] =
{
{ kEventClassMenu, kEventMenuMeasureItemWidth },
{ kEventClassMenu, kEventMenuMeasureItemHeight },
{ kEventClassMenu, kEventMenuDrawItem },
{ kEventClassMenu, kEventMenuCalculateSize },
{ kEventClassCommand, kEventCommandProcess },
/*
These are currently required to support rollover effects in contextual menus.
For pull-down menus, we get these events by registering on the menu window;
however, the WWDC build of HIToolbox sends these events to the clicked window
rather than to the menu window for contextual menus, so we need to register
for them on the menu as well. This problem is fixed in HIToolbox for the post-
WWDC builds, and once those builds are available, these handlers can be removed.
*/
{ kEventClassMouse, kEventMouseMoved },
{ kEventClassMouse, kEventMouseDragged }
};
const EventTypeSpec HILabelView::kMenuContentEvents[] =
{
{ kEventClassControl, kEventControlSimulateHit }
};
CGImageRef HILabelView::sImages[];
CGImageRef HILabelView::sSelectedBackgroundImage;
CGImageRef HILabelView::sPressedBackgroundImage;
CFStringRef HILabelView::sTitleString;
CFStringRef HILabelView::sLabelStrings[];
//======================================================================================
// * Functions and methods
//======================================================================================
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * HILabelViewCreate
// Creates a new instance of the label item view.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelViewCreate( MenuRef inMenu, MenuItemIndex inMenuItem, HIViewRef inMenuContentView, HIViewRef* outView )
{
return HILabelView::Create( inMenu, inMenuItem, inMenuContentView, outView );
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * HILabelView constructor
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
HILabelView::HILabelView(
HIViewRef inControl )
: TView( inControl ),
fMenu( NULL ),
fMenuItem( 0 ),
fMenuContentView( NULL ),
fMenuHandler( NULL ),
fMenuContentHandler( NULL ),
fWindowHandler( NULL ),
fRolloverTimer( NULL ),
fRollover( 0 ),
fTextRollover( 0 ),
fPartRollover( 0 ),
fNewRollover( 0 ),
fSelected( 0 ),
fLeftEdge( 0 ),
fPartYOffset( 0 )
{
// add autoinvalidation
ChangeAutoInvalidateFlags( kAutoInvalidateOnHilite |
kAutoInvalidateOnActivate |
kAutoInvalidateOnEnable,
0 );
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * HILabelView destructor
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
//
HILabelView::~HILabelView()
{
if ( fMenuHandler != NULL )
RemoveEventHandler( fMenuHandler );
if ( fMenuContentHandler != NULL )
RemoveEventHandler( fMenuContentHandler );
if ( fWindowHandler != NULL )
RemoveEventHandler( fWindowHandler );
if ( fRolloverTimer != NULL )
RemoveEventLoopTimer( fRolloverTimer );
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * Create
// Public class method for creating a HILabelView.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelView::Create(
MenuRef inMenu,
MenuItemIndex inMenuItem,
HIViewRef inMenuContentView,
HIViewRef* outView )
{
OSStatus err = noErr;
EventRef event = CreateInitializationEvent(); // create initialization event
require_action( event != NULL, CantCreateEvent, err = eventInternalErr );
// add extra parameters
if ( inMenu != NULL )
SetEventParameter( event, kEventParamMenuRef, typeMenuRef, sizeof( inMenu ), &inMenu );
if ( inMenuItem != 0 )
SetEventParameter( event, kEventParamMenuItemIndex, typeMenuItemIndex, sizeof( inMenuItem ), &inMenuItem );
if ( inMenuContentView != 0 )
SetEventParameter( event, kEventParamControlRef, typeControlRef, sizeof( inMenuContentView ), &inMenuContentView );
// register the subclass
static bool sRegistered = false;
if( !sRegistered )
{
RegisterSubclass( kTViewHILabelViewClassID, Construct );
sRegistered = true;
}
// instantiate the object
err = HIObjectCreate( kTViewHILabelViewClassID, event, (HIObjectRef*) outView );
ReleaseEvent( event );
CantCreateEvent:
return err;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * GetKind
// Returns this view's ControlKind.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
//
ControlKind
HILabelView::GetKind()
{
static const ControlKind kHILabelViewKind = { 'LblI', 'LblI' };
return kHILabelViewKind;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * Construct
// Allocates a new HILabelView object.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelView::Construct(
HIObjectRef inObjectRef,
TObject** outObject )
{
*outObject = new HILabelView( (HIViewRef) inObjectRef );
return noErr;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * Initialize
// Initializes a new HILabelView object.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelView::Initialize(
TCarbonEvent& inEvent )
{
OSStatus err;
MenuItemIndex item;
err = TView::Initialize( inEvent );
require_noerr( err, CantInitBaseClass );
inEvent.GetParameter( kEventParamMenuRef, &fMenu );
item = inEvent.GetParameter<MenuItemIndex>( kEventParamMenuItemIndex, typeMenuItemIndex );
// install a handler on ourselves for a few events that are not currently wrapped by TView
InstallControlEventHandler( GetViewRef(), EventHandler, GetEventTypeCount( kControlEvents ),
kControlEvents, this, NULL );
if ( fMenu != NULL )
{
//
// We can't allow this menu to be cached, because the appearance of the label menu item
// changes dynamically. If the menu image is cached, then the menu will always initially
// display the label item as it was before the user started interacting with it (selecting/
// unselecting label colors), and any changes to the label item won't be initially visible.
//
ChangeMenuAttributes( fMenu, kMenuAttrDoNotCacheImage, 0 );
//
// Insert a new item that will have the specified item index. Use CustomDraw
// for this item so we can override MeasureItemWidth/Height to give it the
// right size for our view.
//
InsertMenuItemTextWithCFString( fMenu, NULL, item - 1, kMenuItemAttrCustomDraw, 0 );
//
// Set a menu property on the item that we've inserted so that we can identify this item
// later, even if other items are inserted or removed before it (which will change its index).
//
HILabelView* view = this;
SetMenuItemProperty( fMenu, item, kMenuPropertyLabelViewCreator, kMenuPropertyLabelViewTag, sizeof( this ), &view );
//
// Install our handlers to implement custom measurement for this item.
// The view itself will provide the custom drawing.
//
InstallMenuEventHandler( fMenu, EventHandler, GetEventTypeCount( kMenuEvents ), kMenuEvents,
this, &fMenuHandler );
//
// If we are given a menu content view, attach ourselves to it.
//
inEvent.GetParameter( kEventParamControlRef, &fMenuContentView );
if ( fMenuContentView != NULL )
{
HIViewAddSubview( fMenuContentView, GetViewRef() );
InstallEventHandler( GetControlEventTarget( fMenuContentView ), EventHandler,
GetEventTypeCount( kMenuContentEvents ), kMenuContentEvents,
this, &fMenuContentHandler );
}
}
CantInitBaseClass:
return err;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * ControlHit
// Handles clicks on the parts of a label item.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelView::ControlHit( HIViewPartCode inPart, UInt32 inModifiers )
{
UInt32 oldSelected;
if ( inPart == 1 )
fSelected = 0;
else
fSelected ^= 1 << ( inPart - 1 );
if ( oldSelected != fSelected )
HIViewSetNeedsDisplay( GetViewRef(), true );
return noErr;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * Draw
// Draws the label item.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
void
HILabelView::Draw(
RgnHandle inLimitRgn,
CGContextRef inContext )
{
HIRect bounds;
GetTitleBounds( &bounds );
HIThemeTextInfo textInfo = { kHIThemeTextInfoVersionZero, kThemeStateActive, kThemeMenuItemFont,
kHIThemeTextHorizontalFlushLeft, kHIThemeTextVerticalFlushTop, 0,
kHIThemeTextTruncationNone, 1, 0 };
HIThemeDrawTextBox( GetTitleString(), &bounds, &textInfo, inContext, kHIThemeOrientationNormal );
HIViewPartCode rollPart = fTextRollover;
if ( rollPart == 0 )
rollPart = fRollover;
if ( rollPart > 1 )
{
CFStringRef str = GetLabelString( rollPart );
if ( str != NULL )
{
GetLabelBounds( &bounds );
textInfo.fontID = kThemeSmallEmphasizedSystemFont;
textInfo.horizontalFlushness = kHIThemeTextHorizontalFlushCenter;
textInfo.truncationPosition = kHIThemeTextTruncationEnd;
// draw this text in gray
CGContextSetRGBFillColor( inContext, 0.5, 0.5, 0.5, 1.0 );
HIThemeDrawTextBox( str, &bounds, &textInfo, inContext, kHIThemeOrientationNormal );
}
}
rollPart = fPartRollover;
if ( rollPart == 0 /* && GetCurrentEventButtonState() != 0 */ )
rollPart = fRollover;
for ( int i = 1; i <= kNumParts; i++ )
{
Boolean rollover = i == rollPart;
Boolean pressed = i == GetControlHilite( GetViewRef() );
Boolean selected = ( fSelected & ( 1 << ( i - 1 ) ) ) != 0;
GetPartBounds( i, &bounds );
CGPoint center = { bounds.origin.x + ( bounds.size.width / 2 ),
bounds.origin.y + ( bounds.size.height / 2 ) };
if ( rollover )
// ideally we'd have a custom image here for the rollover effect, rather than re-using the pressed image
DrawCenteredImage( inContext, center, GetPressedBackgroundImage() );
else
if ( pressed )
DrawCenteredImage( inContext, center, GetPressedBackgroundImage() );
else if ( selected )
DrawCenteredImage( inContext, center, GetSelectedBackgroundImage() );
DrawCenteredImage( inContext, center, GetPartImage( i ) );
}
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * GetTitleString
// Returns the string that is drawn above the label buttons ("Color Label:").
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
CFStringRef
HILabelView::GetTitleString()
{
if ( sTitleString == NULL )
sTitleString = CFBundleCopyLocalizedString( CFBundleGetMainBundle(), CFSTR("HILabelTitle"), NULL, NULL );
return sTitleString;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * GetLabelString
// Returns a string containing the name of a label part;
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
CFStringRef
HILabelView::GetLabelString( int inPart )
{
check( inPart >= 2 );
check( inPart <= 8 );
// convert from 1-based label part index to zero-based label array index
int index = inPart - 2;
// *** we need to invalidate this cache when the label changes
if ( sLabelStrings[ index ] == NULL )
{
RGBColor rgb;
Str255 st;
GetLabel( LabelIndexFromPart( inPart ), &rgb, st );
CFStringRef labelStr = CFStringCreateWithPascalString( NULL, st, GetApplicationTextEncoding() );
if ( labelStr != NULL )
{
CFStringRef formatStr = CFBundleCopyLocalizedString( CFBundleGetMainBundle(),
CFSTR("HILabelFormat"),
NULL, NULL );
if ( formatStr != NULL )
{
sLabelStrings[ index ] = CFStringCreateWithFormat( NULL, NULL, formatStr, labelStr );
CFRelease( formatStr );
}
CFRelease( labelStr );
}
}
return sLabelStrings[ index ];
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * DrawCenteredImage
// Draws a CGImageRef centered around a point.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
void
HILabelView::DrawCenteredImage( CGContextRef inContext, CGPoint inCenter, CGImageRef inImage )
{
float imageWidth = CGImageGetWidth( inImage );
float imageHeight = CGImageGetHeight( inImage );
HIRect bounds;
bounds.size.width = imageWidth;
bounds.size.height = imageHeight;
bounds.origin.x = inCenter.x - ( imageWidth / 2 );
bounds.origin.y = inCenter.y - ( imageHeight / 2 );
HIViewDrawCGImage( inContext, &bounds, inImage );
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// *\xCAGetRegion
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelView::GetRegion( ControlPartCode inPart, RgnHandle outRgn )
{
//
// We provide the clickable meta-region so that if the user clicks in portions of the view
// that are not trackable, the click can pass through the control and initiate async dragging.
//
if ( inPart == kControlClickableMetaPart )
{
RgnHandle tempRgn = NewRgn();
if ( tempRgn == NULL )
return eventNotHandledErr;
for ( int i = 1; i <= kNumParts; i++ )
{
HIRect bounds;
GetPartBounds( i, &bounds );
SetRectRgn( tempRgn,
(short) bounds.origin.x, (short) bounds.origin.y,
(short) CGRectGetMaxX( bounds ), (short) CGRectGetMaxY( bounds ) );
UnionRgn( tempRgn, outRgn, outRgn );
}
DisposeRgn( tempRgn );
return noErr;
}
else
{
return eventNotHandledErr;
}
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// * HitTest
// Hit-tests a point against the label item view.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
HIViewPartCode
HILabelView::HitTest(
const HIPoint& inWhere )
{
HIViewPartCode part = kControlNoPart;
// is the mouse on the item?
if( CGRectContainsPoint( Bounds(), inWhere ) )
{
for ( int i = 1; i <= kNumParts; i++ )
{
HIRect bounds;
GetPartBounds( i, &bounds );
if ( CGRectContainsPoint( bounds, inWhere ) )
{
part = i;
break;
}
}
}
return part;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// *\xCATrack
// Handles control tracking. We only use it to clear our rollover state.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelView::Track( TCarbonEvent& inEvent, ControlPartCode* outPartHit )
{
//
// Clear rollover state so that it doesn't interfere with proper part drawing during tracking.
// Otherwise, when you move the mouse out of the part that you clicked on, the part remains
// drawn in its rollover state because fRollover still contains that part.
//
ClearRollover();
// let the Control Manager's standard control tracking occur
return eventNotHandledErr;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// *\xCAGetSizeConstraints
// Returns our minimum and maximum sizes. Required for views that are used as custom
// toolbar item views.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelView::GetSizeConstraints( HISize* outMin, HISize* outMax )
{
GetOptimalSize( outMin, NULL );
*outMax = *outMin;
return noErr;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// *\xCAGetOptimalSizeSelf
// Returns our optimal size. Required for views that are used as custom toolbar item
// views.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
OSStatus
HILabelView::GetOptimalSizeSelf( HISize* outSize, float* outBaseLine )
{
// get the right edge of the last part
HIRect partBounds;
GetPartBounds( kNumParts, &partBounds );
outSize->width = CGRectGetMaxX( partBounds );
// add the origin of the first part, so that the same space is on the right side of the last part
GetPartBounds( 1, &partBounds );
outSize->width += partBounds.origin.x;
HIThemeTextInfo textInfo = { kHIThemeTextInfoVersionZero, kThemeStateActive, kThemeMenuItemFont,
kHIThemeTextHorizontalFlushLeft, kHIThemeTextVerticalFlushTop,
0, kHIThemeTextTruncationNone, 0 };
HIThemeGetTextDimensions( CFSTR("Sample"), 0, &textInfo, NULL, &fPartYOffset, outBaseLine );
//
// Calculate the bounds of the first part. This will take the new value of fPartYOffset
// into account. The bottom of the part bounds is our view height.
//
GetPartBounds( 1, &partBounds );
outSize->height = CGRectGetMaxY( partBounds ) + kLabelNameHeight;
return noErr;
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// *\xCAVisibilityChanged
// Our visibility has changed. Effectively, this is called when the menu or toolbar
// containing us has changed visibility.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
void
HILabelView::VisibilityChanged()
{
//
// Whenever the label view becomes visible, we install a handler on our owning window
// to track mouse-moved and mouse-dragged events; this lets us show rollover feedback as
// the mouse passes over the label parts.
//
if ( IsVisible() )
{
if ( fWindowHandler == NULL )
{
InstallEventHandler( GetWindowEventTarget( GetControlOwner( GetViewRef() ) ), EventHandler,
GetEventTypeCount( kWindowEvents ),
kWindowEvents, this, &fWindowHandler );
}
// start out with no rollover highlighting
ClearRollover();
}
else
{
if ( fWindowHandler != NULL )
{
RemoveEventHandler( fWindowHandler );
fWindowHandler = NULL;
}
}
}
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
// *\xCAEventHandler
// Handles various events for the view.
//ÑÑÑÑÑ\xD1\xD1\xD1\xD1\xD1\xD1
pascal OSStatus
HILabelView::EventHandler( EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon )
{
TCarbonEvent event( inEvent );
OSStatus result = eventNotHandledErr;
HILabelView* pView = (HILabelView*) inRefcon;
if ( event.GetClass() == kEventClassMouse )
{
check( event.GetKind() == kEventMouseMoved || event.GetKind() == kEventMouseDragged );
if ( pView->GetOwner() == NULL || !IsWindowVisible( pView->GetOwner() ) )
return eventNotHandledErr;
HIPoint pt;
event.GetParameter<HIPoint>( kEventParamMouseLocation, typeHIPoint, &pt );
_HIViewConvertFromGlobalPoint( &pt, pView->GetViewRef() );
pView->fNewRollover = pView->HitTest( pt );
if ( pView->fRollover != pView->fNewRollover )
{
HIRect firstLabelBounds;
HIRect lastLabelBounds;
pView->GetPartBounds( 2, &firstLabelBounds );
pView->GetPartBounds( kNumParts, &lastLabelBounds );
//
// If we are now over a label part, immediately invalidate the label name for redraw.
// Also do this if the mouse passed outside of the area used by the label parts, so
// that we immediately erase the name in this case. By passing fRolloverTimer (which
// may NULL or may be a valid timer) to RolloverChangedTimer, we re-use its behavior
// to remove any installed timer.
//
// If we are not over a label part, delay the invalidation just a little to see if the
// user moves the mouse over another label part. If so, then we'll get back here again
// and we'll invalidate the old and new parts, but never completely erase the label name;
// this avoids flicker. If the user doesn't quickly move the mouse over another label,
// then the timer will fire and we'll invalidate and erase the label name.
//
if ( pView->fNewRollover != 0 || pt.x < firstLabelBounds.origin.x || pt.x > CGRectGetMaxX( lastLabelBounds ) )
RolloverChangedTimer( pView->fRolloverTimer, pView );
else if ( pView->fRolloverTimer == NULL )
InstallEventLoopTimer( GetMainEventLoop(), kRolloverTimerDelay, 0, RolloverChangedTimer, pView, &pView->fRolloverTimer );
else
SetEventLoopTimerNextFireTime( pView->fRolloverTimer, kRolloverTimerDelay );
}
check( result == eventNotHandledErr );
}
else if ( event.GetClass() == kEventClassMenu )
{
switch ( event.GetKind() )
{
case kEventMenuMeasureItemWidth:
{
MenuItemIndex item = event.GetParameter<MenuItemIndex>( kEventParamMenuItemIndex, typeMenuItemIndex );
if ( pView->GetMenuItemViewProperty( item ) == pView )
{
HISize size;
pView->GetOptimalSize( &size, NULL );
SInt16 width = (SInt16) size.width;
event.SetParameter<SInt16>( kEventParamMenuItemWidth, typeShortInteger, width );
result = noErr;
}
break;
}
case kEventMenuMeasureItemHeight:
{
MenuItemIndex item = event.GetParameter<MenuItemIndex>( kEventParamMenuItemIndex, typeMenuItemIndex );
if ( pView->GetMenuItemViewProperty( item ) == pView )
{
HISize size;
pView->GetOptimalSize( &size, NULL );
SInt16 height = (SInt16) size.height;
event.SetParameter<SInt16>( kEventParamMenuItemHeight, typeShortInteger, height );
result = noErr;
}
break;
}
case kEventMenuDrawItem:
// draw nothing
result = noErr;
break;
//
// When the menu is asked to calculate its size, let it do so, and then reposition
// the label view to match the bounds of the menu item that we're implementing.
//
case kEventMenuCalculateSize:
result = CallNextEventHandler( inCaller, inEvent );
if ( result == noErr )
{
MenuItemIndex item = pView->FindItem();
if ( item != 0 )
{
RgnHandle itemRgn = NewRgn();
if ( itemRgn != NULL )
{
GetControlRegion( pView->fMenuContentView, item, itemRgn );
Rect bounds;
GetRegionBounds( itemRgn, &bounds );
DisposeRgn( itemRgn );
HIRect frame = { { bounds.left, bounds.top },
{ bounds.right - bounds.left, bounds.bottom - bounds.top } };
HIViewSetFrame( pView->GetViewRef(), &frame );
}
}
}
break;
}
}
else if ( event.GetClass() == kEventClassControl )
{
switch ( event.GetKind() )
{
// a future version of TView will provide this handler automatically
case kEventControlVisibilityChanged:
pView->VisibilityChanged();
break;
case kEventControlSimulateHit:
{
ControlPartCode part = event.GetParameter<ControlPartCode>( kEventParamControlPart, typeControlPartCode );
if ( pView->GetMenuItemViewProperty( part ) == pView )
{
// remember which item was under the mouse
HIViewPartCode selectedPart = pView->fRollover;
// detect the selected item even if the user releases the mouse before RolloverChangedTimer fires
if ( selectedPart != 0 )
selectedPart = pView->fNewRollover;
// if we are over an item, flash it to show feedback, and invert its state
if ( selectedPart != 0 )
{
enum { kFlashTicks = 5 };
// turn off normal rollover effects
pView->ClearRollover();
// make sure that rollover text continues to be displayed (don't blink it on and off)
pView->fTextRollover = selectedPart;
// turn off part rollover
pView->fPartRollover = 0;
pView->RedrawWithDelay( kFlashTicks );
// turn on part rollover
pView->fPartRollover = selectedPart;
pView->RedrawWithDelay( kFlashTicks );
// turn off part rollover
pView->fPartRollover = 0;
pView->RedrawWithDelay( kFlashTicks );
// invert selected state for this part
pView->ControlHit( selectedPart, 0 );
pView->RedrawWithDelay( kFlashTicks );
}
// we've handled this SimulateHit event; the standard menu view does not need to be called
result = noErr;
}
break;
}
default:
break;
}
}
else if ( event.GetClass() == kEventClassCommand )
{
HICommand cmd;