home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 1999 November / SOTMC_Nov1999-Ultimate.iso / mac / REALbasic ƒ / Plugins / Plugins SDK / testPlugin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-25  |  4.2 KB  |  182 lines  |  [TEXT/CWIE]

  1. #include "rb_plugin.h"
  2.  
  3. static int add5func(int v)
  4. {
  5.     return v + 5;
  6. }
  7.  
  8. static REALstring plugchrfunc(int ch)
  9. {
  10.     char cch = ch;
  11.     return REALBuildString(&cch, 1);
  12. }
  13.  
  14. static int plugascfunc(REALstring str)
  15. {
  16.     return REALCString(str)[0];
  17. }
  18.  
  19. extern struct REALcontrol boxControl;
  20.  
  21. REALevent boxEvents[] = {
  22.     { "Action(X As Integer,Y As Integer)" }
  23. };
  24.  
  25. Pattern black = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  26.  
  27. struct boxData
  28. {
  29.     int fillColor;
  30.     REALpicture backdrop;
  31.     REALstring caption;
  32. };
  33.  
  34. static void boxDevDraw(REALcontrolInstance instance)
  35. {
  36.     ControlData(boxControl, instance, boxData, data);
  37.     RGBColor oldCol, fillCol;
  38.     Rect r;
  39.  
  40.     REALGetControlBounds(instance, &r);
  41.  
  42.     GetForeColor(&oldCol);
  43.     fillCol.red = (data->fillColor >> 16) * 257;
  44.     fillCol.green = ((data->fillColor >> 8) & 0xff) * 257;
  45.     fillCol.blue = ((data->fillColor & 0xff) * 257);
  46.     RGBForeColor(&fillCol);
  47.     FillRect(&r, &black);
  48.     RGBForeColor(&oldCol);
  49.  
  50.     MoveTo(r.left, r.top);
  51.     LineTo(r.right - 1, r.top);
  52.     LineTo(r.right - 1, r.bottom - 1);
  53.     LineTo(r.left, r.bottom - 1);
  54.     LineTo(r.left, r.top);
  55.     LineTo(r.right - 1, r.bottom - 1);
  56. }
  57.  
  58. static Boolean boxDevClick(REALcontrolInstance instance, int x, int y, int modifiers)
  59. {
  60.     MoveTo(x - 3, y - 3);
  61.     LineTo(x + 3, y + 3);
  62.     MoveTo(x - 3, y + 3);
  63.     LineTo(x + 3, y - 3);
  64.     return true;
  65. }
  66.  
  67. static void boxDevDrag(REALcontrolInstance instance, int x, int y)
  68. {
  69.     MoveTo(x - 3, y - 3);
  70.     LineTo(x + 3, y + 3);
  71.     MoveTo(x - 3, y + 3);
  72.     LineTo(x + 3, y - 3);
  73. }
  74.  
  75. static void boxDevMouseUp(REALcontrolInstance instance, int x, int y)
  76. {
  77.     void (*fp)(REALcontrolInstance instance, int ,int);
  78.  
  79.     fp = (void (*)(REALcontrolInstance instance, int, int)) REALGetEventInstance(instance, &boxEvents[0]);
  80.     if (fp)
  81.         fp(instance, x, y);
  82. }
  83.  
  84. static void boxMakeRed(REALcontrolInstance instance)
  85. {
  86.     ControlData(boxControl, instance, boxData, data);
  87.  
  88.     data->fillColor = 0xFF0000;
  89. }
  90.  
  91. static int boxColorComponent(REALcontrolInstance instance, int bit)
  92. {
  93.     ControlData(boxControl, instance, boxData, data);
  94.  
  95.     if (bit == 0)
  96.         return data->fillColor >> 16;
  97.     else if (bit == 1)
  98.         return (data->fillColor >> 8) & 0xff;
  99.     else
  100.         return (data->fillColor) & 0xff;
  101. }
  102.  
  103. static void boxColorComponentSet(REALcontrolInstance instance, int bit, int value)
  104. {
  105.     ControlData(boxControl, instance, boxData, data);
  106.  
  107.     if (value < 0)
  108.         value = 0;
  109.     else if (value > 255)
  110.         value = 255;
  111.     if (bit == 0)
  112.         data->fillColor = (data->fillColor & 0x00FFFF) | (value << 16);
  113.     else if (bit == 1)
  114.         data->fillColor = (data->fillColor & 0xFF00FF) | (value << 8);
  115.     else
  116.         data->fillColor = (data->fillColor & 0xFFFF00) | (value);
  117.     REALInvalidateControl(instance);
  118. }
  119.  
  120. REALproperty boxProperties[] = {
  121.     { "Appearance", "BackColor", "Color", REALpropInvalidate, REALstandardGetter, REALstandardSetter, FieldOffset(boxData, fillColor) },
  122.     { "Appearance", "Backdrop", "Picture", REALpropInvalidate, REALstandardGetter, REALstandardSetter, FieldOffset(boxData, backdrop) },
  123.     { "Appearance", "Caption", "string", REALpropInvalidate, REALstandardGetter, REALstandardSetter, FieldOffset(boxData, caption) },
  124. };
  125.  
  126. REALmethodDefinition add5defn = {
  127.     (REALproc) add5func,
  128.     REALnoImplementation,
  129.     "add5(v as integer) as integer"
  130. };
  131.  
  132. REALmethodDefinition plugascdefn = {
  133.     (REALproc) plugascfunc,
  134.     REALnoImplementation,
  135.     "plugasc(v as string) as integer"
  136. };
  137.  
  138. REALmethodDefinition plugchrdefn = {
  139.     (REALproc) plugchrfunc,
  140.     REALnoImplementation,
  141.     "plugchr(v as integer) as string"
  142. };
  143.  
  144. REALmethodDefinition boxMethods[] = {
  145.     { (REALproc) boxMakeRed, REALnoImplementation, "makered" },
  146.     { (REALproc) boxColorComponent, (REALproc) boxColorComponentSet, "colorComponent(v as integer) as integer" },
  147. };
  148.  
  149. REALcontrolBehaviour boxBehaviour = {
  150.     nil,
  151.     nil,
  152.     boxDevDraw,
  153.     boxDevClick,
  154.     boxDevDrag,
  155.     boxDevMouseUp,
  156. };
  157.  
  158. REALcontrol boxControl = {
  159.     kCurrentREALControlVersion,
  160.     "box",
  161.     sizeof(boxData),
  162.     REALcontrolAcceptFocus | REALcontrolFocusRing,
  163.     169,
  164.     170,
  165.     64, 32,
  166.     boxProperties,
  167.     sizeof(boxProperties) / sizeof(REALproperty),
  168.     boxMethods,
  169.     sizeof(boxMethods) / sizeof(REALmethodDefinition),
  170.     boxEvents,
  171.     sizeof(boxEvents) / sizeof(REALevent),
  172.     &boxBehaviour
  173. };
  174.  
  175. void PluginEntry(void)
  176. {
  177.     REALRegisterMethod(&add5defn);
  178.     REALRegisterMethod(&plugascdefn);
  179.     REALRegisterMethod(&plugchrdefn);
  180.     REALRegisterControl(&boxControl);
  181. }
  182.