home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************************
- *
- * Copyright (c) Pierre TOUZEAU.
- * All rights reserved.
- *
- * PROJECT: CompteBon
- * FILE: CompteBon.c
- * AUTHOR: Pierre TOUZEAU 08 Aout 2000
- *
- * DECLARER: Starter
- *
- * DESCRIPTION:
- *
- *
- **********************************************************************/
- #include <Pilot.h>
- #include <SysEvtMgr.h>
- #include <MemoryMgr.h>
-
-
- #ifdef TARGET_FRENCH
- #include "CompteBon-FR-Rsc.c"
- #include "CompteBon-FR_res.h"
- #endif
-
- #ifdef TARGET_ENGLISH
- #include "CompteBon-EN-Rsc.c"
- #include "CompteBon-EN_res.h"
- #endif
-
-
- /***********************************************************************
- *
- * Entry Points
- *
- ***********************************************************************/
-
-
- /***********************************************************************
- *
- * Internal Structures
- *
- ***********************************************************************/
- typedef struct
- {
- Byte replaceme;
- } StarterPreferenceType;
-
- typedef struct
- {
- Byte replaceme;
- } StarterAppInfoType;
-
- typedef StarterAppInfoType* StarterAppInfoPtr;
-
-
- /***********************************************************************
- *
- * Global variables
- *
- ***********************************************************************/
- //static Boolean HideSecretRecords;
-
- #define NbValListInit 14
- #define MaxLenResult 512
-
- CharPtr InitResultFieldStr;
-
- static int dta1[7] = {0,1,7,12,16,19,21};
- static int dta2[7] = {0,6,11,15,18,20,21};
- static char* ListInit[NbValListInit] = {
- "1","2","3","4","5","6","7","8","9","10","25","50","75","100" };
-
- static char car[]="+-*/";
-
-
- Int16 TabInit[2][24];
-
- char TabLabel[6][5];
-
- CharPtr ResultStrValue[2];
- CharPtr alertStr1;
- CharPtr alertStr2;
-
- Int16 paramValues[6];
-
- UInt16 TabCtl[6] = {
- MainVar1PopTrigger,
- MainVar2PopTrigger,
- MainVar3PopTrigger,
- MainVar4PopTrigger,
- MainVar5PopTrigger,
- MainVar6PopTrigger
- };
-
- /***********************************************************************
- *
- * Internal Constants
- *
- ***********************************************************************/
- #define appFileCreator 'strt'
- #define appVersionNum 0x01
- #define appPrefID 0x00
- #define appPrefVersionNum 0x01
-
-
- // Define the minimum OS version we support
- #define ourMinVersion sysMakeROMVersion(2,0,0,sysROMStageRelease,0)
-
-
- /***********************************************************************
- *
- * Internal Functions
- *
- ***********************************************************************/
- Int16 abs (Int16 a)
- {
- return (a>0 ? a:-a);
- }
-
- /***********************************************************************
- *
- * FUNCTION: RomVersionCompatible
- *
- * DESCRIPTION: This routine checks that a ROM version is meet your
- * minimum requirement.
- *
- * PARAMETERS: requiredVersion - minimum rom version required
- * (see sysFtrNumROMVersion in SystemMgr.h
- * for format)
- * launchFlags - flags that indicate if the application
- * UI is initialized.
- *
- * RETURNED: error code or zero if rom is compatible
- *
- * REVISION HISTORY:
- *
- ***********************************************************************/
- static Err RomVersionCompatible(DWord requiredVersion, Word launchFlags)
- {
- DWord romVersion;
-
- // See if we're on in minimum required version of the ROM or later.
- FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
- if (romVersion < requiredVersion)
- {
- if ((launchFlags & (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) ==
- (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp))
- {
- FrmAlert (RomIncompatibleAlert);
-
- // Pilot 1.0 will continuously relaunch this app unless we switch to
- // another safe one.
- if (romVersion < sysMakeROMVersion(2,0,0,sysROMStageRelease,0))
- AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
- }
-
- return (sysErrRomIncompatible);
- }
-
- return (0);
- }
-
- /***********************************************************************
- *
- * FUNCTION: GetObjectPtr
- *
- * DESCRIPTION: This routine returns a pointer to an object in the
- * form FormP or in the current form if FormP is NULL.
- *
- * PARAMETERS: ObjectId - id of the object to search
- *
- * RETURNED: VoidPtr
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- VoidPtr GetObjectPtr( FormPtr formP, Word objectID)
- {
- FormPtr frmP;
-
- if ( formP != NULL )
- frmP = formP;
- else
- frmP = FrmGetActiveForm();
-
- return FrmGetObjectPtr( frmP, FrmGetObjectIndex( frmP, objectID ) );
-
- } // GetObjectPtr
-
-
- /***********************************************************************
- *
- * FUNCTION: SetNewTextInField
- *
- * DESCRIPTION: This routine modify the text in a field.
- *
- * PARAMETERS: frm - pointer to the form (Active form if NULL).
- * oid - Object field Index in the form
- * str - String to place in the field
- * RETURNED: nothing
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- void SetNewTextInField(FormPtr frm, UInt16 oid, char* str)
- {
- VoidPtr ObjP;
- Handle TextH;
- Handle NewTextH;
- char* TextP;
-
- if (frm == NULL)
- frm = FrmGetActiveForm();
-
-
- // get the index of the Field control
- ObjP = GetObjectPtr( frm, oid );
- // get the Mem TextHandle of the Field to set it
- TextH = FldGetTextHandle (ObjP);
- // Cut the link between the field and the TextHandle in order to
- // avoid sync problem (you type & your code both modify the values?)
- FldSetTextHandle (ObjP, NULL);
-
- // Follow the line !
- // Allocate new one, then Lock, then Modify, then Unlock...
- NewTextH = MemHandleNew (StrLen(str)+1);
- TextP = MemHandleLock (NewTextH);
- StrCopy (TextP, str);
- MemHandleUnlock (NewTextH);
- // and then put it back by
- // Re-establish the link between the text handle and the field
- FldSetTextHandle (ObjP, NewTextH);
- // Update the field
- FldDrawField (ObjP);
-
- // free the old TextHandle
- if (TextH != NULL)
- MemHandleFree( TextH);
-
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: Compte (count or compute in french...)
- *
- * DESCRIPTION: This routine compute the solution of the problem :
- * How to obtain the RESULT with 6 number combined with + - * / ?
- *
- * PARAMETERS: 6 input numbers and cible (french word for target).
- * and result the string answer
- * RETURNED: void
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- void Compte(CharPtr result, Int16 param[6], Int16 cible)
- {
- Int16 nb[22],res[22],tot[22],best,delta,new,diff,a,b;
- Int16 i,j,k,n,o,p,q,op[6],p1[7],p2[7],deb[7],fin[7];
- char tmpresult[MaxLenResult];
-
- // TAKE CARE ! nb array begins at 1 !!! (historical reason, i suppose...)
- for(i=0; i<6; i++) nb[i+1] = param[i];
- for (i=0; i<8 ;i++) {deb[i]=dta1[i];fin[i]=dta2[i];};
- // Bubble sort for input parameters
- j=0;
- while (j==0)
- {for (i=1,j=1;i<6;i++)
- {if ((p=nb[i])<(q=nb[i+1])) {nb[i]=q;nb[i+1]=p;j=0;} }
- }
-
- // StrPrintF(tmpresult,"%3d %3d %3d %3d %3d %3d",nb[1],nb[2],nb[3],nb[4],nb[5],nb[6]);
- // FrmCustomAlert (DisplayMsgAlert,"Variables d'entrΘe :",tmpresult," Proc Compte()");
-
- /****** appel de la fonction de calcul ***********************/
- /****** procedure COMPTE pour jeu ****************************/
- delta =1000;
- n=1;
- op[1]=0;p1[1]=deb[1];p2[1]=p1[1]+1;
- while (n)
- {while (p1[n] < fin[n])
- {while (p2[n] <= fin[n])
- {while (op[n] <4)
- { o=op[n]++; p=p1[n]; q=p2[n]; a=nb[p]; b=nb[q];
- switch (o)
- { case 0 : new=a+b; break;
- case 1 : if (a==b) goto pop; else new=a-b; break;
- case 2 : if (a==1 || b==1) goto pop; else new=a*b;break;
- case 3 : if (b==1) goto pop; else new=a/b;
- if (new*b != a ) goto pop;
- default: break;
- }
- diff=abs(cible-new); tot[n]=new;
- if (diff < delta)
- {i=1; j=1; res[1]=n;
- while (i<n)
- {res[++j]=nb[p1[i]]; res[++j]=nb[p2[i]];
- res[++j]=op[i]-1; res[++j]=tot[i++]; }
- res[j+1]=a; res[j+2]=b;
- res[j+3]=o; res[j+4]=new;
- best=new; delta=diff;
- if (diff==0) goto retour;
- }
- if (n<5)
- {i=deb[n+1];
- for (k=deb[n]; k<=fin[n]; k++)
- if (k!=p && k!=q)
- {if (new>nb[k]) {nb[i++]=new; new= -1;}
- nb[i++]=nb[k];
- }
- if (new>0) nb[i]=new;
- op[++n]=0; p1[n]=deb[n];
- p2[n]=p1[n]+1;
- }
- pop:;
- }
- p2[n]++;
- op[n]=0;
- }
- p1[n]++;
- p2[n]=p1[n]+1;
- op[n]=0;
- }
- n=n-1;
- }
- retour:;
-
- /****** impression des resultats du calcul de COMPTE *********/
- /* for(i=0;i<23;i++)printf("res=%d",res[i]); */
- n=res[1];
- p=(cible==best)? 1 : 0;
- StrPrintF(result,"%s %d\n",ResultStrValue[p],best);
- //StrPrintF(tmpresult,"%s %3d %3d %3d %3d %3d %3d",blanc,nb[1],nb[2],nb[3],nb[4],nb[5],nb[6]);
- //StrNCat(result, tmpresult, MaxLenResult);
- for (i=1;i<=n;i++)
- {a=res[i*4-2];b=res[i*4-1];
- o=res[i*4];new=res[i*4+1];
- k=a;p=b;
- for (j=1,q=0;j<=i+6;j++)
- { if(a==nb[j] && nb[j] !=0 && q<2) {a=-1;q++;nb[j]=0;}
- if(b==nb[j] && nb[j] !=0 && q<2) {b=-1;q++;nb[j]=0;}
- }
- nb[i+6]=new;
- StrPrintF(tmpresult,"\n%3d %c%3d =",k,car[o],p);
- StrNCat(result, tmpresult, MaxLenResult);
- //for (k=0; s[k]; k++) {
- // StrPrintF(tmpresult,"%c",s[k]);
- // StrNCat(result, tmpresult, MaxLenResult);
- // }
- StrPrintF(tmpresult," %3d ",new);
- StrNCat(result, tmpresult, MaxLenResult);
- }
- } // Compte
-
-
- /***********************************************************************
- *
- * FUNCTION: MainFormInit
- *
- * DESCRIPTION: This routine initializes the MainForm form.
- *
- * PARAMETERS: frm - pointer to the MainForm form.
- *
- * RETURNED: nothing
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- static void MainFormInit(FormPtr frmP)
- {
- VoidPtr ObjP;
- CharPtr initstr;
- int i;
-
- // About the global init of array TabInit
- for ( i=0; i<24 ; i++ ) TabInit[0][i] = 0;
- for ( i=0; i<10 ; i++ ) TabInit[1][i] = TabInit[1][i+10] = i+1;
- TabInit[1][20] = 25;
- TabInit[1][21] = 50;
- TabInit[1][22] = 75;
- TabInit[1][23] =100;
-
- // About the List Object
- // get the index of the List control
- ObjP = GetObjectPtr( frmP, MainListNumList );
- // Set the items text to the list
- LstSetListChoices(ObjP, ListInit, NbValListInit);
-
- // About the Total Field Object
- SetNewTextInField (frmP, MainTotalField, "888");
-
-
- // Get resource string for About alert
- alertStr1 = MemHandleLock (DmGetResource (strRsc, AlertBody1String));
- alertStr2 = MemHandleLock (DmGetResource (strRsc, AlertBody2String));
-
- // Get the resource string for the Result Field Object during computing
- InitResultFieldStr = MemHandleLock (DmGetResource (strRsc, InitResultString));
-
- // Get resource string for Result string "The count is ..right : "
- ResultStrValue[0] = MemHandleLock (DmGetResource (strRsc, BadResultString));
- ResultStrValue[1] = MemHandleLock (DmGetResource (strRsc, GoodResultString));
-
- // About the Result Field Object
- initstr = MemHandleLock (DmGetResource (strRsc, HelpResultFieldString));
- SetNewTextInField (frmP, MainResultField, initstr);
-
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: MainFormDoCommand
- *
- * DESCRIPTION: This routine performs the menu command specified.
- *
- * PARAMETERS: command - menu item id
- *
- * RETURNED: nothing
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- static Boolean MainFormDoCommand(Word command)
- {
- Boolean handled = false;
-
- switch (command)
- {
- #ifdef TARGET_FRENCH
- case MainOptionsAusujetduCompteestbon :
- #endif
- #ifdef TARGET_ENGLISH
- case MainOptionsAbouttheCountisright :
- #endif
- MenuEraseStatus (0);
- FrmCustomAlert (DisplayMsgAlert,alertStr1,alertStr2,
- "pierre.touzeau@wanadoo.fr");
- //AbtShowAbout (appFileCreator);
- handled = true;
- break;
-
- }
- return handled;
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: MainFormHandleEvent
- *
- * DESCRIPTION: This routine is the event handler for the
- * "MainForm" of this application.
- *
- * PARAMETERS: eventP - a pointer to an EventType structure
- *
- * RETURNED: true if the event has handle and should not be passed
- * to a higher level handler.
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- static Boolean MainFormHandleEvent(EventPtr eventP)
- {
- Boolean handled = false;
- FormPtr frmP=NULL;
- char tmp[MaxLenResult];
- int i, rnd, tabparam[6];
-
-
- switch (eventP->eType)
- {
- case menuEvent:
- return MainFormDoCommand(eventP->data.menu.itemID);
-
- case frmOpenEvent:
- frmP = FrmGetActiveForm();
- MainFormInit( frmP);
- FrmDrawForm ( frmP);
- handled = true;
- break;
- case ctlSelectEvent:
- {
- ControlPtr CtlP;
- FieldPtr FldP;
- //handled = true; //assume we'll handle it
- switch (eventP->data.ctlSelect.controlID)
- {
- case MainRandomTargetButton:
- // We randomly choose a new target(>100) and set the field
- rnd = 0;
- while (rnd<100)
- rnd = SysRandom(0) % 999;
- StrPrintF(tmp,"%3d",rnd);
- SetNewTextInField (frmP, MainTotalField, tmp);
- handled = true; //assume we'll handle it
- break;
- case MainRandomParamButton:
- // We compute the 6 params ramdomly,we choose value in the TabInit array
- // each value can only be choosed 1 time. we lit up and control the flag
- // stored in position [0] in the tabinit array.
- // When value ok, then we print the label in PopupTrigger control, we
- // already prepaped a 6 value array (TabCtl) containing the index of Ctl
- for (i=0; i<6; i++)
- {
- tabparam[i] = SysRandom (0) % 23 ;
- if ( TabInit[0][tabparam[i]]==0 )
- {
- TabInit[0][ tabparam[i] ]++;
- paramValues[i] = TabInit[1][ tabparam[i]];
- StrPrintF(TabLabel[i],"%d",paramValues[i]);
- CtlP = GetObjectPtr( frmP, TabCtl[i] );
- CtlSetLabel( CtlP, TabLabel[i]);
- //StrPrintF(tmp,"paramValues[%d]=%3d",i,paramValues[i]);
- //FrmCustomAlert (DisplayMsgAlert,"Variables d'entrΘe :",tmp," Evnt Tirage");
- }
- else
- i--;
- }
-
- handled = true; //assume we'll handle it
- break;
- case MainCalculButton:
- // first, we clear the result field
- SetNewTextInField (frmP, MainResultField, InitResultFieldStr);
-
- // then we prepare the input values for Compute procedure
- // (we get the index of the popup control and set the array value)
- for (i=0; i<6; i++)
- {
- CtlP = GetObjectPtr( frmP, TabCtl[i] );
- paramValues[i] = StrAToI( CtlGetLabel(CtlP) );
- }
- FldP = GetObjectPtr( frmP, MainTotalField );
- rnd = StrAToI( FldGetTextPtr(FldP));
-
- Compte(tmp, paramValues, rnd);
-
- SetNewTextInField (frmP, MainResultField, tmp);
-
- handled = true; //assume we'll handle it
- break;
- }
- } // end case ctlSelectEvent
- default:
- break;
-
- }
-
- return handled;
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: AppHandleEvent
- *
- * DESCRIPTION: This routine loads form resources and set the event
- * handler for the form loaded.
- *
- * PARAMETERS: event - a pointer to an EventType structure
- *
- * RETURNED: true if the event has handle and should not be passed
- * to a higher level handler.
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- static Boolean AppHandleEvent( EventPtr eventP)
- {
- Word formId;
- FormPtr frmP;
-
-
- if (eventP->eType == frmLoadEvent)
- {
- // Load the form resource.
- formId = eventP->data.frmLoad.formID;
- frmP = FrmInitForm(formId);
- FrmSetActiveForm(frmP);
-
- // Set the event handler for the form. The handler of the currently
- // active form is called by FrmHandleEvent each time is receives an
- // event.
- switch (formId)
- {
- case MainForm:
- FrmSetEventHandler(frmP, MainFormHandleEvent);
- break;
-
- default:
- // ErrFatalDisplay("Invalid Form Load Event");
- break;
-
- }
- return true;
- }
-
- return false;
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: AppEventLoop
- *
- * DESCRIPTION: This routine is the event loop for the application.
- *
- * PARAMETERS: nothing
- *
- * RETURNED: nothing
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- static void AppEventLoop(void)
- {
- Word error;
- EventType event;
-
-
- do {
- EvtGetEvent(&event, evtWaitForever);
-
-
- if (! SysHandleEvent(&event))
- if (! MenuHandleEvent(0, &event, &error))
- if (! AppHandleEvent(&event))
- FrmDispatchEvent(&event);
-
- } while (event.eType != appStopEvent);
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: AppStart
- *
- * DESCRIPTION: Get the current application's preferences.
- *
- * PARAMETERS: nothing
- *
- * RETURNED: Err value 0 if nothing went wrong
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- static Err AppStart(void)
- {
- StarterPreferenceType prefs;
- Word prefsSize;
-
-
- // Read the saved preferences / saved-state information.
- prefsSize = sizeof(StarterPreferenceType);
- if (PrefGetAppPreferences(appFileCreator, appPrefID, &prefs, &prefsSize, true) !=
- noPreferenceFound)
- {
- }
-
- return 0;
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: AppStop
- *
- * DESCRIPTION: Save the current state of the application.
- *
- * PARAMETERS: nothing
- *
- * RETURNED: nothing
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- static void AppStop(void)
- {
- StarterPreferenceType prefs;
-
-
- // Write the saved preferences / saved-state information. This data
- // will be backed up during a HotSync.
- PrefSetAppPreferences (appFileCreator, appPrefID, appPrefVersionNum,
- &prefs, sizeof (prefs), true);
- }
-
-
-
- /***********************************************************************
- *
- * FUNCTION: StarterPilotMain
- *
- * DESCRIPTION: This is the main entry point for the application.
- * PARAMETERS: cmd - word value specifying the launch code.
- * cmdPB - pointer to a structure that is associated with the launch code.
- * launchFlags - word value providing extra information about the launch.
- *
- * RETURNED: Result of launch
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- static DWord StarterPilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
- {
- Err error;
-
-
- error = RomVersionCompatible (ourMinVersion, launchFlags);
- if (error) return (error);
-
-
- switch (cmd)
- {
- case sysAppLaunchCmdNormalLaunch:
- error = AppStart();
- if (error)
- return error;
-
- FrmGotoForm(MainForm);
- AppEventLoop();
- AppStop();
- break;
-
- default:
- break;
-
- }
-
- return 0;
- }
-
-
- /***********************************************************************
- *
- * FUNCTION: PilotMain
- *
- * DESCRIPTION: This is the main entry point for the application.
- *
- * PARAMETERS: cmd - word value specifying the launch code.
- * cmdPB - pointer to a structure that is associated with the launch code.
- * launchFlags - word value providing extra information about the launch.
- * RETURNED: Result of launch
- *
- * REVISION HISTORY:
- *
- *
- ***********************************************************************/
- DWord PilotMain( Word cmd, Ptr cmdPBP, Word launchFlags)
- {
- return StarterPilotMain(cmd, cmdPBP, launchFlags);
- }
-
-