protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            try
            {
                StringFormat sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                sf.Trimming      = StringTrimming.EllipsisCharacter;


                // Paint the Background and Header
                if (this.DateMode == DateMode.SevenDay)
                {
                    // Background
                    graphics.FillRectangle(new SolidBrush(Calendar.ct.DayBackColor), cellBounds);

                    // Header
                    if (this.StartTime.Year == DateTime.Now.Year && this.StartTime.Month == DateTime.Now.Month && this.StartTime.Day == DateTime.Now.Day)
                    {
                        // For Today we draw orange gradient
                        LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(cellBounds.Left, cellBounds.Top, cellBounds.Width, headerHeight), Calendar.ct.HeaderLight, Calendar.ct.HeaderDark, LinearGradientMode.Vertical);
                        graphics.FillRectangle(lgb, cellBounds.Left, cellBounds.Top, cellBounds.Width, headerHeight);
                    }
                    else
                    {
                        graphics.FillRectangle(new SolidBrush(Calendar.ct.BackColor), cellBounds.Left, cellBounds.Top, cellBounds.Width, headerHeight);
                    }
                    graphics.DrawLine(new Pen(new SolidBrush(Calendar.ct.SeparatorDark)), cellBounds.Left, cellBounds.Top + headerHeight, cellBounds.Right, cellBounds.Top + headerHeight);
                    RectangleF r = new RectangleF(cellBounds.Left + 1, cellBounds.Top, cellBounds.Width - 2, headerHeight);
                    if (this.Selected && !bottomSelected)
                    {
                        graphics.FillRectangle(new SolidBrush(Calendar.ct.SelectionColor), cellBounds.Left, cellBounds.Top, cellBounds.Width, headerHeight);
                        sf.Alignment = StringAlignment.Far;
                        graphics.DrawString(this.StartTime.ToLongDateString(), Calendar.ct.FontStd, new SolidBrush(Calendar.ct.SelectedForeColor), r, sf);
                    }
                    else
                    {
                        sf.Alignment = StringAlignment.Far;
                        if (this.StartTime.Year == DateTime.Now.Year && this.StartTime.Month == DateTime.Now.Month && this.StartTime.Day == DateTime.Now.Day)
                        {
                            graphics.DrawString(this.StartTime.ToLongDateString(), Calendar.ct.FontStd, new SolidBrush(Calendar.ct.SelectedForeColor), r, sf);
                        }
                        else
                        {
                            graphics.DrawString(this.StartTime.ToLongDateString(), Calendar.ct.FontStd, Calendar.ct.ForeBrush, r, sf);
                        }
                    }

                    if (this.RowIndex == 2 && this.ColumnIndex == 1) // If Sat/Sun cell drawing the bottom half
                    {
                        // Draw another cell
                        // Draw the middle line through the cell
                        graphics.DrawLine(new Pen(Calendar.ct.BorderBrush), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2) - 1, cellBounds.Right, cellBounds.Top + (int)(cellBounds.Height / 2) - 1);

                        // Header
                        if (this.StartTime.Year == DateTime.Now.Year && this.StartTime.Month == DateTime.Now.Month && this.StartTime.Day == DateTime.Now.Day)
                        {
                            // For Today we draw orange gradient
                            LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width, headerHeight), Calendar.ct.HeaderLight, Calendar.ct.HeaderDark, LinearGradientMode.Vertical);
                            graphics.FillRectangle(lgb, cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width, headerHeight);
                        }
                        else
                        {
                            graphics.FillRectangle(new SolidBrush(Calendar.ct.BackColor), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width, headerHeight);
                        }
                        graphics.DrawLine(new Pen(new SolidBrush(Calendar.ct.SeparatorDark)), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2) + headerHeight, cellBounds.Right, cellBounds.Top + (int)(cellBounds.Height / 2) + headerHeight);
                        r = new RectangleF(cellBounds.Left + 1, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width - 2, headerHeight);
                        if (this.Selected && bottomSelected)
                        {
                            graphics.FillRectangle(new SolidBrush(Calendar.ct.SelectionColor), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width, headerHeight);
                            sf.Alignment = StringAlignment.Far;
                            graphics.DrawString(this.StartTime.AddDays(1).ToLongDateString(), Calendar.ct.FontStd, new SolidBrush(Calendar.ct.SelectedForeColor), r, sf);
                        }
                        else
                        {
                            sf.Alignment = StringAlignment.Far;
                            if (this.StartTime.Year == DateTime.Now.Year && this.StartTime.Month == DateTime.Now.Month && this.StartTime.Day == DateTime.Now.Day)
                            {
                                graphics.DrawString(this.StartTime.AddDays(1).ToLongDateString(), Calendar.ct.FontStd, new SolidBrush(Calendar.ct.SelectedForeColor), r, sf);
                            }
                            else
                            {
                                graphics.DrawString(this.StartTime.AddDays(1).ToLongDateString(), Calendar.ct.FontStd, Calendar.ct.ForeBrush, r, sf);
                            }
                        }
                    }

                    // Border
                    graphics.DrawRectangle(new Pen(Calendar.ct.BorderBrush), cellBounds);
                }
                else // Month mode
                {
                    // Background
                    if (IsNewMonth)
                    {
                        graphics.FillRectangle(new SolidBrush(Calendar.ct.NewMonth), cellBounds);
                    }
                    else
                    {
                        graphics.FillRectangle(new SolidBrush(Calendar.ct.CurrentMonth), cellBounds);
                    }

                    // Header
                    RectangleF r          = new RectangleF(cellBounds.Left + 1, cellBounds.Top, cellBounds.Width - 2, headerHeight);
                    string     headerText = "";
                    if (this.StartTime.Day == 1 || (this.ColumnIndex == 0 && this.RowIndex == 0))
                    {
                        headerText = GetHeaderText(0);
                    }
                    else
                    {
                        headerText = this.StartTime.Day.ToString();
                    }

                    if (this.StartTime.Year == DateTime.Now.Year && this.StartTime.Month == DateTime.Now.Month && this.StartTime.Day == DateTime.Now.Day)
                    {
                        // For Today we draw orange gradient
                        LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(cellBounds.Left, cellBounds.Top, cellBounds.Width, headerHeight), Calendar.ct.HeaderLight, Calendar.ct.HeaderDark, LinearGradientMode.Vertical);
                        graphics.FillRectangle(lgb, cellBounds.Left, cellBounds.Top, cellBounds.Width, headerHeight);

                        graphics.DrawLine(new Pen(Calendar.ct.TodayGradientBrush), cellBounds.Left, cellBounds.Top + headerHeight, cellBounds.Right, cellBounds.Top + headerHeight);
                    }

                    if (this.Selected && !bottomSelected)
                    {
                        graphics.FillRectangle(new SolidBrush(Calendar.ct.SelectionColor), cellBounds.Left, cellBounds.Top, cellBounds.Width, headerHeight);
                        sf.Alignment = StringAlignment.Far;
                        graphics.DrawString(headerText, Calendar.ct.FontStd, new SolidBrush(Calendar.ct.SelectedForeColor), r, sf);
                        graphics.DrawLine(new Pen(new SolidBrush(Calendar.ct.SelectionColor)), cellBounds.Left, cellBounds.Top + headerHeight, cellBounds.Right, cellBounds.Top + headerHeight);
                    }
                    else
                    {
                        sf.Alignment = StringAlignment.Far;
                        if (this.StartTime.Year == DateTime.Now.Year && this.StartTime.Month == DateTime.Now.Month && this.StartTime.Day == DateTime.Now.Day)
                        {
                            graphics.DrawString(headerText, Calendar.ct.FontStd, new SolidBrush(Calendar.ct.SelectedForeColor), r, sf);
                        }
                        else
                        {
                            graphics.DrawString(headerText, Calendar.ct.FontStd, Calendar.ct.ForeBrush, r, sf);
                        }
                    }

                    if (this.ColumnIndex == 5) // Sat/Sun column
                    {
                        // Draw another cell
                        // Draw the middle line through the cell
                        graphics.DrawLine(new Pen(Calendar.ct.BorderBrush), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2) - 1, cellBounds.Right, cellBounds.Top + (int)(cellBounds.Height / 2) - 1);

                        // If this is a new month paint the background
                        if (this.StartTime.AddDays(1).Day == 1)
                        {
                            graphics.FillRectangle(new SolidBrush(Calendar.ct.NewMonth), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width, (int)(cellBounds.Height / 2));
                        }

                        // Header
                        r          = new RectangleF(cellBounds.Left + 1, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width - 2, headerHeight);
                        headerText = "";
                        if (this.StartTime.AddDays(1).Day == 1 || (this.ColumnIndex == 0 && this.RowIndex == 0))
                        {
                            headerText = GetHeaderText(1);
                        }
                        else
                        {
                            headerText = this.StartTime.AddDays(1).Day.ToString();
                        }

                        if (this.StartTime.Year == DateTime.Now.Year && this.StartTime.Month == DateTime.Now.Month && this.StartTime.Day == DateTime.Now.Day)
                        {
                            // For Today we draw orange gradient
                            LinearGradientBrush lgb = new LinearGradientBrush(new Rectangle(cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width, headerHeight), Calendar.ct.HeaderLight, Calendar.ct.HeaderDark, LinearGradientMode.Vertical);
                            graphics.FillRectangle(lgb, cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width, headerHeight);
                            graphics.DrawLine(new Pen(Calendar.ct.TodayGradientBrush), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2) + headerHeight, cellBounds.Right, cellBounds.Top + (int)(cellBounds.Height / 2) + headerHeight);
                        }

                        if (this.Selected && bottomSelected)
                        {
                            graphics.FillRectangle(new SolidBrush(Calendar.ct.SelectionColor), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2), cellBounds.Width, headerHeight);
                            sf.Alignment = StringAlignment.Far;
                            graphics.DrawString(headerText, Calendar.ct.FontStd, new SolidBrush(Calendar.ct.SelectedForeColor), r, sf);
                            graphics.DrawLine(new Pen(new SolidBrush(Calendar.ct.SelectionColor)), cellBounds.Left, cellBounds.Top + (int)(cellBounds.Height / 2) + headerHeight, cellBounds.Right, cellBounds.Top + (int)(cellBounds.Height / 2) + headerHeight);
                        }
                        else
                        {
                            sf.Alignment = StringAlignment.Far;
                            if (this.StartTime.Year == DateTime.Now.Year && this.StartTime.Month == DateTime.Now.Month && this.StartTime.Day == DateTime.Now.Day)
                            {
                                graphics.DrawString(headerText, Calendar.ct.FontStd, new SolidBrush(Calendar.ct.SelectedForeColor), r, sf);
                            }
                            else
                            {
                                graphics.DrawString(headerText, Calendar.ct.FontStd, Calendar.ct.ForeBrush, r, sf);
                            }
                        }
                    }

                    // Border
                    graphics.DrawRectangle(new Pen(Calendar.ct.BorderBrush), cellBounds);
                } // Paint Background and Header

                // Paint items

                // Sort on StartTime
                List <CalendarItem> items = this.CalendarItems;
                for (int i = 0; i < items.Count; i++)
                {
                    for (int j = i; j < items.Count; j++)
                    {
                        if (items[i].StartTime.CompareTo(items[j].StartTime) > 0)
                        {
                            CalendarItem temp = items[i];
                            items[i] = items[j];
                            items[j] = temp;
                        }
                    }
                }

                if (!IsSpecialCell)
                {
                    // Form the text
                    string text = "";
                    for (int i = 0; i < items.Count; i++)
                    {
                        if (this.DateMode == DateMode.SevenDay)
                        {
                            text += String.Format("{0} {1}  {2}\n", items[i].StartTime.ToShortTimeString().ToLower(), items[i].EndTime.ToShortTimeString().ToLower(), items[i].Description);
                        }
                        else
                        {
                            text += String.Format("{0}  {1}\n", items[i].StartTime.ToShortTimeString().ToLower(), items[i].Description);
                        }
                    }

                    RectangleF rect = new RectangleF(cellBounds.Left + 3, cellBounds.Top + headerHeight + 8, cellBounds.Width - 3, cellBounds.Height - headerHeight - 8);

                    //LinearGradientBrush lgb = new LinearGradientBrush(rect, ColorTable.getLightColor(items[i].Color, 110), items[i].Color, LinearGradientMode.Horizontal);
                    //graphics.FillRectangle(lgb, rect);

                    sf.Alignment     = StringAlignment.Near;
                    sf.LineAlignment = StringAlignment.Near;
                    graphics.DrawString(text, Calendar.ct.FontStd, Calendar.ct.ForeBrush, rect, sf);
                }
                else
                {
                    string textSaturday = "";
                    for (int i = 0; i < items.Count; i++)
                    {
                        if (items[i].StartTime.DayOfWeek != DayOfWeek.Saturday)
                        {
                            continue;
                        }
                        if (this.DateMode == DateMode.SevenDay)
                        {
                            textSaturday += String.Format("{0} {1}  {2}\n", items[i].StartTime.ToShortTimeString().ToLower(), items[i].EndTime.ToShortTimeString().ToLower(), items[i].Description);
                        }
                        else
                        {
                            textSaturday += String.Format("{0}  {1}\n", items[i].StartTime.ToShortTimeString().ToLower(), items[i].Description);
                        }
                    }

                    string textSunday = "";
                    for (int i = 0; i < items.Count; i++)
                    {
                        if (items[i].StartTime.DayOfWeek != DayOfWeek.Sunday)
                        {
                            continue;
                        }
                        if (this.DateMode == DateMode.SevenDay)
                        {
                            textSunday += String.Format("{0} {1}  {2}\n", items[i].StartTime.ToShortTimeString().ToLower(), items[i].EndTime.ToShortTimeString().ToLower(), items[i].Description);
                        }
                        else
                        {
                            textSunday += String.Format("{0}  {1}\n", items[i].StartTime.ToShortTimeString().ToLower(), items[i].Description);
                        }
                    }

                    RectangleF rectSaturday = new RectangleF(cellBounds.Left + 3, cellBounds.Top + headerHeight + 8, cellBounds.Width - 3, (int)cellBounds.Height / 2 - headerHeight - 8);
                    RectangleF rectSunday   = new RectangleF(cellBounds.Left + 3, cellBounds.Top + (int)cellBounds.Height / 2 + headerHeight + 8, cellBounds.Width - 3, (int)cellBounds.Height / 2 - headerHeight - 8);
                    sf.Alignment     = StringAlignment.Near;
                    sf.LineAlignment = StringAlignment.Near;
                    graphics.DrawString(textSaturday, Calendar.ct.FontStd, Calendar.ct.ForeBrush, rectSaturday, sf);
                    graphics.DrawString(textSunday, Calendar.ct.FontStd, Calendar.ct.ForeBrush, rectSunday, sf);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        private int CountOfItemsOverlappingWithThisItem(CalendarItem item, ref int order)
        {
            try
            {
                // Find out how many maximum items this item overlaps with
                // for a given cell
                int overlapCount              = 0;
                List <CalendarItem> list      = new List <CalendarItem>();
                DateTime            startTime = item.StartTime;
                while (startTime.CompareTo(item.EndTime) < 0)
                {
                    HalfHourCell        cell  = this.DayColumn.GetHalfHourCell(startTime);
                    List <CalendarItem> items = cell.CalendarItems;
                    if (items == null)
                    {
                        continue;
                    }
                    if (items.Count - 1 > overlapCount)
                    {
                        overlapCount = items.Count - 1;
                        list         = items;
                    }
                    startTime = startTime.AddMinutes(30);
                }

                //// Now check this list to see the correct overlapCount -> this is
                //// bacause the items in the list may be overlapping already and
                //// we need to account for this.
                //for (int i = 0; i < list.Count; i++)
                //{
                //    // Only check items whose index is less that that of this item
                //    int indexOfThisItem = this.DayColumn.Calendar.CalendarItems.IndexOf(item);
                //    int indexOfOtherItem = this.DayColumn.Calendar.CalendarItems.IndexOf(list[i]);
                //    if (indexOfOtherItem >= indexOfThisItem)
                //        continue;

                //    int otherOverlap = this.DayColumn.Calendar.OverlapCount[list[i]];
                //    if (overlapCount <= otherOverlap)
                //        overlapCount = otherOverlap + 1;
                //}

                // Now overlapCount has the maximum number of items this item overlaps with.
                this.DayColumn.Calendar.OverlapCount[item] = overlapCount;

                if (overlapCount == 0)
                {
                    order = 0;
                    this.DayColumn.Calendar.OrderCount[item] = 0;
                    return(0);
                }

                // To find the order of this item we will go through the list
                // and figure out the order
                bool[] orders = new bool[overlapCount + 1];
                for (int i = 0; i < list.Count; i++)
                {
                    // Only check items whose index is less that that of this item
                    int indexOfThisItem  = this.DayColumn.Calendar.CalendarItems.IndexOf(item);
                    int indexOfOtherItem = this.DayColumn.Calendar.CalendarItems.IndexOf(list[i]);
                    if (indexOfOtherItem >= indexOfThisItem)
                    {
                        continue;
                    }

                    // If other item already has an order note it. If a previous
                    // order is available then take that
                    if (this.DayColumn.Calendar.OrderCount.ContainsKey(list[i]))
                    {
                        int itemOrder = this.DayColumn.Calendar.OrderCount[list[i]];
                        orders[itemOrder] = true;
                        for (int k = 0; k < itemOrder; k++)
                        {
                            if (!orders[k])
                            {
                                order     = k;
                                orders[k] = true;
                                this.DayColumn.Calendar.OrderCount[item] = k;
                                return(overlapCount);
                            }
                        }
                    }
                    else
                    {
                        // Item does not have an order. Give it the first available order
                        for (int k = 0; k < orders.Length; k++)
                        {
                            if (!orders[k])
                            {
                                this.DayColumn.Calendar.OrderCount[list[i]] = k;
                                orders[k] = true;
                            }
                        }
                    }
                }// for

                // Now take the first available spot in orders
                for (int k = 0; k < orders.Length; k++)
                {
                    if (!orders[k])
                    {
                        order     = k;
                        orders[k] = true;
                        this.DayColumn.Calendar.OrderCount[item] = k;
                        return(overlapCount);
                    }
                }
                return(0);
            }
            catch (Exception exception)
            {
                Trace.WriteLine(exception.ToString());
                order = 0;
                return(0);
            }
        }