Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

Grandiose Projects 3. Compatibility with Simulink

Rate me:
Please Sign up or sign in to vote.
4.27/5 (11 votes)
8 Feb 2010CPOL23 min read 47.6K   5.9K   38  
Import of Simulink files
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;



namespace ToolBox
{
    /// <summary>
    /// Moved text box
    /// </summary>
    [Serializable()]
    public class MovedTextBox : TextBox, ISetBorders, ISerializable, ICloneable
    {
        #region Fields

        private ControlPanel[] panels;
        private bool moved;
        private int mouseX;
        private int mouseY;
        private PictureBox movePicture;
        private bool isActive;
        static private Image moveImage;

        #endregion

        #region Constructors & Destuctors

        /// <summary>
        /// Default constructor
        /// </summary>
        public MovedTextBox()
        {
            init();
        }


        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        protected MovedTextBox(SerializationInfo info, StreamingContext context)
        {
            Left = (int)info.GetValue("Left", typeof(int));
            Top = (int)info.GetValue("Top", typeof(int));
            Width = (int)info.GetValue("Width", typeof(int));
            Height = (int)info.GetValue("Height", typeof(int));
            Text = (string)info.GetValue("Text", typeof(string));
            string fontFamilyName = (string)info.GetValue("FontFamilyName", typeof(string));
            bool bold = (bool)info.GetValue("FontBold", typeof(bool));
            bool italic = (bool)info.GetValue("FontItalic", typeof(bool));
            bool underline = (bool)info.GetValue("FontUnderline", typeof(bool));
            bool strikeout = (bool)info.GetValue("Strikeout", typeof(bool));
            FontStyle fs = 0;
            if (bold)
            {
                fs |= FontStyle.Bold;

            }
            else
            {
                fs |= FontStyle.Regular;
            }
            if (italic)
            {
                fs |= FontStyle.Italic;
            }
            if (underline)
            {
                fs |= FontStyle.Underline;
            }
            if (strikeout)
            {
                fs |= FontStyle.Strikeout;
            }
            float size = (float)info.GetValue("FontSize", typeof(float));
            this.Font = new Font(fontFamilyName, size, fs, GraphicsUnit.Point);
            BorderStyle = BorderStyle.None;
            init();
        }

        /// <summary>
        /// Destructor
        /// </summary>
        ~MovedTextBox()
        {
            if (!IsDisposed)
            {
                try
                {
                    Dispose();
                }
                catch (Exception)
                {
                }
            }
        }




        #endregion


        /// <summary>
        /// ISerializable interface implementation
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Left", Left);
            info.AddValue("Top", Top);
            info.AddValue("Width", Width);
            info.AddValue("Height", Height);
            info.AddValue("Text", Text);
            Font font = this.Font;
            info.AddValue("FontFamilyName", font.Name);
            info.AddValue("FontSize", font.SizeInPoints);
            info.AddValue("FontBold", font.Bold);
            info.AddValue("FontItalic", font.Italic);
            info.AddValue("FontHeight", font.Height);
            info.AddValue("FontUnderline", font.Underline);
            info.AddValue("Strikeout", font.Strikeout);
        }

        /// <summary>
        /// Clones itself
        /// </summary>
        /// <returns>The clone</returns>
        public object Clone()
        {
            MovedTextBox clone = new MovedTextBox();
            clone.Left = Left;
            clone.Top = Top;
            clone.Width = Width;
            clone.Height = Height;
            string s = "";
            foreach (char c in Text)
            {
                s += c;
            }
            clone.Text = s;
            Font font = this.Font;
            FontStyle fs = 0;
            if (font.Bold)
            {
                fs |= FontStyle.Bold;

            }
            else
            {
                fs |= FontStyle.Regular;
            }
            if (font.Italic)
            {
                fs |= FontStyle.Italic;
            }
            if (font.Underline)
            {
                fs |= FontStyle.Underline;
            }
            if (font.Strikeout)
            {
                fs |= FontStyle.Strikeout;
            }
            clone.Font = new Font(font.Name, font.SizeInPoints, fs, GraphicsUnit.Point);
            clone.BorderStyle = BorderStyle.None;
            return clone;
        }


        /// <summary>
        /// Moved image
        /// </summary>
        static public Image MoveImage
        {
            set
            {
                moveImage = value;
            }
        }

        /// <summary>
        /// Gets corner control
        /// </summary>
        /// <param name="i">Control number</param>
        /// <returns>Corner control</returns>
        public ControlPanel this[int i]
        {
            get
            {
                return panels[i];
            }
        }

        /// <summary>
        /// Deactivates itself
        /// </summary>
        public void Deactivate()
        {
            if (!isActive)
            {
                return;
            }
            ControlPanel.RemovePanels(this);
            if (Text.Length > 0)
            {
                BorderStyle = BorderStyle.None;
            }
            if (Parent != null)
            {
                if (Parent.Contains(movePicture))
                {
                    Parent.Controls.Remove(movePicture);
                }
            }
            isActive = false;
            moved = false;
        }


        /// <summary>
        /// The "is active" sign
        /// </summary>
        public bool IsActive
        {
            get
            {
                return isActive;
            }
        }

        /// <summary>
        /// Moved pictute
        /// </summary>
        public Control MovePicture
        {
            get
            {
                return movePicture;
            }
        }

        /// <summary>
        /// The "is empty" sign
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                return Text.Length != 0;
            }
        }

        /// <summary>
        /// Shows font dialog
        /// </summary>
        /// <param name="control">Base control</param>
        static public void ShowFontDialog(Control control)
        {
            TextBox box = ControlPanel.GetActiveTextBox(control);
            if (box == null)
            {
                return;
            }
            FontDialog dlg = new FontDialog();
            Control c = control.Parent;
            while (true)
            {
                if (c.Parent == null)
                {
                    break;
                }
                c = c.Parent;
            }
            dlg.ShowDialog(c as Form);
            Font font = dlg.Font;
            box.Font = font;
        }

        /// <summary>
        /// Mouse down event
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event arguments</param>
        protected void onMouseDownEventHandler(object sender, MouseEventArgs e)
        {
            mouseX = e.X;
            mouseY = e.Y;
            moved = true;
        }

        /// <summary>
        /// Mouse up event
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event arguments</param>
        protected void onMouseUpEventHandler(object sender, MouseEventArgs e)
        {
            moved = false;
            Control p = Parent;
        }

        /// <summary>
        /// Mouse move event
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event arguments</param>
        protected void onMouseMoveEventHandler(object sender, MouseEventArgs e)
        {
            if (!moved)
            {
                return;
            }
            int dx = e.X - mouseX;
            int dy = e.Y - mouseY;
            ControlPanel.Move(this, dx, dy);
            movePicture.Left += dx;
            movePicture.Top += dy;
        }



        private void keyDown(object sender, KeyEventArgs e)
        {
            if (!e.Control)
            {
                return;
            }
            if (e.KeyCode == Keys.Delete)
            {
                try
                {
                    foreach (Panel p in panels)
                    {
                        Parent.Controls.Remove(p);
                    }
                    Parent.Controls.Remove(movePicture);
                    Parent.Controls.Remove(this);
                }
                catch (Exception)
                {
                   // ex = ex;
                }
            }
        }

        private void enter(object sender, EventArgs e)
        {
            string text = Text.Clone() as string;
            Control p = Parent;
            movePicture.Left = Left;
            movePicture.Top = Top - movePicture.Height;
            p.Controls.Add(movePicture);
            ControlPanel.SetPanels(this, panels);
            ControlPanel.DeactivateOther(this);
            isActive = true;
        }




        private void exit(object sender, EventArgs e)
        {
            Deactivate();
        }

        private void init()
        {
            Multiline = true;
            panels = new ControlPanel[]{new ControlPanel(this, 0), new ControlPanel(this, 1), 
										   new ControlPanel(this, 2), new ControlPanel(this, 3)};
            for (int i = 0; i < panels.Length; i++)
            {
                panels[i].Width = 4;
                panels[i].Height = 4;
                panels[i].BackColor = Color.Black;
            }
            if (moveImage == null)
            {
                moveImage = ResourceImage.Move;
            }
            movePicture = new PictureBox();
            movePicture.Width = moveImage.Width;
            movePicture.Height = moveImage.Height;
            movePicture.Image = moveImage;
            movePicture.Cursor = Cursors.SizeAll;
            Enter += new EventHandler(enter);
            Leave += new EventHandler(exit);
            movePicture.MouseDown += new MouseEventHandler(onMouseDownEventHandler);
            movePicture.MouseUp += new MouseEventHandler(onMouseUpEventHandler);
            movePicture.MouseMove += new MouseEventHandler(onMouseMoveEventHandler);
            KeyDown += new KeyEventHandler(keyDown);
            MenuItem itFont = new MenuItem("Font");
            //	itFont.Text = "Font";
            itFont.Click += new EventHandler(selectFont);
            try
            {
                //ContextMenu..MenuItems..IsReadOnly = false;
                //ContextMenu = new ContextMenu(new MenuItem[]{itFont});
            }
            catch (Exception)
            {
                //ex = ex;
            }
        }




        private void selectFont(object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog();
            dlg.ShowDialog(Parent);
            Font = dlg.Font;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions