// HRegEXTextview.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 into gtkextensions

using System;
using System.Text.RegularExpressions;
using Mono.Unix;

namespace HollyLibrary
{
	[System.ComponentModel.ToolboxItem(true)]
	public partial class HRegExTextview : Gtk.Bin
	{
		private enum ImageType
		{
			Error, 
			Ok
		}
		
		public event EventHandler Changed;
		
		string regularExpression = "";
		string errorMessage      = Catalog.GetString("Error");
		string okMessage         = "Ok!";
		
		public HRegExTextview ()
		{
			this.Build ();
			this.TextBox.Buffer.Changed += this.OnTextBoxChanged;
			this.TextBox.FocusInEvent  += new Gtk.FocusInEventHandler ( delegate { this.QueueDraw(); } );
			this.TextBox.FocusOutEvent += new Gtk.FocusOutEventHandler( delegate { this.QueueDraw(); } );
			this.OnTextBoxChanged(null, null);
		}
		
		/*
		 * TODO
		protected override bool OnExposeEvent (Gdk.EventExpose evnt)
		{
			Gdk.Rectangle rect = new Gdk.Rectangle( Allocation.X, Allocation.Y, Allocation.Width , Allocation.Height );
			
			//
			//Gtk.Style.PaintFlatBox( TextBox.Style, this.GdkWindow, TextBox.State, TextBox.ShadowType, this.Allocation, TextBox, "entry_bg", rect.X, rect.Y, rect.Width, rect.Height );
			//Gtk.Style.PaintShadow ( TextBox.Style, this.GdkWindow, TextBox.State, TextBox.ShadowType, this.Allocation, TextBox, "entry"   , rect.X, rect.Y, rect.Width, rect.Height );
			
			//Gtk.Style.PaintFlatBox( TextBox.Style, this.GdkWindow, TextBox.State, Gtk.ShadowType.In, this.Allocation, TextBox, "entry_bg", rect.X, rect.Y, rect.Width, rect.Height );
			Gtk.Style.PaintShadow ( TextBox.Style, this.GdkWindow, TextBox.State, Gtk.ShadowType.In, this.Allocation, TextBox, "entry"   , rect.X, rect.Y, rect.Width, rect.Height );
			
			return base.OnExposeEvent (evnt);
		}
		*/
		
		private void SetImage( ImageType type )
		{
			if( type == ImageType.Error )
			{
				if (this.ShowErrorTooltip == true)
					HToolTip.SetToolTip( EbErrorImage, Catalog.GetString("Error"), errorMessage, "gtk-dialog-warning" );
				else
					HToolTip.UnsetToolTip(EbErrorImage);
				ErrorImage.Stock        	= this.StockImageError;
				ErrorImage.Visible 			= this.ShowErrorImage;
			}
			else
			{
				if (this.ShowOKTooltip == true)
					HToolTip.SetToolTip( EbErrorImage, Catalog.GetString("Ok"), okMessage, "gtk-yes" );
				else
					HToolTip.UnsetToolTip(EbErrorImage);
				ErrorImage.Stock         	=	this.StockImageOK;
				ErrorImage.Visible 			= this.ShowOkImage;
			}
		}
		
		protected virtual void OnTextBoxChanged (object sender, System.EventArgs e)
		{
			if( IsTextValid )
				SetImage(ImageType.Ok);
			else
				SetImage(ImageType.Error);
			if( Changed != null ) 
				Changed( sender, e );
		}
		
		public Gtk.TextView TextBox
		{
			get {return this.text_view;}
		}
		
		public string Text
		{
			get
			{
				return this.TextBox.Buffer.Text;
			}
			set
			{
				this.TextBox.Buffer.Text = value;
			}
		}
		
		public bool IsTextValid
		{
			get
			{
				Regex rx;
				try
				{
					rx = new Regex(RegularExpression);
				}
				catch
				{
					return false;
				}
				
				return rx.IsMatch( this.Text );
			}
		}
		
		public string RegularExpression 
		{
			get 
			{
				return regularExpression;
			}
			set 
			{
				regularExpression = value;
				this.OnTextBoxChanged(null, null);
			}
		}
		
		public string OkMessage {
			get {
				return okMessage;
			}
			set {
				okMessage = value;
			}
		}

		public string ErrorMessage {
			get {
				return errorMessage;
			}
			set {
				errorMessage = value;
			}
		}
		
		private bool show_ok_image = true;
		public bool ShowOkImage
		{
			get {return this.show_ok_image;}
			set 
			{
				this.show_ok_image = value;
				this.OnTextBoxChanged(null, null);
			}
		}
		
		
		private bool show_error_image = true;
		public bool ShowErrorImage
		{
			get {return this.show_error_image;}
			set 
			{
				this.show_error_image = value;
				this.OnTextBoxChanged(null, null);
			}
		}
		
		
		private string stock_image_ok = "gtk-yes";
		public string StockImageOK
		{
			get {return this.stock_image_ok;}
			set 
			{
				this.stock_image_ok = value;
				this.OnTextBoxChanged(null, null);
			}
		}
		
		private string stock_image_error = "gtk-dialog-warning";
		public string StockImageError
		{
			get {return this.stock_image_error;}
			set 
			{
				this.stock_image_error = value;
				this.OnTextBoxChanged(null, null);
			}
		}
		
		private bool show_error_tooltip = true;
		public bool ShowErrorTooltip
		{
			get {return this.show_error_tooltip;}
			set
			{
				
				this.show_error_tooltip = value;
				this.OnTextBoxChanged(null, null);
			}
		}
		
		private bool show_ok_tooltip = true;
		public bool ShowOKTooltip
		{
			get {return this.show_ok_tooltip;}
			set
			{
				
				this.show_ok_tooltip = value;
				this.OnTextBoxChanged(null, null);
			}
		}
	}
}

