// 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.Collections;
namespace CalWidget{
public class Calendar : System.Windows.Forms.Control {
private System.Windows.Forms.VScrollBar vScrollBar1;
CalContent cc;
CalTitle ct;
public static ArrayList cd;
uint hoursViewed;
public Calendar()
{
cd = new ArrayList();
hoursViewed = 12;
this.vScrollBar1 = new System.Windows.Forms.VScrollBar();
this.vScrollBar1.ValueChanged += new System.EventHandler(this.VScrollBar1ValueChanged);
this.Controls.Add(this.vScrollBar1);
cc = new CalContent();
ct = new CalTitle();
this.Controls.Add(cc);
this.Controls.Add(ct);
this.Resize += new System.EventHandler(this.CalendarResize);
cc.KeyDown += new System.Windows.Forms.KeyEventHandler(CcKeyDown);
cc.DateChanged += new System.EventHandler(this.CCDateChanged);
cc.CalendarChanged += new System.EventHandler(this.CCCalendarChanged);
ct.StyleChanged += new System.EventHandler(this.CTStyleChanged);
ct.DefineContent(cc);
cc.DefineTitle(ct);
}
public ArrayList CalendarData {
get {
return cd;
}
}
public CalData NewCalData()
{
CalData aux = new CalData();
cd.Add(aux);
return aux;
}
public void RemoveCalendar(CalData aux)
{
cd.Remove(aux);
UpdateCalendar();
}
public System.DateTime Date {
get {
return cc.Date;
}
set {
cc.Date = value;
ct.UpdateCalendar(true);
}
}
public uint FirstDayOfWeek {
get {
return cc.FirstDayOfWeek;
}
set {
cc.FirstDayOfWeek = value;
ct.FirstDayOfWeek = value;
}
}
public int DaysByWeek {
get {
return cc.DaysByWeek;
}
set {
int auxValue = value;
if (auxValue != 5) auxValue=7;
cc.DaysByWeek = auxValue;
ct.DaysByWeek = auxValue;
}
}
public int StartHour {
get {
return cc.StartHour;
}
set {
cc.StartHour = value;
}
}
public int EndHour {
get {
return cc.EndHour;
}
set {
cc.EndHour = value;
}
}
public int TimeFormat {
get {
return cc.TimeFormat;
}
set {
cc.TimeFormat = value;
}
}
public uint HoursViewed {
get {
return hoursViewed;
}
set {
if (value == hoursViewed) return;
uint auxValue;
auxValue = value;
if (auxValue>24) auxValue=24;
if (auxValue<6) auxValue=6;
hoursViewed = auxValue;
Resize2();
}
}
public void UpdateCalendar()
{
cc.UpdateCalendar(true);
ct.UpdateCalendar(true);
}
public CalendarStyle Style {
get {
return cc.Style;
}
set {
cc.Style = value;
ct.Style = value;
Resize2();
}
}
public int Alpha {
get {
return cc.Alpha;
}
set {
cc.Alpha = value;
}
}
void Resize2()
{
if ((this.ClientRectangle.Height<1)||(this.ClientRectangle.Width<1)) return;
vScrollBar1.SuspendLayout();
cc.SuspendLayout();
ct.SuspendLayout();
if (cc.Style != CalendarStyle.Month)
ct.SetBounds(0,0,this.ClientRectangle.Width, 41);
else
ct.SetBounds(0,0,this.ClientRectangle.Width, 16);
vScrollBar1.Visible = ((cc.Style != CalendarStyle.Month) && (hoursViewed != 24));
if ((cc.Style != CalendarStyle.Month) && (hoursViewed != 24)) {
vScrollBar1.Left = this.ClientRectangle.Width - vScrollBar1.Width;
vScrollBar1.Top = ct.Height;
vScrollBar1.Height = this.ClientRectangle.Height - ct.Height;
if (vScrollBar1.Height<1) vScrollBar1.Height=1;
cc.SetBounds(0, ct.Height, this.ClientRectangle.Width - vScrollBar1.Width, vScrollBar1.Height);
} else {
cc.SetBounds(0, ct.Height, this.ClientRectangle.Width, this.ClientRectangle.Height - ct.Height);
}
if (cc.Style == CalendarStyle.Month) {
if (cc.ScrollMax != cc.Height)
cc.ScrollMax = cc.Height;
} else {
if (cc.ScrollMax != ((cc.Height*24)/hoursViewed))
cc.ScrollMax = (int) ((cc.Height*24)/hoursViewed);
}
vScrollBar1.LargeChange = ((cc.ScrollMax - cc.Height) * cc.Height) / cc.ScrollMax;
vScrollBar1.Maximum = cc.ScrollMax - cc.Height;
SetScroll();
ct.ResumeLayout(true);
vScrollBar1.ResumeLayout(true);
cc.ResumeLayout(true);
}
void CalendarResize(object sender, System.EventArgs e)
{
Resize2();
}
void CCDateChanged(object sender, System.EventArgs e)
{
OnDateChanged();
}
void CTStyleChanged(object sender, System.EventArgs e)
{
OnDateChanged();
OnStyleChanged();
}
void CCCalendarChanged(object sender, System.EventArgs e)
{
OnCalendarChanged();
}
void SetScroll()
{
int aux = ((vScrollBar1.Value * vScrollBar1.Maximum)/(vScrollBar1.Maximum - vScrollBar1.LargeChange+1)) ;
if (aux > vScrollBar1.Maximum)
aux = vScrollBar1.Maximum;
cc.ScrollValue = aux;
}
void VScrollBar1ValueChanged(object sender, System.EventArgs e)
{
SetScroll();
}
void CcKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
switch (e.KeyCode) {
case System.Windows.Forms.Keys.Delete:
foreach (CalData item in cd) {
if (cc.Selected != null) item.DataList.Remove(cc.Selected.calTask);
}
UpdateCalendar();
break;
}
}
protected virtual void OnDateChanged()
{
if (DateChanged != null) DateChanged(this, System.EventArgs.Empty);
}
protected virtual void OnCalendarChanged()
{
if (CalendarChanged != null) CalendarChanged(this, System.EventArgs.Empty);
}
protected virtual void OnStyleChanged()
{
if (StyleChanged != null) StyleChanged(this, System.EventArgs.Empty);
}
public event EventHandler CalendarChanged;
new public event EventHandler StyleChanged;
public event EventHandler DateChanged;
public void Translate(string key, string text)
{
switch (key) {
case "NewEvent":
cc.Translate(key, text);
break;
case "DateFormatShort":
case "DateFormatNormal":
case "DateFormatLong":
case "DateFormatLong2":
ct.Translate(key, text);
// ct.UpdateCalendar(true);
break;
}
}
}
public enum CalendarStyle { Day, Week, Month}
}
|