2009-09-03 21:01:11 +02:00
|
|
|
#region License
|
|
|
|
//
|
|
|
|
// The Open Toolkit Library License
|
|
|
|
//
|
|
|
|
// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do
|
|
|
|
// so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
|
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
// OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.Drawing.Text;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using OpenTK.Examples.Properties;
|
2009-11-16 11:56:07 +01:00
|
|
|
using System.Threading;
|
2010-10-02 20:52:34 +02:00
|
|
|
using System.IO;
|
2009-09-03 21:01:11 +02:00
|
|
|
|
|
|
|
namespace Examples
|
|
|
|
{
|
|
|
|
public partial class ExampleBrowser : Form
|
|
|
|
{
|
|
|
|
#region Fields
|
|
|
|
|
|
|
|
//PrivateFontCollection font_collection = new PrivateFontCollection();
|
|
|
|
bool show_warning = true;
|
2010-11-24 19:08:20 +01:00
|
|
|
readonly string SourcePath;
|
2010-10-02 20:52:34 +02:00
|
|
|
|
2009-09-03 21:01:11 +02:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
public ExampleBrowser()
|
|
|
|
{
|
2010-11-24 19:08:20 +01:00
|
|
|
SourcePath =
|
|
|
|
FindSourcePath(Directory.GetCurrentDirectory()) ??
|
|
|
|
FindSourcePath(Assembly.GetExecutingAssembly().Location);
|
|
|
|
|
2009-09-03 21:01:11 +02:00
|
|
|
Font = SystemFonts.DialogFont;
|
|
|
|
|
|
|
|
InitializeComponent();
|
2009-11-04 02:19:35 +01:00
|
|
|
Icon = Resources.App;
|
2009-09-03 21:01:11 +02:00
|
|
|
|
|
|
|
// Windows 6 (Vista) and higher come with Consolas, a high-quality monospace font. Use that or fallback to
|
|
|
|
// the generic monospace font on other systems.
|
|
|
|
if (System.Environment.OSVersion.Platform == PlatformID.Win32NT &&
|
|
|
|
System.Environment.OSVersion.Version.Major >= 6)
|
|
|
|
{
|
|
|
|
textBoxOutput.Font = richTextBoxSource.Font = new Font("Consolas", 10.0f, FontStyle.Regular);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textBoxOutput.Font = richTextBoxSource.Font =
|
|
|
|
new Font(FontFamily.GenericMonospace, 10.0f, FontStyle.Regular);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Protected Members
|
|
|
|
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
|
|
{
|
|
|
|
base.OnLoad(e);
|
|
|
|
|
2010-10-05 19:43:33 +02:00
|
|
|
// Add those by hand, because using the designer results in an empty
|
|
|
|
// image list when cross-compiling on Mono.
|
|
|
|
imageListSampleCategories.Images.Add("OpenAL", Resources.OpenAL);
|
|
|
|
imageListSampleCategories.Images.Add("OpenGL", Resources.OpenGL);
|
|
|
|
imageListSampleCategories.Images.Add("OpenGLES", Resources.OpenGLES);
|
|
|
|
imageListSampleCategories.Images.Add("OpenCL", Resources.OpenCL);
|
|
|
|
imageListSampleCategories.Images.Add("OpenTK", Resources.OpenTK);
|
|
|
|
imageListSampleCategories.Images.Add("1.x", Resources.v1x);
|
|
|
|
imageListSampleCategories.Images.Add("2.x", Resources.v2x);
|
|
|
|
imageListSampleCategories.Images.Add("3.x", Resources.v3x);
|
|
|
|
imageListSampleCategories.Images.Add("4.x", Resources.v4x);
|
|
|
|
|
2009-09-03 21:01:11 +02:00
|
|
|
treeViewSamples.TreeViewNodeSorter = new SamplesTreeViewSorter();
|
|
|
|
|
|
|
|
LoadSamplesFromAssembly(Assembly.GetExecutingAssembly());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnShown(EventArgs e)
|
|
|
|
{
|
|
|
|
if (show_warning)
|
|
|
|
{
|
|
|
|
//MessageBox.Show("The new Sample Browser is not complete. Please report any issues at http://www.opentk.com/project/issues.",
|
|
|
|
// "Work in Progress", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
|
show_warning = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Private Members
|
|
|
|
|
|
|
|
#region Events
|
|
|
|
|
|
|
|
#region TreeView
|
|
|
|
|
|
|
|
private void treeViewSamples_AfterSelect(object sender, TreeViewEventArgs e)
|
|
|
|
{
|
|
|
|
const string no_docs = "Documentation has not been entered.";
|
|
|
|
const string no_source = "Source code has not been entered.";
|
|
|
|
|
2010-11-24 19:08:20 +01:00
|
|
|
if (SourcePath != null && e.Node.Tag != null &&
|
|
|
|
!String.IsNullOrEmpty(((ExampleInfo)e.Node.Tag).Attribute.Documentation))
|
2009-09-03 21:01:11 +02:00
|
|
|
{
|
2010-10-02 20:52:34 +02:00
|
|
|
string docs = null;
|
|
|
|
string source = null;
|
|
|
|
|
|
|
|
ExampleInfo einfo = (ExampleInfo)e.Node.Tag;
|
|
|
|
string sample = einfo.Attribute.Documentation;
|
|
|
|
string category = einfo.Attribute.Category.ToString();
|
|
|
|
string subcategory = einfo.Attribute.Subcategory;
|
|
|
|
|
|
|
|
string path = Path.Combine(Path.Combine(Path.Combine(SourcePath, category), subcategory), sample);
|
|
|
|
string sample_rtf = Path.ChangeExtension(path, "rtf");
|
|
|
|
string sample_cs = Path.ChangeExtension(path, "cs");
|
|
|
|
|
|
|
|
if (File.Exists(sample_rtf))
|
|
|
|
{
|
|
|
|
docs = File.ReadAllText(sample_rtf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (File.Exists(sample_cs))
|
|
|
|
{
|
|
|
|
source = File.ReadAllText(sample_cs);
|
|
|
|
}
|
2009-09-03 21:01:11 +02:00
|
|
|
|
|
|
|
if (String.IsNullOrEmpty(docs))
|
2010-10-02 20:52:34 +02:00
|
|
|
richTextBoxDescription.Text = String.Format("File {0} not found.", sample_rtf);
|
2009-09-03 21:01:11 +02:00
|
|
|
else
|
|
|
|
richTextBoxDescription.Rtf = docs;
|
2010-10-02 20:52:34 +02:00
|
|
|
|
2009-09-03 21:01:11 +02:00
|
|
|
if (String.IsNullOrEmpty(source))
|
2010-10-02 20:52:34 +02:00
|
|
|
richTextBoxSource.Text = String.Format("File {0} not found.", sample_cs);
|
2009-09-03 21:01:11 +02:00
|
|
|
else
|
|
|
|
richTextBoxSource.Text = source;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
richTextBoxDescription.Text = no_docs;
|
|
|
|
richTextBoxSource.Text = no_source;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void treeViewSamples_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Node.Tag != null)
|
|
|
|
{
|
|
|
|
ActivateNode(e.Node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void treeViewSamples_KeyDown(object sender, KeyEventArgs e)
|
|
|
|
{
|
|
|
|
// The enter key activates a node (either expands/collapses or executes its sample).
|
|
|
|
switch (e.KeyCode)
|
|
|
|
{
|
|
|
|
case Keys.Enter:
|
|
|
|
ActivateNode(treeViewSamples.SelectedNode);
|
|
|
|
e.Handled = true;
|
|
|
|
e.SuppressKeyPress = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void treeViewSamples_MouseDown(object sender, MouseEventArgs e)
|
|
|
|
{
|
|
|
|
// Make sure that right-clicking a new node will select that node before displaying
|
|
|
|
// the context menu. Without this, right-clicking a node does not select it, which
|
|
|
|
// is completely disorienting.
|
|
|
|
// As a bonus, make any mouse button select the underlying node,
|
|
|
|
TreeNode node = treeViewSamples.HitTest(e.Location).Node;
|
|
|
|
if (node != null)
|
|
|
|
treeViewSamples.SelectedNode = node;
|
|
|
|
|
|
|
|
// Middle click selects and activates a node (either expands/collapses or executes its sample).
|
|
|
|
// Right button displays the context menu.
|
|
|
|
// All other mouse buttons simply select the underlying node.
|
|
|
|
switch (e.Button)
|
|
|
|
{
|
|
|
|
case MouseButtons.Middle:
|
|
|
|
ActivateNode(node);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MouseButtons.Right:
|
|
|
|
treeViewSamples.ContextMenuStrip.Show(sender as Control, e.Location);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void treeViewSamples_AfterExpand(object sender, TreeViewEventArgs e)
|
|
|
|
{
|
|
|
|
foreach (TreeNode child in e.Node.Nodes)
|
|
|
|
child.EnsureVisible();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void contextMenuStripSamples_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
|
|
{
|
|
|
|
switch (e.ClickedItem.Text)
|
|
|
|
{
|
|
|
|
case "&Run Sample": RunSample(this, (ExampleInfo)treeViewSamples.SelectedNode.Tag); break;
|
|
|
|
case "View Description": tabControlSample.SelectedTab = tabDescription; break;
|
|
|
|
case "View Source Code": tabControlSample.SelectedTab = tabSource; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Description
|
|
|
|
|
|
|
|
private void richTextBoxDescription_MouseDown(object sender, MouseEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Button == MouseButtons.Right)
|
|
|
|
{
|
|
|
|
richTextBoxDescription.ContextMenuStrip.Show(sender as Control, e.X, e.Y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void contextMenuStripDescription_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.ClickedItem.Text == "&Copy")
|
|
|
|
{
|
|
|
|
Clipboard.SetText(richTextBoxDescription.SelectedRtf, TextDataFormat.Rtf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Source Code
|
|
|
|
|
|
|
|
private void richTextBoxSource_MouseDown(object sender, MouseEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.Button == MouseButtons.Right)
|
|
|
|
{
|
|
|
|
richTextBoxSource.ContextMenuStrip.Show(sender as Control, e.X, e.Y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void contextMenuStripSource_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
|
|
|
|
{
|
|
|
|
if (e.ClickedItem.Text == "&Copy")
|
|
|
|
{
|
|
|
|
Clipboard.SetText(richTextBoxSource.SelectedText, TextDataFormat.Text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Actions
|
|
|
|
|
|
|
|
void LoadSamplesFromAssembly(Assembly assembly)
|
|
|
|
{
|
|
|
|
if (assembly == null)
|
|
|
|
throw new ArgumentNullException("assembly");
|
|
|
|
|
|
|
|
Type[] types = assembly.GetTypes();
|
|
|
|
foreach (Type type in types)
|
|
|
|
{
|
|
|
|
object[] attributes = type.GetCustomAttributes(false);
|
|
|
|
ExampleAttribute example = null;
|
|
|
|
foreach (object attr in attributes)
|
|
|
|
{
|
|
|
|
if (attr is ExampleAttribute)
|
|
|
|
{
|
|
|
|
example = (ExampleAttribute)attr;
|
|
|
|
|
|
|
|
if (example.Visible)
|
|
|
|
{
|
|
|
|
// Add this example to the sample TreeView.
|
|
|
|
// First check whether the ExampleCategory exists in the tree (and add it if it doesn't).
|
|
|
|
// Then add the example as a child node on this category.
|
|
|
|
|
|
|
|
if (!treeViewSamples.Nodes.ContainsKey(example.Category.ToString()))
|
|
|
|
{
|
|
|
|
int category_index = GetImageIndexForSample(imageListSampleCategories, example.Category.ToString(), String.Empty);
|
|
|
|
treeViewSamples.Nodes.Add(example.Category.ToString(), String.Format("{0} samples", example.Category),
|
|
|
|
category_index, category_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
int image_index = GetImageIndexForSample(imageListSampleCategories, example.Category.ToString(), example.Subcategory);
|
|
|
|
TreeNode node = new TreeNode(example.Title, image_index, image_index);
|
|
|
|
node.Name = example.Title;
|
|
|
|
node.Tag = new ExampleInfo(type, example);
|
|
|
|
treeViewSamples.Nodes[example.Category.ToString()].Nodes.Add(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
treeViewSamples.Sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ActivateNode(TreeNode node)
|
|
|
|
{
|
|
|
|
if (node == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (node.Tag == null)
|
|
|
|
{
|
|
|
|
if (node.IsExpanded)
|
|
|
|
node.Collapse();
|
|
|
|
else
|
|
|
|
node.Expand();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tabControlSample.SelectedTab = tabPageOutput;
|
|
|
|
textBoxOutput.Clear();
|
|
|
|
RunSample(node.TreeView.TopLevelControl, (ExampleInfo)node.Tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetImageIndexForSample(ImageList list, string category, string subcategory)
|
|
|
|
{
|
|
|
|
if (list == null)
|
|
|
|
throw new ArgumentNullException("list");
|
|
|
|
|
2010-10-05 19:43:33 +02:00
|
|
|
foreach (string extension in new string[] { "", ".png", ".jpg" })
|
|
|
|
{
|
|
|
|
string name = subcategory.ToString() + extension;
|
|
|
|
if (list.Images.ContainsKey(name))
|
|
|
|
return list.Images.IndexOfKey(name);
|
2009-09-03 21:01:11 +02:00
|
|
|
|
2010-10-05 19:43:33 +02:00
|
|
|
name = category.ToString() + extension;
|
|
|
|
if (list.Images.ContainsKey(name))
|
|
|
|
return list.Images.IndexOfKey(name);
|
|
|
|
}
|
|
|
|
|
2010-10-02 20:52:34 +02:00
|
|
|
return -1;
|
2009-09-03 21:01:11 +02:00
|
|
|
}
|
|
|
|
|
2010-12-08 08:51:41 +01:00
|
|
|
void RunSample(Control parent, ExampleInfo e)
|
2009-09-03 21:01:11 +02:00
|
|
|
{
|
|
|
|
if (e == null)
|
|
|
|
return;
|
|
|
|
|
2013-10-02 01:10:03 +02:00
|
|
|
try
|
2009-09-03 21:01:11 +02:00
|
|
|
{
|
2013-10-02 01:10:03 +02:00
|
|
|
if (parent != null)
|
2009-09-03 21:01:11 +02:00
|
|
|
{
|
2013-10-02 01:10:03 +02:00
|
|
|
parent.Visible = false;
|
|
|
|
Application.DoEvents();
|
2009-09-03 21:01:11 +02:00
|
|
|
}
|
2013-10-02 01:10:03 +02:00
|
|
|
Trace.WriteLine(String.Format("Launching sample: \"{0}\"", e.Attribute.Title));
|
|
|
|
Trace.WriteLine(String.Empty);
|
2010-11-24 19:08:20 +01:00
|
|
|
|
2013-10-02 01:10:03 +02:00
|
|
|
var info = new ProcessStartInfo
|
|
|
|
{
|
|
|
|
FileName = Application.ExecutablePath,//.Replace("vshost.exe", String.Empty),
|
|
|
|
Arguments = e.Example.ToString()
|
|
|
|
};
|
|
|
|
var process = Process.Start(info);
|
|
|
|
process.WaitForExit();
|
2010-11-24 19:08:20 +01:00
|
|
|
}
|
2013-10-02 01:10:03 +02:00
|
|
|
finally
|
2010-11-24 19:08:20 +01:00
|
|
|
{
|
2013-10-02 01:10:03 +02:00
|
|
|
if (parent != null)
|
2010-11-24 19:08:20 +01:00
|
|
|
{
|
2013-10-02 01:10:03 +02:00
|
|
|
try
|
2010-12-08 08:43:43 +01:00
|
|
|
{
|
2013-10-02 01:10:03 +02:00
|
|
|
textBoxOutput.Text = File.ReadAllText("debug.log");
|
2010-12-08 08:43:43 +01:00
|
|
|
}
|
2013-10-02 01:10:03 +02:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
Debug.Print(ex.ToString());
|
|
|
|
}
|
|
|
|
parent.Visible = true;
|
|
|
|
parent.Focus();
|
|
|
|
Application.DoEvents();
|
2010-11-24 19:08:20 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-02 01:10:03 +02:00
|
|
|
|
2010-11-24 19:08:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tries to detect the path that contains the source for the examples.
|
|
|
|
static string FindSourcePath(string guess)
|
|
|
|
{
|
2010-11-24 19:11:01 +01:00
|
|
|
guess = Path.GetDirectoryName(guess);
|
|
|
|
|
2010-10-02 20:52:34 +02:00
|
|
|
// Typically, our working directory is either "[opentk]/Binaries/OpenTK/[config]" or "[opentk]".
|
|
|
|
// The desired source path is "[opentk]/Source/Examples/[ExampleCategory]"
|
|
|
|
if (CheckPath(ref guess))
|
|
|
|
return guess; // We were in [opentk] after all
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
DirectoryInfo dir = Directory.GetParent(guess);
|
2010-11-24 19:08:20 +01:00
|
|
|
if (dir == null || !dir.Exists)
|
2010-10-02 20:52:34 +02:00
|
|
|
break;
|
|
|
|
guess = dir.FullName;
|
|
|
|
}
|
2010-11-24 19:08:20 +01:00
|
|
|
|
2010-10-02 20:52:34 +02:00
|
|
|
if (CheckPath(ref guess))
|
|
|
|
return guess; // We were in [opentk]/Binaries/OpenTK/[config] after all
|
|
|
|
|
2010-11-24 19:08:20 +01:00
|
|
|
return null;
|
2010-10-02 20:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool CheckPath(ref string path)
|
|
|
|
{
|
|
|
|
string guess = path;
|
|
|
|
if (Directory.Exists(guess))
|
|
|
|
{
|
|
|
|
guess = Path.Combine(guess, "Source");
|
|
|
|
if (Directory.Exists(guess))
|
|
|
|
{
|
|
|
|
guess = Path.Combine(guess, "Examples");
|
|
|
|
if (Directory.Exists(guess))
|
|
|
|
{
|
|
|
|
// We are have found [opentk]/Source/Examples
|
|
|
|
path = guess;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2009-09-03 21:01:11 +02:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|