home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / gui / precog2_1.lha / Precognition2_1 / src / src.lha / Library / stringgadget.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-19  |  8.0 KB  |  295 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "StringGadget.h"
  4. #include "StringGadgetClass.h"
  5. #include "minmax.h"
  6. #include "EmbossedGadgetClass.h"
  7. #ifndef __GNUC__
  8. #include <clib/exec_protos.h>
  9. #include <clib/intuition_protos.h>
  10. #include <clib/graphics_protos.h>
  11. #endif
  12. #ifdef __GNUC__
  13. #include <proto/exec.h>
  14. #include <proto/intuition.h>
  15. #include <proto/graphics.h>
  16. #endif
  17. #ifdef __SASC
  18. #include <proto/exec.h>
  19. #include <proto/intuition.h>
  20. #include <proto/graphics.h>
  21. #endif
  22. #include "amigamem.h"
  23.  
  24.  
  25.  
  26. void StringGadget_CleanUp( StringGadget *self )
  27. {
  28.    struct StringInfo *stringinfo;
  29.  
  30.    stringinfo = (struct StringInfo *) self->g.SpecialInfo;
  31.  
  32.    if( stringinfo )
  33.    {
  34.       self->g.SpecialInfo = NULL;
  35.       Afree( stringinfo->Buffer );
  36.       Afree( stringinfo );
  37.    }
  38.  
  39.    Afree( self->BoxBorder );
  40.    self->BoxBorder = NULL;
  41. }
  42.  
  43.  
  44. #define BORDERWIDTH_X 4
  45. #define BORDERWIDTH_Y 2
  46.  
  47. Point StringGadget_SetLocation( StringGadget *self,
  48.                                  PIXELS        LeftEdge,
  49.                                  PIXELS        TopEdge )
  50. {
  51.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  52.    self->Location.x = LeftEdge;
  53.    self->Location.y = TopEdge;
  54.    self->g.LeftEdge = LeftEdge + BORDERWIDTH_X;
  55.    self->g.TopEdge  = TopEdge  + BORDERWIDTH_Y;
  56.    Permit();
  57. /*
  58.    printf( "Location=(%d,%d) (%d,%d), LineWidth=(%d,%d)\n",
  59.       self->Location.x, self->Location.y,
  60.       self->g.LeftEdge, self->g.TopEdge,
  61.       LineWidth.x, LineWidth.y );
  62. */
  63.    return self->Location;
  64. }
  65.  
  66. #define FONT_HEIGHT 8
  67.  
  68. Point StringGadget_AskSize( StringGadget *self,
  69.                              PIXELS        Width,
  70.                              PIXELS        Height )
  71. {
  72.    Point size;
  73.  
  74.    size.x = MAX( Width,  6 + BORDERWIDTH_X*2 );
  75.    size.y = FONT_HEIGHT + BORDERWIDTH_Y*2;
  76.  
  77.    return size;
  78. }
  79.  
  80. Point StringGadget_SetSize( StringGadget *self,
  81.                             PIXELS        Width,
  82.                             PIXELS        Height )
  83. {
  84.  
  85.    pcg_3DBevel *bevel;
  86.    Point        size;
  87.  
  88.    bevel = (pcg_3DBevel *)self->BoxBorder;
  89.    size  = AskSize( (GraphicObject *)self, Width, Height );
  90.  
  91.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  92.    self->Size       = size;
  93.    self->g.Width    = size.x - BORDERWIDTH_X*2;
  94.    self->g.Height   = size.y - BORDERWIDTH_Y*2;
  95.  
  96.    pcg_Init3DBevel( (pcg_3DBevel *) self->BoxBorder,
  97.                     -BORDERWIDTH_X, -BORDERWIDTH_Y, size.x, size.y, 0,
  98.                     self->Pens.BrightPen,  self->Pens.DarkPen, NULL );
  99.    Permit();
  100.    return size;
  101. }
  102.  
  103.  
  104.  
  105.  
  106. USHORT StringGadget_ClaimEvent( StringGadget *self,
  107.                                 IntuiMessage *event )
  108. {
  109.    USHORT response = 0;
  110.  
  111.    switch( event->Class )
  112.    {
  113.       case REFRESHWINDOW:
  114.          response = RESPONDED;
  115.          break;
  116.  
  117.       case GADGETDOWN:
  118.          if( event->IAddress == (void *)&self->g )
  119.          {
  120.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  121.          }
  122.          break;
  123.  
  124.       case GADGETUP:
  125.          if( event->IAddress == (void *)&self->g )
  126.          {
  127.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE | DEACTIVATED;
  128.          }
  129.          break;
  130.    }
  131.  
  132.    return response;
  133. }
  134.  
  135.  
  136. void StringGadget_Refresh( StringGadget *self )
  137. {
  138.    pcgWindow *window;
  139.  
  140.    if( window = InteractorWindow( (Interactor *)self ) )
  141.    {
  142.       EmbossedGadget_Refresh( self );
  143.  
  144.       if( self->LabelText.IText )
  145.          PrintIText( RPort( window ), (struct IntuiText *)&self->LabelText,
  146.                      self->Location.x, self->Location.y );
  147.    }
  148. }
  149.  
  150. void StringGadget_Render( StringGadget *self, RastPort *RPort )
  151. {
  152.  
  153.    EmbossedGadget_Render( self, RPort );
  154.  
  155.    if( self->LabelText.IText )
  156.       PrintIText( RPort, (struct IntuiText *)&self->LabelText,
  157.                   self->Location.x, self->Location.y );
  158. }
  159.  
  160.  
  161.  
  162.                                   /* added missing BOOL argument -- EDB */
  163. BOOL StringGadget_Activate( StringGadget *self, BOOL activate )
  164. {             /* This should probably check the BOOL argument -- EDB */
  165.               /* This should allow deactivation as well? -- EDB */
  166.    pcgWindow *pw;
  167.    Window *w;
  168.  
  169.    if( isEnabled( (Interactor *)self ) )
  170.       if( pw = InteractorWindow( (Interactor *)self ) )
  171.          if( w = iWindow( pw ) )
  172.          {
  173.             ActivateGadget( FirstGadget( (Interactor *)self ), w, NULL );
  174.             return TRUE;
  175.          }
  176.    return FALSE;
  177. }
  178.  
  179. /* Changed to long from 'char *' -- EDB */
  180. long StringGadget_Value( StringGadget *self )
  181. {
  182.    struct StringInfo *stringinfo;
  183.  
  184.    stringinfo = (StringInfo *)self->g.SpecialInfo;
  185.  
  186.    if( stringinfo->NumChars > 0 )
  187.       return /* (char*) */ (long)stringinfo->Buffer;
  188.    else return NULL;
  189.    /* Is it ok to return a long? -- EDB */
  190. }
  191.  
  192. /* Changed to long from 'char *' -- EDB */
  193. long StringGadget_SetValue( StringGadget *self, LONG selection )
  194. {
  195.    struct StringInfo *stringinfo;
  196.  
  197.    stringinfo = (StringInfo *)self->g.SpecialInfo;
  198.    Forbid();
  199.  
  200.    if( selection )
  201.       strncpy( stringinfo->Buffer, (char const *)selection, stringinfo->MaxChars );
  202.    else
  203.       stringinfo->Buffer[0] = '\0';
  204.  
  205.    Permit();
  206.  
  207.    return (long)selection;
  208. }
  209.  
  210.  
  211.  
  212. BOOL StringGadget_elaborated = FALSE;
  213.  
  214. struct ValuatorClass StringGadget_Class;
  215.  
  216. void StringGadgetClass_Init( struct ValuatorClass *class )
  217. {
  218.    ValuatorClass_Init( class );
  219.    EmbossedGadgetClass_Init( (struct InteractorClass *)class );
  220.    class->isa         = ValuatorClass();
  221.    class->ClassName   = "StringGadget";
  222.    class->CleanUp     = (void(*)(PObject *))StringGadget_CleanUp;
  223.    class->SetLocation = (Point(*)(GraphicObject *, PIXELS, PIXELS))StringGadget_SetLocation;
  224.    class->SetSize     = (Point(*)(GraphicObject *, PIXELS, PIXELS))StringGadget_SetSize;
  225.    class->AskSize     = (Point(*)(GraphicObject *, PIXELS, PIXELS))StringGadget_AskSize;
  226.    class->SizeFlags   = GraphicObject_SizeFlagsX; /* X axis only */
  227.    class->Render      = (void(*)(GraphicObject *, RastPort *))StringGadget_Render;
  228.    class->ClaimEvent  = (USHORT(*)(Interactor *, IntuiMessage *))StringGadget_ClaimEvent;
  229.    class->Respond     = (USHORT(*)(Interactor *, IntuiMessage *))StringGadget_ClaimEvent;
  230.    class->Refresh     = (void(*)(Interactor *))StringGadget_Refresh;
  231.    class->Activate    = (BOOL(*)(Interactor *, BOOL))StringGadget_Activate;
  232.    class->isActive    = NULL;
  233.    class->Value       = (LONG(*)(Valuator *))StringGadget_Value;
  234.    class->SetValue    = (LONG(*)(Valuator *, LONG))StringGadget_SetValue;
  235.  
  236. }
  237.  
  238.  
  239. struct ValuatorClass *StringGadgetClass( void )
  240. {
  241.    if( ! StringGadget_elaborated )
  242.    {
  243.       StringGadgetClass_Init( &StringGadget_Class );
  244.       StringGadget_elaborated = TRUE;
  245.    }
  246.  
  247.    return &StringGadget_Class;
  248. }
  249.  
  250.  
  251.  
  252. void StringGadget_Init( StringGadget *self,
  253.                         PIXELS        LeftEdge,
  254.                         PIXELS        TopEdge,
  255.                         PIXELS        Width,
  256.                         USHORT        nChars,
  257.                         pcg_3DPens    Pens,
  258.                         char         *label )
  259. {
  260.    struct StringInfo *stringinfo;
  261.    AlignInfo *ainfo;
  262.  
  263.               /* remove GADGHCOMP -- EDB */
  264.    EmbossedGadget_Init( self, LeftEdge, TopEdge, Width, 0,
  265.          GADGHCOMP /* GFLG_GADGHNONE */ , RELVERIFY, STRGADGET, Pens, NULL );
  266.  
  267.    self->isa             = StringGadgetClass();
  268.    Afree( self->BoxBorder );
  269.    self->BoxBorder       = (pcg_3DBox *) Acalloc(1, sizeof(pcg_3DBevel));
  270.  
  271.    SetLocation( (GraphicObject *)self, LeftEdge, TopEdge );
  272.    SetSize( (GraphicObject *)self, Width, 0 );
  273.  
  274.    ainfo = TextAlignment( (GraphicObject *)self );
  275.    SetTextAlignment( (GraphicObject *)self, tx_OUTSIDE | tx_LEFT | tx_YCENTER,
  276.       ainfo->Xpad, ainfo->Ypad );
  277.  
  278.  
  279.    stringinfo = (struct StringInfo *) Acalloc(1, sizeof(struct StringInfo));
  280.    stringinfo->BufferPos  = 0;
  281.    stringinfo->DispPos    = 0;
  282.    stringinfo->MaxChars   = nChars+1;
  283.    stringinfo->Buffer     = Acalloc( 1, nChars+1 );
  284.    stringinfo->UndoBuffer = NULL;
  285.  
  286.    self->g.GadgetRender  = (APTR)&self->BoxBorder->TopLeft;
  287.    self->g.SelectRender  = NULL;
  288.    self->g.GadgetText    = NULL;
  289.    self->g.MutualExclude = 0L;
  290.    self->g.SpecialInfo   = (APTR)stringinfo;
  291.  
  292.    SetTitle( (GraphicObject *)self, label );
  293. }
  294.  
  295.