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

📄 sugarmemomain.c

📁 最好用的背单词软件, palm 平台, 如果能移植到别的平台
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <PalmOS.h>

#include "SugarMemo.h"
#include "SugarMemoDB.h"
#include "SugarMemoBrowse.h"
#include "SugarMemoLearn.h"
#include "SugarMemoLearn2.h"
#include "SugarMemoEdit.h"
#include "SugarMemoDetail.h"
#include "SugarMemoRsc.h"
#include "SugarMemoTools.h"
#include "SugarMemoPrefs.h"
#include "SugarMemoFont.h"

#define RowsInDatabaseTable (Int16)11
#define	IntColumnWidth		(UInt16)32

#define DatabaseNameColumn	0

// Define the minimum OS version we support
#define ourMinVersion    sysMakeROMVersion(3,0,0,sysROMStageDevelopment,0)
#define kPalmOS10Version sysMakeROMVersion(1,0,0,sysROMStageRelease,0)

// ********************************************************************
// Global variables
// ********************************************************************

// g_prefs	cache for application preferences during program execution
SugarMemoPreferenceType g_prefs;

UInt32	DatabaseID[RowsInDatabaseTable];
UInt16 	HeaderRecordIndex = HeaderIndexCounts;

//custom font handle
MemHandle phoneticSmallFontH, phoneticLargeFontH, phoneticHugeFontH, OS5_16_FontH;
FontType* phoneticSmallFontP, *phoneticLargeFontP, *phoneticHugeFontP, *OS5_16_FontP;

// ********************************************************************
// Internal Constants
// ********************************************************************

//*******************************************************************
//	Internal functions
//*******************************************************************

//extern Boolean Learn2DoHardKey(EventType* event);

static void DrawDatabaseName(void* table, Int16 row, Int16 column, RectangleType* bounds);
static void DrawCell(Int32 count, RectangleType* cellBounds);
static void MainFormLoadTable();
static void MainFormInit();

static Boolean GetSelectedDatabaseID(LocalID* id);
static Boolean GetSelectedDatabase();

static void DoCommandNewDatabase();
static void DoCommandRenameDatabase();
static void DoCommandDeleteDatabase();
static void DoCommandResetDatabase();
static void DoCommandImport();
static void ClearChoiceBuffer();
static void SortStudyList();
static void RebuildTestQueue();
static void InitializeAllDatabase();
static void PeekDBVersion();


//Main Form Init
void MainFormInit()
{
	Int16 row;
	TableType* table = GetObjectPtr(MainDatabaseTable);

	for (row = 0; row < RowsInDatabaseTable; row++)
	{
		TblSetItemStyle(table, row, DatabaseNameColumn, customTableItem);
		TblSetRowUsable(table, row, false);
	}

	TblSetColumnUsable(table, DatabaseNameColumn, true);

	// Set the callback routine that will draw the records.
	TblSetCustomDrawProcedure (table, DatabaseNameColumn, DrawDatabaseName);	
}

void MainFormLoadTable(){
	
	TableType* table = GetObjectPtr(MainDatabaseTable);
	Int16 row = 0;
	
	Boolean newSearch = true;
	DmSearchStateType searchState;
	UInt16 cardNo;
	LocalID dbID;
	
	DmOpenRef db;
	
	TblUnhighlightSelection(table);
				
	//	search & retrieve database info, set Column value
	while ((0 == DmGetNextDatabaseByTypeCreator(newSearch, &searchState, appVocabularyDB, 
		appFileCreator, false, &cardNo, &dbID)) && (row < RowsInDatabaseTable)){
	
		newSearch = false;
		
		DatabaseID[row] = dbID;			//	save db ID
		db = DmOpenDatabase(cardNo, dbID, dmModeReadOnly);
		
		if (db == 0) {
			ErrAlert(DmGetLastErr());
		}
		else {
			TblSetRowUsable(table, row, true);
			DmCloseDatabase(db);
		} 
		
		row++;				
	}
	
	//	mark left row unusable
	while (row < RowsInDatabaseTable){
		TblSetRowUsable(table, row, false);
		row ++;
	}

	TblDrawTable(table);	
}

void DrawDatabaseName(void* table, Int16 row, Int16 column, RectangleType* bounds){
	
	Char name[dmDBNameLength + 1];
	DmOpenRef	db;
	FontID	currFont;
	Int32	count;
	RectangleType cellBounds;
	Err error;
	
	error = DmDatabaseInfo(0, DatabaseID[row], name, NULL, NULL, NULL, NULL, NULL,
		NULL, NULL, NULL, NULL, NULL);
		
	if (error) {
		ErrAlert(error);
		StrPrintF(name, "%s", "Error in DmDatabaseInfo()");
	}	
	
	currFont = FntSetFont(boldFont);
	WinDrawChars(name, StrLen(name), bounds ->topLeft.x, bounds ->topLeft.y);

	db = DmOpenDatabase(0, DatabaseID[row], dmModeReadWrite);
	
	if (db == 0) ErrAlert(DmGetLastErr());
	else {
		count = (Int32)GetHeaderItemNumber(db, LearningRecordIndex);
		
		cellBounds.topLeft.x = bounds -> topLeft.x + bounds -> extent.x - 3 * IntColumnWidth;
		cellBounds.extent.x = IntColumnWidth;
		cellBounds.topLeft.y = bounds -> topLeft.y;
		cellBounds.extent.y = bounds -> extent.y;
		
		DrawCell(count, &cellBounds);
		
		count = (Int32)GetHeaderItemNumber(db, TestingRecordIndex);
		cellBounds.topLeft.x += IntColumnWidth;
		DrawCell(count, &cellBounds);
		
		count = (Int32)DmNumRecordsInCategory(db, dmAllCategories) - FirstWordRecord;
		cellBounds.topLeft.x += IntColumnWidth;
		DrawCell(count, &cellBounds);
		
		DmCloseDatabase(db);
	}
}

void DrawCell(Int32 count, RectangleType* cellBounds){
	
	Int16 strWidth;
	Int16 strLength;
	Boolean fitInWidth;
	
	Char num[maxStrIToALen + 1];
	StrIToA(num, count);
	
	strLength = StrLen(num);
	strWidth = cellBounds -> extent.x;
	FntCharsInWidth(num, &strWidth, &strLength, &fitInWidth);
	
	WinDrawChars(num, strLength, cellBounds -> topLeft.x + (cellBounds -> extent.x - strWidth)/2, cellBounds -> topLeft.y);
}

Boolean GetSelectedDatabaseID(LocalID* id){

	TableType* table = GetObjectPtr(MainDatabaseTable);
	Int16 row, column;
	
	if (!TblGetSelection(table, &row, &column)){
	
		DoCustomDialog("Sorry", "Please select a database from the list first.", 
			false, 0, NULL, false);
		return false;
	
	}

	*id = DatabaseID[row];
	return true;
}


Boolean GetSelectedDatabase(){

	LocalID localID;
	
	if (!GetSelectedDatabaseID(&localID)) return false;
	
	CurrentDB = DmOpenDatabase(0, localID, dmModeReadWrite);
	
	if (CurrentDB == 0){
		ErrAlert(DmGetLastErr());
		return false;
	}
		
	return true;
}

void DoCommandNewDatabase(){
	
	Char dbName[dmDBNameLength + 1];
	
	MenuEraseStatus(0);
	
	if (DoCustomDialog("New Database", "Input new database name", true, dmDBNameLength, dbName, true)){
		CreateWordDB(dbName);	
		MainFormLoadTable();
    }
}

void DoCommandRenameDatabase(){
	
	LocalID localID;
	Char dbName[dmDBNameLength + 1];
	Err error;
	
	MenuEraseStatus(0);
	
	if (!GetSelectedDatabaseID(&localID)) return;
			
	if (DoCustomDialog("Rename database", "Input new name", true, dmDBNameLength, dbName, true)){
		
		error = DmSetDatabaseInfo(0, localID, dbName, NULL, NULL, 
    				NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    	
    	if (error) ErrAlert(error);
    	else MainFormLoadTable();
		
	}
}

void DoCommandDeleteDatabase(){
	
	TableType* table = GetObjectPtr(MainDatabaseTable);
	LocalID localID;
	Char dbName[dmDBNameLength + 1];
	Err error;
	
	MenuEraseStatus(0);
	
	if (!GetSelectedDatabaseID(&localID)) return;
	
	error = DmDatabaseInfo(0, localID, dbName, NULL, NULL, NULL, NULL, NULL, 
		NULL, NULL, NULL, NULL, NULL);
		
	if (error){
		ErrAlert(error);
		return;
	}

	if (DoCustomDialog(dbName, "Are you sure to delete the selected database?", false, 0, NULL, true)){
		TblUnhighlightSelection(table);
		DeleteWordDB(0, localID);	
		MainFormLoadTable();
	}
}

void DoCommandResetDatabase(){

	LocalID localID;
	DmOpenRef db;

	if (!GetSelectedDatabaseID(&localID)) return;
	
	if (DoCustomDialog("Reset Database", "This operation will clear the memorizing status of all words, continue?", 
		false, 0, NULL, true)){
    	
    	db = DmOpenDatabase(0, localID, dmModeReadWrite);
    	
    	if (db == 0) {   		
    		ErrAlert(DmGetLastErr());
    		return;
    	}
    	
    	ResetDatabase(db);
   		DmCloseDatabase(db);
   		
		MainFormLoadTable();
	}
}

void ClearChoiceBuffer(){

	LocalID localID;
	DmOpenRef db;
	
	if (!GetSelectedDatabaseID(&localID)) return;

	if (DoCustomDialog("Confirm", "This operation wont clear any memorizing information. Use this operation if you encounter invalide record unique ID error",
		false, NULL, NULL, true)){
		
		db = DmOpenDatabase(0, localID, dmModeReadWrite);
		
		if (db == 0) {
			ErrAlert(DmGetLastErr());
			return;
		}
		
		ClearHeaderRecord(db, LearningChoiceBuffer);	
		ClearHeaderRecord(db, TestingChoiceBuffer);
		DmCloseDatabase(db);
		
		MainFormLoadTable();
	}

}

void SortStudyList(){

	LocalID localID;
	DmOpenRef db;
	
	if (!GetSelectedDatabaseID(&localID)) return;

	if (DoCustomDialog("Confirm", "This operation will sort and rebuild study list.\n\nWARNING: This will clear the learning status of all words in study list.",
		false, NULL, NULL, true)){
		
		db = DmOpenDatabase(0, localID, dmModeReadWrite);
		
		if (db == 0) {
			ErrAlert(DmGetLastErr());
			return;
		}
		
		RebuildLearningQueue(db);	
		DmCloseDatabase(db);
		
		MainFormLoadTable();
	}
}

void RebuildTestQueue(){

	LocalID localID;
	DmOpenRef db;
	
	if (!GetSelectedDatabaseID(&localID)) return;

	if (DoCustomDialog("Confirm", "This operation will rebuild and repair testing list.",
		false, NULL, NULL, true)){

		db = DmOpenDatabase(0, localID, dmModeReadWrite);
		
		if (db == 0) {
			ErrAlert(DmGetLastErr());
			return;
		}
		
		RebuildTestingQueue(db, true);
		DmCloseDatabase(db);
		
		MainFormLoadTable();
	}
}

void InitializeAllDatabase(){

	SugarAppInfoType* app, *appHelper = 0;
	TableType* table = GetObjectPtr(MainDatabaseTable);
	Int16 i, lastRow;
	UInt16 version, bits;
	DmOpenRef db;
	Err error;
	
	lastRow = TblGetLastUsableRow(table);
	
	if (lastRow == tblUnusableRow) return;
	
	for (i = 0; i <= lastRow; i++){
	
		db = DmOpenDatabase(0, DatabaseID[i], dmModeReadWrite);
		
		if (db == 0) {
			ErrAlert(DmGetLastErr());
			continue;
		}
		
		UpdateOldVersionDatabase3(db);
		//DoCustomDialog("Debug", "Old Version Database updated", false, 0, NULL, false);
		

		ValidateHeaderRecord(db);
		//DoCustomDialog("Debug", "HeaderRecord validated", false, 0, NULL, false);

		error = SugarDBAppInfoInit(db);		
		if (error){
			ErrAlert(error);			
			DmCloseDatabase(db);
			continue;
		}
		
		bits = 0;
		SetBitMacro(bits, WordFieldPhonetic);
		SetBitMacro(bits, WordFieldNote3);

		app = GetAppInfoPtr(db);
		if (app -> answerPageFields.allBits != bits){
		
			DmWrite(app, (UInt32)&appHelper -> answerPageFields.allBits, &bits, sizeof(bits));
		
		}
		MemPtrUnlock(app);
		
		RebuildTestingQueue(db, false);
		
		// Added by hytown
//		ResetTestingStatus(db);
		
		//DoCustomDialog("Debug", "successfully rebuild testing queue", false, 0, NULL, false);
		DmCloseDatabase(db);
	}

	// Added by hytown to update database infomation	
	if (lastRow >= 0) TblSelectItem(table, 0, 0); // Seems not working
	TblDrawTable(table);

}

void PeekDBVersion(){
	
	SugarAppInfoType* app;
	LocalID localID;
	DmOpenRef db;
	Char buffer[maxStrIToALen + 20];
	
	if (!GetSelectedDatabaseID(&localID)) return;

	db = DmOpenDatabase(0, localID, dmModeReadWrite);
		
	if (db == 0) {
		ErrAlert(DmGetLastErr());
		return;
	}
	
	app = GetAppInfoPtr(db);
	
	if (app != NULL) {
		StrPrintF(buffer, "%s %d", "DB Version:", app -> version);
		MemPtrUnlock(app);
		
		ProgressPie(0, buffer);
		SysTaskDelay(60);
		ProgressPie(ProgressFull, buffer);
	}
		
	DmCloseDatabase(db);		
}

void DoCommandImport(){

	Char name[dmDBNameLength + 1];
	Char newName[dmDBNameLength + 1];

	Char cache[200];
	
	DmOpenRef db, newdb;
	LocalID localID;
	UInt16 num, index, newIndex;
	UInt16 attr = dmUnfiledCategory;
	
	WordRecordType defaultNewRecord, record;
	MemHandle recordH;

	Boolean result;
	

	result = DoCustomDialog("转换数据库", "输入数据库名称", 
		true, dmDBNameLength, name, true);

	if (result == false) return;
	
	localID = DmFindDatabase(0, name);
	
	if (localID == 0) {
		DoCustomDialog("!@#$%^&*", "俺找不到数据库。", false, 0, NULL, false);
		return;
	}

    db = DmOpenDatabase(0, localID, dmModeReadWrite);
    			
    num = DmNumRecordsInCategory(db, dmAllCategories);
    			
    if (num < 17) {

⌨️ 快捷键说明

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