📄 gmdbview.c
字号:
/********************************************************************* FILE: GMDBView.c** DESCRIPTION: A generic database viewer for the Global Find Project.** VERSION: 2.0**********************************************************************/#include <PalmOS.h>#include "GMDBView.h"#include "GMDBViewRsc.h"#include "Utils.h"#include "UtilsRsc.h"/*********************************************************************** * FUNCTION: PrgTextCallback * * DESCRIPTION: The progress manager dialog update callback. * * RETURNED: true if the dialog should be updated ***********************************************************************/static Boolean PrgTextCallback // ( out ) update dialog?( PrgCallbackDataPtr dataP // ( in ) progress manager data structure pointer){ char tempStr [ 5 ]; StrCopy ( dataP->textP, "Reading database number " ); StrIToA ( tempStr, gVirDBIndex + 1 ); StrCat ( dataP->textP, tempStr ); return true;}/*********************************************************************** * FUNCTION: PrgStart * * DESCRIPTION: Start the database scanning process. * * RETURNED: nothing ***********************************************************************/static void PrgStart(){ gVirDBIndex = 0; gEvtTimer = 10; gPrgP = PrgStartDialogV31 ( prgTitle, &PrgTextCallback );}/*********************************************************************** * FUNCTION: PrgStop * * DESCRIPTION: Stop the database scanning process. * * RETURNED: nothing ***********************************************************************/static void PrgStop( void){ PrgStopDialog ( gPrgP, true ); gPrgP = NULL; gVirDBIndex = 0; gEvtTimer = evtWaitForever;}#pragma mark ----------------/*********************************************************************** * FUNCTION: GetRealDBIndex * * DESCRIPTION: Convert the virtual DB Index into a Card/Index pair. * * RETURNED: stores card # and DB index in passed addresses ***********************************************************************/static void GetRealDBIndex( Int16 virIndex, // ( in ) the virtual database number UInt16* cardNum, // ( out ) the actual card number UInt16* dbIndex // ( out) the actual database number) { UInt16 start = 0, end = 0, i = 0;// Scan thru the database counts to find the card and DB index// that the current virtual database index fits into. do { end = end + gDBCounts [ i ]; if ( virIndex >= start && virIndex < end ) { * cardNum = i; * dbIndex = virIndex - start; break; } else start = start + gDBCounts [ i++ ]; } while (( gDBCounts [ i ] > 0 ) && ( i < MAX_NUM_CARDS ));}/*********************************************************************** * FUNCTION: MoveForwardInDatabases * * DESCRIPTION: Names kinda says it all. * * RETURNED: nothing. * * SIDE EFFECT: Sets gVirDBIndex ***********************************************************************/static void MoveForwardInDatabases( UInt8 increment // ( in ) the # of databases to move forward) {// Check to see if at end of the database list if ( gVirDBIndex >= gTotDBsNum - 1 ) { // If so, and we're doing a progress dialog, turn it off if ( gPrgP ) { PrgStop (); } // Otherwise, tell the user we're at the end of the list else { SndPlaySystemSound ( sndWarning ); } } else {// Not at the end of the list, move forward gVirDBIndex = gVirDBIndex + increment; if ( gVirDBIndex >= gTotDBsNum - 1 ) { gVirDBIndex = ( Int16 ) gTotDBsNum - 1; } // Redraw the progress dialog or update the database// info on the main form, depending on current state. if ( gPrgP ) PrgUpdateDialog ( gPrgP, 0, ( UInt16 ) gVirDBIndex, NULL, true ); else MainFormDraw (); }}/*********************************************************************** * FUNCTION: MoveBackwardInDatabases * * DESCRIPTION: Refer to the function name. * * RETURNED: nothing. * * SIDE EEFFECT: Sets gVirDBIndex ***********************************************************************/static void MoveBackwardInDatabases( UInt8 increment // ( in ) the number of database to move backwards) {// Check to see if at beginning of all the databases if ( gVirDBIndex == 0 ) SndPlaySystemSound ( sndWarning ); else { // No, move backwards gVirDBIndex = gVirDBIndex - increment; if ( gVirDBIndex < 0 ) gVirDBIndex = 0; // Draw the new info on the main form MainFormDraw (); }}#pragma mark ----------------/*********************************************************************** * FUNCTION: MainFormInit * * DESCRIPTION: This routine initializes the main form. * * RETURNED: nothing ***********************************************************************/static void MainFormInit( FormPtr /*frmP */ // ( in ) pointer to the main form.){ return;}/*********************************************************************** * FUNCTION: MainFormDraw * * DESCRIPTION: This routine draws the contents of the main form. * * RETURNED: nothing ***********************************************************************/static void MainFormDraw( void){ UInt16 cardNum, dbIndex; LocalID dbID; Err error; UInt32 type, creator; Char nameStr [ 32 ]; Char tempStr [ 32 ]; UInt16 version; UInt32 creationDateSecs; DateTimeType creationDate; // Get the card number & database index GetRealDBIndex ( gVirDBIndex, &cardNum, &dbIndex ); // Get the database ID dbID = DmGetDatabase ( cardNum, dbIndex ); if ( dbID ) { // Get the database info error = DmDatabaseInfo ( cardNum, dbID, nameStr, NULL, &version, &creationDateSecs, NULL, NULL, NULL, NULL, NULL, &type, &creator ); if ( ! error ) { // Convert the various pieces of data for main form and// put them on the screen StrIToA ( tempStr, ( cardNum + 1 ) ); SetTextFieldWithString ( MainCardField, tempStr, true ); StrIToA ( tempStr, gMaxCardsNum ); SetTextFieldWithString ( MainNumCardsField, tempStr, true ); StrIToA ( tempStr, ( dbIndex + 1 )); SetTextFieldWithString ( MainDBIndexField, tempStr, true ); StrIToA ( tempStr, gDBCounts [ cardNum ] ); SetTextFieldWithString ( MainNumDBsField, tempStr, true ); SetTextFieldWithString( MainDBNameField, nameStr, true ); QuadToString ( type, tempStr ); SetTextFieldWithString ( MainTypeField, tempStr, true ); QuadToString ( creator, tempStr ); SetTextFieldWithString ( MainCreatorField, tempStr, true ); StrIToA ( tempStr, version ); SetTextFieldWithString ( MainVersionField, tempStr, true ); TimSecondsToDateTime( creationDateSecs, &creationDate ); DateToAscii ( ( UInt8 ) creationDate.month, ( UInt8 ) creationDate.day, ( UInt16 ) creationDate.year, dfDMYLong, tempStr ); SetTextFieldWithString ( MainDateField, tempStr, true ); } else FrmAlert ( CantGetDatabaseInfoAlert ); } else FrmAlert ( CantGetDatabaseInfoAlert );}/*********************************************************************** * FUNCTION: MainFormDone * * DESCRIPTION: This routine cleans up after the main form. * * RETURNED: nothing ***********************************************************************/static void MainFormDone( FormPtr frmP // ( in) Pointer to the main form){ if ( frmP != NULL ) { // Insert application-specific code first// Then do this as the very last thing FrmEraseForm ( frmP ); FrmDeleteForm ( frmP ); }}/*********************************************************************** * FUNCTION: MainFormDoMenuCommand * * DESCRIPTION: This routine handles menu commands. * * RETURNED: true if event handled ***********************************************************************/ static Boolean MainFormDoMenuCommand // ( out ) handled?( UInt16 command // ( in ) The ID of menu command to do){ Boolean handled = false; FormPtr frmP; MenuEraseStatus ( 0 ); switch ( command ) { case MainOptionsAboutGMDBViewer:// About dialog frmP = FrmInitForm ( AboutForm ); FrmDoDialog ( frmP ); FrmDeleteForm ( frmP ); handled = true; break;// Scan the databases case MainOptionsScantheDatabases: PrgStart (); handled = true; break; } return handled;}/*********************************************************************** * FUNCTION: MainFormHandleControl * * DESCRIPTION: This routine handles control events for the main form * * RETURNED: true if the event is handled. ***********************************************************************/static Boolean MainFormHandleControl // ( out ) event handled?( UInt16 controlID // ( in ) the ID of the control) { Boolean handled = true; switch( controlID ) { case MainNextButton: MoveForwardInDatabases ( 1 ); break; case MainBackButton: MoveBackwardInDatabases ( 1 ); break; case MainBigNextButton: MoveForwardInDatabases ( BIG_JUMP ); break; case MainBigPrevButton: MoveBackwardInDatabases ( BIG_JUMP ); break; default: handled = false; } return handled;}/*********************************************************************** * FUNCTION: MainFormHandleEvent * * DESCRIPTION: This routine is the event handler for main. * * RETURNED: true if the event is handled. ***********************************************************************/static Boolean MainFormHandleEvent // ( out ) event handled?( EventPtr eventP // ( in ) pointer to the event to handle){ Boolean handled = false; FormPtr frmP = FrmGetActiveForm (); if ( frmP ) { switch ( eventP->eType ) { // Form events case frmOpenEvent: MainFormInit ( frmP ); // ***** Falls thru to next case case frmUpdateEvent: FrmDrawForm ( frmP ); MainFormDraw (); handled = true; break; case frmCloseEvent: MainFormDone ( frmP ); handled = true; break;// Menu events case menuEvent: handled = MainFormDoMenuCommand ( eventP->data.menu.itemID ); break;// Control events case ctlSelectEvent: handled = MainFormHandleControl ( eventP->data.ctlSelect.controlID ); break; } } return handled;}#pragma mark ----------------/*********************************************************************** * FUNCTION: AppHandlePrgEvent * * DESCRIPTION: Handle progress manager events. * * RETURNED: nothing ***********************************************************************/ static void AppHandlePrgEvent( EventPtr eventP // ( in ) Pointer to an eventType){// On a nil event, scan to the next database if ( eventP->eType == nilEvent ) { MoveForwardInDatabases ( 1 ); } else { // Not a nil, some other user-generated event like a Cancel button perhaps if ( ! PrgHandleEvent ( gPrgP, eventP )) { // Ignore all other non Progress events except Cancel if ( PrgUserCancel ( gPrgP )) PrgStop (); } }}/*********************************************************************** * FUNCTION: AppHandleEvent * * DESCRIPTION: Loads a form's resources and set its event handler. * * RETURNED: true if the event is handled. ***********************************************************************/static Boolean AppHandleEvent // ( out ) event handled?( EventPtr eventP // ( in ) pointer to the event to handle){ Boolean handled = false; if ( eventP->eType == frmLoadEvent ) { // Form load event--initialize the form and make it the active form. UInt16 formId = eventP->data.frmLoad.formID; FormPtr frmP = FrmInitForm ( formId ); FrmSetActiveForm ( frmP );// Set the event handler for the form. switch ( formId ) { case MainForm: FrmSetEventHandler ( frmP, MainFormHandleEvent ); handled = true; break;// Insert other cases as needed for other forms. } } return handled;}/*********************************************************************** * FUNCTION: AppEventLoop * * DESCRIPTION: The main event loop for the application. * * RETURNED: nothing ***********************************************************************/static void AppEventLoop( void){ UInt16 error; EventType event; Boolean handled = false; do { EvtGetEvent ( &event, gEvtTimer );// Check for the progress manager if ( gPrgP ) { AppHandlePrgEvent ( &event ); handled = true; } // The progress manager is not active, use the standard System Event handler else handled = SysHandleEvent ( &event );// Not handled by either, assume it's some other type of event. if ( ! handled ) if (! MenuHandleEvent ( 0, &event, &error )) if (! AppHandleEvent ( &event )) FrmDispatchEvent ( &event ); } while (( event.eType != appStopEvent ) || ( gPrgP ));}/*********************************************************************** * FUNCTION: AppStart * * DESCRIPTION: Do whatever is necessary to get started, like read the app preferences. * * RETURNED: zero. * * SIDE EFFECT: sets globals gDBCounts, gTotDBsNum, and gMaxCardsNum. ***********************************************************************/static UInt32 AppStart // ( out ) error code or zero( void){ UInt16 i = 0; UInt32 result = 0;// Scan thru the storage cards fetching database counts do { gDBCounts [ i ] = DmNumDatabases ( i ); gTotDBsNum = gTotDBsNum + gDBCounts [ i ]; if ( gDBCounts [ i ] > 0 ) { gMaxCardsNum = i + 1; } } while (( gDBCounts [ i++ ] != 0 ) && ( i < MAX_NUM_CARDS )); return result;}/*********************************************************************** * FUNCTION: AppStop * * DESCRIPTION: Do whatever you need to do, like save the preferences. * * RETURNED: nothing ***********************************************************************/static void AppStop( void){ FrmCloseAllForms ();}/*********************************************************************** * FUNCTION: PilotMain * * DESCRIPTION: The application main entry point. * * RETURNED: 0 if launch & execution are successful, non-zero otherwise. ***********************************************************************/UInt32 PilotMain // ( out ) error code or zero( UInt16 cmd, // ( in ) the launch code MemPtr /* cmdPBP*/, // ( in ) pointer to the launch code structure UInt16 launchFlags // ( in ) extra launch info){ UInt32 error = 0;// Check the ROM version for compatibility. error = RomVersionCompatible ( MIN_ROM_VERSION, launchFlags); if ( error == 0 ){// ROM OK, check for the various launch codes as needed. switch (cmd) { case sysAppLaunchCmdNormalLaunch: error = AppStart (); if ( error == 0 ) { FrmGotoForm ( MainForm ); AppEventLoop (); AppStop (); } break; } } return error;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -