void MCStack::redrawicon() { // MW-2005-07-18: It is possible for this to be called if window == NULL in which // case bad things can happen - so don't let this occur. if (iconid != 0 && window != NULL) { MCImage *iptr = (MCImage *)getobjid(CT_IMAGE, iconid); if (iptr != NULL) { CGImageRef tdockimage; CGrafPtr tport; CGrafPtr curport; CGContextRef context; OSStatus theErr; CGRect cgrect = CGRectMake(0,0,128,128); GetPort( &curport); OSErr err = CreateQDContextForCollapsedWindowDockTile((WindowPtr)window->handle.window, &tport); if (err == noErr) { SetPort(tport); CreateCGContextForPort(tport, &context); tdockimage = iptr -> makeicon(128, 128); CGContextDrawImage(context,cgrect,tdockimage); if ( tdockimage ) CGImageRelease( tdockimage ); CGContextFlush(context); CGContextRelease(context); SetPort(curport); ReleaseQDContextForCollapsedWindowDockTile((WindowPtr)window->handle.window, tport); } } } }
void PoofItGood( Point centerPt ) { CGRect box; WindowRef window; Rect bounds; CGContextRef context; CGImageRef image; float windowWidth; float windowHeight; int i; image = GetThePoofImage(); if ( image == NULL ) goto Bail; windowWidth = CGImageGetWidth( image ) / NUMBER_OF_POOF_ANIM_FRAMES; windowHeight = CGImageGetHeight( image ); // Start our animation bounds at the first item in the animation strip box.origin.x = box.origin.y = 0; box.size.width = CGImageGetWidth( image ); box.size.height = CGImageGetHeight( image ); bounds.top = centerPt.v - (SInt16)(windowHeight / 2); bounds.left = centerPt.h - (SInt16)(windowWidth / 2); bounds.bottom = bounds.top + (SInt16)windowHeight; bounds.right = bounds.left + (SInt16)windowWidth; CreateNewWindow( kOverlayWindowClass, 0, &bounds, &window ); CreateCGContextForPort( GetWindowPort( window ), &context ); ShowWindow( window ); for ( i = 1; i <= NUMBER_OF_POOF_ANIM_FRAMES; i++ ) { CGContextClearRect( context, box ); CGContextDrawImage( context, box, image ); CGContextFlush( context ); Delay( EventTimeToTicks( POOF_ANIMATION_DELAY ), NULL ); box.origin.x -= windowWidth; } CGContextRelease( context ); CGImageRelease( image ); DisposeWindow( window ); Bail: return; }
OSStatus SetCGContext(QuartzDesc *xd) { Rect rect; OSStatus err = noErr; CGRect cgRect; if(xd->context){ CGContextRelease(xd->context); xd->context = NULL; } if(xd->auxcontext){ CGContextRelease(xd->auxcontext); xd->auxcontext = NULL; } if(xd->window) err = CreateCGContextForPort(GetWindowPort(xd->window), &xd->context); if(xd->window) GetPortBounds(GetWindowPort(xd->window), &rect); if(xd->context){ CGContextTranslateCTM(xd->context,0, (float)(rect.bottom - rect.top)); /* Be aware that by performing a negative scale in the following line of code, your text will also be flipped */ CGContextScaleCTM(xd->context, 1, -1); /* We apply here Antialiasing if necessary */ CGContextSetShouldAntialias(xd->context, xd->Antialias); } return err; }
// Draws the current ATSUI data. Takes a GrafPtr as an argument so // that it can handle printing as well as drawing into a window. // void DrawATSUIStuff(GrafPtr drawingPort) { GrafPtr savedPort; Rect portBounds, quarterRect2, quarterRect3; float windowHeight, quarter; CGContextRef context; TXNTextBoxOptionsData optionsData; Boolean needToUseCGStrokeMethod; // Set up the GrafPort GetPort(&savedPort); SetPort(drawingPort); GetPortBounds(drawingPort, &portBounds); EraseRect(&portBounds); // Divide the window into vertical quarters, and draw the text in the middle two quarters windowHeight = portBounds.bottom - portBounds.top; quarter = windowHeight / 4.0; MacSetRect(&quarterRect2, portBounds.left, portBounds.top + quarter, portBounds.right, portBounds.bottom - (quarter * 2.0)); FrameRect(&quarterRect2); MacSetRect(&quarterRect3, portBounds.left, portBounds.top + (quarter * 2.0), portBounds.right, portBounds.bottom - quarter); FrameRect(&quarterRect3); // Set up the CGContext if (gNewCG) QDBeginCGContext(drawingPort, &context); else CreateCGContextForPort(drawingPort, &context); // Setup the options to pass into TXNDrawUnicodeTextBox optionsData.optionTags = kTXNUseCGContextRefMask | kTXNSetFlushnessMask | kTXNUseFontFallBackMask; optionsData.flushness = X2Frac(0.5); // Center the text horizontally, just for this demo. optionsData.options = (void *)context; // This parameter really needs to be renamed, see 3198383. // Draw the text once without the extr bold verify_noerr( TXNDrawUnicodeTextBox(gText, gLength, &quarterRect2, gStyle, &optionsData) ); // ---------------------------------------------------------- // // Here is where we change the setting to do the extra stroke // The value of gStrokeThicknessFactor determines how thick the extra stroke is. // The "standard" value used by ATSUI is 0.024; // this was changed to 0.044 for bug 3189696, // and will probably be changed back, so if you // want the extra stroke, you will have to do it // manually, as is done below. // // The extra stroke method: // - will look good on-screen when CG anti-aliasing is ON // - will look good when printing // - will *NOT* look good on-screen when CG anti-aliasing is OFF // (just use kATSUQDBoldfaceTag in that case) // needToUseCGStrokeMethod = gCurrentlyPrinting || IsAntiAliased(gPointSize); if ( needToUseCGStrokeMethod ) { CGContextSaveGState(context); CGContextSetTextDrawingMode(context, kCGTextFillStroke); CGContextSetLineWidth(context, gStrokeThicknessFactor * Fix2X(gPointSize)); // You might want to call CGContextSetStrokeColor() here, // just to make certain it is the same as the text/fill color. } else MySetBoldfaceTag(gStyle); // This will look very strong on-screen when CG anti-aliasing is off // Draw the text again with the extra bold for comparison verify_noerr( TXNDrawUnicodeTextBox(gText, gLength, &quarterRect3, gStyle, &optionsData) ); // Undo the previous CG text mode setting if ( needToUseCGStrokeMethod ) CGContextRestoreGState(context); else MyClearBoldfaceTag(gStyle); // Tear down the CGContext since we are done with it if (gNewCG) QDEndCGContext(drawingPort, &context); else CGContextRelease(context); // Restore the GrafPort SetPort(savedPort); }
void LineTool( WindowRef window ) { OSStatus err; Point endPt; MouseTrackingResult trackingResult; Point beginPt; Rect windowRect; WindowRef overlayWindow; CGRect cgRect; CGContextRef cgContext; Boolean isStillDown = true; SetThemeCursor( kThemeCrossCursor ); SetPortWindowPort( window ); GetWindowPortBounds( window, &windowRect ); LocalToGlobalRect( &windowRect ); (void) CreateNewWindow( kOverlayWindowClass, kWindowHideOnSuspendAttribute | kWindowIgnoreClicksAttribute, &windowRect, &overlayWindow ); SetPortWindowPort( overlayWindow ); SetWindowGroup( overlayWindow, GetWindowGroup(window) ); // This assures we draw into the same layer as the window ShowWindow( overlayWindow ); GetMouse( &beginPt ); cgRect = CGRectMake( 0, 0, windowRect.right - windowRect.left+1, windowRect.bottom - windowRect.top+1 ); CreateCGContextForPort( GetWindowPort(overlayWindow), &cgContext ); CGContextSetLineWidth( cgContext, 3 ); // Line is 3 pixels wide CGContextSetRGBStrokeColor( cgContext, 1.0, .45, .3, .4 ); // Make it orange with alpha = 0.4 SyncCGContextOriginWithPort( cgContext, GetWindowPort(overlayWindow) ); CGContextTranslateCTM( cgContext, 0, windowRect.bottom - windowRect.top ); // Flip & rotate the context to use QD coordinates CGContextScaleCTM( cgContext, 1.0, -1.0 ); do { err = TrackMouseLocation( GetWindowPort(window), &endPt, &trackingResult ); switch ( trackingResult ) { case kMouseTrackingMouseDragged: CGContextClearRect( cgContext, cgRect ); // "Erase" the window #if ( 1 ) CGContextMoveToPoint( cgContext, beginPt.h, beginPt.v ); // Draw the line CGContextAddLineToPoint( cgContext, endPt.h, endPt.v ); CGContextStrokePath( cgContext ); #else MoveTo( beginPt.h, beginPt.v ); // We could use QuickDraw and draw opaque lines LineTo( endPt.h, endPt.v ); #endif CGContextFlush( cgContext ); // Flush our drawing to the screen break; case kMouseTrackingMouseDown: break; case kMouseTrackingMouseUp: case kMouseTrackingUserCancelled: isStillDown = false; break; } } while( isStillDown == true ); CGContextRelease( cgContext ); DisposeWindow( overlayWindow ); SetThemeCursor( kThemeArrowCursor ); return; }
void MyTimerProc ( EventLoopTimerRef inTimer, void *inUserData ) { WindowRef window = (WindowRef)inUserData; static int i = 0; static int j = 0; static int step = 5; GrafPtr curPort; CGContextRef context; CGrafPtr windowPort = GetWindowPort( window ); GetPort(&curPort); SetPort(windowPort); CreateCGContextForPort( windowPort, &context ); // Do our drawing in here CGContextSetGrayFillColor( context, 0.0, 1.0 ); CGContextFillRect( context, CGRectMake( 0, 0, 800, 600 ) ); CGContextSetGrayFillColor( context, 1.0, 1.0 ); CGContextSetRGBStrokeColor( context, 1.0, 0.0, 0.0, 1.0 ); CGContextSetLineWidth( context, 5.0 ); CGContextSetLineCap( context, kCGLineCapRound ); CGContextSetLineJoin( context, kCGLineJoinRound ); CGContextSetMiterLimit( context, 5 ); // Draw each of the 4 transform demos // // 1. Translate - move the origin, draw the graphic // Place the origin in the llh corner of the upper left quadrant CGContextSaveGState( context ); CGContextTranslateCTM( context, i / 2, i / 2 + 300 ); CGContextRotateCTM( context, -2.0 * 3.1416 / 8 ); drawGraphic( context, 0, 0 ); CGContextRestoreGState( context ); // 2. Rotate - move origin, do rotation, draw the graphic // Put the origin back into the llh corner of the upper right quadrant CGContextSaveGState( context ); if ( j < 1000/2 ) { // Rotate around left circle CGContextTranslateCTM( context, 500, 450 ); CGContextRotateCTM( context, -j * 3.1416 * 2.0 / 500.0 - 3.1416 ); CGContextTranslateCTM( context, -100, -20 ); drawGraphic( context, 0, 0 ); } if ( j >= 1000/2 ) { // Rotate under bottom half and over top right 1/4 CGContextTranslateCTM( context, 700, 450 ); CGContextRotateCTM( context, j * 3.1416 * 2.0 / 500.0 ); CGContextScaleCTM( context, 1, 1 ); CGContextTranslateCTM( context, -100, 20 ); CGContextRotateCTM( context, 3.1416 ); drawGraphic( context, 0, 0 ); } CGContextRestoreGState( context ); // 3. Scale - move origin, set scale, draw the graphic CGContextSaveGState( context ); CGContextScaleCTM( context, i * 400.0 / 500.0 / 40.0, i * 300.0 / 500.0 / 40.0 ); CGContextRotateCTM( context, -3.1416 / 4 ); drawGraphic( context, 0, 0 ); CGContextRestoreGState( context ); // 4. Show the basic graphic CGContextTranslateCTM( context, 600, 150 ); drawGraphic( context, 0, 0 ); CGContextFlush( context ); CGContextRelease( context ); SetPort ( curPort ); i += step; j += abs( step ); if ( i >= 500 ) { step = -step; } if ( i <= 0 ) { step = -step; j = 0; } }