Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

User-driven applications

, 10 Apr 2010
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.
TheoryOfUserDrivenApplications.zip
TheoryOfUserDrivenApplications
TheoryOfUserDrivenApplications
bin
Debug
TheoryOfUserDrivenApplications.vshost.exe
Release
MoveGraphLibrary.dll
TheoryOfUserDrivenApplications.bin
TheoryOfUserDrivenApplications.exe
TheoryOfUserDrivenApplications.vshost.exe
Properties
Settings.settings
Resources
ArrowDown.bmp
ArrowUp.bmp
DELETE.BMP
Hangar.bmp
Lock.bmp
palette_.bmp
RuralHouse.bmp
RuralLeftPlus.bmp
RuralRightPlus.bmp
SimpleHouse.bmp
User_driven_applications.doc
User_driven_applications.pdf
using System;
using System .Collections .Generic;
using System .ComponentModel;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace UserDrivenApplications
{
    public enum Calculator_DigitsGroupView
    {
        OneRow, TwoRows, TwoRowsUneven, ThreeRows, ThreeRowsUneven, FourRows, SixRows,
        OneCol, TwoCols, TwoColsUneven, ThreeCols, ThreeColsUneven, FourCols, SixCols,
        Arbitrary
    };
    public enum Calculator_OperationsGroupView { OneRow, TwoRowsUneven, ThreeRows, OneCol, TwoColsUneven, ThreeCols, Arbitrary };
    public enum Calculator_FunctionsGroupView { OneRow, TwoRowsUneven, ThreeRows, OneCol, TwoColsUneven, ThreeCols, Arbitrary };
    public enum Grouped { None, Digits, Operations, Functions, Arbitrary };

    public partial class Form_Calculator : Form
    {
        int version = 606; 

        const string strAddRegKey = "Form_LiveCalculator";
        Mover mover;
        Point ptMouse_Down, ptMouse_Up, ptMouse_Move;
        bool bRectInView = false;
        ElasticGroup frame;      
        Grouped grouped;
        List<Control> controlsSingle = new List<Control> ();
        List<Control> controlsInFrame = new List<Control> ();

        int spaceDigits_Hor, spaceDigits_Ver, spaceOperations_Hor, spaceOperations_Ver, spaceFunctions_Hor, spaceFunctions_Ver;
        Control [] controlOperationPlus, controlInfo, controlClear;

        List<Control> listDigits = new List<Control> ();
        List<Control> listOperations = new List<Control> ();
        List<Control> listFunctions = new List<Control> ();

        Calculator_DigitsGroupView groupviewDigits = Calculator_DigitsGroupView .Arbitrary;
        Calculator_OperationsGroupView groupviewOperations = Calculator_OperationsGroupView .Arbitrary;
        Calculator_FunctionsGroupView groupviewFunctions = Calculator_FunctionsGroupView .Arbitrary;
        int [] placesDigits;
        int [] placesOperations;
        int [] placesFunctions;

        int nMaxStrLen = 20;
        List<double> args = new List<double> ();
        List<OperationPair> opPair = new List<OperationPair> ();

        Control controlTouched;

        Size sizeClientDefault;
        Rectangle [] rcDefault;
        Font fntFormDefault, fntDigitDefault, fntOperationDefault, fntFunctionDefault, fntInfoDefault;
        Color clrDigitDefault, clrOperationDefault, clrFunctionDefault;

        bool bClearRegistry = false;
        bool bMovable = true;

        Point ptMouseInit;

        // -------------------------------------------------
        public Form_Calculator (Point ptMouseScreen)
        {
            InitializeComponent ();
            ptMouseInit = ptMouseScreen;
            mover = new Mover (this);
            mover .Clipping = Clipping .Safe;

            GetDefaults ();

            Control [] controlDigit = new Control [] {  button_0, button_1, button_2, button_3, button_4, button_5, 
                                                        button_6, button_7, button_8, button_9, btnSign, btnDot };
            for (int i = 0; i < controlDigit .Length; i++)
            {
                listDigits .Add (controlDigit [i]);
            }
            Control [] controlOperation = new Control [] { btnPlus, btnMinus, btnMultiply, btnDivide, btnDegree, btnEqual };
            for (int i = 0; i < controlOperation .Length; i++)
            {
                listOperations .Add (controlOperation [i]);
            }
            Control [] controlFunction = new Control [] { btnLn, btnLog, btnExp, btnSin, btnCos, btnTg, btnInverse, btnSqrt };
            for (int i = 0; i < controlFunction .Length; i++)
            {
                listFunctions .Add (controlFunction [i]);
            }
            controlOperationPlus = new Control [] {btnPlus, btnMinus, btnMultiply, btnDivide, btnDegree, btnEqual, 
                                                   btnBackspace, btnClearAll, btnClearEdit};
            controlInfo = new Control [] { textboxValue, labelExpression };
            controlClear = new Control [] { btnBackspace, btnClearAll, btnClearEdit };

            grouped = Grouped .None;

            groupviewDigits = Calculator_DigitsGroupView .TwoRowsUneven;
            Auxi_Controls .ControlsByRows (listDigits, 10, spaceDigits_Hor, spaceDigits_Ver);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            bool bRestored = false;
            Rectangle rc = new Rectangle ();
            RestoreFromRegistry (out bRestored, ref rc);

            Location = ptMouseInit;
            SetGroup (rc); // RenewMover (); is inside !!!
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            foreach (Control ctrl in controlsSingle)
            {
                mover .Add (new FramedControl (ctrl, bMovable));
            }
            if (controlsInFrame .Count > 0)
            {
                string title = "";
                if (EqualLists (controlsInFrame, listDigits))
                {
                    grouped = Grouped .Digits;
                    title = "Digits";
                }
                else if (EqualLists (controlsInFrame, listOperations))
                {
                    grouped = Grouped .Operations;
                    title = "Operations";
                }
                else if (EqualLists (controlsInFrame, listFunctions))
                {
                    grouped = Grouped .Functions;
                    title = "Functions";
                }
                else
                {
                    grouped = Grouped .Arbitrary;
                    title = "Arbitrary";
                }
                frame = new ElasticGroup (this, controlsInFrame, new int [] { 10, 6, 10, 10 }, title); 
                frame .Movable = bMovable;
                frame .IntoMover (mover, 0);
            }
            else
            {
                grouped = Grouped .None;
            }
        }
        // -------------------------------------------------        GetDefaults
        private void GetDefaults ()
        {
            sizeClientDefault = ClientSize;
            int nCtrls = Controls .Count;
            rcDefault = new Rectangle [nCtrls];
            for (int i = 0; i < nCtrls; i++)
            {
                rcDefault [i] = Controls [i] .Bounds;
            }
            fntFormDefault = this .Font;
            fntDigitDefault = button_0 .Font;
            fntOperationDefault = btnPlus .Font;
            fntFunctionDefault = btnSqrt .Font;
            fntInfoDefault = textboxValue .Font;
            clrDigitDefault = button_0 .ForeColor;
            clrOperationDefault = btnPlus .ForeColor;
            clrFunctionDefault = btnSqrt .ForeColor;
        }

        // ***************      Calculator
        // -------------------------------------------------        Click_btnBackspace
        private void Click_btnBackspace (object sender, EventArgs e)
        {
            string str = textboxValue .Text .Trim ();
            if (str .Length > 0)
            {
                str = str .Remove (str .Length - 1, 1);
                textboxValue .Text = str;
            }
        }
        // -------------------------------------------------        Click_btnClearAll
        private void Click_btnClearAll (object sender, EventArgs e)
        {
            args .Clear ();
            opPair .Clear ();
            ShowExpression ();
            textboxValue .Text = "";
        }
        // -------------------------------------------------        Click_btnClearEdit
        private void Click_btnClearEdit (object sender, EventArgs e)
        {
            textboxValue .Text = "";
        }
        // -------------------------------------------------        NewValueExist
        private bool NewValueExist (ref double fRead)
        {
            string str = textboxValue .Text .Trim ();
            textboxValue .Text = str;
            return (str .Length > 0 && Double .TryParse (str, out fRead));
        }
        // -------------------------------------------------        ShowExpression
        private void ShowExpression ()
        {
            string str = "";
            for (int i = 0; i < Math .Min (args .Count, opPair .Count); i++)
            {
                str += args [i] .ToString () + opPair [i] .ToString ();
            }
            labelExpression .Text = str;
        }
        // -------------------------------------------------        Click_btnEqual
        private void Click_btnEqual (object sender, EventArgs e)
        {
            double fRead = 0.0;
            if (NewValueExist (ref fRead) && opPair .Count > 0)
            {
                if (opPair [opPair .Count - 1] .Operation == Operation .Divide && fRead == 0.0)
                {
                    MessageBox .Show ("Cannot divide by zero ");
                    return;
                }
                else
                {
                    string str = labelExpression .Text + textboxValue .Text + " = ";
                    args .Add (fRead);
                    while (args .Count > 1)
                    {
                        double result = 0.0;
                        double a = args [args .Count - 2];
                        double b = args [args .Count - 1];
                        switch (opPair [opPair .Count - 1] .Operation)
                        {
                            case Operation .Plus: result = a + b; break;
                            case Operation .Minus: result = a - b; break;
                            case Operation .Multiply: result = a * b; break;
                            case Operation .Divide: result = a / b; break;
                            case Operation .Degree: result = Math .Pow (a, b); break;
                        }
                        args .RemoveAt (args .Count - 1);
                        args [args .Count - 1] = result;
                        opPair .RemoveAt (opPair .Count - 1);
                    }
                    str += args [0] .ToString ();

                    labelExpression .Text = str;
                    opPair .Clear ();
                    textboxValue .Text = args [0] .ToString ();
                }
            }
        }
        // -------------------------------------------------        Click_number
        private void Click_number (object sender, EventArgs e)
        {
            string strNum = (sender as Button) .Text;
            string str = textboxValue .Text .Trim ();

            if (str .Length < nMaxStrLen)
            {
                if (str .Length == 1 && str [0] == '0')
                {
                    str = strNum;
                }
                else
                {
                    str += strNum;
                }
                textboxValue .Text = str;
            }
        }
        // -------------------------------------------------        Click_btnDot
        private void Click_btnDot (object sender, EventArgs e)
        {
            double a = 0.0;
                
            if (NewValueExist (ref a))
            {
                string str = textboxValue .Text .Trim ();

                if (str .Length > 0 && str .Length < nMaxStrLen && char .IsDigit (str [str .Length - 1]) && !str .Contains ("."))
                {
                    str += ".";
                    textboxValue .Text = str;
                }
            }
        }
        // -------------------------------------------------        Click_operation
        private void Click_operation (object sender, EventArgs e)
        {
            OperationPair newOpPair = new OperationPair (OperationFromButton (sender as Control));
            double fRead = 0.0;

            if (NewValueExist (ref fRead))
            {
                if (args .Count <= 0)
                {
                    args .Add (fRead);
                    opPair .Add (newOpPair);
                }
                else if (args .Count == 1 && opPair .Count == 0)
                {
                    args [0] = fRead;
                    opPair .Add (newOpPair);
                }
                else
                {
                    if (opPair [opPair .Count - 1] .Priority < newOpPair .Priority)
                    {
                        args .Add (fRead);
                        opPair .Add (newOpPair);
                    }
                    else
                    {
                        args .Add (fRead);
                        while (args .Count > 1 && opPair [opPair .Count - 1] .Priority <= newOpPair .Priority)
                        {
                            double result = 0.0;
                            double a = args [args .Count - 2];
                            double b = args [args .Count - 1];
                            switch (opPair [opPair .Count - 1] .Operation)
                            {
                                case Operation .Plus: result = a + b; break;
                                case Operation .Minus: result = a - b; break;
                                case Operation .Multiply: result = a * b; break;
                                case Operation .Divide: result = a / b; break;
                                case Operation .Degree: result = Math .Pow (a, b); break;
                            }
                            args .RemoveAt (args .Count - 1);
                            args [args .Count - 1] = result;
                            opPair .RemoveAt (opPair .Count - 1);
                        }
                        opPair .Add (newOpPair);
                    }
                }
                ShowExpression ();
                textboxValue .Text = "";
            }
            else
            {
                if (opPair .Count > 0)
                {
                    opPair [opPair .Count - 1] .Operation = newOpPair .Operation;
                    ShowExpression ();
                }
            }
        }
        // -------------------------------------------------        Click_btnSign
        private void Click_btnSign (object sender, EventArgs e)
        {
            double a = 0.0;
            if (NewValueExist (ref a))
            {
                double fNeg = -2.0;
                string strNeg = fNeg .ToString ();
                string str = textboxValue .Text .Trim ();

                if (str [0] == strNeg [0])
                {
                    str = str .Remove (0, 1);
                }
                else
                {
                    str = str .Insert (0, strNeg .Substring (0, 1));
                }
                textboxValue .Text = str;
            }
        }
        // -------------------------------------------------        Click_function
        private void Click_function (object sender, EventArgs e)
        {
            double fRead = 0.0;
            if (NewValueExist (ref fRead))
            {
                double fTmp;
                Control ctrl = sender as Control;
                if (ctrl == btnSin)
                {
                    fTmp = Math .Sin (fRead);
                }
                else if (ctrl == btnCos)
                {
                    fTmp = Math .Cos (fRead);
                }
                else if (ctrl == btnTg)
                {
                    fTmp = Math .Tan (fRead);
                }
                else if (ctrl == btnLn)
                {
                    if (fRead <= 0.0)
                    {
                        MessageBox .Show ("Invalid argument for logarithm");
                        return;
                    }
                    fTmp = Math .Log (fRead);
                }
                else if (ctrl == btnLog)
                {
                    if (fRead <= 0.0)
                    {
                        MessageBox .Show ("Invalid argument for logarithm");
                        return;
                    }
                    fTmp = Math .Log10 (fRead);
                }
                else if (ctrl == btnExp)
                {
                    fTmp = Math .Exp (fRead);
                }
                else if (ctrl == btnSqrt)
                {
                    if (fRead < 0.0)
                    {
                        MessageBox .Show ("Invalid argument for a square root");
                        return;
                    }
                    fTmp = Math .Sqrt (fRead);
                }
                else                    // if (ctrl == btnInverse)
                {
                    if (fRead == 0.0)
                    {
                        MessageBox .Show ("Invalid argument for 1/x");
                        return;
                    }
                    fTmp = 1.0 / fRead;
                }
                textboxValue .Text = fTmp .ToString ();
            }
        }
        // -------------------------------------------------        OperationFromButton
        private Operation OperationFromButton (Control ctrl)
        {
            if (ctrl == btnPlus)
            {
                return (Operation .Plus);
            }
            else if (ctrl == btnMinus)
            {
                return (Operation .Minus);
            }
            else if (ctrl == btnMultiply)
            {
                return (Operation .Multiply);
            }
            else if (ctrl == btnDivide)
            {
                return (Operation .Divide);
            }
            else                            //if (ctrl == btnDegree)
            {
                return (Operation .Degree);
            }
        }

        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            if (bRectInView)
            {
                grfx .DrawRectangle (Pens .Blue, Math .Min (ptMouse_Down .X, ptMouse_Move .X),
                                                 Math .Min (ptMouse_Down .Y, ptMouse_Move .Y),
                                     Math .Abs (ptMouse_Move .X - ptMouse_Down .X), Math .Abs (ptMouse_Move .Y - ptMouse_Down .Y));
            }
            if (controlsInFrame .Count > 0)
            {
                frame .Draw (grfx);
            }
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            ContextMenuStrip = null;
            if (!mover .Catch (e .Location, e .Button) && e .Button == MouseButtons .Left)
            {
                bRectInView = true;
            }
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            double dist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);
            if (mover .Release ())
            {
                if (e .Button == MouseButtons .Left)
                {
                    SwitchToArbitrary (mover .WasCaughtObject);
                }
                else if (e .Button == MouseButtons .Right  &&  dist <= 3)
                {
                    ContextMenuSelection (mover .WasCaughtObject);
                }
            }
            else
            {
                if (bRectInView)
                {
                    SetGroup (new Rectangle (Math .Min (ptMouse_Down .X, e .X), Math .Min (ptMouse_Down .Y, e .Y),
                                             Math .Abs (e .X - ptMouse_Down .X), Math .Abs (e .Y - ptMouse_Down .Y)));
                    bRectInView = false;
                }
                else
                {
                    if (e .Button == MouseButtons .Right && dist <= 3)
                    {
                        ContextMenuStrip = menuOnEmpty;
                    }
                }
            }
            Invalidate ();
        }
        // -------------------------------------------------        SwitchToArbitrary
        private void SwitchToArbitrary (int iInMover)
        {
            GraphicalObject grobj = mover [iInMover] .Source;

            if (grobj is FramedControl)
            {
                Control btn = (grobj as FramedControl) .Control as Button;
                if (listDigits .Contains (btn))
                {
                    groupviewDigits = Calculator_DigitsGroupView .Arbitrary;
                }
                else if (listOperations .Contains (btn))
                {
                    groupviewOperations = Calculator_OperationsGroupView .Arbitrary;
                }
                else if (listFunctions .Contains (btn))
                {
                    groupviewFunctions = Calculator_FunctionsGroupView .Arbitrary;
                }
            }
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseMove (object sender, MouseEventArgs e)
        {
            ptMouse_Move = e .Location;
            if (mover .Move (e .Location))
            {
                Invalidate ();
                if (mover [mover .CaughtObject] .Source is ElasticGroup)
                {
                    Update ();
                }
            }
            else
            {
                if (bRectInView)
                {
                    Invalidate ();
                }
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        ContextMenuSelection
        private void ContextMenuSelection (int iInMover)
        {
            GraphicalObject grobj = mover [iInMover] .Source;

            if (grobj is FramedControl)
            {
                Control ctrl = (grobj as FramedControl) .Control;
                if (listDigits .Contains (ctrl))
                {
                    controlTouched = ctrl;
                    ContextMenuStrip = menuOnDigitBtn;
                }
                else if (listOperations .Contains (ctrl))
                {
                    controlTouched = ctrl;
                    ContextMenuStrip = menuOnOperationBtn;
                }
                else if (listFunctions .Contains (ctrl))
                {
                    controlTouched = ctrl;
                    ContextMenuStrip = menuOnFunctionBtn;
                }
            }
        }
        // -------------------------------------------------        EqualLists
        private bool EqualLists (List<Control> list_0, List<Control> list_1)
        {
            if (list_0 != null && list_1 != null && list_0 .Count == list_1 .Count)
            {
                for (int i = 0; i < list_0 .Count; i++)
                {
                    if (!list_1 .Contains (list_0 [i]))
                    {
                        return (false);
                    }
                }
                return (true);
            }
            return (false);
        }
        // -------------------------------------------------        SetGroup
        private void SetGroup (Rectangle rc)
        {
            controlsSingle .Clear ();
            controlsInFrame .Clear ();

            foreach (Control control in Controls)
            {
                if (rc .Contains (control .Bounds))
                {
                    controlsInFrame .Add (control);
                }
                else
                {
                    controlsSingle .Add (control);
                }
            }
            if (controlsInFrame .Count == 1)
            {
                controlsSingle .Add (controlsInFrame [0]);
                controlsInFrame .Clear ();
            }
            RenewMover ();
        }
        // -------------------------------------------------        SetGroup
        private void SetGroup (List<Control> ctrlsToFrame)
        {
            controlsSingle .Clear ();
            controlsInFrame .Clear ();

            if (ctrlsToFrame == null || ctrlsToFrame .Count <= 0)
            {
                foreach (Control control in Controls)
                {
                    controlsSingle .Add (control);
                }
            }
            else
            {
                foreach (Control control in Controls)
                {
                    if (ctrlsToFrame .Contains (control))
                    {
                        controlsInFrame .Add (control);
                    }
                    else
                    {
                        controlsSingle .Add (control);
                    }
                }
            }
            if (controlsInFrame .Count == 1)
            {
                controlsSingle .Add (controlsInFrame [0]);
                controlsInFrame .Clear ();
            }
            RenewMover ();
        }

        // *************    menuMain
        // -------------------------------------------------        DropDownOpening_miSettings
        private void DropDownOpening_miSettings (object sender, EventArgs e)
        {
            ToolStripMenuItem itemParent = (ToolStripMenuItem) (menuMain .Items ["miSettings"]);
            ToolStripItemCollection items = itemParent .DropDownItems;

            items ["miFixUnfixObjects"] .Text = bMovable ? "Fix all objects" : "Unfix all objects";
            items ["miFrameDigits"] .Enabled = grouped != Grouped .Digits;
            items ["miFrameOperations"] .Enabled = grouped != Grouped .Operations;
            items ["miFrameFunctions"] .Enabled = grouped != Grouped .Functions;
            items ["miDeleteFrame"] .Enabled = grouped != Grouped .None;
        }
        // -------------------------------------------------        Click_miFixUnfixElements
        private void Click_miFixUnfixElements (object sender, EventArgs e)
        {
            bMovable = !bMovable;
            RenewMover ();
        }
        // -------------------------------------------------        Click_miFrameDigits
        private void Click_miFrameDigits (object sender, EventArgs e)
        {
            SetGroup (listDigits);
            Invalidate ();
        }
        // -------------------------------------------------        Click_miFrameOperations
        private void Click_miFrameOperations (object sender, EventArgs e)
        {
            SetGroup (listOperations);
            Invalidate ();
        }
        // -------------------------------------------------        Click_miFrameFunctions
        private void Click_miFrameFunctions (object sender, EventArgs e)
        {
            SetGroup (listFunctions);
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDeleteFrame
        private void Click_miDeleteFrame (object sender, EventArgs e)
        {
            SetGroup (new Rectangle ());
            Invalidate ();
        }
        // -------------------------------------------------        Click_miUniversalFont
        private void Click_miUniversalFont (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = button_0 .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                foreach (Control control in Controls)
                {
                    control .Font = dlg .Font;
                }
            }
        }
        // -------------------------------------------------        Click_miInformationFont
        private void Click_miInformationFont (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = textboxValue .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                SetInformationFont (dlg .Font);
            }
        }
        // -------------------------------------------------        SetInformationFont
        private void SetInformationFont (Font fnt)
        {
            Auxi_Controls .SameFont (controlInfo, fnt);
        }
        // -------------------------------------------------        OrderedControls
        // all controls are supposed to be of the same size as ctrls [0] !!!
        private void OrderedControls (List<Control> ctrls, int [] places, int spaceHor, int spaceVer) 
        {
            int w = ctrls [0] .Width;
            int h = ctrls [0] .Height;
            int cxL = ctrls [0] .Left;
            int cyT = ctrls [0] .Top;
            int nControls = ctrls .Count;
            int iRow, jCol;
            for (int i = 1; i < ctrls .Count; i++)
            {
                iRow = places [i] / nControls;
                jCol = places [i] % nControls;
                ctrls [i] .Location = new Point (cxL + jCol * (w + spaceHor), cyT + iRow * (h + spaceVer));
            }
        }

        // *************    for the menuMain  -  Digits
        // -------------------------------------------------        Click_miDigits_StandardPositioning
        private void Click_miDigits_StandardPositioning (object sender, EventArgs e)
        {
            Form_BtnsPlacement_Digits form = new Form_BtnsPlacement_Digits ((int) groupviewDigits, spaceDigits_Hor, spaceDigits_Ver);   
            if (DialogResult .OK == form .ShowDialog ())
            {
                spaceDigits_Hor = form .HorSpaces; 
                spaceDigits_Ver = form .VerSpaces; 
                groupviewDigits = form .SelectedView;
                placesDigits = form .Places;
                UpdateGroup_Digits ();
            }
        }
        // -------------------------------------------------        UpdateGroup_Digits
        private void UpdateGroup_Digits ()
        {
            if (groupviewDigits != Calculator_DigitsGroupView .Arbitrary)
            {
                OrderedControls (listDigits, placesDigits, spaceDigits_Hor, spaceDigits_Ver);
            }
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miDigits_Font
        private void Click_miDigits_Font (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = button_0 .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameFont (listDigits, dlg .Font);
            }
        }
        // -------------------------------------------------        Click_miDigits_Color
        private void Click_miDigits_Color (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = button_0 .ForeColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameForeColor (listDigits, dlg .Color);
            }
        }

        // *************    for the menuMain  -  Operations
        // -------------------------------------------------        Click_miOperations_StandardPositioning
        private void Click_miOperations_StandardPositioning (object sender, EventArgs e)
        {
            Form_BtnsPlacement_Operations form = new Form_BtnsPlacement_Operations ((int) groupviewOperations, spaceOperations_Hor,
                                                                                                               spaceOperations_Ver);
            if (DialogResult .OK == form .ShowDialog ())
            {
                spaceOperations_Hor = form .HorSpaces;
                spaceOperations_Ver = form .VerSpaces;
                groupviewOperations = form .SelectedView;
                placesOperations = form .Places;
                UpdateGroup_Operations ();
            }
        }
        // -------------------------------------------------        UpdateFrame_Operations
        private void UpdateGroup_Operations ()
        {
            if (groupviewOperations != Calculator_OperationsGroupView .Arbitrary)
            {
                OrderedControls (listOperations, placesOperations, spaceOperations_Hor, spaceOperations_Ver); 
            }
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miOperations_Font
        private void Click_miOperations_Font (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = btnPlus .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameFont (controlOperationPlus, dlg .Font);
            }
        }
        // -------------------------------------------------        Click_miOperations_Color
        private void Click_miOperations_Color (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = btnPlus .ForeColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameForeColor (controlOperationPlus, dlg .Color);
            }
        }

        // *************    for the menuMain  -  Functions
        // -------------------------------------------------        Click_miFunctions_StandardPositioning
        private void Click_miFunctions_StandardPositioning (object sender, EventArgs e)
        {
            Form_BtnsPlacement_Functions form = new Form_BtnsPlacement_Functions ((int) groupviewFunctions, spaceFunctions_Hor,
                                                                                                            spaceFunctions_Ver);
            if (DialogResult .OK == form .ShowDialog ())
            {
                spaceFunctions_Hor = form .HorSpaces;
                spaceFunctions_Ver = form .VerSpaces;
                groupviewFunctions = form .SelectedView;
                placesFunctions = form .Places;
                UpdateGroup_Functions ();
            }
        }
        // -------------------------------------------------        UpdateFrame_Functions
        private void UpdateGroup_Functions ()
        {
            if (groupviewFunctions != Calculator_FunctionsGroupView .Arbitrary)
            {
                OrderedControls (listFunctions, placesFunctions, spaceFunctions_Hor, spaceFunctions_Ver); 
            }
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miFunctions_Font
        private void Click_miFunctions_Font (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = btnSqrt .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameFont (listFunctions, dlg .Font);
            }
        }
        // -------------------------------------------------        Click_miFunctions_Color
        private void Click_miFunctions_Color (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = btnSqrt .ForeColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameForeColor (listFunctions, dlg .Color);
            }
        }

        // *************    for the menuOnDigitBtn
        // -------------------------------------------------        Click_miDigitBtn_SizeForDigits
        private void Click_miDigitBtn_SizeForDigits (object sender, EventArgs e)
        {
            Auxi_Controls .SameSize (listDigits, new Size (controlTouched .Width, controlTouched .Height));
            UpdateGroup_Digits ();
        }
        // -------------------------------------------------        Click_miDigitBtn_SizeForDigitsOperations
        private void Click_miDigitBtn_SizeForDigitsOperations (object sender, EventArgs e)
        {
            SameSizeDigitsOperations ();
        }
        // -------------------------------------------------        SameSizeDigitsOperations
        private void SameSizeDigitsOperations ()
        {
            Size size = new Size (controlTouched .Width, controlTouched .Height);
            Auxi_Controls .SameSize (listDigits, size);
            Auxi_Controls .SameSize (listOperations, size);
            UpdateGroup_Digits ();
            UpdateGroup_Operations ();
        }
        // -------------------------------------------------        Click_miDigitBtn_FontSample
        private void Click_miDigitBtn_FontSample (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = controlTouched .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameFont (listDigits, dlg .Font);
            }
        }
        // -------------------------------------------------        Click_miDigitBtn_ColorSample
        private void Click_miDigitBtn_ColorSample (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = controlTouched .ForeColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameForeColor (listDigits, dlg .Color);
            }
        }

        // *************    for the menuOnOperationBtn
        // -------------------------------------------------        Click_miOperationBtn_SizeForOperations
        private void Click_miOperationBtn_SizeForOperations (object sender, EventArgs e)
        {
            Auxi_Controls .SameSize (listOperations, new Size (controlTouched .Width, controlTouched .Height));
            UpdateGroup_Operations ();
        }
        // -------------------------------------------------        Click_miOperationBtn_SizeForDigitsOperations
        private void Click_miOperationBtn_SizeForDigitsOperations (object sender, EventArgs e)
        {
            SameSizeDigitsOperations ();
        }
        // -------------------------------------------------        Click_miOperationBtn_FontSample
        private void Click_miOperationBtn_FontSample (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = controlTouched .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameFont (listOperations, dlg .Font);
            }
        }
        // -------------------------------------------------        Click_miOperationBtn_ColorSample
        private void Click_miOperationBtn_ColorSample (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = controlTouched .ForeColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameForeColor (listOperations, dlg .Color);
            }
        }

        // *************    for the menuOnFunctionBtn
        // -------------------------------------------------        Click_miFunctionBtn_SizeForFunctions
        private void Click_miFunctionBtn_SizeForFunctions (object sender, EventArgs e)
        {
            Auxi_Controls .SameSize (listFunctions, new Size (controlTouched .Width, controlTouched .Height));
            UpdateGroup_Functions ();
        }
        // -------------------------------------------------        Click_miFunctionBtn_SizeForThreeGroups
        private void Click_miFunctionBtn_SizeForThreeGroups (object sender, EventArgs e)
        {
            Size size = new Size (controlTouched .Width, controlTouched .Height);
            Auxi_Controls .SameSize (listDigits, size);
            Auxi_Controls .SameSize (listOperations, size);
            Auxi_Controls .SameSize (listFunctions, size);
            UpdateGroup_Digits ();
            UpdateGroup_Operations ();
            UpdateGroup_Functions ();
        }
        // -------------------------------------------------        Click_miFunctionBtn_FontSample
        private void Click_miFunctionBtn_FontSample (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = controlTouched .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameFont (listFunctions, dlg .Font);
            }
        }
        // -------------------------------------------------        Click_miFunctionBtn_ColorSample
        private void Click_miFunctionBtn_ColorSample (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = controlTouched .ForeColor;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                Auxi_Controls .SameForeColor (listFunctions, dlg .Color);
            }
        }

        // ***********     menuOnEmpty
        // -------------------------------------------------        Opening_menuOnEmpty
        private void Opening_menuOnEmpty (object sender, CancelEventArgs e)
        {
            menuOnEmpty .Items ["miFixUnfixObjects_2"] .Text = bMovable ? "Fix all objects" : "Unfix all objects";
        }

        // -------------------------------------------------        Click_miDefaultView
        private void Click_miDefaultView (object sender, EventArgs e)
        {
            if (DialogResult .Yes ==
                    MessageBox .Show ("On setting the Default view, all the current settings will be lost. Do you want to proceed?",
                                      "Default view",
                                      MessageBoxButtons .YesNo, MessageBoxIcon .Question, MessageBoxDefaultButton .Button1))
            {
                bClearRegistry = true;
                DialogResult = DialogResult .Retry;
                Close ();
            }
        }
        // -------------------------------------------------        Click_miAboutCalculator
        private void Click_miAboutCalculator (object sender, EventArgs e)
        {
            Form_AboutCalculator form = new Form_AboutCalculator ();
            form .ShowDialog ();
        }

        const string nameAuxi = "Auxi";
        const string nameLocation = "Location";
        const string nameFonts = "Fonts";
        const string nameColors = "Colors";
        // -------------------------------------------------        SaveInfoToRegistry
        private void SaveInfoToRegistry ()
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            try
            {
                if (bClearRegistry)
                {
                    Registry .CurrentUser .DeleteSubKey (strkey, false);
                }
                else
                {
                    regkey = Registry .CurrentUser .CreateSubKey (strkey);
                    if (regkey != null)
                    {
                        string [] strAuxi = {version .ToString (),          // 0
                                             ClientSize .Width .ToString (),            // 1
                                             ClientSize .Height .ToString (),           // 2
                                             Controls .Count .ToString (),              // 3
                                             ((int) groupviewDigits) .ToString (),      // 4
                                             ((int) groupviewOperations) .ToString (),  // 5
                                             ((int) groupviewFunctions) .ToString (),   // 6
                                             ((int) grouped) .ToString (),              // 7
                                             "", "", "", "",                            // 8 - 11
                                             bMovable .ToString (),         // 12
                                             spaceDigits_Hor .ToString (),          // 13
                                             spaceDigits_Ver .ToString (),          // 14
                                             spaceOperations_Hor .ToString (),          // 15
                                             spaceOperations_Ver .ToString (),          // 16
                                             spaceFunctions_Hor .ToString (),       // 17
                                             spaceFunctions_Ver .ToString (),       // 18

                                            };
                        if (grouped != Grouped .None)
                        {
                            strAuxi [8] = frame .FrameArea .Left .ToString ();  
                            strAuxi [9] = frame .FrameArea .Top .ToString ();    
                            strAuxi [10] = frame .FrameArea .Width .ToString ();  
                            strAuxi [11] = frame .FrameArea .Height .ToString ();  
                        }
                        regkey .SetValue (nameAuxi, strAuxi, RegistryValueKind .MultiString);

                        int nControls = Controls .Count;
                        string [] strSizes = new string [nControls * 4];
                        for (int i = 0; i < nControls; i++)
                        {
                            strSizes [i * 4] = Controls [i] .Left .ToString ();
                            strSizes [i * 4 + 1] = Controls [i] .Top .ToString ();
                            strSizes [i * 4 + 2] = Controls [i] .Width .ToString ();
                            strSizes [i * 4 + 3] = Controls [i] .Height .ToString ();
                        }
                        regkey .SetValue (nameLocation, strSizes, RegistryValueKind .MultiString);

                        Font fontDigits = button_0 .Font;
                        Font fontOperations = btnPlus .Font;
                        Font fontFunctions = btnSqrt .Font;
                        Font fontInformation = textboxValue .Font;
                        string [] strFonts = {Font .Name .ToString (),              // 0
                                              Font .SizeInPoints .ToString (),      // 1
                                              ((int) (Font .Style)) .ToString (),   // 2

                                              fontDigits .Name .ToString (),              // 3
                                              fontDigits .SizeInPoints .ToString (),      // 4
                                              ((int) (fontDigits .Style)) .ToString (),   // 5

                                              fontOperations .Name .ToString (),                // 6
                                              fontOperations .SizeInPoints .ToString (),        // 7
                                              ((int) (fontOperations .Style)) .ToString (),     // 8

                                              fontFunctions .Name .ToString (),             // 9
                                              fontFunctions .SizeInPoints .ToString (),     // 10
                                              ((int) (fontFunctions .Style)) .ToString (),  // 11

                                              fontInformation .Name .ToString (),               // 12
                                              fontInformation .SizeInPoints .ToString (),       // 13
                                              ((int) (fontInformation .Style)) .ToString (),    // 14
                                             };
                        regkey .SetValue (nameFonts, strFonts, RegistryValueKind .MultiString);

                        Color clrNums = button_0 .ForeColor;
                        Color clrOperations = btnPlus .ForeColor;
                        Color clrFunctions = btnSqrt .ForeColor;
                        string [] strClrs = {((int) (clrNums .A)) .ToString (),  // 0
                                             ((int) (clrNums .R)) .ToString (),  // 1
                                             ((int) (clrNums .G)) .ToString (),  // 2
                                             ((int) (clrNums .B)) .ToString (),  // 3

                                             ((int) (clrOperations .A)) .ToString (),    // 4
                                             ((int) (clrOperations .R)) .ToString (),    // 5
                                             ((int) (clrOperations .G)) .ToString (),    // 6
                                             ((int) (clrOperations .B)) .ToString (),    // 7

                                             ((int) (clrFunctions .A)) .ToString (), // 8
                                             ((int) (clrFunctions .R)) .ToString (), // 9
                                             ((int) (clrFunctions .G)) .ToString (), // 10
                                             ((int) (clrFunctions .B)) .ToString (), // 11
                                            };
                        regkey .SetValue (nameColors, strClrs, RegistryValueKind .MultiString);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
        // -------------------------------------------------        RestoreFromRegistry
        private void RestoreFromRegistry (out bool bRestored, ref Rectangle rc)
        {
            string strkey = Form_Main .strRegKey + strAddRegKey;

            RegistryKey regkey = null;
            bRestored = false;
            try
            {
                regkey = Registry .CurrentUser .OpenSubKey (strkey);
                if (regkey != null)
                {
                    string [] strAuxi = (string []) regkey .GetValue (nameAuxi);
                    string [] strSizes = (string []) regkey .GetValue (nameLocation);
                    string [] strFonts = (string []) regkey .GetValue (nameFonts);
                    string [] strClrs = (string []) regkey .GetValue (nameColors);

                    if (strAuxi != null && strAuxi .Length == 19 && Convert .ToInt32 (strAuxi [0]) == 606 &&
                        strSizes != null && strSizes .Length == 4 * Convert .ToInt32 (strAuxi [3]) &&
                        strFonts != null && strFonts .Length == 15 &&
                        strClrs != null && strClrs .Length == 12)
                    {
                        ClientSize = Auxi_Convert .ToSize (strAuxi, 1);
                        groupviewDigits = (Calculator_DigitsGroupView) Convert .ToInt32 (strAuxi [4]);
                        groupviewOperations = (Calculator_OperationsGroupView) Convert .ToInt32 (strAuxi [5]);
                        groupviewFunctions = (Calculator_FunctionsGroupView) Convert .ToInt32 (strAuxi [6]);
                        if (Grouped .None != (Grouped) Convert .ToInt32 (strAuxi [7]))
                        {
                            Rectangle rcRead = Auxi_Convert .ToRectangle (strAuxi, 8);
                            if (rcRead .IsEmpty)
                            {
                                rc = new Rectangle ();
                            }
                            else
                            {
                                rc = rcRead;
                            }
                        }
                        else
                        {
                            rc = new Rectangle ();
                        }
                        bMovable = Convert .ToBoolean (strAuxi [12]);
                        spaceDigits_Hor = Convert .ToInt32 (strAuxi [13]);
                        spaceDigits_Ver = Convert .ToInt32 (strAuxi [14]);
                        spaceOperations_Hor = Convert .ToInt32 (strAuxi [15]);
                        spaceOperations_Ver = Convert .ToInt32 (strAuxi [16]);
                        spaceFunctions_Hor = Convert .ToInt32 (strAuxi [17]);
                        spaceFunctions_Ver = Convert .ToInt32 (strAuxi [18]);

                        for (int i = 0; i < strSizes .Length / 4; i++)
                        {
                            Controls [i] .Bounds = Auxi_Convert .ToRectangle (strSizes, i * 4);
                        }
                        Font = Auxi_Convert .ToFont (strFonts, 0);
                        Auxi_Controls .SameFont (listDigits, Auxi_Convert .ToFont (strFonts, 3));
                        Auxi_Controls .SameFont (listOperations, Auxi_Convert .ToFont (strFonts, 6));
                        Auxi_Controls .SameFont (listFunctions, Auxi_Convert .ToFont (strFonts, 9));
                        Auxi_Controls .SameFont (controlInfo, Auxi_Convert .ToFont (strFonts, 12));
                        Auxi_Controls .SameForeColor (listDigits, Auxi_Convert .ToColor (strClrs, 0));
                        Auxi_Controls .SameForeColor (listOperations, Auxi_Convert .ToColor (strClrs, 4));
                        Auxi_Controls .SameForeColor (listFunctions, Auxi_Convert .ToColor (strClrs, 8));
                    }
                    bRestored = true;
                }
            }
            catch
            {
            }
            finally
            {
                if (regkey != null) regkey .Close ();
            }
        }
    }
    // ******************************************
    public class OperationPair
    {
        Operation operation;
        Priority priority;

        // -------------------------------------------------
        public OperationPair (Operation oper)
        {
            operation = oper;
            DefinePriority ();
        }
        // -------------------------------------------------        DefinePriority
        private void DefinePriority ()
        {
            switch (operation)
            {
                case Operation .Plus:
                case Operation .Minus:
                    priority = Priority .Plus;
                    break;
                case Operation .Multiply:
                case Operation .Divide:
                    priority = Priority .Mult;
                    break;
                case Operation .Degree:
                    priority = Priority .Degr;
                    break;
            }
        }
        // -------------------------------------------------        Operation
        public Operation Operation
        {
            get { return (operation); }
            set
            {
                operation = value;
                DefinePriority ();
            }
        }
        // -------------------------------------------------        Priority
        public Priority Priority
        {
            get { return (priority); }
        }
        // -------------------------------------------------        ToString
        override public string ToString ()
        {
            switch (operation)
            {
                case Operation .Plus:
                default: return (" + ");
                case Operation .Minus: return (" - ");
                case Operation .Multiply: return (" * ");
                case Operation .Divide: return (" / ");
                case Operation .Degree: return (" ^ ");
            }
        }
    }
}

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)

Share

About the Author

SergeyAndreyev

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 10 Apr 2010
Article Copyright 2010 by SergeyAndreyev
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid