home *** CD-ROM | disk | FTP | other *** search
/ Hand Held Organizer Toolkit / walnutcreekcdrom-handheldorganizertoolkit-march1998.iso / PalmPilot / development / sample_source_code / wrist_srcs.c < prev   
C/C++ Source or Header  |  1997-12-30  |  10KB  |  369 lines

  1.  
  2.  
  3. /* WristRest - v0.1 isb@pobox.com, 7 Jan 1996. Cold and Grey */
  4.  
  5. #include <Pilot.h>                // all the system toolbox headers
  6. #include <SoundMgr.h>
  7.  
  8.  
  9. #include "WristRsc.h"        // application resource defines
  10.  
  11.  
  12. /***********************************************************************
  13.  * Prototypes for internal functions
  14.  **********************************************************************/
  15. static void StartApplication(void);
  16. static Boolean MainFormHandleEvent(EventPtr event);
  17. static void EventLoop(void);
  18. static void CheckUI();
  19.  
  20. static char workStr[20] = "";
  21. static char breakStr[20] = "";
  22.  
  23. static int minCount = 0;
  24. static char minStr[30] = "0 minutes left";
  25.  
  26. static char stoppedStr[30] = "Press START & begin work";
  27. static char workingStr[30] = "You should be WORKING!!!";
  28. static char restingStr[30] = "         Take a BREAK!!!";
  29. int x = 30;
  30. int y = 120;
  31.  
  32. static void SetPrefs(int workP, int breakP, int version);
  33. static void UpdatePrefs();
  34.  
  35. static UInt cardNo;
  36. LocalID appID;
  37.  
  38. typedef struct
  39.     {
  40.         int workPeriod;
  41.         int breakPeriod;
  42.         int version;
  43.     } WristPreferenceType;
  44.     
  45. WristPreferenceType prefs;
  46.  
  47. static void SetPrefs(int workP, int breakP, int version)
  48. {
  49.     prefs.workPeriod = workP;
  50.     prefs.breakPeriod = breakP;
  51.     prefs.version = version;
  52. }
  53.  
  54. static void UpdatePrefs()
  55. {
  56.     PrefSetAppPreferences('Wris', 1, &prefs, sizeof(WristPreferenceType));
  57. }
  58.  
  59. static void StartApplication(void)
  60. {
  61.     FormPtr    frm;
  62.     FieldPtr workPtr, breakPtr;
  63.  
  64.     // Initialize and draw the main memo pad form.
  65.     frm = FrmInitForm(mainForm);    
  66.     FrmSetActiveForm(frm);
  67.         
  68.     FrmDrawForm(frm);
  69.  
  70.     if (PrefGetAppPreferences('Wris', 1, &prefs, sizeof(WristPreferenceType)) == NULL)
  71.         SetPrefs(30,5,0);
  72.         
  73.     //Clear any existing alarms we've got
  74.     AlmSetAlarm(cardNo, appID, 0, 0, 0);
  75.  
  76.     WinDrawChars(stoppedStr, StrLen(stoppedStr), x, y);
  77.     WinDrawChars("i", 1, 153, 0);
  78.     
  79.     SysCurAppDatabase(&cardNo, &appID);
  80.     
  81.     //Put old text values back onto screen
  82.     workPtr = (FieldPtr)(FrmGetObjectPtr(frm, (FrmGetObjectIndex(frm, mainWorkField))));
  83.     breakPtr = (FieldPtr)(FrmGetObjectPtr(frm, (FrmGetObjectIndex(frm, mainBreakField))));
  84.  
  85.      StrIToA(workStr, prefs.workPeriod);
  86.      StrIToA(breakStr, prefs.breakPeriod);
  87.     FldInsert(workPtr, workStr, StrLen(workStr));
  88.     FldInsert(breakPtr, breakStr, StrLen(breakStr));
  89.  
  90.     FldSetDirty(workPtr, false);
  91.     FldSetDirty(breakPtr, false);
  92. }
  93.  
  94. void CheckUI()
  95. {
  96.     FormPtr frm;
  97.     FieldPtr workPtr, breakPtr;
  98.     int tempWork, tempBreak;
  99.                     
  100.     //Update the values from the UI...
  101.     frm = FrmGetActiveForm();
  102.     workPtr = (FieldPtr)(FrmGetObjectPtr(frm, (FrmGetObjectIndex(frm, mainWorkField))));
  103.     breakPtr = (FieldPtr)(FrmGetObjectPtr(frm, (FrmGetObjectIndex(frm, mainBreakField))));
  104.  
  105.      if (FldGetTextPtr(workPtr) == NULL)
  106.          tempWork = 0;
  107.      else
  108.          tempWork = StrAToI(FldGetTextPtr(workPtr));
  109.  
  110.      if (FldGetTextPtr(breakPtr) == NULL)
  111.          tempBreak = 0;
  112.      else
  113.          tempBreak = StrAToI(FldGetTextPtr(breakPtr));
  114.  
  115.     if (tempWork < 0)
  116.         tempWork = 0;
  117.         
  118.     if (tempBreak < 0)
  119.         tempBreak = 0;
  120.         
  121.      SetPrefs(tempWork, tempBreak, 1);
  122.      UpdatePrefs();
  123. }
  124.  
  125. /***********************************************************************
  126.  *
  127.  * FUNCTION:        MainFormHandleEvent
  128.  *
  129.  * DESCRIPTION:    Handles processing of events for the ╥main╙ form.
  130.  *
  131.  * PARAMETERS:        event    - the most recent event.
  132.  *
  133.  * RETURNED:        True if the event is handled, false otherwise.
  134.  *
  135.  ***********************************************************************/
  136. static Boolean MainFormHandleEvent(EventPtr event)
  137. {
  138.         Boolean        handled = false;
  139.  
  140.         if (event->eType == nilEvent)
  141.         {        
  142.             ULong nowTime, alarmTime;
  143.             DWord ref=0;
  144.             int tempCnt;
  145.             
  146.             //Lets look at the clock and see if we're on a new minute
  147.             nowTime = TimGetSeconds();
  148.             alarmTime = AlmGetAlarm(cardNo, appID, &ref);    
  149.             if (alarmTime)
  150.             {
  151.                 tempCnt  = ((alarmTime - nowTime) / 60) + 1;
  152.                 
  153.                 if (tempCnt != minCount)
  154.                 {
  155.                     minCount = tempCnt;
  156.                     WinEraseChars(minStr, StrLen(minStr), x+20, y+15);
  157.                      StrIToA(minStr, minCount);
  158.                      if (minCount==1)
  159.                          StrCat(minStr, " minute left  ");
  160.                      else
  161.                          StrCat(minStr, " minutes left ");
  162.                                      
  163.                     WinDrawChars(minStr, StrLen(minStr), x+20, y+15);
  164.                 }
  165.             }
  166.         }
  167.           if (event->eType == ctlSelectEvent)        // P2. process menu events for this form
  168.         {
  169.         
  170.             FormPtr frm;
  171.             ULong alarmTime;
  172.             
  173.             if (event->data.ctlEnter.controlID == startButton)
  174.             {    
  175.                 FormPtr frm;
  176.                 FieldPtr fld;
  177.                 FieldAttrType fldAttr;
  178.                 
  179.                  CheckUI();
  180.                  
  181.                 //Cancel any alarm...
  182.                 AlmSetAlarm(cardNo, appID, 0, 0, 0);
  183.             
  184.                 //Set a new alarm for now + workPeriod...
  185.                 alarmTime = (prefs.workPeriod * 60) + TimGetSeconds();
  186.                 AlmSetAlarm(cardNo, appID, 1, alarmTime, 0);
  187.                 WinEraseChars(stoppedStr, StrLen(stoppedStr), x, y);
  188.                 WinDrawChars(workingStr, StrLen(workingStr), x, y);
  189.  
  190.                 //Set the fields not editable
  191.                 frm = FrmGetActiveForm();
  192.                 fld = (FieldPtr)(FrmGetObjectPtr(frm, (FrmGetObjectIndex(frm, mainWorkField))));
  193.                 FldGetAttributes(fld, &fldAttr);
  194.                 fldAttr.editable = 0;
  195.                 FldSetAttributes(fld, &fldAttr);
  196.                 fld = (FieldPtr)(FrmGetObjectPtr(frm, (FrmGetObjectIndex(frm, mainBreakField))));
  197.                 FldGetAttributes(fld, &fldAttr);
  198.                 fldAttr.editable = 0;
  199.                 FldSetAttributes(fld, &fldAttr);
  200.                 
  201.                 handled = true;            
  202.             }
  203.             else
  204.             if (event->data.ctlEnter.controlID == stopButton)
  205.             {        
  206.                 FormPtr frm;
  207.                 FieldPtr fld;
  208.                 FieldAttrType fldAttr;
  209.                 
  210.                 CheckUI();
  211.                     
  212.                 //Cancel any alarm...
  213.                 AlmSetAlarm(cardNo, appID, 0, 0, 0);
  214.                 WinEraseChars(minStr, StrLen(minStr), x+20, y+15);
  215.                 WinEraseChars(workingStr, StrLen(workingStr), x, y);
  216.                 WinDrawChars(stoppedStr, StrLen(stoppedStr), x, y);
  217.  
  218.                 //Set the fields editable
  219.                 frm = FrmGetActiveForm();
  220.                 fld = (FieldPtr)(FrmGetObjectPtr(frm, (FrmGetObjectIndex(frm, mainWorkField))));
  221.                 FldGetAttributes(fld, &fldAttr);
  222.                 fldAttr.editable = 1;
  223.                 FldSetAttributes(fld, &fldAttr);
  224.                 fld = (FieldPtr)(FrmGetObjectPtr(frm, (FrmGetObjectIndex(frm, mainBreakField))));
  225.                 FldGetAttributes(fld, &fldAttr);
  226.                 fldAttr.editable = 1;
  227.                 FldSetAttributes(fld, &fldAttr);
  228.  
  229.                 handled = true;            
  230.             }
  231.         } 
  232.  
  233.     return(handled);
  234. }
  235.  
  236. /***********************************************************************
  237.  *
  238.  * FUNCTION:        EventLoop
  239.  *
  240.  * DESCRIPTION:    A simple loop that obtains events from the Event
  241.  *                        Manager and passes them on to various applications and
  242.  *                        system event handlers before passing them on to
  243.  *                        FrmHandleEvent for default processing.
  244.  *
  245.  * PARAMETERS:        None.
  246.  *
  247.  * RETURNED:        Nothing.
  248.  *
  249.  ***********************************************************************/
  250. static void EventLoop(void)
  251. {
  252.     EventType    event;
  253.     Word            error;
  254.     
  255.     do
  256.         {
  257.         // Get the next available event.
  258. //        EvtGetEvent(&event, evtWaitForever);
  259.         EvtGetEvent(&event, sysTicksPerSecond * 10000); //Not too often I hope...
  260.         
  261.         // Give the system a chance to handle the event.
  262.         if (! SysHandleEvent (&event))
  263.  
  264.             // P2. Give the menu bar a chance to update and handle the event.    
  265.             if (! MenuHandleEvent(NULL, &event, &error))
  266.  
  267.                 // Give the application a chance to handle the event.
  268.                 if (! MainFormHandleEvent(&event))
  269.     
  270.                     // Let the form object provide default handling of the event.
  271.                     FrmHandleEvent(FrmGetActiveForm(), &event);
  272.         } 
  273.     while (event.eType != appStopEvent);
  274.                                 // ** SPECIAL NOTE **
  275.                                 // In order for the Emulator to exit
  276.                                 // cleanly when the File|Quit menu option is
  277.                                 // selected, the running application
  278.                                 // must exit.  The emulator will generate an 
  279.                                 // ╥appStopEvent╙ when Quit is selected.
  280. }
  281.  
  282.  
  283. /***********************************************************************
  284.  *
  285.  * FUNCTION:        PilotMain
  286.  *
  287.  * DESCRIPTION:    This function is the equivalent of a main() function
  288.  *                        in standard ╥C╙.  It is called by the Emulator to begin
  289.  *                        execution of this application.
  290.  *
  291.  * PARAMETERS:        cmd - command specifying how to launch the application.
  292.  *                        cmdPBP - parameter block for the command.
  293.  *                        launchFlags - flags used to configure the launch.            
  294.  *
  295.  * RETURNED:        Any applicable error code.
  296.  *
  297.  ***********************************************************************/
  298. DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
  299. {
  300.  
  301.     if (cmd == sysAppLaunchCmdNormalLaunch)
  302.     {
  303.     
  304.         //We've just been called
  305.         
  306.         // Set up initial form.
  307.         StartApplication();
  308.         
  309.         // Start up the event loop.
  310.         EventLoop();
  311.     
  312.         CheckUI();    
  313.         
  314.         //Cancel any lingering alarms...
  315.         AlmSetAlarm(cardNo, appID, 0, 0, 0);        
  316.     }
  317.     else
  318.     if (cmd == sysAppLaunchCmdAlarmTriggered)
  319.     {
  320.         DWord ref;
  321.         ULong forUs;
  322.         ULong alarmTime;
  323.         
  324.         ref = ((SysAlarmTriggeredParamType *)cmdPBP)->ref;
  325.         
  326.         if (!ref)
  327.         {
  328.             WinDrawChars("Boo!", 4, 30, 30);
  329.             return 0;
  330.         }
  331.             
  332.         if (ref == 1) //The end of work time
  333.         {
  334.             //Cancel any alarm...
  335.             AlmSetAlarm(cardNo, appID, 0, 0, 0);
  336.         
  337.             //Set a new alarm for now + breakPeriod...
  338.             alarmTime = (prefs.breakPeriod * 60) + TimGetSeconds();
  339.             AlmSetAlarm(cardNo, appID, 2, alarmTime, 0);    
  340.             
  341.             //Tell us to take a break!
  342.             WinEraseChars(minStr, StrLen(minStr), x+20, y+15);
  343.             WinEraseChars(workingStr, StrLen(workingStr), x, y);
  344.             WinDrawChars(restingStr, StrLen(restingStr), x, y);
  345.             SndPlaySystemSound(sndAlarm);
  346.         }
  347.         else
  348.         if (ref == 2)
  349.         {
  350.             //Cancel any alarm...
  351.             AlmSetAlarm(cardNo, appID, 0, 0, 0);
  352.         
  353.             //Set a new alarm for now + workPeriod...
  354.             alarmTime = (prefs.workPeriod * 60) + TimGetSeconds();
  355.             AlmSetAlarm(cardNo, appID, 1, alarmTime, 0);
  356.             WinEraseChars(minStr, StrLen(minStr), x+20, y+15); 
  357.             WinEraseChars(restingStr, StrLen(restingStr), x, y);
  358.             WinDrawChars(workingStr, StrLen(workingStr), x, y);
  359.             SndPlaySystemSound(sndAlarm);
  360.         }
  361.     }
  362.         
  363.     return(0);
  364.  
  365. }
  366.  
  367.  
  368.  
  369.