Mini Kabibi Habibi
using System;
using System.Windows.Forms;
using System.Drawing;
using DevExpress.XtraPrinting;
using System.Threading;
using System.Globalization;
namespace XtraPrintingDemos.Calendar {
public class CalendarLink {
static bool IsHoliday(DayOfWeek date) {
return date == DayOfWeek.Saturday || date == DayOfWeek.Sunday;
}
const int WeeksInMonth = 6;
ImageList imageList;
string[] daysOfWeek;
string[] shortDaysOfWeek;
DayOfWeek[] week;
DayOfWeek firstDayOfWeek;
DateTime? selectedDate;
public DateTime SelectedDate {
get {
return selectedDate.HasValue ? selectedDate.Value : DateTime.Now;
}
set {
selectedDate = value;
}
}
public CalendarLink(ImageList iml) {
daysOfWeek = Thread.CurrentThread.CurrentCulture.DateTimeFormat.DayNames;
shortDaysOfWeek = Thread.CurrentThread.CurrentCulture.DateTimeFormat.AbbreviatedDayNames;
firstDayOfWeek = Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
week = new DayOfWeek[daysOfWeek.Length];
int index = 0;
for(int i = (int)firstDayOfWeek; i < daysOfWeek.Length; i++)
week[index++] = (DayOfWeek)i;
for(int i = 0; i < (int)firstDayOfWeek; i++)
week[index++] = (DayOfWeek)i;
if(iml != null)
imageList = iml;
else {
imageList = new ImageList();
imageList.ImageSize = new Size(35, 35);
}
}
public void CreateYearCalendar(PrintingSystem ps, int year) {
BrickGraphics gr = ps.Graph;
ps.Begin();
int columnCount;
int rowCount;
int width = (int)gr.ClientPageSize.Width - 1;
int height = (int)gr.ClientPageSize.Height - 1;
int monthHorizontalPadding;
int monthVerticalPadding = 24;
if(width > height) {
monthHorizontalPadding = 33;
rowCount = 2;
columnCount = 3;
height = height / 4;
width = width / 9 * 2;
} else {
monthHorizontalPadding = 40;
columnCount = 2;
rowCount = 3;
width = width / 7 * 2;
height = height / 5;
}
CreatePageFooter(gr);
gr.Modifier = BrickModifier.ReportHeader;
gr.Font = new Font("Tahoma", 26f, FontStyle.Bold);
gr.StringFormat = new BrickStringFormat(StringAlignment.Center, StringAlignment.Center);
gr.BackColor = Color.Transparent;
Brick brick = gr.DrawString(year.ToString(), Color.FromArgb(41, 113, 182), new RectangleF(0f, 0f, gr.ClientPageSize.Width, height / 2f), BorderSide.None);
brick.Separable = true;
gr.Modifier = BrickModifier.Detail;
int month = 1;
PaddingInfo daysPadding = new PaddingInfo(10, 10, 10, 10);
for(int row = 0; row <= rowCount; row++) {
for(int column = 0; column <= columnCount; column++) {
MonthPrint(
gr,
column * (width + monthHorizontalPadding),
row * (height + monthVerticalPadding),
height / 10 + daysPadding.Top,
(width - daysPadding.Left) / daysOfWeek.Length,
(height - daysPadding.Top) / (WeeksInMonth + 3),
month,
year,
18f,
9f,
"Tahoma",
daysPadding);
month++;
}
}
ps.End();
}
public void CreateMonthCalendar(PrintingSystem ps, int format, int month, int year) {
BrickGraphics gr = ps.Graph;
ps.Begin();
CreatePageFooter(gr);
const int LeftRightDaysPadding = 53;
const int TopBottomDaysPadding = 35;
PaddingInfo daysPadding = new PaddingInfo(LeftRightDaysPadding, LeftRightDaysPadding, TopBottomDaysPadding, TopBottomDaysPadding);
int leftCell = 0;
int topRow = 0;
int dayWidth = (int)(Math.Floor((double)(gr.ClientPageSize.Width - (daysPadding.Left + daysPadding.Right)) / week.Length));
int dayHeight = 60;
float daysFontSize = 22f;
float captionFontSize = 36f;
switch(format) {
case 1:
dayWidth = dayWidth / 3 * 2;
dayHeight = dayHeight / 3 * 2;
daysFontSize = daysFontSize / 3 * 2;
captionFontSize = captionFontSize / 3 * 2;
leftCell = (int)(gr.ClientPageSize.Width / WeeksInMonth);
topRow = (int)(gr.ClientPageSize.Height / WeeksInMonth);
break;
case 2:
dayWidth /= 2;
dayHeight /= 2;
daysFontSize /= 2;
captionFontSize /= 2;
leftCell = (int)((double)gr.ClientPageSize.Width / 4f);
topRow = (int)((double)gr.ClientPageSize.Height / 4f);
break;
}
gr.Modifier = BrickModifier.Detail;
MonthPrint(
gr,
leftCell,
topRow,
dayHeight + 5,
dayWidth,
dayHeight,
month,
year,
captionFontSize,
daysFontSize,
"Tahoma",
daysPadding);
ps.End();
}
private bool AllowWidth(BrickGraphics gr, string[] weekdays, int width) {
bool result = true;
foreach(string s in weekdays)
if(gr.MeasureString(s).Width > width)
result = false;
return result;
}
private void CreatePageFooter(BrickGraphics gr) {
gr.Font = new Font("Tahoma", 8f, FontStyle.Underline);
gr.BackColor = Color.Transparent;
gr.Modifier = BrickModifier.MarginalFooter;
RectangleF r = new RectangleF(0, 0, 0, gr.Font.Height);
PageInfoBrick brick = gr.DrawPageInfo(PageInfo.None, "XtraPrinting Library by Developer Express Inc.", Color.Blue, r, BorderSide.None);
brick.Url = "http://devexpress.com/Products/NET/XtraPrinting/";
brick.Hint = brick.Url;
brick.Alignment = BrickAlignment.Far;
brick.AutoWidth = true;
}
private void MonthPrint(BrickGraphics gr, int leftCell, int topRow, int daysHeight, int dayWidth, int dayHeight, int month, int year, float monthNameFontSize, float daysFontSize, string fontName, PaddingInfo dayPadding) {
gr.BeginUnionRect();
string captionMonth = Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames[month - 1];
gr.Font = new Font(fontName, monthNameFontSize, FontStyle.Bold);
gr.StringFormat = new BrickStringFormat(StringAlignment.Far, StringAlignment.Center);
gr.BackColor = Color.FromArgb(71, 143, 212);
gr.BorderColor = Color.FromArgb(42, 114, 183);
gr.StringFormat = gr.StringFormat.ChangeAlignment(StringAlignment.Center);
int captionWidth = dayWidth * daysOfWeek.Length + dayPadding.Left + dayPadding.Right;
int captionHeight = Math.Max(dayHeight * 2 - 14, gr.Font.Height);
gr.DrawString(
captionMonth,
Color.White,
new RectangleF(leftCell, topRow, captionWidth, captionHeight),
BorderSide.Top | BorderSide.Right | BorderSide.Left);
topRow += captionHeight;
gr.Font = new Font(fontName, daysFontSize, FontStyle.Regular);
gr.StringFormat = gr.StringFormat.ChangeAlignment(StringAlignment.Center);
gr.BackColor = Color.White;
DayOfWeek firstDayInWeek = new DateTime(year, month, 1).DayOfWeek;
int daysInMonth = DateTime.DaysInMonth(year, month);
bool allowWidth = AllowWidth(gr, daysOfWeek, dayWidth);
gr.BorderColor = Color.FromArgb(159, 159, 159);
gr.DrawRect(new RectangleF(leftCell, topRow, dayWidth * daysOfWeek.Length + dayPadding.Left + dayPadding.Right, daysHeight), BorderSide.Right | BorderSide.Left | BorderSide.Bottom, gr.BackColor, Color.FromArgb(159, 159, 159));
string weekDay = null;
int index = 0;
foreach(DayOfWeek dayOfWeek in week) {
int i = (int)dayOfWeek;
weekDay = (allowWidth) ? daysOfWeek[i] : shortDaysOfWeek[i].Substring(0, 2);
gr.DrawString(weekDay, IsHoliday(dayOfWeek) ? Color.FromArgb(188, 0, 0) : Color.FromArgb(42, 114, 183), new RectangleF(leftCell + index * dayWidth + dayPadding.Left, topRow, dayWidth, daysHeight), BorderSide.None);
index++;
}
topRow += daysHeight;
gr.DrawRect(new RectangleF(leftCell, topRow, dayWidth * daysOfWeek.Length + dayPadding.Left + dayPadding.Right, dayHeight * WeeksInMonth + dayPadding.Top + dayPadding.Bottom), BorderSide.All, Color.FromArgb(255, 241, 219), Color.FromArgb(159, 159, 159));
gr.Font = new Font(fontName, daysFontSize);
int day = 1;
bool drawHorizontalLine = true;
for(int weekIndex = 0; weekIndex < WeeksInMonth; weekIndex++) {
foreach(DayOfWeek dayOfWeek in week) {
string dayNumber = string.Empty;
string hint = null;
gr.BackColor = Color.Transparent;
gr.ForeColor = Color.Black;
if(drawHorizontalLine && day > daysInMonth && dayOfWeek == week[0])
drawHorizontalLine = false;
if(!(weekIndex == 0 && Array.IndexOf(week, dayOfWeek) < Array.IndexOf(week, firstDayInWeek)) && day <= daysInMonth) {
dayNumber = day.ToString();
DateTime currentDate = new DateTime(year, month, day);
if (currentDate.Equals(SelectedDate)) {
gr.BackColor = Color.FromArgb(0x47, 0x8f, 0xd4);
gr.ForeColor = Color.White;
drawHorizontalLine = true;
} else if(IsHoliday(dayOfWeek))
gr.ForeColor = Color.FromArgb(188, 0, 0);
hint = currentDate.ToString("dddd, MMMM dd yyyy");
day++;
}
BorderSide borders = weekIndex > 0 && drawHorizontalLine ? BorderSide.Top : BorderSide.None;
TextBrick dayBrick = gr.DrawString(
dayNumber,
gr.ForeColor,
new RectangleF(
leftCell + Array.IndexOf(week, dayOfWeek) * dayWidth + dayPadding.Left,
topRow + dayHeight * weekIndex + dayPadding.Top,
dayWidth,
dayHeight),
borders);
dayBrick.Hint = hint;
dayBrick.BorderStyle = BrickBorderStyle.Outset;
}
}
gr.EndUnionRect();
}
}
}