forked from cellularmitosis/ADC-reference-library-2009-july
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisting15.html
More file actions
executable file
·2041 lines (1685 loc) · 66.2 KB
/
Copy pathlisting15.html
File metadata and controls
executable file
·2041 lines (1685 loc) · 66.2 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>ImageBrowserView - /ImageBrowser/TImageBrowserView.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');">ImageBrowserView</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">ImageBrowserView</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>/ImageBrowser/TImageBrowserView.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">/HIFramework/HIFramework.h</option>
<option value="listing2.html">/HIFramework/TCarbonEvent.cp</option>
<option value="listing3.html">/HIFramework/TCarbonEvent.h</option>
<option value="listing4.html">/HIFramework/TEventHandler.cp</option>
<option value="listing5.html">/HIFramework/TEventHandler.h</option>
<option value="listing6.html">/HIFramework/TObject.cp</option>
<option value="listing7.html">/HIFramework/TObject.h</option>
<option value="listing8.html">/HIFramework/TRect.cp</option>
<option value="listing9.html">/HIFramework/TRect.h</option>
<option value="listing10.html">/HIFramework/TView.cp</option>
<option value="listing11.html">/HIFramework/TView.h</option>
<option value="listing12.html">/ImageBrowser/ImageBrowserView.c</option>
<option value="listing13.html">/ImageBrowser/ImageBrowserView.h</option>
<option value="listing14.html">/ImageBrowser/main.c</option>
<option value="listing15.html">/ImageBrowser/TImageBrowserView.cp</option>
<option value="listing16.html">/ImageBrowser/TImageBrowserView.h</option></select>
</p>
</form>
<p><strong><a href="ImageBrowserView.zip">Download Sample</a></strong> (“ImageBrowserView.zip”, 104.7K)<BR>
<strong><a href="ImageBrowserView.dmg">Download Sample</a></strong> (“ImageBrowserView.dmg”, 155.9K)</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: TImageBrowserView.cp
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 © 2005 Apple Computer, Inc., All Rights Reserved
*/
#include "TImageBrowserView.h"
#include "ImageBrowserView.h"
//------------------------------------------------------------------------------
// constants
//------------------------------------------------------------------------------
//
const float kImageMargin = 2;
const float kButtonSize = 24;
const float kButtonMargin = 32;
const float kButtonPad = 4;
enum // view parts
{
kImagePart = 1,
kBrowseBackPart,
kBrowseForwardPart,
kDeleteImagePart
};
#define kImageURLsArchiveKey CFSTR("ImageURLsKey")
#define kImageIndexArchiveKey CFSTR("ImageIndexKey")
#pragma mark -
#pragma mark Client API
//-----------------------------------------------------------------------------------
// RegisterClass
//-----------------------------------------------------------------------------------
//
OSStatus
TImageBrowserView::RegisterClass()
{
static bool sRegistered;
if ( !sRegistered )
{
TView::RegisterSubclass( kImageBrowserViewClassID, Construct );
sRegistered = true;
}
return noErr;
}
//------------------------------------------------------------------------------
// ImageBrowserViewCreate
//------------------------------------------------------------------------------
// creates a TImageBrowserView instance
//
OSStatus TImageBrowserView::Create(
WindowRef inWindow,
const HIRect* inBounds,
CFArrayRef inImageURLs,
HIViewRef* outView )
{
OSStatus err;
HIViewRef content;
EventRef event;
// make sure the ImageBrowserView class is registered
err = ImageBrowserViewRegister();
require_noerr( err, CantRegister );
// create an initialization event
err = CreateEvent( NULL, kEventClassHIObject, kEventHIObjectInitialize, GetCurrentEventTime(), 0, &event );
require_noerr( err, CantCreateEvent );
// if bounds were provided, add them to the initialization event
if ( inBounds != NULL )
{
err = SetEventParameter( event, kEventParamBounds, typeHIRect, sizeof( HIRect ), inBounds );
require_noerr( err, CantSetParameter );
}
// if image URLs were provided, add them to the initialization event
if ( inImageURLs != NULL )
{
err = SetEventParameter( event, kEventParamImageURLArray, typeCFTypeRef, sizeof( CFArrayRef ), inImageURLs );
require_noerr( err, CantSetParameter );
}
// create a new instance of this class
err = HIObjectCreate( kImageBrowserViewClassID, event, (HIObjectRef*)outView );
require_noerr( err, CantCreate );
// if a parent window was provided, place the new view into the parent window
if ( inWindow != NULL )
{
verify_noerr( HIViewFindByID( HIViewGetRoot( inWindow ), kHIViewWindowContentID, &content ) );
verify_noerr( HIViewAddSubview( content, *outView ) );
}
CantCreate:
CantSetParameter:
CantCreateEvent:
ReleaseEvent( event );
CantRegister:
return err;
}
//------------------------------------------------------------------------------
// ImageBrowserViewAddImages
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::AddImages(
HIViewRef inView,
CFArrayRef inImageURLs )
{
OSStatus err = noErr;
TImageBrowserView* data;
data = (TImageBrowserView*)HIObjectDynamicCast( (HIObjectRef)inView, kImageBrowserViewClassID );
require_action( data != NULL, CantGetInstanceData, err = paramErr );
CFArrayAppendArray( data->fImageURLs, inImageURLs, CFRangeMake( 0, CFArrayGetCount( inImageURLs ) ) );
verify_noerr( data->Invalidate() );
CantGetInstanceData:
return err;
}
#pragma mark -
#pragma mark Methods
// -----------------------------------------------------------------------------
// TImageBrowserView constructor
// -----------------------------------------------------------------------------
//
TImageBrowserView::TImageBrowserView(
HIViewRef inView )
: TView( inView )
{
// turn on the TObject and TView accessibility, drag and drop, and keyboard focus interfaces
verify_noerr( ActivateInterface( kAccessibility ) );
verify_noerr( ActivateInterface( kDragAndDrop ) );
verify_noerr( ActivateInterface( kKeyboardFocus ) );
// register for the various Carbon Events that cannot be implicitly turned on via ActivateInterface
static const EventTypeSpec kImageBrowserViewEvents[] =
{
{ kEventClassHIObject, kEventHIObjectEncode },
{ kEventClassControl, kEventControlGetFocusPart },
{ kEventClassControl, kEventControlTrackingAreaEntered },
{ kEventClassControl, kEventControlTrackingAreaExited }
};
verify_noerr( ::AddEventTypesToHandler( GetEventHandler(), GetEventTypeCount( kImageBrowserViewEvents ),
kImageBrowserViewEvents ) );
}
// -----------------------------------------------------------------------------
// TImageBrowserView destructor
// -----------------------------------------------------------------------------
//
TImageBrowserView::~TImageBrowserView()
{
if ( fImageURLs != NULL )
::CFRelease( fImageURLs );
if ( fTrackingArea != NULL )
verify_noerr( ::HIViewDisposeTrackingArea( fTrackingArea ) );
InvalidateImageCache();
}
// -----------------------------------------------------------------------------
// GetKind
// -----------------------------------------------------------------------------
//
ControlKind
TImageBrowserView::GetKind()
{
const ControlKind kMyKind = { 'TImB', 'TImB' };
return kMyKind;
}
//-----------------------------------------------------------------------------------
// Construct
//-----------------------------------------------------------------------------------
//
OSStatus
TImageBrowserView::Construct(
HIObjectRef inObjectRef,
TObject** outObject )
{
*outObject = new TImageBrowserView( (HIViewRef)inObjectRef );
return noErr;
}
//------------------------------------------------------------------------------
// IInitialize
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::Initialize(
TCarbonEvent& inEvent )
{
OSStatus err = noErr;
HIArchiveRef decoder = NULL;
HIViewRef view = GetViewRef();
CFArrayRef imageURLs = NULL;
CFStringRef imagePath;
// look for the optional HIArchive decoder in the event
inEvent.GetParameter( kEventParamHIArchive, typeCFTypeRef, sizeof( HIArchiveRef ), &decoder );
// defult to the first image
fImageIndex = 0;
if ( decoder != NULL ) // if we're unarchiving, initialize from the decoder ...
{
// decode our persistant state
err = ::HIArchiveCopyDecodedCFType( decoder, kImageURLsArchiveKey, (CFTypeRef*)&imageURLs );
require_noerr( err, CantDecodeImageURLs );
err = ::HIArchiveDecodeNumber( decoder, kImageIndexArchiveKey, kCFNumberCFIndexType, &fImageIndex );
require_noerr( err, CantDecodeImageIndex );
}
else // ... otherwise initialize as normal
{
// extract the optional image URLs from the initialization event
inEvent.GetParameter( kEventParamImageURLArray, typeCFTypeRef, sizeof( CFArrayRef ), &imageURLs );
}
// create a mutable version of the image URLs array for later modification
if ( imageURLs != NULL )
fImageURLs = ::CFArrayCreateMutableCopy( kCFAllocatorDefault, 0, imageURLs );
else
fImageURLs = ::CFArrayCreateMutable( kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks );
require_action( fImageURLs != NULL, CantCreateImageURLArray, err = coreFoundationUnknownErr );
// extract the optional image path from the initialization event
if ( inEvent.GetParameter( kEventParamImageURL, typeCFStringRef, sizeof( CFStringRef ), &imagePath ) == noErr )
{
CFURLRef imageURL = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, imagePath, kCFURLPOSIXPathStyle, false );
require_action( imageURL != NULL, CantCreateImageURL, err = coreFoundationUnknownErr );
::CFArrayAppendValue( fImageURLs, imageURL );
::CFRelease( imageURL );
}
// we're not tracking any drags by default
fTrackingDrag = false;
// install a tracking area on the entire view to implement a mouse over effect
verify_noerr( ::HIViewNewTrackingArea( view, NULL, 0, &fTrackingArea ) );
// the mouse is not in the view by default
fMouseInView = false;
// default to a non-focused state
fCurrentFocusPart = kHIViewFocusNoPart;
// we have no cached image
fImageCache = NULL;
fImageCacheDisabled = NULL;
fImageCacheIndex = -1;
// allow the view to be archived
verify_noerr( HIObjectSetArchivingIgnored( (HIObjectRef)GetViewRef(), false ) );
// let HIToolbox know that we're an opaque view for performance
verify_noerr( ::HIViewChangeFeatures( view, kHIViewFeatureIsOpaque, 0 ) );
// always invalidate on enable and activate changes
ChangeAutoInvalidateFlags( kAutoInvalidateOnEnable | kAutoInvalidateOnActivate, 0 );
CantCreateImageURL:
CantCreateImageURLArray:
CantDecodeImageIndex:
CantDecodeImageURLs:
InitBoundsParameterMissing:
CantInitializeSuperclass:
return err;
}
//------------------------------------------------------------------------------
// Encode
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::Encode(
HIArchiveRef inEncoder )
{
OSStatus err;
// encode our persistant state
err = HIArchiveEncodeCFType( inEncoder, kImageURLsArchiveKey, fImageURLs );
require_noerr( err, CantEncodeImageURLs );
err = HIArchiveEncodeNumber( inEncoder, kImageIndexArchiveKey, kCFNumberCFIndexType, &fImageIndex );
require_noerr( err, CantEncodeImageIndex );
CantEncodeImageIndex:
CantEncodeImageURLs:
CantEncodeSuperclass:
return err;
}
//-----------------------------------------------------------------------------------
// Draw
//-----------------------------------------------------------------------------------
//
void TImageBrowserView::Draw(
RgnHandle inLimitRgn,
CGContextRef inContext )
{
OSStatus err = noErr;
HIRect bounds = Bounds();
CFIndex imageURLCount = CFArrayGetCount( fImageURLs );
// draw a black background/frame
::CGContextSetRGBFillColor( inContext, 0, 0, 0, 1 );
::CGContextFillRect( inContext, bounds );
// draw the current image if it exists
if ( imageURLCount > 0 )
{
CGImageSourceRef imageSource;
HIRect insetBounds = ::CGRectInset( bounds, kImageMargin, kImageMargin );
HIRect imageBounds = insetBounds;
float scale;
CGImageRef image;
if ( fImageCacheIndex != fImageIndex )
InvalidateImageCache();
// don't rerender the image if the cache is still valid
if ( fImageCache == NULL )
{
CFURLRef imageURL = (CFURLRef)::CFArrayGetValueAtIndex( fImageURLs, fImageIndex );
// dynamically load and cache the image from the URL with ImageIO as necessary
imageSource = ::CGImageSourceCreateWithURL( imageURL, NULL );
require_action( imageSource != NULL, CantCreateImageSource, err = coreFoundationUnknownErr );
fImageCache = ::CGImageSourceCreateImageAtIndex( imageSource, 0, NULL );
CFRelease( imageSource );
require_action( fImageCache != NULL, CantCreateImage, err = coreFoundationUnknownErr );
// cache the image data in a bitmap context to improve drawing performance
ConvertImageToBitmapImage( &fImageCache );
// create a disabled version of the image
::HICreateTransformedCGImage( fImageCache, kHITransformDisabled, &fImageCacheDisabled );
fImageCacheIndex = fImageIndex;
}
imageBounds.size.width = ::CGImageGetWidth( fImageCache );
imageBounds.size.height = ::CGImageGetHeight( fImageCache );
// determine image scale factor based on relative sizes of view and image bounds
if ( imageBounds.size.width/insetBounds.size.width > imageBounds.size.height/insetBounds.size.height )
scale = insetBounds.size.width/imageBounds.size.width;
else
scale = insetBounds.size.height/imageBounds.size.height;
// scale and center the image
imageBounds.size.width *= scale;
imageBounds.size.height *= scale;
imageBounds.origin.x += (insetBounds.size.width - imageBounds.size.width )/2;
imageBounds.origin.y += (insetBounds.size.height - imageBounds.size.height )/2;
// determine whether to use the regular or disabled image
if ( !(IsEnabled() && IsActive()) && fImageCacheDisabled != NULL )
image = fImageCacheDisabled;
else
image = fImageCache;
// draw the image
verify_noerr( ::HIViewDrawCGImage( inContext, &imageBounds, image ) );
CantCreateImageSource:
CantCreateImage:
;
}
// draw a drag hilight if necessary
if ( fTrackingDrag )
{
RGBColor dragColor;
// draw with the appropriate theme brush
verify_noerr( ::GetThemeBrushAsColor( kThemeBrushDragHilite, 32, true, &dragColor ) );
::CGContextSetRGBStrokeColor( inContext, (float)dragColor.red/65535, (float)dragColor.green/65535,
(float)dragColor.blue/65535, 1 );
::CGContextSetLineWidth( inContext, 5 );
::CGContextStrokeRect( inContext, GetPartFrame( kImagePart ) );
}
// draw the currently focused area if necessary
if ( fCurrentFocusPart != kHIViewFocusNoPart && IsEnabled() && IsActive() )
{
RGBColor hiliteColor;
HIRect focusFrame = GetPartFrame( fCurrentFocusPart );
// draw with the appropriate theme brush
verify_noerr( ::GetThemeBrushAsColor( kThemeBrushFocusHighlight, 32, true, &hiliteColor ) );
::CGContextSetRGBStrokeColor( inContext, (float)hiliteColor.red/65535, (float)hiliteColor.green/65535,
(float)hiliteColor.blue/65535, 1 );
::CGContextSetLineWidth( inContext, (fCurrentFocusPart == kImagePart) ? 5 : 3 ); // thicker for the image part
::CGContextStrokeRect( inContext, focusFrame );
}
// only draw the browse buttons when we're enable, active and the mouse button is inside the view or they're focused
if ( IsEnabled() && IsActive() && ( fMouseInView || fCurrentFocusPart == kBrowseBackPart ||
fCurrentFocusPart == kBrowseForwardPart || fCurrentFocusPart == kDeleteImagePart) )
{
HISize shadowOffset = { 0, -2 };
HIRect frame;
HIViewPartCode hilite = GetHilite();
// add a little shadow to the buttons for effect
::CGContextSetShadow( inContext, shadowOffset, 2 );
// draw the browse back button
frame = GetPartFrame( kBrowseBackPart );
::CGContextBeginPath( inContext );
::CGContextMoveToPoint( inContext, frame.origin.x + frame.size.width - kButtonPad, frame.origin.y );
::CGContextAddLineToPoint( inContext, frame.origin.x, frame.origin.y + kButtonSize/2 );
::CGContextAddLineToPoint( inContext, frame.origin.x + frame.size.width - kButtonPad, frame.origin.y + kButtonSize );
::CGContextAddLineToPoint( inContext, frame.origin.x + frame.size.width - kButtonPad, frame.origin.y );
::CGContextClosePath( inContext );
// display hilited or disabled if necessary
if ( hilite == kBrowseBackPart )
::CGContextSetRGBFillColor( inContext, .66, .66, .66, 1 );
else
::CGContextSetRGBFillColor( inContext, 1, 1, 1, IsPartAvailable( kBrowseBackPart ) ? 1 : .33 );
::CGContextFillPath( inContext );
// draw the browse forward button (appearing disabled if necessary)
frame = GetPartFrame( kBrowseForwardPart );
::CGContextBeginPath( inContext );
::CGContextMoveToPoint( inContext, frame.origin.x + kButtonPad, frame.origin.y );
::CGContextAddLineToPoint( inContext, frame.origin.x + frame.size.width, frame.origin.y + kButtonSize/2 );
::CGContextAddLineToPoint( inContext, frame.origin.x + kButtonPad, frame.origin.y + kButtonSize );
::CGContextAddLineToPoint( inContext, frame.origin.x + kButtonPad, frame.origin.y );
::CGContextClosePath( inContext );
// display hilited or disabled if necessary
if ( hilite == kBrowseForwardPart )
::CGContextSetRGBFillColor( inContext, .66, .66, .66, 1 );
else
::CGContextSetRGBFillColor( inContext, 1, 1, 1, IsPartAvailable( kBrowseForwardPart ) ? 1 : .33 );
::CGContextFillPath( inContext );
// draw the delete image button (appearing disabled if necessary)
frame = ::CGRectInset( GetPartFrame( kDeleteImagePart ), 2, 2 );
::CGContextBeginPath( inContext );
::CGContextMoveToPoint( inContext, frame.origin.x, frame.origin.y );
::CGContextAddLineToPoint( inContext, frame.origin.x + frame.size.width, frame.origin.y + frame.size.height );
::CGContextMoveToPoint( inContext, frame.origin.x, frame.origin.y + frame.size.height );
::CGContextAddLineToPoint( inContext, frame.origin.x + frame.size.width, frame.origin.y );
::CGContextClosePath( inContext );
// display hilited or disabled if necessary
if ( hilite == kDeleteImagePart )
::CGContextSetRGBStrokeColor( inContext, .66, .66, .66, 1 );
else
::CGContextSetRGBStrokeColor( inContext, 1, 1, 1, IsPartAvailable( kDeleteImagePart ) ? 1 : .33 );
::CGContextSetLineWidth( inContext, 3 );
::CGContextStrokePath( inContext );
}
}
//-----------------------------------------------------------------------------------
// HitTest
//-----------------------------------------------------------------------------------
//
ControlPartCode
TImageBrowserView::HitTest(
const HIPoint& inWhere )
{
HIViewPartCode part;
// find the part that was hit, and make sure that part is currently available
part = GetViewPart( inWhere );
if ( !IsPartAvailable( part ) )
part = kControlNoPart;
return part;
}
//-----------------------------------------------------------------------------------
// GetRegion
//-----------------------------------------------------------------------------------
//
OSStatus
TImageBrowserView::GetRegion(
ControlPartCode inPart,
RgnHandle outRgn )
{
OSStatus err = noErr;
TRect bounds;
Rect qdBounds;
if ( inPart == kControlContentMetaPart
|| inPart == kControlStructureMetaPart
/* || inPart == kControlOpaqueRegionMetaPart */ )
{
bounds = Bounds();
qdBounds = bounds;
::RectRgn( outRgn, &qdBounds );
}
return err;
}
//------------------------------------------------------------------------------
// Track
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::Track(
TCarbonEvent& inEvent,
ControlPartCode* outPartHit )
{
OSStatus err = eventNotHandledErr;
HIPoint mouse;
HIViewPartCode part;
// get the mouse location in view coordinates
verify_noerr( inEvent.GetParameter( kEventParamMouseLocation, typeHIPoint, sizeof( HIPoint ), &mouse ) );
part = GetViewPart( mouse );
if ( part == kImagePart )
err = noErr;
// return the part that was tracked
*outPartHit = part;
return err;
}
//------------------------------------------------------------------------------
// Hit
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::ControlHit(
ControlPartCode inPart,
UInt32 inModifiers )
{
OSStatus err = noErr;
// don't handle the Hit event if this part isn't currently available
if ( !IsPartAvailable( inPart ) )
return eventNotHandledErr;
switch ( inPart )
{
case kBrowseBackPart:
PreviousImage();
break;
case kBrowseForwardPart:
NextImage();
break;
case kDeleteImagePart:
DeleteCurrentImage();
break;
default:
err = eventNotHandledErr;
break;
}
return err;
}
#pragma mark -
#pragma mark Keyboard Focus
//------------------------------------------------------------------------------
// SetFocusPart
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::SetFocusPart(
ControlPartCode inDesiredFocus,
Boolean inFocusEverything,
ControlPartCode* outActualFocus )
{
OSStatus err = noErr;
HIViewPartCode oldFocusPart = fCurrentFocusPart;
HIViewPartCode newFocusPart = oldFocusPart;
switch ( inDesiredFocus )
{
case kControlFocusNextPart:
// focus the next part, if there is one that is available
if ( !inFocusEverything )
{
if ( oldFocusPart != kImagePart )
newFocusPart = kImagePart;
else
newFocusPart = kHIViewFocusNoPart;
}
else
{
while ( ++newFocusPart >= kImagePart && newFocusPart <= kDeleteImagePart )
{
if ( IsPartAvailable( newFocusPart ) )
break;
}
if ( newFocusPart > kDeleteImagePart )
newFocusPart = kHIViewFocusNoPart;
}
break;
case kControlFocusPrevPart:
if ( !inFocusEverything )
{
if ( oldFocusPart != kImagePart )
newFocusPart = kImagePart;
else
newFocusPart = kHIViewFocusNoPart;
}
else
{
// if no focus, set up so that we move to the DeleteImage part first
if ( newFocusPart == kHIViewFocusNoPart )
newFocusPart = kDeleteImagePart + 1;
// focus the previous part, if there is one that is available
while ( --newFocusPart >= kImagePart && newFocusPart <= kDeleteImagePart )
{
if ( IsPartAvailable( newFocusPart ) )
break;
}
if ( newFocusPart < kImagePart )
newFocusPart = kHIViewFocusNoPart;
}
break;
default:
if ( IsPartAvailable( inDesiredFocus ) )
newFocusPart = inDesiredFocus;
else
newFocusPart = inDesiredFocus + (inDesiredFocus - oldFocusPart); // request the next part if necessary
break;
}
fCurrentFocusPart = newFocusPart;
*outActualFocus = newFocusPart;
// invalidate ourselves if the focus changed
if ( fCurrentFocusPart != oldFocusPart )
{
// if focus was changing to or from the image part, invalidate the entire view ...
if ( fCurrentFocusPart == kImagePart || oldFocusPart == kImagePart )
{
verify_noerr( Invalidate() );
}
else // ... otherwise, only invalidate the subparts for better performance
{
HIViewRef view = GetViewRef();
HIRect focusUpdateFrame = ::CGRectUnion( GetPartFrame( (HIViewPartCode)kBrowseBackPart ),
GetPartFrame( (HIViewPartCode)kDeleteImagePart ) );
// make sure to invalidate the entire focus area
focusUpdateFrame = ::CGRectInset( focusUpdateFrame, -2, -2 );
verify_noerr( ::HIViewSetNeedsDisplayInRect( view, &focusUpdateFrame, true ) );
}
}
return err;
}
//------------------------------------------------------------------------------
// GetFocusPart
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::GetFocusPart(
HIViewPartCode* outCurrentFocusPart )
{
*outCurrentFocusPart = fCurrentFocusPart;
return noErr;
}
#pragma mark -
#pragma mark Drag and Drop
//------------------------------------------------------------------------------
// DragEnter
//------------------------------------------------------------------------------
//
bool TImageBrowserView::DragEnter(
DragRef inDrag )
{
PasteboardRef pasteboard;
CFURLRef fileURL = NULL;
Boolean wouldAccept = false;
verify_noerr( ::GetDragPasteboard( inDrag, &pasteboard ) );
fileURL = CreateFileURLFromPasteboard( pasteboard, 1 );
wouldAccept = (fileURL != NULL); // accept the drag if it contains an image file URL
if ( wouldAccept )
{
// note that we're tracking an acceptable drag
fTrackingDrag = true;
// invalidate ourseleves so we can display drag feedback
verify_noerr( Invalidate() );
}
if ( fileURL != NULL )
::CFRelease( fileURL );
return wouldAccept;
}
//------------------------------------------------------------------------------
// DragLeave
//------------------------------------------------------------------------------
//
bool TImageBrowserView::DragLeave(
DragRef inDrag )
{
// note that we are no longer tracking a drag
fTrackingDrag = false;
// invalidate ourseleves so we can turn off our drag feedback
verify_noerr( Invalidate() );
return true;
}
//------------------------------------------------------------------------------
// DragReceive
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::DragReceive(
DragRef inDrag )
{
PasteboardRef pasteboard;
verify_noerr( ::GetDragPasteboard( inDrag, &pasteboard ) );
if ( pasteboard != NULL )
{
ItemCount itemCount;
CFIndex itemIndex;
verify_noerr( ::PasteboardGetItemCount( pasteboard, &itemCount ) );
for ( itemIndex = 1; itemIndex <= itemCount; itemIndex++ )
{
CFURLRef fileURL = CreateFileURLFromPasteboard( pasteboard, itemIndex );
// insert the image file URL after the current index and increment
if ( fileURL != NULL )
{
AddImage( fileURL );
::CFRelease( fileURL );
}
}
}
return noErr;
}
//------------------------------------------------------------------------------
// TextInput
//------------------------------------------------------------------------------
//
OSStatus TImageBrowserView::TextInput(
TCarbonEvent& inEvent )
{
OSStatus err = noErr;
UniChar uniChar;
HIViewRef view = GetViewRef();
err = inEvent.GetParameter( kEventParamTextInputSendText, typeUnicodeText, sizeof( UniChar ), &uniChar );
require_noerr( err, CantGetTextInputParameter );
switch ( uniChar )
{
case kSpaceCharCode:
if ( fCurrentFocusPart == kBrowseBackPart || fCurrentFocusPart == kBrowseForwardPart ||
fCurrentFocusPart == kDeleteImagePart )
err = ::HIViewSimulateClick( view, fCurrentFocusPart, 0, NULL );
else
err = eventNotHandledErr; // otherwise let someone else deal with the keystroke
break;
case kBackspaceCharCode:
case kDeleteCharCode:
case kClearCharCode:
if ( fCurrentFocusPart == kImagePart )
err = ::HIViewSimulateClick( view, kDeleteImagePart, 0, NULL );
break;
case kLeftArrowCharCode:
case kRightArrowCharCode:
if ( fCurrentFocusPart == kImagePart )
{
if ( uniChar == kLeftArrowCharCode )
PreviousImage();
else
NextImage();
}
else
{
Boolean foundFocusablePart = false;
HIViewPartCode focusPart = fCurrentFocusPart;
// loop forward or backward through the focusable subparts
while ( !foundFocusablePart )
{
focusPart += (uniChar == kLeftArrowCharCode) ? -1 : 1;
// cycle through the subparts
if ( focusPart == kBrowseBackPart - 1 )
focusPart = kDeleteImagePart;
if ( focusPart == kDeleteImagePart + 1 )
focusPart = kBrowseBackPart;
// if the part is enabled we've got it
if ( IsPartAvailable( focusPart ) )
{