-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing2.html
executable file
·505 lines (425 loc) · 19 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
<!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>HISimpleList - /HISimpleList.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');">HISimpleList</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">HISimpleList</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>/HISimpleList.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">/HINotSoSimpleList.cp</option>
<option value="listing2.html">/HISimpleList.cp</option>
<option value="listing3.html">/HISimpleList.h</option>
<option value="listing4.html">/TestHIView.cp</option>
<option value="listing5.html">/TestHIView.r</option></select>
</p>
</form>
<p><strong><a href="HISimpleList.zip">Download Sample</a></strong> (“HISimpleList.zip”, 75.7K)<BR>
<strong><a href="HISimpleList.dmg">Download Sample</a></strong> (“HISimpleList.dmg”, 135.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: HISimpleList.cp Contains: Demonstrates creating a simple list using the HIView APIs. Version: 1.0.2 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 © 2002 Apple Computer, Inc., All Rights Reserved*/#include <Carbon/Carbon.h>#include "HISimpleList.h"// structure in which we hold our custom push button's datatypedef struct { HIViewRef view; // the HIViewRef for our list HIPoint originPoint; HISize lineSize; UInt32 nbLines; }SimpleListData;pascal OSStatus ViewHandler(EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon);CFStringRef GetSimpleListClass();/* * HICreateSimpleList() * * Summary: * Creates a simple list control. * * Parameters: * * outSimpleList: * On exit, contains the new control. * * Availability: * Mac OS X: in version 10.2 and later since it needs the HIView APIs * CarbonLib: not available * Non-Carbon CFM: not available */extern OSStatus HICreateSimpleList(HIViewRef * outSimpleList) { HIViewRef scrollView; *outSimpleList = NULL; OSStatus status = HIScrollViewCreate(kHIScrollViewOptionsVertScroll | kHIScrollViewOptionsAllowGrow, &scrollView); if (status != noErr) goto exitCreate; HIObjectRef hiObject; status = HIObjectCreate(GetSimpleListClass(), 0, &hiObject); if (status != noErr) goto exitCreate; status = HIViewAddSubview(scrollView, (HIViewRef)hiObject); if (status != noErr) goto exitCreate; HIViewSetVisible(scrollView, true); HIViewSetVisible((HIViewRef)hiObject, true);// HIScrollViewSetScrollBarAutoHide(scrollView, true); *outSimpleList = scrollView;exitCreate: return status; }// background colors for alternate lines#define kWhitish 57536RGBColor gBackGrounds[] = { {65535, kWhitish, kWhitish}, {kWhitish, 65535, kWhitish}, {kWhitish, kWhitish, 65535}, {65535, 65535, kWhitish}, {65535, kWhitish, 65535}, {kWhitish, 65535, 65535}, {3060, 3060, 30600} };OSStatus SimpleListViewDrawing(CGContextRef context, const HIRect * bounds, const SimpleListData * myData) { UInt32 start, end, i; start = (int) (myData->originPoint.y / myData->lineSize.height); end = (int) ((myData->originPoint.y + bounds->size.height) / myData->lineSize.height + 1); CGContextSelectFont(context, "Helvetica", myData->lineSize.height * 0.75, kCGEncodingMacRoman); char p[20]; HIRect lineBounds = *bounds; lineBounds.size.height = myData->lineSize.height; float y = start * myData->lineSize.height - myData->originPoint.y; lineBounds.origin.y = y; for (i = start; (i<= end) && (i < myData->nbLines); i++, y += myData->lineSize.height) { CGContextSetRGBFillColor(context, RGBToFloatRed(i % 6), RGBToFloatGreen(i % 6), RGBToFloatBlue(i % 6), 1.0); CGContextFillRect(context, lineBounds); CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0); CGContextSetTextPosition(context, 10.0, y + 0.75 * myData->lineSize.height); sprintf(p, "Line %ld", i+1); CGContextShowText(context, p, strlen(p)); lineBounds.origin.y += myData->lineSize.height; } return noErr; }/*----------------------------------------------------------------------------------------------------------*/// * GetSimpleListClass// Registers and returns an HIObject class for a simple list control./*----------------------------------------------------------------------------------------------------------*/CFStringRef GetSimpleListClass() { // following code is pretty much boiler plate. static HIObjectClassRef theClass; if (theClass == NULL) { static EventTypeSpec kFactoryEvents[] = { // the next 3 messages are boiler plate { kEventClassHIObject, kEventHIObjectConstruct }, { kEventClassHIObject, kEventHIObjectInitialize }, { kEventClassHIObject, kEventHIObjectDestruct }, // the next 2 messages are Scroll specific { kEventClassScrollable, kEventScrollableGetInfo }, { kEventClassScrollable, kEventScrollableScrollTo }, // the next 2 messages are Control specific { kEventClassControl, kEventControlSetData }, { kEventClassControl, kEventControlDraw } }; HIObjectRegisterSubclass(kSimpleListClassID, kHIViewClassID, 0, ViewHandler, GetEventTypeCount(kFactoryEvents), kFactoryEvents, 0, &theClass); } return kSimpleListClassID; }/*----------------------------------------------------------------------------------------------------------*/// * ViewHandler// Event handler that implements our simple list control./*----------------------------------------------------------------------------------------------------------*/pascal OSStatus ViewHandler(EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon) { OSStatus result = eventNotHandledErr; SimpleListData* myData = (SimpleListData*)inRefcon; switch (GetEventClass(inEvent)) { case kEventClassHIObject: switch (GetEventKind(inEvent)) { case kEventHIObjectConstruct: { // allocate some instance data SimpleListData* myData = (SimpleListData*) calloc(1, sizeof(SimpleListData)); // get our superclass instance HIViewRef epView; GetEventParameter(inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL, sizeof(epView), NULL, &epView); // remember our superclass in our instance data and initialize other fields myData->view = epView; myData->originPoint.x = 0; myData->originPoint.y = 0; myData->lineSize.width = 0; myData->lineSize.height = 24; myData->nbLines = 100; // set the control ID so that we can find it later with HIViewFindByID result = SetControlID(myData->view, &kSimpleListViewID); if (result != noErr) DebugStr("\pSetControlID failed!"); // store our instance data into the event result = SetEventParameter(inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof(myData), &myData); break; } case kEventHIObjectDestruct: { free(myData); result = noErr; break; } case kEventHIObjectInitialize: { // always begin kEventHIObjectInitialize by calling through to the previous handler result = CallNextEventHandler(inCaller, inEvent); // if that succeeded, do our own initialization if (result == noErr) { // nothing to do here } break; } default: break; } break; case kEventClassScrollable: switch (GetEventKind(inEvent)) { case kEventScrollableGetInfo: { // we're being asked to return information about the scrolled view that we set as Event Parameters HISize imageSize = myData->lineSize; imageSize.height *= myData->nbLines; SetEventParameter(inEvent, kEventParamImageSize, typeHISize, sizeof(imageSize), &imageSize); HIRect bounds; HIViewGetBounds(myData->view, &bounds); SetEventParameter(inEvent, kEventParamViewSize, typeHISize, sizeof(bounds.size), &bounds.size); SetEventParameter(inEvent, kEventParamLineSize, typeHISize, sizeof(myData->lineSize), &myData->lineSize); SetEventParameter(inEvent, kEventParamOrigin, typeHIPoint, sizeof(myData->originPoint), &myData->originPoint); result = noErr; break; } case kEventScrollableScrollTo: { // we're being asked to scroll, we just do a sanity check and ask for a redraw HIPoint where; GetEventParameter(inEvent, kEventParamOrigin, typeHIPoint, NULL, sizeof(where), NULL, &where); myData->originPoint.y = (where.y < 0.0)?0.0:where.y; HIViewSetNeedsDisplay(myData->view, true); break; } default: break; } break; case kEventClassControl: switch (GetEventKind(inEvent)) { // Draw the view. case kEventControlDraw: { CGContextRef context; result = GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(context), NULL, &context); if (result != noErr) {DebugStr("\pGetEventParameter failed for kEventControlDraw"); break;} HIRect bounds; HIViewGetBounds(myData->view, &bounds); CGContextSaveGState(context); CGAffineTransform transform = CGAffineTransformIdentity; // adjust the transform so the text doesn't draw upside down transform = CGAffineTransformScale(transform, 1, -1); CGContextSetTextMatrix(context, transform); // now that the proper parameters and configurations have been dealt with, let's draw SimpleListViewDrawing(context, &bounds, myData); CGContextRestoreGState(context); result = noErr; break; } case kEventControlSetData: { // we override this event, being only interested in the kSetNumberOfLinesCommand tag // if that's the case, we then get the value, and send a kEventClassScrollable + kEventScrollableInfoChanged // to the scrollview so that it will then ask us for a kEventClassScrollable + kEventScrollableGetInfo and change // its internal state, we also ask for a later redraw. UInt32 theCommand, theData, theNewValue; Boolean changed = false; GetEventParameter(inEvent, kEventParamControlDataTag, typeEnumeration, NULL, sizeof(theCommand), NULL, &theCommand); GetEventParameter(inEvent, kEventParamControlDataBuffer, typePtr, NULL, sizeof(theData), NULL, &theData); switch (theCommand) { case kSetNumberOfLinesCommand: { theNewValue = *((UInt32 *)theData); if (theNewValue != myData->nbLines) {changed = true; myData->nbLines = theNewValue;} } break; default: break; } if (changed) { HISize imageSize = myData->lineSize; imageSize.height *= myData->nbLines; HIRect bounds; HIViewGetBounds(myData->view, &bounds); if (myData->originPoint.y + bounds.size.height > imageSize.height) myData->originPoint.y = imageSize.height - bounds.size.height; if (myData->originPoint.y < 0) myData->originPoint.y = 0; EventRef theEvent; CreateEvent(NULL, kEventClassScrollable, kEventScrollableInfoChanged, GetCurrentEventTime(), kEventAttributeUserEvent, &theEvent); SendEventToEventTarget(theEvent, GetControlEventTarget(myData->view)); ReleaseEvent(theEvent); HIViewSetNeedsDisplay(myData->view, true); HIViewSetNeedsDisplay(HIViewGetSuperview(myData->view), true); } break; } default: break; } break; default: break; } return result; }</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div style="width: 100%; height: 1px; background-color: #919699; margin-top: 5px; margin-bottom: 15px"></div></td>
</tr>
<tr>
<td align="center"><br/>
<table border="0" cellpadding="0" cellspacing="0" class="graybox">
<tr>
<th>Did this document help you?</th>
</tr>
<tr>
<td>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=1&url=/samplecode/HISimpleList/listing2.html%3Fid%3DDTS10000639-1.0&media=dvd" target=_new>Yes</a>: Tell us what works for you.</div>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=2&url=/samplecode/HISimpleList/listing2.html%3Fid%3DDTS10000639-1.0&media=dvd" target=_new>It’s good, but:</a> Report typos, inaccuracies, and so forth.</div>
<div><a href="http://developer.apple.com/feedback/?v=3&url=/samplecode/HISimpleList/listing2.html%3Fid%3DDTS10000639-1.0&media=dvd" target=_new>It wasn’t helpful</a>: Tell us what would have helped.</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- START BOTTOM APPLE NAVIGATION -->
<!--#include virtual="/includes/footer"-->
<!-- END BOTTOM APPLE NAVIGATION -->
<!-- START CENTER CLOSE -->
</center>
<!-- END CENTER CLOSE -->
</body>
</html>