// PageLayout.cs created with MonoDevelop
//
//    QuickStep: Acquire, view and identify spectra 
//    Copyright (C) 2012  Thomas Klose & Torsten Schache, Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


//TODO transfer this int gtkextensions

using System;
using Cairo;
using Gtk;



namespace HollyLibrary
{
	public enum PaperOrientation
	{
		Normal,
		Landscapeal,
	}
	
	public enum PaperFormat
	{
		A4,
		Letter,
	}
	
	public class PageLayout : Gtk.Alignment
	{
		private const int A4Height 		= 297;
		private const int A4Widht		= 210;
		
		private const int LetterHeight	= 279;
		private const int LetterWidth	= 216;
		
		private Cairo.Color background_color 	= new Cairo.Color(0.8, 0.8, 0.8, 1);
		private Cairo.Color paper_color 		= new Cairo.Color(1, 1, 1, 1);
		private Cairo.Color shadow_color 		= new Cairo.Color(0.3, 0.3, 0.3, 1);
		private Cairo.Color activ_color 		= new Cairo.Color(1, 1, 1, 1);
		
		private int page_shadow_offset 	= 10;
		private int page_left_offset 	= 30;
		private int page_right_offset 	= 30;
		private int page_top_offset 	= 30;
		private int page_bottom_offset 	= 30;	
		
		public PageLayout (PaperFormat format, PaperOrientation orientation) : base(0, 0, 0, 0)
		{
			Gtk.Table table = new Gtk.Table(3,3,false);
			
			this.CurrentPaperFormat = format;
			this.CurrentPaperOrientation = orientation;
			
			this.LeftRuler = new GraficRuler(RulerPosition.Left, this.CurrentPaperHeight);
			table.Attach(this.LeftRuler, 0, 1, 1, 2);
			
			this.RightRuler = new GraficRuler(RulerPosition.Right, this.CurrentPaperHeight);
			table.Attach(this.RightRuler, 2, 3, 1, 2);
			
			this.TopRuler = new GraficRuler(RulerPosition.Top, this.CurrentPaperWidth);
			table.Attach(this.TopRuler, 1, 2, 0, 1);
			
			this.BottmRuler = new GraficRuler(RulerPosition.Bottom, this.CurrentPaperWidth);
			table.Attach(this.BottmRuler, 1, 2, 2, 3);
			
			this.Page = new EmptyPage(this.CurrentPaperWidth, this.CurrentPaperHeight);
			table.Attach(this.Page, 1, 2, 1, 2);
			
			this.LTFiller = new LayoutFill(this.LeftRuler.WidthRequest, this.TopRuler.HeightRequest);
			table.Attach(this.LTFiller,0, 1, 0, 1);
			
			this.RTFiller = new LayoutFill(this.RightRuler.WidthRequest, this.TopRuler.HeightRequest);
			table.Attach(this.RTFiller,2, 3, 0, 1);
			
			this.LBFiller = new LayoutFill(this.LeftRuler.WidthRequest, this.BottmRuler.HeightRequest);
			table.Attach(this.LBFiller,0, 1, 2, 3);
			
			this.RBFiller = new LayoutFill(this.RightRuler.WidthRequest, this.BottmRuler.HeightRequest);
			table.Attach(this.RBFiller,2, 3, 2, 3);
			
			//this.BackgroundColor = new Cairo.Color(0.5, 0.5, 0.5, 1);
			//this.PaperColor = new Cairo.Color(1, 0.5, 0.5, 1);
			//this.ShadowColor = new Cairo.Color(0.5, 1, 0.5, 1);
			//this.ActiveColor = new Cairo.Color(0.5, 0.5, 1, 1);
			
			this.PageShadowOffset = 20;
			this.PageLeftOffset = 40;
			this.PageRightOffset = 40;
			this.PageTopOffset = 40;
			this.PageBottomOffset = 40;
			
			
			this.Add(table);
		}
		
		private GraficRuler LeftRuler{get;set;}
		private GraficRuler RightRuler{get;set;}
		private GraficRuler TopRuler{get;set;}
		private GraficRuler BottmRuler{get;set;}
		
		private LayoutFill LTFiller{get;set;}
		private LayoutFill RTFiller{get;set;}
		private LayoutFill LBFiller{get;set;}
		private LayoutFill RBFiller{get;set;}
		
		private EmptyPage Page{get;set;}
		
		public Cairo.Color BackgroundColor
		{
			get{return this.background_color;}	
			set
			{
				this.background_color 			= value;
				this.LTFiller.BackgroundColor 	= value;
				this.LBFiller.BackgroundColor 	= value;
				this.RTFiller.BackgroundColor 	= value;
				this.RBFiller.BackgroundColor 	= value;
				this.Page.BackgroundColor 		= value;
				this.LeftRuler.BackgroundColor	= value;
				this.RightRuler.BackgroundColor	= value;
				this.TopRuler.BackgroundColor	= value;
				this.BottmRuler.BackgroundColor	= value;
			}
		}
		
		public Cairo.Color PaperColor
		{
			get{return this.paper_color;}	
			set
			{
				this.paper_color 		= value;
				this.Page.PaperColor 	= value;
			}
		}
		
		public Cairo.Color ShadowColor
		{
			get{return this.shadow_color;}	
			set
			{
				this.shadow_color 		= value;
				this.Page.ShadowColor 	= value;
			}
		}
		
		public Cairo.Color ActiveColor
		{
			get{return this.activ_color;}
			set
			{
				this.activ_color 			= value;
				this.LeftRuler.ActiveColor	= value;
				this.RightRuler.ActiveColor	= value;
				this.TopRuler.ActiveColor	= value;
				this.BottmRuler.ActiveColor	= value;
			}
		}
		
		public int PageShadowOffset
		{
			get{return this.page_shadow_offset;}
			set
			{
				this.page_shadow_offset 	= value;
				this.Page.PageShadowOffset 	= value;
			}
		}
		
		public int PageLeftOffset
		{
			get{ return this.page_left_offset;}
			set
			{
				this.page_left_offset 			= value;
				this.Page.PageLeftOffset 		= value;
				this.TopRuler.RulerLeftOffset 	= value;
				this.BottmRuler.RulerLeftOffset = value;
			}
		}
		
		public int PageRightOffset
		{
			get{ return this.page_right_offset;}
			set
			{
				this.page_right_offset 				= value;
				this.Page.PageRightOffset 			= value;
				this.TopRuler.RulerRightOffset 		= value;
				this.BottmRuler.RulerRightOffset	= value;
			}
		}
		
		public int PageTopOffset
		{
			get{ return this.page_top_offset;}
			set
			{
				this.page_top_offset 			= value;
				this.Page.PageTopOffset 		= value;
				this.LeftRuler.RulerTopOffset 	= value;
				this.RightRuler.RulerTopOffset	= value;
			}
		}
		
		public int PageBottomOffset
		{
			get{ return this.page_bottom_offset;}
			set
			{
				this.page_bottom_offset 			= value;
				this.Page.PageBottomOffset 			= value;
				this.LeftRuler.RulerBottomOffset 	= value;
				this.RightRuler.RulerBottomOffset	= value;
			}
		}
		
		
		private PaperOrientation CurrentPaperOrientation{get;set;}
		private PaperFormat CurrentPaperFormat{get;set;}
		
		private int CurrentPaperWidth
		{
			get
			{
				int width 	= 0;
				int height 	= 0;
				
				switch(this.CurrentPaperFormat)
				{
				case PaperFormat.A4:
					width 	= A4Widht;
				 	height 	= A4Height;
					break;
					
				case PaperFormat.Letter:
					width 	= LetterWidth;
				 	height 	= LetterHeight;
					break;
					
				default:
					break;
				}
				
				return this.CurrentPaperOrientation == PaperOrientation.Normal ? width : height;
			}
		}
		
		private int CurrentPaperHeight
		{
			get
			{
				int width 	= 0;
				int height 	= 0;
				
				switch(this.CurrentPaperFormat)
				{
				case PaperFormat.A4:
					width 	= A4Widht;
				 	height 	= A4Height;
					break;
					
				case PaperFormat.Letter:
					width 	= LetterWidth;
				 	height 	= LetterHeight;
					break;
					
				default:
					break;
				}
				
				return this.CurrentPaperOrientation == PaperOrientation.Normal ? height : width;
			}
		}
	}
}

