home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Palm Finder 2 / Src / App Sources / Finder.cpp < prev    next >
Encoding:
Text File  |  2001-06-23  |  7.0 KB  |  322 lines

  1. // Finder.cpp
  2.  
  3. #pragma mark Includes
  4. #include <Pilot.h>
  5. #include <SysEvtMgr.h>
  6. #include <SysAll.h>
  7. #include "Finder_res.h"
  8. #include "pane.h"
  9. #include "main_form.h"
  10. #include "drawing.h"
  11. #include "prefs.h"
  12. #include "events.h"
  13. #include "commander.h"
  14. #include "da_mgr.h"
  15.  
  16.  
  17. // Internal Structures
  18. struct FinderAppInfoType {
  19.     Byte replaceme;
  20. } ;
  21.  
  22. // Internal Constants
  23. const unsigned long appFileCreator = 'MACS';
  24. const unsigned short appVersionNum = 0x01;
  25. const unsigned short appPrefID = 0x00;
  26. const unsigned short appPrefVersionNum = 0x01;
  27. const unsigned long version30 = 0x03000000;
  28. const unsigned long version20 = 0x02000000;
  29.  
  30.  
  31. // globals
  32. static main_form*                     g_main_form=NULL;
  33.  
  34.  
  35. #pragma mark Prototypes
  36. // Prototypes
  37. Err        RomVersionCompatible(DWord requiredVersion, Word launchFlags);
  38. Err        AppStart(void);
  39. void        AppEventLoop(void);
  40. void        AppStop(void);
  41. Boolean AppHandleEvent(EventPtr eventP);
  42. //-
  43. Boolean    form_handle_event(EventPtr eventP);
  44.  
  45.  
  46. #pragma mark -
  47.  
  48. /***********************************************************************
  49.  *
  50.  * FUNCTION:    PilotMain
  51.  *
  52.  * DESCRIPTION: This is the main entry point for the application.
  53.  *
  54.  * PARAMETERS:  cmd - word value specifying the launch code. 
  55.  *              cmdPB - pointer to a structure that is associated with the launch code. 
  56.  *              launchFlags -  word value providing extra information about the launch.
  57.  * RETURNED:    Result of launch
  58.  *
  59.  * REVISION HISTORY:
  60.  *
  61.  *
  62.  ***********************************************************************/
  63. DWord PilotMain( Word cmd, Ptr cmdPBP, Word launchFlags)
  64. {
  65.     // variables
  66.     Err error;
  67.     
  68.     // check for ROM version 3.0 or greater
  69.     error = RomVersionCompatible (version30, launchFlags);
  70.     if (error) return (error);
  71.  
  72.     switch (cmd) {
  73.         case sysAppLaunchCmdNormalLaunch:
  74.             error = AppStart();
  75.             if (error) 
  76.                 return error;
  77.             AppEventLoop();
  78.             AppStop();
  79.             break;
  80.  
  81.         default:
  82.             break;
  83.     }
  84.     return 0;
  85. }
  86.  
  87. /***********************************************************************
  88.  *
  89.  * FUNCTION:    RomVersionCompatible
  90.  *
  91.  * DESCRIPTION: This routine checks that a ROM version is meet your
  92.  *              minimum requirement.
  93.  *
  94.  * PARAMETERS:  requiredVersion - minimum rom version required
  95.  *                                (see sysFtrNumROMVersion in SystemMgr.h 
  96.  *                                for format)
  97.  *              launchFlags     - flags that indicate if the application 
  98.  *                                UI is initialized.
  99.  *
  100.  * RETURNED:    error code or zero if rom is compatible
  101.  *
  102.  * REVISION HISTORY:
  103.  *
  104.  ***********************************************************************/
  105. Err RomVersionCompatible(DWord requiredVersion, Word launchFlags)
  106. {
  107.     DWord romVersion;
  108.  
  109.     // See if we're on in minimum required version of the ROM or later.
  110.     FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
  111.     if (romVersion < requiredVersion)
  112.         {
  113.         if ((launchFlags & (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) ==
  114.             (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp))
  115.             {
  116.             FrmAlert (RomIncompatibleAlert);
  117.         
  118.             // Pilot 1.0 will continuously relaunch this app unless we switch to 
  119.             // another safe one.
  120.             if (romVersion < version20)
  121.                 {
  122.                 // Err err;
  123.                 
  124.                 AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
  125.                 }
  126.             }
  127.         
  128.         return (sysErrRomIncompatible);
  129.         }
  130.  
  131.     return (0);
  132. }
  133.  
  134.  
  135. //
  136. // AppStart()
  137. //
  138. //    Read the saved state of the application. Return 0 if no error.
  139. Err AppStart(void)
  140. {
  141.     // variables
  142.     FinderPrefsType*    prefs = prefs::get_prefs();
  143.     Word                     prefsSize;
  144.  
  145.     // Read the saved preferences / saved-state information.
  146.     prefsSize = sizeof(FinderPrefsType);
  147.     if (PrefGetAppPreferences(appFileCreator, appPrefID, prefs, &prefsSize, true)== 
  148.         noPreferenceFound)
  149.     {
  150.         // defaults for when no prefs are found
  151.         prefs->trash_position.x = 140;
  152.         prefs->trash_position.y = 150;
  153.         prefs->show_startup = true;
  154.     }
  155.     
  156.     if (prefs->show_startup)
  157.         startup_sequence();
  158.     prefs->show_startup = false;
  159.     
  160.     // show the Finder
  161.     FrmGotoForm(MainForm);
  162.  
  163.     // constrain values
  164.     PointType* p = &prefs->trash_position;
  165.     if (p->x<0)
  166.         p->x = 0;
  167.     if (p->x>160)
  168.         p->x = 160;
  169.     if (p->y<10)
  170.         p->y = 10;
  171.     if (p->y>160)
  172.         p->y = 160;
  173.     
  174.    return 0;
  175. }
  176.  
  177.  
  178. //
  179. // AppStop()
  180. //
  181. // Save the current state of the application.
  182. void AppStop(void)
  183. {
  184.     // variables
  185.     FinderPrefsType*    prefs = prefs::get_prefs();
  186.  
  187.     // Write the saved preferences / saved-state information.  This data 
  188.     // will be backed up during a HotSync.
  189.     PrefSetAppPreferences (appFileCreator, appPrefID, appPrefVersionNum, 
  190.         prefs, sizeof (FinderPrefsType), true);
  191. }
  192.  
  193.  
  194. //
  195. // AppEventLoop()
  196. //
  197. void AppEventLoop(void)
  198. {
  199.     // variables
  200.     Word         error;
  201.     EventType     event;
  202.     Int32        sleep_time = 1 * SysTicksPerSecond(); // one second
  203.     
  204.     // instructions
  205.     do {
  206.         EvtGetEvent(&event, sleep_time);
  207.         if (! SysHandleEvent(&event))
  208.             if (! MenuHandleEvent(0, &event, &error))
  209.                 if (! AppHandleEvent(&event))
  210.                     FrmDispatchEvent(&event);
  211.         
  212.     } while (event.eType != appStopEvent);
  213.     
  214.     // dispose of main form
  215.     if (g_main_form != NULL) {
  216.         delete g_main_form;
  217.         g_main_form = NULL;
  218.     }
  219. }
  220.  
  221.  
  222.  
  223. /***********************************************************************
  224.  *
  225.  * FUNCTION:    AppHandleEvent
  226.  *
  227.  * DESCRIPTION: This routine loads form resources and set the event
  228.  *              handler for the form loaded.
  229.  *
  230.  * PARAMETERS:  event  - a pointer to an EventType structure
  231.  *
  232.  * RETURNED:    true if the event has handle and should not be passed
  233.  *              to a higher level handler.
  234.  *
  235.  * REVISION HISTORY:
  236.  *
  237.  *
  238.  ***********************************************************************/
  239. Boolean 
  240. AppHandleEvent( EventPtr eventP)
  241. {
  242.     Word formId;
  243.     FormPtr frmP;
  244.  
  245.  
  246.     if (eventP->eType == frmLoadEvent)
  247.         {
  248.         // Load the form resource.
  249.         formId = eventP->data.frmLoad.formID;
  250.         frmP = FrmInitForm(formId);
  251.         FrmSetActiveForm(frmP);
  252.  
  253.         // Set the event handler for the form.  The handler of the currently
  254.         // active form is called by FrmHandleEvent each time it receives an
  255.         // event.
  256.         switch (formId)
  257.             {
  258.             case MainForm:
  259.                 {
  260.                     commander::set_default_commander(NULL);
  261.                     g_main_form = new main_form(frmP);
  262.                     FrmSetEventHandler(frmP, form_handle_event);
  263.                     commander::set_top_commander(g_main_form);
  264.                 }
  265.                 break;
  266.  
  267.             default:
  268. //                ErrFatalDisplay("Invalid Form Load Event");
  269.                 break;
  270.  
  271.             }
  272.         return true;
  273.         }
  274.     
  275.     return false;
  276. }
  277.  
  278.  
  279. /***********************************************************************
  280.  *
  281.  * FUNCTION:    GetObjectPtr
  282.  *
  283.  * DESCRIPTION: This routine returns a pointer to an object in the current
  284.  *              form.
  285.  *
  286.  * PARAMETERS:  formId - id of the form to display
  287.  *
  288.  * RETURNED:    VoidPtr
  289.  *
  290.  * REVISION HISTORY:
  291.  *
  292.  *
  293.  ***********************************************************************/
  294. static VoidPtr GetObjectPtr(Word objectID)
  295.     {
  296.     FormPtr frmP;
  297.  
  298.     frmP = FrmGetActiveForm();
  299.  
  300.     return (FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, objectID)));
  301. }
  302.  
  303.  
  304.  
  305. #pragma mark -
  306.  
  307. //
  308. // form_handle_event()
  309. //
  310. Boolean    
  311. form_handle_event(EventPtr eventP) {
  312.     // variables
  313.     Boolean handled = false;
  314.  
  315.     if (g_main_form!=NULL)
  316.         handled = g_main_form->handle_event(eventP);
  317.  
  318.     return handled;
  319. }
  320.  
  321.  
  322.