home *** CD-ROM | disk | FTP | other *** search
/ Freelog 42 / Freelog042.iso / Palm / Millikey / evplugbase / EXAMPLE / EXAMPLE1 / SRC / EXAMPLE1.C next >
C/C++ Source or Header  |  1998-12-05  |  13KB  |  462 lines

  1. /*******************************************************************
  2.  *
  3.  *    Copyright (c) 1998, EVSoft co., Ltd., All Rights Reserved
  4.  *
  5.  *------------------------------------------------------------------
  6.  * FileName:
  7.  *        Example1.c
  8.  *
  9.  * Description:
  10.  *        This is a PlugBase sample, which shows how to create a plugin for PalmOS.
  11.  *
  12.  *      Email:  gzevsoft@public.guangzhou.gd.cn
  13.  *
  14.  * History:
  15.  *       9/20/98  - Created by Winter Du
  16.  *
  17.  *******************************************************************/
  18.  
  19. #include <Pilot.h>                        // all the system toolbox headers
  20.  
  21. #include "..\..\..\Src\PlugBase.h"        // PlugBase defines
  22. #include "Example1Rsc.h"                // application resource defines
  23.  
  24.  
  25. /***********************************************************************
  26.  * Structures for this module
  27.  **********************************************************************/
  28.  
  29. /***********************************************************************
  30.  * Global defines for this module
  31.  **********************************************************************/
  32. #define version20               0x02000000    // ROM Version
  33. #define appCreator            'pbE1'        // app's creator
  34.  
  35. #define SoftPenStroke1ID    0
  36. #define SoftPenStroke2ID    1
  37.  
  38. #define PenStroke1From        findButton
  39. #define PenStroke1To        graffitiLetterUpArea
  40. #define PenStroke2From        findButton
  41. #define PenStroke2To        graffitiNumberDownArea
  42.  
  43. /***********************************************************************
  44.  * Global variables for this module
  45.  **********************************************************************/
  46.  
  47. /***********************************************************************
  48.  * Prototypes for internal functions
  49.  **********************************************************************/
  50. static void  msgBox (CharPtr msg, ULong delay);
  51. static PlugBaseAPIPtr getAPIPtr ();
  52.  
  53. static void uninstallSoftPenStrokeOfExample (PlugBaseAPIPtr apiP);
  54. static void installSoftPenStrokeOfExample (
  55.     PlugBaseAPIPtr apiP,
  56.     ScreenAreaType from1, ScreenAreaType to1,
  57.     ScreenAreaType from2, ScreenAreaType to2);
  58.  
  59. typedef void (*SysKeyboardDialogV10Ptr) (void);
  60. typedef void (*SysKeyboardDialogPtr) (KeyboardType kbd);
  61.  
  62. /***********************************************************************
  63.  *
  64.  * FUNCTION:    getAPIPtr
  65.  *
  66.  * DESCRIPTION: Get PlugBase API pointer
  67.  *
  68.  * PARAMETERS:  nothing
  69.  *
  70.  * RETURNED:    PlugBase API pointer
  71.  *              0 - PlugBase doesn't exist or doesn't activated
  72.  *
  73.  * REVISION HISTORY:
  74.  *            Name    Date        Description
  75.  *            ----    ----        -----------
  76.  *            Winter    9/20/98        Initial Revision
  77.  *
  78.  ***********************************************************************/
  79. static PlugBaseAPIPtr
  80. getAPIPtr ()
  81. {
  82.     PlugBaseAPIPtr    apiP;
  83.  
  84.     PBGetAPIPtr (apiP);
  85.  
  86.     ErrNonFatalDisplayIf (apiP == 0, "You must install EVPlugBase and active it first!");
  87.  
  88.     return apiP;
  89. }
  90.  
  91. static VoidPtr
  92. getObjectPtrByID (FormPtr frmP, Word objID)
  93. {
  94.     return FrmGetObjectPtr (frmP, FrmGetObjectIndex (frmP, objID));
  95. }
  96.  
  97. inline static ListPtr
  98. getListPtrByID (FormPtr frmP, Word objID)
  99. {
  100.   return (ListPtr) getObjectPtrByID(frmP, objID);
  101. }
  102.  
  103. inline static ControlPtr
  104. getControlPtrByID (FormPtr frmP, Word objID)
  105. {
  106.   return (ControlPtr) getObjectPtrByID(frmP, objID);
  107. }
  108.  
  109. static void
  110. setPopListByID (FormPtr frmP, Word popTriggerID, Word listID, Word newValue)
  111. {
  112.     ListPtr        lstP;
  113.     ControlPtr    ctlP;
  114.     
  115.     lstP = getListPtrByID (frmP, listID);
  116.     LstSetSelection (lstP, newValue);
  117.     ctlP = getControlPtrByID (frmP, popTriggerID);
  118.     CtlSetLabel (ctlP, LstGetSelectionText (lstP, newValue));
  119. }
  120.  
  121. static Word
  122. getListSelectionByID (FormPtr frmP, Word objID)
  123. {
  124.     return LstGetSelection (getListPtrByID (frmP, objID));
  125. }
  126.  
  127. static void
  128. msgBox (CharPtr msg, ULong delay)
  129. {
  130.     Int                msgLen;
  131.     Word            error;
  132.     RectangleType    rect;
  133.     RectangleType    rectFrame;
  134.     WinHandle        saveWin;
  135.  
  136.     if (msg == 0)
  137.         msg = "No message...";
  138.         
  139.     if (delay < 10)
  140.         delay = 10;
  141.  
  142.     msgLen = StrLen (msg);
  143.  
  144.     rectFrame.extent.x  = FntCharsWidth (msg, msgLen) + 8;
  145.     rectFrame.extent.y  = FntCharHeight () + 6;
  146.     rectFrame.topLeft.x = (160-(SWord)rectFrame.extent.x)/2;
  147.     rectFrame.topLeft.y = (160-(SWord)rectFrame.extent.y)/2;
  148.  
  149.     saveWin = WinSaveBits (&rectFrame, &error);
  150.  
  151.     RctSetRectangle (&rect, rectFrame.topLeft.x+2, rectFrame.topLeft.y+2,
  152.                             rectFrame.extent.x-4, rectFrame.extent.y-4);
  153.  
  154.     WinEraseRectangle (&rect, 0);
  155.     WinDrawRectangleFrame (dialogFrame, &rect);
  156.  
  157.     WinDrawChars (msg, msgLen, rectFrame.topLeft.x+4, rectFrame.topLeft.y+3);
  158.  
  159.     SysTaskDelay (delay);
  160.     
  161.     if (saveWin)
  162.         WinRestoreBits (saveWin, rectFrame.topLeft.x, rectFrame.topLeft.y);
  163. }
  164.  
  165.  
  166. static void
  167. doDialog (Word frmID)
  168. {
  169.     DmOpenRef        pluginDB;
  170.     FormPtr            frm;
  171.  
  172.     pluginDB = DmOpenDatabaseByTypeCreator (PlugBasePluginType, appCreator, dmModeReadOnly);
  173.  
  174.     frm = FrmInitForm (frmID);
  175.     FrmDoDialog (frm);
  176.     FrmDeleteForm (frm);
  177.  
  178.     DmCloseDatabase (pluginDB);
  179. }
  180.  
  181. /***********************************************************************
  182.  *
  183.  * FUNCTION:    mySysKeyboardDialogV10
  184.  *
  185.  * DESCRIPTION: SysKeyboardDialog patch code for PalmOS 1.0
  186.  *
  187.  * PARAMETERS:  nothing
  188.  *
  189.  * RETURNED:    nothing
  190.  *
  191.  * REVISION HISTORY:
  192.  *            Name    Date        Description
  193.  *            ----    ----        -----------
  194.  *            Winter    9/20/98        Initial Revision
  195.  *
  196.  ***********************************************************************/
  197. static void
  198. mySysKeyboardDialogV10 (void)
  199. {
  200.     SysKeyboardDialogV10Ptr    pSysKeyboardDialogV10;
  201.  
  202.     // Do something here
  203.     // .....
  204.     msgBox ("Waiting for Keyboard", 200);
  205.  
  206.     // Call old Trap address of SysKeyboardDialog
  207.     FtrGet (appCreator, sysTrapSysKeyboardDialogV10, (DWordPtr)(&pSysKeyboardDialogV10));
  208.     (*pSysKeyboardDialogV10) ();
  209. }
  210.  
  211. /***********************************************************************
  212.  *
  213.  * FUNCTION:    mySysKeyboardDialog
  214.  *
  215.  * DESCRIPTION: SysKeyboardDialog patch code for PalmOS 2.0 or later
  216.  *
  217.  * PARAMETERS:  kbd
  218.  *
  219.  * RETURNED:    nothing
  220.  *
  221.  * REVISION HISTORY:
  222.  *            Name    Date        Description
  223.  *            ----    ----        -----------
  224.  *            Winter    9/20/98        Initial Revision
  225.  *
  226.  ***********************************************************************/
  227. static void
  228. mySysKeyboardDialog (KeyboardType kbd)
  229. {
  230.     SysKeyboardDialogPtr    pSysKeyboardDialog;
  231.  
  232.     // Do something here
  233.     // .....
  234.     msgBox ("Waiting for Keyboard", 200);
  235.  
  236.     // Call old Trap address of SysKeyboardDialog
  237.     FtrGet (appCreator, sysTrapSysKeyboardDialog, (DWordPtr)(&pSysKeyboardDialog));
  238.     (*pSysKeyboardDialog) (kbd);
  239. }
  240.  
  241.  
  242. static void
  243. installSoftPenStrokeOfExample (
  244.     PlugBaseAPIPtr apiP,
  245.     ScreenAreaType from1, ScreenAreaType to1,
  246.     ScreenAreaType from2, ScreenAreaType to2)
  247. {
  248.     SoftPenStrokeType    softPenStroke;
  249.  
  250.     if (!apiP) return;
  251.  
  252.     softPenStroke.type    = PlugBasePluginType;
  253.     softPenStroke.creator = appCreator;
  254.  
  255.     softPenStroke.id      = SoftPenStroke1ID;
  256.     softPenStroke.penFrom = from1;
  257.     softPenStroke.penTo   = to1;
  258.     (*(apiP->installSoftPenStroke)) (&softPenStroke);
  259.  
  260.     softPenStroke.id      = SoftPenStroke2ID;
  261.     softPenStroke.penFrom = from2;
  262.     softPenStroke.penTo   = to2;
  263.     (*(apiP->installSoftPenStroke)) (&softPenStroke);
  264. }
  265.  
  266. static void
  267. uninstallSoftPenStrokeOfExample (PlugBaseAPIPtr apiP)
  268. {
  269.     SoftPenStrokeType    softPenStroke;
  270.  
  271.     if (!apiP) return;
  272.  
  273.     softPenStroke.type    = PlugBasePluginType;
  274.     softPenStroke.creator = appCreator;
  275.  
  276.     softPenStroke.id      = SoftPenStroke1ID;
  277.     (*(apiP->uninstallSoftPenStroke)) (&softPenStroke);
  278.  
  279.     softPenStroke.id      = SoftPenStroke2ID;
  280.     (*(apiP->uninstallSoftPenStroke)) (&softPenStroke);
  281. }
  282.  
  283. static void
  284. configFormInit (FormPtr frm)
  285. {
  286.     PlugBaseAPIPtr apiP   = getAPIPtr ();
  287.  
  288.     if (apiP)
  289.     {
  290.         (*(apiP->setSoftPenStrokeListChoices)) (getListPtrByID (frm, ConfigFrom1List), areaFrom, true);
  291.         (*(apiP->setSoftPenStrokeListChoices)) (getListPtrByID (frm, ConfigTo1List),   areaTo,   true);
  292.  
  293.         (*(apiP->setSoftPenStrokeListChoices)) (getListPtrByID (frm, ConfigFrom2List), areaFrom, true);
  294.         (*(apiP->setSoftPenStrokeListChoices)) (getListPtrByID (frm, ConfigTo2List),   areaTo,   true);
  295.     }
  296.  
  297.     setPopListByID (frm, ConfigFrom1PopTrigger,   ConfigFrom1List,   PenStroke1From);
  298.     setPopListByID (frm, ConfigTo1PopTrigger,     ConfigTo1List,     PenStroke1To);
  299.  
  300.     setPopListByID (frm, ConfigFrom2PopTrigger,   ConfigFrom2List,   PenStroke2From);
  301.     setPopListByID (frm, ConfigTo2PopTrigger,     ConfigTo2List,     PenStroke2To);
  302. }
  303.  
  304. static void
  305. configFromSave (FormPtr frm)
  306. {
  307.     ScreenAreaType    from1, to1;
  308.     ScreenAreaType    from2, to2;
  309.     PlugBaseAPIPtr    apiP;
  310.  
  311.     from1 = (ScreenAreaType) getListSelectionByID (frm, ConfigFrom1List);
  312.     to1   = (ScreenAreaType) getListSelectionByID (frm, ConfigTo1List);
  313.  
  314.     from2 = (ScreenAreaType) getListSelectionByID (frm, ConfigFrom2List);
  315.     to2   = (ScreenAreaType) getListSelectionByID (frm, ConfigTo2List);
  316.  
  317.     apiP = getAPIPtr ();
  318.     if (apiP && (*(apiP->pluginEnabled)) (PlugBasePluginType, appCreator))
  319.         installSoftPenStrokeOfExample (apiP, from1, to1, from2, to2);
  320. }
  321.  
  322. static void
  323. doConfig ()
  324. {
  325.     DmOpenRef        pluginDB;
  326.     FormPtr            frm;
  327.  
  328.     pluginDB = DmOpenDatabaseByTypeCreator (PlugBasePluginType, appCreator, dmModeReadOnly);
  329.  
  330.     frm = FrmInitForm (ConfigForm);
  331.  
  332.     configFormInit (frm);
  333.  
  334.     if (FrmDoDialog (frm) == ConfigOKButton)
  335.         configFromSave (frm);
  336.  
  337.     FrmDeleteForm (frm);
  338.  
  339.     DmCloseDatabase (pluginDB);
  340. }
  341.  
  342. /***********************************************************************
  343.  *
  344.  * FUNCTION:    PilotMain
  345.  *
  346.  * DESCRIPTION: This is the main entry point for the Example1 application.
  347.  *
  348.  * PARAMETERS:  cmd
  349.  *              cmdPBP
  350.  *              launchFlags
  351.  *
  352.  * RETURNED:    nothing
  353.  *
  354.  * REVISION HISTORY:
  355.  *            Name    Date        Description
  356.  *            ----    ----        -----------
  357.  *            Winter    9/20/98        Initial Revision
  358.  *
  359.  ***********************************************************************/
  360. DWord
  361. PilotMain (Word cmd, Ptr cmdPBP, Word launchFlags)
  362. {
  363.     PBLaunchParameterPtr    pmP = (PBLaunchParameterPtr) cmdPBP;
  364.  
  365.     // Get detailed imformation of the plugin by PlugBase
  366.     if (cmd == PBLaunchCmd_GetInformation)
  367.     {
  368.         // This plugin support information panel
  369.         // and can response PBLaunchCmd_ActivePluginInfoPanel Launch code
  370.         pmP->data.getInformation.supportInfoPanel   = true;
  371.  
  372.         // This plugin support config panel
  373.         // and can response PBLaunchCmd_ActivePluginControlPanel Launch code
  374.         pmP->data.getInformation.supportConfigPanel = true;
  375.         pmP->err = 0;
  376.     }
  377.  
  378.     // Active infomation panel
  379.     else if (cmd == PBLaunchCmd_ActivePluginInfoPanel)
  380.     {
  381.         doDialog (AboutForm);
  382.         pmP->err = 0;
  383.     }
  384.  
  385.     // Active Control panel
  386.     else if (cmd == PBLaunchCmd_ActivePluginControlPanel)
  387.     {
  388.         doConfig ();
  389.         pmP->err = 0;
  390.     }
  391.  
  392.     // PlugBase send these Launch code to Attach/Detach plugin
  393.     else if (cmd == PBLaunchCmd_PluginAttach || cmd == PBLaunchCmd_PluginDetach)
  394.     {
  395.         DmOpenRef            pluginDB;
  396.         VoidHand            codeResH;
  397.         HookTrapType        trapItem;
  398.         DWord                romVersion;
  399.  
  400.         // Get PalmOS Rom Version to determine which version of
  401.         // sysTrapSysKeyboardDialog will be patched.   
  402.         FtrGet (sysFtrCreator, sysFtrNumROMVersion, &romVersion);
  403.  
  404.         pluginDB = DmOpenDatabaseByTypeCreator (PlugBasePluginType, appCreator, dmModeReadOnly);
  405.  
  406.         // Lock/Unlock code segment.
  407.         // the code segment must be Locked/Unlocked by yourself,
  408.         // if there has patch code in the code segment.
  409.         codeResH  = DmGet1Resource (CodeResType, 1);
  410.         if (cmd == PBLaunchCmd_PluginAttach)
  411.             MemHandleLock (codeResH);
  412.         else
  413.             MemHandleUnlock (codeResH);
  414.         DmReleaseResource (codeResH);
  415.         DmCloseDatabase (pluginDB);
  416.  
  417.         // patch/unpatch sysTrapSysKeyboardDialog according to difference PalmOS
  418.         trapItem.type        = PlugBasePluginType;
  419.         trapItem.creator     = appCreator;
  420.         if (romVersion < version20)
  421.         {
  422.             trapItem.trapNum     = sysTrapSysKeyboardDialogV10;
  423.             trapItem.newTrapAddr = mySysKeyboardDialogV10;
  424.         }
  425.         else {
  426.             trapItem.trapNum     = sysTrapSysKeyboardDialog;
  427.             trapItem.newTrapAddr = mySysKeyboardDialog;
  428.         }
  429.         if (cmd == PBLaunchCmd_PluginAttach)
  430.             (*(pmP->apiP->hookSysTrap)) (&trapItem);
  431.         else
  432.             (*(pmP->apiP->unhookSysTrap)) (&trapItem);
  433.  
  434.         // install/uninstall SoftPenStroke used by example1
  435.         if (cmd == PBLaunchCmd_PluginAttach)
  436.             installSoftPenStrokeOfExample (pmP->apiP, PenStroke1From, PenStroke1To, PenStroke2From, PenStroke2To);
  437.         else
  438.             uninstallSoftPenStrokeOfExample (pmP->apiP);
  439.  
  440.         pmP->err = 0;
  441.     }
  442.  
  443.     // The installed SoftPenStroke has inputed by user, your must response it
  444.     // by ID of SoftPenStroke installed by you. 
  445.     else if (cmd == PBLaunchCmd_SoftPenStroke)
  446.     {
  447.         CharPtr    msg = 0;
  448.  
  449.         if (pmP->data.softPenStroke.id == SoftPenStroke1ID)
  450.             msg = "SoftPenStroke1";
  451.         else if (pmP->data.softPenStroke.id == SoftPenStroke2ID)
  452.             msg = "SoftPenStroke2";
  453.  
  454.         if (msg) msgBox (msg, 100);
  455.  
  456.         pmP->err = 0;
  457.     }
  458.  
  459.     return 0;
  460. }
  461.  
  462.