home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d2xx / d269 / propgadget.lha / PropGadget / ex.c < prev    next >
C/C++ Source or Header  |  1989-11-06  |  9KB  |  218 lines

  1. /*  The ill-timed truth we might have kept,
  2.     who knows how sharply it pierced and stung?
  3.     The works we had not the sense to say,
  4.     who knows how grandly they'd have rung?        Milton Sills
  5. */
  6. /*          Jerry J. Trantow                                                  */
  7. /*          1560 A East Irving Place                                          */
  8. /*          Milwaukee, Wi 53202-1460                                          */
  9. /*   The purpose of this program is to illustrate how to calculate the body   */
  10. /*   and pot values in the PropGadget structure.  Many examples I have seen   */
  11. /*   either overflow the LONG variable or don't use all the resolution        */
  12. /*   available.  If your proportional gadgets act funny when you use values   */
  13. /*   greater than 64K you are probably overflowing. One solution is to use    */
  14. /*   floating point for the calculations. I prefer to stay with fixed point   */
  15. /*   math.  Note it would be easy to convert this to assembly, but this is    */
  16. /*   meant to be an easy to understand example.                               */
  17.  
  18. /* 4 Dec 88 Started Example program to illustrate use of proportional Gadgets */
  19. /* 6 Dec 88 Proportional, arrow, and OK gadgetry all set up                   */
  20. /* 7 Dec 88 Added the Calculated IntuiText                                    */
  21. /* 2 Jan 89 Explicitly declared Font to be Topaz, overrides setfont           */
  22. /* 2 Jan 89 Decided to remove Vertical Scroll Gadget for simplicity           */
  23. /* 4 Jan 89 Started Implementation of Reverse Calculation              */
  24. /* 4 Jan 89 Deleted Cancel Gadget                                             */
  25. /* 4 Jan 89 Essentially Finished                                              */
  26. /* 5 Jan 89 Note that I rely on the UserData pointing to the Scroll Gadget    */
  27. /* 8 Jan 89 Decided that a 32x32=64 Multiply was really required              */
  28. /* 12 Jan 89 Also added a 64/32=64 bit Divide                                 */
  29. /* 22 Jan 89 Finished Lucas board, back to programming                        */
  30. /* 24 Jan 89 Added Limit checking for illegal values                          */
  31. /* 29 Jan 89 Changed Dynamic Gadgets to statics for demo purposes             */
  32. /* 30 Jan 89 Cleaned up the Gadgetry and comments                             */
  33. /* 30 Jan 89 Added an 020 QuadMult routine for added performance              */
  34. /* 30 Jan 89 Added Code and IDCMP flags to do calculations on MouseMovements  */
  35. /*  1 Feb 89 Changed variable names to be closer to Harriet Tolly's article   */
  36. /*  4 Feb 89 Compiles under either Lattice or Aztec with minimal Warnings     */
  37. /*  -------------------------- Submitted to Amazing Computing --------------- */
  38. /* 18 Feb 89 looks for 020/881 in the inline version                      */
  39.  
  40. #include <exec/types.h>
  41. #include <exec/devices.h>
  42. #include <exec/memory.h>
  43. #include <exec/execbase.h>
  44. #include <intuition/intuition.h>
  45. #include <intuition/intuitionbase.h>
  46. #include <libraries/dos.h>
  47. #include <libraries/dosextens.h>
  48.  
  49. #ifdef AZTEC_C
  50.   #include <functions.h>
  51.   #define min(x,y) ((x)<(y) ? (x) : (y))
  52.   ULONG  Prop_Gad_Reverse();
  53.   USHORT Prop_Gad_Calculate();
  54.   void   CleanUp();
  55.   void   GadgetUpHandler();
  56.   void   QuadMult020();
  57.   void   QuadDiv020();
  58.   void   QuadAdd();
  59.   void   QuadMult68000();
  60.   void   QuadDiv68000();
  61.   #define FAST register
  62. #endif
  63. #ifdef LATTICE
  64.   #include <stdio.h>
  65.   #include <stdlib.h>
  66.   #include <proto/all.h>
  67.   ULONG  Prop_Gad_Reverse(ULONG,ULONG,ULONG);
  68.   USHORT Prop_Gad_Calculate(ULONG,ULONG,ULONG,ULONG *,ULONG *);
  69.   void   CleanUp(TEXT *);
  70.   void   GadgetUpHandler(struct IntuiMessage *);
  71.   void   QuadMult020(ULONG,ULONG,ULONG *);
  72.   void   QuadDiv020(ULONG *,ULONG);
  73.   void   QuadAdd(ULONG,ULONG *);
  74.   void   QuadMult68000(ULONG,ULONG,ULONG *);
  75.   void   QuadDiv68000(ULONG *,ULONG);
  76.   #define FAST              /* supposedly Lattice knows better */
  77. #endif
  78.                 
  79. #define  LEFTARROWGAD      (1)        /*  Definitions for Gadget ID */ 
  80. #define  RIGHTARROWGAD     (2)
  81. #define  HORIZSCROLLGAD    (3)
  82.  
  83. #define GAD_OK          (100)
  84. #define GAD_Total    (102)        /* these are string gadgets */
  85. #define GAD_First    (103)
  86. #define GAD_Visible    (104)
  87.     
  88. #define MAXBUF  15
  89. #include "prop.inc"         /* this could be compiled individually but */
  90. #include "clean2.inc"           /* this is easier for the novice to follow */
  91. #include "struct.inc"        
  92.  
  93. struct IntuitionBase *IntuitionBase;
  94. struct GfxBase *GfxBase;
  95. struct ExecBase *ExecBase;
  96.  
  97. void main()
  98. {      
  99.   FAST struct Window *WPtr;  /* Points to the Window where everything happens */
  100.   FAST struct RastPort *RPtr;
  101.   FAST struct IntuiMessage *MyIntuiMessage;
  102.   FAST struct Gadget *GPtr;
  103.  
  104.   ULONG GHBody,GHPot;
  105.   FAST SHORT MouseFlag,CLOSEFlag;
  106.  
  107.   IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  108.   if (IntuitionBase==NULL) CleanUp("Can not open Intuition.library.");
  109.   GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0L);
  110.   if (GfxBase==NULL) CleanUp("Can not open graphics library.");
  111.   ExecBase=(struct ExecBase *)OpenLibrary("exec.library",0L);
  112.   if (ExecBase==NULL) CleanUp("Can not open Exec Library.");
  113.  
  114. #ifdef machine=MC68020
  115.   if ((ExecBase->AttnFlags&(UWORD)(AFB_68020))!=AFB_68020)
  116.     CleanUp("020 needed for this .version!!\n");
  117. #endif
  118.  
  119.   sprintf(TotalGadBuffer, "%ld",TotalString.LongInt);    /* Initialize Buffers */
  120.   sprintf(VisibleGadBuffer, "%ld",VisibleString.LongInt);
  121.   sprintf(FirstGadBuffer,"%ld",FirstString.LongInt);
  122.   sprintf(GadUndoBuffer,  "0");
  123.  
  124.   WPtr=(struct Window *)OpenWindow((struct NewWindow *)&NewWindow);
  125.   if (WPtr==NULL) CleanUp("Can not open Window.");
  126.  
  127.   RPtr=WPtr->RPort;
  128.   CLOSEFlag=FALSE;
  129.  
  130.   Prop_Gad_Calculate(VisibleString.LongInt,FirstString.LongInt,TotalString.LongInt,&GHBody,&GHPot);
  131.   sprintf(HBufferBody,"Body Value %04lx",GHBody);
  132.   sprintf(HBufferPot, "Pot Value  %04lx",GHPot);
  133.   PrintIText(RPtr,(struct IntuiText *)&HorizText,120L,75L);
  134.  
  135.   ModifyProp(&ScrollGadget,WPtr,NULL,(LONG)AUTOKNOB|FREEHORIZ,GHPot,0L,GHBody,0L);
  136.          
  137.   while (CLOSEFlag==FALSE)
  138.   { 
  139.     MouseFlag=FALSE;
  140.     Wait((LONG)(1L<<(LONG)WPtr->UserPort->mp_SigBit));
  141.     while (MyIntuiMessage=(struct IntuiMessage *)GetMsg((struct MsgPort *)WPtr->UserPort))
  142.     { 
  143.       switch (MyIntuiMessage->Class)
  144.       {
  145.         case CLOSEWINDOW:        /* close a project */
  146.           CLOSEFlag=TRUE;        /* don't close until messages answered */
  147.           ReplyMsg((struct Message *)MyIntuiMessage);    
  148.           break;
  149.         case MOUSEMOVE:        /* accumulate Mouse Movements */
  150.           MouseFlag=TRUE;
  151.           ReplyMsg((struct Message *)MyIntuiMessage);    
  152.           break;
  153.         case GADGETUP:        /* adjust the cursors to the scroll gadget */
  154.           MouseFlag=FALSE;
  155.           GPtr=(struct Gadget *)MyIntuiMessage->IAddress;
  156.           switch (GPtr->GadgetID)
  157.           {
  158.            case HORIZSCROLLGAD:        /* reverse calculation */
  159.              FirstString.LongInt=(ULONG)Prop_Gad_Reverse(TotalString.LongInt,VisibleString.LongInt,(ULONG)ScrollPropInfo.HorizPot);
  160.              sprintf(FirstGadBuffer,"%lu",FirstString.LongInt);
  161.              break;
  162.            case LEFTARROWGAD:        /* move ONE SAMPLE */
  163.              if (FirstString.LongInt>0L)
  164.                FirstString.LongInt-=1L;
  165.              else
  166.                FirstString.LongInt=0L;
  167.              sprintf(FirstGadBuffer,"%ld",FirstString.LongInt);
  168.              break;
  169.            case RIGHTARROWGAD:        /* move ONE SAMPLE */
  170.              if (VisibleString.LongInt+FirstString.LongInt<TotalString.LongInt)
  171.                FirstString.LongInt+=1L;
  172.              else 
  173.                FirstString.LongInt=TotalString.LongInt-VisibleString.LongInt;
  174.              sprintf(FirstGadBuffer,"%ld",FirstString.LongInt);
  175.              break;
  176.            case GAD_Total:
  177.            case GAD_First:
  178.            case GAD_Visible:
  179.            case GAD_OK:
  180.              VisibleString.LongInt=min(VisibleString.LongInt,TotalString.LongInt);
  181.              sprintf(VisibleGadBuffer,"%ld",VisibleString.LongInt);
  182.              FirstString.LongInt=min(FirstString.LongInt,TotalString.LongInt-VisibleString.LongInt);
  183.              sprintf(FirstGadBuffer,"%ld",FirstString.LongInt);
  184.              break;
  185. /*           default:
  186.              CleanUp("GadgetPtr->GadgetID was invalid\n");
  187.              break; */    /* this break is only for consistancy */
  188.           }
  189.  
  190.           if (GPtr->UserData!=NULL)    /* catches OK,Arrow, and Scroll Gads */
  191.           {
  192.             Prop_Gad_Calculate(VisibleString.LongInt,FirstString.LongInt,TotalString.LongInt,&GHBody,&GHPot);
  193.             sprintf(HBufferBody,"Body Value %04lx",GHBody);
  194.             sprintf(HBufferPot, "Pot Value  %04lx",GHPot);   
  195.             ModifyProp((struct Gadget *)GPtr->UserData,WPtr,NULL,(LONG)AUTOKNOB|FREEHORIZ,GHPot,0L,GHBody,0L);
  196.             PrintIText(RPtr,(struct IntuiText *)&HorizText,120L,75L);
  197.           }
  198.           ReplyMsg((struct Message *)MyIntuiMessage);
  199.           break;
  200. /*        default:            
  201.             CleanUp("Unknown IntuiMessage in switch(Class)");
  202.           break;     */
  203.       } /* switch (Class) */
  204.     }   /* end of IntuiMessages */
  205.     if (MouseFlag==TRUE)
  206.     {
  207.       FirstString.LongInt=(ULONG)Prop_Gad_Reverse(TotalString.LongInt,VisibleString.LongInt,(ULONG)ScrollPropInfo.HorizPot);
  208.       sprintf(FirstGadBuffer,"%lu",FirstString.LongInt);
  209.       sprintf(HBufferPot, "Pot Value  %04x",(USHORT)ScrollPropInfo.HorizPot);  
  210.       PrintIText(RPtr,(struct IntuiText *)&HorizText,120L,75L);
  211.       RefreshGadgets(&FirstGadget,WPtr,NULL);
  212.       MouseFlag=FALSE;
  213.     }
  214.   }   
  215.   CloseWindow(WPtr);
  216.   CleanUp("Clean Finish");
  217. }
  218.