// HRegExEntry.cs created with MonoDevelop
// User: dantes at 1:20 PM 5/20/2008
//

//TODO transfer this int gtkextensions and replace by old copy

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

namespace HollyLibrary
{
	
	
	[System.ComponentModel.ToolboxItem(true)]
	public partial class HRegExEntry : Gtk.Bin
	{
		private enum ImageType
		{
			Error, Ok
		}
		
		public event EventHandler Changed;
		
		string regularExpression = "";
		string errorMessage      = Catalog.GetString("Error");
		string okMessage         = Catalog.GetString("Ok");
		
		public HRegExEntry()
		{
			this.Build();
			this.TextBox.FocusInEvent  += new Gtk.FocusInEventHandler ( delegate { this.QueueDraw(); } );
			this.TextBox.FocusOutEvent += new Gtk.FocusOutEventHandler( delegate { this.QueueDraw(); } );
			this.OnTextBoxChanged(null, null);
			this.TextBox.HasFrame = true;
		}
		
		/*
		 * 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 );
		}

#region entry properties
		public Gtk.Entry Entry
		{
			get
			{
				return TextBox;
			}
		}
		
		public string Text
		{
			get
			{
				return TextBox.Text;
			}
			set
			{
				TextBox.Text = value;
			}
		}
#endregion

#region properties
		public bool IsTextValid
		{
			get
			{
				Regex rx;
				try
				{
					rx = new Regex(RegularExpression);
				}
				catch
				{
					return false;
				}
				
				return rx.IsMatch( TextBox.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.Stock.DialogWarning;
		public string StockImageError
		{
			get {return this.stock_image_error;}
			set 
			{
				this.stock_image_error = 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);
			}
		}
		
		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);
			}
		}
		
		
		
#endregion	

	}
}
