unit CalSetup;

interface

uses
	Windows, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons;

type
	TSetup = class(TForm)
		gbToolTip: TGroupBox;
		gbReminder: TGroupBox;
		btnTTTest1: TButton;
		btnrTest: TButton;
		gbHolidays: TGroupBox;
		rbOutlook: TRadioButton;
		rbICS: TRadioButton;
		chbShowHolidays: TCheckBox;
		chbReminders: TCheckBox;
		chbTooltip: TCheckBox;
		FontDialog1: TFontDialog;
		ColorDialog1: TColorDialog;
		OpenDialog1: TOpenDialog;
		txtICSFile: TEdit;
		sbtnICSFile: TSpeedButton;
		btnTTFont: TButton;
		btnTTFontColor: TButton;
		btnrFont: TButton;
		btnrFontColor: TButton;
		btnTTBGColor: TButton;
		btnrBGColor: TButton;
		chbBGImage: TCheckBox;
		btnTTBorderColor: TButton;
		txtTTRoundCorners: TEdit;
		lblTTRoundCorners: TLabel;
		txtRBGImageFile: TEdit;
		sbtnRBGImage: TSpeedButton;
		chbFitImage: TCheckBox;
		btnrBorderColor: TButton;
		txtRRoundCorners: TEdit;
		lblrRoundCorners: TLabel;
		btnSave: TButton;
		btnTTTest2: TButton;
		procedure chbShowHolidaysClick(Sender: TObject);
		procedure rbOutlookClick(Sender: TObject);
		procedure btnSaveClick(Sender: TObject);
		procedure rbICSClick(Sender: TObject);
		procedure chbTooltipClick(Sender: TObject);
		procedure chbRemindersClick(Sender: TObject);
		procedure FormCreate(Sender: TObject);
		procedure chbBGImageClick(Sender: TObject);
		procedure btnrTestClick(Sender: TObject);
		procedure btnTTTest1MouseDown(Sender: TObject; Button: TMouseButton;
			Shift: TShiftState; X, Y: Integer);
		procedure btnTTFontColorClick(Sender: TObject);
		procedure btnTTBGColorClick(Sender: TObject);
		procedure btnTTBorderColorClick(Sender: TObject);
		procedure btnTTFontClick(Sender: TObject);
		procedure btnrFontClick(Sender: TObject);
		procedure btnrFontColorClick(Sender: TObject);
		procedure btnrBGColorClick(Sender: TObject);
		procedure btnrBorderColorClick(Sender: TObject);
		procedure sbtnRBGImageClick(Sender: TObject);
		procedure sbtnICSFileClick(Sender: TObject);
		procedure FontDialog1Show(Sender: TObject);
		procedure ColorDialog1Show(Sender: TObject);
		procedure btnTTTest2MouseDown(Sender: TObject; Button: TMouseButton;
			Shift: TShiftState; X, Y: Integer);
		procedure OpenDialog1Show(Sender: TObject);
	private
		Param: PChar;
    Appts: TStrings;
		TTFontName: String; 
		TTFontSize: Integer;
		TTFontColor: TColor;
		TTBGColor: TColor;
		TTBorderColor: TColor;
		rBGColor: TColor;
		rBorderColor: TColor;
		rFontName: String;
		rFontColor: TColor;
		rFontSize: Integer;
		procedure ToggleHoliday(State: Boolean);
		procedure ToggleToolTip(State: Boolean);
		procedure ToggleReminder(State: Boolean);
		procedure ToggleBGImage(State: Boolean);
		procedure RegLoad;
		procedure RegSave;
	public
		function Execute: Boolean;
	end;

const
  RegPath = '\Software\The Coolmon Project\Calender';

var
	Setup: TSetup;

implementation
       
{$R *.dfm}

uses
	CalPopup, Registry;

Function TSetup.Execute: Boolean;
begin
	Result := (ShowModal = mrOK);
end;

procedure TSetup.chbShowHolidaysClick(Sender: TObject);
begin
	ToggleHoliday(chbShowHolidays.Checked);
end;

procedure TSetup.rbOutlookClick(Sender: TObject);
begin
	chbReminders.Enabled := True;
	chbTooltip.Enabled := True;
	ToggleToolTip(chbTooltip.Checked);
	ToggleReminder(chbReminders.Checked);
	sbtnICSFile.Enabled := False;
	txtICSFile.Enabled := False;
end;

procedure TSetup.btnSaveClick(Sender: TObject);
begin
	try
		RegSave;
	finally
		self.Close;
	end;
end;

procedure TSetup.rbICSClick(Sender: TObject);
begin
	chbReminders.Enabled := False;
	chbTooltip.Enabled := True;
	ToggleToolTip(chbTooltip.Checked);
	ToggleReminder(False);
	sbtnICSFile.Enabled := True;
	txtICSFile.Enabled := True;
end;

procedure TSetup.chbTooltipClick(Sender: TObject);
begin
	ToggleToolTip(chbTooltip.Checked);
end;

procedure TSetup.chbRemindersClick(Sender: TObject);
begin
	ToggleReminder(chbReminders.Checked);
end;  

procedure TSetup.ToggleHoliday(State: Boolean);
begin
	if State then
	begin
		rbOutlook.Enabled := True;
		rbICS.Enabled := True;
		if rbOutlook.Checked then
		begin
			chbReminders.Enabled := True;
			chbTooltip.Enabled := True;
			ToggleToolTip(chbTooltip.Checked);
			ToggleReminder(chbReminders.Checked);
		end
			else if rbICS.Checked then
		begin
			sbtnICSFile.Enabled := True;
			txtICSFile.Enabled := True;
			chbReminders.Enabled := False;
			chbTooltip.Enabled := True;
			ToggleToolTip(chbTooltip.Checked);
			ToggleReminder(False);
		end
			else
		begin
			sbtnICSFile.Enabled := False;
			txtICSFile.Enabled := False;
			chbReminders.Enabled := False;
			chbTooltip.Enabled := False;
			ToggleToolTip(False);
			ToggleReminder(False);
		end;
	end
		else
	begin
		chbReminders.Enabled := False;
		chbTooltip.Enabled := False;
		ToggleToolTip(False);
		ToggleReminder(False);
		rbOutlook.Enabled := False;
		rbICS.Enabled := False;
		sbtnICSFile.Enabled := False;
		txtICSFile.Enabled := False;
	end;
end;

procedure TSetup.ToggleToolTip(State: Boolean);
begin
	btnTTBGColor.Enabled := State;
	btnTTBorderColor.Enabled := State;
	btnTTFont.Enabled := State;
	btnTTFontColor.Enabled := State;
	btnTTTest1.Enabled := State;
	btnTTTest2.Enabled := State;
	txtTTRoundCorners.Enabled := State;
end;

procedure TSetup.ToggleReminder(State: Boolean);
begin
	btnRBGColor.Enabled := State;
	btnRBorderColor.Enabled := State;
	btnRFont.Enabled := State;
	btnRFontColor.Enabled := State;
	btnRTest.Enabled := State;
	txtRRoundCorners.Enabled := State;
	chbBGImage.Enabled := State;
	if chbBGImage.Enabled then
		ToggleBGImage(chbBGImage.Checked)
	else
		ToggleBGImage(False);
end;

procedure TSetup.ToggleBGImage(State: Boolean);
begin
	chbFitImage.Enabled := State;
	txtRBGImageFile.Enabled := State;
	sbtnRBGImage.Enabled := State;
end;

procedure TSetup.RegLoad;
var
  Reg: TRegistry;
begin
	TTBGColor := clGray;
	TTFontSize := 8;
	rBGColor := clGray;
	rFontSize := 10;

  Reg := TRegistry.Create;
  With Reg do
  try
    if OpenKey(RegPath, False) then
		begin
			//Holidays
      if UpperCase(ReadString('ShowHolidays')) = 'TRUE' then
        chbShowHolidays.Checked := True;
      if UpperCase(ReadString('HolidayType')) = 'OUTLOOK' then
        rbOutlook.Checked := True
      else if UpperCase(ReadString('HolidayType')) = 'ICS' then
        rbICS.Checked := True;
      txtICSFile.Text := ReadString('HolidayICS');

			//ToolTip
      if UpperCase(ReadString('ToolTips')) = 'TRUE' then
        chbToolTip.Checked := True;
      TTBGColor := StringToColor(ReadString('TTBGColor'));
      TTBorderColor := StringToColor(ReadString('TTBorderColor'));
			TTFontName := ReadString('TTFontName');
			TTFontSize := ReadInteger('TTFontSize');
      TTFontColor := StringToColor(ReadString('TTFontColor'));
      txtTTRoundCorners.Text := IntToStr(ReadInteger('TTRoundCorners'));

			//Reminder
      if UpperCase(ReadString('Reminders')) = 'TRUE' then
        chbReminders.Checked := True;
      rBGColor := StringToColor(ReadString('rBGColor'));
      rBorderColor := StringToColor(ReadString('rBorderColor'));
			rFontName := ReadString('rFontName');
			rFontSize := ReadInteger('rFontSize');
      rFontColor := StringToColor(ReadString('rFontColor'));
      txtrRoundCorners.Text := IntToStr(ReadInteger('rRoundCorners'));
      if UpperCase(ReadString('UseBGImage')) = 'TRUE' then
        chbBGImage.Checked := True;
      txtrBGImageFile.Text := ReadString('rBGImage');
      if UpperCase(ReadString('rFitImage')) = 'TRUE' then
        chbFitImage.Checked := True;

      CloseKey;
		end;
	finally
		Reg.Free;
	end;
end;

procedure TSetup.RegSave;
var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create;
  with Reg do
	try
    if OpenKey(RegPath, True) then
		begin
			//Holidays
			if chbShowHolidays.Checked then
				WriteString('ShowHolidays','True')
			else
				WriteString('ShowHolidays', 'False');
			if rbOutlook.Checked then
				WriteString('HolidayType','Outlook')
			else if rbICS.Checked then
				WriteString('HolidayType','ICS')
			else
				WriteString('HolidayType', 'None');
			WriteString('HolidayICS', txtICSFile.Text);

			//ToolTip
			if chbToolTip.Checked then
				WriteString('ToolTips', 'True')
			else
				WriteString('ToolTips', 'False');
			WriteString('TTBGColor', ColorToString(TTBGColor));
			WriteString('TTBorderColor', ColorToString(TTBorderColor));
			WriteString('TTFontName', TTFontName);
			WriteInteger('TTFontSize', TTFontSize);
			WriteString('TTFontColor', ColorToString(TTFontColor));
			try
				WriteInteger('TTRoundCorners', StrToInt(txtTTRoundCorners.Text));
			except
				WriteInteger('TTRoundCorners', 0);
			end;

			//Reminder
			if chbReminders.Checked then
				WriteString('Reminders', 'True')
			else
				WriteString('Reminders', 'False');
			WriteString('rBGColor', ColorToString(rBGColor));
			WriteString('rBorderColor', ColorToString(rBorderColor));
			WriteString('rFontName', rFontName);
			WriteInteger('rFontSize', rFontSize);
			WriteString('rFontColor', ColorToString(rFontColor));
			try
				WriteInteger('rRoundCorners', StrToInt(txtrRoundCorners.Text));
			except
				WriteInteger('rRoundCorners', 0);
			end;
			if chbBGImage.Checked then
				WriteString('UseBGImage', 'True')
			else
				WriteString('UseBGImage', 'False');
			WriteString('rBGImage', txtrBGImageFile.Text);
			if chbFitImage.Checked then
				WriteString('rFitImage','True')
			else
				WriteString('rFitImage','False');

      CloseKey;
		end;
	finally
		Reg.Free;
	end;
end;

procedure TSetup.FormCreate(Sender: TObject);
begin
	RegLoad;
	ToggleHoliday(chbShowHolidays.Checked);

  Appts := TStringList.Create;
end;

procedure TSetup.chbBGImageClick(Sender: TObject);
begin
	ToggleBGImage(chbBGImage.Checked);
end;

procedure TSetup.btnrTestClick(Sender: TObject);
var
	Reminder: TPopup;
begin
	Reminder := TPopup.Create(nil);
	try
		with Reminder do
		begin
			Setup := True;
			IsReminder := True;
			EntryID := '';
			Title := '3:00p - 5:00p Appointment (Location)';
			TimeTill := 10;
			if chbBGImage.Checked then
				ImagePath := txtrBGImageFile.Text;
			FitImage := chbFitImage.Checked;
			BGColor := rBGColor;
			BorderColor := rBorderColor;
			FontName := rFontName;
			FontSize := rFontSize;
			FontColor := rFontColor;
			try
				RoundCorners := StrToInt(txtRRoundCorners.Text);
			except
				RoundCorners := 0; 
			end;     
			Execute;
		end;
	finally
		FreeAndNil(Reminder);
	end;
end;

procedure TSetup.btnTTTest1MouseDown(Sender: TObject;
		Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
	ToolTip: TPopup;
begin
	Appts.Clear;
	Appts.Add('11:00a - 2:00p First Appointment (Location)');
	Appts.Add('8:30p - 9:00p Second Appointment');

	ToolTip := TPopup.Create(nil);
	try
		With ToolTip do
		begin
			Title := '[Today] ' + FormatDateTime('dddd | mmmm dd, yyyy' , Now);
			Appt := Appts;
			BGColor := TTBGColor;
			BorderColor := TTBorderColor;
			try
				RoundCorners := StrToInt(txtTTRoundCorners.Text);
			except
				RoundCorners := 0;
			end;
			FontName := TTFontName;
			FontSize := TTFontSize;
			FontColor := TTFontColor;
			Execute;
		end;
	finally
		FreeAndNil(ToolTip);
	end;
end;

procedure TSetup.btnTTFontColorClick(Sender: TObject);
begin
	Param := 'ToolTip Font Color';
	ColorDialog1.Color := TTFontColor;
	if ColorDialog1.Execute then
		TTFontColor := ColorDialog1.Color;
end;

procedure TSetup.btnTTBGColorClick(Sender: TObject);
begin                     
	Param := 'ToolTip Background Color';
	ColorDialog1.Color := TTBGColor;
	if ColorDialog1.Execute then
		TTBGColor := ColorDialog1.Color;
end;

procedure TSetup.btnTTBorderColorClick(Sender: TObject);
begin             
	Param := 'ToolTip Border Color';
	ColorDialog1.Color := TTBorderColor;
	if ColorDialog1.Execute then
		TTBorderColor := ColorDialog1.Color;
end;

procedure TSetup.btnTTFontClick(Sender: TObject);
begin
	Param := 'ToolTip Font';
	FontDialog1.Font.Name := TTFontName;
	FontDialog1.Font.Size := TTFontSize;
	if FontDialog1.Execute then
	begin
		TTFontName := FontDialog1.Font.Name;
		TTFontSize := FontDialog1.Font.Size;
	end;
end;

procedure TSetup.btnrFontClick(Sender: TObject);
begin
	Param := 'Reminder Font';
	FontDialog1.Font.Name := rFontName;
	FontDialog1.Font.Size := rFontSize;
	if FontDialog1.Execute then
	begin
		rFontName := FontDialog1.Font.Name;
		rFontSize := FontDialog1.Font.Size;
	end;
end;

procedure TSetup.btnrFontColorClick(Sender: TObject);
begin                 
	Param := 'Reminder Font Color';
	ColorDialog1.Color := rFontColor;
	if ColorDialog1.Execute then
		rFontColor := ColorDialog1.Color;
end;

procedure TSetup.btnrBGColorClick(Sender: TObject);
begin              
	Param := 'Reminder Background Color';
	ColorDialog1.Color := rBGColor;
	if ColorDialog1.Execute then
		rBGColor := ColorDialog1.Color;
end;

procedure TSetup.btnrBorderColorClick(Sender: TObject);
begin                  
	Param := 'Reminder Border Color';
	ColorDialog1.Color := rBorderColor;
	if ColorDialog1.Execute then
		rBorderColor := ColorDialog1.Color;
end;

procedure TSetup.sbtnRBGImageClick(Sender: TObject);
begin
	OpenDialog1.FileName := txtrBGImageFile.Text;
	OpenDialog1.Filter := 'Supported Image Files|*.jpg;*.jpeg;*.png;*.bmp|Jpeg (*.jpg,*.jpeg)|*.jpg;*.jpeg|PNG (*.png)|*.png|Bitmap (.bmp)|*.bmp';
	OpenDialog1.FilterIndex := 1;
	OpenDialog1.Title := 'Reminder Background Image';
	if OpenDialog1.Execute then
		txtrBGImageFile.Text := OpenDialog1.FileName;
end;

procedure TSetup.sbtnICSFileClick(Sender: TObject);
begin
	OpenDialog1.FileName := txtICSFile.Text;
	OpenDialog1.Filter := 'ICS Files (*.ics)|*.ics';
	OpenDialog1.FilterIndex := 1;
	OpenDialog1.Title := 'Holiday ICS';
	if OpenDialog1.Execute then
		txtICSFile.Text := OpenDialog1.FileName;
end;

procedure TSetup.FontDialog1Show(Sender: TObject);
begin
	SetWindowText(FontDialog1.Handle, Param);
	ShowWindow(GetDlgItem(FontDialog1.Handle, 1091), SW_HIDE);
	ShowWindow(GetDlgItem(FontDialog1.Handle, 1139), SW_HIDE);
	ShowWindow(GetDlgItem(FontDialog1.Handle, 1094), SW_HIDE);
	ShowWindow(GetDlgItem(FontDialog1.Handle, 1140), SW_HIDE);
	SetWindowPos(FontDialog1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
end;

procedure TSetup.ColorDialog1Show(Sender: TObject);
begin
	SetWindowText(ColorDialog1.Handle, Param);
	SetWindowPos(ColorDialog1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
end;

procedure TSetup.btnTTTest2MouseDown(Sender: TObject;
	Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var              
	ToolTip: TPopup;
begin
	ToolTip := TPopup.Create(nil);
	try
		With ToolTip do
		begin
			Title := FormatDateTime('dddd | mmmm dd, yyyy' , EncodeDate(CurrentYear, 10, 31)) + ' - Halloween';
			BGColor := TTBGColor;
			BorderColor := TTBorderColor;
			try
				RoundCorners := StrToInt(txtTTRoundCorners.Text);
			except
				RoundCorners := 0;
			end;
			FontName := TTFontName;
			FontSize := TTFontSize;
			FontColor := TTFontColor;
			Execute;
		end;
	finally
		FreeAndNil(ToolTip);
	end;
end;

procedure TSetup.OpenDialog1Show(Sender: TObject);
begin
	SetWindowPos(OpenDialog1.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);
end;

end.


