home *** CD-ROM | disk | FTP | other *** search
- /*====================================================================================
- File: main.c
- Description:
- Demostrates the Relative Time String routines.
-
- __________________________________________________________________________________
- History:
- 11/8/94 11:37:37 AM created
-
- =====================================================================================*/
-
- ////////////////////////
- // Includes
- ////////////////////////
- #include "Relative Time Strings.h"
- #include "main.h"
-
- ////////////////////////
- // Internal Prototypes
- ////////////////////////
- void Initialize_Toolbox( void );
- void Show_Dialog (void);
- void Toggle_Check_Box( DialogPtr the_dialog, short the_item );
- void Get_Text_From_Edit( DialogPtr the_dialog, short edit_item, Str255 the_string );
- void Secs_To_Date_Time(unsigned long total_seconds, Str255 the_date_str, Str255 the_time_str );
- void Set_Text_From_Edit( DialogPtr the_dialog, short edit_item, Str255 the_string );
- void SetMemory(register Ptr the_ptr, register long data_size, register char init_value);
- unsigned long Date_Time_To_Secs( Str255 the_date_str, Str255 the_time_str );
-
-
- /*++++++++++++++++++++++ main listing ++++++++++++++++++++++++*/
-
- void main( void )
- {
- MaxApplZone();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MoreMasters();
-
- Initialize_Toolbox();
-
- Show_Dialog();
- ExitToShell();
- }
-
- /*++++++++++++++++++ Initialize the Toolbox ++++++++++++++++++*/
-
- void Initialize_Toolbox( void )
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( NIL );
- FlushEvents( everyEvent, REMOVE_EVENTS );
- InitCursor();
- }
-
- void Show_Dialog (void)
- {
- DialogPtr the_dialog;
- short the_item;
- Boolean all_done = FALSE;
- Str255 date_string;
- Str255 time_string;
- Str255 the_string;
- Display_Type_Ptr display;
- Count_Rec time_rec;
-
- the_dialog = GetNewDialog( 150, nil, IN_FRONT );
-
- display = &time_rec.Display;
- time_rec.Display.display_items = 0;
-
- time_rec.Display.fractional = FALSE;
- time_rec.Display.significant = FALSE;
- time_rec.Display.count_days = FALSE;
- time_rec.Display.hide_zeros = FALSE;
- time_rec.Display.allow_clock_display = FALSE;
- time_rec.Display.allow_day_rounding = FALSE;
- time_rec.Display.abbreviated = FALSE;
-
- time_rec.Target_In_Secs = 2840227200; // first time to display (in seconds) 1/1/90
- Secs_To_Date_Time(time_rec.Target_In_Secs, date_string, time_string);
- Set_Text_From_Edit( the_dialog, TO_DATE, date_string);
- Set_Text_From_Edit( the_dialog, TO_TIME, time_string);
-
- GetDateTime(&time_rec.From_In_Secs); // start with the current time & date
- Secs_To_Date_Time(time_rec.From_In_Secs, date_string, time_string);
- Set_Text_From_Edit( the_dialog, FROM_DATE, date_string);
- Set_Text_From_Edit( the_dialog, FROM_TIME, time_string);
-
- ShowWindow (the_dialog);
-
- while (! all_done)
- {
- Get_Rel_Time_Str (&time_rec, the_string);
- Set_Text_From_Edit( the_dialog, EVENT_EXAMPLE, the_string);
-
- ModalDialog(NIL, &the_item);
- switch (the_item)
- {
- case OK_BUTTON:
- case CANCEL_BUTTON:
- all_done = TRUE;
- break;
-
- case YEAR_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->display_items ^= year;
- break;
-
- case MONTH_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->display_items ^= month;
- break;
-
- case WEEK_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->display_items ^= week;
- break;
-
- case DAY_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->display_items ^= day;
- break;
-
- case HOUR_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->display_items ^= hour;
- break;
-
- case MIN_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->display_items ^= minute;
- break;
-
- case SEC_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->display_items ^= second;
- break;
-
- case FRAC_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->fractional = !display->fractional;
- break;
-
- case SIGNIF_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->significant = !display->significant;
- break;
-
- case DAY1_CHECKBOX:
- Toggle_Check_Box ( the_dialog, the_item);
- display->count_days = !display->count_days;
- break;
-
- case DISPLAY_ZEROS:
- Toggle_Check_Box ( the_dialog, the_item);
- time_rec.Display.hide_zeros = !time_rec.Display.hide_zeros;
- break;
-
- case ALLOW_CLOCK_DISPLAY:
- Toggle_Check_Box ( the_dialog, the_item);
- time_rec.Display.allow_clock_display = !time_rec.Display.allow_clock_display;
- break;
-
- case ALLOW_ROUNDING:
- Toggle_Check_Box ( the_dialog, the_item);
- time_rec.Display.allow_day_rounding = !time_rec.Display.allow_day_rounding;
- break;
-
- case ABREVIATE:
- Toggle_Check_Box ( the_dialog, the_item);
- time_rec.Display.abbreviated = !time_rec.Display.abbreviated;
- break;
-
-
- case TO_DATE:
- case TO_TIME:
- Get_Text_From_Edit( the_dialog, TO_DATE, date_string);
- Get_Text_From_Edit( the_dialog, TO_TIME, time_string);
- time_rec.Target_In_Secs = Date_Time_To_Secs( date_string, time_string );
- break;
-
- case FROM_DATE:
- case FROM_TIME:
- Get_Text_From_Edit( the_dialog, FROM_DATE, date_string);
- Get_Text_From_Edit( the_dialog, FROM_TIME, time_string);
- time_rec.From_In_Secs = Date_Time_To_Secs( date_string, time_string );
- break;
-
- }
- }
- DisposeDialog (the_dialog);
- }
-
- void Toggle_Check_Box( DialogPtr the_dialog, short the_item )
- {
- Handle item_handle;
- short item_type;
- Rect item_rect;
- int old_value;
-
- GetDItem( the_dialog, the_item, &item_type, &item_handle, &item_rect );
- old_value = GetCtlValue( ( ControlHandle )item_handle );
-
- if ( old_value == CONTROL_ON )
- SetCtlValue( (ControlHandle )item_handle, CONTROL_OFF );
- else
- SetCtlValue( (ControlHandle )item_handle, CONTROL_ON );
- }
-
- /*++++++++ Get user-entered text from a text edit item +++++++*/
- void Get_Text_From_Edit( DialogPtr the_dialog, short edit_item, Str255 the_string )
- {
- Handle item_handle;
- short item_type;
- Rect item_rect;
-
- GetDItem( the_dialog, edit_item, &item_type, &item_handle, &item_rect );
- GetIText( item_handle, the_string );
- }
-
- /*++++++++ Set user-entered text +++++++*/
-
- void Set_Text_From_Edit( DialogPtr the_dialog, short edit_item, Str255 the_string )
- {
- Handle item_handle;
- short item_type;
- Rect item_rect;
-
- GetDItem( the_dialog, edit_item, &item_type, &item_handle, &item_rect );
- SetIText( item_handle, the_string );
- }
-
- /*********************************************
- *
- * Secs_To_Date_Time: send it an unsigned long of the seconds and it will return a
- * a string with the date and a string with the time in the format
- * specified by the international resources
- *
- **********************************************/
- void Secs_To_Date_Time(unsigned long total_seconds, Str255 the_date_str, Str255 the_time_str )
- {
- IUDateString(total_seconds, shortDate, the_date_str);
- IUTimeString(total_seconds, longDate, the_time_str);
- }
-
- /*********************************************
- *
- * Date_Time_To_Secs: send it a date and time string and it will return a unsigned long
- * of the seconds for that date and time
- *
- **********************************************/
- unsigned long Date_Time_To_Secs( Str255 the_date_str, Str255 the_time_str )
- {
- String2DateStatus the_status;
- long length_used;
- LongDateRec date_time;
- unsigned long total_seconds = 0;
- DateCacheRecord date_cache;
-
- SetMemory((Ptr)&date_time, sizeof(LongDateRec), 0);
- InitDateCache (&date_cache);
-
- the_status = String2Date((Ptr)&(the_date_str[1]), StrLength(the_date_str), &date_cache, &length_used, &date_time);
- if (the_status >= 0)
- Date2Secs(&date_time.od.oldDate, &total_seconds);
-
- if (StrLength(the_time_str) != 0)
- {
- the_status = String2Time((Ptr)&(the_time_str[1]), StrLength(the_time_str), &date_cache, &length_used, &date_time);
- if (the_status >= 0)
- Date2Secs(&date_time.od.oldDate, &total_seconds);
- }
- return (total_seconds);
- }
-
- /*********************************************
- * SetMemory
- *
- * Set a bunch of bytes to the same value.
- *
- * -> thePtr The address of the memory to set
- * -> dataSize The number of bytes to set
- * -> initValue The value to initialize each byte to
- *
- **********************************************/
- void SetMemory(register Ptr the_ptr, register long data_size, register char init_value)
- {
- register short index;
-
- for (index = 0; index < data_size; index++)
- the_ptr[index] = init_value;
- return;
- }
-
-
-