Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/GridMainDemo/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/WinForms/CS/GridMainDemo/Modules/OutlookStyle.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraBars.Alerter;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;

namespace DevExpress.XtraGrid.Demos {
	/// <summary>
	/// Summary description for OutlookStyle.
	/// </summary>
	public partial class OutlookStyle : TutorialControl {
        public OutlookStyle() {
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
            TutorialInfo.WhatsThisCodeFile = "CS\\GridMainDemo\\Modules\\OutlookStyle.cs";
            TutorialInfo.WhatsThisXMLFile = "DevExpress.XtraGrid.Demos.CodeInfo.OutlookStyle.xml";
			// TODO: Add any initialization after the InitForm call

            //<gridControl1>
            /*
            ~Note: the following properties are set at design time and listed here only for demonstration purposes.
            gridView1.OptionsView.GroupDrawMode = Views.Grid.GroupDrawMode.Office2003;
            gridView1.OptionsView.ShowGroupedColumns = true;
            gridView1.OptionsView.ShowGroupPanel = false;
            gridView1.OptionsView.ShowVerticalLines = Utils.DefaultBoolean.False;
            */
            //</gridControl1>
            
		}
        
        MailTimer mTimer;
		public override DevExpress.XtraGrid.Views.Base.BaseView ExportView { get { return gridView1; } }
		private void OutlookStyle_Load(object sender, System.EventArgs e) {
            gridControl1.DataSource = OutlookData.CreateDataTable();
			gridView1.SetRowExpanded(-1, true);
			gridView1.SetRowExpanded(-2, true);
            mTimer = new MailTimer(gridControl1.DataSource as DataTable, alertControl1, this.FindForm());
            OnStyleChanged();
            SetAlertControlButtonHint();
		}

        void SetAlertControlButtonHint() {
            alertControl1.Buttons[0].Hint = Properties.Resources.FlagItem;
            alertControl1.Buttons[1].Hint = Properties.Resources.MarkAsRead;
            alertControl1.Buttons[2].Hint = Properties.Resources.OpenAttachment;
            alertControl1.Buttons[3].Hint = Properties.Resources.DeleteItem;
        }
        private void gridView1_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e) {
			GridGroupRowInfo info = e.Info as GridGroupRowInfo;
			if(info == null) return;
			info.GroupText = info.GroupText.Replace("1 " + Properties.Resources.Items, "1 " + Properties.Resources.Item);
		}

        protected override void DoShow() {
            base.DoShow();
            gridControl1.Focus();
            mTimer.Start();
        }
        protected override void DoHide() {
            mTimer.Stop();
        }

        #region Alerter
        private void alertControl1_ButtonDownChanged(object sender, AlertButtonDownChangedEventArgs e) {
            if(e.ButtonName == "Flag") 
                ((MailData)e.Info.Tag).Flag = e.Down ? 0 : 1;
            if(e.ButtonName == "Read") 
                ((MailData)e.Info.Tag).Read = e.Down ? 1 : 0;
        }

        private void alertControl1_ButtonClick(object sender, AlertButtonClickEventArgs e) {
            MailData data = e.Info.Tag as MailData;
            e.AlertForm.OwnerForm.Activate();
            if(e.ButtonName == "Attachment") {
                e.AlertForm.Close();
                XtraMessageBox.Show(FindForm(), "Open attachment dialog.", string.Format("Mail From: {0}", data.From));
            }
            if(e.ButtonName == "Delete") 
                DeleteItem(e, data);
        }
        void OpenItem(MailData data) {
            for(int i = 0; i < gridView1.RowCount; i++) {
                if(data.Row.Equals(gridView1.GetDataRow(i))) {
                    gridView1.FocusedRowHandle = i;
                    break;
                }
            }
        }
        void DeleteItem(AlertClickEventArgs args, MailData data) {
            args.AlertForm.Close();
            try {
                DataTable tbl = gridControl1.DataSource as DataTable;
                tbl.Rows.Remove(data.Row);
                gridView1.LayoutChanged();
            }
            catch { }
        }
        private void alertControl1_AlertClick(object sender, AlertClickEventArgs e) {
            MailData data = e.Info.Tag as MailData;
            OpenItem(data);
        }
        #endregion
        #region AlertPopupMenu
        bool updatePopupMenu = false;
        private void popupMenu1_BeforePopup(object sender, CancelEventArgs e) {
            AlertClickEventArgs args = popupMenu1.ItemLinks[0].Item.Tag as AlertClickEventArgs;
            if(args == null) return;
            MailData data = args.Info.Tag as MailData;
            updatePopupMenu = true;
            bcFlag.Checked = 0.Equals(data.Flag);
            if(1.Equals(data.Read)) {
                biRead.Caption = Properties.Resources.UnreadMark;
                biRead.ImageIndex = 2;
            }
            else {
                biRead.Caption = Properties.Resources.ReadMark;
                biRead.ImageIndex = 1;
            }
            if(data.Priority == 0) bcLow.Checked = true;
            if(data.Priority == 1) bcMedium.Checked = true;
            if(data.Priority == 2) bcHigh.Checked = true; 
            updatePopupMenu = false;
        }
        private void biOpen_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            AlertClickEventArgs args = e.Item.Tag as AlertClickEventArgs;
            OpenItem(args.Info.Tag as MailData);
        }
        private void bcFlag_DownChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            if(updatePopupMenu) return;
            AlertClickEventArgs args = e.Item.Tag as AlertClickEventArgs;
            MailData data = args.Info.Tag as MailData;
            data.Flag = ((BarCheckItem)e.Item).Checked ? 0 : 1;
            args.AlertForm.Buttons["Flag"].Down = data.Flag == 0;
        }
        private void biRead_ItemClick(object sender, ItemClickEventArgs e) {
            AlertClickEventArgs args = e.Item.Tag as AlertClickEventArgs;
            MailData data = args.Info.Tag as MailData;
            data.Read = data.Read == 1 ? 0 : 1;
            args.AlertForm.Buttons["Read"].Down = data.Read == 1;
        }
        private void biDelete_ItemClick(object sender, ItemClickEventArgs e) {
            AlertClickEventArgs args = e.Item.Tag as AlertClickEventArgs;
            DeleteItem(args, args.Info.Tag as MailData);
        }
        private void bc_DownChanged(object sender, ItemClickEventArgs e) {
            if(updatePopupMenu) return;
            AlertClickEventArgs args = e.Item.Tag as AlertClickEventArgs;
            MailData data = args.Info.Tag as MailData;
            BarCheckItem item = e.Item as BarCheckItem;
            if(!item.Checked) return;
            if(bcLow.Checked) data.Priority = 0;
            if(bcMedium.Checked) data.Priority = 1;
            if(bcHigh.Checked) data.Priority = 2;
        }
        private void barManager1_ItemClick(object sender, ItemClickEventArgs e) {
            AlertClickEventArgs args = e.Item.Tag as AlertClickEventArgs;
            if(args == null) return;
            MailData data = args.Info.Tag as MailData;
            args.AlertForm.OwnerForm.Activate();
        }
        #endregion

        private void gridControl1_MouseDoubleClick(object sender, MouseEventArgs e) {
            GridHitInfo hi = gridView1.CalcHitInfo(new Point(e.X, e.Y));
            if(hi.InRow && gridView1.FocusedRowHandle > -1) {
                mTimer.ShowAlert(new MailData(gridView1.GetDataRow(gridView1.FocusedRowHandle)));
            }
        }

        private void alertControl1_BeforeFormShow(object sender, AlertFormEventArgs e) {
            //((MailData)e.AlertForm.Info.Tag).Form.Activate();
        }

        protected override void OnStyleChanged() {
            base.OnStyleChanged();
            ColorHelper.UpdateColor(imageList1, gridControl1.LookAndFeel);
        }
    }
}