Commit dc0d6e0d authored by Diederik van Arkel's avatar Diederik van Arkel

Initial commit MacOS Carbon version

parent 701a536e
#define Clean(a)
typedef struct clean_string *CleanString;
/* a string in Clean is:
struct clean_string {
int clean_string_length;
char clean_string_characters[clean_string_length];
};
The string does not end with a '\0' !
*/
/* CleanStringLength(clean_string) returns length of the clean_string in characters */
#define CleanStringLength(clean_string) (*(unsigned int *)(clean_string))
/* CleanStringCharacters(clean_string) returns a pointer to the characters of the clean_string */
#define CleanStringCharacters(clean_string) ((char*)(1+(unsigned int *)(clean_string)))
/* CleanStringSizeInts(string_length) return size of CleanString in integers */
#define CleanStringSizeInts(string_length) (1+(((unsigned int)(string_length)+3)>>2))
/* CleanStringVariable(clean_string,string_length) defines variable clean_string with length string_length,
before using the clean_string variable, cast to CleanString, except for the macros above */
#define CleanStringVariable(clean_string,string_length) unsigned int clean_string[CleanStringSizeInts(string_length)]
/* CleanStringSizeBytes(string_length) return size of CleanString in bytes */
#define CleanStringSizeBytes(string_length) (4+(((unsigned int)(string_length)+3) & -4))
#include <PMApplication.h>
#include <PMDefinitions.h>
#include <PMCore.h>
#include <Carbon.h>
#include "Clean.h"
typedef struct pass_object *PassObject;
/* with a PassObject one can pass strings back to Clean. It's first DWORD
contains the size of the object -8, the second DWORD contains the length
of the string, and the rest contains the string itself. In
PassObject pObj;
the following expression points to a Clean string:
((char*)passObj)[4]
*/
#define kDontScaleOutput nil
static PMPrintSession gPrintSession = NULL;
static PMPrintSettings gPrintSettings = NULL;
static int prGrafPortOK = 0;
static int prGrafPort;
static int prXRes;
static int prYRes;
// prGrafPortOK indicates, that printing is in progress. prOpenDoc sets this
// flag to True, and only prEndDoc sets it again to False, so these two functions
// MUST be balanced in every condition (exception: abort)
// prGrafPortOK <=> prGrafPort, prXRes & prYRes are valid
PassObject passObj1 = NULL;
int CStringLength(char* string)
// returns length of C string INCLUDING the '\0'
{
int i=0;
while(string[i]!='\0')
i++;
i++;
return i;
}
CleanString PassMacHandle(PassObject *pPassObj,char** data,unsigned int size, int *pErr)
// err code: 0==ok; 1==out of memory
{
unsigned int passObjSize,i;
PassObject passObj;
if (*pPassObj==NULL)
{
*pPassObj = (PassObject) NewPtr(size+8);
if (*pPassObj==NULL)
{ *pErr = 1;
((unsigned int*)*pPassObj)[1] = 0;
return (CleanString) (((unsigned int*)*pPassObj)+1);
};
((unsigned int*)*pPassObj)[0] = size;
};
passObjSize = ((unsigned int*)*pPassObj)[0];
if (passObjSize<size)
{
DisposePtr((char*)pPassObj);
*pPassObj = (PassObject) NewPtr(size+8);
if (*pPassObj==NULL)
{ *pErr = 1;
((unsigned int*)*pPassObj)[1] = 0;
return (CleanString) (((unsigned int*)*pPassObj)+1);
};
((unsigned int*)*pPassObj)[0] = size;
};
// now the pass object is big enough, so fill it with data
passObj = *pPassObj;
((unsigned int*)passObj)[1] = size;
for(i=0; i<size; i++)
((char*)passObj)[i+8] = (*data)[i];
*pErr = 0;
return (CleanString) (((unsigned int*)passObj)+1);
}
void getDefaultPrintSetupC(int *pErr, CleanString *pCleanString)
{
OSStatus status = noErr;
PMPrintSession printSession = NULL;
PMPageFormat pageFormat = kPMNoPageFormat;
status = PMCreateSession(&printSession);
if (status == noErr)
{
status = PMCreatePageFormat(&pageFormat);
if ((status == noErr) && (pageFormat != kPMNoPageFormat))
{
status = PMSessionDefaultPageFormat(printSession,pageFormat);
}
else
{
if (status == noErr)
status = kPMGeneralError;
}
}
if (status == noErr)
{
Handle flattenFormat = NULL;
PMFlattenPageFormat(pageFormat, &flattenFormat);
*pCleanString = PassMacHandle(&passObj1,flattenFormat,GetHandleSize(flattenFormat),pErr);
DisposeHandle(flattenFormat);
}
else
{
if (pageFormat != kPMNoPageFormat)
{
PMRelease(pageFormat);
pageFormat = kPMNoPageFormat;
}
if (printSession != NULL)
{
PMRelease(printSession);
printSession = NULL;
}
}
return;
}
void prOpenPage(PMPageFormat pageFormat, int os, int *okReturn, int *osReturn)
{
OSStatus err;
*osReturn = os;
err = PMSessionBeginPage(gPrintSession,pageFormat,kDontScaleOutput);
err = PMSessionError(gPrintSession);
*okReturn = err==noErr;
}
void prClosePage(PMPageFormat pageFormat, int os, int *okReturn, int *osReturn)
{
OSStatus err;
*osReturn = os;
err = PMSessionEndPage(gPrintSession);
err = PMSessionError(gPrintSession);
*okReturn = err==noErr;
}
void prOpenDoc(PMPageFormat pageFormat, int os, int *err, int *grPortReturn, int *osReturn)
// error codes: 0=noErr, 2=non fatal error (especially user abort)
// 1 = fatal error.
{
*osReturn = os;
*err = PMSessionBeginDocument(gPrintSession,gPrintSettings,pageFormat);
*err = PMSessionGetGraphicsContext(gPrintSession,NULL,(void**)grPortReturn);
*err = PMSessionError(gPrintSession);
if (*err==noErr)
{ *err=0;
prGrafPortOK = 1;
prGrafPort = *grPortReturn;
}
else if (*err==iMemFullErr) *err=1;
else *err=2;
}
int prCloseDoc(PMPageFormat pageFormat, int os, int pRecHandle)
{
OSStatus err;
prGrafPortOK = 0;
err = PMSessionEndDocument(gPrintSession);
err = PMSessionError(gPrintSession);
return os;
}
void setMaxResolution(PMPageFormat handle)
{
}
void allocPrintRecord(CleanString printSetup,PMPageFormat *pageFormat,int *pErr)
// error code: 0 no error; 1=out of memory
{
int length,i;
char *from,*to;
Handle handle = NULL;
Boolean changed;
OSStatus err;
length = CleanStringLength(printSetup);
handle = NewHandle(length);
if (handle==NULL)
{ *pErr = 1;
return;
};
from = CleanStringCharacters(printSetup);
to = (char*) *handle;
for(i=0;i<length;i++)
to[i] = from[i];
// PMBegin();
err = PMUnflattenPageFormat(handle,pageFormat);
if (err != noErr)
{ *pErr = 1;
DebugStr("\pUnflatten failed");
return;
};
err = PMValidatePageFormat(*pageFormat, &changed);
if (err != noErr)
{ *pErr = 1;
DebugStr("\pValidate failed");
return;
};
// PMEnd();
*pErr = 0;
}
static PMPageFormat sPageFormat;
void getPageDimensionsC( CleanString printSetup, int emulateScreen,
int *pErr,
int *maxX, int *maxY,
// int *leftPaper, int *topPaper,
// int *rightPaper, int *bottomPaper,
int *xRes, int *yRes
)
{
// PMPageFormat pageFormat;
PMRect rect;
PMResolution res;
OSStatus err;
if (!prGrafPortOK) PMBegin();
allocPrintRecord(printSetup,&sPageFormat,pErr);
if (*pErr)
return;
if (!emulateScreen)
{
setMaxResolution(sPageFormat);
};
err = PMGetAdjustedPageRect(sPageFormat,&rect);
if (err != noErr)
{ DebugStr("\pGetAdjustedPageRect failed");
*pErr = err;
return;
};
*maxX = (int)rect.right;
*maxY = (int)rect.bottom;
// According to "Inside Macintosh, ...Quickdraw.." rPage.left and rPage.top are always zero
// status = PMGetAdjustedPaperRect(sPageFormat,rect);
// *leftPaper = (int)rect->left;
// *topPaper = (int)rect->top;
// *rightPaper = (int)rect->right;
// *bottomPaper = (int)rect->bottom;
err = PMGetResolution(sPageFormat,&res);
if (err != noErr)
{ DebugStr("\pGetResolution failed");
*pErr = err;
return;
};
*xRes = (int)res.hRes;
*yRes = (int)res.vRes;
// PMDisposePageFormat(sPageFormat);
DebugStr("\pOK to here");
return;
}
void getPageDimensionsMore( int *pErr,
int *leftPaper, int *topPaper,
int *rightPaper, int *bottomPaper
)
{
PMRect rect;
OSStatus err;
*pErr = 0;
err = PMGetAdjustedPaperRect(sPageFormat,&rect);
if (err != noErr)
{ DebugStr("\pPMGetAdjustedPaperRect failed");
*pErr = err;
return;
};
*leftPaper = (int)rect.left;
*topPaper = (int)rect.top;
*rightPaper = (int)rect.right;
*bottomPaper = (int)rect.bottom;
err = PMDisposePageFormat(sPageFormat);
if (err != noErr)
{ DebugStr("\pPMDisposePageFormat failed");
*pErr = err;
return;
};
if (!prGrafPortOK) PMEnd();
DebugStr("\pOK to here");
return;
}
void printSetupDialogC(CleanString inSetup, int *pErr, CleanString *pOutSetup)
{
PMPrintSession printSession;
PMPageFormat pageFormat;
Boolean changed, accepted;
OSStatus err;
PMBegin();
allocPrintRecord(inSetup,&pageFormat,pErr);
if (*pErr)
return;
PMValidatePageFormat(pageFormat, &changed);
PMEnd();
err = PMCreateSession(&printSession);
if (err) return;
err = PMSessionPageSetupDialog(printSession,pageFormat,&accepted);
if (err == noErr && accepted)
{
Handle flattenFormat = NULL;
PMFlattenPageFormat(pageFormat, &flattenFormat);
*pOutSetup = PassMacHandle(&passObj1,flattenFormat,GetHandleSize(flattenFormat),pErr);
DisposeHandle(flattenFormat);
}
else
*pOutSetup = inSetup;
PMRelease(printSession);
}
void getPrintInfoC( int doDialog, int emulateScreen, CleanString inSetup, int unq,
int *pErr,
int *pRecHandleP,
int *first, int *last,
int *copies,
CleanString *pOutSetup,
int *unqReturn
)
// error code: 0==ok, 1=out of memory, 2==user cancelled
{
PMPageFormat pageFormat;
PMResolution res;
Boolean changed, accepted;
OSStatus err;
void *pTemp;
*unqReturn = unq;
*pOutSetup = inSetup;
// check whether there is enough extra memory for printing
pTemp = NewPtr(200000);
if (!pTemp) {
*pErr = 1;
return;
};
DisposePtr(pTemp);
PMBegin();
allocPrintRecord(inSetup,&pageFormat,pErr);
PMEnd();
if (*pErr)
return;
*pRecHandleP = (int) pageFormat;
err = PMCreateSession(&gPrintSession);
if (!emulateScreen)
setMaxResolution(pageFormat);
err = PMSessionValidatePageFormat(gPrintSession,pageFormat,&changed);
if (changed)
{
Handle flattenFormat = NULL;
PMFlattenPageFormat(pageFormat, &flattenFormat);
*pOutSetup = PassMacHandle(&passObj1,flattenFormat,GetHandleSize(flattenFormat),pErr);
DisposeHandle(flattenFormat);
if (*pErr)
return;
};
err = PMCreatePrintSettings(&gPrintSettings);
err = PMSessionDefaultPrintSettings(gPrintSession,gPrintSettings);
if (doDialog)
{
err = PMSessionPrintDialog(gPrintSession,gPrintSettings,pageFormat,&accepted);
if (err != noErr || !accepted)
{
PMRelease(gPrintSession);
PMRelease(gPrintSettings);
gPrintSession = NULL;
gPrintSettings = NULL;
*pErr = 2;
return;
};
};
*pErr = 0;
err = PMGetFirstPage(gPrintSettings,(UInt32 *)first);
err = PMGetLastPage(gPrintSettings,(UInt32 *)last);
err = PMGetCopies(gPrintSettings,(UInt32 *)copies);
// reset the values for first and last page. This is also done in the
// "imaging with quickdraw" example. (otherwise the following could
// happen: user chooses range 3..5, program generates 3 pages (nr 3..nr 5),
// print manager leaves out two of them, only page nr. 5 is printed
err = PMSetFirstPage(gPrintSettings,1,false);
err = PMSetLastPage(gPrintSettings,kPMPrintAllPages,false);
// store printer resolution in globals for later use in getResolutionC and adjustToPrinterRes
err = PMGetResolution(pageFormat,&res);
prXRes = (int)res.hRes;
prYRes = (int)res.vRes;
}
int prClose(int os)
{
PMRelease(gPrintSession);
PMRelease(gPrintSettings);
gPrintSession = NULL;
gPrintSettings = NULL;
return os;
}
int adjustToPrinterRes(int scrnSize)
// if the current grafport is the printer grafport, then strech the size accordingly
// This function is called in quickdraw.icl
{
short scrnXRes,scrnYRes;
if (isPrinting())
{ ScreenRes(&scrnXRes,&scrnYRes);
return (scrnSize*prYRes) / scrnYRes;
}
else
return scrnSize;
}
int isPrinting()
// if the current grafport is the printer grafport, then strech the size accordingly
// This function is called in quickdraw.icl
{
GrafPtr curGrafPort;
if (!prGrafPortOK)
return 0;
else
{ GetPort(&curGrafPort);
return (int) curGrafPort == (int) prGrafPort;
};
}
void getResolutionC(int *xResP, int *yResP)
{
short scrnXRes,scrnYRes;
if (isPrinting())
{ *xResP = prXRes;
*yResP = prYRes;
}
else
{ ScreenRes(&scrnXRes,&scrnYRes);
*xResP = scrnXRes;
*yResP = scrnYRes;
};
}
int os_printsetupvalid(CleanString inSetup)
{
PMPageFormat pageFormat;
Boolean changed;
OSStatus status;
int err, handleChanged;
if (CleanStringLength(inSetup)!=sizeof(TPrint))
return false;
if (!prGrafPortOK) PMBegin();
allocPrintRecord(inSetup,&pageFormat,&err);
if (err)
return false;
if (!prGrafPortOK)
{
status = PMValidatePageFormat(pageFormat, &changed);
}
else
{
status = PMSessionValidatePageFormat(gPrintSession,pageFormat,&changed);
}
if (!prGrafPortOK) PMEnd();
return !changed;
}
This diff is collapsed.
#include <MacTypes.h>
#include <Files.h>
#include <Events.h>
#include <EPPC.h>
#include <AppleEvents.h>
#include <AERegistry.h>
static char *result_string;
static int n_free_result_string_characters;
static pascal OSErr DoAEOpenApplication (AppleEvent *theAppleEvent,AppleEvent *replyAppleEvent,long refCon)
{
return noErr;
}
static int has_required_parameters (AppleEvent *theAppleEvent)
{
Size actual_size;
DescType returned_type;
OSErr r;
r=AEGetAttributePtr (theAppleEvent,keyMissedKeywordAttr,typeWildCard,&returned_type,NULL,0,&actual_size);
if (r==errAEDescNotFound)
return noErr;
if (r==noErr)
r=errAEEventNotHandled;
return r;
}
static pascal OSErr DoAEOpenDocuments (AppleEvent *theAppleEvent,