📄 spreadwi.cpp
字号:
case CMD_SETHPOS: ssOrg.x = *((int*)tmpl) >= 0 ? *((long*)tmpl) : 0L; return Command(CMD_SETSCROLL, tmpl, o); case CMD_SETVPOS: ssOrg.y = *((int*)tmpl) >= 0 ? *((long*)tmpl) : 0L; return Command(CMD_SETSCROLL, tmpl, o); case CMD_SETFOCUS:
Undo.SetDisp(w); return true; case CMD_KILLFOCUS: return true; case CMD_TEXTSIZE: if(tmpl)ssText.iSize = *((int*)tmpl); return true; case CMD_CONFIG: return defs.PropertyDlg();
case CMD_NONE: return true; case CMD_PRINT: return PrintData(o); } } return false;}boolSpreadWin::ShowGrid(int CellWidth, int CellHeight, int FirstWidth, POINT *cp){ int i, c, nr, nc, ac = 1, na = 0; RECT rc; char text[20]; POINT grid[2]; TextDEF ButtText; bool redim = false;
cpos.x = cp->x; cpos.y = cp->y; if(ch != CellHeight || cw != CellWidth || fw != FirstWidth) redim = true; ch = CellHeight; cw = CellWidth; fw = FirstWidth; if(redim){ if(cButtons) { for(i = 0; cButtons[i]; i++) if(cButtons[i]) delete(cButtons[i]); free(cButtons); } if(rButtons) { for(i = 0; rButtons[i]; i++) if(rButtons[i]) delete(rButtons[i]); free(rButtons); }
if(aButton) delete(aButton); aButton = 0L; cButtons = rButtons = 0L; }
if(!aButton) aButton = new ssButton(this, 0, w->MenuHeight, FirstWidth, CellHeight);
memcpy(&ButtText, &ssText, sizeof(TextDEF)); ButtText.Align = TXA_HCENTER | TXA_VCENTER; w->GetSize(&rc); if(!cButtons) { c = (rc.right/CellWidth)+1; cButtons = (ssButton **)calloc(c, sizeof(ssButton*)); for(i = 0; i < (c-1); i++) cButtons[i] = new ssButton(this, i*CellWidth+FirstWidth, w->MenuHeight, CellWidth+1, CellHeight); } if(!rButtons) { c = (rc.bottom/CellHeight)+1; rButtons = (ssButton**)calloc(c, sizeof(ssButton*)); for(i = 0; i < (c-1); i++) rButtons[i] = new ssButton(this, 0, i*CellHeight+CellHeight+w->MenuHeight, FirstWidth, CellHeight); } w->SetLine((LineDEF *)&GrayLine); d->GetSize(&nc, &nr); grid[0].x = rc.left+FirstWidth; i = (nc-ssOrg.x)*CellWidth+FirstWidth; grid[1].x = i < rc.right ? i : rc.right; if(rButtons) for(i = 0; rButtons[i]; i++) { sprintf(text, "%d", i+1+ssOrg.y); if(rButtons[i]) { rButtons[i]->Command(CMD_SETTEXTDEF, &ButtText, w); rButtons[i]->Command(CMD_SETTEXT, text, w); rButtons[i]->Command(CMD_SELECT, (cpos.y == (i+ssOrg.y)) ? &ac : &na, w);
w->SetLine((LineDEF *)&GrayLine); } grid[0].y = grid[1].y = w->MenuHeight + i*CellHeight - 1; if(i < (2+nr-ssOrg.y)) w->oSolidLine(grid); } grid[0].y = rc.top+CellHeight+w->MenuHeight; i = (1+nr-ssOrg.y)*CellHeight; grid[1].y = (i+w->MenuHeight)< rc.bottom ? i+w->MenuHeight : rc.bottom; if(cButtons) for(i = 0; cButtons[i]; i++) { if(cButtons[i]) { cButtons[i]->Command(CMD_SETTEXTDEF, &ButtText, w); cButtons[i]->Command(CMD_SETTEXT, Int2ColLabel(i+ssOrg.x, true), w); cButtons[i]->Command(CMD_SELECT, (cpos.x == (i+ssOrg.x)) ? &ac : &na, w); w->SetLine((LineDEF *)&GrayLine); } grid[0].x = grid[1].x = i*CellWidth+FirstWidth-1; if(i <= (nc-ssOrg.x)) w->oSolidLine(grid); } w->SetTextSpec(&ssText);
if(aButton) aButton->DoPlot(w); return true;}boolSpreadWin::PrintData(anyOutput *o){ int i, j, k, l, pfw, pcw, pch, rpp, cpp, nc, nr, ix, iy, cpages; RECT rc, margin, margin_first; POINT pp_pos, ss_pos, grid[2]; LineDEF Line1, Line2; TextDEF td, tdp; bool bContinue; time_t ti = time(0L); double val; Line1.patlength = Line2.patlength = 1.0; Line1.color = Line2.color = 0x0; //black gridlines Line1.pattern = Line2.pattern = 0x0; //solid lines switch(defs.cUnits) { case 1: Line1.width = 0.01; break; case 2: Line1.width = 0.003937; break; default: Line1.width = 0.1; break; } Line2.width = Line1.width * 3.0; d->GetSize(&nc, &nr); if(!(o->StartPage())) return false; pfw = iround(o->hres * ((double)fw)/w->hres); pcw = iround(o->hres * ((double)cw)/w->hres); pch = iround(o->vres * ((double)ch)/w->vres + o->vres/20.0); o->ActualSize(&rc); tdp.ColTxt = 0x0; tdp.ColBg = 0x00ffffffL; tdp.fSize = tdp.RotBL = tdp.RotCHAR = 0.0; tdp.Align = TXA_HRIGHT | TXA_VCENTER;#ifdef _WINDOWS tdp.iSize = iround(o->hres/6.0);#else tdp.iSize = iround(o->hres/7.5);#endif tdp.Mode = TXM_TRANSPARENT; tdp.Style = TXS_NORMAL; tdp.Font = FONT_HELVETICA; tdp.text = 0L; memcpy(&td, &ssText, sizeof(TextDEF)); td.Align = TXA_HCENTER | TXA_VCENTER; td.Style = TXS_NORMAL; td.iSize = 0;#ifdef _WINDOWS td.fSize = defs.GetSize(SIZE_CELLTEXT);#else td.fSize = defs.GetSize(SIZE_CELLTEXT)*.8;#endif margin.left = iround(o->hres); margin.right = iround(o->hres/2.0); margin.top = margin.bottom = iround(o->hres); memcpy(&margin_first, &margin, sizeof(RECT)); cpp = (rc.right - margin.left - margin.right - pfw)/pcw; rpp = (rc.bottom - margin.top - margin.bottom - pch)/pch; pp_pos.x = margin.left; pp_pos.y = margin.top; ss_pos.x = 0; ss_pos.y = 0; cpages = 1; do { pp_pos.x = margin.left; pp_pos.y = margin.top; k = (ss_pos.x + cpp) > nc ? nc-ss_pos.x : cpp; l = (ss_pos.y + rpp) > nr ? nr-ss_pos.y : rpp; grid[0].y = margin.top +pch; grid[1].y = grid[0].y + l * pch; o->SetLine(&Line2); grid[0].x = grid[1].x = pp_pos.x; o->oSolidLine(grid); grid[0].x = grid[1].x = pp_pos.x+pfw; o->oSolidLine(grid); o->SetLine(&Line1); for(i = 1; i <= k; i++) { //vertical grid grid[0].x = grid[1].x = pp_pos.x + pfw + i * pcw; o->oSolidLine(grid); } grid[0].x = margin.left+pfw; grid[1].x = grid[0].x + k * pcw; o->SetLine(&Line2); grid[0].y = grid[1].y = pp_pos.y; o->oSolidLine(grid); grid[0].y = grid[1].y = pp_pos.y+pch; o->oSolidLine(grid); o->SetLine(&Line1); for(i = 1; i <= l; i++) { //horizontal grid grid[0].y = grid[1].y = pp_pos.y + pch + i * pch; o->oSolidLine(grid); } o->SetLine(&Line2); td.Align = TXA_HCENTER | TXA_VCENTER; o->SetTextSpec(&td); grid[0].y = margin.top; grid[1].y = grid[0].y + pch; iy = margin.top + (pch >>1); for(i = 0; i <= k; i++) { //column headers grid[0].x = grid[1].x = pp_pos.x + pfw + i * pcw; o->oSolidLine(grid); ix = grid[0].x + (pcw >>1); if(i < k) o->oTextOut(ix, iy, Int2ColLabel(i+ss_pos.x, true), 0); } td.Align = TXA_HRIGHT | TXA_VCENTER; o->SetTextSpec(&td); ix = margin.left + pfw - iround(o->hres/20.0); grid[0].x = margin.left; grid[1].x = grid[0].x + pfw; for(i = 0; i <= l; i++) { //row labels grid[0].y = grid[1].y = pp_pos.y + pch + i * pch; o->oSolidLine(grid); iy = grid[0].y + (pch >>1); sprintf(TmpTxt, "%d", i+1+ss_pos.y); if(i < l) o->oTextOut(ix, iy, TmpTxt, 0); } for(i = 0; i < k; i++) { //spreadsheet data for (j = 0; j < l; j++) { if(d->GetText(j+ss_pos.y, i+ss_pos.x, TmpTxt, TMP_TXT_SIZE)){ if(d->etRows[j+ss_pos.y][i+ss_pos.x]->isFormula()){ td.Align = TXA_HRIGHT | TXA_VCENTER; ix = margin.left+pfw+pcw + i*pcw - iround(o->hres/20.0); d->etRows[j+ss_pos.y][i+ss_pos.x]->GetValue(&val); sprintf(TmpTxt,"%g", val); fit_num_rect(o, pcw - iround(o->hres/30.0), TmpTxt);
} else if(d->etRows[j+ss_pos.y][i+ss_pos.x]->isValue()){ td.Align = TXA_HRIGHT | TXA_VCENTER; ix = margin.left+pfw+pcw + i*pcw - iround(o->hres/20.0); } else { td.Align = TXA_HLEFT | TXA_VCENTER; ix = margin.left+pfw + i*pcw + iround(o->hres/20.0); } iy = pp_pos.y + pch + (pch>>1) + j * pch; o->SetTextSpec(&td); o->oTextOut(ix, iy, TmpTxt, 0); } } } //prepare for next table ss_pos.x += k; bContinue = false; if(ss_pos.x >= nc) {ss_pos.x = 0; ss_pos.y += l; } if(ss_pos.y < nr) { ix = pfw + (cpp % nc)*pcw + iround(o->hres/3.5); iy = (l+2)*pch; if((margin.left + ix + pfw + k*pcw) < (rc.right-margin.right)) { margin.left += pfw + k*pcw + iround(o->hres/3.5); bContinue = true; } else if((margin.top + iy + pch + l*pch) < (rc.bottom - margin.bottom)) { margin.top += iy; margin.left = margin_first.left; bContinue = true; } else { tdp.Align = TXA_HRIGHT | TXA_VCENTER; o->SetTextSpec(&tdp); sprintf(TmpTxt, "page %d", cpages++); o->oTextOut(rc.right-margin.right, rc.bottom-(margin.bottom>>1), TmpTxt, 0); tdp.Align = TXA_HCENTER | TXA_VCENTER; o->SetTextSpec(&tdp); sprintf(TmpTxt, "%s", ctime(&ti)); TmpTxt[24] = 0; o->oTextOut((rc.right-rc.left)>>1, rc.bottom-(margin.bottom>>1), TmpTxt, 0); tdp.Align = TXA_HLEFT | TXA_VCENTER; o->SetTextSpec(&tdp); sprintf(TmpTxt, "RLPlot %s", SZ_VERSION); o->oTextOut(margin_first.left, rc.bottom-(margin.bottom>>1), TmpTxt, 0); memcpy(&margin, &margin_first, sizeof(RECT)); bContinue = true; o->Eject(); } } }while(bContinue); tdp.Align = TXA_HRIGHT | TXA_VCENTER; o->SetTextSpec(&tdp); sprintf(TmpTxt, "page %d", cpages++); o->oTextOut(rc.right-margin.right, rc.bottom-(margin.bottom>>1), TmpTxt, 0); tdp.Align = TXA_HCENTER | TXA_VCENTER; o->SetTextSpec(&tdp); sprintf(TmpTxt, "%s", ctime(&ti)); TmpTxt[24] = 0; o->oTextOut((rc.right-rc.left)>>1, rc.bottom-(margin.bottom>>1), TmpTxt, 0); tdp.Align = TXA_HLEFT | TXA_VCENTER; o->SetTextSpec(&tdp); sprintf(TmpTxt, "RLPlot %s", SZ_VERSION); o->oTextOut(margin_first.left, rc.bottom-(margin.bottom>>1), TmpTxt, 0); o->EndPage(); return true;}
void
SpreadWin::WriteGraphXML(unsigned char **ptr, long *cbd)
{
unsigned char *pg;
long cb = 0, size = 0;
int i;
if(cbd) size = (*cbd / 10000)+ 10000;
for(i = 0; i < NumGraphs; i++) if(g[i]) {
pg = (unsigned char*)GraphToMem(g[i], &cb);
if(pg && cb) {
while ((*cbd+cb+100) > size){
*ptr = (unsigned char*)realloc(*ptr, size += 10000);
}
*cbd += sprintf(((char*)*ptr)+*cbd, "<Graph><![CDATA[\n");
memcpy(*ptr+*cbd, pg, cb); *cbd += cb;
*cbd += sprintf(((char*)*ptr)+*cbd, "]]>\n</Graph>\n");
if(pg) free(pg); pg = 0L; cb = 0;
}
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// This data object is a spreadsheet//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~class SpreadData:public DataObj{typedef struct _pos_info {
POINT ssOrg, currpos;
void *CurrText;};
public: SpreadData(GraphObj *par); ~SpreadData(); bool Init(int nRows, int nCols); bool mpos2dpos(POINT *mp, POINT *dp);
bool Select(POINT *p); void MarkRange(char *range); void HideMark(bool cclp); bool WriteData(char *FileName); bool AddCols(int nCols); bool AddRows(int nRows);
bool ChangeSize(int nCols, int nRows, bool bUndo);
bool DeleteCols(); bool InsertCols();
bool DeleteRows();
bool InsertRows();
void DoPlot(anyOutput *o); bool DelRange(); bool PasteRange(int cmd, char *txt); bool InitCopy(int cmd, void *tmpl, anyOutput *o);
bool SavePos(); bool Command(int cmd, void *tmpl, anyOutput *o); bool ReadData(char *FileName, unsigned char *buffer, int type); bool ReadXML(char *file, unsigned char *buffer, int type, DWORD undo_flags = 0L); bool ReadTSV(char *file, unsigned char *buffer, int type); bool MemList(unsigned char **ptr, int type);private: int CellHeight, CellWidth, FirstWidth, r_disp, c_disp, mrk_offs; RECT rcCopy, cp_src_rec; //bounding rectangle for copy range bool bActive, new_mark, bCopyCut, bUpdate, isRowMark, isColMark; POINT currpos, currpos2; anyOutput *w; SpreadWin *Disp;
GraphObj *g_parent;
EditText *et_racc; char *m_range, *c_range; //mark and copy ranges char *err_msg, *last_err; //error message
_pos_info pos_info; //save position settings};SpreadData::SpreadData(GraphObj *par){ Disp = 0L; m_range = 0L; c_range = 0L; w = 0L; err_msg=last_err = 0L; g_parent = par; CellWidth = CellHeight = FirstWidth = 0;
et_racc = 0L; currpos.x = currpos.y = r_disp = c_disp = mrk_offs = 0; bActive = bCopyCut = bUpdate = isRowMark = isColMark = false; rcCopy.left = rcCopy.right = rcCopy.top = rcCopy.bottom = 0; cp_src_rec.left = cp_src_rec.right = cp_src_rec.top = cp_src_rec.bottom = 0; if(defs.IniFile && FileExist(defs.IniFile)) { OpenGraph(0L, defs.IniFile, 0L); } pos_info.currpos.x = currpos.x; pos_info.currpos.y = currpos.y;
pos_info.CurrText = CurrText;
}SpreadData::~SpreadData(){ FlushData(); if(Disp) delete Disp; Disp = 0L; if(m_range) free(m_range); m_range = 0L; if(c_range) free(c_range); c_range = 0L;}boolSpreadData::Init(int nRows, int nCols){ int i, j; RECT rc; rcCopy.left = rcCopy.top = 0; rcCopy.bottom = cRows = nRows; rcCopy.right = cCols = nCols; currpos.x = currpos.y = 0; new_mark = bCopyCut = false; if(!Disp) { Disp = new SpreadWin(g_parent, this); w = Disp->w; if(w) { CellWidth = w->un2ix(defs.GetSize(SIZE_CELLWIDTH)); CellHeight = w->un2iy(defs.GetSize(SIZE_CELLTEXT)/defs.ss_txt) + 2; FirstWidth = 32;
w->GetSize(&rc); r_disp = (rc.bottom-rc.top)/CellHeight; c_disp = (rc.right-rc.left)/CellWidth+1; } else return false; Disp->ShowGrid(CellWidth, CellHeight, FirstWidth, &currpos); pos_info.ssOrg.x = Disp->ssOrg.x; pos_info.ssOrg.y = Disp->ssOrg.y;
pos_info.CurrText = CurrText;
} if(etRows)FlushData(); etRows = (EditText ***)calloc (cRows, sizeof(EditText **)); if(etRows) for(i = 0; i < cRows; i++) { etRows[i] = (EditText **)calloc(cCols, sizeof(EditText *)); if(etRows[i]) for(j = 0; j < cCols; j++) {#ifdef _DEBUG char text[20]; sprintf (text, "%.2f", i*10.0 + j); etRows[i][j] = new EditText(this, text, i, j);#else etRows[i][j] = new EditText(this, 0L, i, j);#endif } } if (LoadFile) { strcpy(TmpTxt, LoadFile); //we will reenter by recursion ! free(LoadFile); LoadFile = 0L; Disp->Command(CMD_DROPFILE, TmpTxt, w); }
else DoPlot(w); return true;}boolSpreadData::mpos2dpos(POINT *mp, POINT *dp){ if(mp->x < (FirstWidth+10) && mp->x > FirstWidth && Disp->ssOrg.x >0) { Disp->ssOrg.x -= 1; if(CurrText) CurrText->Update(2, w, mp); CurrText = 0L; Disp->Command(CMD_SETSCROLL, 0L, w); mp->x += CellWidth; } if(mp->y < (w->MenuHeight + CellHeight+9) && mp->y > (w->MenuHeight + CellHeight) && Disp->ssOrg.y >0) { Disp->ssOrg.y -= 1; if(CurrText) CurrText->Update(2, w, mp); CurrText = 0L; Disp->Command(CMD_SETSCROLL, 0L, w);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -