Click here to Skip to main content
15,895,799 members
Articles / Desktop Programming / Windows Forms

Farsi Library - Working with Dates, Calendars, and DatePickers

Rate me:
Please Sign up or sign in to vote.
4.93/5 (236 votes)
21 Nov 2007MIT8 min read 2.9M   95.2K   202  
A library to work with "Persian Calendar", "Hijri Calendar", and "Gregorian Calendar" with WinForms GUI controls designed for Persian (Farsi) or Arabic language applications in mind, but usable in any Windows application that makes use of calendars and dates.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using FarsiLibrary.Win.FAPopup;
using FarsiLibrary.Win.Helpers;

namespace FarsiLibrary.Win.Controls
{
    /// <summary>
    /// FAMonthViewContainer is a control which hosts a <see cref="FAMonthView"/> control, and displays in <see cref="FADatePicker"/> control when user wants to select a date.
    /// </summary>
    [ToolboxItem(false)]
    public class FAMonthViewContainer : FAPopupContainer, IPopupControl
    {
        #region Fields

        private FAMonthView mv;
        private Control owner;
        private FAHookPopup hook;
        private IPopupServiceControl serviceObject;
        private static IPopupServiceControl popupServiceControl = new FAHookPopupController();

        #endregion

        #region Props

        /// <summary>
        /// Owner control of this Popup control.
        /// </summary>
        [Browsable(false)]
        public Control OwnerControl
        {
            get { return owner; }
            set { owner = value; }
        }

        /// <summary>
        /// Service object which handles popup behaviors.
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public IPopupServiceControl ServiceObject
        {
            get { return serviceObject; }
            set
            {
                if (value == null) return;
                serviceObject = value;
            }
        }

        /// <summary>
        /// Actual control that is being displayed.
        /// </summary>
        [Browsable(false)]
        public FAMonthView MonthViewControl
        {
            get { return mv; }
        }

        /// <summary>
        /// Editor which shows the popup control.
        /// </summary>
        public override Control OwnerEdit
        {
            get { return owner; }
        }


        #endregion

        #region Ctor

        /// <summary>
        /// Creates a new instance of FAMonthViewContainer class.
        /// </summary>
        public FAMonthViewContainer() : this(null)
        {
        }

        /// <summary>
        /// Creates a new instance of FAMonthViewContainer which hosts a <see cref="FAMonthView"/> control in popup mode.
        /// </summary>
        /// <param name="ownerControl"></param>
        public FAMonthViewContainer(Control ownerControl)
        {
            hook = new FAHookPopup(this);
            mv = new FAMonthView(true);
            mv.Dock = DockStyle.Fill;
            Size = new Size(mv.Size.Width - 2, mv.Size.Height - 2);
            Controls.Add(mv);
            mv.IsPopupMode = true;
            serviceObject = popupServiceControl;
            RealBounds = new Rectangle(mv.Bounds.X, mv.Bounds.Y, mv.Bounds.Width, mv.Bounds.Height);
            Parent = owner;
            FormBorderStyle = FormBorderStyle.FixedToolWindow;
            ControlBox = false;
            owner = ownerControl;
            SetStyle(ControlStyles.Opaque, true);
            ShadowSize = 3;
            RightToLeft = ownerControl.RightToLeft;
        }

        /// <summary>
        /// Disposes the control.
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            hook.Dispose();
            base.Dispose(disposing);
        }
        
        #endregion

        #region Methods

        protected override void OnRightToLeftChanged(EventArgs e)
        {
            base.OnRightToLeftChanged(e);
            UpdateShadows();
        }

        private void ShowCalendar()
        {
            Rectangle r = OwnerEdit.RectangleToScreen(OwnerEdit.ClientRectangle);
            Point showLocation;
            Point topLocation;

            if (OwnerEdit.RightToLeft == RightToLeft.Yes)
            {
                topLocation = new Point(r.Left, r.Bottom);
            }
            else
            {
                topLocation = new Point(r.Right - Width, r.Bottom);
            }

            Point bottomLocation = new Point(topLocation.X, topLocation.Y);
            showLocation = ControlUtils.CalcLocation(bottomLocation, topLocation, Size);

            ClientSize = Size;
            Location = showLocation;

            CalendarChanged(true);
            Visible = true;
        }

        public void ShowCalendar(Point position)
        {
            Point topLocation = position;
            Point bottomLocation = new Point(topLocation.X, topLocation.Y);
            Point newLoc = ControlUtils.CalcLocation(bottomLocation, topLocation, Size);

            ClientSize = Size;
            Location = newLoc;

            CalendarChanged(true);
            Visible = true;
        }

        private void HideCalendar()
        {
            Visible = false;

            Form form = OwnerEdit.FindForm();
            if (form != null && ActiveForm == form)
                form.Activate();
        }

        protected virtual void CalendarChanged(bool makeVisible)
        {
            if (!Visible && !makeVisible)
                return;

            Invalidate();
            if (makeVisible)
                Visible = true;
        }

        #endregion

        #region IPopupControl Members

        /// <summary>
        /// Closes the Popup window.
        /// </summary>
        public void ClosePopup()
        {
            HideCalendar();
        }

        /// <summary>
        /// Shows the Popup window.
        /// </summary>
        public void ShowPopup()
        {
            ShowCalendar();
        }

        /// <summary>
        /// Popup control that will be shown.
        /// </summary>
        public Control PopupWindow
        {
            get { return mv; }
        }

        /// <summary>
        /// Is mouse clicks on the control allowed?
        /// </summary>
        /// <param name="control"></param>
        /// <param name="mousePosition"></param>
        /// <returns></returns>
        public bool AllowMouseClick(Control control, Point mousePosition)
        {
            return false;
        }

        internal FAHookPopup PopupHook
        {
            get { return hook; }
        }

        #endregion
    }
}

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 MIT License


Written By
Software Developer (Senior) Readify
Australia Australia
Working on both Java and .NET Technology, I have developed various enterprise level applications on both platforms. Currently, I am working as a Senior Software Developer at Readify which is a leading company on .NET technology in Australia.

Comments and Discussions