home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / FreeWheel / Scroll.c < prev    next >
C/C++ Source or Header  |  2000-01-02  |  5KB  |  223 lines

  1. #include <stdio.h>
  2.  
  3. #include <exec/types.h>
  4.  
  5. #include <intuition/intuitionbase.h>
  6. #include <intuition/intuition.h>
  7. #include <intuition/screens.h>
  8. #include <graphics/clip.h>
  9. #include <devices/inputevent.h>
  10.  
  11. #include <clib/exec_protos.h>
  12. #include <clib/intuition_protos.h>
  13. #include <clib/layers_protos.h>
  14.  
  15. #include "WheelMouse.h"
  16.  
  17. #include "Scroll.h"
  18. #include "RawKey.h"
  19.  
  20. extern struct IntuitionBase *IntuitionBase;
  21.  
  22. struct Window *WindowUnderPointer();
  23.  
  24. void SendIDCMP(struct WheelMouseContext *wmc,int class,int code,int qualifier,struct IntuiMessage *im)
  25. {
  26.   struct Window *win=wmc->Window;
  27.   struct Gadget *gg=wmc->Gadget;
  28.   im->IDCMPWindow=win;
  29.   im->IAddress=gg;
  30.   im->Class=class;
  31.   im->Code=code;
  32.   im->Qualifier=qualifier;
  33.   im->MouseX=win->MouseX;
  34.   im->MouseY=win->MouseY;
  35.   im->ExecMessage.mn_ReplyPort=wmc->ReplyPort;
  36.   im->Seconds=IntuitionBase->Seconds; im->Micros=IntuitionBase->Micros;
  37.   im->SpecialLink=NULL;
  38.   PutMsg(win->UserPort,(struct Message *)im);
  39. }
  40.  
  41.  
  42. void NudgePropGadget(struct Gadget *gg,struct Window *win,int axis,int direction)
  43. {
  44.   struct PropInfo *pi=(struct PropInfo *)gg->SpecialInfo;
  45.  
  46.   long current,offset;
  47.  
  48.   if(axis==FREEVERT)
  49.   {
  50.     current=pi->VertPot;
  51.     offset=pi->VPotRes;
  52.     current+=(direction*offset)/4;
  53.     if(current<0) current=0;
  54.     if(current>0xffff) current=0xffff;
  55.     pi->VertPot=current;
  56.   }
  57.  
  58.   if(axis==FREEHORIZ)
  59.   {
  60.     current=pi->HorizPot;
  61.     offset=pi->HPotRes;
  62.     current+=(direction*offset)/4;
  63.     if(current<0) current=0;
  64.     if(current>0xffff) current=0xffff;
  65.     pi->HorizPot=current;
  66.   }
  67.  
  68. }
  69.  
  70.  
  71. struct Gadget *FindPropGadget(struct Window *win,long type)
  72. {
  73.   struct Gadget *gg=win->FirstGadget,*bestgadget=NULL;
  74.   int gx,gy,dd,distance=32767;
  75.   while(gg)
  76.   {
  77.     if((gg->GadgetType>YP_GTYPEMASK)==GTYP_PROPGADGET)
  78.     {
  79.       struct PropInfo *pi=(struct PropInfo *)gg->SpecialInfo;
  80.       if(pi)
  81.       {
  82.         if(pi->Flags&type)
  83.         {
  84.           if(type==FREEVERT)
  85.           {
  86.             gx=gg->LeftEdge+gg->Width;
  87.             if(gg->Flags&GFLG_RELRIGHT)
  88.               gx+=win->Width;
  89.             dd=gx-win->MouseX;
  90.             if(dd<0)
  91.               dd=-dd+win->Width; /* bias to left of scrollbar */
  92.             gy=(gg->TopEdge+gg->Height+32)-win->MouseY;
  93.             if((gg->TopEdge-win->MouseY)*(gg->TopEdge-win->MouseY)<(gy*gy))
  94.               gy=gg->TopEdge-win->MouseY;
  95.             if(gy<0)
  96.               gy=-gy;
  97.             dd+=gy;
  98.           }
  99.           if(type==FREEHORIZ)
  100.           {
  101.             gx=gg->TopEdge+gg->Height;
  102.             if(gg->Flags&GFLG_RELBOTTOM)
  103.               gx+=win->Height;
  104.             dd=gx-win->MouseY;
  105.             if(dd<0)
  106.               dd=-dd+win->Height; /* bias to top of scrollbar */
  107.             gy=(gg->LeftEdge+gg->Width+32)-win->MouseX;
  108.             if((gg->LeftEdge-win->MouseX)*(gg->TopEdge-win->MouseX)<(gy*gy))
  109.               gy=gg->TopEdge-win->MouseX;
  110.             if(gy<0)
  111.               gy=-gy;
  112.             dd+=gy;
  113.           }
  114.           if(dd<0) dd=-dd;
  115.           if(dd<distance)
  116.           {
  117.             distance=dd;
  118.             bestgadget=gg;
  119.           }
  120.         }
  121.       }
  122.     }
  123.     gg=gg->NextGadget;
  124.   }
  125.   return(bestgadget);
  126. }
  127.  
  128.  
  129. int DoScroll(struct WheelMouseContext *wmc,int axis,int direction)
  130. {
  131.   struct Window *win;
  132.   Forbid();
  133.   if(wmc->WindowMode==OverWindow)
  134.     win=wmc->Window=WindowUnderPointer();
  135.   else
  136.     win=wmc->Window=IntuitionBase->ActiveWindow;
  137.   if(!(win))
  138.     return(direction);
  139.   wmc->Gadget=FindPropGadget(win,axis);
  140.   if((!(wmc->Gadget))&&(wmc->WindowMode==OverWindow)&&((win->IDCMPFlags&IDCMP_RAWKEY)==0))
  141.   {
  142.     if(!(win=wmc->Window=IntuitionBase->ActiveWindow))
  143.       return(direction);
  144.     wmc->Gadget=FindPropGadget(win,axis);
  145.   }
  146.   if(wmc->Gadget)
  147.   {
  148.     NudgePropGadget(wmc->Gadget,win,axis,direction);
  149.     SendIDCMP(wmc,IDCMP_GADGETDOWN,0,0,&wmc->Msg1.eim_IntuiMessage);
  150.     SendIDCMP(wmc,IDCMP_GADGETUP,0,0,&wmc->Msg2.eim_IntuiMessage);
  151.     RefreshGList(wmc->Gadget,wmc->Window,NULL,1);
  152.     Permit();
  153.     WaitPort(wmc->ReplyPort);
  154.     GetMsg(wmc->ReplyPort);
  155.     WaitPort(wmc->ReplyPort);
  156.     GetMsg(wmc->ReplyPort);
  157.     return(direction);
  158.   }
  159.   else if(win->IDCMPFlags&IDCMP_RAWKEY)
  160.   {
  161.     int code,c,d,q=0;
  162.     d=direction;
  163.     if(d>0)
  164.       if(axis==FREEVERT)
  165.         code=RK_Down;
  166.       else
  167.         code=RK_Right;
  168.     else
  169.     {
  170.       if(axis==FREEVERT)
  171.         code=RK_Up;
  172.       else
  173.         code=RK_Left;
  174.       d=-d;
  175.     }
  176.     if(d>1)
  177.       q=IEQUALIFIER_LSHIFT;
  178.     SendIDCMP(wmc,IDCMP_RAWKEY,code,q,&wmc->Msg1.eim_IntuiMessage);
  179.     SendIDCMP(wmc,IDCMP_RAWKEY,code|IECODE_UP_PREFIX,q,&wmc->Msg2.eim_IntuiMessage);
  180.     Permit();
  181.     WaitPort(wmc->ReplyPort);
  182.     GetMsg(wmc->ReplyPort);
  183.     WaitPort(wmc->ReplyPort);
  184.     GetMsg(wmc->ReplyPort);
  185.     return(direction);
  186.     /*
  187.     if(direction<0)
  188.       return(-1);
  189.     else
  190.       return(1);
  191.     */
  192.   }
  193.   Permit();
  194.   return(direction);
  195. }
  196.  
  197.  
  198. struct Window *WindowUnderPointer()
  199. {
  200.   int x,y;
  201.   unsigned long lock;
  202.   struct Window *win=NULL;
  203.   struct Screen *scr;
  204.   struct Layer *layer;
  205.  
  206.   scr=IntuitionBase->FirstScreen;
  207.   do
  208.   {
  209.     x=scr->MouseX; y=scr->MouseY;
  210.     if((x<0) || (y<0))
  211.       scr=scr->NextScreen;
  212.   } while((scr!=NULL) && ((x<0)||(y<0)));
  213.  
  214.   if(!scr)
  215.     return(NULL);
  216.  
  217.   layer=WhichLayer(&scr->LayerInfo,x,y);
  218.   if(layer)
  219.     win=layer->Window;
  220.   return(win);
  221. }
  222.  
  223.