home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / fish / applications / xlispstat / src / src1.lzh / Amiga / amiiviewwindow2.c < prev    next >
C/C++ Source or Header  |  1990-10-11  |  16KB  |  439 lines

  1. /* AmiIviewWindow2.c - Low Level Window Objects for Amiga              */
  2. /* Copyright (c) 1990 by J.K. Lindsey                                  */
  3. /* Additions to XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney     */
  4. /* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
  5. /* You may give out copies of this software; for conditions see the    */
  6. /* file COPYING included with this distribution.                       */
  7.  
  8. #include <proto/exec.h>
  9. #include <proto/intuition.h>
  10. #include <proto/graphics.h>
  11. #include <graphics/gfxmacros.h>
  12. #include <stdlib.h>
  13. #include "xlisp.h"
  14. #include "osdef.h"
  15. #include "xlproto.h"
  16. #include "xlsproto.h"
  17. #include "iviewproto.h"
  18. #include "Stproto.h"
  19. #include "osproto.h"
  20. #include "amivar.h"
  21. #include "xlsvar.h"
  22.  
  23. static void remove_scroll(struct Window *w,struct Gadget *gad){
  24.    if(gad){
  25.       RemoveGadget(w,gad);
  26.       free(gad->GadgetRender);
  27.       free(gad->SpecialInfo);
  28.       free(gad);
  29.       RefreshWindowFrame(w);}}
  30.  
  31. /**************************************************************************/
  32. /**                                                                      **/
  33. /**                    Action Installation Functions                     **/
  34. /**                                                                      **/
  35. /**************************************************************************/
  36.  
  37. void StGWSetFreeMem(StGWWinInfo *gwinfo,void(*FreeMem)(IVIEW_WINDOW)){}
  38.  
  39. int StGWIdleOn(StGWWinInfo *gwinfo){
  40.    return(gwinfo?gwinfo->idleOn:0);}
  41.  
  42. void StGWSetIdleOn(StGWWinInfo *gwinfo,int on){
  43.    if(gwinfo)gwinfo->idleOn=on;}
  44.  
  45. /**************************************************************************/
  46. /**                                                                      **/
  47. /**                      Window Management Functions                     **/
  48. /**                                                                      **/
  49. /**************************************************************************/
  50.  
  51. void StGWShowWindow(StGWWinInfo *gwinfo){
  52.    struct Window *w;
  53.    if(gwinfo&&(w=gwinfo->window)){
  54.       if(!gwinfo->initialized)send_message_1L(gwinfo->Object,sk_update,s_true);
  55.       WindowToFront(w);}}
  56.  
  57. void StGWRemove(StGWWinInfo *gwinfo){
  58.    struct Window *w,*ww;
  59.    if(gwinfo&&(ww=gwinfo->window)){
  60.       w=screen->FirstWindow;
  61.       while(w&&w!=ww)w=w->NextWindow;
  62.       if(!w)return;
  63.       if(gwinfo->hasVscroll)remove_scroll(w,gwinfo->vscroll);
  64.       if(gwinfo->hasHscroll)remove_scroll(w,gwinfo->hscroll);
  65.       ClearMenuStrip(w);
  66.       CloseWindow(w);
  67.       gwinfo->window=0;}}
  68.  
  69. void StGWWhileButtonDown(StGWWinInfo *gwinfo,void(*action)(struct Window *,int,int),int motionOnly){
  70.    struct Window *w;
  71.    struct IntuiMessage *message;
  72.    unsigned short code;
  73.    unsigned long class;
  74.    int x,y;
  75.    if(!gwinfo||!(w=gwinfo->window))return;
  76.    for(;;){
  77.       if(message=(struct IntuiMessage *)GetMsg(w->UserPort)){
  78.          class=message->Class;
  79.          code=message->Code;
  80.          ReplyMsg((struct Message *)message);
  81.          if(class==MOUSEBUTTONS&&code==SELECTUP)break;}
  82.       x=w->MouseX;
  83.       y=w->MouseY;
  84.       if(x<=0||x>=w->Width||y<=0||y>=w->Height)break;
  85.       ami_do_cursor(gwinfo);
  86.       x-=w->BorderLeft;
  87.       y-=w->BorderTop;
  88.       if(gwinfo->mouse_x!=x||gwinfo->mouse_y!=y||!motionOnly){
  89.          gwinfo->mouse_x=x;
  90.          gwinfo->mouse_y=x;
  91.          scroll_clip(gwinfo,&x,&y);
  92.          if(action)(*action)(w,x,y);}}}
  93.  
  94. void StWSetLocation(IVIEW_WINDOW w,int left,int top,int frame){
  95.    if(w){
  96.       if(!frame){
  97.          left-=w->BorderLeft;
  98.          top-=w->BorderTop;}
  99.       if(left<0)left=0;;
  100.       if(top<0)top=0;
  101.       if(left+w->Width>screenw)left=screenw-w->Width;
  102.       if(top+w->Height>screenh)top=screenh-w->Height;
  103.       MoveWindow(w,(short)(left-w->LeftEdge),(short)(top-w->TopEdge));}}
  104.  
  105. void StWGetLocation(struct Window *w,int *left,int *top,int frame){
  106.    if(w){
  107.       if(frame){
  108.          if(left)*left=w->LeftEdge;
  109.          if(top)*top=w->TopEdge;}
  110.       else {
  111.          if(left)*left=w->LeftEdge+w->BorderLeft;
  112.          if(top)*top=w->TopEdge+w->BorderTop;}}}
  113.  
  114. void StGWSetSize(StGWWinInfo *gwinfo,int width,int height,int frame){
  115.    struct Window *w;
  116.    if(gwinfo&&(w=gwinfo->window))StWSetSize(w,width,height,frame);}
  117.  
  118. void StWSetSize(struct Window *w,int width,int height,int frame){
  119.    if(w){
  120.       if(!frame){
  121.          width+=w->BorderLeft+w->BorderRight;
  122.          height+=w->BorderTop+w->BorderBottom;}
  123.       SizeWindow(w,(short)(width-w->Width),(short)(height-w->Height));}}
  124.  
  125. void StWGetSize(struct Window *w,int *width,int *height,int frame){
  126.    if(w){
  127.       if(frame){
  128.          if(width)*width=w->Width;
  129.          if(height)*height=w->Height;}
  130.       else {
  131.          if(width)*width=w->Width-w->BorderLeft-w->BorderRight;
  132.          if(height)*height=w->Height-w->BorderTop-w->BorderBottom;}}}
  133.  
  134. /**************************************************************************/
  135. /**                                                                      **/
  136. /**             Window State Access and Mutation Functions               **/
  137. /**                                                                      **/
  138. /**************************************************************************/
  139.  
  140. void SetHardwareState(StGWWinInfo *gwinfo){
  141.    struct Window *w;
  142.    if(!gwinfo||(!(w=gwinfo->window)))return;
  143.    rp=(dbflag)?&dbrp:w->RPort;
  144.    SetAPen(&dbrp,gwinfo->drawColor);
  145.    SetAPen(w->RPort,gwinfo->drawColor);
  146.    SetBPen(&dbrp,gwinfo->backColor);
  147.    SetBPen(w->RPort,gwinfo->backColor);
  148.    if(gwinfo->drawMode==JAM1)gwinfo->symbolMode=JAM2;
  149.    else gwinfo->drawMode=gwinfo->symbolMode=COMPLEMENT;
  150.    SetDrMd(&dbrp,gwinfo->drawMode);
  151.    SetDrMd(w->RPort,gwinfo->drawMode);
  152.  
  153.    /* set line type */
  154.    if(gwinfo->lineType&&gwinfo->drawColor!=gwinfo->backColor){
  155.       SetDrPt(rp,0xAAAA);} /* requires brackets for macro ! */
  156.    else {
  157.       SetDrPt(rp,0xFFFF);}
  158.  
  159.    /* set pen size - not implemented in present Amiga ROM */
  160.    rp->PenWidth=rp->PenHeight=gwinfo->lineWidth;}
  161.  
  162. StGWCanvasWidth(StGWWinInfo *gwinfo){
  163.    return(gwinfo?gwinfo->canvasWidth:0);}
  164.  
  165. StGWCanvasHeight(StGWWinInfo *gwinfo){
  166.    return(gwinfo?gwinfo->canvasHeight:0);}
  167.  
  168. StGWLineType(StGWWinInfo *gwinfo){
  169.    return(gwinfo?gwinfo->lineType:0);}
  170.  
  171. StGWDrawMode(StGWWinInfo *gwinfo){
  172.    return(gwinfo?gwinfo->drawMode:0);}
  173.  
  174. ColorCode StGWDrawColor(StGWWinInfo *gwinfo){
  175.   return((ColorCode)(gwinfo?gwinfo->drawColor:0));}
  176.  
  177. ColorCode StGWBackColor(StGWWinInfo *gwinfo){
  178.   return((ColorCode)(gwinfo?gwinfo->backColor:0));}
  179.  
  180. StGWUseColor(StGWWinInfo *gwinfo){
  181.    return(gwinfo?gwinfo->use_color:0);}
  182.  
  183. void StGWGetLineWidth(StGWWinInfo *gwinfo,int *width){
  184.   if(!gwinfo)return;
  185.   if(width)*width=gwinfo->lineWidth;}
  186.  
  187. static void set_state(StGWWinInfo *gwinfo,int which,int value){
  188.    int changed;
  189.    if(!gwinfo)return;
  190.    switch (which) {
  191.       case 'L': if((changed=(value!=gwinfo->lineType)))gwinfo->lineType=value;  break;
  192.       case 'M': if((changed=(value!=gwinfo->drawMode)))gwinfo->drawMode=value;  break;
  193.       case 'D': if((changed=(value!=gwinfo->drawColor)))gwinfo->drawColor=value; break;
  194.       case 'B': if((changed=(value!=gwinfo->backColor)))gwinfo->backColor=value; break;
  195.       case 'C': if((changed=(value!=gwinfo->use_color)))gwinfo->use_color =value;break;}
  196.    if(changed)SetHardwareState(gwinfo);}
  197.  
  198. void StGWSetLineType(StGWWinInfo *gwinfo,int type){
  199.   set_state(gwinfo,'L',type);}
  200.  
  201. void StGWSetDrawMode(StGWWinInfo *gwinfo,int mode){
  202.   set_state(gwinfo,'M',mode);}
  203.  
  204. void StGWSetDrawColor(StGWWinInfo *gwinfo,ColorCode color){
  205.   set_state(gwinfo,'D',color);}
  206.  
  207. void StGWSetBackColor(StGWWinInfo *gwinfo,ColorCode color){
  208.   set_state(gwinfo,'B',color);}
  209.  
  210. void StGWSetUseColor(StGWWinInfo *gwinfo,ColorCode use){
  211.   set_state(gwinfo,'C',use);}
  212.  
  213. void StGWSetLineWidth(StGWWinInfo *gwinfo,int width){
  214.    int changed;
  215.    if(!gwinfo)return;
  216.    changed=(width!=gwinfo->lineWidth);
  217.    if(changed){
  218.       gwinfo->lineWidth=width;
  219.       SetHardwareState(gwinfo);}}
  220.  
  221. void StGWReverseColors(StGWWinInfo *gwinfo){
  222.    ColorCode backColor,drawColor;
  223.    backColor=StGWBackColor(gwinfo);
  224.    drawColor=StGWDrawColor(gwinfo);
  225.    if(backColor!=drawColor){
  226.       StGWSetBackColor(gwinfo,drawColor);
  227.       StGWSetDrawColor(gwinfo,backColor);
  228.       StGWObRedraw(gwinfo->Object);}}
  229.  
  230. void StGWGetViewRect(StGWWinInfo *gwinfo,int *left,int *top,int *width,int *height){
  231.    struct Window *w;
  232.    if(gwinfo&&(w=gwinfo->window)){
  233.       if(left)*left=0;
  234.       if(top)*top=0;
  235.       StWGetSize(w,width,height,0);
  236.       if(width&&gwinfo->hasVscroll)*width-=8;}}
  237.  
  238. /**************************************************************************/
  239. /**                                                                      **/
  240. /**                       Window Scrolling Functions                     **/
  241. /**                                                                      **/
  242. /**************************************************************************/
  243.  
  244. static void set_has_scroll(StGWWinInfo *gwinfo,char which,int has,int size){
  245.    struct Window *w;
  246.    int view_size,old_has;
  247.    short s;
  248.    struct Gadget *gad;
  249.    struct PropInfo *pinfo;
  250.    if(!gwinfo||!(w=gwinfo->window))return;
  251.    if(has&&size<=0)StPerror("size must be positive");
  252.    old_has=(which=='H')?gwinfo->hasHscroll:gwinfo->hasVscroll;
  253.    if(which=='H'){
  254.       gwinfo->hasHscroll=has;
  255.       if(has){
  256.          gwinfo->canvasWidth=size;
  257.          if(old_has){
  258.             StGWGetViewRect(gwinfo,0,0,&view_size,0);
  259.             gwinfo->view_h=0;
  260.             s=(size-view_size>0)?(MAXBODY*view_size)/size:MAXBODY;
  261.             NewModifyProp(gwinfo->hscroll,w,0,AUTOKNOB|FREEHORIZ,0,MAXPOT,s,MAXBODY,1);
  262.             return;}}}
  263.    else {
  264.       gwinfo->hasVscroll=has;
  265.       if(has){
  266.          gwinfo->canvasHeight=size;
  267.          if(old_has){
  268.             StGWGetViewRect(gwinfo,0,0,0,&view_size);
  269.             gwinfo->view_v=0;
  270.             s=(size-view_size>0)?(MAXBODY*view_size)/size:MAXBODY;
  271.             NewModifyProp(gwinfo->vscroll,w,0,AUTOKNOB|FREEVERT,MAXPOT,0,MAXBODY,s,1);
  272.             return;}}}
  273.    if(has){
  274.       if(!(gad=calloc(1,sizeof(struct Gadget))))xlfail("scroll bar not allocated");
  275.       gad->Flags=GADGIMAGE;
  276.       gad->Activation=RELVERIFY;
  277.       gad->GadgetType=PROPGADGET;
  278.       if(!(gad->GadgetRender=malloc(sizeof(struct Image))))xlfail("slider image allocation failed");
  279.       if(!(pinfo=(struct PropInfo *)gad->SpecialInfo=malloc(sizeof(struct PropInfo))))
  280.       xlfail("slider SpecialInfo allocation failed");
  281.       pinfo->Flags=AUTOKNOB;
  282.       if(which=='H'){
  283.          gad->LeftEdge=1;
  284.          gad->TopEdge=-8;
  285.          gad->Width=-14;
  286.          gad->Height=8;
  287.          gad->Flags|=GRELBOTTOM|GRELWIDTH;
  288.          gad->Activation|=BOTTOMBORDER;
  289.          gad->GadgetID=0;
  290.          gwinfo->hscroll=gad;
  291.          StGWGetViewRect(gwinfo,0,0,&view_size,0);
  292.          pinfo->Flags|=FREEHORIZ;
  293.          gwinfo->view_h=pinfo->HorizPot=0;
  294.          pinfo->HorizBody=(size-view_size>0)?(MAXBODY*view_size)/size:MAXBODY;
  295.          pinfo->VertPot=MAXPOT;
  296.          pinfo->VertBody=MAXBODY;}
  297.       else {
  298.          gad->LeftEdge=-11;
  299.          gad->TopEdge=10;
  300.          gad->Width=11;
  301.          gad->Height=-19;
  302.          gad->GadgetID=1;
  303.          gad->Flags|=GRELRIGHT|GRELHEIGHT;
  304.          gad->Activation|=RIGHTBORDER;
  305.          gwinfo->vscroll=gad;
  306.          StGWGetViewRect(gwinfo,0,0,0,&view_size);
  307.          pinfo->Flags|=FREEVERT;
  308.          gwinfo->view_v=pinfo->VertPot=0;
  309.          pinfo->VertBody=(size-view_size>0)?(MAXBODY*view_size)/size:MAXBODY;
  310.          pinfo->HorizPot=MAXPOT;
  311.          pinfo->HorizBody=MAXBODY;}
  312.       AddGList(w,gad,-1,1,0);
  313.       RefreshGList(gad,w,0,1);}
  314.    else {
  315.       if(which=='H'){
  316.          StGWGetViewRect(gwinfo,0,0,&gwinfo->canvasWidth,0);
  317.          if(old_has){
  318.             remove_scroll(w,gwinfo->hscroll);
  319.             gwinfo->hscroll=0;}
  320.          gwinfo->view_h=0;}
  321.       else {
  322.          StGWGetViewRect(gwinfo,0,0,0,&gwinfo->canvasHeight);
  323.          if(old_has){
  324.             remove_scroll(w,gwinfo->vscroll);
  325.             gwinfo->vscroll=0;}
  326.          if(gwinfo->hasHscroll){
  327.             StGWGetViewRect(gwinfo,0,0,&view_size,0);
  328.             NewModifyProp(gwinfo->hscroll,w,0,GADGIMAGE|GRELBOTTOM|GRELWIDTH,0,MAXPOT,
  329.             (gwinfo->canvasWidth-view_size)?(MAXBODY*view_size)/gwinfo->canvasWidth:MAXBODY,
  330.             MAXBODY,1);}
  331.          gwinfo->view_v=0;}}
  332.    StGWObResize(gwinfo->Object);}
  333.  
  334. void StGWSetHasHscroll(StGWWinInfo *gwinfo,int has,int size){
  335.    set_has_scroll(gwinfo,'H',has,size);}
  336.  
  337. void StGWSetHasVscroll(StGWWinInfo *gwinfo,int has,int size){
  338.    set_has_scroll(gwinfo,'V',has,size);}
  339.  
  340. int StGWHasHscroll(StGWWinInfo *gwinfo){
  341.    return(gwinfo?gwinfo->hasHscroll:0);}
  342.  
  343. int StGWHasVscroll(StGWWinInfo *gwinfo){
  344.    return(gwinfo?gwinfo->hasVscroll:0);}
  345.  
  346. void StGWSetScroll(StGWWinInfo *gwinfo,int h,int v,int move){
  347.    if(!gwinfo||!gwinfo->window)return;
  348.    if(gwinfo->hasHscroll)gwinfo->view_h=h;
  349.    if(gwinfo->hasVscroll)gwinfo->view_v=v;}
  350.  
  351. void StGWGetScroll(StGWWinInfo *gwinfo,int *h,int *v){
  352.    if(gwinfo){
  353.       if(h)*h=gwinfo->view_h;
  354.       if(v)*v=gwinfo->view_v;}}
  355.  
  356. void StGWSetHscrollIncs(StGWWinInfo *gwinfo,int inc,int pageInc){
  357.    if(!gwinfo)return;
  358.    gwinfo->h_scroll_inc[0]=inc;
  359.    gwinfo->h_scroll_inc[1]=pageInc;}
  360.  
  361. void StGWGetHscrollIncs(StGWWinInfo *gwinfo,int *inc,int *pageInc){
  362.    if(!gwinfo)return;
  363.    if(inc)*inc=gwinfo->h_scroll_inc[0];
  364.    if(pageInc)*pageInc=gwinfo->h_scroll_inc[1];}
  365.  
  366. void StGWSetVscrollIncs(StGWWinInfo *gwinfo,int inc,int pageInc){
  367.    if(!gwinfo)return;
  368.    gwinfo->v_scroll_inc[0]=inc;
  369.    gwinfo->v_scroll_inc[1]=pageInc;}
  370.  
  371. void StGWGetVscrollIncs(StGWWinInfo *gwinfo,int *inc,int *pageInc){
  372.    if(!gwinfo)return;
  373.    if(inc)*inc=gwinfo->v_scroll_inc[0];
  374.    if(pageInc)*pageInc=gwinfo->v_scroll_inc[1];}
  375.  
  376. /**************************************************************************/
  377. /**                                                                      **/
  378. /**                    Graph Window RefCon Functions                     **/
  379. /**                                                                      **/
  380. /**************************************************************************/
  381.  
  382. void StGWSetRefCon(StGWWinInfo *gwinfo,IView x){
  383.    if(gwinfo)gwinfo->RefCon=x;}
  384.  
  385. IView StGWGetRefCon(StGWWinInfo *gwinfo){
  386.    return(gwinfo?gwinfo->RefCon:0);}
  387.  
  388. void StGWSetObject(StGWWinInfo *gwinfo,LVAL x){
  389.    if(gwinfo)gwinfo->Object=x;}
  390.  
  391. LVAL IViewWindowGetObject(struct Window *w){
  392.    StGWWinInfo *gwinfo=(StGWWinInfo *)w->UserData;
  393.    return(gwinfo?gwinfo->Object:0);}
  394.  
  395. void StGWInitialDraw(StGWWinInfo *gwinfo){
  396.    if(gwinfo)graph_update_action(gwinfo,1);}
  397.  
  398. /**************************************************************************/
  399. /**                                                                      **/
  400. /**                    Graph Window Color Functions                      **/
  401. /**                                                                      **/
  402. /**************************************************************************/
  403.  
  404. #define NumBasicColors 8
  405. #define NumRGBColors   8
  406.  
  407. static int NumColors;
  408.  
  409. typedef struct {
  410.    long old;
  411.    LVAL refcon;
  412. }  ctab_entry;
  413.  
  414. static ctab_entry *ctable;
  415.  
  416. void init_ami_colors(void){
  417.    int i;
  418.    NumColors=NumBasicColors+NumRGBColors;
  419.    ctable=(ctab_entry *)StCalloc(NumColors,sizeof(ctab_entry));
  420.    for(i=0;i<NumColors;i++)ctable[i].old=i;}
  421.  
  422. ColorCode StGWMakeColor(double red,double green,double blue,LVAL refcon){
  423.    ColorCode index;
  424.    for(index=NumBasicColors;index<NumColors&&StGWGetColRefCon(index);index++);
  425.    if(index>=NumColors)return(-1);
  426.    else {
  427.       StGWSetColRefCon(index,refcon);
  428.       SetRGB4(&screen->ViewPort,index,(char)(15*red),(char)(15*green),(char)(15*blue));
  429.       return(index);}}
  430.  
  431. void StGWFreeColor(ColorCode index){
  432.    if(index<NumColors)StGWSetColRefCon(index,0);}
  433.  
  434. void StGWSetColRefCon(ColorCode index,LVAL rc){
  435.    if(index<NumColors)ctable[index].refcon=rc;}
  436.  
  437. LVAL StGWGetColRefCon(ColorCode index){
  438.    return((index<NumColors)?ctable[index].refcon:0);}
  439.