// boxControl.cpp // // This file defines a simple example of a REALbasic plug-in control. // It makes a box, with a black outline and diagonal slash, and filled // with a user-definable color. It has a "MakeRed" method which, when // called, sets the color to red, and also invokes an event handler. // //---------------------------------------------------------------------- #import "rb_plugin.h" extern REALcontrol boxControl; REALevent boxEvents[] = { { "Action(Red As Integer)" } }; struct boxData { int fillColor; // color in RB color format (xxRRGGBB) }; static void boxInit(REALcontrolInstance) { } static void boxDraw(REALcontrolInstance instance, REALobject context) { // Find the bounds rect Rect r; REALGetControlBounds(instance, &r); // Make sure our aspect ratio is constant. short height = r.bottom-r.top, width = r.right-r.left; if (width < height) { // shorten height to match width height = width; r.bottom = r.top + height; //if (REALinRuntime()) REALSetControlPosition( instance, kREALHeight, height ); } else if (height < width) { // shorten width to match height width = height; r.right = r.left + width; //if (REALinRuntime()) REALSetControlPosition( instance, kREALWidth, width ); } // Get our custom data into "data" ControlData(boxControl, instance, boxData, data); CGFloat red = (data->fillColor >> 16) / 255.0; CGFloat green = ((data->fillColor >> 8) & 0xff) / 255.0; CGFloat blue = (data->fillColor & 0xff) / 255.0; CGContextRef ctx = [NSGraphicsContext currentContext].CGContext; //CGFloat ww = CGBitmapContextGetWidth(ctx); CGFloat hh = CGBitmapContextGetHeight(ctx); NSString *str2 = [NSString stringWithFormat:@"y=%d, h=%f",r.top,hh]; [str2 drawAtPoint:CGPointMake(10, 20) withAttributes:nil]; CGFloat x, y, w, h, x1, x2, y1, y2; if (REALinRuntime()) { x=0.0; y=0.0; w=r.right-r.left; h=r.bottom-r.top; x1=0; y1=r.bottom-r.top; x2=r.right-r.left; y2=0; } else { x=r.left; y=hh-r.bottom; w=r.right-r.left; h=r.bottom-r.top; x1=r.left; y1=hh-r.top; x2=r.right; y2=hh-r.bottom; //x=r.left; y=hh-r.bottom-16.0; w=r.right-r.left; h=r.bottom-r.top; // 横スクロールバーが表示されている場合はバーの高さ分を引く //x1=r.left; y1=hh-r.top-16.0; x2=r.right; y2=hh-r.bottom-16.0; } CGContextSetRGBFillColor(ctx, red, green, blue, 1.0); CGRect r1 = CGRectMake(x, y, w, h); CGContextAddRect(ctx,r1); CGContextFillPath(ctx); //[[NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1.0f] setFill]; //NSRect aRect = NSMakeRect(x, y, w, h); //NSRectFill(aRect); CGContextSetRGBStrokeColor(ctx, 0.0, 0.0, 0.0, 1.0); CGContextAddRect(ctx,r1); CGContextStrokePath(ctx); //[[NSColor blackColor] setFill]; //NSFrameRect(aRect); CGContextSetLineWidth(ctx, 1.0); // Line width CGContextMoveToPoint(ctx, x1, y1); CGContextAddLineToPoint(ctx, x2, y2); CGContextStrokePath(ctx); //[[NSColor blackColor] setStroke]; //[NSBezierPath strokeLineFromPoint:NSMakePoint(x1, y1) toPoint:NSMakePoint(x2, y2)]; } static void boxMakeRed(REALcontrolInstance instance) { // get our private data ControlData(boxControl, instance, boxData, data); // make the box red data->fillColor = 0xFF0000; // invoke the event handler, if there is one void (*fp)(REALcontrolInstance instance, int); fp = (void (*)(REALcontrolInstance instance, int)) REALGetEventInstance(instance, &boxEvents[0]); if (fp) fp(instance, data->fillColor >> 16); } static int boxColorComponent(REALcontrolInstance instance, int bit) { ControlData(boxControl, instance, boxData, data); if (bit == 0) return data->fillColor >> 16; else if (bit == 1) return (data->fillColor >> 8) & 0xff; else return (data->fillColor) & 0xff; } REALproperty boxProperties[] = { { "Appearance", "BackColor", "Color", REALpropInvalidate, REALstandardGetter, REALstandardSetter, FieldOffset(boxData, fillColor) } }; REALmethodDefinition boxMethods[] = { { (REALproc) boxMakeRed, REALnoImplementation, "MakeRed" }, { (REALproc) boxColorComponent, REALnoImplementation, "ColorComponent(v as integer) as integer" }, }; REALcontrolBehaviour boxBehaviour = { boxInit, nil, boxDraw, nil, nil, nil }; REALcontrol boxControl = { kCurrentREALControlVersion, "Box", sizeof(boxData), 0, // for invisible controls like Timer, use: REALinvisibleControl 128, 128, 32, 32, boxProperties, sizeof(boxProperties) / sizeof(REALproperty), boxMethods, sizeof(boxMethods) / sizeof(REALmethodDefinition), boxEvents, sizeof(boxEvents) / sizeof(REALevent), &boxBehaviour }; void PluginEntry(void) { REALRegisterControl(&boxControl); }