Click here to Skip to main content
15,898,020 members
Articles / Mobile Apps

Windows Mobile Password Safe

Rate me:
Please Sign up or sign in to vote.
4.87/5 (58 votes)
12 Jan 2009CPOL16 min read 161.9K   3.1K   139  
A password safe with a touch screen UI introducing Fluid Controls.
using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;
using Fluid.Native;
using Fluid.Drawing.GdiPlus;
using System.Diagnostics;

namespace Fluid.Controls
{
    /// <summary>
    /// Default class for IControlContainer controls.
    /// </summary>
    public class ControlContainer : FluidControl, IMultiControlContainer, ICommandContainer
    {
        public ControlContainer()
            : base()
        { }

        public ControlContainer(int x, int y, int width, int height)
            : base(x, y, width, height)
        {
        }


        protected override void InitControl()
        {
            base.InitControl();
            Init();
        }

        private void Init()
        {
            controls = new FluidControlCollection(this);
            ForeColor = Color.Empty;
            Initialize();
        }

        protected virtual void Initialize()
        {
        }

        #region IControlContainer Members

        internal FluidControlCollection controls;

        protected virtual IEnumerable<FluidControl> GetControls()
        {
            return controls;
        }

        IEnumerable<FluidControl> IMultiControlContainer.Controls
        {
            get { return GetControls(); }
        }

        public virtual void Invalidate(Rectangle bounds)
        {
            if (!Visible) return;
            if (Initializing) return;
            if (Container == null) return;

            bounds.Offset(Left, Top);
            // don't let the child control invalidate anything outside the own bounds:
            ClipRectangle(ref bounds);
            Container.Invalidate(bounds);
        }

        public IHost Host
        {
            get { return Container != null ? this.Container.Host : null; }
        }

        public System.Drawing.Point PointToHost(int x, int y)
        {
            return Container.PointToHost(x + Left, y + Top);
        }


        #endregion

        private SizeF defaultSize = new SizeF(1f, 1f);
        protected override void OnSizeChanged(Size oldSize, Size newSize)
        {
            base.OnSizeChanged(oldSize, newSize);
        }


        public override void OnPaint(FluidPaintEventArgs e)
        {
            base.OnPaint(e);
            PaintControls(e);
        }

        private bool AreAllControOpaque(FluidControlCollection controls)
        {
            bool result = true;
            foreach (FluidControl c in controls)
            {
                if (!c.Visible) continue;
                Rectangle r = c.Bounds;
                if (c.Width <= 0 || c.Height <= 0) continue;
                if (c.IsTransparent)
                {
                    result = false;
                    break;
                }
            }
            return result;
        }


        /// <summary>
        /// Paints all  controls.
        /// The controls are painted in an optimized way:
        /// The controls are rendered from the last to the first, and after painting each control, the bounds of the control are excluded from the region, so this region is not painted twice.
        /// If a there is a transparent control, the remaining controls are painted from the first to this control, while the region is not excluded.
        /// </summary>
        /// <param name="pe">The PaintEventArgs.</param>
        protected virtual void PaintControls(FluidPaintEventArgs pe)
        {
#if true
            Region region = null;


            Region clip = pe.Region;
            Graphics g = pe.Graphics;
            int index = 0;


            for (int i = controls.Count - 1; i >= 0; i--)
            {
                FluidControl c = controls[i];
                if (c.Visible && c.IsTransparent)
                {
                    region = pe.Graphics.Clip;
                    index = i + 1;
                    break;
                }
                Rectangle bounds = PaintControl(pe, c);
                if (!bounds.IsEmpty)
                {
                    clip.Exclude(bounds);
                    g.Clip = clip;
                }
            }
            for (int i = 0; i < index; i++)
            {
                FluidControl c = controls[i];
                if (c.IsTransparent)
                {
                    pe.Graphics.Clip = region;
                }
                PaintControl(pe, c);
            }
            if (region != null) region.Dispose();

#else
            foreach (FluidControl c in controls)
            {
                PaintControl(pe, c);
            }
#endif
        }

        /// <summary>
        /// Paints the control. If the control was painted, the bounds of the rectangle in the canvas are returned, otherwise Rectangle.Empty is returned.
        /// </summary>
        /// <param name="pe">The PaintEventArgs</param>
        /// <param name="control">The control</param>
        /// <returns>If the control was painted, the bounds of the rectangle in the canvas are returned, otherwise Rectangle.Empty is returned.</returns>
        protected Rectangle PaintControl(FluidPaintEventArgs pe, FluidControl control)
        {
            if (!control.Visible) return Rectangle.Empty;
            Rectangle bounds = control.Bounds;
            if (bounds.Width <= 0 || bounds.Height <= 0) return Rectangle.Empty;

            Rectangle controlBounds = pe.ControlBounds;
            Region clip = pe.Region;
            bounds.Offset(controlBounds.X, controlBounds.Y);
            if (clip.IsVisible(bounds))
            {
                PaintControlUnbuffered(pe, control, bounds);
                return bounds;
            }
            return Rectangle.Empty;
        }

        private FluidPaintEventArgs paintEventArgs = new FluidPaintEventArgs();

        protected void PaintControlUnbuffered(FluidPaintEventArgs pe, FluidControl control, Rectangle bounds)
        {
            FluidPaintEventArgs e = paintEventArgs;
            e.Graphics = pe.Graphics;
            e.ControlBounds = bounds;
            e.Region = pe.Region;
            e.ScaleFactor = pe.ScaleFactor;
            control.OnPaint(e);
        }

        private FluidControl downControl = null;

        private PointEventArgs TranslatePoint(FluidControl c, PointEventArgs p)
        {
            p.X -= c.Bounds.X;
            p.Y -= c.Bounds.Y;
            return p;
        }

        public override void OnGesture(GestureEventArgs e)
        {
            if (!Enabled) return;
            if (downControl != null) downControl.OnGesture(e);

        }

        public override void OnMove(PointEventArgs p)
        {
            if (downControl != null) downControl.OnMove(this.TranslatePoint(downControl, p));
        }


        private FluidControl selectedControl;

        public override void OnDown(PointEventArgs p)
        {
            if (!Enabled) return;
            base.OnDown(p);
            FluidControl control = ControlFromPoint(p.X, p.Y);
            if (control != null && !control.Active) control = null;
            downControl = control;
            selectedControl = control;
            if (control != null)
            {
                PointEventArgs pointEventArgs = TranslatePoint(control, p);
                if (control.Active) control.OnDown(pointEventArgs);
            }
        }

        public override void PerformClick()
        {
            base.PerformClick();
            FluidControl control = selectedControl;
            if (control != null)
            {
                if (control.Selectable) Host.FocusedControl = control;
            }
        }

        public override void OnUp(PointEventArgs p)
        {
            base.OnUp(p);
            if (downControl != null) downControl.OnUp(TranslatePoint(downControl, p));
        }

        public override void OnRelease(PointEventArgs p)
        {
            base.OnRelease(p);
            if (downControl != null) downControl.OnRelease(TranslatePoint(downControl, p));
        }

        public override bool OnClick(PointEventArgs p)
        {
            if (!Enabled) return false;
            if (downControl != null) return downControl.OnClick(TranslatePoint(downControl, p));
            base.OnClick(p);
            return false;
        }

        Point p = new Point();

        /// <summary>
        /// Gets the first control that is under the specified point.
        /// </summary>
        /// <param name="x">The x value of the point.</param>
        /// <param name="y">The y value of the point.</param>
        /// <returns>An ITouchControl, otherwise null.</returns>
        /// <remarks>
        /// The enumeration starts from the last to the first control, because the controls appear in this
        /// order in case they are overlapping.
        /// </remarks>
        public virtual FluidControl ControlFromPoint(int x, int y)
        {
            p.X = x;
            p.Y = y;
            for (int i = controls.Count - 1; i >= 0; i--)
            {
                FluidControl c = controls[i];
                if (!c.Visible) continue;
                Rectangle r = c.Bounds;
                if (r.Contains(p)) return c;
            }
            return null;
        }

        Rectangle IContainer.GetScreenBounds(Rectangle bounds)
        {
            bounds.Offset(Left, Top);
            return Container.GetScreenBounds(bounds);
        }

        public override bool Active { get { return true; } }

        #region ICommandContainer Members


        public virtual void RaiseCommand(CommandEventArgs e)
        {
            IsDown = false;
            if (Command != null)
            {
                Command(this, e);
            }
            if (!e.Handled)
            {
                ICommandContainer container = this.Container as ICommandContainer;
                if (container != null)
                {
                    container.RaiseCommand(e);
                }
            }
        }

        public event EventHandler<CommandEventArgs> Command;

        #endregion

        /// <summary>
        /// Paints a background with a gradient.
        /// </summary>
        /// <param name="e"></param>
        /// <param name="alpha">the alpha value between white and the background color for the starting gradiant color.</param>
        /// <param name="gradientHeight">the height of the gradient until solid color.</param>
        public void PaintGradientBackground(FluidPaintEventArgs e, int alpha, int gradientHeight)
        {
            Graphics g = e.Graphics;
            Rectangle r = e.ControlBounds;
            Color color = BackColor;
            Color startColor = ColorConverter.AlphaBlendColor(Color.White, color, alpha);

            Rectangle gr = r;
            gr.Height = gradientHeight;
            GdiExt.GradientFill(g, gr, startColor, color, GdiExt.FillDirection.TopToBottom);
            if (gr.Height < r.Height)
            {
                gr.Y = r.Top + gradientHeight;
                gr.Height = r.Height - gradientHeight;
                using (SolidBrush brush = new SolidBrush(BackColor))
                {
                    g.FillRectangle(brush, gr);
                }
            }

            r.Width--;
            r.Height--;
            using (Pen pen = new Pen(color))
            {
                g.DrawRectangle(pen, r);
            }
        }

        /// <summary>
        /// Paints a background with a gradient.
        /// </summary>
        /// <param name="e"></param>
        public void PaintGradientBackground(FluidPaintEventArgs e)
        {
            PaintGradientBackground(e, 50, Height);
        }

        private SizeF scaleFactor = new SizeF(1f, 1f);

        public override void Scale(SizeF scaleFactor)
        {
            if (this.scaleFactor != scaleFactor)
            {
                this.scaleFactor = scaleFactor;
                base.Scale(scaleFactor);
            }
        }

    }
}

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
Software Developer (Senior)
Germany Germany
MCPD
Enterprise Application Developer 3.5
Windows Developer 3.5
.ASP.NET Developer 3.5
.NET 2.0 Windows Developer
.NET 2.0 Web Developer
.NET 2.0 Enterprise Application Developer


MCTS
.NET 3.5 Windows Forms Applications
.NET 3.5 ASP.NET Applications
.NET 3.5, ADO.NET Application Development
.NET 3.5 WCF
.NET 3.5 WPF
.NET 3.5 WF
Microsoft SQL Server 2008, Database Development
.NET 2.0 Windows Applications
.NET 2.0 Web Applications
.NET 2.0 Distributed Applications
SQL Server 2005
Sharepoint Services 3.0 Application Development
Windows Vista Client Configuration

Comments and Discussions