📄 utilobj.cpp
字号:
if(!text) return false;
if(CursorPos <=0){
Out->TextCursor(text, MyPos, (POINT *) NULL, &CursorPos,
scroll_et == this ? scroll_dist : scroll_dist=0);
return false;
}
Undo.TextCell(this, Out, text, &CursorPos, &m1, &m2, parent, 0L);
CursorPos--; //continue as if delete
case CMD_DELETE:
if(!text) return false;
if(parent) {
((DataObj*)parent)->Command(CMD_ETRACC, text[0] == '=' ? this : 0L, 0L);
}
if(cmd == CMD_DELETE) Undo.TextCell(this, Out, text, &CursorPos, &m1, &m2, parent, 0L);
if(parent) {
((DataObj*)parent)->Command(CMD_MRK_DIRTY, 0L, 0L);
((DataObj*)parent)->Command(CMD_SAVEPOS, 0L, 0L);
}
bRet = false;
if(!text || !text[0]) {
type = ET_UNKNOWN; CursorPos = 0;
}
if(hasMark()) { //delete marked part of text
if (!text || !text[0]) return false;
if(m1 > m2) Swap(m1, m2);
if(m2 >= (short int)strlen(text)) text[m1] = 0;
else strcpy(text+m1, text+m2);
CursorPos = m1;
m1 = m2 = -1;
if(Out) Redraw(Out, (bRet = true));
}
else if(text[CursorPos]) {
strcpy(text + CursorPos, text + CursorPos + 1);
if(!text || !text[0]) {
type = ET_UNKNOWN; CursorPos = 0;
}
if(Out)Redraw(Out, (bRet = true));
}
if(Out)Out->TextCursor(text, MyPos, (POINT *) NULL, &CursorPos,
scroll_et == this ? scroll_dist : scroll_dist=0);
return bRet;
case CMD_COPY:
if(text && text[0]) {
rMark.left = loc.x+2; rMark.right = rb.x-2;
rMark.top = loc.y+1; rMark.bottom = rb.y-2;
if(m1 != m2 && m1 >=0 && m2 >=0) {
if (m1 >m2) Swap(m1, m2);
rMark.left = mx1; rMark.right = mx2;
if(Out) Out->UpdateRect(&rMark, false);
CopyText(text+m1, m2-m1);
if(Out) {
Out->MrkMode = MRK_NONE;
ShowCopyMark(Out, &rMark, 1);
Out->UpdateRect(&rMark, true);
}
return false;
}
CopyText(text, strlen(text));
if(Out)Out->UpdateRect(&rMark, true);
return false;
}
return false;
case CMD_PASTE:
if(pt = PasteText()) {
Undo.TextCell(this, Out, text, &CursorPos, &m1, &m2, parent, 0L);
for(i = 0; pt[i] > 0x20 && i < 81; i++) this->AddChar(pt[i], 0L, 0L);
if(Out) Redraw(Out, true); free(pt);
if(parent && text) {
((DataObj*)parent)->Command(CMD_ETRACC, text[0] == '=' ? this : 0L, 0L);
}
if(i) return true;
}
return false;
case CMD_SHIFTRIGHT:
if(CursorPos == m1 && text[m1]) m1++;
else if(CursorPos == m2 && text[m2]) m2++;
else if(text[CursorPos]){
m1 = CursorPos; m2 = CursorPos+1;
}
if(parent && text) {
((DataObj*)parent)->Command(CMD_ETRACC, text[0] == '=' ? this : 0L, 0L);
}
if(text[CursorPos]) CursorPos++;
else return false;
case CMD_SHIFTLEFT:
if(parent && text) {
((DataObj*)parent)->Command(CMD_ETRACC, text[0] == '=' ? this : 0L, 0L);
}
if (!(CursorPos)) return false;
case CMD_REDRAW:
if(cmd == CMD_SHIFTLEFT) {
if(CursorPos == m1 && m1 >0) m1--;
else if(CursorPos == m2 && m2 >0) m2--;
else if(CursorPos > 0){
m1 = CursorPos; m2 = CursorPos-1;
}
if(CursorPos >0) CursorPos--;
}
if(m1 >=0 && m2 >= 0 && m1 != m2 && Out) {
if(m1 > m2) Swap(m1, m2);
w = h = 0;
if(Align & TXA_HRIGHT) { //right justified text
Out->oGetTextExtent(text, 0, &w, &h);
mx1 = crb.x-4 - w;
}
else { //left justified text
mx1 = loc.x +4;
}
Out->oGetTextExtent(text, m1, &w, &h);
mx1 += (m1 ? w : 0);
Out->oGetTextExtent(text+m1, m2-m1, &w, &h);
mx2 = mx1 + w;
}
HideTextCursor(); Redraw(Out, true);
Out->TextCursor(text, MyPos, (POINT *) NULL, &CursorPos,
scroll_et == this ? scroll_dist : scroll_dist=0);
return true;
case CMD_CURRLEFT:
m1 = m2 = -1;
if(parent && text) {
((DataObj*)parent)->Command(CMD_ETRACC, text[0] == '=' ? this : 0L, 0L);
}
if(CursorPos >0) {
CursorPos--;
if(Redraw(Out, true) && Out->TextCursor(text, MyPos, (POINT *) NULL,
&CursorPos, scroll_et == this ? scroll_dist : scroll_dist=0)) return true;
else return false;
}
else if (data_obj) {
MyPos.x = loc.x-4; MyPos.y = (rb.y+loc.y)/2;
if(((DataObj*)data_obj)->Select(&MyPos))return true;
MyPos.x = loc.x+4;
((DataObj*)data_obj)->Select(&MyPos);
}
return false;
case CMD_CURRIGHT:
m1 = m2 = -1;
if(parent && text) {
((DataObj*)parent)->Command(CMD_ETRACC, text[0] == '=' ? this : 0L, 0L);
}
if(text[CursorPos]){
CursorPos++;
if(Redraw(Out, true) && Out->TextCursor(text, MyPos, (POINT *) NULL,
&CursorPos, scroll_et == this ? scroll_dist : scroll_dist=0)) return true;
else return false;
}
else if (data_obj) {
MyPos.x = rb.x+4; MyPos.y = (rb.y+loc.y)/2; crb.x = rb.x;
if(((DataObj*)data_obj)->Select(&MyPos)) return true;
MyPos.x = rb.x-4;
((DataObj*)data_obj)->Select(&MyPos);
}
return false;
case CMD_UPDATE:
m1 = m2 = -1;
Redraw(Out, true);
return true;
case CMD_POS_FIRST: case CMD_POS_LAST:
if(parent && text) {
((DataObj*)parent)->Command(CMD_ETRACC, text[0] == '=' ? this : 0L, 0L);
}
CursorPos = (cmd == CMD_POS_LAST && text && text[0]) ? strlen(text) : 0;
m1 = m2 = -1;
Redraw(Out, true);
Out->TextCursor(text, MyPos, (POINT *) NULL, &CursorPos,
scroll_et == this ? scroll_dist : scroll_dist=0);
return true;
case CMD_CURRDOWN: case CMD_CURRUP:
if (data_obj) {
//the following calculation of the cursor position is crude
//it is based on a aspect of 2:1 for digits
if(Align & TXA_HRIGHT) //right justified text
MyPos.x = rb.x-4-((rb.y-loc.y-4)*(strlen(text)-CursorPos))/2;
else MyPos.x = loc.x+4+((rb.y-loc.y-4)*CursorPos)/2;
MyPos.y = (cmd == CMD_CURRUP) ? loc.y-2 : rb.y+2;
if(MyPos.x < loc.x) MyPos.x = loc.x +4;
if(MyPos.x > rb.x) MyPos.x = rb.x -4;
if(((DataObj*)data_obj)->Select(&MyPos))return true;
MyPos.y = rb.y;
((DataObj*)data_obj)->Select(&MyPos);
}
return false;
case CMD_MOUSE_EVENT: //track left mouse button
mev = (MouseEvent*) data_obj;
if(!text || !text[0]) return false;
if(mev->x <loc.x || mev->x >crb.x || mev->y <loc.y || mev->y >rb.y)return false;
if(mev->Action == MOUSE_LBDOWN) {
m1 = m2 = -1;
return true;
}
if(mev->Action == MOUSE_LBDOUBLECLICK) {
rMark.top = loc.y+1; rMark.bottom = rb.y-2;
if(!Out->oGetTextExtent(text, strlen(text), &w, &h)) return false;
m1 = 0; m2 = strlen(text);
if(Align & TXA_HRIGHT) { //right justfied text
rMark.right = crb.x -4; rMark.left = crb.x - w - 4;
}
else { //left justified text
rMark.left = loc.x +4; rMark.right = rMark.left +w;
}
mx1 = rMark.left; mx2 = rMark.right;
Redraw(Out, true);
if(parent && text) {
((DataObj*)parent)->Command(CMD_ETRACC, text[0] == '=' ? this : 0L, 0L);
}
return true;
}
MyPos.x = Align & TXA_HRIGHT ? mev->x + 4 : mev->x - 4;
MyPos.y = mev->y;
Out->TxtSet.Align = Align;
j = Out->CalcCursorPos(text, Align & TXA_HRIGHT ? crb :loc, &MyPos);
if(j == m1 || j == m2) return true;
if(Align & TXA_HRIGHT) { //right justfied text
if((i = strlen(text)-j)){
if(!Out->oGetTextExtent(text+j, i, &w, &h)) return false;
w = crb.x - w - 4;
}
else w = crb.x-1;
}
else { //left justified text
if(!j) w = 0;
else if(!Out->oGetTextExtent(text, j, &w, &h))return false;
w += (loc.x+4);
}
if(m1 == m2 && m1 == -1) {
mx1 = (short)(rMark.left = w);
m1 = j;
}
else if(j != m2){
m2 = j;
if(m2 >= 0)Out->UpdateRect(&rMark, false);
mx2 = (short)(rMark.right = w);
rMark.top = loc.y+1; rMark.bottom = rb.y-2;
if(rMark.right < crb.x && rMark.right > loc.x &&
rMark.left > loc.x && rMark.left < crb.x)
Out->UpdateRect(&rMark,true);
}
if(hasMark() && parent)
//remove range-mark of data
((DataObj*)parent)->Command(CMD_UNLOCK, 0L, 0L);
return true;
}
return false;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// return the value (i.e. the floating point equivalent) of text
bool
EditText::GetValue(double *v)
{
anyResult * res;
if(((type & 0xff) == ET_UNKNOWN) && (text)) FindType();
if(!text || !text[0]) {
if((type & 0xff) == ET_VALUE) {
*v = Value; return true;
}
return false;
}
if(CurrText == this && !(type & ET_BUSY)) FindType();
if((type & 0xff) == ET_VALUE){
*v = Value; return true;
}
if((type & 0xff) == ET_FORMULA && text && text[0]){
if(!(type & ET_BUSY)){
type |= ET_BUSY;
if(res = do_formula((DataObj*)parent, text+1)) {
if(res->type == ET_VALUE) Value = res->value;
else Value = 0.0;
*v = Value; type &= ~ET_BUSY;
return res->type == ET_VALUE;
}
type &= ~ET_BUSY;
return false;
}
else type |= ET_CIRCULAR;
*v = Value;
return true;
}
return false;
}
bool
EditText::GetText(char *tx, int size)
{
char *t;
if(text && text[0]) {
if(text[0] =='\'' && text[1]) t = text + 1;
else t = text;
}
else t = 0L;
if(t) {
if((int)strlen(t) < size) strcpy(tx, t);
else {
memcpy(tx, text, size-1); tx[size-1] = 0;
}
return true;
}
if((type & 0xff) == ET_VALUE) {
if(text = (char*)realloc(text, 20)) sprintf(text, "%g", Value);
if(text && text[0]) return(GetText(tx, size));
}
return false;
}
bool
EditText::GetResult(anyResult *r, bool use_last)
{
anyResult * res;
if(!text || !text[0]) {
r->text = 0L;
if((type & 0xff) == ET_VALUE) {
r->value = Value; r->type = ET_VALUE;
}
else {
r->value = 0.0; r->type = ET_UNKNOWN;
}
return true;
}
if((type & 0xff) == ET_UNKNOWN) FindType();
if((type & 0xff) == ET_VALUE) {
r->text = 0L; r->value = Value; r->type = ET_VALUE;
return true;
}
if((type & 0xff) == ET_TEXT) {
r->text = text; r->value = 0.0; r->type = ET_TEXT;
return true;
}
if((type & 0xff) == ET_FORMULA){
if(use_last) {
if(ftext) {
r->text = ftext; r->value = 0.0; r->type = ET_TEXT;
}
else {
r->text = 0L; r->value = Value; r->type = ET_VALUE;
}
return true;
}
if(!(type & ET_BUSY)){
type |= ET_BUSY;
if(res = do_formula((DataObj*)parent, text+1)) {
if(res->type == ET_VALUE) Value = res->value;
else Value = 0.0;
type &= ~ET_BUSY;
memcpy(r, res, sizeof(anyResult));
return true;
}
type &= ~ET_BUSY;
return false;
}
else {
type |= ET_CIRCULAR;
r->text = "#CIRC."; r->value = 0.0; r->type = ET_TEXT;
return true;
}
return false;
}
return false;
}
bool
EditText::SetValue(double v)
{
if(text) text[0] = 0;
Value = v; type = ET_VALUE;
return true;
}
bool
EditText::SetText(char *t)
{
Value = 0.0; type = ET_UNKNOWN;
bgLine = &ETbgnn; bgFill = &ETfbnn; TextCol = 0x00000000L;
if(t && t[0] && (text = (char*)realloc(text, strlen(t)+2))) strcpy(text, t);
else if (text) text[0] = 0;
return false;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// return the ASCII reprentation of value of text if applicable. If not return
// the text with double quotes: comma separated file syntax.
bool
EditText::GetItem(char *dest, int size)
{
char tmp[50];
int i;
if(!text || !text[0] || !dest || size < 3) return false;
if((type & 0xff) == ET_UNKNOWN) FindType();
if(CurrText == this) FindType();
//its a value
if((type & 0xff) == ET_VALUE){
if(text && strlen(text) < (sizeof(tmp)-1)){
strcpy(tmp, text);
if(defs.DecPoint[0] != '.') {
for(i = 0; tmp[i]; i++)
if(tmp[i] == defs.DecPoint[0]) tmp[i]='.';
}
}
else {
sprintf(tmp, "%f", Value); RmTrail(tmp);
}
for(i = 0; i < (size-1) && tmp[i]; i++) dest[i] = tmp[i];
dest[i] = 0;
}
//else its a string
else {
dest[0] = '"';
for(i = 1; i < (size-2) && text[i-1]; i++) dest[i] = text[i-1];
dest[i++] = '"';
dest[i] = 0;
}
return true;
}
void
EditText::SetRec(RECT *rc)
{
loc.x = rc->left; loc.y = rc->top;
crb.x = rb.x = rc->right; crb.y = rb.y = rc->bottom;
}
bool
EditText::isValue()
{
if((type & 0xff)==ET_UNKNOWN) FindType();
return (type == ET_VALUE || type == ET_FORMULA);
}
bool
EditText::isFormula()
{
if((type & 0xff)==ET_UNKNOWN) FindType();
return (type == ET_FORMULA);
}
void
EditText::FindType()
{
if(text && text[0] == '=') {
Align = TXA_VCENTER | TXA_HRIGHT;
type = ET_FORMULA;
}
else if(text && (Txt2Flt(text, &Value))) {
Align = TXA_VCENTER | TXA_HRIGHT;
type = ET_VALUE;
}
else if (text && text[0]) {
Align = TXA_VCENTER | TXA_HLEFT;
type = ET_TEXT;
}
else if ((type && 0xff) == ET_VALUE) {
Align = TXA_VCENTER | TXA_HRIGHT;
type = ET_VALUE;
}
else {
Align = TXA_VCENTER | TXA_HRIGHT;
type = ET_UNKNOWN;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// output formated text - style and font changes
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typedef struct _tag_info {
char *tag;
int and_style, or_style;
int font, op;
}tag_info;
static tag_info tags[] = {
{"<i>", ~TXS_NORMAL, TXS_ITALIC, -1, 0},
{"</i>", ~TXS_ITALIC, TXS_NORMAL, -1, 0},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -