Click here to Skip to main content
15,895,746 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 33.1K   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 .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace UserDrivenApplications
{
    public class PrimitiveBarChart : GraphicalObject
    {
        int version = 606;
        RectArea rarea;
        int nSets;                      // each set has its own colour
        int nSegments;                  // each segment includes a strip of every colour
        double [] vals;                 // all values are from [0, 1]
        List<StripInRectangle> columns = new List<StripInRectangle> ();
        List<Color> clrs = new List<Color> ();          // one color per each set

        double fEmptyStartPart = 0.0;   // a part of the segment
        double fColoredPart = 0.8;      // a part of the segment

        int minSets = 1;
        int minSegments = 2;

        Random rand;
        int nSeed;

        // -------------------------------------------------
        // emptyLeftPartOfSegment >= 0
        // coloredPartOfSegment > 0
        // emptyLeftPartOfSegment + coloredPartOfSegment <= 1
        //
        public PrimitiveBarChart (Rectangle rc, int sets, int segments, double [] nums, List<Color> colors)
        {
            rarea = new RectArea (rc);
            nSets = Math .Max (minSets, sets);
            nSegments = Math .Max (minSegments, segments);

            vals = new double [nSets * nSegments];
            rand = Auxi_Common .RandomByCurTime (out nSeed);
            if (nums == null || nums .Length != nSets * nSegments)
            {
                for (int i = 0; i < vals .Length; i++)
                {
                    vals [i] = rand .NextDouble ();
                }
            }
            else
            {
                for (int i = 0; i < vals .Length; i++)
                {
                    vals [i] = Math .Min (Math .Max (0, nums [i]), 1);
                }
            }
            if (colors == null || colors .Count != nSets)
            {
                for (int i = 0; i < nSets; i++)
                {
                    clrs .Add (Auxi_Colours .ColorPredefined (i));
                }
            }
            else
            {
                clrs = colors;
            }

            double segmentWidth = rarea .Area .Width / (double) nSegments;
            double stripWidth = segmentWidth * fColoredPart / nSets;
            double leftSpaceInSegment = segmentWidth * fEmptyStartPart;
            double cxL_colored, cxL_strip;
            int cyB = rarea .Area .Bottom;
            int cxL = rarea .Area .Left;
            double nW = rarea .Area .Width;
            float stripXCoef = Convert .ToSingle (stripWidth / nW);
            int k;
            for (int iSegment = 0; iSegment < nSegments; iSegment++)
            {
                cxL_colored = rarea .Area .Left + iSegment * segmentWidth + leftSpaceInSegment;
                for (int jStrip = 0; jStrip < nSets; jStrip++)
                {
                    k = iSegment * nSets + jStrip;
                    cxL_strip = cxL_colored + jStrip * stripWidth;
                    columns .Add (new StripInRectangle (rarea .Area, Convert .ToSingle ((cxL_strip - cxL) / nW), 1,
                                                        LineDir .Hor, stripXCoef, Convert .ToSingle (-vals [k]),
                                                        -1, 0, clrs [jStrip]));
                }
            }
        }
        // -------------------------------------------------
        public override sealed bool ShowCover
        {
            get { return (false); }
        }
        // -------------------------------------------------        RectAround
        new public Rectangle RectAround
        {
            get { return (rarea .Area); }
        }
        // -------------------------------------------------        Columns
        public List<StripInRectangle> Columns
        {
            get { return (columns); }
        }
        // -------------------------------------------------        Values
        public double [] Values
        {
            get { return (vals); }
        }
        // -------------------------------------------------        TwoDimensionalValues
        // this is correct only in the case, which works now
        public double [,] TwoDimensionalValues
        {
            get
            {
                double [,] nums = new double [nSets, nSegments];
                for (int k = 0; k < vals .Length; k++)
                {
                    vals [k] = -columns [k] .Ycoef;
                    nums [k % nSets, k / nSets] = vals [k];
                }
                return (nums);
            }
        }
        // -------------------------------------------------        Colors
        public List<Color> Colors
        {
            get { return (clrs); }
            set
            {
                if (clrs != null && clrs .Count == value .Count)
                {
                    clrs = value;
                    for (int i = 0; i < columns .Count; i++)
                    {
                        columns [i] .Color = clrs [i % nSets];
                    }
                }
            }
        }
        // -------------------------------------------------        EmptyStartPart
        public double EmptyStartPart
        {
            get { return (fEmptyStartPart); }
        }
        // -------------------------------------------------        ColoredPart
        public double ColoredPart
        {
            get { return (fColoredPart); }
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx)
        {
            int segmentWidth = rarea .Area .Width / nSegments;
            int [] cxSegmentBorders = new int [nSegments + 1];
            for (int i = 0; i < cxSegmentBorders .Length; i++)
            {
                cxSegmentBorders [i] = rarea .Area .Left + i * segmentWidth;
            }
            int yParts = 5;
            int partHeight = rarea .Area .Height / yParts;
            int [] cyLines = new int [yParts + 1];
            for (int i = 0; i < cyLines .Length; i++)
            {
                cyLines [i] = rarea .Area .Bottom - i * partHeight;
            }
            rarea .Draw (grfx, cxSegmentBorders, cyLines);
            for (int i = 0; i < columns .Count; i++)
            {
                columns [i] .Draw (grfx);
            }
        }
        // -------------------------------------------------        DefineCover
        public override void DefineCover ()
        {
            cover = new Cover (rarea .Area, Resizing .Any);
        }
        // -------------------------------------------------        Move
        public override void Move (int dx, int dy)
        {
            rarea .Move (dx, dy);
            InformRelatedElements ();
        }
        // -------------------------------------------------        MoveNode
        public override bool MoveNode (int i, int dx, int dy, Point ptM, MouseButtons catcher)
        {
            bool bRet = false;
            if (catcher == MouseButtons .Left)
            {
                if (i == 8)
                {
                    Move (dx, dy);
                }
                else
                {
                    if (i == 0)        //LT corner
                    {
                        if (rarea .Area .Height - dy >= RectArea .MinSize)
                        {
                            MoveBorder_Top (dy);
                            bRet = true;
                        }
                        if (rarea .Area .Width - dx >= RectArea .MinSize)
                        {
                            MoveBorder_Left (dx);
                            bRet = true;
                        }
                    }
                    else if (i == 1)        // RT corner
                    {
                        if (rarea .Area .Height - dy >= RectArea .MinSize)
                        {
                            MoveBorder_Top (dy);
                            bRet = true;
                        }
                        if (rarea .Area .Width + dx >= RectArea .MinSize)
                        {
                            MoveBorder_Right (dx);
                            bRet = true;
                        }
                    }
                    else if (i == 2)        // RB corner
                    {
                        if (rarea .Area .Width + dx >= RectArea .MinSize)
                        {
                            MoveBorder_Right (dx);
                            bRet = true;
                        }
                        if (rarea .Area .Height + dy >= RectArea .MinSize)
                        {
                            MoveBorder_Bottom (dy);
                            bRet = true;
                        }
                    }
                    else if (i == 3)        // LB corner
                    {
                        if (rarea .Area .Height + dy >= RectArea .MinSize)
                        {
                            MoveBorder_Bottom (dy);
                            bRet = true;
                        }
                        if (rarea .Area .Width - dx >= RectArea .MinSize)
                        {
                            MoveBorder_Left (dx);
                            bRet = true;
                        }
                    }
                    else if (i == 4)     // on left side
                    {
                        if (rarea .Area .Width - dx >= RectArea .MinSize)
                        {
                            MoveBorder_Left (dx);
                            bRet = true;
                        }
                    }
                    else if (i == 5)   // on right side
                    {
                        if (rarea .Area .Width + dx >= RectArea .MinSize)
                        {
                            MoveBorder_Right (dx);
                            bRet = true;
                        }
                    }
                    else if (i == 6)      // on top
                    {
                        if (rarea .Area .Height - dy >= RectArea .MinSize)
                        {
                            MoveBorder_Top (dy);
                            bRet = true;
                        }
                    }
                    else if (i == 7)   // on bottom
                    {
                        if (rarea .Area .Height + dy >= RectArea .MinSize)
                        {
                            MoveBorder_Bottom (dy);
                            bRet = true;
                        }
                    }
                    if (bRet)
                    {
                        InformRelatedElements ();
                    }
                }
            }
            return (bRet);
        }
        // -------------------------------------------------        MoveBorder_Top
        private void MoveBorder_Top (int cy)
        {
            Rectangle rc = rarea .Area;

            rc .Y += cy;
            rc .Height -= cy;
            rarea .Area = rc;
        }
        // -------------------------------------------------        MoveBorder_Bottom
        private void MoveBorder_Bottom (int cy)
        {
            Rectangle rc = rarea .Area;

            rc .Height += cy;
            rarea .Area = rc;
        }
        // -------------------------------------------------        MoveBorder_Left
        private void MoveBorder_Left (int cx)
        {
            Rectangle rc = rarea .Area;

            rc .X += cx;
            rc .Width -= cx;
            rarea .Area = rc;
        }
        // -------------------------------------------------        MoveBorder_Right
        private void MoveBorder_Right (int cx)
        {
            Rectangle rc = rarea .Area;

            rc .Width += cx;
            rarea .Area = rc;
        }
        // -------------------------------------------------        InformRelatedElements
        private void InformRelatedElements ()
        {
            foreach (StripInRectangle strip in columns)
            {
                strip .ParentRect = rarea .Area;
            }
        }
        // -------------------------------------------------        IntoMover
        new public void IntoMover (Mover mover, int iPos)
        {
            if (iPos < 0 || mover .Count < iPos)
            {
                return;
            }
            mover .Insert (iPos, this);
            foreach (StripInRectangle strip in columns)
            {
                mover .Insert (iPos, strip);
            }
        }
        // -------------------------------------------------        PositionsInMover
        new public int PositionsInMover
        {
            get
            {
                return (1 + columns .Count);
            }
        }

        const string nameMain = "PBC_";
        const string nameValues = "PBCValues_";
        const string nameClrs = "PBCClrs_";
        // -------------------------------------------------        IntoRegistry
        public void IntoRegistry (RegistryKey regkey, string strB)
        {
            try
            {
                regkey .SetValue (nameMain + strB, new string [] {version .ToString (),             // 0
                                                                  id .ToString (),                  // 1
                                                                  nSets .ToString (),               // 2
                                                                  nSegments .ToString (),           // 3
                                                                  fEmptyStartPart .ToString (),     // 4
                                                                  fColoredPart .ToString (),        // 5
                                                                },
                                                   RegistryValueKind .MultiString);
                rarea .IntoRegistry (regkey, "PBCArea_" + strB);

                string [] strVal = new string [vals .Length];
                for (int i = 0; i < vals .Length; i++)
                {
                    strVal [i] = vals [i] .ToString ();
                }
                regkey .SetValue (nameValues + strB, strVal, RegistryValueKind .MultiString);

                string [] strClrs = new string [clrs .Count * 4];
                for (int i = 0; i < clrs .Count; i++)
                {
                    strClrs [i * 4] = ((int) (clrs [i] .A)) .ToString ();
                    strClrs [i * 4 + 1] = ((int) (clrs [i] .R)) .ToString ();
                    strClrs [i * 4 + 2] = ((int) (clrs [i] .G)) .ToString ();
                    strClrs [i * 4 + 3] = ((int) (clrs [i] .B)) .ToString ();
                }
                regkey .SetValue (nameClrs + strB, strClrs, RegistryValueKind .MultiString);
            }
            catch
            {
            }
            finally
            {
            }
        }
        // -------------------------------------------------        FromRegistry
        public static PrimitiveBarChart FromRegistry (RegistryKey regkey, string strB, bool bNewValues)
        {
            try
            {
                string [] strs = (string []) regkey .GetValue (nameMain + strB);
                if (strs == null ||
                    strs .Length != 6 ||
                    Convert .ToInt32 (strs [0]) != 606)
                {
                    return (null);
                }
                RectArea ra = RectArea .FromRegistry (regkey, "PBCArea_" + strB);
                if (ra == null)
                {
                    return (null);
                }
                int sets = Convert .ToInt32 (strs [2]);
                int segments = Convert .ToInt32 (strs [3]);
                double emptyLeftPartOfSegment = Convert .ToDouble (strs [4]);
                double coloredPartOfSegment = Convert .ToDouble (strs [5]);

                double [] nums = null;
                if (!bNewValues)
                {
                    string [] strVal = (string []) regkey .GetValue (nameValues + strB);
                    if (strVal != null || strVal .Length == sets * segments)
                    {
                        nums = new double [strVal .Length];
                        for (int i = 0; i < strVal .Length; i++)
                        {
                            nums [i] = Convert .ToDouble (strVal [i]);
                        }
                    }
                }
                List<Color> colors = new List<Color> ();
                string [] strClrs = (string []) regkey .GetValue (nameClrs + strB);
                if (strClrs != null && strClrs .Length == sets * 4)
                {
                    for (int i = 0; i < sets; i++)
                    {
                        Color clrRead = Auxi_Convert .ToColor (strClrs, i * 4);
                        if (clrRead != null)
                        {
                            colors .Add (clrRead);
                        }
                    }
                }
                PrimitiveBarChart chart = new PrimitiveBarChart (ra .Area, sets, segments, nums, colors); 
                if (chart != null)
                {
                    chart .ID = Convert .ToInt64 (strs [1]);
                }
                return (chart);
            }
            catch
            {
                return (null);
            }
            finally
            {
            }
        }

    }
}

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