我有一個程式萬年曆程式`~~~~
我要設定成`~~~~
假若我在某天設定重要事情時,
他能在那天到時發出聲音提醒並跳出那頁來~~~
要來出何做呢???
程式:
/************************************************************************
* Project: BookInfo *
* Author: Anne Yang *
* Description: Record book information. A Demo for database & field. *
* Note: Example of Chap. 8. *
************************************************************************/
#include <Pilot.h>
#include <SysEvtMgr.h>
#include "DateInfo_res.h"
/************************************************************************
* Internal Constants *
************************************************************************/
#define appFileCreator 'Unls'
#define DBCreatorID appFileCreator
#define DBTypeID 'Data'
#define DBName "DateInfoDB-Unls-DATA"
#define MAX_NAME_LEN 0x33
#define MAX_PLACE_LEN 0x33
#define MAX_NOTE_LEN 0x3D
#define DATE_TIME_START 0x00
#define NAME_START DATE_TIME_START+sizeof(DateTimeType)
#define PLACE_START NAME_START+MAX_NAME_LEN
#define NOTE_START PLACE_START+MAX_PLACE_LEN
#define FIRST_RECORD 0x00
/************************************************************************
* Internal Structures *
************************************************************************/
typedef struct
{
DateTimeType DateTime;
char DateName[MAX_NAME_LEN];
char DatePlace[MAX_PLACE_LEN];
char DateNote[MAX_NOTE_LEN];
} DateDBType;
typedef DateDBType * DateDBTypePtr;
/************************************************************************
* Global variables *
************************************************************************/
static DmOpenRef DateDBRef; // DB reference
static UInt16 CurrentRecord; // Current selected record
static UInt16 TotalRecordNo; // Total record count
static Boolean DirtyBit; // Record dirty bit
static VoidHand CurrentRecordH; // Current record handle
static DateTimeType DateTimeSelect; // Date selected by user
/************************************************************************
* Internal Functions *
************************************************************************/
/************************************************************************
* Name: GetObjectPtr *
* Description: Get a pointer to an object in the current form. *
* Input: objectID = ID of the object in question *
* Output: Pointer to the object *
************************************************************************/
static VoidPtr GetObjectPtr(Word objectID)
{
FormPtr frmP;
frmP = FrmGetActiveForm();
return (FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, objectID)));
}
/************************************************************************
* Name: SaveField *
* Description: Save the text string of the field to the database. *
* Input: RecordP = Pointer to the record *
* DataOffset = Data offset in the record *
* fieldP = Pointer to the FieldType struct of the field *
* Output: None *
************************************************************************/
static void SaveField(DateDBTypePtr RecordP, UInt32 DataOffset, FieldPtr fieldP)
{
CharPtr fieldTxtP;
fieldTxtP = FldGetTextPtr(fieldP);
DmWrite(RecordP, DataOffset, fieldTxtP, StrLen(fieldTxtP));
}
/************************************************************************
* Name: ShowField *
* Description: Display the text string of the database in the field *
* Input: RecordTxtP = Pointer to the text string in the record *
* fieldP = Pointer to the FieldType struct of the field *
* Output: None *
************************************************************************/
static void ShowField(FieldPtr fieldP, CharPtr RecordTxtP)
{
VoidHand NewFldTxtH, OldFldTxtH;
CharPtr NewFldTxtP;
OldFldTxtH = FldGetTextHandle(fieldP);
NewFldTxtH = MemHandleNew(StrLen(RecordTxtP) + 1);
NewFldTxtP = MemHandleLock(NewFldTxtH);
StrCopy(NewFldTxtP, RecordTxtP);
MemHandleUnlock(NewFldTxtH);
FldSetTextHandle(fieldP, NewFldTxtH);
FldDrawField(fieldP);
if (OldFldTxtH != NULL)
MemHandleFree(OldFldTxtH);
}
/************************************************************************
* Name: ShowDate *
* Description: Display the date of the record in the selector trigger. *
* Input: DateSelect = DateTimeType structure of selected date. *
* Output: None *
************************************************************************/
static void ShowDate(DateTimeType DateSelect)
{
char DateInString[dateStringLength];
if ((DateSelect.month != NULL) && (DateSelect.day != NULL))
{
DateToAscii(DateSelect.month, DateSelect.day, DateSelect.year,
(DateFormatType)PrefGetPreference(prefDateFormat), DateInString);
CtlSetLabel(GetObjectPtr(MainSelectorDateSelTrigger), DateInString);
DateTimeSelect.month = DateSelect.month;
DateTimeSelect.day = DateSelect.day;
DateTimeSelect.year = DateSelect.year;
}
else
CtlSetLabel(GetObjectPtr(MainSelectorDateSelTrigger), "MM/DD/YY"
}
/************************************************************************
* Name: ShowTime *
* Description: Display the time of the record in the selector trigger. *
* Input: TimeSelect = DateTimeType structure of selected date. *
* Output: None *
************************************************************************/
static void ShowTime(DateTimeType TimeSelect)
{
char TimeInString[timeStringLength];
if ((TimeSelect.hour != 0xFFFF) && (TimeSelect.minute != 0xFFFF))
{
TimeToAscii(TimeSelect.hour, TimeSelect.minute,
(TimeFormatType)PrefGetPreference(prefTimeFormat), TimeInString);
CtlSetLabel(GetObjectPtr(MainSelectorTimeSelTrigger), TimeInString);
DateTimeSelect.minute = TimeSelect.minute;
DateTimeSelect.hour = TimeSelect.hour;
}
else
CtlSetLabel(GetObjectPtr(MainSelectorTimeSelTrigger), "HH:MM"
}
/************************************************************************
* Name: SaveRecord *
* Description: Save current record in the database *
* Input: frmP = Pointer to the form *
* RecordIndex = Index of the record *
* Output: None *
************************************************************************/
static void SaveRecord(FormPtr frmP, UInt16 RecordIndex)
{
DateDBTypePtr RecordP;
Word ObjIndex;
ObjIndex = FrmGetFocus(frmP);
FrmSetFocus(frmP, noFocus);
CurrentRecordH = DmGetRecord(DateDBRef, RecordIndex);
RecordP = MemHandleLock(CurrentRecordH);
DmWrite(RecordP, DATE_TIME_START, [$DateTimeSelect, sizeof(DateTimeType))]
SaveField(RecordP, NAME_START, GetObjectPtr(MainFieldNameField));
SaveField(RecordP, PLACE_START, GetObjectPtr(MainFieldPlaceField));
SaveField(RecordP, NOTE_START, GetObjectPtr(MainFieldNoteField));
MemHandleUnlock(CurrentRecordH);
DmReleaseRecord(DateDBRef, RecordIndex, true);
DirtyBit = false;
FrmSetFocus(frmP, ObjIndex);
}
/************************************************************************
* Name: ShowRecord *
* Description: Display current record *
* Input: frmP = Pointer to the form *
* RecordIndex = Index of the record *
* Output: None *
************************************************************************/
static Err ShowRecord(FormPtr frmP, UInt16 RecordIndex)
{
DateDBTypePtr RecordP;
CurrentRecordH = DmQueryRecord(DateDBRef, RecordIndex);
if (CurrentRecordH == NULL)
return DmGetLastErr();
RecordP = MemHandleLock(CurrentRecordH);
ShowDate(RecordP->DateTime);
ShowTime(RecordP->DateTime);
ShowField(GetObjectPtr(MainFieldNameField), RecordP->DateName);
ShowField(GetObjectPtr(MainFieldPlaceField), RecordP->DatePlace);
ShowField(GetObjectPtr(MainFieldNoteField), RecordP->DateNote);
MemHandleUnlock(CurrentRecordH);
FrmSetFocus(frmP, FrmGetObjectIndex(frmP, MainFieldNameField));
return 0;
}
/************************************************************************
* Name: SetDate *
* Description: Save the user selected date into a temp varable. *
* Input: None *
* Output: None *
************************************************************************/
static void SetDate(void)
{
DateTimeType TempDate;
if ((DateTimeSelect.month == NULL) || (DateTimeSelect.day == NULL))
TimSecondsToDateTime(TimGetSeconds(), [$TempDate)]
else
{
TempDate.month = DateTimeSelect.month;
TempDate.day = DateTimeSelect.day;
TempDate.year = DateTimeSelect.year;
}
if(SelectDay(selectDayByDay, &(TempDate.month), &(TempDate.day),
&(TempDate.year), "Select Date")
{
DateTimeSelect.month = TempDate.month;
DateTimeSelect.day = TempDate.day;
DateTimeSelect.year = TempDate.year;
}
ShowDate(DateTimeSelect);
}
/************************************************************************
* Name: SetTime *
* Description: Save the user selected time into a temp varable. *
* Input: None *
* Output: None *
************************************************************************/
static void SetTime(void)
{
DateTimeType TempTime;
if ((DateTimeSelect.hour == 0xFFFF) || (DateTimeSelect.minute == 0xFFFF))
TimSecondsToDateTime(TimGetSeconds(), [$TempTime)]
else
{
TempTime.hour = DateTimeSelect.hour;
TempTime.minute = DateTimeSelect.minute;
}
if (SelectOneTime(&(TempTime.hour), &(TempTime.minute), "Select Time")
{
DateTimeSelect.minute = TempTime.minute;
DateTimeSelect.hour = TempTime.hour;
}
ShowTime(DateTimeSelect);
}
/************************************************************************
* Name: RecordEmpty *
* Description: Check if current record is empty *
* Input: RecordIndex = Index of the record *
* Output: true = Record is empty *
* false = Reocrd is not empty *
************************************************************************/
static Boolean RecordEmpty(UInt16 RecordIndex)
{
Boolean EmptyFlag = true;
DateDBTypePtr RecordP;
CurrentRecordH = DmQueryRecord(DateDBRef, RecordIndex);
if (CurrentRecordH != NULL)
{
RecordP = MemHandleLock(CurrentRecordH);
if ((StrLen(RecordP->DateName) != 0) ||
(StrLen(RecordP->DatePlace) != 0) ||
(StrLen(RecordP->DateNote) != 0) ||
(RecordP->DateTime.hour != noTime) ||
(RecordP->DateTime.minute != noTime) ||
(RecordP->DateTime.month != 0) ||
(RecordP->DateTime.day != 0) ||
(RecordP->DateTime.year != 0))
EmptyFlag = false;
MemHandleUnlock(CurrentRecordH);
}
return EmptyFlag;
}
/************************************************************************
* Name: CreateNewReocrd *
* Description: Create a new record *
* Input: None *
* Output: None *
************************************************************************/
static void CreateNewRecord(void)
{
DateDBTypePtr RecordP;
UInt32 RecordLen;
RecordLen = sizeof(DateDBType);
CurrentRecordH = DmNewRecord(DateDBRef, [$CurrentRecord, RecordLen)]
RecordP = MemHandleLock(CurrentRecordH);
DmSet(RecordP, 0, 6, 0xFF);
DmSet(RecordP, 6, RecordLen-6, 0);
MemHandleUnlock(CurrentRecordH);
DmReleaseRecord(DateDBRef, CurrentRecord, true);
DateTimeSelect.second = DateTimeSelect.minute = DateTimeSelect.hour = 0xFFFF;
TotalRecordNo++;
DirtyBit = true;
}
/************************************************************************
* Name: MainFormInit *
* Description: Initialize the Main Form *
* Input: frmP - Pointer to the form *
* Output: None *
************************************************************************/
static void MainFormInit(FormPtr frmP)
{
}
/************************************************************************
* Name: MainFormHandleEvent *
* Description: Event handler of Main Form *
* Input: eventP = pointer to an EventType structure *
* Output: true = The event has been handled *
* false = The event has not been handled *
************************************************************************/
static Boolean MainFormHandleEvent(EventPtr eventP)
{
FormPtr frmP;
frmP = FrmGetActiveForm();
switch (eventP->eType)
{
case frmOpenEvent:
MainFormInit(frmP);
FrmDrawForm (frmP);
if ((CurrentRecord == 0) && (TotalRecordNo == 0))
CreateNewRecord();
ShowRecord(frmP, CurrentRecord);
return true;
break;
case frmCloseEvent:
SaveRecord(frmP, CurrentRecord);
return true;
break;
case ctlSelectEvent:
switch (eventP->data.ctlSelect.controlID)
{
case MainButtonSaveButton:
SaveRecord(frmP, CurrentRecord);
return true;
break;
case MainButtonDeleteButton:
DmRemoveRecord(DateDBRef, CurrentRecord);
TotalRecordNo--;
if (CurrentRecord == TotalRecordNo)
{
CurrentRecord++;
CreateNewRecord();
}
DateTimeSelect.minute = DateTimeSelect.hour = 0xFFFF;
ShowRecord(frmP, CurrentRecord);
return true;
break;
case MainButtonPrevButton:
if (CurrentRecord > 0)
{
CurrentRecord--;
DateTimeSelect.minute = DateTimeSelect.hour = 0xFFFF;
}
ShowRecord(frmP, CurrentRecord);
return true;
break;
case MainButtonNextButton:
if (RecordEmpty(CurrentRecord) == false)
{
CurrentRecord++;
if (CurrentRecord == TotalRecordNo)
CreateNewRecord();
DateTimeSelect.minute = DateTimeSelect.hour = 0xFFFF;
}
ShowRecord(frmP, CurrentRecord);
return true;
break;
case MainSelectorDateSelTrigger:
SetDate();
return true;
break;
case MainSelectorTimeSelTrigger:
SetTime();
return true;
break;
default:
break;
}
break;
case fldEnterEvent:
DirtyBit = true;
return false;
break;
default:
break;
}
return false;
}
/************************************************************************
* Name: AppHandleEvent *
* Description: Load form resource and set its event handler *
* Input: eventP = pointer to an EventType structure *
* Output: true = The event has been handled *
* false = The event has not been handled *
************************************************************************/
static Boolean AppHandleEvent( EventPtr eventP)
{
Word formId;
FormPtr frmP;
if (eventP->eType == frmLoadEvent)
{
formId = eventP->data.frmLoad.formID;
frmP = FrmInitForm(formId);
FrmSetActiveForm(frmP);
switch (formId)
{
case MainForm:
FrmSetEventHandler(frmP, MainFormHandleEvent);
break;
default:
break;
}
return true;
}
return false;
}
/************************************************************************
* Name: AppEventLoop *
* Description: The event loop for the application *
* Input: None *
* Output: None *
************************************************************************/
static void AppEventLoop(void)
{
EventType event;
do {
EvtGetEvent([$event, evtWaitForever)]
if (! SysHandleEvent(&event))
if (! AppHandleEvent(&event))
FrmDispatchEvent([$event)]
} while (event.eType != appStopEvent);
}
/************************************************************************
* Name: AppOpenDB *
* Desciption: Open database. *
* Input: None *
* Output: Error code *
************************************************************************/
Err AppOpenDB(void)
{
Err DBErrCode = 0;
UInt16 DBOpenMode = dmModeReadWrite;
DateDBRef = DmOpenDatabaseByTypeCreator(DBTypeID, DBCreatorID, DBOpenMode);
if (!DateDBRef)
{
DBErrCode = DmCreateDatabase(0, DBName, DBCreatorID, DBTypeID, false);
if (DBErrCode == 0)
DateDBRef = DmOpenDatabaseByTypeCreator(DBTypeID, DBCreatorID, DBOpenMode);
}
TotalRecordNo = DmNumRecords(DateDBRef);
CurrentRecord = FIRST_RECORD;
return DBErrCode;
}
/************************************************************************
* Name: AppStart *
* Desciption: Open database. *
* Input: None *
* Output: Error code *
************************************************************************/
static Err AppStart(void)
{
return AppOpenDB();
}
/************************************************************************
* Name: AppStop *
* Description: Close forms and database of the application. *
* Input: None *
* Output: None *
************************************************************************/
static void AppStop(void)
{
FrmCloseAllForms();
DmCloseDatabase(DateDBRef);
}
/************************************************************************
* Name: PilotMain *
* Desciption: Main entry point for the application. *
* Input: cmd = Launch code *
* cmdPB = Pointer to launch code parameter block *
* launchFlags = Launch Flag *
* Output: Launch result *
************************************************************************/
DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
{
switch (cmd)
{
case sysAppLaunchCmdNormalLaunch:
AppStart();
FrmGotoForm(MainForm);
AppEventLoop();
AppStop();
break;
default:
break;
}
return 0;
} |