home *** CD-ROM | disk | FTP | other *** search
- #include "rb_plugin.h"
-
- extern struct REALcontrol boxControl;
-
- REALevent boxEvents[] = {
- { "Action(Red As Integer)" }
- };
-
- struct boxData
- {
- int fillColor; // color in RB color format (xxRRGGBB)
- };
-
- static void boxInit(REALcontrolInstance)
- {
- }
-
- #pragma mark boxDraw
- #ifdef TARGET_WIN32
- static void boxDraw(REALcontrolInstance instance, REALgraphics context)
- #else
- static void boxDraw(REALcontrolInstance instance)
- #endif
- {
- // 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);
-
- // Do the drawing (using native graphics toolbox)
- #if TARGET_WIN32
- // Windows implementation:
-
- HDC hDC = REALGraphicsDC(REALGetControlGraphics(instance));
-
- HBRUSH fillBrush = CreateSolidBrush(RGB(
- (data->fillColor >> 16), (data->fillColor >> 8) & 0xFF, (data->fillColor & 0xFF)));
- HBRUSH lineBrush = CreateSolidBrush(RGB(0,0,0));
- HPEN oldPen, linePen = CreatePen(PS_SOLID, 1, RGB(0,0,0));
-
- RECT winr;
- ::SetRect(&winr, r.left, r.top, r.right, r.bottom);
- ::FillRect(hDC, &winr, fillBrush);
-
- ::FrameRect(hDC, &winr, lineBrush);
-
- oldPen = (HPEN) SelectObject(hDC, linePen);
- ::MoveToEx(hDC, r.left, r.top, nil);
- ::LineTo(hDC, r.right - 1, r.bottom - 1);
-
- SelectObject(hDC, oldPen);
-
- DeleteObject(fillBrush);
- DeleteObject(lineBrush);
- DeleteObject(linePen);
-
- #else
- // Mac implementation:
- RGBColor oldCol, fillCol, lineCol = {0,0,0};
-
- GetForeColor(&oldCol);
- fillCol.red = (data->fillColor >> 16) * 257;
- fillCol.green = ((data->fillColor >> 8) & 0xff) * 257;
- fillCol.blue = ((data->fillColor & 0xff) * 257);
- RGBForeColor(&fillCol);
- PaintRect(&r);
-
- RGBForeColor(&lineCol);
- FrameRect(&r);
-
- MoveTo(r.left, r.top);
- LineTo(r.right - 1, r.bottom - 1);
-
- RGBForeColor(&oldCol);
- #endif
- }
-
- 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);
-
- // just for testing, let's also try out some of the newer control methods
- REALSetControlPosition( instance, kREALTop, REALGetControlPosition(instance, kREALTop) - 20 );
- }
-
- 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,
- 129,
- 32, 32,
- boxProperties,
- sizeof(boxProperties) / sizeof(REALproperty),
- boxMethods,
- sizeof(boxMethods) / sizeof(REALmethodDefinition),
- boxEvents,
- sizeof(boxEvents) / sizeof(REALevent),
- &boxBehaviour
- };
-
- void PluginEntry(void)
- {
- REALRegisterControl(&boxControl);
- }
-