📄 boolexpwinc.c
字号:
voidBoolExpWinC::AddTermExp(TermExpC *term){ WArgList args; args.Reset(); args.UserData(this); Widget w = term->CreateWidget(expDA, ARGS); XtManageChild(w); XtOverrideTranslations(w, translations); ExpElemC *elem = new ExpElemC(term, w); expList.insert(elem, insertPos); insertPos++; DrawExp();}/*------------------------------------------------------------------------- * Method to calculate positions for all expression elements */voidBoolExpWinC::DrawExp(){ resizeOk = False; // Don't respond to self-induced resizes rowList.removeAll();// XtUnmanageChild(expDA);//// Get element sizes// rowHt = cursorHt; unsigned count = expList.size(); int i; for (i=0; i<count; i++) { ExpElemC *elem = expList[i]; XtVaSetValues(*elem, XmNborderWidth, 0, NULL); // Clear border elem->GetSize(); if ( elem->Height() > rowHt ) rowHt = elem->Height(); }//// Position elements// Position x = marWd; Position y = marHt; WArgList args; for (i=0; i<count; i++) { ExpElemC *elem = expList[i];//// Draw insertion cursor if necessary// if ( insertPos == i ) { if ( (int)x >= (int)marWd && (int)(x+cursorWd) >= (int)(clipWd-marWd) ) { x = marWd; y += rowHt; } if ( x == marWd ) rowList.append(i); args.X(x); args.Y(y + ((int)(rowHt - cursorHt)/(int)2)); // Center vertically XtSetValues(cursorLabel, ARGS); x += cursorWd; } // End if placing cursor here//// See if it will fit on this row// if ( (int)x >= (int)marWd && (int)(x+elem->Width()) >= (int)(clipWd-marWd) ) { x = marWd; y += rowHt; } if ( x == marWd ) rowList.append(i);//// Set and update position// args.X(x); args.Y(y + ((int)(rowHt - elem->Height())/(int)2)); // Center vertically XtSetValues(*elem, ARGS); x += elem->Width(); } // End for each expression if ( insertPos == count ) { if ( (int)x >= (int)marWd && (int)(x+cursorWd) >= (int)(clipWd-marWd) ) { x = marWd; y += rowHt; } if ( x == marWd ) rowList.append(i); args.X(x); args.Y(y + ((int)(rowHt - cursorHt)/(int)2)); // Center vertically XtSetValues(cursorLabel, ARGS); }// XtManageChild(expDA); resizeOk = True;} // End BoolExpWinC DrawExp/*----------------------------------------------------------------------- * Event handler for scrolled window resize */voidBoolExpWinC::HandleResize(Widget, BoolExpWinC *be, XEvent*, Boolean*){ XtVaGetValues(be->clipWin, XmNwidth, &be->clipWd, XmNheight, &be->clipHt, 0); XtVaSetValues(be->cornerDA, XmNx, (Position)(be->clipWd - be->borWd*2 - 1), XmNy, (Position)(be->clipHt - be->borWd*2 - 1), NULL); if ( be->resizeOk ) be->DrawExp();}/*----------------------------------------------------------------------- * Method to build an expression structure for the specified portion of the * expression list */BoolExpC*BoolExpWinC::ParseExpList(int first, int last){ if ( last < first ) return NULL;//// Try to split the expression list into: (left side) operation (right side).// Start reading from the right. This will insure that left to right// precedence is followed.// BoolExpC *expB;//// Look for a right parenthesis// ExpElemC *elem = expList[last]; ExpElemC::ExpElemT type = elem->Type(); if ( type == ExpElemC::RPAR ) {//// Back up to the corresponding left parenthesis// int lpos = last-1; int rcount = 1; while ( rcount > 0 && lpos >= first ) { switch (expList[lpos]->Type()) { case (ExpElemC::RPAR): rcount++; break; case (ExpElemC::LPAR): rcount--; break; } if ( rcount != 0 ) lpos--; } // End while matching left not found//// Check for missing left parenthesis// if ( rcount != 0 ) { XtVaSetValues(*elem, XmNborderWidth, 1, NULL); StringC msg("Right parenthesis is missing a corresponding left."); PopupMessage(msg); return NULL; }//// Check for empty expression // if ( lpos == last-1 ) { XtVaSetValues(*elem, XmNborderWidth, 1, NULL); StringC msg("Missing expression."); PopupMessage(msg); return NULL; }//// Create a new expression from the elements between the parentheses// expB = ParseExpList(lpos+1, last-1); if ( !expB ) return NULL; last = lpos-1; } // End if last element is a right parenthesis else if ( type == ExpElemC::TERMINAL ) { expB = new BoolExpC((TermExpC*)*elem); last--; } else { XtVaSetValues(*elem, XmNborderWidth, 1, NULL); StringC msg("Syntax error. Expecting ')' or terminal expression."); PopupMessage(msg); return NULL; }//// See if we're done// if ( last < first ) return expB;//// Look for possible NOTs// elem = expList[last]; type = elem->Type(); while ( type == ExpElemC::NOT ) {//// Negate current expression// expB = new BoolExpC(BoolExpC::NOT, expB); last--;//// See if we're done// if ( last < first ) return expB;//// Get next element// elem = expList[last]; type = elem->Type(); } // End for each NOT element//// Look for an operation// BoolExpC::BoolOpT op; if ( type == ExpElemC::AND ) { op = BoolExpC::AND; last--; } else if ( type == ExpElemC::OR ) { op = BoolExpC::OR; last--; } else { XtVaSetValues(*elem, XmNborderWidth, 1, NULL); StringC msg("Syntax error. Expecting AND or OR."); PopupMessage(msg); delete expB; return NULL; }//// Look for left side of operation// if ( last < first ) { XtVaSetValues(*elem, XmNborderWidth, 1, NULL); StringC msg("Syntax error. Expecting first argument for binary operator."); PopupMessage(msg); delete expB; return NULL; }//// Everything else is left side// BoolExpC *expA = ParseExpList(first, last); if ( !expA ) { delete expB; return NULL; } return new BoolExpC(expA, op, expB);} // End BoolExpWinC ParseExpList/*--------------------------------------------------------------- * Timer proc to blink insertion cursor */voidBoolExpWinC::BlinkProc(BoolExpWinC *be, XtIntervalId*){ if ( be->cursorOn ) { XtSetMappedWhenManaged(be->cursorLabel, False); be->cursorOn = False; } else { XtSetMappedWhenManaged(be->cursorLabel, True); be->cursorOn = True; } be->blinkTimer = XtAppAddTimeOut(halApp->context, be->blinkInterval, (XtTimerCallbackProc)BlinkProc, (XtPointer)be);} // End BoolExpWinC BlinkProc/*--------------------------------------------------------------- * Handle a keyboard focus change event in the area */voidBoolExpWinC::HandleFocusChange(Widget, BoolExpWinC *be, XEvent *ev, Boolean*){ if ( be->blinkTimer ) XtRemoveTimeOut(be->blinkTimer); switch (ev->type) { case (FocusIn): case (EnterNotify): if ( be->blinkInterval > 0 ) BlinkProc(be, NULL); break; case (FocusOut): case (LeaveNotify): be->cursorOn = True; XtSetMappedWhenManaged(be->cursorLabel, True); break; }} // End BoolExpWinC HandleFocusChangevoidBoolExpWinC::HandleInsertLParen(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); DoLParen(NULL, be, NULL);}voidBoolExpWinC::HandleInsertRParen(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); DoRParen(NULL, be, NULL);}voidBoolExpWinC::HandleInsertAnd(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); DoAnd(NULL, be, NULL);}voidBoolExpWinC::HandleInsertOr(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); DoOr(NULL, be, NULL);}voidBoolExpWinC::HandleInsertNot(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); DoNot(NULL, be, NULL);}voidBoolExpWinC::HandleDeleteLeft(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); if ( be->insertPos > 0 ) { be->insertPos--; delete be->expList[be->insertPos]; be->expList.remove(be->insertPos); be->DrawExp(); }}voidBoolExpWinC::HandleDeleteRight(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); if ( be->insertPos < be->expList.size() ) { delete be->expList[be->insertPos]; be->expList.remove(be->insertPos); be->DrawExp(); }}voidBoolExpWinC::HandleMoveLeft(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); if ( be->insertPos > 0 ) { be->insertPos--; be->DrawExp(); }}voidBoolExpWinC::HandleMoveRight(Widget w, XKeyEvent*, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); if ( be->insertPos < be->expList.size() ) { be->insertPos++; be->DrawExp(); }}voidBoolExpWinC::HandleMovePointer(Widget w, XButtonEvent *ev, String*, Cardinal*){ BoolExpWinC *be; XtVaGetValues(w, XmNuserData, &be, NULL); XmProcessTraversal(be->expDA, XmTRAVERSE_CURRENT);//// Translate event coordinates to expDA if this is a child// if ( XtParent(w) == be->expDA ) { Position x, y; XtVaGetValues(w, XmNx, &x, XmNy, &y, NULL); ev->x += x; ev->y += y; } int row = 0; int y = be->marHt + be->rowHt; while ( ev->y > y ) { y += be->rowHt; row++; } unsigned rowCount = be->rowList.size(); if ( row >= rowCount ) row = rowCount - 1; int first = *be->rowList[row]; int last; if ( row == rowCount - 1 ) last = be->expList.size() - 1; else last = *be->rowList[row+1] - 1; int x = be->marWd; Boolean found = (ev->x <= x); int i; for (i=first; !found && i<=last; i++) { ExpElemC *elem = be->expList[i];//// Add space for insertion cursor if necessary// if ( i != first && be->insertPos == i ) x += be->cursorWd;//// Add space for current element// x += elem->Width(); found = (ev->x <= x); } // End for each expression if (found) i--;//// Place insertion cursor in front of i// be->insertPos = i; be->DrawExp();} // End BoolExpWinC HandleMovePointer
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -