// one line to give the program's name and an idea of what it does.
// Copyright (C) 2005 name of Borja Snchez Zamorano
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
using System;
using System.Reflection;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace CalWidget{
internal class CalContent : System.Windows.Forms.Control {
private System.Windows.Forms.TextBox textBox1;
Bitmap _offBmp = null;
Bitmap _offBmpBuffer = null;
int scrollMax=100;
int scrollValue=0;
System.DateTime firstDate, lastDate;
System.DateTime date;
int[] limitsH = new int[8];
int[] limitsV = new int[7];
uint firstDayOfWeek;
int daysByWeek;
bool movingEvent = false;
int timeFormat;
string timeFormatText, timeFormatTextLeft;
CalendarStyle style;
ArrayList dataView, dataViewed;
Image shadow00, shadow02, shadow10, shadow12, shadow20, shadow21, shadow22;
public EventTask Selected;
OperationMouseMove lastMouse;
CalTitle ct;
int alpha;
string labelNewEvent = "New event";
int startHour = 0;
int endHour = 24;
// Cosas de dibujado para ahorrar new.... Siempre son iguales a diferencia del color
Pen pen1;
SolidBrush sb1;
class OperationMouseMove {
public System.DateTime start, end;
public System.TimeSpan duration;
public System.DateTime mouse, newMouse;
public int type;
// Type:
// 0: Nada
// 1: Redimensionando un evento
// 2: Moviendo un evento
// 3: Se esta aadiendo un evento con el arrastre del raton
}
struct RectangleViewed {
public int y, y2, day;
public EventTask et;
}
public class EventTask {
public System.DateTime Start;
public System.DateTime End;
public CalTask calTask;
public CalData calData;
}
internal CalContent()
{
pen1 = new Pen(Color.Empty);
sb1 = new SolidBrush(Color.Empty);
InitializeComponent();
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserMouse, true);
this.SetStyle(ControlStyles.ResizeRedraw, false);
this.SetStyle(ControlStyles.Opaque, true);
dataView = new ArrayList();
dataViewed = new ArrayList();
Selected = null;
daysByWeek = 7;
timeFormat = 1;
timeFormatText = "H:mm";
timeFormatTextLeft = "H:mm";
System.Globalization.DateTimeFormatInfo dtfi = new System.Globalization.DateTimeFormatInfo();
firstDayOfWeek = (uint) dtfi.FirstDayOfWeek;
style = CalendarStyle.Week;
date = System.DateTime.Now;
shadow00 = GetResourceImage("shadow-0-0.png");
shadow02 = GetResourceImage("shadow-0-2.png");
shadow10 = GetResourceImage("shadow-1-0.png");
shadow12 = GetResourceImage("shadow-1-2.png");
shadow20 = GetResourceImage("shadow-2-0.png");
shadow21 = GetResourceImage("shadow-2-1.png");
shadow22 = GetResourceImage("shadow-2-2.png");
}
#region Cdigo generado por el Diseador de Windows Forms
void InitializeComponent() {
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.AutoSize = false;
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox1.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
this.textBox1.Location = new System.Drawing.Point(40, 48);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(80, 64);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.Visible = false;
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBox1KeyDown);
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextBox1KeyPress);
this.textBox1.Leave += new System.EventHandler(this.TextBox1Leave);
//
// CalContent
//
this.Controls.Add(this.textBox1);
this.Name = "CalContent";
this.SizeChanged += new System.EventHandler(this.CalContentSizeChanged);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CalContentMouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.CalContentPaint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CalContentMouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CalContentMouseDown);
this.ResumeLayout(false);
}
#endregion
public void DefineTitle(CalTitle ct)
{
this.ct = ct;
}
Graphics GenerateBackground()
{
// HACK: Si hay cache, la uso
if ((movingEvent) && (_offBmpBuffer != null)) {
if (_offBmp != null) _offBmp.Dispose();
_offBmp = (Bitmap) _offBmpBuffer.Clone(new Rectangle(0, 0, _offBmpBuffer.Width, _offBmpBuffer.Height), _offBmpBuffer.PixelFormat);
return Graphics.FromImage( _offBmp );
}
// Else
if ((_offBmp == null) || (_offBmp.Height != scrollMax) || (_offBmp.Width != this.ClientRectangle.Width)) {
if (_offBmp != null) _offBmp.Dispose();
_offBmp = new Bitmap( this.ClientRectangle.Width, scrollMax);
}
Graphics _g = Graphics.FromImage( _offBmp );
_g.Clear(Color.White);
if (style == CalendarStyle.Month) {
PaintStyleMonth(_g);
} else {
PaintStyleWeek(_g);
}
if (movingEvent) {
_offBmpBuffer = (Bitmap) _offBmp.Clone(new Rectangle(0, 0, _offBmp.Width, _offBmp.Height), _offBmp.PixelFormat);
}
return _g;
}
void Repaint2()
{
if ((this.ClientRectangle.Width<1) || (this.ClientRectangle.Height<1)) return;
Graphics _g = GenerateBackground();
if (style != CalendarStyle.Month) {
if (Selected != null) {
drawTask(_g, Selected);
}
}
_g.Dispose();
Paint2();
}
void Paint2()
{
if ((_offBmp == null) || (_offBmp.Height != scrollMax) || (_offBmp.Width != this.ClientRectangle.Width)) {
Repaint2();
return;
}
if ((this.ClientRectangle.Width<1) || (this.ClientRectangle.Height<1)) return;
IntPtr hwnd = new IntPtr();
hwnd = this.Handle;
Graphics drawingSurface = Graphics.FromHwnd(hwnd);
drawingSurface.DrawImage(_offBmp, 0, -scrollValue);
drawingSurface.Dispose();
}
void CalContentPaint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawImage(_offBmp, 0, -scrollValue);
}
void CalContentSizeChanged(object sender, System.EventArgs e)
{
// Comprueba si hay dialogo de texto para quitarlo
IsChangeText();
Repaint2();
if (ct != null)
ct.setWidth(this.ClientRectangle.Width);
}
Image GetResourceImage (string name)
{
Assembly assembly = System.Reflection.Assembly.GetCallingAssembly();
System.IO.Stream s = assembly.GetManifestResourceStream (name);
return Image.FromStream(s);
}
void PaintText(Graphics g, Font font1, SolidBrush brush, string text, int x, int y, int width, int height)
{
int height2 = font1.Height;
while ((height2+font1.Height)<height) height2+= font1.Height;
RectangleF drawRect = new RectangleF( x, y, width, height2);
g.DrawString(text, font1, brush, drawRect);
}
Color Color2Alpha(Color color)
{
return Color.FromArgb(alpha, color.R, color.G, color.B);
}
void PaintTasksPref(Graphics g, EventTask et, int x1, int y1, int ancho, int alto)
{
int red, green, blue, red2, green2, blue2;
int i;
red = et.calData.ColorBgSelectedLeft.R;
green = et.calData.ColorBgSelectedLeft.G;
blue = et.calData.ColorBgSelectedLeft.B;
red2 = et.calData.ColorBgSelectedRight.R - red;
green2 = et.calData.ColorBgSelectedRight.G - green;
blue2 = et.calData.ColorBgSelectedRight.B - blue;
int alpha2 = 255;
if (_offBmpBuffer != null) {
alpha2 = alpha;
} else {
alpha2 = 255;
}
if (et.calData.Selected) {
if ((alto-y1+1)>30) {
i=0;
y1 = y1+14;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-6);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-4);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-3);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-2);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-2);
i=ancho-5;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-2);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-2);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-3);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-4);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-6);
} else {
i=0;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+5, x1+i, alto-6);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+3, x1+i, alto-4);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+2, x1+i, alto-3);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+1, x1+i, alto-2);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+1, x1+i, alto-2);
i=ancho-5;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+1, x1+i, alto-2);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+1, x1+i, alto-2);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+2, x1+i, alto-3);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+3, x1+i, alto-4);
i++;
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1+5, x1+i, alto-6);
}
for (i=5; i<(ancho-5); i++) {
pen1.Color = Color.FromArgb(alpha2, (red+((i*red2)/ancho)), (green+((i*green2)/ancho)), (blue+((i*blue2)/ancho)));
g.DrawLine(pen1, x1+i, y1, x1+i, alto-1);
}
} else {
pen1.Color = Color2Alpha(et.calData.ColorBgUnselected);
sb1.Color = Color2Alpha(et.calData.ColorBgUnselected);
i=0;
g.DrawLine(pen1, x1+i, y1+5, x1+i, alto-6);
i++;
g.DrawLine(pen1, x1+i, y1+3, x1+i, alto-4);
i++;
g.DrawLine(pen1, x1+i, y1+2, x1+i, alto-3);
i++;
g.DrawLine(pen1, x1+i, y1+1, x1+i, alto-2);
i++;
g.DrawLine(pen1, x1+i, y1+1, x1+i, alto-2);
i=ancho-5;
g.DrawLine(pen1, x1+i, y1+1, x1+i, alto-2);
i++;
g.DrawLine(pen1, x1+i, y1+1, x1+i, alto-2);
i++;
g.DrawLine(pen1, x1+i, y1+2, x1+i, alto-3);
i++;
g.DrawLine(pen1, x1+i, y1+3, x1+i, alto-4);
i++;
g.DrawLine(pen1, x1+i, y1+5, x1+i, alto-6);
g.FillRectangle(sb1, x1+5, y1, ancho-10, alto - y1);
}
}
int getPosHours(double hour, double minute)
{
double pos=hour+(minute/60);
return (int)((scrollMax*pos)/24);
}
void drawPartTask(Graphics g, EventTask et, System.DateTime start, System.DateTime end, bool drawBegin, bool drawEnd)
{
if ((end < firstDate) || (start > lastDate)) return;
int x = getPosHours(start.Hour, start.Minute);
int x2 = getPosHours(end.Hour, end.Minute);
if ((x==0) && (x2==0)) return;
if ((x2 - x) < getPosHours(0, 30))
x2 = getPosHours(start.Hour, start.Minute + 30);
if (x2 < x)
x2 = x + getPosHours(0, 30) + 1;
if (!drawBegin) x -= 15;
if (!drawEnd) x2 += 7;
int day = 0;
while (firstDate.AddDays(day).DayOfWeek != start.DayOfWeek) day++;
// Aadir a la lista de objetos visibles
RectangleViewed rect;
rect.day = day;
rect.y = x;
rect.y2 = x2;
rect.et = et;
dataViewed.Add(rect);
// Dibujar sombra si no se mueve o redimensiona
if (_offBmpBuffer == null) {
// Dibujar sombra solo si esta selecionado
if ((et.calData.Selected) && (et == Selected)) {
Rectangle destRect1;
System.Drawing.Imaging.ImageAttributes ia = new System.Drawing.Imaging.ImageAttributes();
ia.SetWrapMode(WrapMode.Tile);
destRect1 = new Rectangle( limitsH[day]-5, x+5, shadow00.Width, shadow00.Height);
g.DrawImage(shadow00, destRect1, 0, 0, shadow00.Width, shadow00.Height, GraphicsUnit.Pixel, ia);
destRect1 = new Rectangle( limitsH[day+1]+1, x+5, shadow02.Width, shadow02.Height);
g.DrawImage(shadow02, destRect1, 0, 0, shadow02.Width, shadow02.Height, GraphicsUnit.Pixel, ia);
destRect1 = new Rectangle( limitsH[day]-5, x2-6, shadow20.Width, shadow20.Height);
g.DrawImage(shadow20, destRect1, 0, 0, shadow20.Width, shadow20.Height, GraphicsUnit.Pixel, ia);
destRect1 = new Rectangle( limitsH[day+1]-9, x2-6, shadow22.Width, shadow22.Height);
g.DrawImage(shadow22, destRect1, 0, 0, shadow22.Width, shadow22.Height, GraphicsUnit.Pixel, ia);
destRect1 = new Rectangle( limitsH[day]-5, x+5+shadow00.Height, shadow10.Width, x2-6 - (x+5+shadow00.Height));
g.DrawImage(shadow10, destRect1, 0, 0, shadow10.Width, shadow10.Height, GraphicsUnit.Pixel, ia);
destRect1 = new Rectangle( limitsH[day+1]+1, x+5+shadow00.Height, shadow12.Width, x2-6 - (x+5+shadow00.Height));
g.DrawImage(shadow12, destRect1, 0, 0, shadow12.Width, shadow12.Height, GraphicsUnit.Pixel, ia);
destRect1 = new Rectangle( limitsH[day]-5 + shadow20.Width, x2, limitsH[day+1]-9-(limitsH[day]-5 + shadow20.Width), shadow21.Height);
g.DrawImage(shadow21, destRect1, 0, 0, shadow21.Width, shadow21.Height, GraphicsUnit.Pixel, ia);
ia.Dispose();
}
}
PaintTasksPref(g, et, limitsH[day] + 1, x +1, limitsH[day+1] - limitsH[day] - 1, x2 - 1);
int alpha2 = 255;
if (_offBmpBuffer != null) {
alpha2 = alpha;
} else {
alpha2 = 255;
}
// Mismo color para selected o no selected
Color color = et.calData.ColorBorder;
color = Color.FromArgb(alpha2, color.R, color.G, color.B);
pen1.Color = color;
//Bordes
g.DrawLine(pen1, limitsH[day], x+6, limitsH[day], x2 - 7);
g.DrawLine(pen1, limitsH[day+1], x+6, limitsH[day+1], x2 - 7);
g.DrawLine(pen1, limitsH[day]+6, x, limitsH[day+1]-6, x);
g.DrawLine(pen1, limitsH[day]+6, x2-1, limitsH[day+1]-6, x2 - 1);
//Abajo Derecha
g.DrawLine(pen1, limitsH[day+1]-5, x2-2 , limitsH[day+1]-4, x2-2);
g.DrawLine(pen1, limitsH[day+1]-2, x2-4, limitsH[day+1]-3, x2-3);
g.DrawLine(pen1, limitsH[day+1]-1, x2-6 , limitsH[day+1]-1, x2-5);
//Abajo Izquierda
g.DrawLine(pen1, limitsH[day]+4, x2-2, limitsH[day]+5, x2-2);
g.DrawLine(pen1, limitsH[day]+3, x2-3, limitsH[day]+2, x2-4);
g.DrawLine(pen1, limitsH[day]+1, x2-6, limitsH[day]+1, x2-5);
//Arriba Izquierda
g.DrawLine(pen1, limitsH[day]+4, x+1, limitsH[day]+5, x+1);
g.DrawLine(pen1, limitsH[day]+3, x+2, limitsH[day]+2, x+3);
g.DrawLine(pen1, limitsH[day]+1, x+4, limitsH[day]+1, x+5);
//Arriba Derecha
g.DrawLine(pen1, limitsH[day+1]-5, x+1, limitsH[day+1]-4, x+1);
g.DrawLine(pen1, limitsH[day+1]-3, x+2, limitsH[day+1]-2, x+3);
g.DrawLine(pen1, limitsH[day+1]-1, x+4, limitsH[day+1]-1, x+5);
sb1.Color = color;
StringFormat align1 = new StringFormat();
Font font1 = new Font("Tahoma", 8, FontStyle.Bold);
if ((x2 -x -1) > 30) {
if (et.calData.Selected) {
g.DrawLine(pen1, limitsH[day]+6, x+1, limitsH[day+1]-6, x+1);
g.DrawLine(pen1, limitsH[day]+4, x+2, limitsH[day+1]-4, x+2);
g.DrawLine(pen1, limitsH[day]+3, x+3, limitsH[day+1]-3, x+3);
g.DrawLine(pen1, limitsH[day]+2, x+4, limitsH[day+1]-2, x+4);
g.DrawLine(pen1, limitsH[day]+2, x+5, limitsH[day+1]-2, x+5);
color = et.calData.ColorBorder;
color = Color.FromArgb(alpha2, color.R, color.G, color.B);
sb1.Color = color;
g.FillRectangle(sb1, limitsH[day] + 1, x + 6, limitsH[day+1]-limitsH[day] - 1, 9);
sb1.Color = et.calData.ColorText;
}
g.DrawString(et.Start.ToString(timeFormatText), font1, sb1, limitsH[day]+5, x, align1);
if ((Selected == et) && (lastMouse != null) && drawEnd) {
align1.Alignment = StringAlignment.Far;
g.DrawString(et.End.ToString(timeFormatText), font1, sb1, limitsH[day+1]-3, x2-(font1.Height+3), align1);
}
PaintText(g, font1, sb1, et.calTask.Summary, limitsH[day]+5, x+17, limitsH[day+1]-limitsH[day] - 8, x2 -x - 16);
} else {
if (et.calData.Selected)
sb1.Color = et.calData.ColorText;
PaintText(g, font1, sb1, et.calTask.Summary, limitsH[day]+5, x+2, limitsH[day+1]-limitsH[day] - 8, x2 -x -1);
if ((Selected == et) && (lastMouse != null) && (lastMouse.type == 1) && drawEnd) {
align1.Alignment = StringAlignment.Far;
g.DrawString(et.End.ToString(timeFormatText), font1, sb1, limitsH[day+1]-3, x2-(font1.Height+3), align1);
}
}
font1.Dispose();
}
void drawTask(Graphics g, EventTask et)
{
System.DateTime end = et.End;
if ((end.Hour==0) && (end.Minute==0))
if (et.Start != end)
end = end.AddSeconds(-1);
else
end = end.AddMinutes(30);
if (et.Start.Day != end.Day) {
int i = -1;
System.DateTime aux;
System.DateTime start;
System.DateTime aux2;
aux = new System.DateTime(end.Year, end.Month, end.Day, 0, 0, 0);
drawPartTask(g, et, aux, end, false, true);
start = end.AddDays(i);
while (start.Day != et.Start.Day) {
aux = new System.DateTime(start.Year, start.Month, start.Day, 0, 0, 0);
aux2 = new System.DateTime(start.Year, start.Month, start.Day, 23, 59, 59);
drawPartTask(g, et, aux, aux2, false, false);
i--;
start = end.AddDays(i);
}
aux = new System.DateTime(et.Start.Year, et.Start.Month, et.Start.Day, 23, 59, 59);
drawPartTask(g, et, et.Start, aux, true, false);
} else {
drawPartTask(g, et, et.Start, end, true, true);
}
}
void PaintTasks(Graphics g)
{
dataViewed.Clear();
foreach (EventTask et in dataView) {
if (Selected != et)
drawTask(g, et);
}
}
void PaintHours(Graphics g)
{
int x;
Color colorLight = Color.FromArgb(255, 229, 229, 229);
Color colorDark = Color.FromArgb(255, 204, 204, 204);
int totalMediumHours = 24*2;
for (int i=1; i<totalMediumHours; i++) {
if ((i%2)==0)
pen1.Color = colorDark;
else
pen1.Color = colorLight;
x = (scrollMax*i)/totalMediumHours;
g.DrawLine(pen1, 40, x, this.ClientRectangle.Width, x);
}
StringFormat align1 = new StringFormat();
align1.Alignment = StringAlignment.Far;
Font font1 = new Font("Tahoma", 8);
sb1.Color = Color.FromArgb(255, 0, 0, 0);
for (int i=1; i<=23; i++) {
System.DateTime a = new System.DateTime(1,1,1,i,0,0);
g.DrawString(a.ToString(timeFormatTextLeft), font1, sb1, 38, ((scrollMax*i)/24)-font1.Height, align1);
}
font1.Dispose();
}
void PaintStyleWeek(Graphics g)
{
int max = (style == CalWidget.CalendarStyle.Day)? 1 : daysByWeek;
pen1.Color = Color.FromArgb(255, 204, 204, 204);
g.DrawLine(pen1, 40, 0, 40, scrollMax);
limitsH[0]= 40;
limitsH[max]= this.ClientRectangle.Width-1;
for (int i=1;i<max; i++) {
limitsH[i]= (((limitsH[daysByWeek]-40)*i)/daysByWeek)+40;
}
for (int i=1;i<=max; i++) {
g.DrawLine(pen1, limitsH[i], 0, limitsH[i], scrollMax);
}
Color color1 = Color.FromArgb(255, 247, 247, 247);
Color color2 = Color.FromArgb(255, 236, 243, 252);
for (int i=0;i<max; i++) {
sb1.Color = color1;
if (firstDate.AddDays(i).ToString("yyyyMMdd")==date.ToString("yyyyMMdd")) {
g.FillRectangle(sb1, limitsH[i] + 1, 0, limitsH[i+1]-limitsH[i] - 1, scrollMax);
}
sb1.Color = color2;
if (firstDate.AddDays(i).ToString("yyyyMMdd")==System.DateTime.Now.ToString("yyyyMMdd")) {
g.FillRectangle(sb1, limitsH[i] + 1, 0, limitsH[i+1]-limitsH[i] - 1, scrollMax);
}
}
PaintHours(g);
sb1.Color = Color.FromArgb(12, 0, 0, 0);
g.FillRectangle(sb1, 0, 0, this.ClientRectangle.Width, getPosHours(startHour, 0));
g.FillRectangle(sb1, 0, getPosHours(endHour, 0), this.ClientRectangle.Width, scrollMax - getPosHours(endHour, 0));
PaintTasks(g);
}
#region Vista Mensual
void drawTaskMonth(Graphics g, EventTask et, int col, int row, int line)
{
Font font1 = new Font("Tahoma", 8, FontStyle.Bold);
sb1.Color = et.calData.ColorBorder;
int y = limitsV[row]+5 + (line*13) + 11;
if (y < limitsV[row+1]-11)
PaintText(g, font1, sb1, et.Start.ToString(timeFormatText) + " " + et.calTask.Summary, limitsH[col]+5, y, limitsH[col+1]-limitsH[col]-5, 11);
}
void PaintTasksMonth(Graphics g)
{
dataViewed.Clear();
int col = 0;
int row = 0;
int line = 0;
System.DateTime dt = firstDate;
while (dt<lastDate) {
line = 0;
foreach (EventTask et in dataView) {
if (dt.ToString("yyyyMMdd")==et.calTask.start.ToString("yyyyMMdd")) {
drawTaskMonth(g, et, col, row, line);
line++;
}
}
col++;
if (col==7) {
col=0;
row++;
}
dt = dt.AddDays(1);
}
}
void PaintStyleMonth(Graphics g)
{
int rows = ((lastDate - firstDate).Days / 7)+1;
pen1.Color = Color.FromArgb(255, 204, 204, 204);
for (int i=0; i<=7; i++)
limitsH[i] = (this.ClientRectangle.Width*i)/7;
for (int i=0; i<=rows; i++)
limitsV[i] = 31 + (((scrollMax-31)*i)/rows);
for (int i=1; i<=6; i++)
g.DrawLine(pen1, limitsH[i], limitsV[0], limitsH[i], scrollMax);
for (int i=0; i<rows; i++)
g.DrawLine(pen1, 0, limitsV[i], this.ClientRectangle.Width, limitsV[i]);
int col, row;
System.DateTime dt = firstDate;
col = row = 0;
int status = 0;
Color colorLight = Color.FromArgb(255, 198, 198, 198);
Color colorDark = Color.FromArgb(255, 64, 64, 64);
Color colorBlack = Color.FromArgb(255, 0, 0, 0);
Color color = colorLight;
Font font1 = new Font("Tahoma", 9);
StringFormat alignRight = new StringFormat();
alignRight.Alignment = StringAlignment.Far;
while (dt<lastDate) {
if (dt.ToString("yyyyMMdd")==date.ToString("yyyyMMdd")) {
sb1.Color = Color.FromArgb(255, 247, 247, 247);
g.FillRectangle(sb1, limitsH[col] + 1, limitsV[row]+1, limitsH[col+1]-limitsH[col] - 1, limitsV[row+1]-limitsV[row] - 1);
}
if (dt.ToString("yyyyMMdd")==System.DateTime.Now.ToString("yyyyMMdd")) {
sb1.Color = Color.FromArgb(255, 237, 243, 250);
g.FillRectangle(sb1, limitsH[col] + 1, limitsV[row]+1, limitsH[col+1]-limitsH[col] - 1, limitsV[row+1]-limitsV[row] - 1);
}
if (dt.Day==1) {
if (status==1) {
color = colorLight;
status = 2;
}
if (status==0) {
color = colorDark;
status = 1;
}
}
sb1.Color = color;
g.DrawString(dt.Day.ToString(), font1, sb1,limitsH[col+1]-4, limitsV[row]+2, alignRight);
col++;
if (col==7) {
col=0;
row++;
}
dt = dt.AddDays(1);
}
sb1.Color = colorBlack;
dt = firstDate;
alignRight.Alignment = StringAlignment.Center;
for (int i=0; i<=6; i++) {
g.DrawString(dt.ToString("dddd"), font1, sb1, (limitsH[i]+limitsH[i+1])/2, limitsV[0]-font1.Height, alignRight);
dt = dt.AddDays(1);
}
font1.Dispose();
PaintTasksMonth(g);
}
#endregion
public System.DateTime Date {
get {
return date;
}
set {
date = value;
UpdateCalendar(false);
}
}
public int ScrollMax {
get {
return scrollMax;
}
set {
IsChangeText();
scrollMax = value;
UpdateCalendar(false);
}
}
public int ScrollValue {
get {
return scrollValue;
}
set {
scrollValue = value;
UpdateCalendar(false);
}
}
public uint FirstDayOfWeek {
get {
return firstDayOfWeek;
}
set {
IsChangeText();
firstDayOfWeek = value;
UpdateCalendar(false);
}
}
public int DaysByWeek {
get {
return daysByWeek;
}
set {
IsChangeText();
daysByWeek = value;
UpdateCalendar(false);
}
}
public int StartHour {
get {
return startHour;
}
set {
int auxValue = value;
if (auxValue<0) auxValue=0;
if (auxValue>12) auxValue=12;
startHour = auxValue;
UpdateCalendar(false);
}
}
public int EndHour {
get {
return endHour;
}
set {
int auxValue = value;
if (auxValue<12) auxValue=12;
if (auxValue>24) auxValue=24;
endHour = auxValue;
UpdateCalendar(false);
}
}
public int TimeFormat {
get {
return timeFormat;
}
set {
IsChangeText();
timeFormat = value;
if (timeFormat==0) {
System.Globalization.DateTimeFormatInfo dtfi = System.Globalization.DateTimeFormatInfo.CurrentInfo;
dtfi.AMDesignator = "am";
dtfi.PMDesignator = "pm";
timeFormatText = "h:mmtt";
timeFormatTextLeft = "htt";
} else {
timeFormatText = "H:mm";
timeFormatTextLeft = "H:mm";
}
UpdateCalendar(false);
}
}
public CalendarStyle Style {
get {
return style;
}
set {
style = value;
UpdateCalendar(false);
}
}
public int Alpha {
get {
return alpha;
}
set {
alpha = value;
UpdateCalendar(false);
}
}
public void UpdateCalendar(bool forceUpdate)
{
RecalculateDateLimits(forceUpdate);
Repaint2();
}
void RecalculateDateLimits(bool forceUpdate)
{
System.DateTime first, last;
switch(style) {
case CalendarStyle.Week:
first = date.AddDays(0);
for (int i=0; i<7; i++) {
first = date.AddDays(-i);
if (daysByWeek==5) {
if (first.DayOfWeek == DayOfWeek.Monday)
break;
} else {
if ((uint) first.DayOfWeek == firstDayOfWeek)
break;
}
}
last = first.AddDays(daysByWeek-1);
break;
case CalendarStyle.Month:
first = date;
first = first.AddDays(-(date.Day - 1));
last = first;
last = last.AddDays(System.DateTime.DaysInMonth(last.Year, last.Month)-1);
while ((uint) first.DayOfWeek != firstDayOfWeek)
first = first.AddDays(-1);
while ((uint) last.AddDays(1).DayOfWeek != firstDayOfWeek)
last = last.AddDays(1);
break;
default:
first = last = date;
break;
}
last = last.AddDays(1).AddSeconds(-1);
if ( forceUpdate || ((last != lastDate) || (first != firstDate)) ) {
firstDate = first;
lastDate = last;
RefreshData();
}
}
void RefreshData()
{
EventTask aux;
EventTask Selected2 = Selected;
dataView.Clear();
foreach (CalData cd in Calendar.cd) {
if (!cd.Visible) continue;
foreach (CalTask ct in cd.DataList) {
if (ct.RRule == null) {
if ((ct.end > firstDate) && (ct.start < lastDate)) {
aux = new EventTask();
aux.Start = ct.start;
aux.End = ct.end;
aux.calTask = ct;
aux.calData = cd;
if (Selected != null) {
if ((Selected.Start == aux.Start) && (Selected.End == aux.End) && (Selected.calTask == aux.calTask) && (Selected.calData == aux.calData))
Selected = aux;
}
dataView.Add(aux);
}
} else {
System.DateTime end = new System.DateTime(firstDate.Year, firstDate.Month, firstDate.Day);
System.DateTime start;
System.TimeSpan duration = ct.end - ct.start;
end -= duration;
end = new System.DateTime(end.Year, end.Month, end.Day);
do {
if (ct.IsInDate(new System.DateTime(end.Year, end.Month, end.Day, ct.start.Hour, ct.start.Minute, ct.start.Second))) {
start = new System.DateTime(end.Year, end.Month, end.Day, ct.start.Hour, ct.start.Minute, ct.start.Second);
end = start + duration;
if ((end > firstDate) && (start < lastDate)) {
aux = new EventTask();
aux.Start = start;
aux.End = end;
aux.calTask = ct;
aux.calData = cd;
if (Selected != null) {
if ((Selected.Start == aux.Start) && (Selected.End == aux.End) && (Selected.calTask == aux.calTask) && (Selected.calData == aux.calData))
Selected = aux;
}
dataView.Add(aux);
}
}
end = end.AddDays(1);
} while (end < lastDate);
}
}
}
// HACK: Error in insert Text (Solved)
if (Selected == Selected2) Selected=null;
SortData();
}
void SortData()
{
object aux = null;
for (int i=0; i<dataView.Count; i++) {
for (int j=0; j<dataView.Count; j++) {
if (i==j) continue;
if (((EventTask)dataView[i]).Start < ((EventTask)dataView[j]).Start) {
aux = dataView[i];
dataView[i] = dataView[j];
dataView[j] = aux;
}
}
}
}
System.DateTime getPosDateTime(double x, double y) {
System.DateTime aux = System.DateTime.MinValue;
if (x < limitsH[0]) return aux;
if (x >= this.ClientRectangle.Width) return aux;
if (y < 0) return aux;
if (y >= scrollMax) return aux;
int minutes = (15 * (int) System.Math.Round( (((y * (24*60))/scrollMax))/15));
aux = new System.DateTime(firstDate.Year, firstDate.Month, firstDate.Day);
aux = aux.AddMinutes(minutes);
int i = 0;
while (aux <= lastDate) {
if ((x>=limitsH[i]) && (x<=limitsH[i+1]))
break;
i++;
aux = aux.AddDays(1);
}
return aux;
}
void InsertNewEvent()
{
CalData cd = null;
foreach (CalData cd2 in CalWidget.Calendar.cd)
if (cd2.Selected)
cd = cd2;
if (cd == null) return;
Selected = new EventTask();
Selected.Start = lastMouse.mouse;
Selected.End = lastMouse.mouse;
CalTask ct = new CalTask();
ct.start = lastMouse.mouse;
ct.end = lastMouse.mouse;
ct.Summary = labelNewEvent;
ct.RRule = null;
cd.DataList.Add(ct);
Selected.calTask = ct;
Selected.calData = cd;
lastMouse.start = Selected.Start;
lastMouse.end = Selected.End;
Selected.Start = lastMouse.start;
Selected.End = Selected.Start + lastMouse.duration;
Repaint2();
}
void ChangeNameTask()
{
lastMouse = null;
textBox1.Text = Selected.calTask.Summary;
textBox1.SelectAll();
int day = 0;
while (firstDate.AddDays(day).DayOfWeek != Selected.Start.DayOfWeek) day++;
textBox1.Left = limitsH[day];
textBox1.Width = limitsH[day+1] - limitsH[day] + 1;
textBox1.Top = getPosHours(Selected.Start.Hour, Selected.Start.Minute) - scrollValue;
if (Selected.Start.DayOfWeek != Selected.End.DayOfWeek)
textBox1.Height = getPosHours(23, 59) - textBox1.Top;
else
textBox1.Height = getPosHours(Selected.End.Hour, Selected.End.Minute) - textBox1.Top;
textBox1.Height = textBox1.Height - scrollValue;
if (textBox1.Height > 30) {
textBox1.Height = textBox1.Height - 15;
textBox1.Top = textBox1.Top + 15;
} else {
if (textBox1.Height < getPosHours(0, 30))
textBox1.Height = getPosHours(0, 30);
}
textBox1.BackColor = Color.FromArgb(255, Selected.calData.ColorBgUnselected.R, Selected.calData.ColorBgUnselected.G, Selected.calData.ColorBgUnselected.B);
textBox1.ForeColor = Selected.calData.ColorBorder;
textBox1.Visible = true;
textBox1.Focus();
/* Rectangle cloneRect = new Rectangle(textBox1.Left, textBox1.Top + scrollValue, textBox1.Width, textBox1.Height);
Bitmap cloneBitmap = _offBmp.Clone(cloneRect, _offBmp.PixelFormat);
textBox1.BackgroundImage = cloneBitmap;
Graphics g = textBox1.CreateGraphics();
g.DrawImage(cloneBitmap, 0, scrollValue);
Graphics drawingSurface = Graphics.FromHwnd(Handle);
drawingSurface.DrawImage(cloneBitmap, 0, 0);
drawingSurface.Dispose();*/
}
void IsChangeText()
{
if (textBox1.Visible == true) {
// if (Selected != null) {
Selected.calTask.Summary = textBox1.Text;
// }
textBox1.Visible = false;
Repaint2();
}
}
void TextBox1Leave(object sender, System.EventArgs e)
{
IsChangeText();
}
private Keys keyPressed;
void TextBox1KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
keyPressed = e.KeyCode;
}
void TextBox1KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
switch (keyPressed) {
case Keys.Escape:
textBox1.Text = Selected.calTask.Summary;
IsChangeText();
e.Handled = true;
break;
case Keys.Enter:
IsChangeText();
e.Handled = true;
break;
}
}
#region Eventos del raton
#region Tratamiento del raton para meses
void MouseDownMonth(System.Windows.Forms.MouseEventArgs e)
{
// TODO: MouseDownMonth
}
void DoubleClickMonth(System.Windows.Forms.MouseEventArgs e)
{
// TODO: DoubleClickMonth
}
void MouseMoveMonth(System.Windows.Forms.MouseEventArgs e)
{
// TODO: MouseMoveMonth
}
void MouseUpMonth(System.Windows.Forms.MouseEventArgs e)
{
// TODO: MouseUpMonth
}
#endregion
#region Tratamiento del raton para semanas
void MouseMoveWeek(System.Windows.Forms.MouseEventArgs e)
{
int Y = e.Y + scrollValue;
if (lastMouse != null) {
if (e.Button != MouseButtons.Left) return;
if ((Selected==null) && (getPosDateTime(e.X, Y) != lastMouse.mouse)) {
lastMouse.type = 3;
}
System.DateTime dt = getPosDateTime(e.X, Y);
if (dt != System.DateTime.MinValue) {
if (lastMouse.newMouse != dt) {
lastMouse.newMouse = dt;
// If mouse move Event
if (lastMouse.type == 2) {
movingEvent = true;
Selected.Start = lastMouse.start;
Selected.Start += lastMouse.newMouse - lastMouse.mouse;
Selected.End = Selected.Start + lastMouse.duration;
Repaint2();
}
// If mouse resize Event
if (((lastMouse.type == 1) || (lastMouse.type == 4)) && (Selected != null)) {
movingEvent = true;
Selected.Start = lastMouse.start;
Selected.End = lastMouse.end;
Selected.End += lastMouse.newMouse - lastMouse.mouse;
if (Selected.End < Selected.Start) {
Selected.Start = Selected.End;
Selected.End = lastMouse.start;
}
Repaint2();
}
if (lastMouse.type == 3) {
if (Selected == null) {
this.Cursor = System.Windows.Forms.Cursors.HSplit;
lastMouse.type = 4;
InsertNewEvent();
}
}
}
}
} else {
int i = dataViewed.Count;
int cursor = 0;
RectangleViewed rv;
while (i != 0) {
rv = (RectangleViewed) dataViewed[--i];
if ((Y >= rv.y) && (Y<=rv.y2) && (e.X>=limitsH[rv.day]) && (e.X<=limitsH[rv.day+1])) {
cursor = 2;
if (Y <= (rv.y +5)) {
cursor = 1;
}
if (Y >= (rv.y2 -5)) {
cursor = 1;
}
break;
}
}
switch (cursor) {
case 0:
this.Cursor = System.Windows.Forms.Cursors.Default;
break;
case 1:
this.Cursor = System.Windows.Forms.Cursors.HSplit;
break;
case 2:
this.Cursor = System.Windows.Forms.Cursors.SizeAll;
break;
}
}
}
void MouseUpWeek(System.Windows.Forms.MouseEventArgs e)
{
movingEvent = false;
_offBmpBuffer = null;
if (lastMouse != null) {
if (lastMouse.type == 4) {
ChangeNameTask();
}
if (Selected != null) {
Selected.calTask.start = Selected.Start;
Selected.calTask.end = Selected.End;
}
lastMouse = null;
UpdateCalendar(true);
}
}
void DoubleClickWeek(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button != System.Windows.Forms.MouseButtons.Left) return;
if (Selected == null) {
lastMouse.type = 1;
// Sino cambia de dia, centra el evento
if (lastMouse.mouse.AddMinutes(-15).Day == lastMouse.mouse.Day)
lastMouse.mouse = lastMouse.mouse.AddMinutes(-15);
InsertNewEvent();
ChangeNameTask();
UpdateCalendar(true);
} else {
ChangeNameTask();
}
}
void MouseDownWeek(System.Windows.Forms.MouseEventArgs e)
{
int Y = e.Y + scrollValue;
if (e.Button != MouseButtons.Left) return;
bool selected = false;
// Resize
lastMouse = new OperationMouseMove();
lastMouse.type = 1;
lastMouse.mouse = getPosDateTime(e.X, Y);
if (lastMouse.mouse == System.DateTime.MinValue) {
lastMouse = null;
return;
}
{
System.DateTime aux = date;
date = new System.DateTime(lastMouse.mouse.Year, lastMouse.mouse.Month, lastMouse.mouse.Day);
if (aux != date) OnDateChanged();
}
Repaint2();
int i = dataViewed.Count;
RectangleViewed rv;
while (i != 0) {
rv = (RectangleViewed) dataViewed[--i];
if ((Y >= rv.y) && (Y<=rv.y2) && (e.X>=limitsH[rv.day]) && (e.X<=limitsH[rv.day+1])) {
Selected = rv.et;
foreach (CalData aux in CalWidget.Calendar.cd) {
aux.Selected = false;
}
rv.et.calData.Selected = true;
Repaint2();
selected = true;
lastMouse.duration = Selected.End - Selected.Start;
lastMouse.start = Selected.Start;
lastMouse.end = Selected.End;
lastMouse.type = 2;
if (Y <= (rv.y +5)) {
lastMouse.start = Selected.End;
lastMouse.end = Selected.Start;
lastMouse.type = 1;
}
if (Y >= (rv.y2 -5)) {
lastMouse.type = 1;
}
break;
}
}
// Si no se ve el calendario, no insertamos nada
CalData cd = null;
foreach (CalData cd2 in CalWidget.Calendar.cd)
if ((cd2.Selected) && (cd2.Visible))
cd = cd2;
if (cd != null) OnCalendarChanged();
if (cd == null) return;
if (selected == false) {
if (e.Clicks == 1) {
Selected = null;
lastMouse.type = 0;
}
}
}
#endregion
void CalContentMouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (style == CalendarStyle.Month)
MouseUpMonth(e);
else
MouseUpWeek(e);
}
void CalContentMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (style == CalendarStyle.Month)
MouseMoveMonth(e);
else
MouseMoveWeek(e);
}
void CalContentMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
IsChangeText();
if (style == CalendarStyle.Month)
MouseDownMonth(e);
else
MouseDownWeek(e);
if (e.Clicks == 2) {
if (style == CalendarStyle.Month)
DoubleClickMonth(e);
else
DoubleClickWeek(e);
}
}
#endregion
protected virtual void OnCalendarChanged()
{
if (CalendarChanged != null) CalendarChanged(this, System.EventArgs.Empty);
}
public event EventHandler CalendarChanged;
protected virtual void OnDateChanged()
{
if (DateChanged != null) DateChanged(this, System.EventArgs.Empty);
}
public event EventHandler DateChanged;
public void Translate(string key, string text)
{
switch (key) {
case "NewEvent":
labelNewEvent = text;
break;
}
}
}
}
|