main.c

/*
    File:       main.c
 
    Contains:   draws corrent location and time zone information.           
                shows  a function that determines if Daylight               
                savings time is turned on or off from the Date & Time       
                control panel.  Inside Mac: OS Utililies Chapter 4          
                should be consulted for further information.
 
    Written by: David Hayward   
 
    Copyright:  Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
 
                You may incorporate this Apple sample source code into your program(s) without
                restriction. This Apple sample source code has been provided "AS IS" and the
                responsibility for its operation is yours. You are not permitted to redistribute
                this Apple sample source code as "Apple sample source code" after having made
                changes. If you're going to re-distribute the source, we require that you make
                it clear in the source that the code was descended from Apple sample source
                code, but that you've made changes.
 
    Change History (most recent first):
                7/23/1999   Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
                
 
*/
/****************************************************************************/
#include <TextUtils.h>
#include <Dialogs.h>
#include <Fonts.h>
#include <Icons.h>
#include <Memory.h>
#include <Types.h>
#include <ToolUtils.h>
#include <Windows.h>
#include <FixMath.h>
#include <QuickDraw.h>
#include <GestaltEqu.h>
#include <OSUtils.h>
#include "initMac.h"
 
 
/*\
|*| ---------------------------------------------------------------------
|*| GLOBALS
|*| ---------------------------------------------------------------------
\*/
WindowPtr       gWindow;
MachineLocation gLocation;
Boolean         done=0;
 
 
/*\
|*| ---------------------------------------------------------------------
|*| PROTOTYPES
|*| ---------------------------------------------------------------------
\*/
void main               (void);
void createWindow       (void);
void DoEventLoop        (void);
void DoUpdate           (WindowPtr whichWindow);
void DoMouseDown        (EventRecord event);
void DrawHMS (long time);
void DrawDMS (long degr);
int IsDaylightSavingsOn();
 
/*\
|*| ---------------------------------------------------------------------
|*| Determine if Daylight Savings Time is on
|*| ---------------------------------------------------------------------
\*/
 
int IsDaylightSavingsOn()
{
    int retVal = 0;
    MachineLocation theLocation;
 
    
    ReadLocation(&theLocation);
    if (theLocation.u.dlsDelta == (signed char) 0x80) {
        retVal = 1;
    }
    return(retVal);
}       
            
 
 
void main (void)
{
    InitToolBox(1);
 
    createWindow();
    
ReadLocation(&gLocation);
    
    DoEventLoop();
}
 
 
void createWindow (void)
{
    gWindow = GetNewCWindow(128,nil,nil);
    SetPort( gWindow );
}
 
 
/*\
|*| ---------------------------------------------------------------------
|*| DoEventLoop()
|*| ---------------------------------------------------------------------
\*/
void DoEventLoop (void)
{
    EventRecord event;
    long        mssg;
 
    while ( !done ) 
    {
        if (WaitNextEvent( everyEvent, &event, 60, nil ))
        {
            mssg = event.message;
            switch (event.what)
            {
                case mouseDown :                        /* handle mouse clicks */
                    DoMouseDown (event);
                    break;
                
                case keyDown :                          /* handle key hits */
                case autoKey :
                    break;
                    
                case updateEvt :                        /* handle update events */
                    DoUpdate((WindowPtr)mssg);
                    break;
                
                case osEvt:                             /* handle os events */
                    if ( (mssg>>24)                     /* if high byte of message indicates */
                          == suspendResumeMessage )     /* this is suspend/resume event */
                    {
                        if (mssg & resumeFlag)          /* if resume event */
                        {
                            /* we're switching back from another app so */
                            /* we may need to show floating window */
                        //  InvalRect(&((GrafPtr)gWindow)->portRect);
                        }
                    }
                    break;
            }
        }
        else            /* null event */
        {
            MachineLocation temp;
            
            ReadLocation(&temp);
            
            if ( (gLocation.latitude != temp.latitude) ||
                 (gLocation.longitude != temp.longitude) ||
                 (gLocation.u.gmtDelta != temp.u.gmtDelta) )
            {
                gLocation = temp;
                InvalRect(&((GrafPtr)gWindow)->portRect);
            }
        }
    }
}
 
 
/*\
|*| ---------------------------------------------------------------------
|*| DoMouseDown
|*| handle DoMouseDown events
|*| ---------------------------------------------------------------------
\*/
void DoMouseDown (EventRecord event)
{
    WindowPtr   window;
    short       clickArea;
    Rect        screenRect;
 
    clickArea = FindWindow( event.where, &window );
    
    switch (clickArea)
    {
        case inDrag:
            screenRect = (**GetGrayRgn()).rgnBBox;
            DragWindow( window, event.where, &screenRect );
            break;
 
        case inContent:
            if (window != FrontWindow())
                SelectWindow( window );
            break;
        
        case inGoAway:
            if (TrackGoAway( window, event.where ))
                done = 1;
            break;
    }
}
            
            
/*\
|*| ---------------------------------------------------------------------
|*| DoUpdate
|*| handle update events
|*| ---------------------------------------------------------------------
\*/
void DoUpdate (WindowPtr window)
{
    Str255  str;
    long    gmt;
    
    SetPort( window );
    BeginUpdate( window );
 
    EraseRect( &((GrafPtr)window)->portRect);
    
    MoveTo(10,20);
    DrawString("\pLat: ");
    DrawDMS((long)gLocation.latitude);
    
    MoveTo(10,40);
    DrawString("\pLong: ");
    DrawDMS((long)gLocation.longitude);
    
    MoveTo(10,60);
    DrawString("\pdlsÆ: ");
    NumToString( (long)gLocation.u.dlsDelta, str);
    DrawString(str);
    
    if (IsDaylightSavingsOn()) {
        DrawString("\p Daylight On ");
    } else {
        DrawString("\p Daylight Off ");
    }
    
    MoveTo(10,80);
    DrawString("\pgmtÆ: ");
    gmt = gLocation.u.gmtDelta;
    gmt &= 0x00FFFFFF;          // mask off dlsDelta
    if (gmt & 0x00800000)       // if sign bit of gmtDelta is set
        gmt |= 0xFF000000;      // then turn on high char if long
    DrawHMS (gmt);
 
    EndUpdate( window );
}
 
 
void DrawHMS (long time)
{
    Str255  str;
 
    if (time<0)
    {
        time = -time;
        DrawString("\p- ");
    }
 
    NumToString( time / 3600L, str);
    DrawString(str);
    DrawString("\ph ");
    
    NumToString( (time % 3600L) / 60L, str);
    DrawString(str);
    DrawString("\pm ");
 
}
 
 
void DrawDMS (long degr)
{
    Str255  str;
    
    if (degr<0)
    {
        degr = -degr;
        DrawString("\p- ");
    }
 
    NumToString( degr / 11930464L, str);
    DrawString(str);
    DrawString("\p¡ ");
    
    NumToString( (degr % 11930464L) / 198841L, str);
    DrawString(str);
    DrawString("\pm ");
}