home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / spreadsheets / a180_1 / !SSS / c / SSS < prev   
Text File  |  1991-07-30  |  15KB  |  537 lines

  1. /* SSS IconBar handler
  2.    Tue,30 Jul 1991
  3.    Copyright C.T.Stretch 1991
  4. */
  5.  
  6. #include "ssshdr.h"
  7. #include "drhdr.h"
  8. #include "akbd.h"
  9.  
  10. #define HOMEKEY 30
  11. #define PROGNAME "SSS"
  12. #define NONULL wimp_EMNULL
  13. #define PRWIDTH 2
  14. #define PRSEP 4
  15. #define PRREP 6
  16. #define PRMONO 9
  17. #define PRA4P 11
  18. #define PRREAD 14
  19.  
  20. static wimp_w bwind;
  21. static menu barmenu,wmenu;
  22. static dbox edbox;
  23. static int dx=0,dy=1;
  24.  
  25. static void setext()
  26. { int i,w=0;
  27.   wimp_redrawstr r;
  28.   wimp_wstate ws;
  29.   wimp_close_wind(swind);
  30.   r.w=swind;
  31.   r.box.x0=0;
  32.   for(i=0;i<ncols;i++) w+=width[i];
  33.   dispwidth=w*CHWIDTH;
  34.   r.box.x1=dispwidth;
  35.   r.box.y1=0;
  36.   r.box.y0=-LINEHT*nrows;
  37.   wimpt_noerr(wimp_set_extent(&r));
  38.   wimp_get_wind_state(swind,&ws);
  39.   wimpt_noerr(wimp_open_wind(&(ws.o)));
  40. }
  41.  
  42. static void fred()
  43. { wimp_wstate ws;
  44.   wimp_get_wind_state(swind,&ws);
  45.   ws.o.box.x0=0;
  46.   ws.o.box.y1=0;
  47.   ws.o.box.x1=dispwidth;
  48.   ws.o.box.y0=-LINEHT*nrows;
  49.   wimp_force_redraw((wimp_redrawstr*)&(ws.o));
  50. }
  51.  
  52. static void showentry()
  53. { entry **cb=sheet+sx0+sy0*NCOLS;
  54.   char tstr[64];
  55.   int t;
  56.   wimp_caretstr cs;
  57.   for(t=ICSTRING;t<ICRIGHT;t++) dbox_setnumeric(edbox,t,0);
  58.   if(*cb)
  59.   { t=(*cb)->t;
  60.     dbox_setnumeric(edbox,ICSTRING+t,1);
  61.     dbox_setfield(edbox,ICWRITE,&((*cb)->c));
  62.   }
  63.   else
  64.   { t=FINT;
  65.     if(sy0>0)
  66.     { cb=sheet+sx0+(sy0-1)*NCOLS;
  67.       if(*cb) t=(*cb)->t;
  68.     }
  69.     dbox_setnumeric(edbox,ICSTRING+t,1);
  70.     dbox_setfield(edbox,ICWRITE,"");
  71.   }
  72.   ex0=sx0;ex1=sx1;ey0=sy0;ey1=sy1;
  73.   if(sx0==sx1&&sy0==sy1) sprintf(tstr,"Edit [%d,%d]",sx0,sy0);
  74.   else sprintf(tstr,"Edit [%d,%d][%d,%d]",sx0,sy0,sx1,sy1);
  75.   sheet_ebox();
  76.   win_settitle(bwind,tstr);
  77.   dbox_showstatic(edbox);
  78.   cs.w=bwind;
  79.   cs.i=ICWRITE;
  80.   cs.x=-1;cs.y=-1;
  81.   cs.height=-1;
  82.   cs.index=0;
  83.   wimpt_noerr(wimp_set_caret_pos(&cs));
  84. }
  85.  
  86. static void showprev()
  87. { entry **cb=sheet+sx0+sy0*NCOLS;
  88.   int t;
  89.   for(t=ICSTRING;t<ICRIGHT;t++) dbox_setnumeric(edbox,t,0);
  90.   if(*cb)
  91.   { t=(*cb)->t;
  92.     dbox_setnumeric(edbox,ICSTRING+t,1);
  93.     dbox_setfield(edbox,ICWRITE,&((*cb)->c));
  94.   }
  95.   else
  96.   { dbox_setnumeric(edbox,ICSTRING+FINT,1);
  97.     dbox_setfield(edbox,ICWRITE,"");
  98.   }
  99.   sx0=ex0;sx1=ex1;sy0=ey0;sy1=ey1;
  100.   sheet_box();
  101. }
  102.  
  103. static void mouse(wimp_mousestr *m)
  104. { int bx,by,x;
  105.   wimp_wstate state;
  106.   wimpt_noerr(wimp_get_wind_state(swind,&state));
  107.   bx=state.o.box.x0-state.o.x;
  108.   by=state.o.box.y1-state.o.y;
  109.   switch(m->bbits)
  110.   { case wimp_BLEFT:x=(m->x-bx+8)/CHWIDTH;
  111.                     for(sx0=0;((x-=width[sx0])>0)&&(sx0<ncols-1);sx0++);
  112.                     sy0=(by-m->y)/LINEHT;
  113.                     if(sy0>=nrows) sy0=nrows-1;
  114.                     sx1=sx0;sy1=sy0;
  115.                     sheet_box();
  116.                     break;
  117.     case wimp_BRIGHT:x=(m->x-bx+8)/CHWIDTH;
  118.                     for(sx1=0;((x-=width[sx1])>0)&&(sx1<ncols-1);sx1++);
  119.                     sy1=(by-m->y)/LINEHT;
  120.                     if(sy1>=nrows)sy1=nrows-1;
  121.                     if(sx0>sx1) {x=sx0;sx0=sx1;sx1=x;}
  122.                     if(sy0>sy1) {x=sy0;sy0=sy1;sy1=x;}
  123.                     sheet_box();
  124.                     break;
  125.   }
  126. }
  127.  
  128. static void openedit()
  129. { dbox_hide(edbox);
  130.   showentry();
  131. }
  132.  
  133. static void barclick(wimp_i i)
  134. { wimp_wstate ws;
  135.   i=i;
  136.   setext();
  137.   wimp_get_wind_state(swind,&ws);
  138.   ws.o.behind=-1;
  139.   wimpt_noerr(wimp_open_wind(&(ws.o)));
  140.   openedit();
  141. }
  142.  
  143. static void display(wimp_redrawstr *r)
  144. { int bx,by,more,tl,bl,l,i,x1,x2;
  145.   entry *ce;
  146.   char num[256];
  147.   wimpt_noerr(wimp_redraw_wind(r,&more));
  148.   bx=r->box.x0-r->scx;
  149.   by=r->box.y1-r->scy;
  150.   while(more)
  151.   { wimp_setcolour(7);
  152.     tl=(by-r->g.y1)/LINEHT;
  153.     if(tl<0) tl=0;
  154.     bl=(by-r->g.y0)/LINEHT;
  155.     if(bl>=nrows) bl=nrows-1;
  156.     for(l=tl;l<=bl;l++)
  157.     { bbc_move(r->g.x0,by-LINEHT*l);
  158.       bbc_draw(r->g.x1,by-LINEHT*l);
  159.     }
  160.     x1=bx;
  161.     for(i=0;i<ncols;i++) if(width[i])
  162.     { x2=x1+width[i]*CHWIDTH;
  163.       if(x1>r->g.x1) break;
  164.       if(x2>=r->g.x0)
  165.       { bbc_move(x2,r->g.y0);
  166.         bbc_draw(x2,r->g.y1);
  167.       }
  168.       if(x2>r->g.x0) for(l=tl;l<=bl;l++)
  169.       { ce=sheet[i+l*NCOLS];
  170.         if(ce)
  171.         { bbc_move(x1+XOFF,by-LINEHT*l-YOFF);
  172.           if(ce->p) printf("%.*s",width[i]-1,"P???????");
  173.           else
  174.           { if(ce->a) printf("%.*s",width[i]-1,"A!!!!!!!");
  175.             else switch(ce->t)
  176.             { case FSTRING:
  177.               case FLONG:printf("%.*s",width[i]-1,&(ce->c));break;
  178.               case FINT:sprintf(num,"%.0f",ce->v);
  179.                         printf("%.*s",width[i]-1,num);break;
  180.               case F2DP:sprintf(num,"%.2f",ce->v);
  181.                         printf("%.*s",width[i]-1,num);break;
  182.               case F4DP:sprintf(num,"%.4f",ce->v);
  183.                         printf("%.*s",width[i]-1,num);break;
  184.               case FEXP:sprintf(num,"%.4e",ce->v);
  185.                         printf("%.*s",width[i]-1,num);break;
  186.             }
  187.           }
  188.         }
  189.       }
  190.       x1=x2;
  191.     }
  192.     wimp_setcolour(15+16*3);
  193.     bbc_rectanglefill(bx+bx0,by-by1,bw,bh);
  194.     bbc_rectanglefill(bx+bx0+8,by-by1+8,bw-16,bh-16);
  195.     wimp_setcolour(12+16*3);
  196.     bbc_rectanglefill(bx+ebx0,by-eby1,ebw,ebh);
  197.     wimp_get_rectangle(r,&more);
  198.   }
  199. }
  200.  
  201. static void update()
  202. { if(uy>=nrows)
  203.   { uy=0;
  204.     if(changed) fred();
  205.     if(!autox||!changed)
  206.     { event_setmask(NONULL);
  207.       return;
  208.     }
  209.     changed=FALSE;
  210.   }
  211.   sheet_update();
  212.   uy++;
  213. }
  214.  
  215. static void handler(wimp_eventstr *e,void *v)
  216. { v=v;
  217.   switch(e->e)
  218.   { case wimp_ENULL  :update();break;
  219.     case wimp_EREDRAW:display((wimp_redrawstr*)(&(e->data.o)));break;
  220.     case wimp_EOPEN  :wimpt_noerr(wimp_open_wind(&(e->data.o)));break;
  221.     case wimp_ECLOSE :wimpt_noerr(wimp_close_wind(e->data.o.w));break;
  222.     case wimp_EBUT   :mouse(&(e->data.but.m));break;
  223.     case wimp_ESEND:case wimp_ESENDWANTACK:
  224.               switch (e->data.msg.hdr.action)
  225.               { case wimp_MDATALOAD:io_merge();setext();break;
  226.               }
  227.               break;
  228.   }
  229. }
  230.  
  231. static void getdirn()
  232. { if(dbox_getnumeric(edbox,ICRIGHT))
  233.   { dx=1;dy=0;return;
  234.   }
  235.   dx=0;dy=1;
  236. }
  237.  
  238. static void getnext()
  239. { getdirn();
  240.   if(ex0==ex1)
  241.   { sx0=ex0+dx;
  242.     if(sx0>=ncols) sx0=ncols-1;
  243.     sx1=sx0;
  244.   }
  245.   else
  246.   { sx0=ex0;sx1=ex1;
  247.   }
  248.   if(ey0==ey1)
  249.   { sy0=ey0+dy;
  250.     if(sy0>=nrows) sy0=nrows-1;
  251.     sy1=sy0;
  252.   }
  253.   else
  254.   { sy0=ey0;sy1=ey1;
  255.   }
  256.   sheet_box();
  257.   showentry();
  258. }
  259.  
  260. static void insrange(BOOL rel)
  261. { wimp_caretstr cs;
  262.   int n;
  263.   char buf2[BUFLEN];
  264.   wimp_get_caret_pos(&cs);
  265.   if(cs.w!=bwind) werr(0,"window %d",cs.w);
  266.   n=cs.index;
  267.   dbox_getfield(edbox,ICWRITE,buf,BUFLEN);
  268.   if(strlen(buf)+27>=BUFLEN) return;
  269.   if(sx0==sx1&&sy0==sy1)
  270.   { if(rel) sprintf(buf2,"%.*s[x%+d,y%+d]%s",n,buf,sx0-ex0,sy0-ey0,buf+n);
  271.     else
  272.         sprintf(buf2,"%.*s[%d,%d]%s",n,buf,sx0,sy0,buf+n);
  273.   }
  274.   else
  275.   { if(rel)
  276.     { sprintf(buf2,"%.*s[x%+d,y%+d][x%+d,y%+d]%s",
  277.             n,buf,sx0-ex0,sy0-ey0,sx1-ex0,sy1-ey0,buf+n);
  278.     }
  279.     else
  280.         sprintf(buf2,"%.*s[%d,%d][%d,%d]%s",
  281.         n,buf,sx0,sy0,sx1,sy1,buf+n);
  282.   }
  283.   dbox_setfield(edbox,ICWRITE,buf2);
  284.   cs.index+=strlen(buf2)-strlen(buf);
  285.   wimpt_noerr(wimp_set_caret_pos(&cs));
  286. }
  287.  
  288. static void edhandler(dbox d,void *h)
  289. { dbox_field f=dbox_get(d);
  290.   int x,y,t;
  291.   entry *cb;
  292.   h=h;
  293.   switch (f)
  294.   { case dbox_CLOSE:dbox_hide(d);break;
  295.     case ICOK:dbox_getfield(d,ICWRITE,buf,256);
  296.            for(t=0;t<FEXP;t++) if(dbox_getnumeric(edbox,ICSTRING+t)) break;
  297.            for(x=ex0;x<=ex1;x++) if(width[x])
  298.              for(y=ey0;y<=ey1;y++) sheet_getentry(x,y,t);
  299.            fred();
  300.            getnext();
  301.            if(autox) sheet_change();
  302.            break;
  303.     case ICEDIT:showentry();break;
  304.     case ICFORMAT:
  305.            for(t=0;t<FEXP;t++) if(dbox_getnumeric(edbox,ICSTRING+t)) break;
  306.            if(t<FINT) break;
  307.            for(x=ex0;x<=ex1;x++) for(y=ey0;y<=ey1;y++)
  308.            { cb=sheet[x+y*NCOLS];
  309.              if(cb&&cb->t>=FINT) cb->t=t;
  310.            }
  311.            fred();break;
  312.     case ICNEXT:getnext();break;
  313.     case ICCANCEL:showprev();break;
  314.     case ICABS:insrange(0);break;
  315.     case ICREL:insrange(1);break;
  316.     default:break;
  317.   }
  318. }
  319.  
  320. static BOOL edkeys(dbox d,void *e,void *h)
  321. { wimp_wstate state;
  322.   h=h;d=d;
  323.   if(((wimp_eventstr*)e)->e!=wimp_EKEY) return FALSE;
  324.   switch(((wimp_eventstr*)e)->data.key.chcode)
  325.   { case HOMEKEY:if(akbd_pollsh())
  326.                  { sx0=sx1=ncols-1;sy0=sy1=nrows-1;
  327.                    wimp_get_wind_state(swind,&state);
  328.                    state.o.x=dispwidth-state.o.box.x0+state.o.box.x1;
  329.                    state.o.y=-nrows*LINEHT-state.o.box.y0+state.o.box.y1;
  330.                  }
  331.                  else
  332.                  { sx0=0;sx1=0;sy0=0;sy1=0;
  333.                    wimp_get_wind_state(swind,&state);
  334.                    state.o.x=0;
  335.                    state.o.y=0;
  336.                  }
  337.                    wimp_open_wind(&(state.o));
  338.                  break;
  339.     case akbd_Fn+8:sx0=0;sx1=ncols-1;break;
  340.     case akbd_Fn+9:sy0=0;sy1=nrows-1;break;
  341.     case akbd_PrintK:io_pr("printer:",0);break;
  342.     default:return FALSE;
  343.   }
  344.   sheet_box();
  345.   return TRUE;
  346. }
  347.  
  348. static BOOL unknown(wimp_eventstr *e,void *h)
  349. { h=h;
  350.   switch(e->e)
  351.   { case wimp_ESEND:case wimp_ESENDWANTACK:
  352.               switch (e->data.msg.hdr.action)
  353.               { case wimp_MDATAOPEN:if(!io_read(0)) return FALSE;
  354.                                     win_settitle(swind,fname);
  355.                                     sheet_home();
  356.                                     barclick(0);
  357.                                     return TRUE;
  358.                 case wimp_MDATALOAD:if(!io_read(1)) return FALSE;
  359.                                     win_settitle(swind,fname);
  360.                                     sheet_home();
  361.                                     barclick(0);
  362.                                     return TRUE;
  363.                 case wimp_MPREQUIT:return TRUE;
  364.               }
  365.               break;
  366.   }
  367.   return FALSE;
  368. }
  369.  
  370. static void resize(char c)
  371. { dbox d;
  372.   int n,x;
  373.   switch(c)
  374.   { case 1:sheet_newcol();break;
  375.     case 2:sheet_newrow();break;
  376.     case 3:d=dbox_new("size");
  377.            if(d)
  378.            { dbox_setnumeric(d,2,ncols);
  379.              dbox_setnumeric(d,3,nrows);
  380.              dbox_show(d);
  381.              if(dbox_fillin(d)==0)
  382.              { n=dbox_getnumeric(d,2);
  383.                if(n>ncols) ncols=n;
  384.                if(ncols>NCOLS) ncols=NCOLS;
  385.                n=dbox_getnumeric(d,3);
  386.                if(n>nrows) if(flex_extend((flex_ptr)&sheet,4*n*NCOLS))
  387.                { for(x=nrows*NCOLS;x<n*NCOLS;x++) sheet[x]=0;
  388.                  nrows=n;
  389.                }
  390.              }
  391.              dbox_dispose(&d);
  392.            }
  393.            break;
  394.     case 4:sheet_delcol();break;
  395.     case 5:sheet_delrow();break;
  396.   }
  397.   sheet_home();setext();
  398. }
  399.  
  400. static void wmenuproc(void *wh,char *h)
  401. #define WMENU  "Edit,>Width,Auto,Update,Save,Resize,Copy,Sort,Graph"
  402. #define SMENU  ">Sheet,>Part,>Text"
  403. #define RMENU  "New Col,New Row,>Extend,Del Cols,Del Rows"
  404. #define GMENU  ">Bars,>Line,>Scatter"
  405. { dbox d;
  406.   int n,i;
  407.   wh=wh;
  408.   switch(h[0])
  409.   { case 1:openedit();break;
  410.     case 2:d=dbox_new("width");
  411.            if(d)
  412.            { dbox_setnumeric(d,1,width[sx0]);
  413.              dbox_show(d);
  414.              if(dbox_fillin(d)==0)
  415.              { n=dbox_getnumeric(d,1);
  416.                for(i=sx0;i<=sx1;i++) width[i]=n;
  417.                sheet_box();sheet_ebox();
  418.                setext();
  419.              }
  420.              dbox_dispose(&d);
  421.            }
  422.            break;
  423.     case 3:autox=!autox;menu_setflags(wmenu,3,autox,0);if(!autox) break;
  424.     case 4:sheet_change();break;
  425.     case 5:switch(h[1])
  426.            { case 1:saveas(SSSTYPE,fname,0,io_save,0,0,0);
  427.              win_settitle(swind,fname);
  428.              break;
  429.              case 2:saveas(SSSTYPE,"Part",0,io_save,0,0,(void*)1);
  430.              break;
  431.              case 3:if(h[2]) saveas(0xFFF,"SheetText",0,io_pr,0,0,0);
  432.                     else io_pr("Printer:",0);
  433.              break;
  434.            }
  435.            break;
  436.     case 6:resize(h[1]);break;
  437.     case 7:sheet_bcopy();fred();break;
  438.     case 8:sheet_sort();fred();break;
  439.     case 9:switch(h[1])
  440.            { case 1:saveas(0xAFF,"Bars",0,graph_bars,0,0,0);
  441.              break;
  442.              case 2:saveas(0xAFF,"Line",0,graph_line,0,0,0);
  443.              break;
  444.              case 3:saveas(0xAFF,"Scatter",0,graph_line,0,0,(void*)1);
  445.              break;
  446.            }
  447.            break;
  448.   }
  449. }
  450.  
  451. static void barmenuproc(void *wh,char *h)
  452. { dbox d;
  453.   int n;
  454.   wh=wh;
  455.   switch (h[0])
  456.   { case 1:d=dbox_new("Info");
  457.            if(d)
  458.            { dbox_setfield(d,4,"0.1 "__DATE__);
  459.              dbox_show(d);dbox_fillin(d);
  460.              dbox_dispose(&d);
  461.            }
  462.            break;
  463.     case 2:d=dbox_new("prset");
  464.            if(d)
  465.            { dbox_setnumeric(d,PRWIDTH,prwidth);
  466.              dbox_setfield(d,PRSEP,sep);
  467.              dbox_setnumeric(d,PRREP,repfirst);
  468.              dbox_setnumeric(d,PRMONO,mono);
  469.              for(n=PRA4P;n<=PRREAD;n++) 
  470.                         dbox_setnumeric(d,n,dr_page==n-PRA4P+1);
  471.              dbox_show(d);
  472.              if(dbox_fillin(d)==0)
  473.              { prwidth=dbox_getnumeric(d,PRWIDTH);
  474.                dbox_getfield(d,PRSEP,sep,4);
  475.                repfirst=dbox_getnumeric(d,PRREP);
  476.                mono=dbox_getnumeric(d,PRMONO);
  477.                for(n=PRA4P;n<=PRREAD;n++) if(dbox_getnumeric(d,n))
  478.                dr_page=n-PRA4P+1;
  479.              }
  480.              dbox_dispose(&d);
  481.            }
  482.            break;
  483.     case 3:exit(0);
  484.   }
  485. }
  486.  
  487. static BOOL init(void)
  488. { menu tempmenu;
  489.   wimpt_init(PROGNAME);
  490.   res_init(PROGNAME);
  491.   template_init();
  492.   dbox_init();
  493.   flex_init();
  494.   barmenu=menu_new(PROGNAME,">Info,>PrSetup,Quit");
  495.   if(!barmenu) return FALSE;
  496.   baricon("!"PROGNAME,1,barclick);
  497.   if(!event_attachmenu(win_ICONBAR,barmenu,barmenuproc,0)) return FALSE;
  498.   wimpt_noerr(wimp_create_wind(template_syshandle("sheet"),&swind));
  499.   win_register_event_handler(swind,handler,0);
  500.   edbox=dbox_new("edit");
  501.   if(!edbox) return FALSE;
  502.   bwind=dbox_syshandle(edbox);
  503.   dbox_eventhandler(edbox,edhandler,0);
  504.   dbox_raw_eventhandler(edbox,edkeys,0);
  505.   wmenu=menu_new(PROGNAME,WMENU);
  506.   if(!wmenu) return FALSE;
  507.   tempmenu=menu_new("Save",SMENU);
  508.   if(!tempmenu) return FALSE;
  509.   menu_submenu(wmenu,5,tempmenu);
  510.   tempmenu=menu_new("Save",RMENU);
  511.   if(!tempmenu) return FALSE;
  512.   menu_submenu(wmenu,6,tempmenu);
  513.   tempmenu=menu_new("Graph",GMENU);
  514.   if(!tempmenu) return FALSE;
  515.   menu_submenu(wmenu,9,tempmenu);
  516.   if(!event_attachmenu(swind,wmenu,wmenuproc,0)) return FALSE;
  517.   win_register_event_handler(win_ICONBARLOAD,
  518.           (void(*)(wimp_eventstr*,void*))unknown,0);
  519.   win_add_unknown_event_processor(unknown,0);
  520.   win_claim_idle_events(swind);
  521.   if(!sheet_init()) return FALSE;
  522.   visdelay_init();
  523.   return TRUE;
  524. }
  525.  
  526. int main(int n,char **s)
  527. { if(!init()) return 1;
  528.   if(n>1)
  529.   { io_load(s[1]);
  530.     win_settitle(swind,fname);
  531.     sheet_home();
  532.     barclick(0);
  533.   }
  534.   for(;;) event_process();
  535.   return 0;
  536. }
  537.