home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / grantscgi / Interface / PrefDialog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-20  |  7.7 KB  |  307 lines

  1. /*****
  2.  *
  3.  *    PrefDialog.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/cgi/framework/
  12.  *
  13.  *****/
  14.  
  15. /***
  16.  *  Warning: if you modify the dialog to use non-numeric text entry fields,
  17.  *        you'll have to modify 'prefDialogEventFilter' to not trap non-numeric
  18.  *        characters.
  19.  ***/
  20.  
  21. #include "MyConfiguration.h"
  22. #if kCompileWithPreferencesDialog
  23.  
  24. #include <Dialogs.h>
  25. #include <OSUtils.h>
  26. #include <Quickdraw.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. //#include "constants.h"
  31. #include "globals.h"
  32.  
  33. //#include "CGI.h"
  34. //#include "DebugUtil.h"
  35. #include "DialogFunc.h"
  36. #include "EventUtil.h"
  37. //#include "FileUtil.h"
  38. //#include "MemoryUtil.h"
  39. #include "PrefUtil.h"
  40. #include "ProcessUtil.h"
  41.  
  42. #include "PrefDialog.h"
  43.  
  44.  
  45. /***  CONSTANT DECLARATIONS  ***/
  46.  
  47. #define krPrefsDialog            500
  48.  
  49. #define kriSleepTicks            7
  50. #define kriSleepTicksBusy        8
  51. #define kriIdleTimeBeforeQuit    9
  52.  
  53.  
  54. /***  LOCAL VARIABLES  ***/
  55.  
  56.  
  57.  
  58. /***  LOCAL PROTOTYPES  ***/
  59.  
  60. pascal Boolean    prefDialogEventFilter    ( DialogPtr, EventRecord *, short * );
  61.  
  62.  
  63. /***  FUNCTIONS  ***/
  64.  
  65. /* Bring up the preferences dialog */
  66. void
  67. PrefDialog ( void )
  68. {
  69.     GrafPtr        savePort;
  70.     DialogPtr    theDialog;
  71.     UInt32        sleepTicks;
  72.     char        tempString[256];
  73.     short        itemType;
  74.     Handle        item;
  75.     Rect        box;
  76.     UserItemUPP        defaultButtonOutlineUPP;
  77.     ModalFilterUPP    dialogEventFilterUPP;
  78.     short        itemHit;
  79.     Boolean        dialogNotDone;
  80.     Boolean        dialogCancelled;
  81.     WindowPtr    dialogWindow;
  82.     char        sleepTicksString[256];
  83.     char        sleepTicksBusyString[256];
  84.     char        idleTimeQuitString[256];
  85.     
  86.     ProcessWaitUntilFront ( kProcessNoTimeout );
  87.     
  88.     GetPort ( &savePort );    /* save the current port, whatever it is */
  89.  
  90.     /* get dialog ptr from resource file */
  91.     theDialog = GetNewDialog ( krPrefsDialog, NULL, (WindowPtr)-1L );
  92.     if ( theDialog == NULL )
  93.     {
  94.         SysBeep ( 30 );
  95.         
  96.         return;
  97.     }
  98.     
  99.     /* Sleep Ticks */
  100.     sleepTicks = ProcessSleepGetDefault ();
  101.     sprintf ( tempString, "%d", sleepTicks );
  102.     C2PStr ( tempString );
  103.     GetDialogItem ( theDialog, kriSleepTicks, &itemType, &item, &box );
  104.     SetDialogItemText ( item, (StringPtr)tempString );
  105.     SelectDialogItemText ( theDialog, kriSleepTicks, nil, 32767 );
  106.     
  107.     /* Sleep Ticks Busy */
  108.     sleepTicks = ProcessSleepGetBusy ();
  109.     sprintf ( tempString, "%d", sleepTicks );
  110.     C2PStr ( tempString );
  111.     GetDialogItem ( theDialog, kriSleepTicksBusy, &itemType, &item, &box );
  112.     SetDialogItemText ( item, (StringPtr)tempString );
  113.     
  114.     /* Idle Time to Quit */
  115.     #if kCompileWithQuitOnLongIdle
  116.     sleepTicks = gIdleTimeToQuit / 60;    /* convert ticks to seconds */
  117.     sprintf ( tempString, "%d", sleepTicks );
  118.     C2PStr ( tempString );
  119.     GetDialogItem ( theDialog, kriIdleTimeBeforeQuit, &itemType, &item, &box );
  120.     SetDialogItemText ( item, (StringPtr)tempString );
  121.     #else
  122.     //••• disable the dialog item and text item
  123.     #endif
  124.     
  125.     /* drawing function for default button's outline */
  126.     defaultButtonOutlineUPP = NewUserItemProc ( DialogDefaultButtonOutline );
  127.     GetDialogItem ( theDialog, kDefaultButtonOutline, &itemType, &item, &box );
  128.     SetDialogItem ( theDialog, kDefaultButtonOutline, itemType, (Handle)defaultButtonOutlineUPP, &box );
  129.     
  130.     dialogEventFilterUPP = NewModalFilterProc ( prefDialogEventFilter );
  131.     
  132.     /* initialize loop variables */
  133.     itemHit            = 0;
  134.     dialogNotDone    = true;
  135.     dialogCancelled    = false;
  136.     
  137.     /* now make it visible */
  138.     dialogWindow = GetDialogWindow ( theDialog );
  139.     ShowWindow ( dialogWindow );
  140. //    SetPort ( theDialog );
  141.     
  142.     /* loop on modal dialog */
  143.     do
  144.     {
  145.         ModalDialog ( dialogEventFilterUPP, &itemHit );
  146.         switch ( itemHit )
  147.         {
  148.             case kDefaultButtonID :
  149.                 /* set the user login id */
  150.                 GetDialogItem ( theDialog, kriSleepTicks, &itemType, &item, &box );
  151.                 GetDialogItemText ( item, (StringPtr)sleepTicksString );
  152.                 if ( sleepTicksString[0] == nil )
  153.                 {
  154.                     SysBeep ( 30 );
  155.                     SelectDialogItemText ( theDialog, kriSleepTicks, nil, 32767 );
  156.                 }
  157.                 else
  158.                 {
  159.                     GetDialogItem ( theDialog, kriSleepTicksBusy, &itemType, &item, &box );
  160.                     GetDialogItemText ( item, (StringPtr)sleepTicksBusyString );
  161.                     if ( sleepTicksBusyString[0] == nil )
  162.                     {
  163.                         SysBeep ( 30 );
  164.                         SelectDialogItemText ( theDialog, kriSleepTicksBusy, nil, 32767 );
  165.                     }
  166.                     else
  167.                     {
  168.                         #if kCompileWithQuitOnLongIdle
  169.                         GetDialogItem ( theDialog, kriIdleTimeBeforeQuit, &itemType, &item, &box );
  170.                         GetDialogItemText ( item, (StringPtr)idleTimeQuitString );
  171.                         if ( idleTimeQuitString[0] == nil )
  172.                         {
  173.                             SysBeep ( 30 );
  174.                             SelectDialogItemText ( theDialog, kriIdleTimeBeforeQuit, nil, 32767 );
  175.                         }
  176.                         else
  177.                         {
  178.                         #endif
  179.                         
  180.                             dialogNotDone = false;
  181.                         
  182.                         #if kCompileWithQuitOnLongIdle
  183.                         }
  184.                         #endif
  185.                     }
  186.                 }
  187.                 break;
  188.             
  189.             case kCancelButtonID :
  190.                 dialogCancelled = true;
  191.                 dialogNotDone = false;
  192.                 break;
  193.             
  194.             case kriSleepTicks :
  195.             case kriSleepTicksBusy :
  196.             case kriIdleTimeBeforeQuit :
  197.             default :
  198.                 break;
  199.         }
  200.     } while ( dialogNotDone );
  201.     
  202.     if ( !dialogCancelled )
  203.     {
  204.         /* Sleep Ticks */
  205.         P2CStr ( (StringPtr)sleepTicksString );
  206.         sleepTicks = atol ( sleepTicksString );
  207.         PrefSaveSleepTicks ( sleepTicks );
  208.         ProcessSleepSetDefault ( sleepTicks );
  209.         
  210.         /* Sleep Ticks Busy */
  211.         P2CStr ( (StringPtr)sleepTicksBusyString );
  212.         sleepTicks = atol ( sleepTicksBusyString );
  213.         PrefSaveSleepTicksBusy ( sleepTicks );
  214.         ProcessSleepSetBusy ( sleepTicks );
  215.         
  216.         ProcessResetSleep ();
  217.         
  218.         /* Idle Time to Quit */
  219.         #if kCompileWithQuitOnLongIdle
  220.         P2CStr ( (StringPtr)idleTimeQuitString );
  221.         sleepTicks = atol ( idleTimeQuitString );
  222.         sleepTicks *= 60; /* convert to seconds */
  223.         PrefSaveIdleTimeToQuit ( sleepTicks );
  224.         gIdleTimeToQuit = sleepTicks;    //• this should use an accessor function instead of directly accessing the global
  225.         #endif
  226.     }
  227.     
  228.     DisposeRoutineDescriptor ( dialogEventFilterUPP );
  229.     DisposeRoutineDescriptor ( defaultButtonOutlineUPP );
  230.     DisposeDialog ( theDialog );
  231.     SetPort ( savePort );
  232. } /* PrefDialog */
  233.  
  234.  
  235. /* This function responds to update, and activate events, and key presses.
  236.     return and enter select the 'OK' button. Command-. and esc select 'Cancel'. */
  237. pascal Boolean
  238. prefDialogEventFilter ( DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  239. {
  240.     Boolean        result;
  241.     WindowPtr    dialogWindow;
  242.     char        key;
  243.     short        itemType;
  244.     Handle        itemHandle;
  245.     Rect        itemRect;
  246.     long        finalTicks;
  247.     
  248.     result = false;
  249.     
  250.     switch ( theEvent->what )
  251.     {
  252.         case updateEvt :
  253.             dialogWindow = GetDialogWindow ( theDialog );
  254.             if ( (WindowPtr)theEvent->message != dialogWindow )
  255.             {
  256.                 doUpdateEvent ( theEvent );
  257.             }
  258.             break;
  259.         
  260.         case activateEvt :
  261.             break;
  262.         
  263.         case keyDown :
  264.         case autoKey :
  265.             key = (char)( theEvent->message & charCodeMask );
  266.             if ( ( key == (char)kReturnKey ) || ( key == (char)kEnterKey ) )
  267.             {
  268.                 GetDialogItem    ( theDialog, kDefaultButtonID, &itemType, &itemHandle, &itemRect );
  269.                 HiliteControl    ( (ControlHandle)itemHandle, 0 );
  270.                 Delay            ( (long)kVisualDelay, &finalTicks );
  271.                 HiliteControl    ( (ControlHandle)itemHandle, 0 );
  272.                 
  273.                 *itemHit = (short)kDefaultButtonID;
  274.                 result = true;
  275.             }
  276.             else if ( ( key == (char)kEscapeKey ) ||
  277.                 ( (key == (char)kPeriodKey) && (theEvent->modifiers & cmdKey) ) )
  278.             {
  279.                 GetDialogItem    ( theDialog, kCancelButtonID, &itemType, &itemHandle, &itemRect );
  280.                 HiliteControl    ( (ControlHandle)itemHandle, 0 );
  281.                 Delay            ( (long)kVisualDelay, &finalTicks );
  282.                 HiliteControl    ( (ControlHandle)itemHandle, 0 );
  283.                 
  284.                 *itemHit = (short)kCancelButtonID;
  285.                 result = true;
  286.             }
  287.             else if ( key < ' ' )
  288.             {
  289.                 /* pass control characters */
  290.             }
  291.             else if ( (key < '0') || (key > '9') )
  292.             {
  293.                 /* beep for non-digit chars */
  294.                 SysBeep ( 30 );
  295.                 
  296. //                result = true;
  297.             }
  298.             break;
  299.     }
  300.     
  301.     return ( result );
  302. } /* prefDialogEventFilter */
  303.  
  304.  
  305. #endif /* kCompileWithPreferencesDialog */
  306. /*****  EOF  *****/
  307.