
#import "CalObject.h"
#include <stdio.h>
#include <time.h>
#include <sys/types.h>

@implementation CalObject

	NXRect frame;
 	int temp_x, temp_y;

// Calendar by Eric "E.T." Tremblay, February 14, 1993

//INIT
// This is one of the first methods executed. (Presently not used)
// after the nib is loaded. This is not the best way to initialize
// an object because not all method and varibles are ready to be used.
- init
{	
	[super init];
		
        return self;
}


// AWAKEFROMNIB
// This is one of the last methods executed after the nib file is loaded.
// This is the BEST way I know how to initialize an object and jump into
// a method after the nib file was loaded, because you are all the methods
// and variables needed can be used. (Unlike init which is not compeletly
// ready when that message is sent
- awakeFromNib
{
 
	[self NowCalendar:NULL];

    	return self;
}


- windowDidMove:sender;
{

    	//Local variables
    	NXRect frame;
    	int calendarPanel_x, calendarPanel_y;
	int Month_x = 0;
	int Month_y = 0;
	int Year_x  = 0;
	int Year_y  = 0;

	[calendarPanel getFrame:&frame]; // Get the current xy position
	
        calendarPanel_x = NX_X(&frame);// Assign current x location to variable
	calendarPanel_y = NX_Y(&frame);// Assign current y location to variable

	Month_x = calendarPanel_x + 327;
	Month_y = calendarPanel_y + 5;

	Year_x = calendarPanel_x + 68;
	Year_y = calendarPanel_y - 136;


	[MonthPanel moveTo:(Month_x):(Month_y)]; // re-position the MonthPanel
	[YearPanel moveTo:(Year_x):(Year_y)]; // re-position the YearPanel

	return self;

}


// NOWCALENDAR
// This gets the present date and executes the rebuildCalView method
- NowCalendar:sender
{    	

	// Local Variables
	long time(), tmpTime;
	struct tm *tm, *localtime();
	tmpTime = time(0);
	tm = localtime( &tmpTime );
	month = (tm->tm_mon+1);
	year = (1900+tm->tm_year);

	[self rebuildCalView:sender];

        return self;
}



// REBUILDCALVIEW
// This method builds the proper calendar in the window
- rebuildCalView:sender
{
	// Local Variables
	FILE *calCmd;
	char c, buff[256], *pbuff;
	
	[calendarPanel makeKeyAndOrderFront:NULL];

	sprintf( buff, "cal %d %d", month, year );
	if ( (calCmd=popen(buff, "r")) == NULL )
		[theCalView setStringValue:"Can't build calendar!"];
	else {
		pbuff = buff;
		while( (c=fgetc(calCmd)) != EOF )
			*(pbuff++)=c;
		*pbuff='\0';
		pclose(calCmd);
		[theCalView setStringValue:buff];
		}
    
 	[theCalView display];

	return self;
}



// LASTMONTH
- lastMonth:sender
{
    
    if ( --month == 0 ) {
		month = 12;
		[self lastYear:sender];
		}
	else
		[self rebuildCalView:sender];

	return self;
}


// LASTYEAR
- lastYear:sender
{

       year--;
	if (year<1900) year =1900; /* Does not go lower then that year */
	[self rebuildCalView:sender];

    return self;
}


// NEXTMONTH
- nextMonth:sender
{
     	if ( month++ == 12 ) 
	      {
		month = 1;
		[self nextYear:sender];
	      }
	else
		[self rebuildCalView:sender];
  

	return self;
}


// NEXTYEAR
- nextYear:sender
{
      	year++;
	[self rebuildCalView:sender];
	return self;
}

// SETYEAR
- SetYear:sender
{
      	year = [SelectYearDisplay intValue];
	[YearPanel performClose:sender];
	[self rebuildCalView:sender];
	return self;
}


// JANUARY
- January:sender
{
    month = 1;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// FEBRUARY
- February:sender
{
    month = 2;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// MARCH
- March:sender
{
    month = 3;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// APRIL
- April:sender
{
    month = 4;
    [MonthPanel performClose:sender]; 
    [self rebuildCalView:sender];
    return self;
}


// MAY
- May:sender
{
    month = 5;
    [MonthPanel performClose:sender]; 
    [self rebuildCalView:sender];
    return self;
}


// JUNE
- June:sender
{
    month = 6;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// JULY
- July:sender
{
    month = 7;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// AUGUST
- August:sender
{
    month = 8;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// SEPTEMBER
- September:sender
{
    month = 9;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// OCTOBER
- October:sender
{
    month = 10;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// NOVEMBER
- November:sender
{
    month = 11;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// DECEMBER
- December:sender
{
    month = 12;
    [MonthPanel performClose:sender];
    [self rebuildCalView:sender];
    return self;
}


// PRINTCALENDAR
- PrintCalendar:sender
// This method will print the theCalView to the printer
// Printing is rather simple; just send printPSCode: to the text view
// you wish to print. The print panel will automatically pop up and unless
// the user cancels the printout the text view will be printed.
{
    
    [theCalView printPSCode:self];
    return self;
 }


@end
