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

📄 contacts.c

📁 CD_《Palm OS编程实践》
💻 C
字号:
// CH.2 The super-include for the Palm OS
#include <Pilot.h>

// CH.3 Our resource file
#include "Contacts_res.h"

// CH.2 Prototypes for our event handler functions
static Boolean myHandleEvent( EventPtr event );
static Boolean menuEventHandler( EventPtr event );

// CH.3 Our field memory handle
static Handle htext;    // CH.3 Handle to the text in our edit field
#define HTEXT_SIZE 81   // CH.3 Size of our edit field

// CH.2 The main entry point
DWord PilotMain( Word cmd, Ptr, Word )
{
    FormPtr     form;   // CH.2 A pointer to our form structure */
    Handle      hsrc;   // CH.3 Handle to the string resource
    CharPtr     psrc;   // CH.3 Points to the text in the resource
    CharPtr     ptext;  // CH.3 Points to the text in the edit field
    Word        index;  // CH.3 A general purpose index
    FieldPtr    field;  // CH.3 Used for manipulating fields
    EventType   event;  // CH.2 Our event structure
    Word        error;  // CH.3 Error word for menu event handler

    // CH.2 If this is not a normal launch, don't launch
    if( cmd != sysAppLaunchCmdNormalLaunch )
        return( 0 );

    // CH.3 Get the initialization string resource handle
    hsrc = DmGetResource( strRsc, FieldInitString );

    // CH.3 Lock the resource, get the pointer
    psrc = MemHandleLock( hsrc );

    // CH.3 Allocate our field chunk
    htext = MemHandleNew( HTEXT_SIZE );
    if( htext == NULL )
        return( 0 );

    // CH.3 Lock the memory, get the pointer
    ptext = MemHandleLock( htext );

    // CH.3 Initialize it
    StrCopy( ptext, psrc );

    // CH.3 Unlock the field's memory
    MemHandleUnlock( htext );

    // CH.3 Unlock the resource's memory
    MemHandleUnlock( hsrc );

    // CH.3 Release the string resource
    DmReleaseResource( hsrc );

    // CH.2 Initialize our form
    form = FrmInitForm( ContactDetailForm );
    FrmSetEventHandler( form, myHandleEvent );
    FrmSetActiveForm( form );

    // CH.3 Get the index of our field
    index = FrmGetObjectIndex( form, ContactDetailFirstNameField );

    // CH.3 Get the pointer to our field
    field = FrmGetObjectPtr( form, index );

    // CH.3 Set the editable text
    FldSetTextHandle( field, htext );

    // CH.2 Draw the form
    FrmDrawForm( form );

    // CH.3 Set the focus to our field
    FrmSetFocus( form, index );

    // CH.2 Our event loop
    do
    {
        // CH.2 Get the next event
        EvtGetEvent( &event, -1 );

        // CH.2 Handle system events
        if( SysHandleEvent( &event ) )
            continue;

        // CH.3 Handle menu events
        if( MenuHandleEvent( NULL, &event, &error ) )
            continue;

        // CH.2 Handle form events
        FrmDispatchEvent( &event );
        
    // CH.2 If it's a stop event, exit
    } while( event.eType != appStopEvent );

    // CH.2 We're done
    return( 0 );
}

// CH.2 Our form handler function
static Boolean myHandleEvent( EventType* event )
{
	// CH.3 Parse menu events
	if( event->eType == menuEvent )
	{
		return( menuEventHandler( event ) );
	}

    // CH.2 We're done
    return( false );
} 

// CH.3 Handle menu events
Boolean menuEventHandler( EventPtr event )
{
	FormPtr		form;		// CH.3 A pointer to our form structure
	Word		index;		// CH.3 A general purpose control index
	FieldPtr	field;		// CH.3 Used for manipulating fields

	// CH.3 Get our form pointer
	form = FrmGetActiveForm();

	// CH.3 Erase the menu status from the display
	MenuEraseStatus( NULL );
			
	// CH.3 Handle graffiti help
	if( event->data.menu.itemID == EditGraffitiHelp )
	{
		// CH.3 Pop up the graffiti reference based on
		// the graffiti state
		SysGraffitiReferenceDialog( referenceDefault );
		return( true );
	}

	// CH.3 Get the index of our field
	index = FrmGetFocus( form );
			
	// CH.3 If there is no field selected, we're done
	if( index == noFocus )
		return( false );
			
	// CH.3 Get the pointer of our field
	field = FrmGetObjectPtr( form, index );
			
	// CH.3 Do the edit command
	switch( event->data.menu.itemID )
	{
		// CH.3 Undo
		case EditUndo:
			FldUndo( field );
		break;
		
		// CH.3 Cut
		case EditCut:
			FldCut( field );
		break;
				
		// CH.3 Copy
		case EditCopy:
			FldCopy( field );
		break;
			
		// CH.3 Paste
		case EditPaste:
			FldPaste( field );
		break;
				
		// CH.3 Select All
		case EditSelectAll:
		{
			// CH.3 Get the length of the string in the field
			Word length = FldGetTextLength( field );
				
			// CH.3 Sound an error if appropriate
			if( length == 0 )
			{
				SndPlaySystemSound( sndError );
				return( false );
			}
			
			// CH.3 Select the whole string
			FldSetSelection( field, 0, length );					
		}
		break;
				
		// CH.3 Bring up the keyboard tool
		case EditKeyboard:
			SysKeyboardDialogV10();
		break;
	}
	
	// CH.3 We're done	
	return( true );
}

⌨️ 快捷键说明

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