Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
SavePictFile.c
/* |
File: SavePictFile.c |
Contains: Save a PICT file: Creates a QuickDraw PICT and saves it as a PICT file, |
including the required header of 512 bytes of nothing important. |
Written by: |
Copyright: Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved. |
You may incorporate this Apple sample source code into your program(s) without |
restriction. This Apple sample source code has been provided "AS IS" and the |
responsibility for its operation is yours. You are not permitted to redistribute |
this Apple sample source code as "Apple sample source code" after having made |
changes. If you're going to re-distribute the source, we require that you make |
it clear in the source that the code was descended from Apple sample source |
code, but that you've made changes. |
Change History (most recent first): |
08/2000 JM Carbonized, non-Carbon code is commented out |
for demonstration purposes. |
7/14/1999 KG Updated for Metrowerks Codewarror Pro 2.1 |
*/ |
#include "CarbonPrefix.h" |
#include <Files.h> |
//#include <StandardFile.h> |
#include <Fonts.h> |
#include <Quickdraw.h> |
#include <Navigation.h> |
#include <Script.h> |
PicHandle InitPicture(void); |
void PutPictToFile(PicHandle thePicture); |
/*------ main ----------------------------------------------------------------------------*/ |
PicHandle ourPict; |
int main() |
{ |
// Most initialized functions are not needed anymore, InitCursor being |
// the exception |
//InitGraf(&qd.thePort); |
//InitFonts(); |
//InitWindows(); |
//InitMenus(); |
//TEInit(); |
//InitDialogs(nil); |
InitCursor(); |
// Not available in carbon |
//OpenPort(&myPort); |
ourPict = InitPicture(); // get our picture |
HLock((Handle)ourPict); |
PutPictToFile(ourPict); // put it to a file |
HUnlock((Handle)ourPict); |
KillPicture(ourPict); // and then kill it |
return 0; |
} /* main */ |
/*------ PutPictToFile ----------------------------------------------------------------------------*/ |
void PutPictToFile(PicHandle thePicture) |
{ |
/* |
SFReply tr; |
short fRefNum, count; |
long inOutCount; |
Point where; |
unsigned char header[512]; |
OSErr myError; |
for (count = 0; count < 512; count++) |
header[count] = 0x00; |
where.h=100; where.v=50; // where the Standard File dialog window goes |
SFPutFile( where, "\pSave PICT as:", "\pMy PICT File", NULL, &tr ); |
if ( tr.good ) { |
myError = Create(tr.fName, tr.vRefNum, '????', 'PICT'); |
if (myError == dupFNErr) { |
myError = FSDelete(tr.fName,tr.vRefNum); |
myError = Create(tr.fName, tr.vRefNum, '????', 'PICT'); |
} // this is quick 'n' dirty or there'd be more robust handling here |
myError = FSOpen( tr.fName, tr.vRefNum, &fRefNum ); |
if ( myError == noErr ) { |
inOutCount = 512; |
myError = FSWrite(fRefNum, &inOutCount, header); // write the header |
HLock((Handle)thePicture); |
if (myError == noErr) { // don't write if error the first time |
inOutCount = GetHandleSize((Handle)thePicture); |
myError = FSWrite(fRefNum,&inOutCount,*thePicture); |
} |
FSClose( fRefNum ); // close it |
HUnlock((Handle)thePicture); |
} |
} |
*/ |
// The file services API in Carbon is called Navigation Services. |
// All (or nearly all) of the Standard File routines are not supported in Carbon. |
// Note: not checking for every error, keeping the example simple |
// Code adapted from: http://developer.apple.com/techpubs/carbon/Files/NavigationServices/Navigation_Services/Conceptual/NavServices-15.html#elementId-1012312 |
OSErr anErr = noErr; |
NavReplyRecord reply; |
NavDialogOptions dialogOptions; |
OSType fileTypeToSave = 'PICT'; |
OSType creatorType = 'ogle'; |
AEKeyword theKeyword; |
DescType actualType; |
Size actualSize; |
FSSpec documentFSSpec; |
long inOutCount; |
short refNum, count; |
unsigned char header[512]; |
for (count = 0; count < 512; count++) |
header[count] = 0x00; |
anErr = NavGetDefaultDialogOptions(&dialogOptions); |
dialogOptions.dialogOptionFlags |= kNavSelectDefaultLocation; |
anErr = NavPutFile( nil, |
&reply, |
&dialogOptions, |
nil, |
fileTypeToSave, |
creatorType, |
nil ); |
if (anErr == noErr && reply.validRecord) { |
anErr = AEGetNthPtr(&(reply.selection), 1, typeFSS, |
&theKeyword, &actualType, |
&documentFSSpec, sizeof(documentFSSpec), |
&actualSize ); |
if (anErr == noErr) { |
anErr = FSpCreate(&documentFSSpec, creatorType, fileTypeToSave, smSystemScript); |
if (anErr == dupFNErr) { |
anErr = FSpDelete(&documentFSSpec); |
anErr = FSpCreate(&documentFSSpec, creatorType, fileTypeToSave, smSystemScript); |
} // this is quick 'n' dirty or there'd be more robust handling here |
// write the file |
FSpOpenDF(&documentFSSpec, fsRdWrPerm, &refNum ); |
inOutCount = 512; |
anErr = FSWrite(refNum, &inOutCount, header); // write the header |
if (anErr == noErr) { |
inOutCount = GetHandleSize((Handle)thePicture); |
anErr = FSWrite(refNum,&inOutCount,*thePicture); |
} |
FSClose( refNum ); |
} |
reply.translationNeeded = false; |
anErr = NavCompleteSave(&reply, kNavTranslateInPlace); |
NavDisposeReply(&reply); |
} |
} |
/*------ InitPicture ----------------------------------------------------------------------*/ |
PicHandle InitPicture (void) |
{ |
Rect myRect; |
PicHandle thePicHandle; |
//CGrafPort myPort; |
CGrafPtr myPort; |
PixPatHandle thePixPat; |
short theFont, textSize = 14; |
// Not available in carbon |
//OpenCPort(&myPort); |
myPort = CreateNewPort(); |
SetRect(&myRect,0,0,200,200); |
thePicHandle = OpenPicture(&myRect); |
ClipRect(&myRect); |
thePixPat = GetPixPat(128); |
FillCOval(&myRect,thePixPat); |
MoveTo(22,22); |
LineTo(55,55); |
LineTo(58,22); |
LineTo(22,58); |
// Not recommended in Carbon |
//GetFNum ("\pTimes", &theFont); |
theFont = FMGetFontFamilyFromName("\pTimes"); |
TextFont (theFont); |
TextSize (textSize); |
DrawString("\pA wonderful test"); |
ClosePicture(); |
//CloseCPort(&myPort); |
DisposePort(myPort); |
return(thePicHandle); |
} /* InitPicture */ |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-10-10