Advertisement
OrenWatson

ancalc.c (main file)

Dec 6th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.41 KB | None | 0 0
  1. #include "ncurses.h"
  2. #include "stdlib.h"
  3. #include "stdio.h"
  4. #include <string.h>
  5. #include "rktlib.h"
  6. #include "ancalc.h"
  7. FILE* errorfp;
  8. struct sheet *mainsheet;
  9.  
  10. int main(int argc,char**argv){
  11.     initscr();          /* Start curses mode          */
  12.     raw();
  13.     cbreak();
  14.     noecho();
  15.     meta(stdscr,TRUE);
  16.     keypad(stdscr,TRUE);
  17.     start_color();
  18.     init_pair(1,COLOR_BLUE,COLOR_BLACK);
  19.     init_pair(2,COLOR_BLACK,COLOR_BLACK);
  20.     init_pair(3,COLOR_RED,COLOR_BLACK);
  21.     int c;
  22.     int place=0;
  23.     char *inpstr=dstcpy("");
  24.     enum cell_type inptyp = cell_t_string;
  25.     mainsheet=NULL;
  26.     int y=0,x=0;
  27.     errorfp = fopen("err","w");
  28.     if(argc>1){
  29.         mainsheet = loadsheet(argv[1]);
  30.     }
  31.     if(!mainsheet){
  32.         mainsheet = newsheet();
  33.     }
  34.     loop{
  35.         struct cell *cl =sheetcell(mainsheet,x,y);
  36.         attrset(A_REVERSE);
  37.         mvprintw(0,0,"{%s}",typestring[inptyp]);
  38.         mvprintw(1,12,"{%s}",typestring[cl->t]);
  39.         mvprintw(1,0,"[%4d,%4d] ",y,x);
  40.         attroff(A_REVERSE);
  41.         mvaddstr(0,8,inpstr);
  42.         mvaddstr(1,20,valtostr(cl->v,cl->t,0));
  43.         attroff(A_UNDERLINE);
  44.         drawsheet(mainsheet,y,x);
  45.         c=mvgetch(0,8+place);
  46.         erase();
  47.         if(c==EOF||c==ERR)break;
  48.         if(c==27){//ALT+
  49.             int d=getch();
  50.             c=d|0x8000;
  51.         }
  52.         if(c==(0x8000|'Q'))break;
  53.         if(c==(0x8000|'2')){
  54.             savesheet(mainsheet,0);
  55.         }else if(c==(0x8000|'@')){
  56.             savesheet(mainsheet,inpstr);
  57.         }
  58.         if(c==(0x8000|'4')){
  59.             let(sht,loadsheet(inpstr));
  60.             if(sht){
  61.                 delsheet(mainsheet);
  62.                 mainsheet=sht;
  63.             }
  64.         }
  65.         if(c<='~' && c>=' '){
  66.             char s[2];
  67.             s[0]=c;
  68.             s[1]=0;
  69.             inpstr=dstins(inpstr,place,s);
  70.             place++;
  71.         }
  72.         if(c==(0x8000|'y')){
  73.             free(inpstr);
  74.             place=0;
  75.             struct cell *c=sheetcell(mainsheet,x,y);
  76.             inpstr = dstcpy(valtostr(c->v,c->t,0));
  77.             inptyp = c->t;
  78.         }
  79.         if(c==(0x8000|'t'))inptyp =(inptyp+1)%cell_t_max;
  80.         if(c==KEY_UP&&y>0)y--;
  81.         if(c==KEY_DOWN&&y<mainsheet->rowc-1)y++;
  82.         if(c==KEY_PPAGE&&x>0)x--;
  83.         if(c==KEY_NPAGE&&x<mainsheet->colc-1)x++;
  84.         if(c==(0x8000|'e'))addsheetcol(mainsheet,x);
  85.         if(c==(0x8000|'r'))addsheetrow(mainsheet,y);
  86.         if(c==(0x8000|'E'))addsheetcol(mainsheet,-1);
  87.         if(c==(0x8000|'R'))addsheetrow(mainsheet,-1);
  88.         if(c=='E'-'@')delsheetcol(mainsheet,x);
  89.         if(c=='R'-'@')delsheetrow(mainsheet,y);
  90.         if(x>=mainsheet->colc)x--;
  91.         if(y>=mainsheet->rowc)y--;
  92.         if(c==(0x8000|' '))evalcell(sheetcell(mainsheet,x,y));
  93.         if(c=='\n'){
  94.             inputsheetcell(mainsheet,y,x,inpstr,inptyp,cell_m_value);
  95.         }
  96.         if(c==(0x8000|'\n')){
  97.             inputsheetcell(mainsheet,y,x,inpstr,inptyp,cell_m_code);
  98.         }
  99.         if(c==(0x8000|'f'))formatcell(sheetcell(mainsheet,x,y),inptyp);
  100.         if(c==KEY_LEFT&&place>0)place--;
  101.         if(c==KEY_RIGHT&&place<strlen(inpstr))place++;
  102.         if(c==KEY_BACKSPACE&&place>0){
  103.             inpstr=dstdel(inpstr,place-1,1);
  104.             place--;
  105.         }
  106.         //logmesg("input loop\n");
  107.     }
  108.     endwin();           /* End curses mode        */
  109.     return 0;
  110. }
  111.  
  112. void logmesg(char *s){
  113.     fprintf(errorfp,"%s",s);
  114.     fflush(errorfp);
  115. }
  116.  
  117. int cellFX(struct cell*c,int sel){
  118.     if(c->t == cell_t_nothing){
  119.         if(!sel)return COLOR_PAIR(1);
  120.         else return COLOR_PAIR(1)|A_REVERSE;
  121.     }else if((c->t == cell_t_cent || c->t == cell_t_yen) && c->v.f <0){
  122.         if(!sel)return COLOR_PAIR(3);
  123.         else return COLOR_PAIR(3)|A_REVERSE;
  124.     }else if(!sel) return A_NORMAL;
  125.     else return A_REVERSE;
  126. }
  127.  
  128. int colwidth(struct sheet *sht,int x,int y,int h){
  129.     int mxw=0,j;
  130.     let(col,sht->col[x]);
  131.     for(j=y;j<h+y&&j<sht->rowc;j++){
  132.         int smw = valwidth(col.c[j]->v,col.c[j]->t);
  133.         if(smw>mxw)mxw=smw;
  134.     }
  135.     return mxw;
  136. }
  137.  
  138. void drawsheet(struct sheet*sht,int sely,int selx){
  139.     int i,j,y,x;
  140.     int h = LINES-2;
  141.     y = sely-h/2;
  142.     if(y<0)y=0;
  143.     x = selx;
  144.     int n = 1;
  145.     int totw=colwidth(sht,x,y,h);
  146.     int left=1,right=1;
  147.     while(left||right){
  148.         if(x+n<sht->colc){
  149.             int mxw=colwidth(sht,x+n,y,h);
  150.             if(totw+mxw+1>COLS)right=0;
  151.             else{n++;totw += mxw+1;}
  152.         }else right=0;
  153.         if(x>0){
  154.             int mxw=colwidth(sht,x-1,y,h);
  155.             if(totw+mxw+1>COLS)left=0;
  156.             else{x--;n++;totw += mxw+1;}
  157.         }else left=0;
  158.     }
  159.     int *wids=newn(int,n);
  160.     for(i=0;i<n;i++){
  161.         wids[i]=colwidth(sht,x+i,y,h);
  162.     }
  163.     for(j=0;j<h;j++){
  164.         int widctr=0;
  165.         for(i=0;i<n;i++){
  166.             if(x+i>=sht->colc)break;
  167.             if(y+j>=sht->rowc)return;
  168.             struct cell*c = sht->col[x+i].c[y+j];
  169.             attrset(cellFX(c,x+i==selx&&y+j==sely));
  170.             char *s = valtostr(c->v,c->t,wids[i]+1);
  171.             int align = typealign[c->t];
  172.             mvaddstr(2+j,widctr+(wids[i]-strlen(s))*align/2,s);
  173.             attrset(COLOR_PAIR(2)|A_BOLD);
  174.             mvaddch(2+j,widctr+wids[i],'|');
  175.             widctr += wids[i]+1;
  176.         }
  177.     }
  178.     free(wids);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement