示例#1
0
文件: TileGrid.cpp 项目: hnney/webkit
void TileGrid::drawTileMapContents(CGContextRef context, CGRect layerBounds) const
{
    CGContextSetRGBFillColor(context, 0.3, 0.3, 0.3, 1);
    CGContextFillRect(context, layerBounds);

    CGFloat scaleFactor = layerBounds.size.width / m_controller.bounds().width();

    CGFloat contextScale = scaleFactor / m_scale;
    CGContextScaleCTM(context, contextScale, contextScale);

    for (TileMap::const_iterator it = m_tiles.begin(), end = m_tiles.end(); it != end; ++it) {
        const TileInfo& tileInfo = it->value;
        PlatformCALayer* tileLayer = tileInfo.layer.get();

        CGFloat red = 1;
        CGFloat green = 1;
        CGFloat blue = 1;
        CGFloat alpha = 1;
        if (tileInfo.hasStaleContent) {
            red = 0.25;
            green = 0.125;
            blue = 0;
        } else if (m_controller.shouldAggressivelyRetainTiles() && tileInfo.cohort != VisibleTileCohort) {
            red = 0.8;
            green = 0.8;
            blue = 0.8;
        }

        TileCohort newestCohort = newestTileCohort();
        TileCohort oldestCohort = oldestTileCohort();

        if (!m_controller.shouldAggressivelyRetainTiles() && tileInfo.cohort != VisibleTileCohort && newestCohort > oldestCohort)
            alpha = 1 - (static_cast<float>((newestCohort - tileInfo.cohort)) / (newestCohort - oldestCohort));

        CGContextSetRGBFillColor(context, red, green, blue, alpha);

        if (tileLayer->superlayer()) {
            CGContextSetLineWidth(context, 0.5 / contextScale);
            CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
        } else {
            CGContextSetLineWidth(context, 1 / contextScale);
            CGContextSetRGBStrokeColor(context, 0.2, 0.1, 0.9, 1);
        }

        CGRect frame = CGRectMake(tileLayer->position().x(), tileLayer->position().y(), tileLayer->bounds().size().width(), tileLayer->bounds().size().height());
        CGContextFillRect(context, frame);
        CGContextStrokeRect(context, frame);

        CGContextSetRGBFillColor(context, 0, 0, 0, 0.5);

        String repaintCount = String::number(m_tileRepaintCounts.get(tileLayer));

        CGContextSaveGState(context);

        tileLayer->drawTextAtPoint(context, frame.origin.x + 64, frame.origin.y + 192, CGSizeMake(3, -3), 58,
                                   repaintCount.ascii().data(), repaintCount.length());

        CGContextRestoreGState(context);
    }
}
示例#2
0
static void quartzPrepareLine (GraphicsScreen me) {
    CGContextSetLineJoin (my d_macGraphicsContext, kCGLineJoinRound);
    if (my duringXor) {
        CGContextSetBlendMode (my d_macGraphicsContext, kCGBlendModeDifference);
        CGContextSetAllowsAntialiasing (my d_macGraphicsContext, false);
        CGContextSetRGBStrokeColor (my d_macGraphicsContext, 1.0, 1.0, 1.0, 1.0);
    } else {
        CGContextSetRGBStrokeColor (my d_macGraphicsContext, my d_macColour.red / 65536.0, my d_macColour.green / 65536.0, my d_macColour.blue / 65536.0, 1.0);
    }
    double lineWidth_pixels = LINE_WIDTH_IN_PIXELS (me);
    CGContextSetLineWidth (my d_macGraphicsContext, lineWidth_pixels);
    CGFloat lengths [4];
    if (my lineType == Graphics_DOTTED)
        lengths [0] = my resolution > 192 ? my resolution / 100.0 : 2,
                      lengths [1] = my resolution > 192 ? my resolution / 75.0 + lineWidth_pixels : 2;
    if (my lineType == Graphics_DASHED)
        lengths [0] = my resolution > 192 ? my resolution / 25 : 6,
                      lengths [1] = my resolution > 192 ? my resolution / 50.0 + lineWidth_pixels : 2;
    if (my lineType == Graphics_DASHED_DOTTED)
        lengths [0] = my resolution > 192 ? my resolution / 25 : 6,
                      lengths [1] = my resolution > 192 ? my resolution / 50.0 + lineWidth_pixels : 2;
    lengths [2] = my resolution > 192 ? my resolution / 100.0 : 2;
    lengths [3] = my resolution > 192 ? my resolution / 50.0 + lineWidth_pixels : 2;
    CGContextSetLineDash (my d_macGraphicsContext, 0.0, my lineType == Graphics_DRAWN ? NULL : lengths, my lineType == 0 ? 0 : my lineType == Graphics_DASHED_DOTTED ? 4 : 2);
}
示例#3
0
void drawRoundedRect(CGContextRef context, CGRect rrect)
{
    // Drawing with a white stroke color
    CGContextSetRGBStrokeColor(context, 0.3, 0.3, 0.3, 1.0);
    CGContextSetRGBFillColor(context, 0.3, 0.3, 0.3, 1.0);
    
    // Add Rect to the current path, then stroke it
    //            CGContextAddRect(context, CGRectMake(10.0, 190.0, 290.0, 73.0));
    
    
    CGContextSetLineWidth(context, 1);
    CGFloat radius = 10.0;
    
    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
    
    // Next, we will go around the rectangle in the order given by the figure below.
    //       minx    midx    maxx
    // miny    2       3       4
    // midy   1 9              5
    // maxy    8       7       6
    // Which gives us a coincident start and end point, which is incidental to this technique, but still doesn't
    // form a closed path, so we still need to close the path to connect the ends correctly.
    // Thus we start by moving to point 1, then adding arcs through each pair of points that follows.
    // You could use a similar tecgnique to create any shape with rounded corners.
    
    // Start at 1
    CGContextMoveToPoint(context, minx, midy);
    // Add an arc through 2 to 3
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    // Add an arc through 4 to 5
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    // Add an arc through 6 to 7
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    // Add an arc through 8 to 9
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
    // Close the path
    CGContextClosePath(context);
    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);
    
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.0);
    CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
    
    CGContextMoveToPoint(context, minx, midy);
    // Add an arc through 2 to 3
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    // Add an arc through 4 to 5
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    // Add an arc through 6 to 7
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    // Add an arc through 8 to 9
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius);
    // Close the path
    CGContextClosePath(context);
    // Fill & stroke the path
    CGContextDrawPath(context, kCGPathFillStroke);
}
示例#4
0
void drawSelectionRect(CGContextRef context, const CGRect& rect)
{
    CGContextSaveGState(context);
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextStrokeRect(context, rect);
    CGContextRestoreGState(context);
}
void drawSkewedCoordinateSystem(CGContextRef context)
{
    // alpha is 22.5 degrees and beta is 15 degrees.
    float alpha = M_PI/8, beta = M_PI/12;
    CGAffineTransform skew;
    // Create a rectangle that is 72 units on a side
    // with its origin at (0,0).
    CGRect r = CGRectMake(0, 0, 72, 72);
    
    CGContextTranslateCTM(context, 144, 144);
    // Draw the coordinate axes untransformed.
    drawCoordinateAxes(context);
    // Fill the rectangle.
    CGContextFillRect(context, r);

    // Create an affine transform that skews the coordinate system,
    // skewing the x-axis by alpha radians and the y-axis by beta radians. 
    skew = CGAffineTransformMake(1, tan(alpha), tan(beta), 1, 0, 0);
    // Apply that transform to the context coordinate system.
    CGContextConcatCTM(context, skew);

    // Set the fill and stroke color to a dark blue.
    CGContextSetRGBStrokeColor(context, 0.11, 0.208, 0.451, 1);
    CGContextSetRGBFillColor(context, 0.11, 0.208, 0.451, 1);
    
    // Draw the coordinate axes again, now transformed.
    drawCoordinateAxes(context);
    // Set the fill color again but with a partially transparent alpha.
    CGContextSetRGBFillColor(context, 0.11, 0.208, 0.451, 0.7);
    // Fill the rectangle in the transformed coordinate system.
    CGContextFillRect(context, r);
}
示例#6
0
static void quartzgen_path(GVJ_t *job, int filled)
{
	CGContextRef context = (CGContextRef)job->context;
	
	/* set up colors */
	if (filled)
		CGContextSetRGBFillColor(context, job->obj->fillcolor.u.RGBA [0], job->obj->fillcolor.u.RGBA [1], job->obj->fillcolor.u.RGBA [2], job->obj->fillcolor.u.RGBA [3]);
	CGContextSetRGBStrokeColor(context, job->obj->pencolor.u.RGBA [0], job->obj->pencolor.u.RGBA [1], job->obj->pencolor.u.RGBA [2], job->obj->pencolor.u.RGBA [3]);
	
	/* set up line style */
	const CGFloat *segments;
	size_t segment_count;
	switch (job->obj->pen) {
	case PEN_DASHED:
		segments = dashed;
		segment_count = sizeof(dashed)/sizeof(CGFloat);
		break;
	case PEN_DOTTED:
		segments = dotted;
		segment_count = sizeof(dotted)/sizeof(CGFloat);
		break;
	default:
		segments = NULL;
		segment_count = 0;
		break;
	}
	CGContextSetLineDash(context, 0.0, segments, segment_count);

	/* set up line width */
	CGContextSetLineWidth(context, job->obj->penwidth); // *job->scale.x);
	
	/* draw the path */
	CGContextDrawPath(context, filled ? kCGPathFillStroke : kCGPathStroke);
}
示例#7
0
void map_pos_rectangle(MapGC *mgc, MapSettings *settings,
		       int x, int y, int width, int height)
{
  if (FALSE);
#ifdef HAVE_CAIRO
  else if (mgc->cairo_cr)
    {
      cairo_set_source_rgb(mgc->cairo_cr, 0, 0, 1);
      cairo_set_line_width(mgc->cairo_cr, 4);
      cairo_rectangle(mgc->cairo_cr, x, y, width, height);
      cairo_stroke(mgc->cairo_cr);
    }
#endif
#ifdef HAVE_GTK
  else if (mgc->gtk_drawable)
    {
      gdk_gc_set_foreground(mgc->gtk_gc, &settings->blue);
      gdk_gc_set_line_attributes(mgc->gtk_gc, 4, 0, 0, 0);
      gdk_draw_rectangle(mgc->gtk_drawable, mgc->gtk_gc, FALSE,
			 x, y, width, height);
    }
#endif
#ifdef HAVE_QT
  else if (mgc->qt_painter)
    {
      qt_pos_rectangle(mgc->qt_painter, settings,
		       x, y, width, height);
    }
#endif
#ifdef HAVE_QUARTZ
  else if (mgc->quartz_gc)
    {
      CGContextSetRGBStrokeColor(mgc->quartz_gc, 0, 0, 1.0, 1.0);
      CGContextSetLineWidth(mgc->quartz_gc, 4.0);
      //gdk_gc_set_line_attributes(mgc->gtk_gc, 4, 0, 0, 0);
      //gdk_draw_rectangle(mgc->gtk_drawable, mgc->gtk_gc, FALSE,
      //		 x, y, width, height);
    }
#endif
#ifdef WIN32
  else if (mgc->win_dc)
    {
	POINT	p[5];

	p[0].x = x;
	p[0].y = y;
	p[1].x = x + width;
	p[1].y = y;
	p[2].x = x + width;
	p[2].y = y + height;
	p[3].x = x;
	p[3].y = y + height;
	p[4].x = x;
	p[4].y = y;

        win32_setpen(mgc, 4, RGB(0x0, 0x0, 0xff));
	Polyline(mgc->win_dc, p, 5);
    }
#endif
}
DRAW_TEST_P(CGContextFillMode, OverlappedEllipses) {
    CGContextRef context = GetDrawingContext();
    CGRect bounds = GetDrawingBounds();
    bounds = CGRectInset(bounds, 16.f, 16.f);
    CGFloat width = bounds.size.width;
    CGFloat height = bounds.size.height;
    CGFloat xstart = bounds.origin.x;
    CGFloat ystart = bounds.origin.y;

    CGPathDrawingMode fillMode = GetParam();

    CGMutablePathRef leftCircles = CGPathCreateMutable();

    CGPathMoveToPoint(leftCircles, NULL, xstart + .25 * width + .4 * height, ystart + .5 * height);
    CGPathAddArc(leftCircles, NULL, xstart + .25 * width, ystart + .5 * height, .4 * height, 0, M_PI, true);
    CGPathAddArc(leftCircles, NULL, xstart + .25 * width, ystart + .5 * height, .4 * height, M_PI, 0, true);

    CGPathMoveToPoint(leftCircles, NULL, xstart + .25 * width + .3 * height, ystart + .5 * height);
    CGPathAddArc(leftCircles, NULL, xstart + .25 * width, ystart + .5 * height, .3 * height, 0, M_PI, true);
    CGPathAddArc(leftCircles, NULL, xstart + .25 * width, ystart + .5 * height, .3 * height, M_PI, 0, true);

    CGPathMoveToPoint(leftCircles, NULL, xstart + .25 * width + .2 * height, ystart + .5 * height);
    CGPathAddArc(leftCircles, NULL, xstart + .25 * width, ystart + .5 * height, .2 * height, 0, M_PI, true);
    CGPathAddArc(leftCircles, NULL, xstart + .25 * width, ystart + .5 * height, .2 * height, M_PI, 0, true);

    CGPathMoveToPoint(leftCircles, NULL, xstart + .25 * width + .1 * height, ystart + .5 * height);
    CGPathAddArc(leftCircles, NULL, xstart + .25 * width, ystart + .5 * height, .1 * height, 0, M_PI, true);
    CGPathAddArc(leftCircles, NULL, xstart + .25 * width, ystart + .5 * height, .1 * height, M_PI, 0, true);

    CGPathCloseSubpath(leftCircles);

    CGMutablePathRef rightCircles = CGPathCreateMutable();

    CGPathMoveToPoint(rightCircles, NULL, xstart + .75 * width + .4 * height, ystart + .5 * height);
    CGPathAddArc(rightCircles, NULL, xstart + .75 * width, ystart + .5 * height, .4 * height, 0, M_PI, false);
    CGPathAddArc(rightCircles, NULL, xstart + .75 * width, ystart + .5 * height, .4 * height, M_PI, 0, false);

    CGPathMoveToPoint(rightCircles, NULL, xstart + .75 * width + .3 * height, ystart + .5 * height);
    CGPathAddArc(rightCircles, NULL, xstart + .75 * width, ystart + .5 * height, .3 * height, 0, M_PI, true);
    CGPathAddArc(rightCircles, NULL, xstart + .75 * width, ystart + .5 * height, .3 * height, M_PI, 0, true);

    CGPathMoveToPoint(rightCircles, NULL, xstart + .75 * width + .2 * height, ystart + .5 * height);
    CGPathAddArc(rightCircles, NULL, xstart + .75 * width, ystart + .5 * height, .2 * height, 0, M_PI, false);
    CGPathAddArc(rightCircles, NULL, xstart + .75 * width, ystart + .5 * height, .2 * height, M_PI, 0, false);

    CGPathMoveToPoint(rightCircles, NULL, xstart + .75 * width + .1 * height, ystart + .5 * height);
    CGPathAddArc(rightCircles, NULL, xstart + .75 * width, ystart + .5 * height, .1 * height, 0, M_PI, true);
    CGPathAddArc(rightCircles, NULL, xstart + .75 * width, ystart + .5 * height, .1 * height, M_PI, 0, true);

    CGPathCloseSubpath(rightCircles);

    CGContextAddPath(context, leftCircles);
    CGContextAddPath(context, rightCircles);
    CGContextSetRGBFillColor(context, 0, 0, 1, 1);
    CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
    CGContextDrawPath(context, fillMode);

    CGPathRelease(leftCircles);
    CGPathRelease(rightCircles);
}
示例#9
0
FX_BOOL CFX_QuartzDeviceDriver::DrawCosmeticLine(FX_FLOAT           x1,
        FX_FLOAT              y1,
        FX_FLOAT              x2,
        FX_FLOAT              y2,
        FX_DWORD              argb,
        int                   alphaFlag       ,
        void*                 iccTransform    ,
        int					blend_type )
{
    CGBlendMode mode = GetCGBlendMode(blend_type);
    if (mode != kCGBlendModeNormal) {
        CGContextSetBlendMode(_context, mode);
    }
    CGPoint pt = CGPointApplyAffineTransform(CGPointMake(x1, y1), _foxitDevice2User);
    x1 = pt.x;
    y1 = pt.y;
    pt = CGPointApplyAffineTransform(CGPointMake(x2, y2), _foxitDevice2User);
    x2 = pt.x;
    y2 = pt.y;
    FX_INT32 a, r, g, b;
    ArgbDecode(argb, a, r, g, b);
    CGContextSetRGBStrokeColor(_context,
                               r / 255.f,
                               g / 255.f,
                               b / 255.f,
                               a / 255.f);
    CGContextMoveToPoint(_context, x1, y1);
    CGContextAddLineToPoint(_context, x2, y2);
    CGContextStrokePath(_context);
    if (mode != kCGBlendModeNormal) {
        CGContextSetBlendMode(_context, kCGBlendModeNormal);
    }
    return TRUE;
}
void MacVegaPrinterListener::SetClipRect(const OpRect& clip)
{
	CGContextRestoreGState(m_ctx);
	CGContextSaveGState(m_ctx);
	CGContextClipToRect(m_ctx, CGRectMake(clip.x, m_winHeight-(clip.y+clip.height), clip.width, clip.height));
	CGContextSetRGBFillColor(m_ctx, m_red, m_green, m_blue, m_alpha);
	CGContextSetRGBStrokeColor(m_ctx, m_red, m_green, m_blue, m_alpha);
}
示例#11
0
/*
RectanglesDrawEventHandler : Handles the draw events for the "Rectangles" window.

Parameter DescriptionsinHandler : A reference to the current handler call chain. This is passed to your handler so that you can call CallNextEventHandler if you need to.
inEvent : The event that triggered this call.
inUserData : The application-specific data you passed in to InstallEventHandler.
*/
OSStatus RectanglesDrawEventHandler (EventHandlerCallRef inHandler, EventRef inEvent, void* inUserData) {
	OSStatus status = eventNotHandledErr;
	CGContextRef context;
    CGRect r;
	
	//CallNextEventHandler in order to make sure the default handling of the inEvent 
	// (drawing the white background) happens
	status = CallNextEventHandler( inHandler, inEvent );
	require_noerr(status, CantCallNextEventHandler);
	
	// Get the CGContextRef
	status = GetEventParameter (inEvent, 
								kEventParamCGContextRef, 
								typeCGContextRef, 
								NULL, 
								sizeof (CGContextRef),
								NULL,
								&context);
	require_noerr(status, CantGetEventParameter);
	
	/* Set the stroke color to black. */
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);
	
    r.size.height = 210;
    r.origin.x = 20;
    r.origin.y = 20;
    r.size.width = 210;
    frameRect(context, r);
	
    r.size.height = 145;
    r.origin.x = 75;
    r.origin.y = 55;
    r.size.width = 100;
    frameRect(context, r);

    /* Set the fill color to green. */
    CGContextSetRGBFillColor(context, 0, 1, 0, 1);
	
    r.size.height = 210;
    r.origin.x = 270;
    r.origin.y = 20;
    r.size.width = 210;
    paintRect(context, r);
	
    /* Set the fill color to yellow. */
    CGContextSetRGBFillColor(context, 1, 1, 0, 1);
	
    r.size.height = 145;
    r.origin.x = 325;
    r.origin.y = 55;
    r.size.width = 100;
    paintRect(context, r);
	
CantCallNextEventHandler:
CantGetEventParameter:
		
		return status;
}
void MacVegaPrinterListener::SetColor(UINT8 red, UINT8 green, UINT8 blue, UINT8 alpha)
{
	m_red=((float)red)/255.0;
	m_green=((float)green)/255.0;
	m_blue=((float)blue)/255.0;
	m_alpha=((float)alpha)/255.0;
	CGContextSetRGBFillColor(m_ctx, m_red, m_green, m_blue, m_alpha);
	CGContextSetRGBStrokeColor(m_ctx, m_red, m_green, m_blue, m_alpha);
}
示例#13
0
// -----------------------------------------------------------------------------
//	HITestViewDraw
// -----------------------------------------------------------------------------
//	Here's the fun stuff.  Draw a red box when not hilighted, a blue box 
//	when hilighted.
//
OSStatus HITestViewDraw(
	EventRef				inEvent,
	HITestViewData*			inData )
{
	OSStatus				err;
	HIRect					bounds;
	CGContextRef			context;
        float				red, green, blue;
        
	err = GetEventParameter( inEvent, kEventParamCGContextRef, typeCGContextRef,
			NULL, sizeof( CGContextRef ), NULL, &context );
	require_noerr( err, ParameterMissing );

	err = HIViewGetBounds( inData->view, &bounds );
	
        red = inData->red;
	green = inData->green;
	blue = inData->blue;
        
	switch ( GetControlHilite( inData->view ) )
	{
		case kControlNoPart:
			CGContextSetRGBFillColor( context, red, green, blue, 0.25 );
			CGContextSetRGBStrokeColor( context, red, green, blue, 1 );
			break;
		
		// Handle synthetic highlights, too
		case kControlInactivePart:
		case kControlDisabledPart:
			CGContextSetRGBFillColor( context, red, green, blue, 0.10 );
			CGContextSetRGBStrokeColor( context, red, green, blue, 0.10 );
			break;
		
		default:
			CGContextSetRGBFillColor( context, 0, 0, 1, 0.25 );
			CGContextSetRGBStrokeColor( context, 0, 0, 1, 1 );
			break;
	}
	CGContextFillRect( context, bounds );
	CGContextStrokeRect( context, bounds );

ParameterMissing:
	return err;
}
void doRoundedRects(CGContextRef context)
{
	CGRect rect = {{10., 10.}, {210., 150.}};
	float ovalWidth = 100., ovalHeight = 100.;
	CGContextSetLineWidth(context, 2.);
	CGContextBeginPath(context);
	addRoundedRectToPath(context, rect, ovalWidth, ovalHeight);
	CGContextSetRGBStrokeColor(context, 1., 0., 0., 1.);
	CGContextDrawPath(context, kCGPathStroke);
}
示例#15
0
static void Quartz_SetStroke(int color, double gamma, NewDevDesc *dd)
{
    QuartzDesc *xd = (QuartzDesc*)dd->deviceSpecific;
    xd->color = color;
    CGContextSetRGBStrokeColor( GetContext(xd), 
				(float)R_RED(color)/255.0, 
				(float)R_GREEN(color)/255.0, 
				(float)R_BLUE(color)/255.0, 
				(float)R_ALPHA(color)/255.0);
}
示例#16
0
//-----------------------------------------------------------------------------
void CGDrawContext::setFrameColor (const CColor& color)
{
	if (currentState.frameColor == color)
		return;
	
	if (cgContext)
		CGContextSetRGBStrokeColor (cgContext, color.red/255.f, color.green/255.f, color.blue/255.f, color.alpha/255.f);

	CDrawContext::setFrameColor (color);
}
DRAW_TEST_F(CGContextFlush, FillFlushMultipleDrawingCounters, WhiteBackgroundTest<>) {
    CGContextRef context = GetDrawingContext();
    CGRect bounds = GetDrawingBounds();
    static int sDrawCount = 5;

    for (int i = 0; i < sDrawCount; ++i) {
        _CGContextPushBeginDraw(context);
    }

    CGContextSetRGBFillColor(context, 1, 0, 0, 1);
    CGContextFillRect(context, bounds);

    for (int i = 0; i < sDrawCount; ++i) {
        // Multiple flushes should work.
        CGContextFlush(context);
    }

    // Add some extra drawings
    CGContextClearRect(context, bounds);

    // We should still have red & clear should not of been executed.
    unsigned char* dataPtr = static_cast<unsigned char*>(CGBitmapContextGetData(context));
    ASSERT_NE(dataPtr, nullptr);

    // Validate only the red fill rect is executed.
    EXPECT_EQ(dataPtr[0], 0x00);
    EXPECT_EQ(dataPtr[1], 0x00);
    EXPECT_EQ(dataPtr[2], 0xff);
    EXPECT_EQ(dataPtr[3], 0xff);

    for (int i = 0; i < 3; ++i) {
        // Multiple flushes should work.
        CGContextFlush(context);
    }

    // validate clear
    EXPECT_EQ(dataPtr[0], 0x00);
    EXPECT_EQ(dataPtr[1], 0x00);
    EXPECT_EQ(dataPtr[2], 0x00);
    EXPECT_EQ(dataPtr[3], 0x00);

    CGContextSetRGBStrokeColor(context, 0, 1, 0, 1);
    CGContextStrokeRect(context, CGRectMake(100, 100, 200, 300));

    // Still should be clear.
    EXPECT_EQ(dataPtr[0], 0x00);
    EXPECT_EQ(dataPtr[1], 0x00);
    EXPECT_EQ(dataPtr[2], 0x00);
    EXPECT_EQ(dataPtr[3], 0x00);

    for (int i = 0; i < sDrawCount; ++i) {
        _CGContextPopEndDraw(context);
    }
}
DRAW_TEST_F(CGContextFlush, FillFlush, WhiteBackgroundTest<>) {
    CGContextRef context = GetDrawingContext();
    CGRect bounds = GetDrawingBounds();
    _CGContextPushBeginDraw(context);

    CGContextSetRGBFillColor(context, 1, 0, 0, 1);
    CGContextFillRect(context, bounds);

    // Flush the red fill rect
    CGContextFlush(context);

    CGContextSetRGBFillColor(context, 0, 0, 1, 1);
    CGContextFillRect(context, CGRectMake(0, 0, 300, 300));

    // We should still have red & blue rectangle should not show up.
    unsigned char* dataPtr = static_cast<unsigned char*>(CGBitmapContextGetData(context));
    ASSERT_NE(dataPtr, nullptr);

    // Validate only the red fill rect is executed.
    EXPECT_EQ(dataPtr[0], 0x00);
    EXPECT_EQ(dataPtr[1], 0x00);
    EXPECT_EQ(dataPtr[2], 0xff);
    EXPECT_EQ(dataPtr[3], 0xff);

    CGContextFlush(context);

    // We should now see the blue fill rect
    EXPECT_EQ(dataPtr[0], 0xff);
    EXPECT_EQ(dataPtr[1], 0x00);
    EXPECT_EQ(dataPtr[2], 0x00);
    EXPECT_EQ(dataPtr[3], 0xff);

    // Add some extra drawings
    CGContextClearRect(context, CGRectMake(100, 100, 200, 300));

    CGPoint center = _CGRectGetCenter(bounds);

    CGMutablePathRef concentricCirclesPath = CGPathCreateMutable();

    CGPathAddEllipseInRect(concentricCirclesPath, nullptr, _CGRectCenteredOnPoint({ 50, 50 }, center));
    CGPathAddEllipseInRect(concentricCirclesPath, nullptr, _CGRectCenteredOnPoint({ 100, 100 }, center));
    CGPathAddEllipseInRect(concentricCirclesPath, nullptr, _CGRectCenteredOnPoint({ 150, 150 }, center));
    CGPathAddEllipseInRect(concentricCirclesPath, nullptr, _CGRectCenteredOnPoint({ 200, 200 }, center));

    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 0.5);
    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);

    CGContextAddPath(context, concentricCirclesPath);
    CGContextDrawPath(context, kCGPathFillStroke);

    CGPathRelease(concentricCirclesPath);

    _CGContextPopEndDraw(context);
}
示例#19
0
    bool setPen(const GiContext* ctx)
    {
        bool changed = !_ctxused[0];
        
        if (ctx && !ctx->isNullLine())
        {
            if (_gictx.getLineColor() != ctx->getLineColor()) {
                _gictx.setLineColor(ctx->getLineColor());
                changed = true;
            }
            if (_gictx.getLineWidth() != ctx->getLineWidth()) {
                _gictx.setLineWidth(ctx->getLineWidth(), ctx->isAutoScale());
                changed = true;
            }
            if (_gictx.getLineStyle() != ctx->getLineStyle()) {
                _gictx.setLineStyle(ctx->getLineStyle());
                changed = true;
            }
        }
        
        if (!ctx) ctx = &_gictx;
        if (!ctx->isNullLine() && changed)
        {
            _ctxused[0] = true;
            
            GiColor color = ctx->getLineColor();
            if (gs())
                color = gs()->calcPenColor(color);
            CGContextSetRGBStrokeColor(getContext(), 
                                       toFloat(color.r), toFloat(color.g),
                                       toFloat(color.b), toFloat(color.a));
            
            float w = ctx->getLineWidth();
            w = gs() ? gs()->calcPenWidth(w, ctx->isAutoScale()) : (w < 0 ? -w : 1);
            CGContextSetLineWidth(getContext(), _fast && w > 1 ? w - 1 : w); // 不是反走样就细一点
            
            int style = ctx->getLineStyle();
            CGFloat pattern[6];
            
            if (style >= 0 && style < sizeof(lpats)/sizeof(lpats[0])) {
                if (lpats[style].arr && !_fast) {                           // 快速画时不要线型
                    makeLinePattern(pattern, lpats[style].arr, lpats[style].n, w);
                    CGContextSetLineDash(getContext(), 0, pattern, lpats[style].n);
                }
                else {
                    CGContextSetLineDash(getContext(), 0, NULL, 0);
                }
                CGContextSetLineCap(getContext(), style > 0 ? kCGLineCapButt : kCGLineCapRound);
            }
        }

        return !ctx->isNullLine();
    }
示例#20
0
void PlatformCALayer::drawRepaintIndicator(CGContextRef context, PlatformCALayer* platformCALayer, int repaintCount, CGColorRef customBackgroundColor)
{
    char text[16]; // that's a lot of repaints
    snprintf(text, sizeof(text), "%d", repaintCount);
    
    FloatRect indicatorBox = platformCALayer->bounds();\
    indicatorBox.setLocation( { 1, 1 } );
    indicatorBox.setSize(FloatSize(12 + 10 * strlen(text), 27));

    CGContextStateSaver stateSaver(context);
    
    CGContextSetAlpha(context, 0.5f);
    CGContextBeginTransparencyLayerWithRect(context, indicatorBox, 0);
    
    if (customBackgroundColor)
        CGContextSetFillColorWithColor(context, customBackgroundColor);
    else
        CGContextSetRGBFillColor(context, 0, 0.5f, 0.25f, 1);
    
    if (platformCALayer->isOpaque())
        CGContextFillRect(context, indicatorBox);
    else {
        Path boundsPath;
        boundsPath.moveTo(indicatorBox.maxXMinYCorner());
        boundsPath.addLineTo(indicatorBox.maxXMaxYCorner());
        boundsPath.addLineTo(indicatorBox.minXMaxYCorner());

        const float cornerChunk = 8;
        boundsPath.addLineTo(FloatPoint(indicatorBox.x(), indicatorBox.y() + cornerChunk));
        boundsPath.addLineTo(FloatPoint(indicatorBox.x() + cornerChunk, indicatorBox.y()));
        boundsPath.closeSubpath();

        CGContextAddPath(context, boundsPath.platformPath());
        CGContextFillPath(context);
    }

    if (platformCALayer->owner()->isUsingDisplayListDrawing(platformCALayer)) {
        CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.65);
        CGContextSetLineWidth(context, 2);
        CGContextStrokeRect(context, indicatorBox);
    }

    if (platformCALayer->acceleratesDrawing())
        CGContextSetRGBFillColor(context, 1, 0, 0, 1);
    else
        CGContextSetRGBFillColor(context, 1, 1, 1, 1);
    
    platformCALayer->drawTextAtPoint(context, indicatorBox.x() + 5, indicatorBox.y() + 22, CGSizeMake(1, -1), 22, text, strlen(text));
    
    CGContextEndTransparencyLayer(context);
}
DISABLED_DRAW_TEST_F(CGContext, Shadow, WhiteBackgroundTest) {
    CGContextRef context = GetDrawingContext();
    CGRect bounds = GetDrawingBounds();

    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextSetLineWidth(context, 5);

    CGContextSetShadow(context, CGSize{ 10.f, 10.f }, 1.0);

    CGPoint center = _CGRectGetCenter(bounds);
    CGRect rect = _CGRectCenteredOnPoint({ 150, 150 }, center);

    CGContextStrokeRect(context, rect);
}
void doPixelAlignedFillAndStroke(CGContextRef context)
{
    CGPoint p1 = CGPointMake(16.7, 17.8);
    CGPoint p2 = CGPointMake(116.7, 17.8);
    CGRect r = CGRectMake(16.7, 20.8, 100.6, 100.6);
    CGSize s;
    
    CGContextSetLineWidth(context, 2);
    CGContextSetRGBFillColor(context, 1., 0., 0., 1.);
    CGContextSetRGBStrokeColor(context, 1., 0., 0., 1.);
    
    // Unaligned drawing.
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, p1.x, p1.y);
    CGContextAddLineToPoint(context, p2.x, p2.y);
    CGContextStrokePath(context);
    CGContextFillRect(context, r);
    
    // Translate to the right before drawing along
    // aligned coordinates.
    CGContextTranslateCTM(context, 106, 0);
    
    // Aligned drawing.
    
    // Compute the length of the line in user space.
    s = CGSizeMake(p2.x - p1.x, p2.y - p1.y);
    
    CGContextBeginPath(context);
    // Align the starting point to a device
    // pixel boundary.
    p1 = alignPointToUserSpace(context, p1);
    // Establish the starting point of the line.
    CGContextMoveToPoint(context, p1.x, p1.y);
    // Compute the line length as an integer
    // number of device pixels.
    s = alignSizeToUserSpace(context, s);
    CGContextAddLineToPoint(context, 
				p1.x + s.width, 
				p1.y + s.height);
    CGContextStrokePath(context);
    // Compute a rect that is aligned to device
    // space with a width that is an integer
    // number of device pixels.
    r = alignRectToUserSpace(context, r);
    CGContextFillRect(context, r);
}
DISABLED_DRAW_TEST_F(CGContext, ShadowWithRotatedCTM, WhiteBackgroundTest) {
    CGContextRef context = GetDrawingContext();
    CGRect bounds = GetDrawingBounds();

    CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
    CGContextSetLineWidth(context, 5);

    CGContextSetShadow(context, CGSize{ 10.f, 10.f }, 1.0);

    CGPoint center = _CGRectGetCenter(bounds);
    CGRect rect = _CGRectCenteredOnPoint({ 150, 150 }, center);
    CGPoint rectCenter = _CGRectGetCenter(rect);

    CGContextTranslateCTM(context, rectCenter.x, rectCenter.y);
    CGContextRotateCTM(context, 15.f * M_PI / 180.f);
    CGContextTranslateCTM(context, -rectCenter.x, -rectCenter.y);

    CGContextStrokeRect(context, rect);
}
示例#24
0
static void initBackground(unsigned w, unsigned h, unsigned max)
{
    CGColorSpaceRef colorspace;
    CGContextRef  gc;
    unsigned char *data;
    float cx, cy;
    float radius, needle;

    data = (unsigned char *)malloc(w * h * 4);

    colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
    gc         = CGBitmapContextCreate(data, w, h, 8, w * 4, colorspace,
                                   kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host);

    cx    = CENTERX * w;
    cy    = CENTERY * h;
    radius = 0.5 * (w > h ? w : h);
    needle = radius * 0.85;

    // background
    CGContextTranslateCTM(gc, 0.0, h);
    CGContextScaleCTM(gc, 1.0, -1.0);
    CGContextClearRect(gc, CGRectMake(0, 0, w, h));
    CGContextSetRGBFillColor(gc, 0.0, 0.0, 0.0, 0.7);
    CGContextAddArc(gc, cx, cy, radius, 0, 2*M_PI, false);
    CGContextFillPath(gc);

    CGGradientRef gradient;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 1.0, 1.0, 1.0, 0.5,  // Start color
                              0.0, 0.0, 0.0, 0.0
                            }; // End color

    gradient = CGGradientCreateWithColorComponents (colorspace, components,
               locations, num_locations);
    // top rim light
    CGContextSaveGState(gc);
    CGContextAddArc(gc, cx, cy, radius, 0, 2*M_PI, false);
    CGContextAddArc(gc, cx, cy, needle*1.05, 0, 2*M_PI, false);
    CGContextEOClip(gc);
    CGContextDrawRadialGradient (gc, gradient,
                                 CGPointMake(cx, cy*1.00), radius*1.01,
                                 CGPointMake(cx, cy*0.96), radius*0.98, 0);
    // bottom rim light
    CGContextDrawRadialGradient (gc, gradient,
                                 CGPointMake(cx, cy*1.00), radius*1.01,
                                 CGPointMake(cx, cy*1.04), radius*0.98, 0);
    // top bevel
    CGContextDrawRadialGradient (gc, gradient,
                                 CGPointMake(cx, cy*2.2), radius*0.2,
                                 CGPointMake(cx, cy*1.0), radius*1.0, 0);
    // bottom bevel
    CGContextRestoreGState(gc);
    CGContextSaveGState(gc);
    CGContextAddArc(gc, cx, cy, needle*1.05, 0, 2*M_PI, false);
    CGContextAddArc(gc, cx, cy, needle*0.96, 0, 2*M_PI, false);
    CGContextEOClip(gc);
    CGContextDrawRadialGradient (gc, gradient,
                                 CGPointMake(cx, cy* -.5), radius*0.2,
                                 CGPointMake(cx, cy*1.0), radius*1.0, 0);

    CGGradientRelease(gradient);
    CGContextRestoreGState(gc);

    CGContextSetRGBFillColor(gc, 0.9, 0.9, 1.0, 1.0);
    CGContextSetRGBStrokeColor(gc, 0.9, 0.9, 1.0, 1.0);
    CGContextSetLineCap(gc, kCGLineCapRound);

    // draw several glow passes, with the content offscreen
    CGContextTranslateCTM(gc, 0, OFFSCREEN - 10);
    CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7));
    drawMarks(gc, w, h, max);
    CGContextTranslateCTM(gc, 0, 20);
    CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7));
    drawMarks(gc, w, h, max);
    CGContextTranslateCTM(gc, -10, -10);
    CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7));
    drawMarks(gc, w, h, max);
    CGContextTranslateCTM(gc, 20, 0);
    CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7));
    drawMarks(gc, w, h, max);
    CGContextTranslateCTM(gc, -10, -OFFSCREEN);

    // draw real content
    CGContextSetShadowWithColor(gc, CGSizeMake(0, 1), 6.0, CGColorCreateGenericRGB(0.7, 0.7, 1.0, 0.9));
    drawMarks(gc, w, h, max);

    glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data);

    CGContextRelease(gc);
    CGColorSpaceRelease(colorspace);
    free(data);
}
示例#25
0
/*
	Handle events of kEventClassControl that get sent to the Frame
*/
OSStatus HandleStarFrameControlEvents(
	EventHandlerCallRef inCallRef,
	EventRef inEvent,
	StarFrameData* frameData)
{
	OSStatus retVal = eventNotHandledErr;

	switch(GetEventKind(inEvent)) {
		case kEventControlInitialize :
			retVal = HandleStarFrameInitialize(inCallRef, inEvent, frameData);
		break;

		case kEventControlOwningWindowChanged : {
			// We only want the star-shaped opaque area of our frame view to
			// draw.  Everything else should be transparent.  To accomplish that
			// we change the features of the owning window so that only the
			// content we draw shows up on screen
			WindowRef newWindow = GetControlOwner(frameData->hiSelf);
			HIWindowChangeFeatures(newWindow, 0, kWindowIsOpaque);
		} break;

		case kEventControlBoundsChanged : {
			retVal = HandleStarFrameBoundsChanged(inCallRef, inEvent, frameData);
		} break;

        case kEventControlDraw : {
			HIRect			bounds;
			CGContextRef	cgContext;

			HIViewGetBounds(frameData->hiSelf, &bounds);
			float radius = fmin(CGRectGetWidth(bounds) / 2.0, CGRectGetHeight(bounds) / 2.0);

			GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(cgContext), NULL, &cgContext );
			if(NULL != cgContext) {
				HIThemeMenuDrawInfo drawInfo;
				CGPathRef starPath = CreatePathForStarFrame(frameData, radius);

				drawInfo.version = 0;
				drawInfo.menuType = frameData->menuType;

				// HIThemeDrawMenuBackground is designed to draw the pin striped background
				// of standard menus.  Our menu is a star and so HIThemeDrawMenuBackground may not be
				// appropriate in this case.  Nevertheless, we'll draw the standard menu background for
				// this menu and clip it to a star.
				CGContextClearRect(cgContext, bounds);
				CGContextSaveGState(cgContext);
				CGContextTranslateCTM(cgContext, radius, radius);
				CGContextAddPath(cgContext, starPath);
				CGContextClip(cgContext);
				CGContextTranslateCTM(cgContext, -radius, -radius);
				HIThemeDrawMenuBackground(&bounds, &drawInfo, cgContext, kHIThemeOrientationNormal);
				CGContextRestoreGState(cgContext);

				// The pin striping looks a bit odd sort of floating out by itself.  We'll also add
				// a lovely gray line to help emphasize the boundary
				CGContextTranslateCTM(cgContext, radius, radius);
				CGContextAddPath(cgContext, starPath);
				CGContextSetRGBStrokeColor(cgContext, 0.8, 0.8, 0.8, 1.0);
				CGContextSetLineWidth(cgContext, 1.0);
				CGContextStrokePath(cgContext);
				
				CGPathRelease(starPath);
				starPath = NULL;
			}

			retVal = noErr;
		} break;

		// Mac OS X v10.4 introduced a Window Manager bug.
		// The workaround is to implement the kEventControlGetFrameMetrics handler.
		// Even after the bug is fixed, the workaround will not be harmful.
		case kEventControlGetFrameMetrics: {
			HIViewRef contentView = NULL;

			// If we can find our content view, ask it for our metrics
			verify_noerr(HIViewFindByID(frameData->hiSelf, kHIViewWindowContentID, &contentView));
			if(NULL != contentView) {
				retVal = SendEventToEventTargetWithOptions( inEvent, GetControlEventTarget( contentView ), kEventTargetDontPropagate );
			}
		} break;

		default:
		break;
	}

	return retVal;
}
示例#26
0
static void MusicBoxDrawIndicator(HIViewRef view, CGContextRef mboxctx)
{
	if (!showIndicator)
		return;

	// Bar

	const double	length[] = { 1.0, 1.0 };

	CGContextSetLineWidth(mboxctx, mbxBarWidth);
	CGContextSetLineDash(mboxctx, 0, length, 2);
	CGContextSetLineJoin(mboxctx, kCGLineJoinMiter);

	CGContextBeginPath(mboxctx);

	double   x = mbxOffsetX + mbxMarginX + mbxBarWidth / 2.0;
	for (int h = 0; h < 8; h++)
	{
		// Inactive

		CGContextSetRGBStrokeColor(mboxctx, (196.0 / 256.0), (200.0 / 256.0), (176.0 / 256.0), 1.0);

		CGContextMoveToPoint   (mboxctx, x,                mbxOffsetY + mbxMarginY);
		CGContextAddLineToPoint(mboxctx, x,                mbxOffsetY + mbxMarginY + mbxBarHeight);

		CGContextMoveToPoint   (mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY);
		CGContextAddLineToPoint(mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY + mbxBarHeight);

		CGContextStrokePath(mboxctx);

		// Max

		Channel	*ch = &SoundData.channels[h];

		ch->envx = ch->xenvx >> 4;
		ch-> left_vol_level = (ch->xenvx * ch->volume_left ) >> 11;
		ch->right_vol_level = (ch->xenvx * ch->volume_right) >> 11;

		short		vl = ch-> left_vol_level;
		short		vr = ch->right_vol_level;
		long long	currentTime;

		if (vl <= 0) vl = 0; else if (vl > 64) vl = 64; else vl = (short) (yyscale * sqrt((double) vl)) & (~0 << 1);
		if (vr <= 0) vr = 0; else if (vr > 64) vr = 64; else vr = (short) (yyscale * sqrt((double) vr)) & (~0 << 1);

		if (vl < prevLVol[h]) vl = ((prevLVol[h] + vl) >> 1);
		if (vr < prevRVol[h]) vr = ((prevRVol[h] + vr) >> 1);

		Microseconds((UnsignedWide *) &currentTime);

		// left

		if ((vl >= prevLMax[h]) && (vl > prevLVol[h]))
		{
			barTimeL[h] = currentTime;
			prevLMax[h] = vl;
		}
		else
		if ((prevLMax[h] > 0) && (barTimeL[h] + 1000000 > currentTime))
		{
			CGContextSetRGBStrokeColor(mboxctx, (22.0 / 256.0), (156.0 / 256.0), (20.0 / 256.0), (double) (barTimeL[h] + 1000000 - currentTime) / 1000000.0);

			CGContextMoveToPoint   (mboxctx, x, mbxOffsetY + mbxMarginY + (double) (prevLMax[h] - 2));
			CGContextAddLineToPoint(mboxctx, x, mbxOffsetY + mbxMarginY + (double) (prevLMax[h]    ));

			CGContextStrokePath(mboxctx);
		}
		else
			prevLMax[h] = 0;

		prevLVol[h] = vl;

		// right

		if ((vr >= prevRMax[h]) && (vr > prevRVol[h]))
		{
			barTimeR[h] = currentTime;
			prevRMax[h] = vr;
		}
		else
		if ((prevRMax[h] > 0) && (barTimeR[h] + 1000000 > currentTime))
		{
			CGContextSetRGBStrokeColor(mboxctx, (22.0 / 256.0), (156.0 / 256.0), (20.0 / 256.0), (double) (barTimeR[h] + 1000000 - currentTime) / 1000000.0);

			CGContextMoveToPoint   (mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY + (double) (prevRMax[h] - 2));
			CGContextAddLineToPoint(mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY + (double) (prevRMax[h]    ));

			CGContextStrokePath(mboxctx);
		}
		else
			prevRMax[h] = 0;

		prevRVol[h] = vr;

		// Active

		CGContextSetRGBStrokeColor(mboxctx, (22.0 / 256.0), (22.0 / 256.0), (20.0 / 256.0), 1.0);

		CGContextMoveToPoint   (mboxctx, x,                mbxOffsetY + mbxMarginY);
		CGContextAddLineToPoint(mboxctx, x,                mbxOffsetY + mbxMarginY + (double) vl);
		CGContextStrokePath(mboxctx);
		CGContextMoveToPoint   (mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY);
		CGContextAddLineToPoint(mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY + (double) vr);
		CGContextStrokePath(mboxctx);

		x += (mbxBarWidth + mbxBarSpace);
	}
}
示例#27
0
static pascal OSStatus CustomSpotViewHandler(EventHandlerCallRef inCaller, EventRef inEvent, void* inRefcon)
{
	OSStatus result = eventNotHandledErr;
	CustomSpotViewData* myData = (CustomSpotViewData*)inRefcon;
	
	switch (GetEventClass(inEvent))
	{
		case kEventClassHIObject:
			switch (GetEventKind(inEvent))
			{
				case kEventHIObjectConstruct:
				{
					myData = (CustomSpotViewData*) calloc(1, sizeof(CustomSpotViewData));
					GetEventParameter(inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL, sizeof(myData->view), NULL, &myData->view);
					result = SetEventParameter(inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof(myData), &myData);
					break;
				}
					
				case kEventHIObjectInitialize:
				{
					HIRect bounds;
					GetEventParameter(inEvent, kEventParamBounds, typeHIRect, NULL, sizeof(bounds), NULL, &bounds);
					myData->spot.x = CGRectGetMidX(bounds) - CGRectGetMinX(bounds);
					myData->spot.y = CGRectGetMidY(bounds) - CGRectGetMinY(bounds);
					HIViewSetVisible(myData->view, true);
					break;
				}
					
				case kEventHIObjectDestruct:
				{
					free(myData);
					result = noErr;
					break;
				}
					
				default:
					break;
			}
			break;
			
		case kEventClassControl:
			switch (GetEventKind(inEvent))
			{
				case kEventControlDraw:
				{
					CGContextRef	context;
					HIRect			bounds;
					result = GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(context), NULL, &context);
					HIViewGetBounds(myData->view, &bounds);
					
					if (!IsControlActive(myData->view))
					{
						CGContextSetGrayStrokeColor(context, 0.5, 0.3);
						CGContextSetGrayFillColor(context, 0.5, 0.3);
					}
					else
					{
						CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.7);
						CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 0.7);
					}
					
					CGContextSetLineWidth(context, 3.0);
					CGContextStrokeRect(context, bounds);
					
					HIRect spot = { {myData->spot.x - 4.0, myData->spot.y - 4.0}, {8.0, 8.0} };
					CGContextFillRect(context, spot);
					
					result = noErr;
					break;
				}
					
				case kEventControlBoundsChanged:
				{
					HIRect newHIBounds;
					GetEventParameter(inEvent, kEventParamCurrentBounds, typeHIRect, NULL, sizeof(newHIBounds), NULL, &newHIBounds);
					myData->spot.x = CGRectGetMidX(newHIBounds) - CGRectGetMinX(newHIBounds);
					myData->spot.y = CGRectGetMidY(newHIBounds) - CGRectGetMinY(newHIBounds);
					break;
				}
					
				case kEventControlHitTest:
				{
					HIPoint	pt;
					HIRect	bounds;
					GetEventParameter(inEvent, kEventParamMouseLocation, typeHIPoint, NULL, sizeof(pt), NULL, &pt);
					HIViewGetBounds(myData->view, &bounds);
					ControlPartCode part = (CGRectContainsPoint(bounds, pt))?kControlButtonPart:kControlNoPart;
					result = SetEventParameter(inEvent, kEventParamControlPart, typeControlPartCode, sizeof(part), &part);
					break;
				}
					
				case kEventControlTrack:
				{
					Point qdPoint;
					Rect qdWindowBounds;
					HIPoint hiPoint;
					HIRect hiViewBounds;
					MouseTrackingResult mouseStatus = kMouseTrackingMouseDown;
					
					HIViewGetBounds(myData->view, &hiViewBounds);
					GetWindowBounds(GetControlOwner(myData->view), kWindowStructureRgn, &qdWindowBounds);
					
					// handle the first mouseDown before moving
					GetEventParameter(inEvent, kEventParamMouseLocation, typeHIPoint, NULL, sizeof(hiPoint), NULL, &hiPoint);
					
					while (mouseStatus != kMouseTrackingMouseUp)
					{
						if (CGRectContainsPoint(hiViewBounds, hiPoint))
						{
							if (hiPoint.x < hiViewBounds.origin.x+4) hiPoint.x = hiViewBounds.origin.x+4;
							if (hiPoint.x > hiViewBounds.origin.x+hiViewBounds.size.width-4) hiPoint.x = hiViewBounds.origin.x+hiViewBounds.size.width-4;
							if (hiPoint.y < hiViewBounds.origin.y+4) hiPoint.y = hiViewBounds.origin.y+4;
							if (hiPoint.y > hiViewBounds.origin.y+hiViewBounds.size.height-4) hiPoint.y = hiViewBounds.origin.y+hiViewBounds.size.height-4;
							myData->spot = hiPoint;
							HIViewSetNeedsDisplay(myData->view, true);
						}
						
						// a -1 GrafPtr to TrackMouseLocation yields global coordinates
						TrackMouseLocation((GrafPtr)-1L, &qdPoint, &mouseStatus);						
						
						// convert to window-relative coordinates
						hiPoint.x = qdPoint.h - qdWindowBounds.left;
						hiPoint.y = qdPoint.v - qdWindowBounds.top;
						
						// convert to view-relative coordinates
						HIViewConvertPoint(&hiPoint, NULL, myData->view);
					}
					break;
				}
					
					
				default:
					break;
			}
			break;
			
		default:
			break;
	}
	
	return result;
}
// Pulled from GraphicsContextCG
static void setCGStrokeColor(CGContextRef context, const Color& color)
{
    CGFloat red, green, blue, alpha;
    color.getRGBA(red, green, blue, alpha);
    CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
}
示例#29
0
void draw__rgb_stroke_color(double r, double g, double b) {
  CGContextSetRGBStrokeColor(ctx, r, g, b, 1.0);
}
示例#30
0
static void drawMarks(CGContextRef gc, int width, int height, int max)
{
    float   angle, c, s, tick, radius, needle;
    float   r0, r1, r2, r3, cx, cy;
    int     i, start, end, redline, section;

    cx        = CENTERX * width;
    cy        = CENTERY * height;
    redline   = TICKS * SUBTICKS * 0.8;
    radius    = 0.5 * (width > height ? width : height);
    needle    = radius * 0.85;

    for (section = 0; section < 2; section++)
    {
        start = section ? redline + 1 : 0;
        end   = section ? TICKS * SUBTICKS : redline;

        if (section)
            CGContextSetRGBStrokeColor(gc, 1.0, 0.1, 0.1, 1.0);
        else
            CGContextSetRGBStrokeColor(gc, 0.9, 0.9, 1.0, 1.0);

        // inner tick ring
        r0        = 0.97 * needle;
        r1        = 1.04 * needle;
        r2        = 1.00 * needle;
        r3        = 1.01 * needle;
        for (i = start; i <= end ; i++)
        {
            tick  = i / (float)(SUBTICKS * TICKS);
            angle = (2.0 / 3.0) * (2 * M_PI) * tick  -  M_PI / 6;

            c     = cos(angle);
            s     = sin(angle);

            if (i % SUBTICKS != 0)
            {
                CGContextMoveToPoint(gc, cx - r0*c, cy + r0*s);
                CGContextAddLineToPoint(gc, cx - r1*c, cy + r1*s);
            }
            else
            {
                CGContextMoveToPoint(gc, cx - r2*c, cy + r2*s);
                CGContextAddLineToPoint(gc, cx - r3*c, cy + r3*s);
            }
        }
        CGContextSetLineWidth(gc, 2.0);
        CGContextStrokePath(gc);

        // outer tick ring
        start = ((float)start / SUBTICKS) + section;
        end   = ((float)end / SUBTICKS);
        r0        = 1.05 * needle;
        r1        = 1.14 * needle;
        for (i = start; i <= end ; i++)
        {
            tick  = i / (float)(TICKS);
            angle = (2.0 / 3.0) * (2 * M_PI) * tick  -  M_PI / 6;

            c     = cos(angle);
            s     = sin(angle);

            CGContextMoveToPoint(gc, cx - r0*c, cy + r0*s);
            CGContextAddLineToPoint(gc, cx - r1*c, cy + r1*s);
        }
        CGContextSetLineWidth(gc, 3.0);
        CGContextStrokePath(gc);
    }

    CGContextSelectFont(gc, "Arial Bold Italic", 20.0, kCGEncodingMacRoman);
    r0        = 0.82 * needle;

    for (i = 0 ; i <= max ; i += (max / TICKS))
    {
        char   text[20];
        float dx, dy;

        sprintf(text, "%d", i);
        
        // hardcoded text centering for this font size
        {
            if (i > 199)
                dx = -18;
            else if (i > 99)
                dx = -17;
            else if (i > 0)
                dx = -14;
            else
                dx = -12;
            dy = -6.0;
        }
        angle = (2.0 / 3.0) * (2 * M_PI) * i / max  -    M_PI / 6;

        c     = cos(angle);
        s     = sin(angle);

        CGContextShowTextAtPoint(gc, cx - r0*c + dx, cy + r0*s + dy,
                                 text, strlen(text));
    }
}