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 / EmbossedGadget.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  16.1 KB  |  593 lines

  1. /* ==========================================================================
  2. **
  3. **                   EmbossedGadget.c
  4. **
  5. ** ©1991 WILLISoft
  6. **
  7. ** ==========================================================================
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <exec/types.h>
  13. #ifndef __GNUC__
  14. #include <clib/intuition_protos.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/diskfont_protos.h>
  18. #endif
  19. #ifdef __GNUC__
  20. #include <proto/intuition.h>
  21. #include <proto/exec.h>
  22. #include <proto/graphics.h>
  23. #include <proto/diskfont.h>
  24. #endif
  25. #ifdef __SASC
  26. #include <proto/intuition.h>
  27. #include <proto/exec.h>
  28. #include <proto/graphics.h>
  29. #include <proto/diskfont.h>
  30. #endif
  31. #include "AmigaMem.h"
  32.  
  33. #include "EmbossedGadget.h"
  34. #include "EmbossedGadgetClass.h"
  35. #include "minmax.h"
  36. #include "Precognition_Utils.h"
  37.  
  38. #define WAITING 0
  39. #define PRESSED 1
  40.  
  41. void EmbossedGadget_CleanUp( EmbossedGadget *self )
  42. {
  43.    Afree( self->BoxBorder );
  44.    self->BoxBorder = NULL;
  45. }
  46.  
  47.  
  48. tPoint EmbossedGadget_SetLocation(  EmbossedGadget *self,
  49.                                     PIXELS          LeftEdge,
  50.                                     PIXELS          TopEdge )
  51. {
  52.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  53.    self->Location.x  = self->g.LeftEdge = LeftEdge;
  54.    self->Location.y  = self->g.TopEdge  = TopEdge;
  55.    Permit();
  56.  
  57.    return self->Location;
  58. }
  59.  
  60.  
  61.  
  62.  
  63. tPoint EmbossedGadget_AskSize( EmbossedGadget *self,
  64.                                PIXELS          Width,
  65.                                PIXELS          Height )
  66. {
  67.    tPoint size;
  68.  
  69.    size.x = MAX( Width,  6 );
  70.    size.y = MAX( Height, 6 );
  71.  
  72.    return size;
  73. }
  74.  
  75.  
  76. tPoint EmbossedGadget_SetSize( EmbossedGadget *self,
  77.                                PIXELS          Width,
  78.                                PIXELS          Height )
  79. {
  80.    tPoint size;
  81.  
  82.    size = AskSize( (GraphicObject *)self, Width, Height );
  83.  
  84.    Forbid(); /* Don't want Intuition looking at these while we modify em. */
  85.    self->Size     = size;
  86.    self->g.Width  = size.x;
  87.    self->g.Height = size.y;
  88.  
  89.    pcg_Init3DBox(  self->BoxBorder, 0, 0, size.x, size.y,
  90.                    self->Pens.BrightPen,  self->Pens.DarkPen,
  91.                    self->BoxBorder->BottomRight.NextBorder );
  92.    Permit();
  93.  
  94.    AlignText( &self->LabelText, Size( (GraphicObject *)self ) );
  95.  
  96.    return size;
  97. }
  98.  
  99.  
  100. Gadget *EmbossedGadget_FirstGadget( EmbossedGadget *self )
  101. {
  102.    return &self->g;
  103. }
  104.  
  105.  
  106. USHORT EmbossedGadget_nGadgets( EmbossedGadget *self )
  107. {
  108.    return 1;
  109. }
  110.  
  111.  
  112. ULONG EmbossedGadget_IDCMPFlags( EmbossedGadget *self )
  113. {
  114.    ULONG flags;
  115.    UWORD activation;
  116.  
  117.    activation = self->g.Activation;
  118.    flags =    ((activation & GADGIMMEDIATE) ? GADGETDOWN :0)
  119.             | ((activation & RELVERIFY)     ? GADGETUP   :0)
  120.             | ((activation & FOLLOWMOUSE)   ? MOUSEMOVE  :0);
  121.    return flags;
  122. }
  123.  
  124.  
  125. USHORT EmbossedGadget_ClaimEvent( EmbossedGadget *self,
  126.                                   IntuiMessage   *event )
  127. {
  128.    USHORT response = 0;
  129.  
  130.    switch( event->Class )
  131.    {
  132.       case REFRESHWINDOW:
  133.          response = RESPONDED;
  134.          break;
  135.  
  136.       case GADGETDOWN:
  137.       case GADGETUP:
  138.          if( event->IAddress == (void *)&self->g )
  139.          {
  140.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  141.          }
  142.          break;
  143.  
  144.       case MOUSEBUTTONS: /* Duration events. */
  145.       case INTUITICKS:
  146.          if( self->state )
  147.          {
  148.             response = RESPONDED | CHANGED_STATE;
  149.          }
  150.          break;
  151.    }
  152.  
  153.    return response;
  154. }
  155.  
  156.  
  157. USHORT EmbossedGadget_Respond( EmbossedGadget   *self,
  158.                                IntuiMessage     *Event )
  159. {
  160.    USHORT response = 0;
  161.    Window *window;
  162.  
  163.    switch( Event->Class )
  164.    {
  165.       case REFRESHWINDOW:
  166.          Refresh( (Interactor *)self );
  167.          response = RESPONDED;
  168.          break;
  169.  
  170.       case GADGETDOWN:
  171.          if( Event->IAddress == (void *)FirstGadget( (Interactor *)self ) )
  172.          {
  173.            /* EmbossedGadget_SelectRender( self, rport ); */
  174.             if( self->g.Activation & GADGDURATION )
  175.             {
  176.                window = Event->IDCMPWindow;
  177.                self->state = PRESSED;
  178.                /* Save the original IDCMP settings. */
  179.                self->IDCMPbuf = window->IDCMPFlags;
  180.  
  181.                if( ! ( self->g.Activation & FOLLOWMOUSE ) )
  182.                {
  183.                   /*
  184.                   ** For Duration gadgets, on GADGETDOWN, INTUITICKS
  185.                   ** are turned on so the EmbossedGadget can get
  186.                   ** continuous messages.
  187.                   */
  188.                   ModifyIDCMP( window,
  189.                         window->IDCMPFlags | INTUITICKS | MOUSEBUTTONS );
  190.                }
  191.             }
  192.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  193.          }
  194.          break;
  195.  
  196.       case GADGETUP:
  197.          if( Event->IAddress == (void *)FirstGadget( (Interactor *)self ) )
  198.          {
  199.             /* EmbossedGadget_SelectRenderRestore( self, rport ); */
  200.             window = Event->IDCMPWindow;
  201.             if( self->state ) /* Duration turned on */
  202.             {
  203.                self->state = WAITING;
  204.                ModifyIDCMP( window, self->IDCMPbuf );
  205.             }
  206.  
  207.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  208.          }
  209.          break;
  210.  
  211.       case INTUITICKS:
  212.          if( self->state )
  213.          {  /* User is still pressing gadget */
  214.             response = RESPONDED | CHANGED_STATE;
  215.          }
  216.          break;
  217.  
  218.       case MOUSEBUTTONS:
  219.          /* MouseButtons happen when the User moves the mouse off
  220.           * off the gadget before releasing.
  221.           */
  222.          if( self->state )
  223.          {
  224.             window      = Event->IDCMPWindow;
  225.             self->state = WAITING;
  226.             ModifyIDCMP( window, self->IDCMPbuf );
  227.  
  228.             response = RESPONDED | CHANGED_STATE;
  229.          }
  230.          break;
  231.  
  232.       case MOUSEMOVE:
  233.          if( self->state )
  234.          {
  235.             response = RESPONDED | CONSUMED_EVENT | CHANGED_STATE;
  236.          }
  237.          break;
  238.  
  239.    }
  240.    return response;
  241. }
  242.  
  243.  
  244. void EmbossedGadget_Refresh( EmbossedGadget *self )
  245. {
  246.    pcgWindow *window;
  247.    RastPort  *rport;
  248.    Point loc;
  249.  
  250.    if( window = InteractorWindow( (Interactor *)self ) )
  251.    {
  252.       RefreshGList( FirstGadget( (Interactor *)self ), iWindow( window ), NULL,
  253.                      nGadgets( (Interactor *)self ) );
  254.       if( self->LabelText.IText )
  255.       {
  256.          rport = RPort( window );
  257.          loc   = Location( (GraphicObject *)self );
  258.          PrintIText( rport, (struct IntuiText *)&self->LabelText, loc.x, loc.y );
  259.       }
  260.    }
  261. }
  262.  
  263. void EmbossedGadget_Render( EmbossedGadget *self, RastPort *rport )
  264. {
  265.    PIXELS x, y;
  266.    Point loc;
  267.  
  268.    x = self->g.LeftEdge;
  269.    y = self->g.TopEdge;
  270.  
  271.    if( self->g.GadgetRender )
  272.    {
  273.       if( self->g.Flags & GADGIMAGE )
  274.       {
  275.          DrawImage( rport, (struct Image *)self->g.GadgetRender, x, y );
  276.       }
  277.       else
  278.       {
  279.          DrawBorder( rport, (struct Border *)self->g.GadgetRender, x, y );
  280.       }
  281.    }
  282.  
  283.    if( self->g.GadgetText )
  284.    {
  285.       PrintIText( rport, self->g.GadgetText, x, y );
  286.    }
  287.  
  288.    if( self->LabelText.IText )
  289.    {
  290.       loc = Location( (GraphicObject *)self );
  291.       PrintIText( rport, (struct IntuiText *)&self->LabelText, loc.x, loc.y );
  292.    }
  293. }
  294.  
  295. #if 0 /* not ready for prime time */
  296. void EmbossedGadget_SelectRender( EmbossedGadget *self, RastPort *rport )
  297. {
  298.    PIXELS x, y;
  299.    Point loc;
  300.  
  301.    x = self->g.LeftEdge;
  302.    y = self->g.TopEdge;
  303.  
  304.    /* SetAPen( ??? ) */
  305.    /* SetBPen( ??? ) */
  306.    /* SetDrawMd( ??? ) */
  307.    /* RectFill from (x,y) to (x+size.x, y+size.y) with select color (blue (3) ) */
  308.    /* then draw in reverse colors */
  309.  
  310.    if( self->g.GadgetRender )
  311.    {
  312.       if( self->g.Flags & GADGIMAGE )
  313.       {
  314.          DrawImage( rport, (struct Image *)self->g.GadgetRender, x, y );
  315.       }
  316.       else
  317.       {
  318.          DrawBorder( rport, (struct Border *)self->g.GadgetRender, x, y );
  319.       }
  320.    }
  321.  
  322.    if( self->g.GadgetText )
  323.    {
  324.       PrintIText( rport, self->g.GadgetText, x, y );
  325.    }
  326.  
  327.    if( self->LabelText.IText )
  328.    {
  329.       loc = Location( (GraphicObject *)self );
  330.       PrintIText( rport, (struct IntuiText *)&self->LabelText, loc.x, loc.y );
  331.    }
  332.  
  333. /*  Restore things to normalcy  */
  334.    /* SetAPen( ??? ) */
  335.    /* SetBPen( ??? ) */
  336.    /* SetDrawMd( ??? ) */
  337.  
  338. }
  339. #endif /* not ready for prime time */
  340.  
  341. #if 0 /* not ready for primetime */
  342. void EmbossedGadget_SelectRenderRestore( EmbossedGadget *self, RastPort *rport )
  343. {
  344.  
  345. /*  Restore things to normalcy  */
  346.    /* SetAPen( ??? ) */
  347.    /* SetBPen( ??? ) */
  348.    /* SetDrawMd( ??? ) */
  349.  /* rectfill back to normal background */
  350.   /* redraw in the normal state */
  351.   EmbossedGadget_Render( self, rport );
  352.  
  353. }
  354. #endif /* not ready for prime time */
  355.  
  356. BOOL EmbossedGadget_EnableIactor( EmbossedGadget *self,
  357.                             BOOL            enable )
  358. {
  359.    struct pcgWindow *window;
  360.    struct Window    *iwindow;
  361.    RastPort *rport;
  362.    Gadget *g;
  363.    PIXELS xmin, xmax, ymin, ymax;
  364.    short i, ngadgets;
  365.  
  366.    window   = InteractorWindow( (Interactor *)self );
  367.    g        = FirstGadget( (Interactor *)self );
  368.    ngadgets = nGadgets( (Interactor *)self );
  369.  
  370.    if( enable )
  371.    {
  372.       if( window && ( rport = RPort( window ) ) ) /* Window is open */
  373.       {
  374.          for( i = 0; i < ngadgets; i++, g = g->NextGadget )
  375.          {
  376.             xmin  = g->LeftEdge;
  377.             ymin  = g->TopEdge;
  378.             xmax  = xmin + g->Width - 1;
  379.             ymax  = ymin + g->Height - 1;
  380.  
  381.             OnGadget( g, window->Window, NULL );
  382.  
  383.             SetDrMd( rport, JAM1 );
  384.             SetAPen( rport, self->Pens.BackPen );
  385.             RectFill( rport, xmin, ymin, xmax, ymax );
  386.             Refresh( (Interactor *)self );
  387.          }
  388.       }
  389.       else
  390.       {
  391.          for( i = 0; i < ngadgets; i++, g = g->NextGadget )
  392.          {
  393.             g->Flags &= ( ~ GADGDISABLED );
  394.          }
  395.       }
  396.    }
  397.    else
  398.    {
  399.       if( window && ( iwindow = iWindow( window ) ) ) /* window is open. */
  400.       {
  401.          for( i = 0; i < ngadgets; i++, g = g->NextGadget )
  402.          {
  403.             OffGadget( g, iwindow, NULL );
  404.          }
  405.       }
  406.       else
  407.       {
  408.          for( i = 0; i < ngadgets; i++, g = g->NextGadget )
  409.          {
  410.             g->Flags |= GADGDISABLED;
  411.          }
  412.       }
  413.    }
  414.  
  415.    return enable;
  416. }
  417.  
  418.  
  419. BOOL EmbossedGadget_isEnabled( Interactor *self ) /*,   FIXED? -- EDB */
  420.                     /*           BOOL       enable )   */ /* BROKEN? -- EDB */
  421. {
  422.    Gadget *g;
  423.  
  424.    if( g = FirstGadget( self ) )
  425.       return (BOOL)( ! ( g->Flags & GADGDISABLED ) );
  426.    else
  427.       return FALSE;
  428. }
  429.  
  430.  
  431. char *EmbossedGadget_Title( EmbossedGadget *self )
  432. {
  433.    return (char *)self->LabelText.IText;
  434. }
  435.  
  436. BOOL EmbossedGadget_SetTitle( EmbossedGadget *self,
  437.                               char           *title )
  438. {
  439.    Afree( self->LabelText.IText );
  440.    self->LabelText.IText  = Astrdup( title );
  441.    AlignText( &self->LabelText, Size( (GraphicObject *)self ) );
  442.  
  443.    return TRUE;
  444. }
  445.  
  446. /* EDB -- Added Default Font Support Methods */
  447. extern TextAttr go_DefaultFont;
  448.  
  449. TextAttr *EmbossedGadget_DefaultFont( EmbossedGadget *self )
  450. {
  451.    if( self->LabelText.ITextFont != NULL )
  452.       return self->LabelText.ITextFont;
  453.    else return &go_DefaultFont;  /* Topaz80 */
  454. }
  455.  
  456. BOOL EmbossedGadget_SetDefaultFont( EmbossedGadget *self, TextAttr *default_font )
  457. {
  458.    struct TextFont *myfont;
  459.  
  460.    if( default_font != NULL )
  461.    {      /* check to see if font is really available */
  462.      if( myfont = OpenDiskFont( default_font ) )
  463.        {
  464.        self->LabelText.ITextFont = default_font;
  465.  
  466.        CloseFont( myfont );
  467.        /* Get ourselves back into alignment */
  468.        AlignText( &self->LabelText, Size( (GraphicObject *)self ) );
  469.        return TRUE;
  470.        }
  471.      else return FALSE; /* font is not available */
  472.    }
  473.    else return FALSE;   /* pointer was invalid */
  474. }
  475.  
  476.         /* changed from AlignInfo to (struct AlignInfo *) -- EDB */
  477. struct AlignInfo *EmbossedGadget_TextAlignment(  GraphicObject *self )
  478. {
  479.    EmbossedGadget *us = NULL;
  480.    us = (EmbossedGadget *)self;
  481.    return( &(us->LabelText.alignment) );
  482. }
  483.  
  484.         /* changed from AlignInfo to (struct AlignInfo *) -- EDB */
  485. BOOL EmbossedGadget_SetTextAlignment( GraphicObject *self,
  486.                                            UBYTE     Flags,
  487.                                            BYTE      Xpad,
  488.                                            BYTE      Ypad )
  489. {
  490.    EmbossedGadget *us = NULL;
  491.    us = (EmbossedGadget *)self;
  492.    us->LabelText.alignment.Flags = Flags;
  493.    us->LabelText.alignment.Xpad  = Xpad;
  494.    us->LabelText.alignment.Ypad  = Ypad;
  495.    AlignText( &us->LabelText, Size( (GraphicObject *)self ) );
  496.  
  497.    return TRUE;
  498. }
  499.  
  500.  
  501. BOOL EmbossedGadget_elaborated = FALSE;
  502.  
  503. struct InteractorClass EmbossedGadget_Class;
  504.  
  505. void EmbossedGadgetClass_Init( struct InteractorClass *class )
  506. {
  507.    InteractorClass_Init( class );
  508.    class->isa         = InteractorClass();
  509.    class->ClassName   = "EmbossedGadget";
  510.    class->CleanUp     = (void(*)(PObject *))EmbossedGadget_CleanUp;
  511.    class->SetLocation = (Point(*)(GraphicObject *, PIXELS, PIXELS))EmbossedGadget_SetLocation;
  512.    class->SetSize     = (Point(*)(GraphicObject *, PIXELS, PIXELS))EmbossedGadget_SetSize;
  513.    class->AskSize     = (Point(*)(GraphicObject *, PIXELS, PIXELS))EmbossedGadget_AskSize;
  514.    class->Render      = (void(*)(GraphicObject *, RastPort *))EmbossedGadget_Render;
  515.    class->FirstGadget = (Gadget*(*)(Interactor *))EmbossedGadget_FirstGadget;
  516.    class->nGadgets    = (USHORT(*)(Interactor *))EmbossedGadget_nGadgets;
  517.    class->IDCMPFlags  = (ULONG(*)(Interactor *))EmbossedGadget_IDCMPFlags;
  518.    class->ClaimEvent  = (USHORT(*)(Interactor *, IntuiMessage *))EmbossedGadget_ClaimEvent;
  519.    class->Respond     = (USHORT(*)(Interactor *, IntuiMessage *))EmbossedGadget_Respond;
  520.    class->Refresh     = (void(*)(Interactor *))EmbossedGadget_Refresh;
  521.    class->EnableIactor= (BOOL(*)(Interactor *, BOOL))EmbossedGadget_EnableIactor;
  522.    class->isEnabled   = (BOOL(*)(Interactor *))EmbossedGadget_isEnabled;
  523.    class->Activate    = NULL;
  524.    class->isActive    = NULL;
  525.    class->Title       = (char*(*)(GraphicObject *))EmbossedGadget_Title;
  526.    class->SetTitle    = (BOOL(*)(GraphicObject *, char *))EmbossedGadget_SetTitle;
  527.    class->DefaultFont = (TextAttr*(*)(GraphicObject *))EmbossedGadget_DefaultFont;
  528.    class->SetDefaultFont = (BOOL(*)(GraphicObject *, TextAttr *))EmbossedGadget_SetDefaultFont;
  529.    class->TextAlignment = (AlignInfo*(*)(GraphicObject *))EmbossedGadget_TextAlignment;
  530.    class->SetTextAlignment = (BOOL(*)(GraphicObject *, UBYTE, BYTE, BYTE))EmbossedGadget_SetTextAlignment;
  531. }
  532.  
  533.  
  534. struct InteractorClass *EmbossedGadgetClass( void )
  535. {
  536.    if( ! EmbossedGadget_elaborated )
  537.    {
  538.       EmbossedGadgetClass_Init( &EmbossedGadget_Class );
  539.       EmbossedGadget_elaborated = TRUE;
  540.    }
  541.  
  542.    return &EmbossedGadget_Class;
  543. }
  544.  
  545.  
  546.  
  547. void EmbossedGadget_Init( EmbossedGadget *self,
  548.                           SHORT           LeftEdge,
  549.                           SHORT           TopEdge,
  550.                           SHORT           Width,
  551.                           SHORT           Height,
  552.                           USHORT          Flags,
  553.                           USHORT          Activation,
  554.                           USHORT          GadgetType,
  555.                           pcg_3DPens      Pens,
  556.                           char           *Label )
  557. {
  558.    Interactor_Init( (Interactor *)self );
  559.  
  560.    self->isa             = EmbossedGadgetClass();
  561.    self->Pens            = Pens;
  562.    self->BoxBorder       = (pcg_3DBox *) Acalloc( 1, sizeof( pcg_3DBox ) );
  563.    self->state           = WAITING;
  564.  
  565.    self->LabelText.FrontPen  = Pens.FrontPen;
  566.    self->LabelText.BackPen   = Pens.BackPen;
  567.    self->LabelText.DrawMode  = JAM1;
  568.    self->LabelText.ITextFont = &go_DefaultFont;  /* &pcg_Topaz80; */
  569.    self->LabelText.IText     = NULL;
  570.    self->LabelText.NextText  = NULL;
  571.    self->LabelText.alignment.Flags = tx_INSIDE | tx_LEFT | tx_YCENTER;
  572.    self->LabelText.alignment.Xpad  = STD_XPAD;
  573.    self->LabelText.alignment.Ypad  = STD_YPAD;
  574.  
  575.    self->g.NextGadget    = NULL;
  576.    self->g.Flags         = Flags;
  577.    self->g.Activation    = Activation;
  578.    self->g.GadgetType    = GadgetType;
  579.    self->g.GadgetRender  = (APTR) &self->BoxBorder->TopLeft;
  580.    self->g.SelectRender  = NULL;
  581.    self->g.MutualExclude = 0L;
  582.    self->g.SpecialInfo   = NULL;
  583.    self->g.GadgetID      = 0;
  584.    self->g.UserData      = NULL;
  585.    self->g.GadgetText    = NULL;
  586.  
  587.    SetLocation( (GraphicObject *)self, LeftEdge, TopEdge );
  588.    SetSize( (GraphicObject *)self, Width, Height );
  589.    SetTitle( (GraphicObject *)self, Label );
  590.  
  591. }
  592.  
  593.