-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing1.html
executable file
·622 lines (524 loc) · 22.4 KB
/
listing1.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
<!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>AddNibToNav - /AddNibToNav.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/idxFileManagement-date.html">File Management</a> > <A HREF="javascript:location.replace('index.html');">AddNibToNav</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">AddNibToNav</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>/AddNibToNav.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">/AddNibToNav.cp</option>
<option value="listing2.html">/AddNibToNav.r</option>
<option value="listing3.html">/AddNibToNav_Prefix.h</option></select>
</p>
</form>
<p><strong><a href="AddNibToNav.zip">Download Sample</a></strong> (“AddNibToNav.zip”, 52.6K)<BR>
<strong><a href="AddNibToNav.dmg">Download Sample</a></strong> (“AddNibToNav.dmg”, 114.5K)</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: AddNibToNav.cp Contains: How to add a .nib file to a Navigation Services window. 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 © 2003 Apple Computer, Inc., All Rights Reserved*/#include <Carbon/Carbon.h>// Common stuff to all samplesvoid InitToolbox(void);void InitMenuBar(void);pascal OSErr HandlePref(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon);pascal OSErr HandleOapp(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon);pascal OSErr HandleOdoc(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon);pascal OSErr HandlePdoc(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon);void InstallAppleEventHandlers(void);pascal OSStatus CommandProcess(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData);void InstallApplicationCarbonEventHandlers(void);void DoCloseWindow(WindowRef theWind);void RecursiveCloseWindow(WindowRef theWind);void CloseAllWindows();void InitApplication(void);void TermApplication(void);// End of common stuffvoid DoAboutBox(void);void DoPreferences(void);void DoNewWindow(void);int main(void) { InitToolbox(); InitApplication(); RunApplicationEventLoop(); TermApplication(); return 0; }//------------------------------- Initializations ------------------------------void InitToolbox(void) { InitCursor(); }void InitApplication(void) { long response; OSErr err; // Can we run this particular demo application? err = Gestalt(gestaltSystemVersion, &response); Boolean ok = ((err == noErr) && (response >= 0x00001020)); if (!ok) { StandardAlert(kAlertStopAlert, "\pMac OS X 10.2 (minimum) is required for this application", "\p", NULL, NULL); ExitToShell(); } InstallAppleEventHandlers(); InstallApplicationCarbonEventHandlers(); InitMenuBar(); DoNewWindow(); }void InitMenuBar(void) { // Getting our menu bar Handle aHand = GetNewMBar(128); SetMenuBar(aHand); // Enabling Preferences menu item EnableMenuCommand(NULL, kHICommandPreferences); // Adding some menu item commands MenuHandle menu = GetMenuHandle(128); SetMenuItemCommandID(menu, 1, kHICommandNew); SetMenuItemCommandID(menu, 2, kHICommandClose); SetMenuItemCommandID(GetMenuHandle(1), 1, kHICommandAbout); // Adding a standard Window menu CreateStandardWindowMenu(0, &menu); InsertMenu(menu, 0); DrawMenuBar(); DisposeHandle(aHand); }//------------------------------- Apple Events ---------------------------------void InstallAppleEventHandlers(void) { OSErr err; err = AEInstallEventHandler(kCoreEventClass, kAEShowPreferences, NewAEEventHandlerUPP(HandlePref), 0, false); if (err) DebugStr("\pAEInstallEventHandler failed for kAEOpenApplication"); err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, NewAEEventHandlerUPP(HandleOapp), 0, false); if (err) DebugStr("\pAEInstallEventHandler failed for kAEOpenApplication"); err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerUPP(HandleOdoc), 0, false); if (err) DebugStr("\pAEInstallEventHandler failed for kAEOpenDocuments"); err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, NewAEEventHandlerUPP(HandlePdoc), 0, false); if (err) DebugStr("\pAEInstallEventHandler failed for kAEPrintDocuments"); // Note: Since RunApplicationEventLoop installs a Quit AE Handler, we no longer have to do it here. }pascal OSErr HandlePref(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon) {#pragma unused (theAppleEvent, reply, handlerRefcon) return errAEEventNotHandled; }pascal OSErr HandleOapp(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon) {#pragma unused (theAppleEvent, reply, handlerRefcon) return noErr; }pascal OSErr HandleOdoc(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon) {#pragma unused (theAppleEvent, reply, handlerRefcon) return errAEEventNotHandled; }pascal OSErr HandlePdoc(const AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon) {#pragma unused (theAppleEvent, reply, handlerRefcon) return errAEEventNotHandled; }//------------------------------- Carbon Events --------------------------------void InstallApplicationCarbonEventHandlers(void) { EventTypeSpec eventType = {kEventClassCommand, kEventCommandProcess}; InstallEventHandler(GetApplicationEventTarget(), NewEventHandlerUPP(CommandProcess), 1, &eventType, NULL, NULL); }pascal OSStatus CommandProcess(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData) {#pragma unused (nextHandler, userData) HICommand aCommand; OSStatus status = noErr; GetEventParameter(theEvent, kEventParamDirectObject, typeHICommand, NULL, sizeof(HICommand), NULL, &aCommand); switch (aCommand.commandID) { case kHICommandAbout: DoAboutBox(); break; case kHICommandPreferences: DoPreferences(); break; case kHICommandNew: DoNewWindow(); break; case kHICommandClose: DoCloseWindow(FrontNonFloatingWindow()); break; default: status = eventNotHandledErr; break; } return status; }//------------------------------- Terminating ----------------------------------void TermApplication(void) { CloseAllWindows(); }void CloseAllWindows() { WindowRef theWind = FrontWindow(); if (theWind) { RecursiveCloseWindow(theWind); } }void RecursiveCloseWindow(WindowRef theWind) { WindowRef nextWind = GetNextWindow(theWind); if (nextWind) RecursiveCloseWindow(nextWind); DoCloseWindow(theWind); }void DoCloseWindow(WindowRef theWind) { EventRef theEvent; CreateEvent(NULL, kEventClassWindow, kEventWindowClose, 0, 0, &theEvent); SetEventParameter(theEvent, kEventParamDirectObject, typeWindowRef, sizeof(WindowRef), &theWind); SendEventToEventTarget(theEvent, GetWindowEventTarget(theWind)); }//------------------------------- End of common stuff --------------------------void DoAboutBox(void) { StandardAlert(kAlertNoteAlert, "\pAddNibToNav 1.0 by DTS, Apple © 2003", "\p", NULL, NULL); }void DoPreferences(void) { StandardAlert(kAlertNoteAlert, "\pNo Preferences yet!", "\p", NULL, NULL); }UInt32 GetHIToolboxVersion() { CFBundleRef bundle; CFStringRef versionStr = NULL; static UInt32 version = 0; if (version != 0) return version; bundle = CFBundleGetBundleWithIdentifier( CFSTR("com.apple.HIToolbox") ); if ( bundle != NULL ) versionStr = (CFStringRef) CFBundleGetValueForInfoDictionaryKey( bundle, CFSTR("CFBundleShortVersionString") ); if ( versionStr != NULL && CFGetTypeID( versionStr ) == CFStringGetTypeID() ) { int major = 0, minor = 0, bugfix = 0; char sz[20]; CFStringGetCString( versionStr, sz, sizeof( sz ), kCFStringEncodingUTF8 ); sscanf( sz, "%d.%d.%d", &major, &minor, &bugfix ); version = ( major << 8 ) + ( minor << 4 ) + bugfix; } return version; }typedef struct { IBNibRef nibRef; WindowRef windowFromNib; } CustomData;void HandleCarbonCustomizeEvent(NavCBRecPtr callBackParms, CustomData * data) { OSStatus status = noErr; SInt16 neededHeight, neededWidth; static SInt16 gCarbonEventLastTryHeight; static SInt16 gCarbonEventLastTryWidth; // If it's the first time we get in this function, we need to create our nib reference and // instantiate the window from the nib. This function might/will be called many times. if (data->nibRef == NULL) { // Create a Nib reference passing the name of the nib file (without the .nib extension). // CreateNibReference only searches into the application bundle. status = CreateNibReference(CFSTR("AddNibToNav"), &data->nibRef); if (status != noErr) { printf("CreateNibReference returned %ld\n", status); return;} // Then instantiate the window "Custom Area". // This name is set in InterfaceBuilder when the nib is created. status = CreateWindowFromNib(data->nibRef, CFSTR("Custom Area"), &data->windowFromNib); if (status != noErr) { printf("CreateWindowFromNib returned %ld\n", status); return;} } Rect windowBounds; GetWindowPortBounds(data->windowFromNib, &windowBounds); neededHeight = callBackParms->customRect.top + (windowBounds.bottom - windowBounds.top); neededWidth = callBackParms->customRect.left + (windowBounds.right - windowBounds.left); // check to see if this is the first round of negotiations: if ((callBackParms->customRect.right == 0) && (callBackParms->customRect.bottom == 0)) { // it is, so tell NavServices what dimensions we want: callBackParms->customRect.right = neededWidth; callBackParms->customRect.bottom = neededHeight; } else { // we are in the middle of negotiating: if (gCarbonEventLastTryWidth != callBackParms->customRect.right) if (callBackParms->customRect.right < neededWidth) // is NavServices width too small for us? callBackParms->customRect.right = neededWidth; if (gCarbonEventLastTryHeight != callBackParms->customRect.bottom) if (callBackParms->customRect.bottom < neededHeight) // is NavServices height too small for us? callBackParms->customRect.bottom = neededHeight; } // remember our last size so the next time we can re-negotiate: gCarbonEventLastTryWidth = callBackParms->customRect.right; gCarbonEventLastTryHeight = callBackParms->customRect.bottom; }void HandleCarbonStartEvent(NavCBRecPtr callBackParms, CustomData * data) { OSStatus status = noErr; ControlID userPaneID = { 'usrp', 100 }; HIViewRef userPane; status = HIViewFindByID(HIViewGetRoot(data->windowFromNib), userPaneID, &userPane); if (status != noErr) { printf("HIViewFindByID returned %ld\n", status); return;} // We need to move the User Pane (along with its sub-controls) from the nib window // to the Navigation Services window in Jaguar. // In Panther, the NavCustomControl/kNavCtlAddControl will move the User Pane from the nib window // to the Navigation Services window so we don't need to explicitly move it before. if (GetHIToolboxVersion() < 0x130) { ControlRef rootControl; status = GetRootControl(callBackParms->window, &rootControl); if (status != noErr) { printf("GetRootControl returned %ld\n", status); return;} status = EmbedControl(userPane, rootControl); if (status != noErr) { printf("EmbedControl returned %ld\n", status); return;} } status = NavCustomControl(callBackParms->context, kNavCtlAddControl, userPane); if (status != noErr) { printf("NavCustomControl returned %ld\n", status); return;} // We're done with the instantiations from the nib so let's dispose them. DisposeWindow(data->windowFromNib); DisposeNibReference(data->nibRef); data->nibRef = NULL; }void HandleCustomMouseDown(NavCBRecPtr callBackParms) { EventRecord *evt = callBackParms->eventData.eventDataParms.event; Point where = evt->where; GlobalToLocal(&where); ControlRef whichControl = FindControlUnderMouse(where, callBackParms->window, NULL); if (whichControl != NULL) { ControlKind theKind; GetControlKind(whichControl, &theKind); // Moving the focus if we clicked in an editable text control // In this sample, we only have a Clock and an Unicode Edit controls if ((theKind.kind == kControlKindEditUnicodeText) || (theKind.kind == kControlKindClock)) { ControlRef currentlyFocusedControl; GetKeyboardFocus(callBackParms->window, &currentlyFocusedControl); if (currentlyFocusedControl != whichControl) SetKeyboardFocus(callBackParms->window, whichControl, kControlFocusNextPart); } HandleControlClick(whichControl, where, evt->modifiers, NULL); } }void HandleNormalEvents(NavCBRecPtr callBackParms) { switch (callBackParms->eventData.eventDataParms.event->what) { case mouseDown: { HandleCustomMouseDown(callBackParms); break; } } }pascal void EventProc(NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms, void *callBackUD) { OSStatus status = noErr; CustomData * data = (CustomData *)callBackUD; switch (callBackSelector) { case kNavCBCustomize: { HandleCarbonCustomizeEvent(callBackParms, data); break; } case kNavCBStart: { HandleCarbonStartEvent(callBackParms, data); break; } case kNavCBEvent: { HandleNormalEvents(callBackParms); break; } case kNavCBAdjustRect: { ControlID userPaneID = { 'usrp', 100 }; HIViewRef userPane; status = HIViewFindByID(HIViewGetRoot(callBackParms->window), userPaneID, &userPane); if (status != noErr) break; // we didn't reach kNavCBStart yet so our user pane is not there. MoveControl(userPane, callBackParms->customRect.left, callBackParms->customRect.top); break; } case kNavCBAccept: { // Getting the current settings of some controls... ControlID checkBoxID = { 'chck', 100 }; HIViewRef checkBox; status = HIViewFindByID(HIViewGetRoot(callBackParms->window), checkBoxID, &checkBox); Boolean isChecked = (GetControlValue(checkBox) == 1); // if (isChecked) printf("is Checked\n"); else printf("is not Checked\n"); ControlID unicodeEditID = { 'ucde', 100 }; HIViewRef unicodeEdit; status = HIViewFindByID(HIViewGetRoot(callBackParms->window), unicodeEditID, &unicodeEdit); CFStringRef theCFString; GetControlData(unicodeEdit, kControlEntireControl, kControlEditTextCFStringTag, sizeof(theCFString), &theCFString, NULL); /*{ char buffer[300]; CFStringGetCString(theCFString, buffer, 300, kCFStringEncodingMacRoman); printf("string is '%s'\n", buffer); }*/ CFRelease(theCFString); break; } } }void DoNewWindow(void) { OSStatus status = noErr; NavDialogCreationOptions options; status = NavGetDefaultDialogCreationOptions(&options); if (status != noErr) { printf("NavGetDefaultDialogCreationOptions returned %ld\n", status); return;} CustomData * data = (CustomData *)malloc(sizeof(CustomData)); data->nibRef = NULL; data->windowFromNib = NULL; NavDialogRef navDialog; status = NavCreatePutFileDialog(&options, kUnknownType, kUnknownType, EventProc, data, &navDialog); if (status != noErr) { printf("NavCreatePutFileDialog returned %ld\n", status); return;} status = NavDialogRun(navDialog); if (status != noErr) { printf("NavDialogRun returned %ld\n", status); return;} NavDialogDispose(navDialog); free(data); }</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/AddNibToNav/listing1.html%3Fid%3DDTS10003158-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/AddNibToNav/listing1.html%3Fid%3DDTS10003158-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/AddNibToNav/listing1.html%3Fid%3DDTS10003158-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>