constructor TYearPlanner.Create(AOwner: TComponent);
var
Dy,Mn,Yr: Word;
begin
Inherited Create(AOwner);
HintWin := THintWindow.Create(Self);
fStringList := TStringList.Create;
fPrintOptions := TPrintOptions.Create(nil);
CopyRightPtr := @CopyRightStr;
Width := 615;
Height := 300;
Color := clGray;
DecodeDate(Date, Yr, Mn, Dy);
fAbout := abNone;
fAllowSelections := True;
fDayColor := clWhite;
{$IFDEF WIN32}
fEndEllipsis := False;
{$ENDIF}
fFlatCells := True;
fGridLines := True;
fHeadingColor := clGray;
fHintColor := clYellow;
fHintDelay := 0;
fLongHint := True;
fMonthColor := clGray;
{$IFDEF WIN32}
fMonthButtons := False;
{$ENDIF}
fNoDayColor := clSilver;
fNoDayPriority := False;
fSelectionColor := clBlue;
fSelectionStyle := ypNormal;
{$IFDEF WIN32}
fSeperator := True;
fSoftBorder := False;
{$ENDIF}
fShowDefaultHint := True;
fStartDayOfWeek := ypMonday;
fStretchImages := False;
fTodayCircleColour := clMaroon;
fTodayCircleFilled := False;
fTodayTextColour:= clWhite;
fUseBitmap := True;
fUseFreeSpace := True;
fWeekendColor := clGray;
fWeekendHeadingColor := clSilver;
fYear := Yr;
fYearColor:= clGray;
{$IFDEF WIN32}
fYearNavigators := True;
{$ENDIF}
fStartDate := Now;
fEndDate := Now;
hUpdating := False;
hWaiting := False;
hWaitingToDestroy := False;
CurrentDate.Day := 0;
CurrentDate.Month := 0;
OldX := -1;
OldY := -1;
hPrinting := False;
hSelecting := ypNotSelecting;
fControl := TBitmap.Create;
fDayFont := TFont.Create;
fHintFont := TFont.Create;
fMonthFont := TFont.Create;
fSelectionFont := TFont.Create;
fYearFont := TFont.Create;
fGridPen := TPen.Create;
fGridPen.OnChange:= OnGridPenChange;
SetupHeadings;
CalculateCalendar;
CalculateData;
CalculateSizes;
end;
destructor TYearPlanner.Destroy;
begin
fPrintOptions.Free;
fStringList.Free;
hWaitingToDestroy := True;
if hUpdating then HintWin.ReleaseHandle;
HintWin.Free;
fControl.Free;
fGridPen.OnChange:= nil;
fGridPen.Free;
fYearFont.Free;
fSelectionFont.Free;
fMonthFont.Free;
fHintFont.Free;
fDayFont.Free;
Inherited Destroy;
end;
procedure TYearPlanner.WMLButtonDblClk(var Message: TWMLButtonDblClk);
begin
if (hSelecting = ypSelected) and (Assigned(fOnYearDblClick)) then
fOnYearDblClick(StDay,EnDay,EnMonth,StMonth,fStartDate,fEndDate);
end;
procedure TYearPlanner.WMLButtonDown(var Message: TWMLButtonDown);
var
Pt,Temp: TPoint;
tX,tY: Integer;
fOnClick: TNotifyEvent;
begin
Inherited;
if fYearNavigators then
begin
GetCursorPos(Pt);
Pt := ScreenToClient(Pt);
if PtInRect(fYearNavLeft,Pt) then
begin
Year := Year - 1;
Invalidate;
Exit;
end;
if PtInRect(fYearNavRight,Pt) then
begin
Year := Year + 1;
Invalidate;
Exit;
end;
end;
Temp := ClientToScreen(Point(Message.XPos,Message.YPos));
if not (FindDragTarget(Temp, True) = Self) then Exit;
XYToCell(Message.XPos,Message.YPos,tX,tY);
if ((tx = 0) or (ty = 0) or (cells[tx,ty] = '')) and (fSelectionStyle = ypNormal) then
begin
ClearSelection;
Exit;
end;
fOnClick := OnClick;
if not Assigned(fOnClick) then hSelecting := ypSelecting;
InDay := tX;
InMonth := tY;
StDay := InDay;
StMonth := InMonth;
EnDay := InDay;
EnMonth := InMonth;
if fSelectionStyle = ypNormal then
begin
fStartDate := EncodeDate(fYear, ty, StrToInt(Cells[tx,ty]));
fEndDate := fStartDate;
end;
Invalidate;
end;
procedure TYearPlanner.WMLButtonUp(var Message: TWMLButtonUp);
var
CountX,CountY: Integer;
begin
hSelecting := ypNotSelecting;
for CountX := StDay to EnDay do
for CountY := StMonth to EnMonth do
if Cells[CountX,CountY] <> '' then
hSelecting := ypSelected;
ProcessSelection;
StartDate := fStartDate;
EndDate := fEndDate;
if Assigned(fOnSelectionEnd) then fOnSelectionEnd(Self);
Inherited;
end;
procedure TYearPlanner.WMRButtonDown(var Message: TWMRButtonDown);
begin
Inherited;
if (hSelecting = ypSelected) and (Assigned(fOnYearRightClick)) then
fOnYearRightClick(StDay,EnDay,EnMonth,StMonth,fStartDate, fEndDate);
end;
procedure TYearPlanner.WMMouseMove(var Message: TWMMouseMove);
var
Temp: TPoint;
HintText, TmpHint, TmpText: String;
HintRect: TRect;
HDelay : {$IFDEF WIN32}Cardinal{$ELSE}LongInt{$ENDIF};
HintH, HintLines, HintSH, HintW: Integer;
Dy,Mn: Byte;
swapTmp:integer;
begin
if hWaitingToDestroy then Exit;
Inherited;
Temp := ClientToScreen(Point(Message.XPos,Message.YPos));
if not (FindDragTarget(Temp, True) = Self) then Exit;
XYToCell(Message.XPos,Message.YPos,cX,cY);
if hSelecting = ypSelecting then
begin
StDay := InDay;
StMonth := InMonth;
EnDay := cX;
EnMonth := cY;
if fSelectionStyle = ypNormal then
begin
if (StMonth > EnMonth) or ((StMonth = EnMonth) and (StDay > EnDay)) then
begin
swapTmp := StDay;
StDay := EnDay;
EnDay := swapTmp;
swapTmp := StMonth;
StMonth := EnMonth;
EnMonth := swapTmp;
end;
end
else
begin
if StDay > EnDay then
begin
swapTmp := StDay;
StDay := EnDay;
EnDay := swapTmp;
end;
if StMonth > EnMonth then
begin
swapTmp := StMonth;
StMonth := EnMonth;
EnMonth := swapTmp;
end;
end;
ProcessSelection;
Invalidate;
Exit;
end;
if ((OldX = cX) and (OldY = cY)) or (cX = 0) or (cY = 0) or
(Cells[cX,cY] = '') then Exit;
CurrentDate.Day := StrToInt(Cells[cX,cY]);
CurrentDate.Month := cY;
if not (Application.ShowHint and (ShowHint or ParentShowHint)) then Exit;
if (CellData[cY,CurrentDate.Day].CellHint = '') and (not fShowDefaultHint) then Exit;
if hUpdating then
begin
hWaiting := True;
Exit;
end;
OldX := cX;
OldY := cY;
Dy := CurrentDate.Day;
Mn := CurrentDate.Month;
HintText := CellData[Mn,Dy].CellHint;
if HintText = '' then
begin
if fLongHint then
HintText := FormatDateTime(LongDateFormat, EncodeDate(Year, Mn, Dy))
else
HintText := FormatDateTime(ShortDateFormat, EncodeDate(Year, Mn, Dy));
end;
HintDate := CellData[Mn,Dy].CellDate;
hUpdating := True;
hWaiting := False;
TmpHint := HintText;
if TmpHint[length(TmpHint)] <> #13 then
TmpHint := TmpHint + #13;
HintLines := 0;
HintW := 0;
repeat
Inc(HintLines);
TmpText := Copy(TmpHint,1,Pos(#13,TmpHint)-1);
if HintWin.Canvas.TextWidth(TmpText) + 5 > HintW then
HintW := HintWin.Canvas.TextWidth(TmpText) + 5;
Delete(TmpHint,1,Pos(#13,TmpHint));
until Pos(#13,TmpHint) = 0;
HintH := (HintWin.Canvas.TextHeight('0') * HintLines) + 3;
HintSH := HintWin.Canvas.TextHeight('0') + 3;
if fHintDelay = 0 then HDelay := Application.HintPause else
HDelay := fHintDelay;
HintRect := Rect(Temp.X, Temp.Y + HintSH, Temp.X + HintW, Temp.Y + HintH + HintSH);
HintWin.Color := fHintColor;
HintWin.Canvas.Font.Assign(fHintFont);
HintWin.ActivateHint(HintRect, HintText);
FirstTickCount := GetTickCount;
repeat
Application.ProcessMessages;
if csDestroying in ComponentState then Exit;
if (not Parent.Showing) or (Application.Terminated) then Break;
if (hSelecting = ypSelecting) or (hWaiting) or (hWaitingToDestroy) then Break;
until (GetTickCount - FirstTickCount > HDelay);
HintWin.ReleaseHandle;
hUpdating := False;
end;
procedure TYearPlanner.WMSize(var Message:TWMSize);
begin
CalculateNavigators;
end;
procedure TYearPlanner.SetColorAtDate(dt: TDateTime; cellColor: TColor; UpdateControl: Boolean);
var
mm,dd,yy: word;
begin
DecodeDate(dt, yy, mm, dd);
CellData[mm, dd].CellColor := cellColor;
CellData[mm, dd].CustomColor := True;
if UpdateControl then Invalidate;
end;
procedure TYearPlanner.SetFontAtDate(dt: TDateTime; cellFont: TFont; UpdateControl: Boolean);
var
mm,dd,yy: word;
begin
DecodeDate(dt, yy, mm, dd);
CellData[mm, dd].CellFont := cellFont;
CellData[mm, dd].CustomFont := True;
if UpdateControl then Invalidate;
end;
procedure TYearPlanner.SetHintAtDate(dt: TDateTime; cellHint: String; UpdateControl: Boolean);
var
mm,dd,yy: word;
begin
DecodeDate(dt, yy, mm, dd);
CellData[mm, dd].CellHint := cellHint;
if UpdateControl then Invalidate;
end;
{$IFDEF WIN32}
procedure TYearPlanner.SetImageAtDate(dt: TDateTime; cellImage: Integer; UpdateControl: Boolean);
var
mm,dd,yy: word;
begin
DecodeDate(dt, yy, mm, dd);
CellData[mm, dd].CellImage := cellImage;
if UpdateControl then Invalidate;
end;
{$ENDIF}
function TYearPlanner.GetCellData(dt: TDateTime): TCellData;
var
mm,dd,yy: word;
begin
DecodeDate(dt, yy, mm, dd);
Result := CellData[mm, dd];
end;
procedure TYearPlanner.Print;
var
TempCap: array[0..255] of char;
pHeight, pWidth: Integer;
DrawFlags: Longint;
TheRect: TRect;
Ratio: Extended;
begin
hPrinting := True;
with fPrintOptions do
begin
Printer.Orientation := fPrinterOrientation;
pWidth := Printer.PageWidth - fPrinterLeftMargin - fPrinterRightMargin;
pHeight := Printer.PageHeight - fPrinterTopMargin - fPrinterBottomMargin;
PrinterPageWidth := round(pWidth * (fPrintReductionSize / 100));
PrinterPageHeight := round(pHeight * (fPrintReductionSize / 100));
if PreserveAspect then
begin
Ratio := Height/Width;
PrinterPageHeight := round(Ratio * PrinterPageWidth);
if PrinterPageHeight > pHeight then
begin
PrinterPageWidth:= round(PrinterPageWidth*(pHeight/PrinterPageHeight));
PrinterPageHeight:= round(pHeight);
end;
end;
PrinterLeftMargin := fPrinterLeftMargin;
PrinterTopMargin := fPrinterTopMargin;
PrinterRightMargin := fPrinterRightMargin;
PrinterBottomMargin := fPrinterBottomMargin;
end;
try
Printer.BeginDoc;
self.Paint;
with fPrintOptions, Printer.Canvas do
begin
if PrintHeader.Caption <> '' then
begin
StrPCopy(TempCap, PrintHeader.Caption);
Font := PrintHeader.Font;
TheRect := Rect(PrinterLeftMargin, 0, PrinterLeftMargin + pWidth,
PrinterTopMargin);
DrawFlags := DT_VCENTER or DT_SINGLELINE;
case PrintHeader.Alignment of
taLeftJustify: DrawFlags := DrawFlags or DT_LEFT;
taCenter: DrawFlags := DrawFlags or DT_CENTER;
taRightJustify: DrawFlags := DrawFlags or DT_RIGHT;
end;
DrawText(Handle, TempCap, StrLen(TempCap), TheRect, DrawFlags);
end;
if PrintFooter.Caption <> '' then
begin
StrPCopy(TempCap, PrintFooter.Caption);
Font := PrintFooter.Font;
TheRect := Rect(PrinterLeftMargin, PrinterTopMargin + pHeight,
PrinterLeftMargin + pWidth, PrinterTopMargin + pHeight + PrinterBottomMargin);
DrawFlags := DT_VCENTER or DT_SINGLELINE;
case PrintFooter.Alignment of
taLeftJustify: DrawFlags := DrawFlags or DT_LEFT;
taCenter: DrawFlags := DrawFlags or DT_CENTER;
taRightJustify: DrawFlags := DrawFlags or DT_RIGHT;
end;
DrawText(Handle, TempCap, StrLen(TempCap), TheRect, DrawFlags);
end;
end;
finally
Printer.EndDoc;
hPrinting := False;
end;
end;
function TYearPlanner.GetStartDate: TDateTime;
begin
GetStartDate := fStartDate;
end;
function TYearPlanner.GetEndDate: TDateTime;
begin
GetEndDate := fEndDate;
end;
function TYearPlanner.IsSelected(date: TDateTime): Boolean;
var
mm,dd,yy: word;
begin
DecodeDate(date, yy, mm, dd);
IsSelected := CellData[mm, dd].Selected;
end;
procedure TYearPlanner.ClearSelection;
begin
StDay := 0;
StMonth := 0;
EnDay := 0;
EnMonth := 0;
fStartDate := Now;
fEndDate := Now;
Invalidate;
end;
procedure TYearPlanner.SelectCells(sDate, eDate: TDateTime);
var
eD, eM, eY, sD, sM, sY: word;
CountX: Integer;
tmpDate: TDateTime;
begin
if sDate > eDate then
begin
tmpDate := sDate;
sDate := eDate;
eDate := tmpDate;
end;
DecodeDate(sDate, sY, sM, sD);
DecodeDate(eDate, eY, eM, eD);
for CountX := 1 to 37 do
if StrToIntDef(Cells[CountX, sM],0) = sD then
begin
StDay := CountX;
StMonth := sM;
fStartDate := sDate;
end;
for CountX := 1 to 37 do
if StrToIntDef(Cells[CountX, eM],0) = eD then
begin
EnDay := CountX;
EnMonth := eM;
fEndDate := eDate;
end;
Invalidate;
Exit;
end;
procedure TYearPlanner.SelectWeek(aWeek: Integer);
var
eDate, sDate: TDateTime;
begin
sDate := FindFirstWeek(Year) + ((aWeek - 1) * 7);
eDate := sDate + 6;
SelectCells(sDate, eDate);
end;
procedure TYearPlanner.ClearCells;
var
mm, dd: Integer;
begin
for mm := 1 to 12 do
for dd := 1 to 31 do
with CellData[mm, dd] do
begin
CellColor := $00000000;
CellFont := fDayFont;
CellHint := '';
CustomColor := False;
CustomFont := False;
{$IFDEF WIN32}
CellImage := -1;
{$ENDIF}
Tag := -1;
end;
Invalidate;
end;
function TYearPlanner.WeekNumber(aDate: TDateTime): Integer;
var
sDay, sMonth, sYear: Word;
begin
DecodeDate(aDate, sYear, sMonth, sDay);
Result := Trunc((StrToInt(FloatToStr(aDate - FindFirstWeek(sYear))) / 7) + 1);
end;
procedure Register;
begin
RegisterComponents('Samples', [TYearPlanner]);
end;
end.