PALM开发教程-第十三章 再论用户界面 |
| 作者:palmheart 来源:palmheart.net 发布时间:2005-12-21 3:41:03 |
|
/ Copyright (c) 1999, Robert Mykland. All rights reserved. ////////////////////////////////////////////////////////////////////////////// ////////////// // Includes // ////////////// #include "app.h" // The definitions for this application #include "calc.h" // Calculator guts /////////////////////// // Global Prototypes // /////////////////////// void calcInit( void ); void calcStop( void ); Boolean calcFormEventHandler( EventPtr spEvent ); void calcDisplay( char* cpNumber ); void calcSignalError( void ); ///////////////////// // Local Variables // ///////////////////// static char cComma; // The comma symbol we're using static char cPoint; // The decimal point we're using static Handle hNumber; // Handle to the number in our edit field ////////////////////// // Global Functions // ////////////////////// //---------------------------------------------------------------------------- void calcInit( //---------------------------------------------------------------------------- // Initializes this module. //---------------------------------------------------------------------------- void ) //---------------------------------------------------------------------------- { DWord dChoice; char* cpNumber; // Find MathLib if( (SysLibFind( MathLibName, &MathLibRef ) == 0) || SysLibLoad( LibType, MathLibCreator, &MathLibRef ) || MathLibOpen( MathLibRef, MathLibVersion ) ) { FrmAlert( MathLibErrorAlert ); sendStopEvent(); } // Allocate our field chunk hNumber = MemHandleNew( MAX_NUMBER_SIZE ); // Lock the memory, get the pointer cpNumber = MemHandleLock( hNumber ); // Initialize it StrCopy( cpNumber, "0" ); // Unlock the field's memory MemHandleUnlock( hNumber ); // Get the number format dChoice = PrefGetPreference( prefNumberFormat ); // Handle the number format switch( dChoice ) { default: cComma = ','; cPoint = '.'; break; case nfPeriodComma: cComma = '.'; cPoint = ','; break; case nfSpaceComma: cComma = ' '; cPoint = ','; break; case nfApostrophePeriod: cComma = '\''; cPoint = '.'; break; case nfApostropheComma: cComma = '\''; cPoint = ','; break; } // We're done return; } //---------------------------------------------------------------------------- void calcStop( //---------------------------------------------------------------------------- // Cleans up stuff from this module. //---------------------------------------------------------------------------- void ) //---------------------------------------------------------------------------- { UInt uiUseCount; // Close MathLib MathLibClose( MathLibRef, &uiUseCount ); if( uiUseCount == 0 ) SysLibRemove( MathLibRef ); // Free the number field chunk MemHandleFree( hNumber ); // We're done return; } //---------------------------------------------------------------------------- Boolean calcFormEventHandler( //---------------------------------------------------------------------------- // Handles events for this form. // Returns true if it fully handled the event. //---------------------------------------------------------------------------- EventPtr spEvent ) //---------------------------------------------------------------------------- { // Handle the event switch( spEvent->eType ) { // The form is opened case frmOpenEvent: { FormPtr spForm; Word wIndex; FieldPtr spField; ControlPtr spButton; char caPoint[2]; // Get the field spForm = FrmGetActiveForm(); wIndex = FrmGetObjectIndex( spForm, calcNumberField ); spField = FrmGetObjectPtr( spForm, wIndex ); // Draw the field FldSetTextHandle( spField, hNumber ); FldDrawField( spField ); // Get the decimal point button wIndex = FrmGetObjectIndex( spForm, calcPointButton ); spButton = FrmGetObjectPtr( spForm, wIndex ); // Draw the decimal point button caPoint[0] = cPoint; caPoint[1] = '\0'; CtlSetLabel( spButton, caPoint ); } break; // A control was selected case ctlSelectEvent: { // Handle the different buttons switch( spEvent->data.ctlSelect.controlID ) { case calcAddButton: calcAdd(); break; case calcChangesignButton: calcChangeSign(); break; case calcClearButton: calcClear(); break; case calcDivideButton: calcDivide(); break; case calcDoneButton: { char* cpNumber; // Lock the memory, get the pointer cpNumber = MemHandleLock( hNumber ); // Copy to the clipboard ClipboardAddItem( clipboardText, cpNumber, StrLen( cpNumber ) ); // Unlock the field's memory MemHandleUnlock( hNumber ); // Exit the application sendStopEvent(); } break; case calcEightButton: calcAppend( 8 ); break; case calcEqualsButton: calcEquals(); break; case calcExponentButton: calcExponent(); break; case calcFieldborderButton: // Ignore this button return( true ); case calcFiveButton: calcAppend( 5 ); break; case calcFourButton: calcAppend( 4 ); break; case calcMultiplyButton: calcMultiply(); break; case calcNineButton: calcAppend( 9 ); break; case calcOneButton: calcAppend( 1 ); break; case calcPointButton: calcPoint(); break; case calcSevenButton: calcAppend( 7 ); break; case calcS |
| [] [返回上一页] [打 印] |
|
文章评论 |
