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 CPOL
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
{
    enum SaveVersion { Short, Full };

    public partial class Form_DefineNewBarChart : Form
    {
        const string strAddRegKey = "Form_DefineNewBarChart";
        Point ptMouseInit;
        Mover mover;
        ElasticGroup group;
        Point ptMouse_Down, ptMouse_Up;
        int nSet, nSegment;
        Color clrStart, clrEnd;
        PrimitiveBarChart chart;
        Random rand;
        int nSeed;

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

        // -------------------------------------------------
        public Form_DefineNewBarChart (Point ptMouseScreen)
        {
            InitializeComponent ();
            ptMouseInit = ptMouseScreen;
            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);
            rand = Auxi_Common .RandomByCurTime (out nSeed);
            nSet = 3;
            nSegment = 5;
            int iStart = rand .Next (nSeed) % 7;
            int iEnd = rand .Next (nSeed) % 7;
            if (iStart == iEnd) iEnd++;
            clrStart = Auxi_Colours .ColorPredefined (iStart);
            clrEnd = Auxi_Colours .ColorPredefined (iEnd);
            comboTexts .SelectedIndex = 1;

            RestoreFromRegistry ();
            if (!bRestore)
            {
                group = new ElasticGroup (this, new CommentedControl [] {
                                                            new CommentedControl (this, numericSets, Resizing .WE, Side .E, "Sets"), 
                                                            new CommentedControl (this, numericSegments, Resizing .WE, Side .E, "Segments"), 
                                                            new CommentedControl (this, btnStartClr, Side .E, "Start color"), 
                                                            new CommentedControl (this, btnEndClr, Side .E, "End color"), 
                                                            new CommentedControl (this, comboTexts, Side .N, SideAlignment .Left, "Texts") 
                                                                        });
                int cyT = group .RectAround .Bottom + 12; 
                Rectangle rc = new Rectangle (group .RectAround .Left, cyT, group .RectAround .Width, buttonOK .Top - 12 - cyT);

                chart = new PrimitiveBarChart (rc, nSet, nSegment, null, Auxi_Colours .SmoothColorsList (nSet, clrStart, clrEnd)); 
            }
            numericSets .Value = nSet;
            numericSegments .Value = nSegment;

            RenewMover ();

            toolTip1 .SetToolTip (btnStartClr, Auxi_Convert .ClrToStr (clrStart, ColorStr .IN_ROWS));
            toolTip1 .SetToolTip (btnEndClr, Auxi_Convert .ClrToStr (clrEnd, ColorStr .IN_ROWS));
            bAfterInit = true;
        }
        // -------------------------------------------------        PrepareTexts
        public string [] PrepareTexts ()
        {
            string [] strs = new string [nSegment];
            string [] strsSrc;
            switch (comboTexts .SelectedIndex)
            {
                case 0:
                default:
                    strsSrc = Auxi_Common .strAlphabet;
                    break;

                case 1:
                    strsSrc = Auxi_Common .strDays;
                    break;

                case 2:
                    strsSrc = Auxi_Common .strMonths;
                    break;

                case 3:
                    strsSrc = Auxi_Common .strNames;
                    break;
            }
            for (int i = 0; i < nSegment; i++)
            {
                strs [i] = Auxi_Common .StringOfText (strsSrc, i);
            }
            return (strs);
        }
        // -------------------------------------------------        RenewMover
        private void RenewMover ()
        {
            mover .Clear ();
            chart .IntoMover (mover, 0);
            group .IntoMover (mover, 0);
            mover .Insert (0, btnChange);
            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;

            chart .Draw (grfx);
            group .Draw (grfx);
        }
        // -------------------------------------------------        OnMouseDown
        private void OnMouseDown (object sender, MouseEventArgs e)
        {
            ptMouse_Down = e .Location;
            mover .Catch (e .Location, e .Button, bShowAngle);
            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, iWasNode;
            if (mover .Release (out iWasCaught, out iWasNode))
            {
                GraphicalObject grobj = mover [iWasCaught] .Source;
                if (e .Button == MouseButtons .Right)
                {
                    if (nDist <= 3)
                    {
                        if (grobj is ElasticGroup)
                        {
                            ContextMenuStrip = menuOnGroup;
                        }
                        else if (grobj is PrimitiveBarChart || grobj is StripInRectangle)
                        {
                            ContextMenuStrip = menuOutsideGroup;
                        }
                    }
                }
            }
            else
            {
                if (e .Button == MouseButtons .Right && nDist <= 3)
                {
                    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 ();
                }
                Invalidate ();
            }
        }
        // -------------------------------------------------        OnContextMenuChanged
        private void OnContextMenuChanged (object sender, EventArgs e)
        {
            if (ContextMenuStrip != null)
            {
                ContextMenuStrip .Show (PointToScreen (ptMouse_Up));
            }
        }
        // -------------------------------------------------        ValueChanged_numericSets
        private void ValueChanged_numericSets (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                nSet = Convert .ToInt32 (numericSets .Value);
                chart = new PrimitiveBarChart (chart .RectAround, nSet, nSegment, null, Auxi_Colours .SmoothColorsList (nSet, clrStart, clrEnd)); 
                RenewMover ();
                Invalidate ();
            }
        }
        // -------------------------------------------------        ValueChanged_numericSegments
        private void ValueChanged_numericSegments (object sender, EventArgs e)
        {
            if (bAfterInit)
            {
                nSegment = Convert .ToInt32 (numericSegments .Value);
                chart = new PrimitiveBarChart (chart .RectAround, nSet, nSegment, null, Auxi_Colours .SmoothColorsList (nSet, clrStart, clrEnd));
                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;
                chart .Colors = Auxi_Colours .SmoothColorsList (nSet, 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;
                chart .Colors = Auxi_Colours .SmoothColorsList (nSet, clrStart, clrEnd);
                toolTip1 .SetToolTip (btnStartClr, Auxi_Convert .ClrToStr (clrStart, ColorStr .IN_ROWS));
                Invalidate ();
            }
        }
        // -------------------------------------------------        Click_btnChange
        private void Click_btnChange (object sender, EventArgs e)
        {
            chart = new PrimitiveBarChart (chart .RectAround, nSet, nSegment, null, Auxi_Colours .SmoothColorsList (nSet, clrStart, clrEnd));
            RenewMover ();
            Invalidate ();
        }
        // -------------------------------------------------        Chart
        public PrimitiveBarChart Chart
        {
            get { return (chart); }
        }
        // -------------------------------------------------        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;
        }
        // -------------------------------------------------        Opening_menuOutsideGroup
        private void Opening_menuOutsideGroup (object sender, CancelEventArgs e)
        {
            ((ToolStripMenuItem) (menuOutsideGroup .Items ["miRotationAngle"])) .Checked = bShowAngle;
        }
        // -------------------------------------------------        Click_miFont
        private void Click_miFont (object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog ();

            dlg .Font = btnChange .Font;
            if (dlg .ShowDialog () == DialogResult .OK)
            {
                group .Font = dlg .Font;
                btnChange .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 nameSize = "Size";
        // -------------------------------------------------        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 (nameSize, new string [] { nSet .ToString (),                  // 0
                                                                    nSegment .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 = btnChange .Font;
                        regkey .SetValue (nameSize, new string [] { ClientSize .Width .ToString (),         // 0
                                                                    ClientSize .Height .ToString (),            // 1
                                                                    btnChange .Bounds .Left .ToString (),       // 2
                                                                    btnChange .Bounds .Top .ToString (),        // 3
                                                                    btnChange .Bounds .Width .ToString (),      // 4
                                                                    btnChange .Bounds .Height .ToString (),     // 5
                                                                    buttonOK .Bounds .Left .ToString (),        // 6
                                                                    buttonOK .Bounds .Top .ToString (),         // 7
                                                                    buttonOK .Bounds .Width .ToString (),       // 8
                                                                    buttonOK .Bounds .Height .ToString (),      // 9
                                                                    fnt .Name .ToString (),             // 10
                                                                    fnt .SizeInPoints .ToString (),     // 11
                                                                    ((int) (fnt .Style)) .ToString (),  // 12
                                                                    nSet .ToString (),              // 13
                                                                    nSegment .ToString (),          // 14
                                                                    ((int) clrStart .A) .ToString (),   // 15 
                                                                    ((int) clrStart .R) .ToString (),   // 16 
                                                                    ((int) clrStart .G) .ToString (),   // 17 
                                                                    ((int) clrStart .B) .ToString (),   // 18 
                                                                    ((int) clrEnd .A) .ToString (),     // 19 
                                                                    ((int) clrEnd .R) .ToString (),     // 20 
                                                                    ((int) clrEnd .G) .ToString (),     // 21 
                                                                    ((int) clrEnd .B) .ToString (),     // 22 
                                                                    comboTexts .SelectedIndex .ToString (),     // 23
                                                                  },
                                                    RegistryValueKind .MultiString);
                        group .IntoRegistry (regkey, "Group");
                        chart .IntoRegistry (regkey, "Chart");
                    }
                }
            }
            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)
                {
                    Font fnt;
                    string [] strs = (string []) regkey .GetValue (nameSize);
                    if (strs == null) {
                        return;
                    }
                    if (strs .Length == 11)
                    {
                        nSet = Convert .ToInt32 (strs [0]);
                        nSegment = 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 == 24)
                    {
                        ClientSize = Auxi_Convert .ToSize (strs, 0);
                        fnt = Auxi_Convert .ToFont (strs, 10);
                        btnChange .Font = fnt;
                        buttonOK .Font = fnt;
                        btnChange .Bounds = Auxi_Convert .ToRectangle (strs, 2);
                        buttonOK .Bounds = Auxi_Convert .ToRectangle (strs, 6);
                        nSet = Convert .ToInt32 (strs [13]);
                        nSegment = Convert .ToInt32 (strs [14]);
                        clrStart = Auxi_Convert .ToColor (strs, 15);
                        clrEnd = Auxi_Convert .ToColor (strs, 19);
                        comboTexts .SelectedIndex = Convert .ToInt32 (strs [23]);
                        group = ElasticGroup .FromRegistry (this, regkey, "Group",
                                                            new Control [] { numericSets, numericSegments, btnStartClr, btnEndClr, comboTexts });
                        chart = PrimitiveBarChart .FromRegistry (regkey, "Chart", true);
                        if (group == null || chart == null)
                        {
                            return;
                        }
                        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)

Share

About the Author

SergeyAndreyev

United States United States
No Biography provided

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