home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Relative Time Strings / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-08  |  8.6 KB  |  302 lines  |  [TEXT/KAHL]

  1. /*====================================================================================
  2.     File:            main.c
  3.     Description:
  4.         Demostrates the Relative Time String routines.
  5.     
  6. __________________________________________________________________________________ 
  7.     History:
  8.     11/8/94  11:37:37 AM        created
  9.     
  10. =====================================================================================*/
  11.  
  12. ////////////////////////
  13. // Includes
  14. ////////////////////////
  15. #include "Relative Time Strings.h"
  16. #include "main.h"
  17.  
  18. ////////////////////////
  19. //    Internal Prototypes
  20. ////////////////////////
  21. void    Initialize_Toolbox( void );
  22. void    Show_Dialog (void);
  23. void    Toggle_Check_Box( DialogPtr the_dialog, short the_item );        
  24. void    Get_Text_From_Edit( DialogPtr the_dialog, short edit_item, Str255 the_string );
  25. void    Secs_To_Date_Time(unsigned long total_seconds, Str255 the_date_str, Str255 the_time_str );
  26. void    Set_Text_From_Edit( DialogPtr the_dialog, short edit_item, Str255 the_string );
  27. void    SetMemory(register Ptr the_ptr, register long data_size, register char init_value);
  28. unsigned long Date_Time_To_Secs( Str255 the_date_str, Str255 the_time_str );
  29.  
  30.  
  31. /*++++++++++++++++++++++  main listing  ++++++++++++++++++++++++*/
  32.  
  33. void main( void )                             
  34. {
  35.    MaxApplZone();
  36.    MoreMasters();
  37.    MoreMasters();
  38.    MoreMasters();
  39.    MoreMasters();
  40.  
  41.    Initialize_Toolbox();    
  42.  
  43.     Show_Dialog();
  44.     ExitToShell();
  45. }
  46.  
  47. /*++++++++++++++++++  Initialize the Toolbox  ++++++++++++++++++*/
  48.  
  49. void  Initialize_Toolbox( void )      
  50. {
  51.    InitGraf( &thePort );      
  52.    InitFonts();            
  53.    InitWindows();           
  54.    InitMenus();               
  55.    TEInit();                  
  56.    InitDialogs( NIL );        
  57.    FlushEvents( everyEvent, REMOVE_EVENTS );
  58.    InitCursor();
  59. }
  60.  
  61. void Show_Dialog (void)
  62. {
  63.     DialogPtr            the_dialog;
  64.     short                the_item;
  65.     Boolean                all_done = FALSE;
  66.     Str255                date_string;
  67.     Str255                time_string;
  68.     Str255                the_string;
  69.     Display_Type_Ptr    display;
  70.     Count_Rec            time_rec;
  71.     
  72.     the_dialog = GetNewDialog( 150, nil, IN_FRONT );
  73.  
  74.     display = &time_rec.Display;
  75.     time_rec.Display.display_items = 0;
  76.     
  77.     time_rec.Display.fractional = FALSE;
  78.     time_rec.Display.significant = FALSE;
  79.     time_rec.Display.count_days = FALSE;
  80.     time_rec.Display.hide_zeros = FALSE;
  81.     time_rec.Display.allow_clock_display = FALSE;
  82.     time_rec.Display.allow_day_rounding = FALSE;
  83.     time_rec.Display.abbreviated = FALSE;
  84.  
  85.     time_rec.Target_In_Secs = 2840227200;            // first time to display (in seconds) 1/1/90
  86.     Secs_To_Date_Time(time_rec.Target_In_Secs, date_string, time_string);
  87.     Set_Text_From_Edit( the_dialog, TO_DATE, date_string);
  88.     Set_Text_From_Edit( the_dialog, TO_TIME, time_string);
  89.     
  90.     GetDateTime(&time_rec.From_In_Secs);            // start with the current time & date
  91.     Secs_To_Date_Time(time_rec.From_In_Secs, date_string, time_string);
  92.     Set_Text_From_Edit( the_dialog, FROM_DATE, date_string);
  93.     Set_Text_From_Edit( the_dialog, FROM_TIME, time_string);
  94.     
  95.     ShowWindow (the_dialog);
  96.     
  97.     while (! all_done)
  98.     {
  99.         Get_Rel_Time_Str (&time_rec, the_string);
  100.         Set_Text_From_Edit( the_dialog, EVENT_EXAMPLE, the_string);
  101.             
  102.         ModalDialog(NIL, &the_item);
  103.         switch (the_item)
  104.         {
  105.             case OK_BUTTON:
  106.             case CANCEL_BUTTON:
  107.                 all_done = TRUE;
  108.                 break;
  109.                 
  110.             case YEAR_CHECKBOX:
  111.                 Toggle_Check_Box ( the_dialog, the_item);
  112.                 display->display_items ^= year;
  113.                 break;
  114.                 
  115.             case MONTH_CHECKBOX:
  116.                 Toggle_Check_Box ( the_dialog, the_item);
  117.                 display->display_items ^= month;
  118.                 break;
  119.                 
  120.             case WEEK_CHECKBOX:
  121.                 Toggle_Check_Box ( the_dialog, the_item);
  122.                 display->display_items ^= week;
  123.                 break;
  124.                 
  125.             case DAY_CHECKBOX:
  126.                 Toggle_Check_Box ( the_dialog, the_item);
  127.                 display->display_items ^= day;
  128.                 break;
  129.                 
  130.             case HOUR_CHECKBOX:
  131.                 Toggle_Check_Box ( the_dialog, the_item);
  132.                 display->display_items ^= hour;
  133.                 break;
  134.                 
  135.             case MIN_CHECKBOX:
  136.                 Toggle_Check_Box ( the_dialog, the_item);
  137.                 display->display_items ^= minute;
  138.                 break;
  139.                 
  140.             case SEC_CHECKBOX:
  141.                 Toggle_Check_Box ( the_dialog, the_item);
  142.                 display->display_items ^= second;
  143.                 break;
  144.                 
  145.             case FRAC_CHECKBOX:
  146.                 Toggle_Check_Box ( the_dialog, the_item);
  147.                 display->fractional = !display->fractional;
  148.                 break;
  149.                 
  150.             case SIGNIF_CHECKBOX:
  151.                 Toggle_Check_Box ( the_dialog, the_item);
  152.                 display->significant = !display->significant;
  153.                 break;
  154.                 
  155.             case DAY1_CHECKBOX:
  156.                 Toggle_Check_Box ( the_dialog, the_item);
  157.                 display->count_days = !display->count_days;
  158.                 break;
  159.                 
  160.             case DISPLAY_ZEROS:
  161.                 Toggle_Check_Box ( the_dialog, the_item);
  162.                 time_rec.Display.hide_zeros = !time_rec.Display.hide_zeros;
  163.                 break;
  164.         
  165.             case ALLOW_CLOCK_DISPLAY:
  166.                 Toggle_Check_Box ( the_dialog, the_item);
  167.                 time_rec.Display.allow_clock_display = !time_rec.Display.allow_clock_display;
  168.                 break;
  169.         
  170.             case ALLOW_ROUNDING:
  171.                 Toggle_Check_Box ( the_dialog, the_item);
  172.                 time_rec.Display.allow_day_rounding = !time_rec.Display.allow_day_rounding;
  173.                 break;
  174.         
  175.             case ABREVIATE:
  176.                 Toggle_Check_Box ( the_dialog, the_item);
  177.                 time_rec.Display.abbreviated = !time_rec.Display.abbreviated;
  178.                 break;
  179.         
  180.             
  181.             case TO_DATE:
  182.             case TO_TIME:
  183.                 Get_Text_From_Edit( the_dialog, TO_DATE, date_string);
  184.                 Get_Text_From_Edit( the_dialog, TO_TIME, time_string);
  185.                 time_rec.Target_In_Secs = Date_Time_To_Secs( date_string, time_string );
  186.                 break;
  187.                 
  188.             case FROM_DATE:
  189.             case FROM_TIME:
  190.                 Get_Text_From_Edit( the_dialog, FROM_DATE, date_string);
  191.                 Get_Text_From_Edit( the_dialog, FROM_TIME, time_string);
  192.                 time_rec.From_In_Secs = Date_Time_To_Secs( date_string, time_string );
  193.                 break;
  194.                 
  195.         }
  196.     }
  197.     DisposeDialog (the_dialog);
  198. }
  199.  
  200. void Toggle_Check_Box( DialogPtr the_dialog, short the_item )        
  201. {
  202.    Handle  item_handle; 
  203.    short   item_type; 
  204.    Rect    item_rect; 
  205.    int     old_value; 
  206.  
  207.    GetDItem( the_dialog, the_item, &item_type, &item_handle, &item_rect ); 
  208.    old_value = GetCtlValue( ( ControlHandle )item_handle );
  209.  
  210.    if ( old_value == CONTROL_ON )
  211.       SetCtlValue( (ControlHandle )item_handle, CONTROL_OFF );
  212.    else
  213.       SetCtlValue( (ControlHandle )item_handle, CONTROL_ON );
  214. }
  215.  
  216. /*++++++++  Get user-entered text from a text edit item  +++++++*/
  217. void  Get_Text_From_Edit( DialogPtr the_dialog, short edit_item, Str255 the_string )
  218. {
  219.         Handle  item_handle;  
  220.         short   item_type; 
  221.         Rect    item_rect; 
  222.  
  223.     GetDItem( the_dialog, edit_item, &item_type, &item_handle, &item_rect ); 
  224.     GetIText( item_handle, the_string );
  225. }
  226.  
  227. /*++++++++  Set user-entered text +++++++*/
  228.  
  229. void  Set_Text_From_Edit( DialogPtr the_dialog, short edit_item, Str255 the_string )
  230. {
  231.    Handle  item_handle;  
  232.    short   item_type; 
  233.    Rect    item_rect; 
  234.  
  235.    GetDItem( the_dialog, edit_item, &item_type, &item_handle, &item_rect ); 
  236.    SetIText( item_handle, the_string );
  237. }
  238.  
  239. /*********************************************
  240. *
  241. *    Secs_To_Date_Time:    send it an unsigned long of the seconds and it will return a
  242. *                        a string with the date and a string with the time in the format
  243. *                        specified by the international resources
  244. *
  245. **********************************************/
  246. void Secs_To_Date_Time(unsigned long total_seconds, Str255 the_date_str, Str255 the_time_str )
  247. {
  248.       IUDateString(total_seconds, shortDate, the_date_str);
  249.     IUTimeString(total_seconds, longDate, the_time_str);
  250. }
  251.  
  252. /*********************************************
  253. *
  254. *    Date_Time_To_Secs:    send it a date and time string and it will return a unsigned long
  255. *                        of the seconds for that date and time
  256. *
  257. **********************************************/
  258. unsigned long    Date_Time_To_Secs( Str255 the_date_str, Str255 the_time_str )
  259. {
  260.         String2DateStatus    the_status;
  261.         long                length_used;
  262.         LongDateRec            date_time;
  263.         unsigned long        total_seconds = 0;
  264.         DateCacheRecord        date_cache;
  265.     
  266.     SetMemory((Ptr)&date_time, sizeof(LongDateRec), 0);
  267.     InitDateCache (&date_cache);
  268.  
  269.     the_status = String2Date((Ptr)&(the_date_str[1]), StrLength(the_date_str), &date_cache, &length_used, &date_time);
  270.     if (the_status >= 0)
  271.         Date2Secs(&date_time.od.oldDate, &total_seconds);
  272.  
  273.     if (StrLength(the_time_str) != 0)
  274.     {
  275.         the_status = String2Time((Ptr)&(the_time_str[1]), StrLength(the_time_str), &date_cache, &length_used, &date_time);
  276.         if (the_status >= 0)
  277.             Date2Secs(&date_time.od.oldDate, &total_seconds);
  278.     }
  279.     return (total_seconds);
  280. }
  281.  
  282. /*********************************************
  283. *    SetMemory
  284. *
  285. *        Set a bunch of bytes to the same value.
  286. *
  287. *        -> thePtr            The address of the memory to set
  288. *        -> dataSize            The number of bytes to set
  289. *        -> initValue        The value to initialize each byte to
  290. *
  291. **********************************************/
  292. void SetMemory(register Ptr the_ptr, register long data_size, register char init_value)
  293. {
  294.         register short    index;
  295.  
  296.     for (index = 0; index < data_size; index++)
  297.         the_ptr[index] = init_value;
  298.     return;
  299. }
  300.  
  301.  
  302.