- Main Page
- Classes
- Files

- File List

# C:/Users/Andreas Schiffler/Desktop/SVN/NewGamePhysics/InfoBrowser/InfoBrowserForm.cs

    00001 ﻿using System;
    00002 using System.IO;
    00003 using System.Collections.Generic;
    00004 using System.ComponentModel;
    00005 using System.Data;
    00006 using System.Drawing;
    00007 using System.Linq;
    00008 using System.Text;
    00009 using System.Windows.Forms;
    00010
    00011 using NewGamePhysics.Networking;
    00012
    00013 namespace InfoBrowser
    00014 {
    00015     public partial class InfoBrowserForm : Form
    00016     {
    00021         delegate void AddInfoLinkCallback(InfoLink infoLink);
    00022
    00026         InfoLinkReceiver infoLinkReceiver;
    00027
    00031         public InfoBrowserForm()
    00032         {
    00033             InitializeComponent();
    00034
    00035             this.toolStripStatusLabel1.Text = string.Empty;
    00036
    00037             this.toolStripButton1.Image = new Bitmap(@"Icons\back.gif");
    00038             this.toolStripButton1.Enabled = false;
    00039             this.webBrowser1.CanGoBackChanged += new EventHandler(webBrowser1_CanGoBackChanged);
    00040
    00041             this.toolStripButton2.Image = new Bitmap(@"Icons\reload.gif");
    00042             this.toolStripButton2.Enabled = false;
    00043
    00044             this.toolStripButton3.Image = new Bitmap(@"Icons\forward.gif");
    00045             this.toolStripButton3.Enabled = false;
    00046             this.webBrowser1.CanGoForwardChanged += new EventHandler(webBrowser1_CanGoForwardChanged);
    00047
    00048             this.toolStripButton4.Image = new Bitmap(@"Icons\stop.gif");
    00049             this.toolStripButton4.Enabled = false;
    00050
    00051             this.webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
    00052             this.webBrowser1.Navigated += new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);
    00053             this.webBrowser1.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser1_ProgressChanged);
    00054             this.webBrowser1.DocumentTitleChanged += new EventHandler(webBrowser1_DocumentTitleChanged);
    00055
    00056             this.treeViewInfoLinks.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
    00057             // this.treeView1.Font = new Font(FontFamily.GenericSansSerif, 8.0f, FontStyle.Bold);
    00058
    00059             // Start the UDP background receiver
    00060             this.infoLinkReceiver = new InfoLinkReceiver(ReceiverCallback);
    00061             this.infoLinkReceiver.StartListener();
    00062
    00063             // Add an initial link
    00064             AddInfoLinkToTree(new InfoLink(
    00065                 "General Topics",
    00066                 "Physics",
    00067                 "What is Physics?",
    00068                 new Uri("http://en.wikipedia.org/wiki/Physics")));
    00069
    00070             // Write welcome message into browser and clear URL
    00071             string html = File.ReadAllText("Welcome.htm");
    00072             WriteHtmlIntoBrowser(html);
    00073             this.textBox1.Text = string.Empty;
    00074
    00075         }
    00076
    00081         private void WriteHtmlIntoBrowser(string html)
    00082         {
    00083             this.webBrowser1.AllowNavigation = false;
    00084             if (this.webBrowser1.Document != null)
    00085             {
    00086                 this.webBrowser1.Document.OpenNew(true);
    00087             }
    00088             else
    00089             {
    00090                 this.webBrowser1.Navigate("about:blank");
    00091             }
    00092
    00093             this.webBrowser1.Document.Write(html);
    00094             this.webBrowser1.AllowNavigation = true;
    00095         }
    00096
    00101         void ReceiverCallback(InfoLink infoLink)
    00102         {
    00103             // Check if this method is running on a different thread
    00104             // than the thread that created the control.
    00105             if (this.treeViewInfoLinks.InvokeRequired)
    00106             {
    00107                 // It's on a different thread, so use Invoke.
    00108                 AddInfoLinkCallback d =
    00109                     new AddInfoLinkCallback(AddInfoLinkToTree);
    00110                 this.Invoke
    00111                     (d, new object[] { infoLink });
    00112             }
    00113             else
    00114             {
    00115                 // It's on the same thread, no need for Invoke
    00116                 AddInfoLinkToTree(infoLink);
    00117             }
    00118         }
    00119
    00125         private void AddInfoLinkToTree(InfoLink infoLink)
    00126         {
    00127             lock (this)
    00128             {
    00129                 // Validate info link
    00130                 if ((null == infoLink) ||
    00131                     (string.IsNullOrEmpty(infoLink.Title)) ||
    00132                     (null == infoLink.Uri) ||
    00133                     (string.IsNullOrEmpty(infoLink.Uri.ToString())))
    00134                 {
    00135                     this.toolStripStatusLabel1.Text = "Dropped invalid InfoLink.";
    00136                     return;
    00137                 }
    00138
    00139                 // Get category
    00140                 string category = string.Empty;
    00141                 if (string.IsNullOrEmpty(infoLink.Category))
    00142                 {
    00143                     category = "Uncategorized";
    00144                 }
    00145                 else
    00146                 {
    00147                     category = infoLink.Category;
    00148                 }
    00149
    00150                 // Find category node
    00151                 TreeNode categoryNode = null;
    00152                 foreach (TreeNode node in this.treeViewInfoLinks.Nodes)
    00153                 {
    00154                     if ((null != node) &&
    00155                         (node.Name.Equals(category)))
    00156                     {
    00157                         categoryNode = node;
    00158                         break;
    00159                     }
    00160                 }
    00161
    00162                 // Maybe create new category node
    00163                 // (adding spaces since we bold it and text may get clipped otherwise)
    00164                 if (null == categoryNode)
    00165                 {
    00166                     categoryNode = new TreeNode();
    00167                     categoryNode.Name = category;
    00168                     categoryNode.Text = category + "      ";
    00169                     categoryNode.Tag = null;
    00170                     categoryNode.ToolTipText = "InfoLink Category: " + category;
    00171                     categoryNode.NodeFont = new Font(this.treeViewInfoLinks.Font, FontStyle.Bold);
    00172                     this.treeViewInfoLinks.Nodes.Add(categoryNode);
    00173                 }
    00174
    00175                 // Get sub-category
    00176                 string subCategory = string.Empty;
    00177                 if (string.IsNullOrEmpty(infoLink.SubCategory))
    00178                 {
    00179                     subCategory = "Uncategorized";
    00180                 }
    00181                 else
    00182                 {
    00183                     subCategory = infoLink.SubCategory;
    00184                 }
    00185
    00186                 // Find sub-category node
    00187                 TreeNode subCategoryNode = null;
    00188                 foreach (TreeNode node in categoryNode.Nodes)
    00189                 {
    00190                     if ((null != node) &&
    00191                         (node.Name.Equals(subCategory)))
    00192                     {
    00193                         subCategoryNode = node;
    00194                         break;
    00195                     }
    00196                 }
    00197
    00198                 // Maybe create new sub-category node
    00199                 if (null == subCategoryNode)
    00200                 {
    00201                     subCategoryNode = new TreeNode();
    00202                     subCategoryNode.Name = subCategory;
    00203                     subCategoryNode.Text = subCategory;
    00204                     subCategoryNode.Tag = null;
    00205                     subCategoryNode.ToolTipText = "InfoLink Sub-Category: " + subCategory;
    00206                     subCategoryNode.NodeFont = new Font(this.treeViewInfoLinks.Font, FontStyle.Italic);
    00207                     categoryNode.Nodes.Add(subCategoryNode);
    00208                 }
    00209
    00210                 // Find info node
    00211                 TreeNode infoNode = null;
    00212                 foreach (TreeNode node in subCategoryNode.Nodes)
    00213                 {
    00214                     // Check if we have this infoLink already
    00215                     if (node.Name.Equals(infoLink.Title))
    00216                     {
    00217                         infoNode = node;
    00218                         break;
    00219                     }
    00220                 }
    00221
    00222                 // Maybe create new info node which stores tag
    00223                 if (null == infoNode)
    00224                 {
    00225                     infoNode = new TreeNode();
    00226                     infoNode.Name = infoLink.Title;
    00227                     infoNode.Text = infoLink.Title;
    00228                     infoNode.Tag = infoLink;
    00229                     infoNode.ToolTipText = "InfoLink to: " + infoLink.Uri.ToString();
    00230                     infoNode.NodeFont = new Font(this.treeViewInfoLinks.Font, FontStyle.Underline);
    00231                     infoNode.ForeColor = Color.Blue;
    00232                     subCategoryNode.Nodes.Add(infoNode);
    00233                     categoryNode.Expand();
    00234                     subCategoryNode.ExpandAll();
    00235                 }
    00236
    00237                 // Mark node as newer
    00238                 infoNode.BackColor = Color.LightGreen;
    00239             }
    00240         }
    00241
    00247         private void treeView1_NodeMouseClick(
    00248             object sender,
    00249             TreeNodeMouseClickEventArgs e)
    00250         {
    00251             TreeNode node = e.Node;
    00252
    00253             if (null != node.Tag)
    00254             {
    00255                 NavigateToUrl(((InfoLink)node.Tag).Uri);
    00256                 node.BackColor = Color.White;
    00257             }
    00258         }
    00259
    00265         private void toolStripButton1_Click(
    00266             object sender,
    00267             EventArgs e)
    00268         {
    00269             if (this.webBrowser1.CanGoBack)
    00270             {
    00271                 this.webBrowser1.GoBack();
    00272             }
    00273         }
    00274
    00280         private void toolStripButton2_Click(
    00281             object sender,
    00282             EventArgs e)
    00283         {
    00284             this.webBrowser1.Refresh();
    00285         }
    00286
    00292         private void toolStripButton3_Click(
    00293             object sender,
    00294             EventArgs e)
    00295         {
    00296             if (this.webBrowser1.CanGoForward)
    00297             {
    00298                 this.webBrowser1.GoForward();
    00299             }
    00300         }
    00301
    00307         private void toolStripButton4_Click(
    00308             object sender,
    00309             EventArgs e)
    00310         {
    00311             this.webBrowser1.Stop();
    00312         }
    00313
    00314         #region browser_event_handlers
    00315
    00321         private void webBrowser1_DocumentTitleChanged(
    00322             object sender,
    00323             EventArgs e)
    00324         {
    00325             this.Text = "InfoBrowser";
    00326             if (!string.IsNullOrEmpty(this.webBrowser1.DocumentTitle))
    00327             {
    00328                 this.Text += " - " + this.webBrowser1.DocumentTitle;
    00329             }
    00330         }
    00331
    00337         private void webBrowser1_CanGoBackChanged(
    00338             object sender,
    00339             EventArgs e)
    00340         {
    00341             this.toolStripButton1.Enabled = webBrowser1.CanGoBack;
    00342         }
    00343
    00349         private void webBrowser1_CanGoForwardChanged(
    00350             object sender,
    00351             EventArgs e)
    00352         {
    00353             this.toolStripButton3.Enabled = webBrowser1.CanGoForward;
    00354         }
    00355
    00361         private void webBrowser1_Navigated(
    00362             object sender,
    00363             WebBrowserNavigatedEventArgs e)
    00364         {
    00365             // Disable stop button
    00366             this.toolStripButton4.Enabled = false;
    00367
    00368             // Update url
    00369             this.textBox1.Text = this.webBrowser1.Url.ToString();
    00370
    00371             // Update reload status button
    00372             this.toolStripButton2.Enabled =
    00373                 (!string.IsNullOrEmpty(this.webBrowser1.Url.ToString()));
    00374         }
    00375
    00381         private void webBrowser1_ProgressChanged(
    00382             object sender,
    00383             WebBrowserProgressChangedEventArgs e)
    00384         {
    00385             // Update status message
    00386             if (e.CurrentProgress == e.MaximumProgress)
    00387             {
    00388                 // Disable stop button
    00389                 this.toolStripButton4.Enabled = false;
    00390
    00391                 // Set message
    00392                 this.toolStripStatusLabel1.Text = string.Empty;
    00393             }
    00394             else
    00395             {
    00396                 // Enable stop button
    00397                 this.toolStripButton4.Enabled = true;
    00398
    00399                 // Set message
    00400                 this.toolStripStatusLabel1.Text = "Loading ...";
    00401             }
    00402         }
    00403
    00409         private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    00410         {
    00411             // Nothing to do
    00412         }
    00413
    00414         #endregion
    00415
    00421         private void textBox1_KeyDown(object sender, KeyEventArgs e)
    00422         {
    00423             if (e.KeyCode == Keys.Enter)
    00424             {
    00425                 string url = this.textBox1.Text;
    00426
    00427                 if (string.IsNullOrEmpty(url))
    00428                 {
    00429                     return;
    00430                 }
    00431
    00432                 // Add http
    00433                 if (!url.Equals("about:blank") &&
    00434                     !url.StartsWith("http://") &&
    00435                     !url.StartsWith("https://"))
    00436                 {
    00437                     url = "http://" + url;
    00438                 }
    00439
    00440                 Uri uri;
    00441                 try
    00442                 {
    00443                     uri = new Uri(url);
    00444                 }
    00445                 catch (System.UriFormatException)
    00446                 {
    00447                     this.toolStripStatusLabel1.Text = "Error - invalid URL.";
    00448                     return;
    00449                 }
    00450
    00451                 NavigateToUrl(uri);
    00452             }
    00453         }
    00454
    00459         private void NavigateToUrl(Uri uri)
    00460         {
    00461             if (null == uri)
    00462             {
    00463                 return;
    00464             }
    00465
    00466             // Browse
    00467             this.webBrowser1.Navigate(uri);
    00468         }
    00469
    00470         private void InfoBrowserForm_FormClosing(object sender, FormClosingEventArgs e)
    00471         {
    00472             this.infoLinkReceiver.StopListener();
    00473             e.Cancel = false;
    00474         }
    00475
    00476         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    00477         {
    00478             this.Close();
    00479         }
    00480
    00481         private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    00482         {
    00483             MessageBox.Show(
    00484                 "InfoLink Receiver with Embedded Browser" + Environment.NewLine+
    00485                 "Receiving UDP packets on port: " + this.infoLinkReceiver.Port + Environment.NewLine +
    00486                 "(C) A. Schiffler, 2009" + Environment.NewLine,
    00487                 "InfoBrowser About");
    00488         }
    00489
    00490         private void saveInfoLinksAsHTMLToolStripMenuItem_Click(object sender, EventArgs e)
    00491         {
    00492             SaveFileDialog saveFileDialog = new SaveFileDialog();
    00493             saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    00494             saveFileDialog.FileName =
    00495                 "InfoLinks-" +
    00496                 DateTime.Now.ToString("yyyyMMMd") +
    00497                 ".htm";
    00498             saveFileDialog.Filter = "HTML File|*.htm";
    00499             saveFileDialog.Title = "Save InfoLinks as HTML";
    00500             saveFileDialog.AddExtension = true;
    00501             saveFileDialog.ShowDialog();
    00502
    00503             if (saveFileDialog.FileName != "")
    00504             {
    00505                 try
    00506                 {
    00507                     FileStream fs = (FileStream)saveFileDialog.OpenFile();
    00508                     SaveNodesAsHtmlPage(fs);
    00509                     fs.Close();
    00510                     this.toolStripStatusLabel1.Text = "Saved to file: " + saveFileDialog.FileName;
    00511                 }
    00512                 catch (Exception)
    00513                 {
    00514                     this.toolStripStatusLabel1.Text = "Error - save failed.";
    00515                 }
    00516             }
    00517         }
    00518
    00519         private void SaveNodesAsHtmlPage(FileStream fs)
    00520         {
    00521             StreamWriter writer = new StreamWriter(fs);
    00522             writer.WriteLine("<html>");
    00523             writer.WriteLine("<title>InfoLinks</title>");
    00524             writer.WriteLine("<h1>InfoLinks Menu</h1>");
    00525             foreach (TreeNode categoryNode in this.treeViewInfoLinks.Nodes)
    00526             {
    00527                 writer.WriteLine("<dt><h3>" + categoryNode.Text + "</h3>");
    00528                 writer.WriteLine("<dl><p>");
    00529                 foreach (TreeNode linkNode in categoryNode.Nodes)
    00530                 {
    00531                     writer.WriteLine(
    00532                         "<dt><a href=\"" +
    00533                         linkNode.Tag.ToString() +
    00534                         "\">" + linkNode.Text + "</a>");
    00535                 }
    00536                 writer.WriteLine("</dl><p>");
    00537             }
    00538             writer.WriteLine("</html>");
    00539             writer.Flush();
    00540         }
    00541     }
    00542 }