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

📄 todo.c

📁 我的Palm OS 5 SDK zhCN_PIMApps代码。 使用codewarrior 开发环境
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************************
 *
 * Copyright (c) 1995-2003 PalmSource, Inc. All rights reserved.
 *
 * File: ToDo.c
 *
 * Release: Palm OS 5 SDK (68K) R3.
 *
 * Description:
 *	  This is the ToDo application's main module.  
 *
 *****************************************************************************/

#include <PalmOS.h>
#include <FntGlue.h>
#include <TraceMgr.h>

#include <PalmUtils.h>

#include "ToDo.h"
#include "ToDoDB.h"
#include "ToDoRsc.h"

// Error checking routines
extern void ECToDoDBValidate(DmOpenRef dbP);

// Exported routines
extern Char* GetToDoNotePtr (ToDoDBRecordPtr recordP);


/***********************************************************************
 *
 *	Internal Constants
 *
 ***********************************************************************/
#define toDoVersionNum					3
#define toDoPrefsVersionNum			3
#define todoPrefID						0x00
#define toDoDBName						"ToDoDB"
#define toDoDBType						'DATA'

// If the preferences version found is less than or equal to this
// value, we need to update the font info.
#define toDoPrefsVerUpdateFonts		2

// Fonts used by application
#define noteTitleFont					boldFont

// Columns in the ToDo table of the list view.
#define completedColumn					0
#define priorityColumn					1
#define descColumn						2
#define dueDateColumn					3
#define categoryColumn					4

#define spaceNoPriority					1
#define spaceBeforeDesc					2
#define spaceBeforeCategory			2

#define newToDoSize  					16
#define defaultPriority					1

#define maxNoteTitleLen					40

// Due Date popup list chooses
#define dueTodayItem						0
#define dueTomorrowItem					1
#define dueOneWeekLaterItem			2
#define noDueDateItem					3
#define selectDateItem					4

// Sort Order popup list choices
#define priorityDueDateItem			0
#define dueDatePriorityItem			1
#define categoryPriorityItem			2
#define categoryDueDateItem			3

// Update codes, used to determine how the ToDo list should 
// be redrawn.
#define updateRedrawAll					0x00
#define updateItemDelete				0x01
#define updateItemMove					0x02
#define updateItemHide					0x04
#define updateCategoryChanged			0x08
#define updateDisplayOptsChanged		0x10
#define updateGoTo						0x20
#define updateFontChanged				0x40

// Field numbers, used to indicate where search string was found.
#define descSeacrchFieldNum			0
#define noteSeacrchFieldNum			1

#define noRecordSelected				0xffff

// Number of system ticks (1/60 seconds) to display crossed out item
// before they're erased.
#define crossOutDelay					40


/***********************************************************************
 *
 *	Internal Structutes
 *
 ***********************************************************************/

// This is the structure of the data stored in the state file.
typedef struct {
	UInt16			currentCategory;
	FontID			v20NoteFont;		// For 2.0 compatibility (BGT)
	Boolean			showAllCategories;
	Boolean 			showCompletedItems;
	Boolean 			showOnlyDueItems;
	Boolean			showDueDates;
	Boolean			showPriorities;
	Boolean			showCategories;	// added in version 2 preferences
	Boolean			saveBackup;
	Boolean			changeDueDate;		// added in version 2 preferences
	
	// Version 3 preferences
	FontID			listFont;
	FontID			noteFont;		// For 3.0 and later units.	(BGT)
	
	UInt8				reserved;
} ToDoPreferenceType;

typedef struct {
	DmOpenRef		db;
	Char *			categoryName;
	UInt16			categoryIndex;
} AcceptBeamType;


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

static DmOpenRef			ToDoDB;										// ToDo database
static char					CategoryName [dmCategoryLength];		// name of the current category
static privateRecordViewEnum		CurrentRecordVisualStatus;	// applies to current record
static privateRecordViewEnum		PrivateRecordVisualStatus;	// applies to all other records
static MenuBarPtr			CurrentMenu;								// pointer to the current menu bar
static UInt16				TopVisibleRecord = 0;					// top visible record in list view
static UInt16				PendingUpdate = 0;						// code of pending list view update
static DateFormatType	DateFormat;
static DateType			Today;										// Date when the device was powered on.

// The following global variables are used to keep track of the edit
// state of the application.
static UInt16				CurrentRecord = noRecordSelected;	// record being edited
static Boolean				ItemSelected = false;					// true if a list view item is selected
static Boolean				RecordDirty = false;						// true if a record has been modified
static UInt16				ListEditPosition = 0;					// position of the insertion point in the desc field
static UInt16				ListEditSelectionLength;				// length of the current selection.

// The following global variables are saved to a state file.
static FontID				NoteFont = stdFont;						// font used in note view
static UInt16				CurrentCategory = dmAllCategories;	// currently displayed category
static Boolean				ShowAllCategories = true;				// true if all categories are being displayed
static Boolean 			ShowCompletedItems = true;				// true if completed items are being displayed
static Boolean 			ShowOnlyDueItems = false;				// true if only due items are displayed
static Boolean				ShowDueDates = false;					// true if due dates are displayed in the list view
static Boolean				ShowPriorities = true;					// true if priorities are displayed in the list view
static Boolean				ShowCategories = false;					// true if categories are displayed in the list view
static Boolean				SaveBackup = true;						// true if save backup to PC is the default
static Boolean				ChangeDueDate = false;					// true if due date is changed to completion date when completed
static FontID				ListFont = stdFont;						// font used to draw to do item

static Boolean				InPhoneLookup = false;					// true if we've called PhoneNumberLookup()

/***********************************************************************
 *
 *	Internal Functions
 *
 ***********************************************************************/
static void ListViewInit (FormPtr frm);
static void ListViewLoadTable (Boolean fillTable);
static void ListViewDrawTable (UInt16 updateCode);
static void ListViewRedrawTable (Boolean fillTable);
static Boolean ListViewUpdateDisplay (UInt16 updateCode);

static Boolean ClearEditState (void);

static void ToDoLoadPrefs(void);		// (BGT)
static void ToDoSavePrefs(void);		// (BGT)

static Boolean SeekRecord (UInt16* indexP, Int16 offset, Int16 direction);

/***********************************************************************
 *
 * FUNCTION:    ECToDoValidateAll
 *
 * DESCRIPTION: This routine preforms various edit checks on the ToDo
 *              application.
 *
 * PARAMETERS:  nothing
 *
 * RETURNED:    nothing
 *
 * REVISION HISTORY:
 *			Name	Date		Description
 *			----	----		-----------
 *			art	5/1/95		Initial Revision
 *
 ***********************************************************************/
#if EMULATION_LEVEL != EMULATION_NONE

static void ECToDoValidateAll ()
{
	MemHandle recordH;


	// If a record in the list view is selected, make sure it exists.
	if (ItemSelected)
		{
		recordH = DmQueryRecord (ToDoDB, CurrentRecord);
		ErrFatalDisplayIf (!recordH, "Selected record does not exist");
		}
		
	else if (CurrentRecord == noRecordSelected)
		{
		// Check the in integrity of the ToDo database.
		#if ERROR_CHECK_LEVEL == ERROR_CHECK_FULL
			ECToDoDBValidate (ToDoDB);
		#endif
		}

}
#endif

/***********************************************************************
 *
 * FUNCTION:     CreateDefaultDatabase
 *
 * DESCRIPTION:  This routine creates the default database from the
 *					  saved image in a resource in the application.
 *
 * PARAMETERS:   none
 *
 * RETURNED:     0 - if no error
 *					  otherwise appropriate error value
 *
 * REVISION HISTORY:
 *			Name	Date		Description
 *			----	----		-----------
 *			vivek	8/17/00	Initial Revision
 *
 ***********************************************************************/
static Err CreateDefaultDatabase(void)
{
	MemHandle resH;
   DmOpenRef dbP;
	Err	error = errNone;

	// Attempt to get our default data image and create our
	// database.
	resH = DmGet1Resource(sysResTDefaultDB, sysResIDDefaultDB);
	if (resH)
		{
		error = DmCreateDatabaseFromImage(MemHandleLock(resH));
		
		if (!error)
			{
			MemHandleUnlock(resH);
			DmReleaseResource(resH);
		
			// set the backup bit on the new database
			ToDoSetDBBackupBit(NULL);
			}
		}

	// If there is no default data, or we had a problem creating it,
	// then attempt to create an empty database.
	if (!resH || error)
		{
		error = ToDoGetDatabase (&dbP, dmModeReadWrite);

		if (!error)
			DmCloseDatabase(dbP);
		}

	return error;
}

/***********************************************************************
 *
 * FUNCTION:     StartApplication
 *
 * DESCRIPTION:  This routine opens the application's database, loads the 
 *               saved-state information and initializes global variables.
 *
 * PARAMETERS:   nothing
 *
 * RETURNED:     nothing
 *
 * REVISION HISTORY:
 *			Name	Date		Description
 *			----	----		-----------
 *			art	2/21/95	Initial Revision
 *			grant	4/6/99	Move code to set backup bit into SetDBBackupBit
 *			jmp	10/4/99	Call ToDoGetDatabase() in place of similar in-line code.
 *
 ***********************************************************************/
static Err StartApplication (void)
{
	Err err = errNone;
	UInt16 mode;

	// Determime if secret record should be shown.
	PrivateRecordVisualStatus = CurrentRecordVisualStatus =
		(privateRecordViewEnum)PrefGetPreference(prefShowPrivateRecords);
	mode = (PrivateRecordVisualStatus == hidePrivateRecords) ?
					dmModeReadWrite : (dmModeReadWrite | dmModeShowSecret);

	// Get the date format from the system preferences.
	DateFormat = (DateFormatType)PrefGetPreference(prefDateFormat);

	// Find the application's data file.  If it doesn't exist create it.
	err = ToDoGetDatabase(&ToDoDB, mode);
	if (err)
		return err;

	// Read the preferences.
	ToDoLoadPrefs();
	TopVisibleRecord = 0;
	CurrentRecord = noRecordSelected;

	// Get today's date.  Will will use it to determine if passed due items
	// need to be redrawn when the device is powered on the next time.
	DateSecondsToDate (TimGetSeconds (), &Today);

	return err;
}


/***********************************************************************
 *
 * FUNCTION:    StopApplication
 *
 * DESCRIPTION: This routine closes the application's database
 *              and saves the current state of the application.
 *
 * PARAMETERS:  nothing
 *
 * RETURNED:    nothing
 *
 * REVISION HISTORY:
 *			Name	Date		Description
 *			----	----		-----------
 *			art	2/21/95		Initial Revision
 *
 ***********************************************************************/
static void StopApplication (void)
{
	ToDoSavePrefs();					// BGT

	// Send a frmSave event to all the open forms.
	FrmSaveAllForms ();
	
	// Close all the open forms.
	FrmCloseAllForms ();

	// Close the application's data file.
	DmCloseDatabase (ToDoDB);
}


/***********************************************************************
 *
 * FUNCTION:    SyncNotification
 *
 * DESCRIPTION: This routine is a entry point of the ToDo application.
 *              It is called when the ToDo application's database is 
 *              synchronized.  This routine will re-sort the database and
 *              reset the state file info if necessary.
 *
 * PARAMETERS:	 nothing
 *
 * RETURNED:	 nothing
 *
 * REVISION HISTORY:
 *			Name	Date		Description
 *			----	----		-----------
 *			art	6/6/95	Initial Revision
 *			jmp	10/4/99	Replaced call to DmOpenDatabaseByTypeCreator() with
 *								ToDoGetDatabase().
 *
 ***********************************************************************/
static void SyncNotification (void)
{
	Err err = errNone;
	char name [dmCategoryLength];
	DmOpenRef dbP;
	ToDoPreferenceType prefs;
	UInt16 prefsSize;

	// Open the application's data file.
	err = ToDoGetDatabase(&dbP, dmModeReadWrite);
	if (err)
		return;
	
	// Re-sort the database.
	ToDoSort (dbP);
	
	// Check if the currrent category still exists.
	prefsSize = sizeof (ToDoPreferenceType);
	if (PrefGetAppPreferences (sysFileCToDo, todoPrefID, &prefs, &prefsSize, true) != noPreferenceFound)
		{
		CategoryGetName (dbP, prefs.currentCategory, name);	
		if (*name == 0) 
			{
			prefs.currentCategory = dmAllCategories;
			prefs.showAllCategories = true;
	
			PrefSetAppPreferences (sysFileCToDo, todoPrefID, toDoVersionNum, &prefs, 
				sizeof (ToDoPreferenceType), true);
			}
		}

	DmCloseDatabase (dbP);
}

/***********************************************************************
 *
 * FUNCTION:     RegisterLocaleChangingNotification

 *
 * DESCRIPTION:  Register for NotifyMgr notifications for locale chagning.
 *				DOLATER : This function and the one above can be rolled into one.
 *						 
 *
 * PARAMETERS:   nothing
 *
 * RETURNED:     nothing
 *
 * REVISION HISTORY:
 *			Name	Date		Description
 *			----	----		-----------
 *			vivek	8/01/00	Initial Revision
 *
 ***********************************************************************/
static void RegisterLocaleChangingNotification(void)
{
	UInt16 cardNo;
	LocalID dbID;
	Err err;
				
	err = SysCurAppDatabase(&cardNo, &dbID);
	ErrNonFatalDisplayIf(err != errNone, "can't get app db info");
	if(err == errNone)
	{
		err = SysNotifyRegister(cardNo, dbID, sysNotifyLocaleChangedEvent, 
						NULL, sysNotifyNormalPriority, NULL);

#if EMULATION_LEVEL == EMULATION_NONE

⌨️ 快捷键说明

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