Click here to Skip to main content
15,881,281 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_DefineNewRing : Form
    {
        int version = 606;

        const string strAddRegKey = "Form_DefineNewRing";
        Point ptMouseInit;
        Mover mover;
        ElasticGroup group;
        Point ptMouse_Down, ptMouse_Up;
        Color clrStart, clrEnd;
        PrimitiveRing ring;
        double fSum;
        int nSectors;
        int iPressedSector;
        List<string> texts = new List<string> ();

        bool bShowAngle = true;
        bool bAfterInit = false;
        bool bRestore;
        SaveVersion saveVersion = SaveVersion .Full;

        // -------------------------------------------------        
        public Form_DefineNewRing (Point ptMouseScreen, int minSeg, int maxSeg)
        {
            InitializeComponent ();
            ptMouseInit = ptMouseScreen;
            numericSectors .Minimum = Math .Min (Math .Abs (minSeg), Math .Abs (maxSeg));
            numericSectors .Maximum = Math .Max (Math .Abs (minSeg), Math .Abs (maxSeg));
            mover = new Mover (this);
        }
        // -------------------------------------------------        OnLoad
        private void OnLoad (object sender, EventArgs e)
        {
            Location = Auxi_Common .FormPosition (this, ptMouseInit, SystemInformation .PrimaryMonitorSize .Width * 3 / 4,
                                                                     SystemInformation .PrimaryMonitorSize .Height * 3 / 4, 0);
            nSectors = 6;
            int iStart = 0;
            int iEnd = 6;
            if (iStart == iEnd) iEnd++;
            clrStart = Auxi_Colours .ColorPredefined (iStart);
            clrEnd = Auxi_Colours .ColorPredefined (iEnd);
            comboTexts .SelectedIndex = 0;

            RestoreFromRegistry ();
            if (!bRestore)
            {
                ElasticGroupElement [] elems = new ElasticGroupElement [] {
                             new ElasticGroupElement (new CommentedControl (this, numericSectors, Resizing .WE, Side .E, "Sectors")), 
                             new ElasticGroupElement (listValues), 
                             new ElasticGroupElement (new CommentedControl (this, btnStartClr, Side .E, SideAlignment .Center, "Start color")), 
                             new ElasticGroupElement (new CommentedControl (this, btnEndClr, Side .E, SideAlignment .Center, "End color")), 
                             new ElasticGroupElement (new CommentedControl (this, comboTexts, Side .N, SideAlignment .Left, "Texts")) };
                group = new ElasticGroup (this, elems, 10);
                Rectangle rc = ClientRectangle;
                Point ptCenter = new Point ((group .FrameArea .Right + rc .Width) / 2, rc .Height / 2);
                int nOutRad = Math .Min (rc .Width, rc .Height) * 9 / 20;
                int nInRad = nOutRad / 2;

                SetRing (ptCenter, nOutRad, nInRad);
            }
            numericSectors .Value = nSectors;
            PrepareRingTexts ();

            RenewMover ();

            toolTip1 .SetToolTip (btnStartClr, Auxi_Convert .ClrToStr (clrStart, ColorStr .IN_ROWS));
            toolTip1 .SetToolTip (btnEndClr, Auxi_Convert .ClrToStr (clrEnd, ColorStr .IN_ROWS));
            bAfterInit = true;
        }
        // -------------------------------------------------        SetRing
        private void SetRing (PointF pt, float radOut, float radIn)
        {
            double [] fVal = new double [nSectors];
            for (int i = 0; i < nSectors; i++)
            {
                fVal [i] = 200;
            }
            ring = new PrimitiveRing (pt, radOut, radIn, 0, fVal);
            fSum = Auxi_Common .SumArray (ring .Values, false);
            ring .SmoothColorLine (clrStart, clrEnd);
            PrepareRingTexts ();
            ShowValues ();
        }
        // -------------------------------------------------        PrepareRingTexts
        private void PrepareRingTexts ()
        {
            texts .Clear ();
            int nSrcLines;
            TxtSource txtsource = (TxtSource) comboTexts .SelectedIndex;
            switch (txtsource)
            {
                case TxtSource .Alphabet:
                    nSrcLines = Auxi_Common .strAlphabet .Length;
                    for (int i = 0; i < nSectors; i++)
                    {
                        texts .Add (Auxi_Common .strAlphabet [i % nSrcLines]);
                    }
                    break;

                case TxtSource .Days:
                    nSrcLines = Auxi_Common .strDays .Length;
                    for (int i = 0; i < nSectors; i++)
                    {
                        texts .Add (Auxi_Common .strDays [i % nSrcLines]);
                    }
                    break;

                case TxtSource .Months:
                    nSrcLines = Auxi_Common .strMonths .Length;
                    for (int i = 0; i < nSectors; i++)
                    {
                        texts .Add (Auxi_Common .strMonths [i % nSrcLines]);
                    }
                    break;

                case TxtSource .Names:
                    nSrcLines = Auxi_Common .strNames .Length;
                    for (int i = 0; i < nSectors; i++)
                    {
                        texts .Add (Auxi_Common .strNames [i % nSrcLines]);
                    }
                    break;

                case TxtSource .Undefined:
                    for (int i = 0; i < nSectors; i++)
                    {
                        texts .Add ("Undefined");
                    }
                    break;
            }
        }
        // -------------------------------------------------        ShowValues
        private void ShowValues ()
        {
            listValues .Items .Clear ();
            for (int i = 0; i < ring .Values .Length; i++)
            {
                listValues .Items .Add ((ring .Values [i] / fSum * 100) .ToString ("F2"));
            }
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            ring .IntoMover (mover, 0);
            group .IntoMover (mover, 0);
            mover .Insert (0, buttonOK);
        }
        // -------------------------------------------------        OnFormClosing
        private void OnFormClosing (object sender, FormClosingEventArgs e)
        {
            SaveInfoToRegistry ();
        }
        // -------------------------------------------------        OnPaint
        private void OnPaint (object sender, PaintEventArgs e)
        {
            Graphics grfx = e .Graphics;

            ring .Draw (grfx);
            group .Draw (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            if (mover .Catch (e .Location, e .Button, bShowAngle))
            {
                GraphicalObject grobj = mover .CaughtSource;
                if (grobj is PrimitiveRing)
                {
                    if (e .Button == MouseButtons .Left)
                    {
                        if (mover .CaughtNodeShape == NodeShape .Strip)
                        {
                            ring .StartResectoring (mover .CaughtNode);
                        }
                    }
                    else if (e .Button == MouseButtons .Right)
                    {
                        ring .StartRotation (e .Location);
                    }
                }
            }
            ContextMenuStrip = null;
        }
        // -------------------------------------------------        OnMouseUp
        private void OnMouseUp (object sender, MouseEventArgs e)
        {
            ptMouse_Up = e .Location;
            double nDist = Auxi_Geometry .Distance (ptMouse_Down, ptMouse_Up);
            int iWasCaught;
            if (mover .Release (out iWasCaught))
            {
                if (e .Button == MouseButtons .Right && nDist <= 3)
                {
                    GraphicalObject grobj = mover [iWasCaught] .Source;
                    if (grobj is ElasticGroup)
                    {
                        ContextMenuStrip = menuOnGroup;
                    }
                    else if (grobj is PrimitiveRing)
                    {
                        iPressedSector = ring .SectorFromPointAngle (ptMouse_Down);
                        ContextMenuStrip = menuOutsideGroup;
                    }
                }
            }
            else
            {
                if (e .Button == MouseButtons .Right && nDist <= 3)
                {
                    iPressedSector = -1;
                    ContextMenuStrip = menuOutsideGroup;
                }
            }
        }
        // -------------------------------------------------        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 ();
                }
                if (mover .CaughtSource is PrimitiveRing && mover .CaughtNodeShape == NodeShape .Strip)
                {
                    int iCounterclock, iClockwise;
                    ring .SectorsUnderChange (out iCounterclock, out iClockwise);
                    listValues .Items [iCounterclock] .Text = (ring .Values [iCounterclock] / fSum * 100) .ToString ("F2");
                    listValues .Items [iClockwise] .Text = (ring .Values [iClockwise] / fSum * 100) .ToString ("F2");
                    listValues .EnsureVisible (iClockwise);
                }
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        ValueChanged_numericSectors
        private void ValueChanged_numericSectors (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                nSectors = Convert .ToInt32 (numericSectors .Value);
                SetRing (ring .Center, ring .OuterRadius, ring .InnerRadius);
                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_btnStartClr
        private void Click_btnStartClr (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = clrStart;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                clrStart = dlg .Color;
                ring .SmoothColorLine (clrStart, clrEnd);
                toolTip1 .SetToolTip (btnStartClr, Auxi_Convert .ClrToStr (clrStart, ColorStr .IN_ROWS));
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_btnEndClr
        private void Click_btnEndClr (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = clrEnd;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                clrEnd = dlg .Color;
                ring .SmoothColorLine (clrStart, clrEnd);
                toolTip1 .SetToolTip (btnStartClr, Auxi_Convert .ClrToStr (clrStart, ColorStr .IN_ROWS));
                Invalidate ();
            }
        }
        // -------------------------------------------------        SelectedIndexChanged_comboTexts
        private void SelectedIndexChanged_comboTexts (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                PrepareRingTexts ();
                //Invalidate ();
            }
        }
        // -------------------------------------------------        Ring
        public PrimitiveRing Ring
        {
            get { return (ring); }
        }
        // -------------------------------------------------        Texts
        public List<string> Texts
        {
            get { return (texts); }
        }

        // **********   menuOnGroup
        // -------------------------------------------------        Opening_menuOnGroup
        private void Opening_menuOnGroup (object sender, CancelEventArgs e)
        {
            ToolStripItemCollection items = menuOnGroup .Items;

            items ["miFixUnfixElements"] .Text = group .ElementsMovable ? "Fix group's elements" : "Unfix group's elements";
            ((ToolStripMenuItem) items ["miTextsRotationAngle"]) .Checked = bShowAngle;
        }
        // -------------------------------------------------        Click_miModifyGroup
        private void Click_miModifyGroup (object sender, EventArgs e)
        {
            group .ParametersDialog (this, PointToScreen (ptMouse_Up), ParametersChanged);
        }
        // -------------------------------------------------		ParametersChanged
        private void ParametersChanged (object sender, EventArgs ea)
        {
            group .Update ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miFixUnfixElements
        private void Click_miFixUnfixElements (object sender, EventArgs e)
        {
            group .ElementsMovable = !group .ElementsMovable;
            RenewMover ();
        }
        // -------------------------------------------------        Click_miGroupDefaultView
        private void Click_miGroupDefaultView (object sender, EventArgs e)
        {
            group .DefaultView ();
            group .Update ();
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Click_miShowRotationAngle
        private void Click_miShowRotationAngle (object sender, EventArgs e)
        {
            bShowAngle = !bShowAngle;
        }

        // **********   menuOutsideGroup
        // -------------------------------------------------        Opening_menuOutsideGroup
        private void Opening_menuOutsideGroup (object sender, CancelEventArgs e)
        {
            ToolStripItemCollection items = menuOutsideGroup .Items;

            items ["miSectorColor"] .Enabled = iPressedSector >= 0;
            ((ToolStripMenuItem) items ["miRotationAngle"]) .Checked = bShowAngle;
        }
        // -------------------------------------------------        Click_miSectorColor
        private void Click_miSectorColor (object sender, EventArgs e)
        {
            ColorDialog dlg = new ColorDialog ();

            dlg .Color = ring .Colors [iPressedSector];
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                ring .SetSectorColor (iPressedSector, dlg .Color);
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_miFont
        private void Click_miFont (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = buttonOK .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                group .Font = dlg .Font;
                buttonOK .Font = dlg .Font;
                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------		Click_miDefaultView
        private void Click_miDefaultView (object sender, EventArgs e)
        {
            saveVersion = SaveVersion .Short;
            DialogResult = DialogResult .Retry;
            Close ();
        }

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

            RegistryKey regkey = null;
            try
            {
                regkey = Registry .CurrentUser .CreateSubKey (strRegKey);
                if (regkey != null)
                {
                    if (saveVersion == SaveVersion .Short)
                    {
                        regkey .SetValue (nameMain, new string [] { version .ToString (),              // 0

                                                                    nSectors .ToString (),              // 1
                                                                    ((int) clrStart .A) .ToString (),   // 2 
                                                                    ((int) clrStart .R) .ToString (),   // 3 
                                                                    ((int) clrStart .G) .ToString (),   // 4 
                                                                    ((int) clrStart .B) .ToString (),   // 5 
                                                                    ((int) clrEnd .A) .ToString (),     // 6 
                                                                    ((int) clrEnd .R) .ToString (),     // 7 
                                                                    ((int) clrEnd .G) .ToString (),     // 8 
                                                                    ((int) clrEnd .B) .ToString (),     // 9 
                                                                    comboTexts .SelectedIndex .ToString (),     // 10
                                                                  },
                                                    RegistryValueKind .MultiString);
                    }
                    else
                    {
                        Font fnt = buttonOK .Font;
                        regkey .SetValue (nameMain, new string [] { version .ToString (),              // 0

                                                                    ClientSize .Width .ToString (),             // 1
                                                                    ClientSize .Height .ToString (),            // 2
                                                                    buttonOK .Bounds .Left .ToString (),        // 3
                                                                    buttonOK .Bounds .Top .ToString (),         // 4
                                                                    buttonOK .Bounds .Width .ToString (),       // 5
                                                                    buttonOK .Bounds .Height .ToString (),      // 6
                                                                    fnt .Name .ToString (),             // 7
                                                                    fnt .SizeInPoints .ToString (),     // 8
                                                                    ((int) (fnt .Style)) .ToString (),  // 9
                                                                    nSectors .ToString (),                      // 10
                                                                    listValues .Columns [0] .Width .ToString (),    // 11
                                                                    ((int) clrStart .A) .ToString (),   // 12 
                                                                    ((int) clrStart .R) .ToString (),   // 13 
                                                                    ((int) clrStart .G) .ToString (),   // 14 
                                                                    ((int) clrStart .B) .ToString (),   // 15 
                                                                    ((int) clrEnd .A) .ToString (),     // 16 
                                                                    ((int) clrEnd .R) .ToString (),     // 17 
                                                                    ((int) clrEnd .G) .ToString (),     // 18 
                                                                    ((int) clrEnd .B) .ToString (),     // 19 
                                                                    comboTexts .SelectedIndex .ToString (),     // 20
                                                                  },
                                                    RegistryValueKind .MultiString);
                        group .IntoRegistry (regkey, "Group");
                        ring .IntoRegistry (regkey, "Ring");
                    }
                }
            }
            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 (nameMain);
                    if (strs == null || Convert .ToInt32 (strs [0]) != 606)
                    {
                        return;
                    }
                    if (strs .Length == 11)
                    {
                        nSectors = Convert .ToInt32 (strs [1]);
                        clrStart = Auxi_Convert .ToColor (strs, 2);
                        clrEnd = Auxi_Convert .ToColor (strs, 6);
                        comboTexts .SelectedIndex = Convert .ToInt32 (strs [10]);
                        return;
                    }
                    if (strs .Length == 21)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 1);
                        buttonOK .Font = Auxi_Convert .ToFont (strs, 7);
                        buttonOK .Bounds = Auxi_Convert .ToRectangle (strs, 3);
                        nSectors = Convert .ToInt32 (strs [10]);
                        listValues .Columns [0] .Width = Convert .ToInt32 (strs [11]);
                        clrStart = Auxi_Convert .ToColor (strs, 12);
                        clrEnd = Auxi_Convert .ToColor (strs, 16);
                        comboTexts .SelectedIndex = Convert .ToInt32 (strs [20]);
                        group = ElasticGroup .FromRegistry (this, regkey, "Group",
                                                            new Control [] { numericSectors, listValues, btnStartClr, btnEndClr, comboTexts });
                        ring = PrimitiveRing .FromRegistry (regkey, "Ring");
                        if (group == null || ring == null)
                        {
                            return;
                        }
                        fSum = Auxi_Common .SumArray (ring .Values, false);
                        ShowValues ();
                        bRestore = true;
                    }
                    else
                    {
                        return;
                    }
                }
            }
            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