'A simple calendar program using a binary file for random with PUT and GET
'I have annotated at great length for complete beginners.  
'The save file is created from the year in the calendar selection plus a suitable suffix 
'(here .agf as my initials)
'The position in the file is determined by the day and  month as integers as 1111 (11.11 for example)
'The amount of data entered is limited here to only 1000 bytes by defining the variable as an istring
'The main problem is that it will create a large file because some dates will be large with 
'data saved at 1112 position (representing 11th December) for example
'Define the controls
CONST CALENDAR1 = 2
CONST CLRBUTTON1 = 1 'quit button
CONST CLRBUTTON2 = 4 'save button
CONST EDIT1 = 3
CONST MCN_SELECT = (MCN_FIRST + 4) 'this controls clicks on the calendar and limits spurious changes (see in handler below)

'define main window
window main
'create the data to be entered as a datatype. 
TYPE MyDiaryInfo  'defines a datatype
     DEF somedata[1000] as istring
     def another as int 'not used, just kept a second data variable in case I wanted to save date number for example
	ENDTYPE
DEF Diary as MyDiaryInfo  'give the type a more convenient short name
'define a few more simple variables for use in the program
int m,d,y,date
string month, day, year,mysavefile,ChosenDate  'mysavefile is the file name based on the year  ChosenDate is day and month
def myfile as bfile  'make sure file type is binary 


'window is set to be always on top which might not be what is normally wanted
OpenWindow main,0,0,800,550,@SIZE|@MINBOX|@MAXBOX|@CAPTION|@SYSMENU|@topmost,0,"Adrian's Quick Calendar",&main_handler

	CalendarControl main,40,8,705,185,@BORDER,0,CALENDAR1
	Control main,@Button,"Save",644,464,60,25,0,CLRBUTTON1
	Control main,@Button,"Quit",718,464,60,25,0,CLRBUTTON2
	Control main,@Edit,"",9,201,779,250,@VSCROLL|@CTEDITMULTI,EDIT1
    'change colour and fonts of various controls and window
	SetFont main,"Times New Roman",14,400,0
	SetWindowColor main, RGB(0,128,255)
	FrontPen main, RGB(0,0,128)
	SetFont main,"System",12,700,0,CALENDAR1
	SetControlColor main,CALENDAR1,RGB(0,0,255),RGB(255,255,0)
	SetFont main,"System",12,700,0,CLRBUTTON1
	SetControlColor main,CLRBUTTON1,RGB(255,255,0),RGB(0,0,255)
	SetFont main,"System",12,700,0,CLRBUTTON2
	SetControlColor main,CLRBUTTON2,RGB(255,255,0),RGB(0,0,255)
	SetFont main,"Bookman Old Style",14,300,0,EDIT1
	SetControlColor main,EDIT1,RGB(0,0,128),RGB(255,255,128)
	Centerwindow main
		'don't close the window until its status has changed
    waituntil main=0


' Here is the main handler for the program and all the controls
			SUB main_handler
	SELECT @MESSAGE
		'wait for a message
		CASE @IDCLOSEWINDOW  'you have clicked the close cross on the window
			closefile myfile  'close any open files
			CLOSEwindow main:end   'close the window and end the program (end is probably not needed)

		CASE @IDCONTROL  'respond to things done to the various controls
				if @controlid=calendar1   'if the control is the calendar 
				if @notifycode=MCN_SELECT Then  'and if the parameters set above are true
				
				ccGetCurSel main,calendar1,m,d,y  'read the selected date as integers on the calendar control
				setcontroltext main,edit1,""  'blank the edit box by printing nothing there
				gosub getfile  'go to sub routine to open anything stored with this date in binary file
				endif
				endif
				
					 
			SELECT @CONTROLID  'select the identity of other controls like buttons
				CASE clrbutton2
					IF @NOTIFYCODE = 0
						/*button clicked*/ 'quit button so go to quit sub routine
					gosub quitme
				
					ENDIF

					CASE clrbutton1
					IF @NOTIFYCODE = 0
						/*button clicked*/ 'save button so go to save subroutine
					gosub savefile
					ENDIF
					
			ENDSELECT  'stop selecting identity of controls
	ENDSELECT          'stop selecting for messages
RETURN
ENDSUB


sub savefile
					

					ccGetCurSel main, 2, m, d, y  'read calendar selection
					mysavefile=str$(y)+".agf"     'create file name based on year and suffix stated
					month=str$(m):day=str$(d):year=str$(y)  'change integer dates into strings
					month=using("0#",m):day=using("0#",d):year=using("0#",y) 'filter out blanks in date strings
					ChosenDate = day+month  'make the chosendate represent the day and month (1010.. 10th Oct for example)
                    diary.somedata=getcontroltext(main,edit1) 'put whatever is typed in the editbox as a string of type 'diary'
					date=val(chosendate) 'creates a number for the save position in file based on the day and month
					if (OPENFILE(myfile,mysavefile,"A") = 0) 'open the file to append
					PUT myfile,date,diary 'put the data into the file at date position and contents of type diary
					closefile myfile
					setcontroltext main,edit1,"" 'cls after save to show something has happened.. could flash word 'saved' instead
					endif
					RETURN
					ENDSUB

				sub getfile  'retrieves information from the file
				somedata=""
				setcontroltext main,edit1,"" 
				ccGetCurSel main, 2, m, d, y  'check current date selection
					mysavefile=str$(y)+".agf"  'redefine file name 
					month=str$(m):day=str$(d):year=str$(y) 'etc
					month=using("0#",m):day=using("0#",d):year=using("0#",y)'remove spaces in strings
					ChosenDate = day+month
                    date=val(chosendate)
					
				IF(OPENFILE(myfile,mysavefile,"R") = 0) 'open file for reading
						

					get myfile,date,diary 'get data from saved position for this date
					setcontroltext main,edit1,diary.somedata  'retrieves this item from date position in file
					closefile myfile
					endif
					
RETURN
endsub

sub quitme
'use a messagebox to allow for changing mind
IF MESSAGEBOX(main,"Are you sure you want to quit?","Abandon without saving",@MB_YESNO) = @IDyes
closefile myfile
closewindow main:end
endif
RETURN
endsub

