Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ncurses.h"
- #include "stdlib.h"
- #include "stdio.h"
- #include <string.h>
- #include "rktlib.h"
- #include "ancalc.h"
- FILE* errorfp;
- struct sheet *mainsheet;
- int main(int argc,char**argv){
- initscr(); /* Start curses mode */
- raw();
- cbreak();
- noecho();
- meta(stdscr,TRUE);
- keypad(stdscr,TRUE);
- start_color();
- init_pair(1,COLOR_BLUE,COLOR_BLACK);
- init_pair(2,COLOR_BLACK,COLOR_BLACK);
- init_pair(3,COLOR_RED,COLOR_BLACK);
- int c;
- int place=0;
- char *inpstr=dstcpy("");
- enum cell_type inptyp = cell_t_string;
- mainsheet=NULL;
- int y=0,x=0;
- errorfp = fopen("err","w");
- if(argc>1){
- mainsheet = loadsheet(argv[1]);
- }
- if(!mainsheet){
- mainsheet = newsheet();
- }
- loop{
- struct cell *cl =sheetcell(mainsheet,x,y);
- attrset(A_REVERSE);
- mvprintw(0,0,"{%s}",typestring[inptyp]);
- mvprintw(1,12,"{%s}",typestring[cl->t]);
- mvprintw(1,0,"[%4d,%4d] ",y,x);
- attroff(A_REVERSE);
- mvaddstr(0,8,inpstr);
- mvaddstr(1,20,valtostr(cl->v,cl->t,0));
- attroff(A_UNDERLINE);
- drawsheet(mainsheet,y,x);
- c=mvgetch(0,8+place);
- erase();
- if(c==EOF||c==ERR)break;
- if(c==27){//ALT+
- int d=getch();
- c=d|0x8000;
- }
- if(c==(0x8000|'Q'))break;
- if(c==(0x8000|'2')){
- savesheet(mainsheet,0);
- }else if(c==(0x8000|'@')){
- savesheet(mainsheet,inpstr);
- }
- if(c==(0x8000|'4')){
- let(sht,loadsheet(inpstr));
- if(sht){
- delsheet(mainsheet);
- mainsheet=sht;
- }
- }
- if(c<='~' && c>=' '){
- char s[2];
- s[0]=c;
- s[1]=0;
- inpstr=dstins(inpstr,place,s);
- place++;
- }
- if(c==(0x8000|'y')){
- free(inpstr);
- place=0;
- struct cell *c=sheetcell(mainsheet,x,y);
- inpstr = dstcpy(valtostr(c->v,c->t,0));
- inptyp = c->t;
- }
- if(c==(0x8000|'t'))inptyp =(inptyp+1)%cell_t_max;
- if(c==KEY_UP&&y>0)y--;
- if(c==KEY_DOWN&&y<mainsheet->rowc-1)y++;
- if(c==KEY_PPAGE&&x>0)x--;
- if(c==KEY_NPAGE&&x<mainsheet->colc-1)x++;
- if(c==(0x8000|'e'))addsheetcol(mainsheet,x);
- if(c==(0x8000|'r'))addsheetrow(mainsheet,y);
- if(c==(0x8000|'E'))addsheetcol(mainsheet,-1);
- if(c==(0x8000|'R'))addsheetrow(mainsheet,-1);
- if(c=='E'-'@')delsheetcol(mainsheet,x);
- if(c=='R'-'@')delsheetrow(mainsheet,y);
- if(x>=mainsheet->colc)x--;
- if(y>=mainsheet->rowc)y--;
- if(c==(0x8000|' '))evalcell(sheetcell(mainsheet,x,y));
- if(c=='\n'){
- inputsheetcell(mainsheet,y,x,inpstr,inptyp,cell_m_value);
- }
- if(c==(0x8000|'\n')){
- inputsheetcell(mainsheet,y,x,inpstr,inptyp,cell_m_code);
- }
- if(c==(0x8000|'f'))formatcell(sheetcell(mainsheet,x,y),inptyp);
- if(c==KEY_LEFT&&place>0)place--;
- if(c==KEY_RIGHT&&place<strlen(inpstr))place++;
- if(c==KEY_BACKSPACE&&place>0){
- inpstr=dstdel(inpstr,place-1,1);
- place--;
- }
- //logmesg("input loop\n");
- }
- endwin(); /* End curses mode */
- return 0;
- }
- void logmesg(char *s){
- fprintf(errorfp,"%s",s);
- fflush(errorfp);
- }
- int cellFX(struct cell*c,int sel){
- if(c->t == cell_t_nothing){
- if(!sel)return COLOR_PAIR(1);
- else return COLOR_PAIR(1)|A_REVERSE;
- }else if((c->t == cell_t_cent || c->t == cell_t_yen) && c->v.f <0){
- if(!sel)return COLOR_PAIR(3);
- else return COLOR_PAIR(3)|A_REVERSE;
- }else if(!sel) return A_NORMAL;
- else return A_REVERSE;
- }
- int colwidth(struct sheet *sht,int x,int y,int h){
- int mxw=0,j;
- let(col,sht->col[x]);
- for(j=y;j<h+y&&j<sht->rowc;j++){
- int smw = valwidth(col.c[j]->v,col.c[j]->t);
- if(smw>mxw)mxw=smw;
- }
- return mxw;
- }
- void drawsheet(struct sheet*sht,int sely,int selx){
- int i,j,y,x;
- int h = LINES-2;
- y = sely-h/2;
- if(y<0)y=0;
- x = selx;
- int n = 1;
- int totw=colwidth(sht,x,y,h);
- int left=1,right=1;
- while(left||right){
- if(x+n<sht->colc){
- int mxw=colwidth(sht,x+n,y,h);
- if(totw+mxw+1>COLS)right=0;
- else{n++;totw += mxw+1;}
- }else right=0;
- if(x>0){
- int mxw=colwidth(sht,x-1,y,h);
- if(totw+mxw+1>COLS)left=0;
- else{x--;n++;totw += mxw+1;}
- }else left=0;
- }
- int *wids=newn(int,n);
- for(i=0;i<n;i++){
- wids[i]=colwidth(sht,x+i,y,h);
- }
- for(j=0;j<h;j++){
- int widctr=0;
- for(i=0;i<n;i++){
- if(x+i>=sht->colc)break;
- if(y+j>=sht->rowc)return;
- struct cell*c = sht->col[x+i].c[y+j];
- attrset(cellFX(c,x+i==selx&&y+j==sely));
- char *s = valtostr(c->v,c->t,wids[i]+1);
- int align = typealign[c->t];
- mvaddstr(2+j,widctr+(wids[i]-strlen(s))*align/2,s);
- attrset(COLOR_PAIR(2)|A_BOLD);
- mvaddch(2+j,widctr+wids[i],'|');
- widctr += wids[i]+1;
- }
- }
- free(wids);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement