⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sugarmemolearn2.c

📁 Palm上著名的背单词软件的源码
💻 C
📖 第 1 页 / 共 4 页
字号:
#include <PalmOS.h>

#include "SugarMemo.h"
#include "SugarMemoRsc.h"
#include "SugarMemoLearn2.h"
#include "SugarMemoDB.h"
#include "SugarMemoTools.h"

//	Sony Jog
#define 	vchrJogUp				(0x1700)
#define 	vchrJogDown				(0x1701)
#define 	vchrJogPushRepeat		(0x1702)
#define 	vchrJogPushedUp			(0x1703)
#define 	vchrJogPushedDown		(0x1704)
#define 	vchrJogPush				(0x1705)
#define 	vchrJogRelease			(0x1706)
#define 	vchrJogBack				(0x1707) 		/* added @ 2001 */


#define 	RowsInChoiceTable 	(Int16)7
#define 	ChoiceTableRowHeight		15

#define 	recollectionMode	true
#define 	testMode			false

#define 	BingoPassFailGroup 		17
#define		RightWrongGroup			23
#define 	DynamicPushButtonGroup 	6666
#define 	DynamicPushButtonStart	1234

#define		maxFail					8

WordFieldFlags UpperPaneFlag;
WordFieldFlags LowerPaneFlag;

UInt16 		UpperPaneTopAnchor;
UInt16 		UpperPaneFirstLineHeight;
Boolean 	UpperPaneScrollableUp;
Boolean		UpperPaneScrollableDown;
UInt16 		LowerPaneTopAnchor;
UInt16 		LowerPaneFirstLineHeight;
Boolean		LowerPaneScrollableUp;
Boolean		LowerPaneScrollableDown;

Boolean 	ShowNext = false;
Boolean 	ShowShow = false;
Boolean 	ShowChoices = false;
Boolean 	ShowRightWrong = false;
Boolean 	ShowBingoPassFail = false;

UInt32		StartingTicks;

WordFields	DefaultField;
WordFields	ChoiceField;

LearningStatus	CurrentLearningStatus;
ControlType* 	FieldTab[WordFieldsCount];
extern EditLabel fieldLabel[WordFieldsCount];

static Boolean 	Learn2FormDoCommand(UInt16 command);
static void 	Learn2FormInitChoiceBuffer();
static void 	Learn2FormInitChoiceTable();
static void		Learn2InitWord();
static void 	Learn2FormQuit();
static void 	DrawChoice(void* table, Int16 row, Int16 column, RectangleType* bounds);
static void 	StrPackString(char* P);
static void 	RemoveBlankHead(char** p);
static Boolean 	DrawField(RectangleType* bounds, char* p, UInt16* top, UInt16 align, 
	UInt16* topAnchor, UInt16* firstLineHeight, Boolean draw);
static Boolean 	DrawFields(RectangleType* bounds, WordFieldFlags flag, UInt16* top, UInt16* topAnchor, 
	UInt16* firstLineHeight, Boolean draw);
static Boolean 	DrawPane(RectangleType* bounds, WordFieldFlags flag, UInt16* topAnchor, UInt16* firstLineHeight);
static void 	DrawUpperPane(WordFieldFlags flag);
static void 	DrawLowerPane(WordFieldFlags flag);
static void 	LoadLowerPaneTab();
static void		ClearLowerPaneTab();
static void 	DrawChoiceTable();
static void		ResetUpperPane();
static void 	ResetLowerPane();
static void		ResetAllPush();
static void		Learn2FormDoButtonNext();
static void 	Learn2FormDoButtonShow();
static void		Learn2FormDoButtonPush(UInt16 controlID);
static void 	Learn2FormTableSelection(Int16 row);
static void		ClearChoiceTable();
static void 	ErasePane(UInt16 ObjectID);
static void 	Learn2ScrollUpperPane(Boolean up);
static void 	Learn2ScrollLowerPane(Boolean up);

static LearningStatus TossNextReviewMode();
static UInt8 Difficulty(float efactor);
static void 	Learn2ProcessReviewRating(Rating rating);
static void		Learn2ProcessLearningRating(Boolean right);
static void		Learn2FinishLearning();
static void 	Learn2FinishReview();
static void 	DrawTranslationLook();
static void 	DrawWordLook();
static void 	DrawTranslationRecollection();
static void 	DrawWordRecollection();
static void		DrawTranslationTest();
static void 	DrawWordTest();

static void		Learn2FormInit();

static Boolean 	Lefty();
static WordFieldFlags 	GetQuestionFields();
static WordFieldFlags 	GetAnswerFields();

static Boolean	LoadWord();
static void 	Learn2DoDynamicPushButton(WordFields field);
static void 	ToggleDynamicField(Boolean prevField);
static void 	Learn2ScrollChoiceSelection(Boolean up);

static void		Learn2ClipBoardWord();

static UInt16 	FontIDToListIndex(FontID font);
static FontID 	ListIndexToFontID(Int16 index);

Boolean Learn2FormHandleEvent(EventType * event){

	FormType* frmP = FrmGetActiveForm();
	Boolean handled = false;
	
	RectangleType bounds;
			
	switch (event->eType){
		
		case frmOpenEvent:
			
			Learn2FormInit();
			FrmDrawForm(frmP);
			if (LoadWord()) Learn2InitWord();
			else Learn2FormQuit();
			handled = true;
			break;
			
		case menuEvent:
			return Learn2FormDoCommand(event->data.menu.itemID);

		case penDownEvent:
			GetObjectBounds(Learn2UpperPaneScrollUpRepeating, &bounds);
			if (UpperPaneScrollableUp && RctPtInRectangle(event->screenX, event->screenY, &bounds)) 
				return false;
				
			GetObjectBounds(Learn2UpperPaneScrollDownRepeating, &bounds);
			if (UpperPaneScrollableDown && RctPtInRectangle(event->screenX, event->screenY, &bounds)) 
				return false;
			
			GetObjectBounds(Learn2UpperPaneGadget, &bounds);
			if (RctPtInRectangle(event->screenX, event->screenY, &bounds)){
				SndPlaySystemSound(sndClick);
				FrmGotoForm(EditForm);
				handled = true;
			}
			break;
			
		case ctlSelectEvent:
					
			if (event->data.ctlSelect.controlID - DynamicPushButtonStart < WordFieldsCount){
				Learn2DoDynamicPushButton(event->data.ctlSelect.controlID - DynamicPushButtonStart);
				handled = true;	
			}
			else {
				switch(event->data.ctlSelect.controlID){
					case Learn2NextButton:
						Learn2FormDoButtonNext();
						handled = true;
						break;
				
					case Learn2ShowButton:
						Learn2FormDoButtonShow();
						handled = true;
						break;
					
					case Learn2RightPushButton:
					case Learn2WrongPushButton:
					case Learn2BingoPushButton:
					case Learn2PassPushButton:
					case Learn2FailPushButton:
						Learn2FormDoButtonPush(event->data.ctlSelect.controlID);
						handled = true;
						break;
			
				}
			}
			break;
			
		case ctlRepeatEvent:
			switch(event->data.ctlRepeat.controlID){
				case Learn2UpperPaneScrollUpRepeating:
					Learn2ScrollUpperPane(true);
					break;
				case Learn2UpperPaneScrollDownRepeating:
					Learn2ScrollUpperPane(false);
					break;
				case Learn2LowerPaneScrollUpRepeating:
					Learn2ScrollLowerPane(true);
					break;
				case Learn2LowerPaneScrollDownRepeating:
					Learn2ScrollLowerPane(false);
					break;
			}
			break;
		
		case tblSelectEvent:
			Learn2FormTableSelection(event->data.tblSelect.row);
			handled = true;
			break;
			
		case keyDownEvent:
			if (EvtKeydownIsVirtual(event))
			{
				switch (event->data.keyDown.chr)
				{
					case vchrPageUp:
						if (ShowChoices){
							Learn2ScrollChoiceSelection(true);
						}
						else if (LowerPaneScrollableUp){
							Learn2ScrollLowerPane(true);
						}
						
						handled = true;
						break;
		
					case vchrPageDown:
						if (ShowChoices){
							Learn2ScrollChoiceSelection(false);
						}
						else if (LowerPaneScrollableDown){
							Learn2ScrollLowerPane(false);
						}
						handled = true;
						break;
						
					case	vchrJogUp:
					case	vchrJogDown:
					case	vchrJogPushRepeat:
					case	vchrJogPushedUp:
					case	vchrJogPushedDown:
					case	vchrJogPush:
					case	vchrJogRelease:
					case 	vchrJogBack:
						 return Learn2FormDoHardKey(event);
					
					default:
						break;
				}
			}
			break;
		
			
		default:
			break;
	}
	return handled;			
}

void Learn2FormInit(){
	
	//bingo, pass, fail push
	//right, wrong, push
	//next, show button
	//lowerpane scroll repeating
	FormType* frmP = FrmGetActiveForm();
	
	if (Lefty()){

		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2BingoPushButton), 47, 145); 	//bingo push
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2PassPushButton), 23, 145);
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2FailPushButton), 1, 145);
	
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2RightPushButton), 38, 145);
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2WrongPushButton), 1, 145);

		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2NextButton), 1, 145);
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2ShowButton), 1, 145);

		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2LowerPaneScrollUpRepeating), 88, 145);
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2LowerPaneScrollDownRepeating), 88, 153);
		
	}
	else {
		
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2BingoPushButton), 86, 145); 	//bingo push
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2PassPushButton), 114, 145);
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2FailPushButton), 138, 145);
	
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2RightPushButton), 86, 145);
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2WrongPushButton), 123, 145);

		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2NextButton), 86, 145);
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2ShowButton), 86, 145);

		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2LowerPaneScrollUpRepeating), 60, 145);
		FrmSetObjectPosition(frmP, GetObjectIndex(Learn2LowerPaneScrollDownRepeating), 60, 153);
	
	}

	ShowNext = false;
 	ShowShow = false;
	ShowChoices = false;
	ShowRightWrong = false;
 	ShowBingoPassFail = false;

	HideObject(Learn2LowerPaneGadget);
	HideObject(Learn2UpperPaneGadget);
	Learn2FormInitChoiceTable();
	Learn2FormInitChoiceBuffer();
}

Boolean Learn2FormDoCommand(UInt16 command){
	
	Boolean handled = false;
	
	switch(command){
		
		case LearnOptionsMenuItem1300:  // font settings
			FrmGotoForm(FontForm);
			handled = true;
			break;
		
		case LearnOptionsMenuItem1302:	// quit
			Learn2FormQuit();
			handled = true;
			break;
			
		default:
			break;
	}
	return handled;
}

void Learn2FormInitChoiceTable(){
	
	Int16 row;
	
	TableType* table = GetObjectPtr(Learn2ChoiceTable);
	
	for (row = 0; row < RowsInChoiceTable; row++){
		TblSetItemStyle(table, row, 0, customTableItem);	
		TblSetRowHeight(table, row, 16);
		TblSetItemFont(table, row, 0, largeBoldFont);
		TblSetRowUsable(table, row, false);
	}
	
	TblSetColumnUsable(table, 0, true);
	TblSetCustomDrawProcedure(table, 0, DrawChoice);
}

void Learn2FormInitChoiceBuffer(){
	
	switch(HeaderRecordIndex){
		case LearningRecordIndex:
			InitHeaderBuffer(CurrentDB, LearningChoiceBuffer);
			break;
			
		case TestingRecordIndex:
			InitHeaderBuffer(CurrentDB, TestingChoiceBuffer);
			break;
			
		default:
			ErrDisplay("Error HeaderRecordIndex in Learn2FormInitChoiceBuffer");
			break;
	}
}

void DrawChoice(void* table, Int16 row, Int16 column, RectangleType* bounds){
	
	WordRecordType record;
	MemHandle recordH;
	
	char* strP;
	MemHandle strH;
	
	UInt16 index = TblGetRowID(table, row);
	Int16 strWidth, strLength;
	Boolean fit;
	FontID currFont;
	
	if (index == noRecord) return;
	
	GetWordRecord(CurrentDB, index, &record, &recordH);
	
	if (record.field[ChoiceField] == NULL || StrLen(record.field[ChoiceField]) == 0){
		MemHandleUnlock(recordH);
		return;
	}
	
	strH = MemHandleNew(StrLen(record.field[ChoiceField]) + 1);
	if (strH == NULL) return;
	
	strP = MemHandleLock(strH);
	
	StrCopy(strP, record.field[ChoiceField]);
	MemHandleUnlock(recordH);

//	Omitted by hytown to avoid phrase messy
//	StrPackString(strP);
		
	currFont = FntSetFont(largeBoldFont);
	strWidth = bounds->extent.x;
	strLength = StrLen(strP);
	FntCharsInWidth(strP, &strWidth, &strLength, &fit);
		
	if (!fit){
		FntSetFont(boldFont);
		strWidth = bounds->extent.x;
		strLength = StrLen(strP);
		FntCharsInWidth(strP, &strWidth, &strLength, &fit);
			
		if (!fit){
			strWidth = bounds->extent.x;
			strLength = StrLen(strP);
			FntSetFont(stdFont);
			FntCharsInWidth(strP, &strWidth, &strLength, &fit);
		}
	}
		
	WinDrawChars(strP, StrLen(strP), bounds->topLeft.x + (bounds->extent.x - strWidth)/2, 
		bounds -> topLeft.y + (bounds -> extent.y + 2 - FntLineHeight())/2 - 1);
			
	MemHandleFree(strH);
	FntSetFont(currFont);

}

void StrPackString(char* P){
	char* P1, *p, *p1;
	MemHandle h;
	
	if (P == NULL || StrLen(P) == 0) return;
	h = MemHandleNew(StrLen(P) + 1);
	if (h == NULL) return;
	P1 = MemHandleLock(h);
	
	p = P;
	p1 = P1;
	
	while (true){

		if ((*p) != 9 && (*p) != 10 && (*p) != 13 && (*p) != 32){
			(*p1) = (*p);
			if ((*p) == '\0') break;
			p1 ++;
		}
		p++;
	}

	StrCopy(P, P1);
	
	MemPtrFree(P1);	
}

void RemoveBlankHead(char** p){
	while ((**p) == 9 			//Horizontal Tabulation
			|| (**p) == 10		//Line Feed
			|| (**p) == 13		//Carrige Return
			|| (**p) == 32)		//Space
		(*p)++;
}

Boolean DrawField(RectangleType* bounds, char* p, UInt16* top, UInt16 align, 
	UInt16* topAnchor, UInt16* firstLineHeight, Boolean draw){

	Int16 strWidth;
	Int16 strLength;
	Int16 xpos;
	Int16 lineHeight = FntLineHeight();
	char* p2;
	Boolean reachBottom = false;
	
	while (true){
		
		//	remove blank
		RemoveBlankHead(&p);
		
		if ((*p) == 0) break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -