/*============================================================================= Project: SharpImage Module: siFormFlowLayout.cs Language: C# Author: Dan Mueller Date: $Date$ Revision: $Revision$ Copyright (c) Queensland University of Technology (QUT) 2008. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using SharpImage.Rendering; using IronPython.Runtime.Calls; namespace SharpImage.Forms { public partial class siFormFlowLayout : siFormTool { #region Construction and Disposal //===================================================================== /// /// Default constructor /// public siFormFlowLayout() { // Initialise InitializeComponent(); } //===================================================================== #endregion #region Properties //===================================================================== private Dictionary m_HandlersCallTarget1 = new Dictionary(); private Dictionary m_HandlersCallTarget2 = new Dictionary(); //===================================================================== #endregion #region Public Methods //===================================================================== /// /// Adds a panel underneath the current panels with the given control. /// To add multiple controls, places them in a panel, and then add the panel. /// If control is null, a blank panel (separator) is added. /// /// /// public void AddCustomControl(int height, Control control) { Panel panel = new Panel(); panel.Size = new Size(400, height); panel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; if (control != null) panel.Controls.Add(control); panel.Dock = DockStyle.Top; this.Controls.Add(panel); this.Controls.SetChildIndex(panel, 0); } /// /// Adds a panel underneath the current panels with the given control. /// To add multiple controls, places them in a panel, and then add the panel. /// If control is null, a blank panel (separator) is added. /// The default height of 48 pixels is used. /// /// public void AddCustomControl(Control control) { this.AddCustomControl(48, control); } /// /// Adds a panel with a button underneath the current panels. /// The handler must be compatiable with the EventHandler delegate /// (void (object, EventArgs) handler). /// The handler function is called when the button is clicked. /// /// /// public void AddButton(String name, CallTarget2 handler) { Button button = new Button(); button.Name = name.Replace(" ", "").Replace("(","").Replace(")",""); button.Text = name; button.Location = new Point(6, 6); button.FlatStyle = FlatStyle.Flat; button.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; button.Click += new EventHandler(OnButtonClicked); Guid key = Guid.NewGuid(); button.Tag = key; this.m_HandlersCallTarget2.Add(key, handler); this.AddCustomControl(30, button); button.Size = new Size(this.ClientSize.Width-12, 24); } /// /// Adds a panel with a textbox and button with an ellipse which /// launches an open file dialog box. Once a valid (existing) file /// has been selected, the handler function (which must be of the form /// void (String) handler) is called with the full path of the selected /// file as the first argument. /// A label describing the textbox is not shown by default. /// /// public void AddOpenFile(CallTarget1 handler) { this.AddOpenFile(null, handler); } /// /// Adds a panel with a textbox and button with an ellipse which /// launches an open file dialog box. Once a valid (existing) file /// has been selected, the handler function (which must be of the form /// void (String) handler) is called with the full path of the selected /// file as the first argument. /// /// /// public void AddOpenFile(String strlabel, CallTarget1 handler) { // Create label Label label = new Label(); bool showlabel = (strlabel != null && strlabel.Length > 0); if (showlabel) label.Text = strlabel.Replace(":", "") + ":"; label.Location = new Point(6, 10); label.Anchor = AnchorStyles.Top | AnchorStyles.Left; // Create textbox TextBox txtpath = new TextBox(); if (showlabel) txtpath.Location = new Point(40, 6); else txtpath.Location = new Point(6, 6); txtpath.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; txtpath.BorderStyle = BorderStyle.FixedSingle; // Create button Button cmdopen = new Button(); cmdopen.Text = "..."; cmdopen.Size = new Size(30, 21); cmdopen.Anchor = AnchorStyles.Top | AnchorStyles.Right; cmdopen.FlatStyle = FlatStyle.Flat; cmdopen.Click += new EventHandler(OnFileOpenClick); Guid key = Guid.NewGuid(); cmdopen.Tag = key; this.m_HandlersCallTarget1.Add(key, handler); // Creating container Panel panel = new Panel(); panel.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; panel.Controls.Add(cmdopen); panel.Controls.Add(txtpath); if (showlabel) panel.Controls.Add(label); this.AddCustomControl(30, panel); // Set sizes panel.Size = new Size(this.ClientSize.Width - 12, 30); if (showlabel) txtpath.Size = new Size(this.ClientSize.Width - 90, 22); else txtpath.Size = new Size(this.ClientSize.Width - 50, 22); cmdopen.Location = new Point(this.ClientSize.Width - 42, 6); } //===================================================================== #endregion #region Private Methods //===================================================================== void OnFileOpenClick(object sender, EventArgs e) { // Lose focus this.Focus(); // Test sender is valid if (!(sender is Control)) return; Control control = sender as Control; // Test tag is valid Guid if (control.Tag == null) return; if (!(control.Tag is Guid)) return; Guid key = (Guid)control.Tag; // Show the open file dialog OpenFileDialog dialog = new OpenFileDialog(); dialog.CheckFileExists = true; dialog.CheckPathExists = true; dialog.Multiselect = false; dialog.RestoreDirectory = true; dialog.Title = "Please browse for file"; DialogResult result = dialog.ShowDialog(); if (result != DialogResult.OK) return; // Invoke appropriate handler if (this.m_HandlersCallTarget1.ContainsKey(key)) { control.Parent.Controls[1].Text = dialog.FileName; this.m_HandlersCallTarget1[key].Invoke(dialog.FileName); } } private void OnButtonClicked(object sender, EventArgs args) { // Lose focus this.Focus(); // Test sender is valid if (!(sender is Control)) return; Control control = sender as Control; // Test tag is valid Guid if (control.Tag == null) return; if (!(control.Tag is Guid)) return; Guid key = (Guid)control.Tag; // Invoke appropriate handler if (this.m_HandlersCallTarget2.ContainsKey(key)) this.m_HandlersCallTarget2[key].Invoke(sender, args); } //===================================================================== #endregion } }