Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / C#

User-driven applications

Rate me:
Please Sign up or sign in to vote.
4.88/5 (24 votes)
10 Apr 2010CPOL136 min read 33K   5   78  
User-driven applications are the programs in which full control is given to the users. Designers of such programs are responsible only for developing an instrument for solving some task, but they do not enforce users to work with this instrument according with a predefined scenario.
using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace UserDrivenApplications
{
    public partial class Form_FuncYx : Form
    {
        int version = 605;
        const string strAddRegKey = "Form_FuncYx";
        FunctionDesigned funcSrc;
        Mover mover;
        Point ptMouse_Down, ptMouse_Up;
        ElasticGroup groupLanguage, groupFunction, groupPressed;
        MSPlot msplot;
        Scale scalePressed;
        List<Elem> Polish_Yx = new List<Elem> ();
        bool bFuncCorrectAndReady = false;
        string strName, strText;
        bool bRestore;
        bool bClearRegistry = false;

        bool bAddResult = true;

        Point ptMouseInit;

        // -------------------------------------------------
        public Form_FuncYx (FunctionDesigned func, Point ptMouseScreen)
        {
            InitializeComponent ();
            funcSrc = func;
            ptMouseInit = ptMouseScreen;
            mover = new Mover (this);
            mover .Clipping = Clipping .Safe;
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            if (ptMouseInit != Point .Empty)
            {
                Location = ptMouseInit;
            }
            RestoreFromRegistry ();
            if (!bRestore)
            {
                Rectangle rc = new Rectangle (new Point (40, 20), new Size (ClientRectangle .Width - 60, textFunction .Top - 60));
                msplot = new MSPlot (this, rc);

                groupFunction = new ElasticGroup (this,
                        new ElasticGroupElement [] {new ElasticGroupElement (textFunction), 
                                                    new ElasticGroupElement (labelErrorYX), 
                                                    new ElasticGroupElement (btnShowFunc), 
                                                    new ElasticGroupElement (btnAddFunc), 
                                                    new ElasticGroupElement (btnChangeFunc), 
                                                    new ElasticGroupElement (new CommentedControl (this, textName, Resizing .WE, Side .W, "Name"))},
                                                  "Function");

                CommentedControl ccOper = new CommentedControl (this, labelOperations, Side .W, "Operations");
                CommentedControl ccFunc = new CommentedControl (this, labelFunctions, Side .W, "Functions");
                int cxL = Math .Min (ccOper .Comment .RectAround .Left, ccFunc .Comment .RectAround .Left);
                ccOper .Comment .Location = new Point (cxL + ccOper .Comment .Width / 2, ccOper .Comment .Location .Y);
                ccFunc .Comment .Location = new Point (cxL + ccFunc .Comment .Width / 2, ccFunc .Comment .Location .Y);
                groupLanguage = new ElasticGroup (this, new CommentedControl [] { ccOper, ccFunc }, "Language");
            }
            if (funcSrc == null)
            {
                btnChangeFunc .Enabled = false;
            }
            else
            {
                textName .Text = funcSrc .Name;
                textFunction .Text = funcSrc .YText;
                msplot .CopyView (funcSrc .MSPlot);
                CheckFunctionYX ();
            }
            RenewMover ();
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            groupLanguage .IntoMover (mover, 0);
            groupFunction .IntoMover (mover, 0);
            msplot .IntoMover (mover, mover .Count);
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            msplot .Draw (grfx);
            if (Polish_Yx .Count > 0)
            {                            
                msplot .DrawYofX (grfx, new MSPlotAuxi (0, SegmentLocation .Partly_Inside), Polish_Yx);
            }
            groupLanguage .Draw (grfx);
            groupFunction .Draw (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            mover .Catch (e .Location, e .Button);
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            double nDist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);
            if (e .Button == MouseButtons .Left)
            {
                mover .Release ();
            }
            else if (e .Button == MouseButtons .Right)
            {
                if (mover .Release ())
                {
                    if (nDist <= 3) 
                    {
                        MenuSelection (mover .WasCaughtObject);
                    }
                }
                else
                {
                    if (nDist <= 3)
                    {
                        ContextMenuStrip = menuOnEmpty;
                    }
                }
            }
        }
        // -------------------------------------------------        MenuSelection
        private void MenuSelection (int iInMover)
        {
            GraphicalObject grobj = mover [iInMover] .Source;
            if (grobj is MSPlot)
            {
                ContextMenuStrip = menuOnPlot;
            }
            else if (grobj is Scale)
            {
                scalePressed = grobj as Scale;
                ContextMenuStrip = menuOnScale;
            }
            else if (grobj is ElasticGroup)
            {
                groupPressed = grobj as ElasticGroup;
                ContextMenuStrip = menuOnGroup;
            }
        }
        // -------------------------------------------------        OnMouseMove
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            if (mover .Move (e .Location))
            {
                if (mover .CaughtSource is ElasticGroup || mover .CaughtSource is FramedControl || mover .CaughtSource is CommentedControl)
                {
                    Update ();
                }
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        OnMouseDoubleClick
        private void OnMouseDoubleClick (object sender, MouseEventArgs e)
        {
            if (mover .Catch (e .Location, MouseButtons .Left))
            {
                if (mover .CaughtSource is Scale)
                {
                    (mover .CaughtSource as Scale) .ParametersDialog (this, RenewMover, ParametersChanged); 
                }
            }
        }
        // -------------------------------------------------		ParametersChanged
        private void ParametersChanged (object sender, EventArgs ea)
        {
            Invalidate ();
        }

        // *****   menuOnGroup   *****
        // -------------------------------------------------        Opening_menuOnGroup
        private void Opening_menuOnGroup (object sender, CancelEventArgs e)
        {
            menuOnGroup .Items ["miFixUnfixElements"] .Text = groupPressed .ElementsMovable ? "Fix group's elements" : "Unfix group's elements";
        }
        // -------------------------------------------------        Click_miModifyGroup
        private void Click_miModifyGroup (object sender, EventArgs e)
        {
            groupPressed .ParametersDialog (this, PointToScreen (ptMouse_Up), GroupParametersChanged);
        }
        // -------------------------------------------------		GroupParametersChanged
        private void GroupParametersChanged (object sender, EventArgs ea)
        {
            groupPressed .Update ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miFixUnfixElements
        private void Click_miFixUnfixElements (object sender, EventArgs e)
        {
            groupPressed .ElementsMovable = !groupPressed .ElementsMovable;
            RenewMover ();
        }
        // -------------------------------------------------        Click_miGroupDefaultView
        private void Click_miGroupDefaultView (object sender, EventArgs e)
        {
            groupPressed .DefaultView ();
            groupPressed .Update ();
            RenewMover ();
            Invalidate ();
        }

        // *****   menuOnPlot   *****
        // -------------------------------------------------        Click_miModifyHorScale
        private void Click_miModifyHorScale (object sender, EventArgs e)
        {
            msplot .HorScales [0] .ParametersDialog (this, RenewMover, ParametersChanged);
        }
        // -------------------------------------------------        Click_miModifyVerScale
        private void Click_miModifyVerScale (object sender, EventArgs e)
        {
            msplot .VerScales [0] .ParametersDialog (this, RenewMover, ParametersChanged);
        }

        // *****   menuOnScale   *****
        // -------------------------------------------------        Click_miModifyScale
        private void Click_miModifyScale (object sender, EventArgs e)
        {
            scalePressed .ParametersDialog (this, RenewMover, ParametersChanged);
        }
        // -------------------------------------------------        Click_miFlipScale
        private void Click_miFlipScale (object sender, EventArgs e)
        {
            scalePressed .Flip ();
            Invalidate ();
        }

        // *****   menuOnEmpty   *****
        // -------------------------------------------------		Click_miDefaultView
        private void Click_miDefaultView (object sender, EventArgs e)
        {
            bClearRegistry = true;
            DialogResult = DialogResult .Retry;
            Close ();
        }

        // -------------------------------------------------        Click_btnShowFunc
        private void Click_btnShowFunc (object sender, EventArgs e)
        {
            if (!CheckFunctionYX ()) return;
            Invalidate ();
        }
        // -------------------------------------------------        CheckFunctionYX
        private bool CheckFunctionYX ()
        {
            labelErrorYX .Text = "";
            Polish_Yx = new List<Elem> ();
            string strInput = textFunction .Text;
            if (strInput .Length <= 0)
            {
                labelErrorYX .Text = "Function text is not prepared";
                return (false);
            }
            int iErr, iErrPlace;
            bool bCorrectFunc = FunctionInterpreter .Analyse (strInput, ref Polish_Yx, out iErr, out iErrPlace);
            if (!bCorrectFunc)
            {
                labelErrorYX .Text = FunctionInterpreter .GetErrorMessage (iErr);
                if (iErrPlace >= 0)
                {
                    textFunction .Select (iErrPlace, 1);
                }
                return (false);
            }
            return (true);
        }
        // -------------------------------------------------		Click_btnAdd
        private void Click_btnAdd (object sender, EventArgs e)
        {
            if (false == CheckResult ())
            {
                return;
            }
            bAddResult = true;
            Close ();
        }
        // -------------------------------------------------		Click_btnChange
        private void Click_btnChange (object sender, EventArgs e)
        {
            if (false == CheckResult ())
            {
                return;
            }
            bAddResult = false;
            Close ();
        }
        // -------------------------------------------------		CheckResult
        private bool CheckResult ()
        {
            if (!CheckFunctionYX ())
            {
                return (false);
            }
            strName = textName .Text;
            strName = strName .Trim ();
            if (string .IsNullOrEmpty (strName))
            {
                labelErrorYX .Text = "Function name is not defined";
                return (false);
            }
            strText = textFunction .Text;
            bFuncCorrectAndReady = true;
            return (true);
        }
        // -------------------------------------------------		NewFunctionReady
        public bool NewFunctionReady
        {
            get { return (bFuncCorrectAndReady); }
        }
        // -------------------------------------------------		NewFunctionName
        public string NewFunctionName
        {
            get { return (strName); }
        }
        // -------------------------------------------------		NewFunctionText
        public string NewFunctionText
        {
            get { return (strText); }
        }
        // -------------------------------------------------		NewYPolish
        public List<Elem> NewYPolish
        {
            get { return (Polish_Yx); }
        }
        // -------------------------------------------------		MSPlot
        public MSPlot MSPlot
        {
            get { return (msplot); }
        }
        // -------------------------------------------------		AddResult
        public bool AddResult
        {
            get { return (bAddResult); }
        }

        const string nameSize = "Size";
        // -------------------------------------------------        SaveInfoToRegistry
        private void SaveInfoToRegistry ()
        {
            string strRegKey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                if (bClearRegistry)
                {
                    Registry .CurrentUser .DeleteSubKey (strRegKey, false);
                }
                else
                {
                    regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                    if (regkey != null)
                    {
                        regkey .SetValue (nameSize, new string [] { version .ToString (),               // 0
                                                                    ClientSize .Width .ToString (),     // 1 
                                                                    ClientSize .Height .ToString ()     // 2
                                                                  },
                                                    RegistryValueKind .MultiString);

                        groupFunction .IntoRegistry (regkey, "GroupFunction");
                        groupLanguage .IntoRegistry (regkey, "GroupLanguage");
                        msplot .IntoRegistry (regkey, "Plot");
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // -------------------------------------------------        RestoreFromRegistry
        private void RestoreFromRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                bRestore = false;
                regkey = Registry .CurrentUser .OpenSubKey (strkey);
                if (regkey != null)
                {
                    string [] strs = (string []) regkey .GetValue (nameSize);
                    if (strs != null && strs .Length == 3  &&  Convert .ToInt32 (strs [0]) == 605)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 1);
                    }
                    else
                    {
                        return;
                    }
                    groupFunction = ElasticGroup .FromRegistry (this, regkey, "GroupFunction", 
                                                                new Control [] {textFunction, labelErrorYX, btnShowFunc, btnAddFunc, 
                                                                                btnChangeFunc, textName } );
                    groupLanguage = ElasticGroup .FromRegistry (this, regkey, "GroupLanguage", 
                                                                new Control [] {labelOperations, labelFunctions } );
                    msplot = MSPlot .FromRegistry (this, regkey, "Plot");
                    if (groupFunction == null || groupLanguage == null  ||  msplot == null)
                    {
                        return;
                    }
                    bRestore = true;
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
    }
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions