context: /*
  
  This file is part of TorqueDev.
  
  TorqueDev 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.
  
  TorqueDev 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 TorqueDev.  If not, see <http://www.gnu.org/licenses>
  
  EXCEPTIONS TO THE GPL: TorqueDev links in a number of third party libraries,
  which are exempt from the license.  If you want to write closed-source
  third party modules that you are going to link into TorqueDev, you may do so
  without restriction.  I acknowledge that this technically allows for
  one to bypass the open source provisions of the GPL, but the real goal
  is to keep the core of TorqueDev free and open.  The rest is up to you.
  
  */
  
  using System;
  using System.Collections;
  using System.ComponentModel;
  using System.Drawing;
  using System.Data;
  using System.Windows.Forms;
  using System.IO;
  
  namespace TSDev
  {
  	/// <summary>
  	/// Summary description for UCBrowser.
  	/// </summary>
  	internal class UCBrowser : System.Windows.Forms.UserControl, RenderSet
  	{
  		public Crownwood.DotNetMagic.Controls.TabPage _parent_tab;
  		private System.Windows.Forms.ImageList ilToolbar;
  		private System.Windows.Forms.ToolTip toolTip1;
  		public WebBrowser wb;
  		private StatusStrip sbMain;
  		private ToolStripStatusLabel lblStatus;
  		private ToolStripStatusLabel toolStripStatusLabel2;
  		private ToolStripStatusLabel lblSecure;
  		private ToolStripButton tsbBack;
  		private ToolStripButton tsbForward;
  		private ToolStripButton tsbStop;
  		private ToolStripButton tsbRefresh;
  		private ToolStrip tsAddress;
  		private ToolStripLabel lblAddress;
  		private ToolStripButton cmdGo;
  		private ToolStrip tsButtons;
          private ToolStripTextBox txtAddress;
          private ToolStripProgressBar pbPage;
  		private System.ComponentModel.IContainer components;
  
  		public UCBrowser(string url, bool hideall)
  		{
  			// This call is required by the Windows.Forms Form Designer.
  			InitializeComponent();
  
  			this.SetRenderMode(g.Config.ColorPref);
  
  			wb.StatusTextChanged += new EventHandler(wb_StatusTextChanged);
              wb.EncryptionLevelChanged += new EventHandler(wb_EncryptionLevelChanged);
              wb.ProgressChanged += new WebBrowserProgressChangedEventHandler(wb_ProgressChanged);
              wb.DocumentTitleChanged += new EventHandler(wb_DocumentTitleChanged);
  			
  
  			if (hideall) {
  				this.sbMain.Visible = false;
  				this.tsAddress.Visible = false;
  				this.tsButtons.Visible = false;
  				this.wb.Dock = DockStyle.Fill;
  				this.wb.IsWebBrowserContextMenuEnabled = false;
  			}
  			
  			if (url == "welcome") {
  				// Attempt to open template file
  				string contents = "";
  				try {
  					StreamReader reader = new StreamReader(Application.StartupPath + "\\templates\\welcome_template.html");
  					contents = reader.ReadToEnd();
  
  					reader.Close();
  				} catch (Exception exc) {
  					Navigate("about:<i>" + exc.Message + "</i>");
  					return;
  				}
  
  				contents = contents.Replace("{{APP_PATH}}", "file:///" + Application.StartupPath);
  
  				// Enumerate recent files:
  				IDictionaryEnumerator enumer = g.Config.recent_files.GetEnumerator();
  
  				string recent_files = "";
  				while(enumer.MoveNext())
  					recent_files += "\t\t\t\t\t<li> <a href=\"torquedev://" + enumer.Key.ToString() + "\"" +
  						" style=\"text-decoration:none\" title=\"" + enumer.Key.ToString() + "\">" + enumer.Value.ToString() + "</a>";
  
  				contents = contents.Replace("{{RECENTFILES}}", recent_files);
  				contents = contents.Replace("{{MOTD}}", g.Config.LastMotd);
  
  				wb.Navigate("about:blank");
  				wb.Document.Write(contents);
  			} else if (url == "firstrun") {
  				// Navigate to the firstrun page
  				Navigate("http://www.torquedev.com/firstrun.php?version=" + Application.ProductVersion);
  			} else if (url.StartsWith("http://") == false) {
  				Navigate(url);
  			} else {
  				Navigate(url, "", "", "", "");
  			}
  		}
  
          void wb_DocumentTitleChanged(object sender, EventArgs e) {
              this._parent_tab.Title = wb.DocumentTitle;
              this._parent_tab.ToolTip = wb.Url.ToString();
  
              txtAddress.Text = wb.Document.Url.ToString();
          }
  
          void wb_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) {
              if (e.CurrentProgress == e.MaximumProgress) {
                  pbPage.Visible = false;
                  pbPage.Value = 0;
              } else {
                  pbPage.Visible = true;
                  pbPage.Value = (int)((Convert.ToDouble(e.CurrentProgress) / Convert.ToDouble(e.MaximumProgress)) * 100);
              }
          }
  
          void wb_EncryptionLevelChanged(object sender, EventArgs e) {
              if (wb.EncryptionLevel == WebBrowserEncryptionLevel.Insecure) {
                  this.txtAddress.BackColor = Color.White;
                  this.lblSecure.Visible = false;
              } else if (wb.EncryptionLevel == WebBrowserEncryptionLevel.Bit128 ||
                  wb.EncryptionLevel == WebBrowserEncryptionLevel.Bit40 ||
                  wb.EncryptionLevel == WebBrowserEncryptionLevel.Bit56 ||
                  wb.EncryptionLevel == WebBrowserEncryptionLevel.Fortezza) {
                  this.txtAddress.BackColor = Color.LightYellow;
                  this.lblSecure.Visible = true;
  
                  if (wb.EncryptionLevel == WebBrowserEncryptionLevel.Bit40)
                      this.lblSecure.Text = "SSL Secured (40-Bit)";
                  else if (wb.EncryptionLevel == WebBrowserEncryptionLevel.Bit56)
                      this.lblSecure.Text = "SSL Secured (56-Bit)";
                  else if (wb.EncryptionLevel == WebBrowserEncryptionLevel.Bit128)
                      this.lblSecure.Text = "SSL Secured (128-Bit)";
                  else if (wb.EncryptionLevel == WebBrowserEncryptionLevel.Fortezza)
                      this.lblSecure.Text = "SSL Secured (Fortezza)";
              } else if (wb.EncryptionLevel == WebBrowserEncryptionLevel.Mixed) {
                  this.txtAddress.BackColor = Color.LightSalmon;
                  this.lblSecure.Visible = false;
              }
          }
  
  		void wb_StatusTextChanged(object sender, EventArgs e) {
  			lblStatus.Text = wb.StatusText;
  		}
  
  		/// <summary> 
  		/// Clean up any resources being used.
  		/// </summary>
  		protected override void Dispose( bool disposing )
  		{
  			if( disposing )
  			{
  				//this.wb.Dispose();
  
  				if(components != null)
  				{
  					components.Dispose();
  				}
  			}
  			base.Dispose( disposing );
  		}
  
  		#region Component Designer generated code
  		/// <summary> 
  		/// Required method for Designer support - do not modify 
  		/// the contents of this method with the code editor.
  		/// </summary>
  		private void InitializeComponent()
  		{
  			this.components = new System.ComponentModel.Container();
  			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCBrowser));
  			this.ilToolbar = new System.Windows.Forms.ImageList(this.components);
  			this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
  			this.wb = new System.Windows.Forms.WebBrowser();
  			this.sbMain = new System.Windows.Forms.StatusStrip();
  			this.lblStatus = new System.Windows.Forms.ToolStripStatusLabel();
  			this.toolStripStatusLabel2 = new System.Windows.Forms.ToolStripStatusLabel();
  			this.pbPage = new System.Windows.Forms.ToolStripProgressBar();
  			this.lblSecure = new System.Windows.Forms.ToolStripStatusLabel();
  			this.tsButtons = new System.Windows.Forms.ToolStrip();
  			this.tsbBack = new System.Windows.Forms.ToolStripButton();
  			this.tsbForward = new System.Windows.Forms.ToolStripButton();
  			this.tsbStop = new System.Windows.Forms.ToolStripButton();
  			this.tsbRefresh = new System.Windows.Forms.ToolStripButton();
  			this.tsAddress = new System.Windows.Forms.ToolStrip();
  			this.lblAddress = new System.Windows.Forms.ToolStripLabel();
  
gt: 			this.txtAddress = new System.Windows.Forms.ToolStripTextBox();