📄 sugarmemolearn2.c
字号:
// calculate one line
strLength = FntWordWrap(p, bounds -> extent.x);
strWidth = FntCharsWidth(p, strLength);
// if has a blank tail, cut it
p2 = p + strLength - 1;
if ((*p2) == 9 || (*p2) == 10 || (*p2) == 13 || (*p2) == 32)
strLength--;
// if the first line crossed by upper bounds, adjust it
if ((Int16)(*top) < (Int16)bounds->topLeft.y && (Int16)(*top) + lineHeight> bounds->topLeft.y){
*topAnchor += bounds->topLeft.y - (*top);
*top = bounds->topLeft.y;
}
// if draw and in the bounds area
if (draw && (Int16)(*top) >= bounds->topLeft.y){
// if current line crossed by bottom bounds, dont draw and return reach bottom
if ((Int16)(*top) + lineHeight > bounds->topLeft.y + bounds->extent.y) {
reachBottom = true;
return reachBottom;
}
// if it is the first line in bounds, write down the line height for scroll usage
if ((*top) == bounds->topLeft.y) *firstLineHeight = lineHeight;
switch (align){
case leftAlign:
xpos = bounds -> topLeft.x;
break;
case centerAlign:
xpos = bounds -> topLeft.x + (bounds -> extent.x - strWidth)/2;
break;
case rightAlign:
xpos = bounds -> topLeft.x + bounds -> extent.x - strWidth;
break;
}
WinDrawChars(p, strLength, xpos, *top);
}
p += strLength;
(*top) += lineHeight;
}
return reachBottom;
}
Boolean DrawFields(RectangleType* bounds, WordFieldFlags flag, UInt16* top, UInt16* topAnchor,
UInt16* firstLineHeight, Boolean draw){
FontID currFont = FntGetFont();
WordRecordType record;
MemHandle recordH;
WordFields i;
SugarAppInfoType * appInfo;
Boolean reachBottom = false;
*top = *topAnchor;
appInfo = GetAppInfoPtr(CurrentDB);
GetWordRecord(CurrentDB, CurrentRecord, &record, &recordH);
for (i = WordFieldQuestion; i < WordFieldsCount; i++){
if (GetBitMacro(flag.allBits, i) != 0 && record.field[i]!= NULL){
FntSetFont(appInfo -> learnFieldFont[i]);
reachBottom = DrawField(bounds, record.field[i], top, appInfo -> learnFieldAlignment[i],
topAnchor, firstLineHeight, draw);
if (draw && reachBottom) break;
}
}
MemHandleUnlock(recordH);
MemPtrUnlock(appInfo);
FntSetFont(currFont);
return reachBottom;
}
Boolean DrawPane(RectangleType* bounds, WordFieldFlags flag, UInt16* topAnchor, UInt16* firstLineHeight){
UInt16 top;
if (*topAnchor == bounds -> topLeft.y){
DrawFields(bounds, flag, &top, topAnchor, firstLineHeight, false);
if (top - *topAnchor < bounds -> extent.y)
*topAnchor = bounds->topLeft.y + (bounds->extent.y - top + *topAnchor) / 2;
}
return DrawFields(bounds, flag, &top, topAnchor, firstLineHeight, true);
}
void DrawUpperPane(WordFieldFlags flag){
RectangleType bounds;
UpperPaneFlag = flag;
GetObjectBounds(Learn2UpperPaneGadget, &bounds);
UpperPaneScrollableDown = DrawPane(&bounds, flag, &UpperPaneTopAnchor, &UpperPaneFirstLineHeight);
UpperPaneScrollableUp = ((Int16)UpperPaneTopAnchor < bounds.topLeft.y );
if (UpperPaneScrollableUp) ShowObject(Learn2UpperPaneScrollUpRepeating);
else HideObject(Learn2UpperPaneScrollUpRepeating);
if (UpperPaneScrollableDown) ShowObject(Learn2UpperPaneScrollDownRepeating);
else HideObject(Learn2UpperPaneScrollDownRepeating);
WinPushDrawState();
WinSetForeColor(UIColorGetTableEntryIndex(UIObjectSelectedFill));
WinDrawLine(0, bounds.topLeft.y + bounds.extent.y, 160, bounds.topLeft.y + bounds.extent.y);
WinPopDrawState();
}
void DrawLowerPane(WordFieldFlags flag){
RectangleType bounds;
LowerPaneFlag = flag;
GetObjectBounds(Learn2LowerPaneGadget, &bounds);
LowerPaneScrollableDown = DrawPane(&bounds, flag, &LowerPaneTopAnchor, &LowerPaneFirstLineHeight);
LowerPaneScrollableUp = ((Int16)LowerPaneTopAnchor < bounds.topLeft.y);
FrmUpdateScrollers(FrmGetActiveForm(), GetObjectIndex(Learn2LowerPaneScrollUpRepeating),
GetObjectIndex(Learn2LowerPaneScrollDownRepeating), LowerPaneScrollableUp, LowerPaneScrollableDown);
}
void DrawChoiceTable(){
TableType* table = GetObjectPtr(Learn2ChoiceTable);
RectangleType expandedBounds = {{0, 48}, {160, 112}};
Int16 row;
UInt16 num, r;
UInt16 BufferIndex, index;
HeaderItem item;
Err error;
SetObjectBounds(Learn2ChoiceTable, &expandedBounds);
switch(HeaderRecordIndex){
case LearningRecordIndex:
BufferIndex = LearningChoiceBuffer;
break;
case TestingRecordIndex:
BufferIndex = TestingChoiceBuffer;
break;
default:
return;
break;
}
while (FindHeaderItemByDBIndex(CurrentDB, BufferIndex, CurrentRecord, &index))
RemoveHeaderItem(CurrentDB, BufferIndex, index);
num = GetHeaderItemNumber(CurrentDB, BufferIndex);
ShuffleHeaderItem(CurrentDB, BufferIndex);
for(row = 0; row < num && row < RowsInChoiceTable - 1; row++){
GetHeaderItem(CurrentDB, BufferIndex, &item, row);
error = DmFindRecordByID(CurrentDB, item, &index);
if (error) {
ErrAlert(error);
index = noRecord;
continue;
}
TblSetRowID(table, row, index);
TblSetRowUsable(table, row, true);
}
if (row != 0){
r = SysRandom(0);
if (r == sysRandomMax) r--;
index = (UInt32)r * (row + 1) / sysRandomMax;
if (index < row)
TblSetRowID(table, row, TblGetRowID(table, (Int16)index));
TblSetRowID(table, index, CurrentRecord);
}
else
TblSetRowID(table, row, CurrentRecord);
TblSetRowUsable(table, row, true);
row ++;
while (row < RowsInChoiceTable){
TblSetRowUsable(table, row, false);
row ++;
}
TblDrawTable(table);
StartingTicks = TimGetTicks();
ShowChoices = true;
}
void Learn2ScrollUpperPane(Boolean up){
ErasePane(Learn2UpperPaneGadget);
if (up){
if (UpperPaneScrollableUp == false) return;
UpperPaneTopAnchor ++;
DrawUpperPane(UpperPaneFlag);
}
else{
if (UpperPaneScrollableDown == false) return;
UpperPaneTopAnchor -= UpperPaneFirstLineHeight;
DrawUpperPane(UpperPaneFlag);
}
}
void Learn2ScrollLowerPane(Boolean up){
ErasePane(Learn2LowerPaneGadget);
if (up){
if (LowerPaneScrollableUp == false) return;
LowerPaneTopAnchor ++;
DrawLowerPane(LowerPaneFlag);
}
else{
if (LowerPaneScrollableDown == false) return;
LowerPaneTopAnchor -= LowerPaneFirstLineHeight;
DrawLowerPane(LowerPaneFlag);
}
}
Boolean LoadWord(){
Err error;
HeaderItem item;
while(true) {
if (0 == GetHeaderItemNumber(CurrentDB, HeaderRecordIndex)){
//Finish();
return false;
}
GetHeaderItem(CurrentDB, HeaderRecordIndex, &item, 0); //fetch first item
error = DmFindRecordByID(CurrentDB, item, &CurrentRecord);
if (error) {
ErrAlert(error);
RemoveHeaderItem(CurrentDB, HeaderRecordIndex, 0);
continue;
}
// if learning, its done
if (HeaderRecordIndex == LearningRecordIndex) break;
// if testing, and if not finished
if (TossNextReviewMode() != 0) break;
Learn2FinishReview();
}
return true;
}
void Learn2InitWord(){
Learn2ClipBoardWord();
//reset all interface settings
ResetUpperPane();
ResetLowerPane();
if (HeaderRecordIndex == LearningRecordIndex)
CurrentLearningStatus = GetLearningStatus(CurrentDB, CurrentRecord);
if (HeaderRecordIndex == TestingRecordIndex)
CurrentLearningStatus = TossNextReviewMode();
switch(CurrentLearningStatus){
case FirstLook:
DrawTranslationLook();
break;
case TranslationRecollection:
DrawTranslationRecollection();
break;
case WordRecollection:
DrawWordRecollection();
break;
case TranslationTest:
DrawTranslationTest();
StartingTicks = TimGetTicks();
break;
case WordTest:
DrawWordTest();
StartingTicks = TimGetTicks();
break;
}
}
void DrawWordLook(){
WordFieldFlags flag;
flag.allBits = 0;
SetBitMacro(flag.allBits, WordFieldAnswer);
DrawUpperPane(flag);
DefaultField = WordFieldQuestion;
LoadLowerPaneTab();
flag.allBits = 0;
SetBitMacro(flag.allBits, DefaultField);
DrawLowerPane(flag);
ShowObject(Learn2NextButton);
ShowNext = true;
}
void DrawTranslationLook(){
WordFieldFlags flag;
flag.allBits = 0;
SetBitMacro(flag.allBits, WordFieldQuestion);
SetBitMacro(flag.allBits, WordFieldPhonetic);
DrawUpperPane(flag);
DefaultField = WordFieldAnswer;
LoadLowerPaneTab();
flag.allBits = 0;
SetBitMacro(flag.allBits, DefaultField);
DrawLowerPane(flag);
ShowObject(Learn2NextButton);
ShowNext = true;
}
void DrawTranslationRecollection(){
WordFieldFlags flag;
flag.allBits = 0;
SetBitMacro(flag.allBits, WordFieldQuestion);
SetBitMacro(flag.allBits, WordFieldPhonetic);
DrawUpperPane(flag);
ShowObject(Learn2ShowButton);
ShowShow = true;
}
void DrawWordRecollection(){
WordFieldFlags flag;
flag.allBits = 0;
SetBitMacro(flag.allBits, WordFieldAnswer);
DrawUpperPane(flag);
ShowObject(Learn2ShowButton);
ShowShow = true;
}
void DrawTranslationTest(){
WordFieldFlags flag;
flag.allBits = 0;
SetBitMacro(flag.allBits, WordFieldQuestion);
SetBitMacro(flag.allBits, WordFieldPhonetic);
DrawUpperPane(flag);
ChoiceField = WordFieldAnswer;
DrawChoiceTable();
}
void DrawWordTest(){
WordFieldFlags flag;
flag.allBits = 0;
SetBitMacro(flag.allBits, WordFieldAnswer);
DrawUpperPane(flag);
ChoiceField = WordFieldQuestion;
DrawChoiceTable();
}
void ResetUpperPane(){
RectangleType bounds;
GetObjectBounds(Learn2UpperPaneGadget, &bounds);
UpperPaneTopAnchor = bounds.topLeft.y;
UpperPaneFirstLineHeight = 0;
UpperPaneScrollableUp = false;
UpperPaneScrollableDown = false;
HideObject(Learn2UpperPaneScrollUpRepeating);
HideObject(Learn2UpperPaneScrollDownRepeating);
}
void ResetLowerPane(){
RectangleType bounds;
GetObjectBounds(Learn2LowerPaneGadget, &bounds);
LowerPaneTopAnchor = bounds.topLeft.y;
LowerPaneFirstLineHeight = 0;
LowerPaneScrollableUp = false;
LowerPaneScrollableDown = false;
FrmUpdateScrollers(FrmGetActiveForm(), GetObjectIndex(Learn2LowerPaneScrollUpRepeating),
GetObjectIndex(Learn2LowerPaneScrollDownRepeating), false, false);
}
WordFieldFlags GetQuestionFields(){
WordFieldFlags flag;
SugarAppInfoType* app;
app = GetAppInfoPtr(CurrentDB);
flag = app->questionPageFields;
MemPtrUnlock(app);
return flag;
}
WordFieldFlags GetAnswerFields(){
WordFieldFlags flag;
SugarAppInfoType* app;
app = GetAppInfoPtr(CurrentDB);
flag = app->answerPageFields;
MemPtrUnlock(app);
return flag;
}
void LoadLowerPaneTab(){
FormType* frmP;
FormType** formPP;
SugarAppInfoType* app;
WordRecordType record;
MemHandle recordH;
UInt16 xpos, ypos = 147, width = 12, height = 12;
WordFields i;
Boolean lefty = Lefty();
if (lefty) xpos = 146;
else xpos = 1;
app = GetAppInfoPtr(CurrentDB);
GetWordRecord(CurrentDB, CurrentRecord, &record, &recordH);
for (i = WordFieldQuestion; i < WordFieldsCount; i++){
if (i == DefaultField || (GetBitMacro(app -> answerPageFields.allBits, i) != 0)
&& (record.field[i] != NULL)){
frmP = FrmGetActiveForm();
formPP = &frmP;
FieldTab[i] = CtlNewControl((void**)formPP, DynamicPushButtonStart + i, pushButtonCtl,
fieldLabel[i], xpos, ypos, width, height, stdFont, DynamicPushButtonGroup, false);
CtlDrawControl(FieldTab[i]);
if (lefty) xpos -= width + 1;
else xpos += width + 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -